Hi TI,
I am using the TI drivers for CC13x2 and CC26x2 mcus
Could you give me a code example of the UART2 in callback mode.
The example in the doc is in blocking mode and I can't make it work, I need callback mode.
Thank best regards.
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Hi TI,
I am using the TI drivers for CC13x2 and CC26x2 mcus
Could you give me a code example of the UART2 in callback mode.
The example in the doc is in blocking mode and I can't make it work, I need callback mode.
Thank best regards.
Hi A,
There is no callback example in the docs as you say. There is however examples planned for the upcoming SDKs. In the mean while, here is an example:
#include <stdint.h> #include <stddef.h> /* POSIX Header files */ #include <semaphore.h> /* Driver Header files */ #include <ti/drivers/GPIO.h> #include <ti/drivers/UART2.h> /* Driver configuration */ #include "ti_drivers_config.h" static sem_t sem; static volatile size_t numBytesRead; /* * ======== callbackFxn ======== */ void callbackFxn(UART2_Handle handle, void *buffer, size_t count, void *userArg, int_fast16_t status) { if (status != UART2_STATUS_SUCCESS) { /* RX error occured in UART2_read() */ while (1); } numBytesRead = count; sem_post(&sem); } /* * ======== mainThread ======== */ void *mainThread(void *arg0) { char input; const char echoPrompt[] = "Echoing characters:\r\n"; UART2_Handle uart; UART2_Params uartParams; int32_t semStatus; uint32_t status = UART2_STATUS_SUCCESS; /* Call driver init functions */ GPIO_init(); /* Configure the LED pin */ GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW); /* Create semaphore */ semStatus = sem_init(&sem, 0, 0); if (semStatus != 0) { /* Error creating semaphore */ while (1); } /* Create a UART in CALLBACK read mode */ UART2_Params_init(&uartParams); uartParams.readMode = UART2_Mode_CALLBACK; uartParams.readCallback = callbackFxn; uartParams.baudRate = 115200; uart = UART2_open(CONFIG_UART2_0, &uartParams); if (uart == NULL) { /* UART2_open() failed */ while (1); } /* Turn on user LED to indicate successful initialization */ GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON); /* Pass NULL for bytesWritten since it's not used in this example */ UART2_write(uart, echoPrompt, sizeof(echoPrompt), NULL); /* Loop forever echoing */ while (1) { numBytesRead = 0; /* Pass NULL for bytesRead since it's not used in this example */ status = UART2_read(uart, &input, 1, NULL); if (status != UART2_STATUS_SUCCESS) { /* UART2_read() failed */ while (1); } /* Do not write until read callback executes */ sem_wait(&sem); if (numBytesRead > 0) { status = UART2_write(uart, &input, 1, NULL); if (status != UART2_STATUS_SUCCESS) { /* UART2_write() failed */ while (1); } } } }