added a degug message for bluetooth comms. Updated bluegiga to read uplink messages of 20 bytes, updated various files in the bluegiga coms tool

This commit is contained in:
kirkscheper
2015-10-19 16:18:27 +02:00
parent c5ea6a53af
commit 10f85e09bd
11 changed files with 2359 additions and 2553 deletions
+1
View File
@@ -856,6 +856,7 @@
<message name="BLUEGIGA" id="106">
<field name="data_rate" type="uint32" unit="bytes/s"/>
<field name="last_msg" type="uint8[]"/>
</message>
<!--107 is free -->
+11 -11
View File
@@ -135,7 +135,7 @@ typedef void (*SPICallback)(struct spi_transaction *trans);
* - The input/output buffers needs to be created separately
* - Take care of pointing input_buf/ouput_buf correctly
* - input_length and output_length can be different, the larger number
* of the two specifies the toal number of exchanged words,
* of the two specifies the total number of exchanged words,
* - if input_length is larger than output length,
* 0 is sent for the remaining words
*/
@@ -198,7 +198,7 @@ struct spi_periph {
extern struct spi_periph spi0;
extern void spi0_init(void);
/** Architecture dependant SPI0 initialization.
/** Architecture dependent SPI0 initialization.
* Must be implemented by underlying architecture
*/
extern void spi0_arch_init(void);
@@ -210,7 +210,7 @@ extern void spi0_arch_init(void);
extern struct spi_periph spi1;
extern void spi1_init(void);
/** Architecture dependant SPI1 initialization.
/** Architecture dependent SPI1 initialization.
* Must be implemented by underlying architecture
*/
extern void spi1_arch_init(void);
@@ -222,7 +222,7 @@ extern void spi1_arch_init(void);
extern struct spi_periph spi2;
extern void spi2_init(void);
/** Architecture dependant SPI2 initialization.
/** Architecture dependent SPI2 initialization.
* Must be implemented by underlying architecture
*/
extern void spi2_arch_init(void);
@@ -234,7 +234,7 @@ extern void spi2_arch_init(void);
extern struct spi_periph spi3;
extern void spi3_init(void);
/** Architecture dependant SPI3 initialization.
/** Architecture dependent SPI3 initialization.
* Must be implemented by underlying architecture
*/
extern void spi3_arch_init(void);
@@ -246,7 +246,7 @@ extern void spi3_arch_init(void);
*/
extern void spi_init(struct spi_periph *p);
/** Initialize all used slaves and uselect them.
/** Initialize all used slaves and unselect them.
*/
extern void spi_init_slaves(void);
@@ -254,7 +254,7 @@ extern void spi_init_slaves(void);
* Must be implemented by the underlying architecture
* @param p spi peripheral to be used
* @param t spi transaction
* @return TRUE if insertion to the transaction queue succeded
* @return TRUE if insertion to the transaction queue succeeded
*/
extern bool_t spi_submit(struct spi_periph *p, struct spi_transaction *t);
@@ -295,7 +295,7 @@ extern bool_t spi_resume(struct spi_periph *p, uint8_t slave);
extern struct spi_periph spi0;
extern void spi0_slave_init(void);
/** Architecture dependant SPI0 initialization as slave.
/** Architecture dependent SPI0 initialization as slave.
* Must be implemented by underlying architecture
*/
extern void spi0_slave_arch_init(void);
@@ -307,7 +307,7 @@ extern void spi0_slave_arch_init(void);
extern struct spi_periph spi1;
extern void spi1_slave_init(void);
/** Architecture dependant SPI1 initialization as slave.
/** Architecture dependent SPI1 initialization as slave.
* Must be implemented by underlying architecture
*/
extern void spi1_slave_arch_init(void);
@@ -319,7 +319,7 @@ extern void spi1_slave_arch_init(void);
extern struct spi_periph spi2;
extern void spi2_slave_init(void);
/** Architecture dependant SPI2 initialization as slave.
/** Architecture dependent SPI2 initialization as slave.
* Must be implemented by underlying architecture
*/
extern void spi2_slave_arch_init(void);
@@ -331,7 +331,7 @@ extern void spi2_slave_arch_init(void);
extern struct spi_periph spi3;
extern void spi3_slave_init(void);
/** Architecture dependant SPI3 initialization as slave.
/** Architecture dependent SPI3 initialization as slave.
* Must be implemented by underlying architecture
*/
extern void spi3_slave_arch_init(void);
+66 -38
View File
@@ -48,6 +48,7 @@ struct bluegiga_periph bluegiga_p;
struct spi_transaction bluegiga_spi;
signed char bluegiga_rssi[256]; // values initialized with 127
unsigned char temlemetry_copy[20];
void bluegiga_transmit(struct bluegiga_periph *p, uint8_t data);
void bluegiga_receive(struct spi_transaction *trans);
@@ -109,7 +110,7 @@ static void send_bluegiga(struct transport_tx *trans, struct link_device *dev)
if (now_ts > last_ts){
uint32_t rate = 1000*bluegiga_p.bytes_recvd_since_last/(now_ts - last_ts);
pprz_msg_send_BLUEGIGA(trans, dev, AC_ID, &rate);
pprz_msg_send_BLUEGIGA(trans, dev, AC_ID, &rate, 20, temlemetry_copy);
bluegiga_p.bytes_recvd_since_last = 0;
last_ts = now_ts;
@@ -213,6 +214,8 @@ void bluegiga_send(struct bluegiga_periph *p)
p->work_tx[i] = 0;
}
p->work_tx[0] = 1; // todo for testing only
coms_status = BLUEGIGA_SENDING;
}
}
@@ -228,65 +231,90 @@ void bluegiga_receive(struct spi_transaction *trans)
}
}
uint8_t packet_len = trans->input_buf[3]; // length of transmitted message
/*
* 0xff communication lost with ground station
* 0xfe RSSI value from broadcaster
* 0xfd Change in broadcast mode
* 0xfc Receive all recorded RSSI
* <=20 Data package from ground station
*/
if (packet_len > trans->input_length) {
// Direct message from Bluegiga
// int k_rssi, i;
switch (packet_len) {
case 0xff: // Connection lost with ground station!
uint8_t packet_len = 0;
uint8_t read_offset = 0;
switch (trans->input_buf[0])
{
case 0xff: // communication lost with ground station
#ifdef MODEM_LED
LED_OFF(MODEM_LED);
LED_OFF(MODEM_LED);
#endif
coms_status = BLUEGIGA_UNINIT;
gpio_set(BLUEGIGA_DRDY_GPIO, BLUEGIGA_DRDY_GPIO_PIN); // Reset interrupt pin
break;
/*case 0xfe: // rssi data
k_rssi = trans->input_buf[2];
for (i = 0; i < k_rssi; i++) {
bluegiga_rssi[i] = trans->input_buf[3 + i];
}
break;*/
case 0xfd: // interrupt handled on bluegiga
gpio_set(BLUEGIGA_DRDY_GPIO, BLUEGIGA_DRDY_GPIO_PIN); // Reset interrupt pin
coms_status = BLUEGIGA_UNINIT;
gpio_set(BLUEGIGA_DRDY_GPIO, BLUEGIGA_DRDY_GPIO_PIN); // Reset interrupt pin
break;
case 0xfe: // RSSI value from broadcaster
bluegiga_rssi[trans->input_buf[1]] = trans->input_buf[2];
packet_len = trans->input_buf[3];
read_offset = 4;
break;
case 0xfd: // Change in broadcast mode
gpio_set(BLUEGIGA_DRDY_GPIO, BLUEGIGA_DRDY_GPIO_PIN); // Reset interrupt pin
// fetch scan status
if (trans->input_buf[2] == 1) {
coms_status = BLUEGIGA_SCANNING;
} else {
coms_status = BLUEGIGA_UNINIT;
}
break;
default:
break;
}
// fetch scan status
if (trans->input_buf[1] == 1) {
coms_status = BLUEGIGA_BROADCASTING;
} else {
coms_status = BLUEGIGA_UNINIT;
}
break;
case 0xfc: // Receive all recorded RSSI
for (uint8_t i = 0; i < trans->input_buf[1]; i++)
bluegiga_rssi[trans->input_buf[2] + i] = trans->input_buf[3 + i];
break;
default:
packet_len = trans->input_buf[0]; // length of transmitted message
read_offset = 1;
}
// handle incoming datalink message
else if (packet_len > 0) {
bluegiga_rssi[trans->input_buf[1]] = trans->input_buf[2];
if (packet_len > 0 && packet_len <= trans->input_length) {
// Handle received message
for (uint8_t i = 0; i < packet_len; i++) {
bluegiga_p.rx_buf[(bluegiga_p.rx_insert_idx + i) % BLUEGIGA_BUFFER_SIZE] = trans->input_buf[i + 4];
bluegiga_p.rx_buf[(bluegiga_p.rx_insert_idx + i) % BLUEGIGA_BUFFER_SIZE] = trans->input_buf[i + read_offset];
}
bluegiga_increment_buf(&bluegiga_p.rx_insert_idx, packet_len);
bluegiga_p.bytes_recvd_since_last += packet_len;
coms_status = BLUEGIGA_IDLE;
for (uint8_t i = 0; i < trans->input_length; i++)
{
temlemetry_copy[i] = trans->input_buf[i];
}
} else {
coms_status = BLUEGIGA_IDLE;
}
// clear rx buffer
for (uint8_t i = 0; i < trans->input_length; i++) {
trans->input_buf[i] = 0;
}
// register spi slave read for next transaction
spi_slave_register(&(BLUEGIGA_SPI_DEV), &bluegiga_spi);
}
}
/* command bluetooth to switch to active scan mode to get rssi values from neighboring drones */
void bluegiga_scan(void)
/* command bluetooth to switch to active scan mode to get rssi values from neighbouring drones */
void bluegiga_scan(struct bluegiga_periph *p)
{
memset(p->work_tx, 0, 20);
p->work_tx[0] = 0xfd; // change broadcast mode header
// trigger bluegiga to read direct command
gpio_clear(BLUEGIGA_DRDY_GPIO, BLUEGIGA_DRDY_GPIO_PIN); // set interrupt
}
/* Request list of all recorded RSSI */
void bluegiga_request_all_rssi(struct bluegiga_periph *p)
{
memset(p->work_tx, 0, 20);
p->work_tx[0] = 0xfc;
// trigger bluegiga to read direct command
gpio_clear(BLUEGIGA_DRDY_GPIO, BLUEGIGA_DRDY_GPIO_PIN); // set interrupt
}
+3 -3
View File
@@ -34,7 +34,7 @@ enum BlueGigaStatus {
BLUEGIGA_UNINIT, /**< The com isn't initialized */
BLUEGIGA_IDLE, /**< The com is in idle */
BLUEGIGA_SENDING, /**< The com is sending */
BLUEGIGA_SCANNING /**< The com is switched from data link to rssi scanning */
BLUEGIGA_BROADCASTING /**< The com is switched from data link to rssi scanning */
};
#ifndef BLUEGIGA_BUFFER_SIZE
@@ -75,7 +75,8 @@ void bluegiga_increment_buf(uint8_t *buf_idx, uint8_t len);
void bluegiga_init(struct bluegiga_periph *p);
void bluegiga_send(struct bluegiga_periph *p);
void bluegiga_scan(void);
void bluegiga_scan(struct bluegiga_periph *p);
void bluegiga_request_all_rssi(struct bluegiga_periph *p);
// BLUEGIGA is using pprz_transport
// FIXME it should not appear here, this will be fixed with the rx improvements some day...
@@ -104,7 +105,6 @@ static inline void bluegiga_read_buffer(struct bluegiga_periph *p, struct pprz_t
}
// transmit previous data in buffer and parse data received
// TODO: (Kirk) Remove hard coded device perph here
#define BlueGigaCheckAndParse(_dev,_trans) { \
if (bluegiga_ch_available(&(_dev))) \
bluegiga_read_buffer(&(_dev), &(_trans)); \
+25 -28
View File
@@ -13,7 +13,7 @@
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
@@ -30,23 +30,23 @@
/* structure packing compability */
#ifndef PACKSTRUCT
#ifdef PACKED
#define PACKSTRUCT(a) a PACKED
#else
/* default packed configuration */
#ifdef __GNUC__
#ifdef _WIN32
#define PACKSTRUCT( decl ) decl __attribute__((__packed__,gcc_struct))
#else
#define PACKSTRUCT( decl ) decl __attribute__((__packed__))
#endif
#define ALIGNED __attribute__((aligned(0x4)))
#ifdef PACKED
#define PACKSTRUCT(a) a PACKED
#else
/* default packed configuration */
#ifdef __GNUC__
#ifdef _WIN32
#define PACKSTRUCT( decl ) decl __attribute__((__packed__,gcc_struct))
#else
#define PACKSTRUCT( decl ) decl __attribute__((__packed__))
#endif
#define ALIGNED __attribute__((aligned(0x4)))
#else // MSVC
#define PACKSTRUCT( decl ) __pragma( pack(push, 1) ) decl __pragma( pack(pop) )
#define ALIGNED
#endif
#endif
#else // MSVC
#define PACKSTRUCT( decl ) __pragma( pack(push, 1) ) decl __pragma( pack(pop) )
#define ALIGNED
#endif
#endif
#endif
typedef unsigned char uint8;
@@ -55,23 +55,20 @@ typedef signed short int16;
typedef unsigned long uint32;
typedef signed char int8;
typedef struct bd_addr_t
{
uint8 addr[6];
typedef struct bd_addr_t {
uint8 addr[6];
} bd_addr;
typedef bd_addr hwaddr;
typedef struct
{
uint8 len;
uint8 data[];
typedef struct {
uint8 len;
uint8 data[];
} uint8array;
typedef struct
{
uint8 len;
int8 data[];
typedef struct {
uint8 len;
int8 data[];
} string;
#endif
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -394,7 +394,7 @@ void ble_rsp_test_debug(const struct ble_msg_test_debug_rsp_t *msg)
void ble_rsp_test_channel_mode(const void *nul)
{
}
}
void ble_rsp_dfu_reset(const void *nul)
{
+188 -185
View File
@@ -13,7 +13,7 @@
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
@@ -29,9 +29,9 @@
#include "uart.h"
#ifdef PLATFORM_WIN
#ifdef _MSC_VER
#define snprintf _snprintf
#endif
#ifdef _MSC_VER
#define snprintf _snprintf
#endif
// Windows implementation of UART access
// see https://msdn.microsoft.com/en-us/library/aa363194(v=vs.85).aspx for reference
@@ -43,181 +43,183 @@ HANDLE serial_handle;
DCB dcb;
// Windows-only serial port device listing
void uart_list_devices() {
char name[] = "Bluegiga Bluetooth Low Energy";
void uart_list_devices()
{
char name[] = "Bluegiga Bluetooth Low Energy";
BYTE* pbuf = NULL;
DWORD reqSize = 0;
DWORD n = 0;
HDEVINFO hDevInfo;
BYTE *pbuf = NULL;
DWORD reqSize = 0;
DWORD n = 0;
HDEVINFO hDevInfo;
//guid for ports
static const GUID guid = { 0x4d36e978, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } };
//guid for ports
static const GUID guid = { 0x4d36e978, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } };
char *str;
char tmp[MAX_PATH + 1];
int i;
SP_DEVINFO_DATA DeviceInfoData;
char *str;
char tmp[MAX_PATH + 1];
int i;
SP_DEVINFO_DATA DeviceInfoData;
snprintf(tmp, MAX_PATH, "%s (COM%%d)", name);
snprintf(tmp, MAX_PATH, "%s (COM%%d)", name);
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
hDevInfo = SetupDiGetClassDevs(&guid, //Retrieve all ports
0L,
NULL, DIGCF_PRESENT);
if(hDevInfo == INVALID_HANDLE_VALUE)
return;
while (1) {
if (!SetupDiEnumDeviceInfo(hDevInfo, n++, &DeviceInfoData))
{
SetupDiDestroyDeviceInfoList(hDevInfo);
return;
}
reqSize = 0;
SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, NULL, NULL, 0, &reqSize);
pbuf = (BYTE*)malloc(reqSize > 1 ? reqSize : 1);
if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, NULL, pbuf, reqSize, NULL))
{
free(pbuf);
continue;
}
str = (char*)pbuf;
if (sscanf(str, tmp, &i) == 1)
{
printf("%s\n", str);
//emit DeviceFound(str,QString("\\\\.\\COM%1").arg(i));
}
free(pbuf);
}
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
hDevInfo = SetupDiGetClassDevs(&guid, //Retrieve all ports
0L,
NULL, DIGCF_PRESENT);
if (hDevInfo == INVALID_HANDLE_VALUE) {
return;
}
while (1) {
if (!SetupDiEnumDeviceInfo(hDevInfo, n++, &DeviceInfoData)) {
SetupDiDestroyDeviceInfoList(hDevInfo);
return;
}
reqSize = 0;
SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, NULL, NULL, 0, &reqSize);
pbuf = (BYTE *)malloc(reqSize > 1 ? reqSize : 1);
if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, NULL, pbuf, reqSize, NULL)) {
free(pbuf);
continue;
}
str = (char *)pbuf;
if (sscanf(str, tmp, &i) == 1) {
printf("%s\n", str);
//emit DeviceFound(str,QString("\\\\.\\COM%1").arg(i));
}
free(pbuf);
}
return;
}
int uart_find_serialport(char *name) {
BYTE* pbuf = NULL;
DWORD reqSize = 0;
DWORD n = 0;
HDEVINFO hDevInfo;
// GUID for ports
static const GUID guid = { 0x4d36e978, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } };
char *str;
char tmp[MAX_PATH+1];
int i;
SP_DEVINFO_DATA DeviceInfoData;
int uart_find_serialport(char *name)
{
BYTE *pbuf = NULL;
DWORD reqSize = 0;
DWORD n = 0;
HDEVINFO hDevInfo;
// GUID for ports
static const GUID guid = { 0x4d36e978, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } };
char *str;
char tmp[MAX_PATH + 1];
int i;
SP_DEVINFO_DATA DeviceInfoData;
snprintf(tmp, MAX_PATH, "%s (COM%%d)", name);
snprintf(tmp, MAX_PATH, "%s (COM%%d)", name);
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
hDevInfo = SetupDiGetClassDevs(&guid, 0L, NULL, DIGCF_PRESENT);
if(hDevInfo == INVALID_HANDLE_VALUE)
return -1;
while (1) {
if (!SetupDiEnumDeviceInfo(hDevInfo, n++, &DeviceInfoData))
{
SetupDiDestroyDeviceInfoList(hDevInfo);
return -1;
}
reqSize = 0;
SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, NULL, NULL, 0, &reqSize);
pbuf = malloc(reqSize > 1 ? reqSize : 1);
if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, NULL, pbuf, reqSize, NULL))
{
free(pbuf);
continue;
}
str = (char*)pbuf;
if (sscanf(str, tmp, &i) == 1)
{
free(pbuf);
SetupDiDestroyDeviceInfoList(hDevInfo);
return i;
}
free(pbuf);
}
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
hDevInfo = SetupDiGetClassDevs(&guid, 0L, NULL, DIGCF_PRESENT);
if (hDevInfo == INVALID_HANDLE_VALUE) {
return -1;
}
while (1) {
if (!SetupDiEnumDeviceInfo(hDevInfo, n++, &DeviceInfoData)) {
SetupDiDestroyDeviceInfoList(hDevInfo);
return -1;
}
reqSize = 0;
SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, NULL, NULL, 0, &reqSize);
pbuf = malloc(reqSize > 1 ? reqSize : 1);
if (!SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, NULL, pbuf, reqSize, NULL)) {
free(pbuf);
continue;
}
str = (char *)pbuf;
if (sscanf(str, tmp, &i) == 1) {
free(pbuf);
SetupDiDestroyDeviceInfoList(hDevInfo);
return i;
}
free(pbuf);
}
return -1;
}
int uart_open(char *port) {
char str[20];
int uart_open(char *port)
{
char str[20];
snprintf(str, sizeof(str) - 1, "\\\\.\\%s", port);
serial_handle = CreateFileA(str,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0, //FILE_FLAG_OVERLAPPED,
NULL);
snprintf(str, sizeof(str) - 1, "\\\\.\\%s", port);
serial_handle = CreateFileA(str,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0, //FILE_FLAG_OVERLAPPED,
NULL);
if (serial_handle == INVALID_HANDLE_VALUE) {
return -1;
}
if (serial_handle == INVALID_HANDLE_VALUE) {
return -1;
}
memset(&dcb, 0, sizeof(DCB));
dcb.DCBlength = sizeof(DCB);
// 115200 bps, 8 data bits, no parity, and 1 stop bit
dcb.BaudRate = CBR_115200; // baud rate
dcb.ByteSize = 8; // data size, xmit and rcv
dcb.Parity = NOPARITY; // parity bit
dcb.StopBits = ONESTOPBIT; // stop bit
dcb.fOutxCtsFlow = 1; // CTS flow control monitoring
dcb.fRtsControl = 1; // RTS flow control output
if (!SetCommState(serial_handle, &dcb)) {
return -2;
}
memset(&dcb, 0, sizeof(DCB));
dcb.DCBlength = sizeof(DCB);
// 115200 bps, 8 data bits, no parity, and 1 stop bit
dcb.BaudRate = CBR_115200; // baud rate
dcb.ByteSize = 8; // data size, xmit and rcv
dcb.Parity = NOPARITY; // parity bit
dcb.StopBits = ONESTOPBIT; // stop bit
dcb.fOutxCtsFlow = 1; // CTS flow control monitoring
dcb.fRtsControl = 1; // RTS flow control output
return 0;
if (!SetCommState(serial_handle, &dcb)) {
return -2;
}
return 0;
}
void uart_close() {
CloseHandle(serial_handle);
void uart_close()
{
CloseHandle(serial_handle);
}
int uart_tx(int len, unsigned char *data) {
DWORD r, written;
while (len) {
r = WriteFile(serial_handle, data, len, &written, NULL);
if (!r) {
return -1;
}
len -= written;
data += written;
int uart_tx(int len, unsigned char *data)
{
DWORD r, written;
while (len) {
r = WriteFile(serial_handle, data, len, &written, NULL);
if (!r) {
return -1;
}
return 0;
len -= written;
data += written;
}
return 0;
}
int uart_rx(int len, unsigned char *data, int timeout_ms) {
int l = len;
DWORD r, rread;
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = MAXDWORD;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.ReadTotalTimeoutConstant = timeout_ms;
timeouts.WriteTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(serial_handle, &timeouts);
while (len) {
r = ReadFile(serial_handle, data, len, &rread, NULL);
if (!r) {
l = GetLastError();
if (l == ERROR_SUCCESS) {
return 0;
}
return -1;
} else {
if (rread == 0) {
return 0;
}
}
len -= rread;
data += rread;
}
int uart_rx(int len, unsigned char *data, int timeout_ms)
{
int l = len;
DWORD r, rread;
COMMTIMEOUTS timeouts;
return l;
timeouts.ReadIntervalTimeout = MAXDWORD;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.ReadTotalTimeoutConstant = timeout_ms;
timeouts.WriteTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(serial_handle, &timeouts);
while (len) {
r = ReadFile(serial_handle, data, len, &rread, NULL);
if (!r) {
l = GetLastError();
if (l == ERROR_SUCCESS) {
return 0;
}
return -1;
} else {
if (rread == 0) {
return 0;
}
}
len -= rread;
data += rread;
}
return l;
}
#else // POSIX or Mac OS X
@@ -252,34 +254,35 @@ int uart_open(char *port)
*/
tcgetattr(serial_handle, &options);
/*
* Set the baud rates to 115200...
*/
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
/*
* Set the baud rates to 115200...
*/
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
/*
* Enable the receiver and set parameters ...
*/
options.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS | HUPCL);
options.c_cflag |= (CS8 | CLOCAL | CREAD);
options.c_lflag &= ~(ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHONL | ECHOCTL | ECHOPRT | ECHOKE | IEXTEN);
options.c_iflag &= ~(INPCK | IXON | IXOFF | IXANY | ICRNL);
options.c_oflag &= ~(OPOST | ONLCR);
/*
* Enable the receiver and set parameters ...
*/
options.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS | HUPCL);
options.c_cflag |= (CS8 | CLOCAL | CREAD);
options.c_lflag &= ~(ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHONL | ECHOCTL | ECHOPRT | ECHOKE | IEXTEN);
options.c_iflag &= ~(INPCK | IXON | IXOFF | IXANY | ICRNL);
options.c_oflag &= ~(OPOST | ONLCR);
//printf( "size of c_cc = %d\n", sizeof( options.c_cc ) );
for (i = 0; i < sizeof(options.c_cc); i++)
options.c_cc[i] = _POSIX_VDISABLE;
//printf( "size of c_cc = %d\n", sizeof( options.c_cc ) );
for (i = 0; i < sizeof(options.c_cc); i++) {
options.c_cc[i] = _POSIX_VDISABLE;
}
options.c_cc[VTIME] = 0;
options.c_cc[VMIN] = 1;
options.c_cc[VTIME] = 0;
options.c_cc[VMIN] = 1;
/*
* Set the new options for the port...
*/
tcsetattr(serial_handle, TCSAFLUSH, &options);
/*
* Set the new options for the port...
*/
tcsetattr(serial_handle, TCSAFLUSH, &options);
return 0;
return 0;
}
void uart_close()
{
@@ -290,16 +293,16 @@ int uart_tx(int len, unsigned char *data)
{
ssize_t written;
while (len) {
written = write(serial_handle, data, len);
if (written < 1) {
return -1;
}
len -= written;
data += written;
while (len) {
written = write(serial_handle, data, len);
if (written < 1) {
return -1;
}
len -= written;
data += written;
}
return 0;
return 0;
}
int uart_rx(int len, unsigned char *data, int timeout_ms)
{
+1 -1
View File
@@ -13,7 +13,7 @@
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//