Example #1
0
void test_open_config_close(void) {
    serial_t serial;
    uint32_t baudrate;
    unsigned int databits;
    serial_parity_t parity;
    unsigned int stopbits;
    bool xonxoff;
    bool rtscts;

    ptest();

    passert(serial_open(&serial, device, 115200) == 0);

    /* Check default settings */
    passert(serial_get_baudrate(&serial, &baudrate) == 0);
    passert(baudrate == 115200);
    passert(serial_get_databits(&serial, &databits) == 0);
    passert(databits == 8);
    passert(serial_get_parity(&serial, &parity) == 0);
    passert(parity == PARITY_NONE);
    passert(serial_get_stopbits(&serial, &stopbits) == 0);
    passert(stopbits == 1);
    passert(serial_get_xonxoff(&serial, &xonxoff) == 0);
    passert(xonxoff == false);
    passert(serial_get_rtscts(&serial, &rtscts) == 0);
    passert(rtscts == false);

    /* Change some stuff around */
    passert(serial_set_baudrate(&serial, 4800) == 0);
    passert(serial_get_baudrate(&serial, &baudrate) == 0);
    passert(baudrate == 4800);
    passert(serial_set_baudrate(&serial, 9600) == 0);
    passert(serial_get_baudrate(&serial, &baudrate) == 0);
    passert(baudrate == 9600);
    passert(serial_set_databits(&serial, 7) == 0);
    passert(serial_get_databits(&serial, &databits) == 0);
    passert(databits == 7);
    passert(serial_set_parity(&serial, PARITY_ODD) == 0);
    passert(serial_get_parity(&serial, &parity) == 0);
    passert(parity == PARITY_ODD);
    passert(serial_set_stopbits(&serial, 2) == 0);
    passert(serial_get_stopbits(&serial, &stopbits) == 0);
    passert(stopbits == 2);
    passert(serial_set_xonxoff(&serial, true) == 0);
    passert(serial_get_xonxoff(&serial, &xonxoff) == 0);
    passert(xonxoff == true);
    #if 0
    passert(serial_set_rtscts(&serial, true) == 0);
    passert(serial_get_rtscts(&serial, &rtscts) == 0);
    passert(rtscts == true);
    #endif
    /* Test serial port may not support rtscts */

    passert(serial_close(&serial) == 0);
}
Example #2
0
int serial_open(urg_serial_t *serial, const char *device, long baudrate)
{
    // COM10 以降への対応用
    enum { NameLength = 11 };
    char adjusted_device[NameLength];

    serial_initialize(serial);

    /* COM ポートを開く */
    _snprintf(adjusted_device, NameLength, "\\\\.\\%s", device);
    serial->hCom = CreateFileA(adjusted_device, GENERIC_READ | GENERIC_WRITE,
                               0, NULL, OPEN_EXISTING,
                               FILE_ATTRIBUTE_NORMAL, NULL);

    if (serial->hCom == INVALID_HANDLE_VALUE) {
        // !!! store error_message buffer
        //printf("open failed: %s\n", device);
        return -1;
    }

    /* 通信サイズの更新 */
    SetupComm(serial->hCom, 4096 * 8, 4096);

    /* ボーレートの変更 */
    serial_set_baudrate(serial, baudrate);

    /* シリアル制御構造体の初期化 */
    serial->has_last_ch = False;

    /* タイムアウトの設定 */
    serial->current_timeout = 0;
    set_timeout(serial, serial->current_timeout);

    return 0;
}
Example #3
0
int dump_mem(uint32_t addr, uint32_t length)
{
    uint32_t left = length;
    uint8_t data[96];
    uint8_t chars[17];
    int i;

    serial_set_baudrate(50);
    stm32w_reset();
    serial_set_baudrate(115200);
    stm32w_bl_ping();

    chars[16]=0;

    printf("%08x: ", addr);
    while (left>0) {
        if (stm32w_bl_read_mem(addr+length-left, data, (left>96) ? 96 : left)<0) {
            printf("\nMemory read error in %u byte block starting at 0x%08x\n",
                   (left>96) ? 96 : left, addr + length - left);
            exit(1);
        }
        for(i=0; i<((left>96) ? 96 : left); i++) {
            printf("%02x ", data[i]);
            chars[i%16]  = ((data[i]<0x20) || (data[i]>0x7e)) ? '.':data[i];
            if(!((i+1)%8))
                printf(" ");
            if(!((i+1)%16)) {
                printf("%s", chars);
                if(left-i-1)
                    printf("\n%08x: ", addr + length - left + i + 1);

            }
        }
        left -= ((left>96) ? 96 : left);
    }
    chars[i%16] = 0;
    while (((i++))%16) {
        printf("   ");
        if((i%16)==8)
            printf(" ");
    }
    printf(" %s\n", chars);
    return 0;
}
Example #4
0
int flash_app(uint32_t addr, char *filename)
{
    struct stat s;
    int fd, i;
    uint8_t buff[MAX_XFER_SIZE];

    fd = open(filename, O_RDONLY);
    if (fd < 0) {
        printf("Cannot open file %s\n", filename);
        exit(1);
    }
    fstat(fd, &s);

    serial_set_baudrate(50);
    stm32w_reset();
    serial_set_baudrate(115200);
    stm32w_bl_ping();

    printf("Erasing flash pages %i to %i ...", 0, (int) s.st_size/1024+1);
    if(stm32w_bl_erase(0,(int) s.st_size/1024+1)) {
        printf("Failed to erase flash.");
        exit(1);
    }
    printf(", done.\n");
    printf("Writing %i bytes from %s to flash:\n",(int) s.st_size, filename);
    for(i=0; i<s.st_size; i+=256) {
        memset(buff, 0xFF, 256);
        if (read(fd, buff, 256) <= 0)
            break;
        if(stm32w_bl_write_mem(addr+i, buff, 256)) {
            printf("Failed to write block to address 0x%08x\n", addr + i);
            exit(1);
        }
        printf("\rWriting 0x%08x (%u %%)...", addr + i,
               i * 100 / (int) s.st_size + 1);
        fflush(stdout);
    }
    printf(", done.\n");
    return 0;
}
Example #5
0
void test_interactive(void) {
    serial_t serial;
    uint8_t buf[] = "Hello World";

    ptest();

    passert(serial_open(&serial, device, 4800) == 0);

    printf("Starting interactive test. Get out your logic analyzer, buddy!\n");
    printf("Press enter to continue...\n");
    getc(stdin);

    printf("Press enter to start transfer...");
    getc(stdin);
    passert(serial_write(&serial, buf, sizeof(buf)) == sizeof(buf));
    printf("Serial transfer baudrate 4800, 8n1 occurred? y/n\n");
    passert(getc_yes());

    passert(serial_set_baudrate(&serial, 9600) == 0);

    printf("Press enter to start transfer...");
    getc(stdin);
    passert(serial_write(&serial, buf, sizeof(buf)) == sizeof(buf));
    printf("Serial transfer baudrate 9600, 8n1 occurred? y/n\n");
    passert(getc_yes());

    passert(serial_set_baudrate(&serial, 115200) == 0);

    printf("Press enter to start transfer...");
    getc(stdin);
    passert(serial_write(&serial, buf, sizeof(buf)) == sizeof(buf));
    printf("Serial transfer baudrate 115200, 8n1 occurred? y/n\n");
    passert(getc_yes());

    passert(serial_close(&serial) == 0);
}
Example #6
0
/*
 打开串口
 */
int serial_open(urg_serial_t *serial, const char *device, long baudrate)
{
    int flags = 0;
    int ret = 0;

    serial_initialize(serial);

#ifndef URG_MAC_OS
    enum { O_EXLOCK = 0x0 }; /*  Not used in Linux, used as dummy */
#endif
	/* Linux系统打开一个设备,详细的可以看 UNIX高级环境编程 */
    serial->fd = open(device, O_RDWR | O_EXLOCK | O_NONBLOCK | O_NOCTTY);
    if (serial->fd < 0) {
        /*  设备打开失败 */
        //strerror_r(errno, serial->error_string, ERROR_MESSAGE_SIZE);
        return -1;
    }

    flags = fcntl(serial->fd, F_GETFL, 0);
    fcntl(serial->fd, F_SETFL, flags & ~O_NONBLOCK);

    /*  串口初始化部分  */
    tcgetattr(serial->fd, &serial->sio);
    serial->sio.c_iflag = 0;
    serial->sio.c_oflag = 0;
	/* 下面的大写字母表示的都是Linux系统的宏定义,没有必要全部搞懂,看一下了解一下 */
    serial->sio.c_cflag &= ~(CSIZE | PARENB | CSTOPB);
    serial->sio.c_cflag |= CS8 | CREAD | CLOCAL;
    serial->sio.c_lflag &= ~(ICANON | ECHO | ISIG | IEXTEN);

    serial->sio.c_cc[VMIN] = 0;
    serial->sio.c_cc[VTIME] = 0;
	/* 串口设置波特率 */
    ret = serial_set_baudrate(serial, baudrate);
    if (ret < 0) {
        return ret;
    }

    /*  Initializes serial control structures */
    serial->has_last_ch = False;

    return 0;
}
Example #7
0
int serial_open(urg_serial_t *serial, const char *device, long baudrate)
{
    int flags = 0;
    int ret = 0;

    serial_initialize(serial);

#ifndef URG_MAC_OS
    enum { O_EXLOCK = 0x0 }; /*  Not used in Linux, used as dummy */
#endif
    serial->fd = open(device, O_RDWR | O_EXLOCK | O_NONBLOCK | O_NOCTTY);
    if (serial->fd < 0) {
        /*  Connection failed */
        //strerror_r(errno, serial->error_string, ERROR_MESSAGE_SIZE);
        return -1;
    }

    flags = fcntl(serial->fd, F_GETFL, 0);
    fcntl(serial->fd, F_SETFL, flags & ~O_NONBLOCK);

    /*  Initializes serial communication  */
    tcgetattr(serial->fd, &serial->sio);
    serial->sio.c_iflag = 0;
    serial->sio.c_oflag = 0;
    serial->sio.c_cflag &= ~(CSIZE | PARENB | CSTOPB);
    serial->sio.c_cflag |= CS8 | CREAD | CLOCAL;
    serial->sio.c_lflag &= ~(ICANON | ECHO | ISIG | IEXTEN);

    serial->sio.c_cc[VMIN] = 0;
    serial->sio.c_cc[VTIME] = 0;

    ret = serial_set_baudrate(serial, baudrate);
    if (ret < 0) {
        return ret;
    }

    /*  Initializes serial control structures */
    serial->has_last_ch = False;

    return 0;
}
Example #8
0
static int lua_serial_newindex(lua_State *L) {
    serial_t *serial;
    const char *field;

    serial = luaL_checkudata(L, 1, "periphery.Serial");

    if (!lua_isstring(L, 2))
        return lua_serial_error(L, SERIAL_ERROR_ARG, 0, "Error: unknown property");

    field = lua_tostring(L, 2);

    if (strcmp(field, "fd") == 0)
        return lua_serial_error(L, SERIAL_ERROR_ARG, 0, "Error: immutable property");
    else if (strcmp(field, "baudrate") == 0) {
        uint32_t baudrate;
        int ret;

        lua_serial_checktype(L, 3, LUA_TNUMBER);
        baudrate = lua_tounsigned(L, 3);

        if ((ret = serial_set_baudrate(serial, baudrate)) < 0)
            return lua_serial_error(L, ret, serial_errno(serial), "Error: %s", serial_errmsg(serial));

        return 0;
    } else if (strcmp(field, "databits") == 0) {
        int databits;
        int ret;

        lua_serial_checktype(L, 3, LUA_TNUMBER);
        databits = lua_tounsigned(L, 3);

        if ((ret = serial_set_databits(serial, databits)) < 0)
            return lua_serial_error(L, ret, serial_errno(serial), "Error: %s", serial_errmsg(serial));

        return 0;
    } else if (strcmp(field, "parity") == 0) {
        const char *s;
        serial_parity_t parity;
        int ret;

        lua_serial_checktype(L, 3, LUA_TSTRING);
        s = lua_tostring(L, 3);

        if (strcmp(s, "none") == 0)
            parity = PARITY_NONE;
        else if (strcmp(s, "odd") == 0)
            parity = PARITY_ODD;
        else if (strcmp(s, "even") == 0)
            parity = PARITY_EVEN;
        else
            return lua_serial_error(L, SERIAL_ERROR_ARG, 0, "Error: invalid parity, should be 'none', 'even', or 'odd'");

        if ((ret = serial_set_parity(serial, parity)) < 0)
            return lua_serial_error(L, ret, serial_errno(serial), "Error: %s", serial_errmsg(serial));

        return 0;
    } else if (strcmp(field, "stopbits") == 0) {
        int stopbits;
        int ret;

        lua_serial_checktype(L, 3, LUA_TNUMBER);
        stopbits = lua_tounsigned(L, 3);

        if ((ret = serial_set_stopbits(serial, stopbits)) < 0)
            return lua_serial_error(L, ret, serial_errno(serial), "Error: %s", serial_errmsg(serial));

        return 0;
    } else if (strcmp(field, "xonxoff") == 0) {
        bool xonxoff;
        int ret;

        lua_serial_checktype(L, 3, LUA_TBOOLEAN);
        xonxoff = lua_toboolean(L, 3);

        if ((ret = serial_set_xonxoff(serial, xonxoff)) < 0)
            return lua_serial_error(L, ret, serial_errno(serial), "Error: %s", serial_errmsg(serial));

        return 0;
    } else if (strcmp(field, "rtscts") == 0) {
        bool rtscts;
        int ret;

        lua_serial_checktype(L, 3, LUA_TBOOLEAN);
        rtscts = lua_toboolean(L, 3);

        if ((ret = serial_set_rtscts(serial, rtscts)) < 0)
            return lua_serial_error(L, ret, serial_errno(serial), "Error: %s", serial_errmsg(serial));

        return 0;
    }

    return lua_serial_error(L, SERIAL_ERROR_ARG, 0, "Error: unknown property");
}
zb_status zigbee_protocol_setBaudRate(zigbee_obj* obj, uint32_t baudRate)
{
  zb_handle_status handle_status;
  zb_status status;
  zigbee_baudrate zbBaudRate;

  assert(obj != NULL);
  status = ZB_CMD_FAILED;

  zigbee_protocol_incrementFrameID(obj);

  switch (baudRate)
  {
    case 1200:
      zbBaudRate = ZB_BD_1200;
      break;

    case 2400:
      zbBaudRate = ZB_BD_2400;
      break;

    case 4800:
      zbBaudRate = ZB_BD_4800;
      break;

    case 9600:
    default:
      zbBaudRate = ZB_BD_9600;
      break;

    case 19200:
      zbBaudRate = ZB_BD_19200;
      break;

    case 38400:
      zbBaudRate = ZB_BD_38400;
      break;

    case 57600:
      zbBaudRate = ZB_BD_57600;
      break;

    case 115200:
      zbBaudRate = ZB_BD_115200;
      break;
  }

  obj->sizeOfFrameToSend = zigbee_encode_setBaudRate(obj->frame, 50, obj->frameID, zbBaudRate);
  obj->atReplyExpected = true;

  handle_status = zigbee_handle(obj);
  if ((handle_status == ZB_AT_REPLY_RECEIVED) &&
      (obj->decodedData.atCmd.status == 0))
  {
    status = ZB_CMD_SUCCESS;
  }

  serial_set_baudrate(obj->fd, baudRate);

  return status;
}
Example #10
0
int stm32w_info()
{
    int i;
    uint8_t buff[MAX_XFER_SIZE];
    uint32_t x;
    uint8_t y;
    uint16_t z;

    serial_set_baudrate(50);

    printf("\nSTM32F USB-to-UART interface:\n");
    if(stm32f_cmd_1_4(CMD_GET_BL_VERSION, &x)) {
        printf("Communication error\n");
        exit(1);
    }
    printf(" %-32s %u.%u.%u.%u\n", "Bootloader Version:",
           (x>>24) & 0xff, (x>>16) & 0xff, (x>>8) & 0xff, x & 0xff);

    if(stm32f_cmd_1_4(CMD_GET_APP_VERSION, &x)) {
        printf("Communication error\n");
        exit(1);
    }
    printf(" %-32s %u.%u.%u.%u\n","Firmware Version:",
           (x>>24) & 0xff, (x>>16) & 0xff, (x>>8) & 0xff, x & 0xff);

    stm32w_reset();

    serial_set_baudrate(115200);

    printf("\nSTM32W108 Device information:\n");
    stm32w_bl_ping();
    stm32w_bl_get(&y);
    printf(" %-32s %u\n","BootLoader Version:",y);

    stm32w_bl_getid(&z);
    printf(" %-32s 0x%04x\n","Device Type:",z);

#define PRINT_EUI64_ADDR(a, s) \
	stm32w_bl_read_mem(a, buff, 8);		\
	printf(" %-32s ", s);				\
	for(i=7;i>0;i--)				\
		printf("%02x:", buff[i]);		\
	printf("%02x\n", buff[0]);

#define PRINT_STRING(a, l, s) \
	stm32w_bl_read_mem(a, buff, l);		\
	printf(" %-32s ", s);				\
	for(i=0;i<l;i++)				\
		printf("%c", ((buff[i]<0x20) || (buff[i]>0x7f)) ? '.' : buff[i]); \
	printf("\n");

    PRINT_EUI64_ADDR(0x080407A2, "Burned-in EUI-64 address:");
    PRINT_EUI64_ADDR(0x080408A2, "CIB EUI-64 address:");
    PRINT_STRING(0x0804081A, 16, "CIB Manufacturer String:");
    PRINT_STRING(0x0804082A, 16, "CIB Manufacturer Board Name:");

    /* Read Option Bytes from CIB */
    stm32w_bl_read_mem(0x08040800, buff, 16);
    printf(" %-32s 0x%02x (%s)\n","CIB Read Protection:", buff[0],
           (buff[0] == 0xa5) ? "inactive" : "active" );
    x = buff[8] | buff[10]<<8 | buff[12]<<16 | buff[14]<<24;
    printf(" %-32s ","CIB Write Protection (pg 0-63):");
    for(i=0; i<32; i++) {
        if (x & 1<<i)
            printf("n");
        else
            printf("y");
        if (!((i+1)%8))
            printf(" ");
    }
    printf("\n");

    /* Read PHY Config from CIB */
    stm32w_bl_read_mem(0x0804083C, buff, 2);
    printf(" %-32s %02x %02x\n","CIB PHY Config:", buff[0], buff[1]);

    printf("\n");
    return 0;
}