CC2640的串口收发很容易,直接调用协议栈中的几个函数即可
/* Call driver init functions */
GPIO_init();
UART_init();
/* Create a UART with data processing off. */
UART_Params_init(&uartParams);
uartParams.writeDataMode = UART_DATA_BINARY;
uartParams.readDataMode = UART_DATA_BINARY;
uartParams.readTimeout = 500000;//5s 5秒
uartParams.readReturnMode = UART_RETURN_FULL;
uartParams.readEcho = UART_ECHO_OFF;
uartParams.baudRate = 115200;
uart = UART_open(Board_UART0, &uartParams);
if (uart == NULL) {
/* UART_open() failed */
while (1);
}
UART_write(uart, echoPrompt, sizeof(echoPrompt));
/* Loop forever echoing */
int k = 0;
while (1) {
k = UART_read(uart, input, 10);
UART_write(uart, input, k);
}
这几个参数的设置,可以追进去看一下
/*!
* @brief Function to initialize the UART_Params struct to its defaults
*
* @param params An pointer to UART_Params structure for
* initialization
*
* Defaults values are:
* readMode = UART_MODE_BLOCKING;
* writeMode = UART_MODE_BLOCKING;
* readTimeout = UART_WAIT_FOREVER;
* writeTimeout = UART_WAIT_FOREVER;
* readCallback = NULL;
* writeCallback = NULL;
* readReturnMode = UART_RETURN_NEWLINE;
* readDataMode = UART_DATA_TEXT;
* writeDataMode = UART_DATA_TEXT;
* readEcho = UART_ECHO_ON;
* baudRate = 115200;
* dataLength = UART_LEN_8;
* stopBits = UART_STOP_ONE;
* parityType = UART_PAR_NONE;
*/
extern void UART_Params_init(UART_Params *params);
readTimeout的单位是0.01毫秒级别
以及读取时的模式阻塞还是非阻塞
/*!
* @brief UART return mode settings
*
* This enumeration defines the return modes for UART_read() and
* UART_readPolling(). This mode only functions when in #UART_DATA_TEXT mode.
*
* #UART_RETURN_FULL unblocks or performs a callback when the read buffer has
* been filled.
* #UART_RETURN_NEWLINE unblocks or performs a callback whenever a newline
* character has been received.
*
* UART operation | UART_RETURN_FULL | UART_RETURN_NEWLINE |
* -------------- | ---------------- | ------------------- |
* UART_read() | Returns when buffer is full | Returns when buffer is full or newline was read |
* UART_write() | Sends data as is | Sends data with an additional newline at the end |
*
* @pre UART driver must be used in #UART_DATA_TEXT mode.
*/
typedef enum UART_ReturnMode_ {
/*! Unblock/callback when buffer is full. */
UART_RETURN_FULL,
/*! Unblock/callback when newline character is received. */
UART_RETURN_NEWLINE
} UART_ReturnMode;