The serial port of CC2640 is very easy to send and receive. Just call several functions in the protocol stack.
/* 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);
}
You can check out the settings of these parameters.
/*!
* @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);
The unit of readTimeout is 0.01 milliseconds
And whether the mode is blocking or non-blocking when reading
/*!
* @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;
|