Found a potential bug. When I enable the UART in text mode, non-blocking with tx and rx callback functions. If echo mode is enabled, it will only print text to my terminal with uart_write if I have a function that manipulates the read buffer in some way (which could just be undefined behavior). Notably, my code works 100% perfectly fine when echo mode is disabled. Attached is the code used. I commented out a for loop in the RX callback function. When echo mode is on it does not print anything when uart_write is used. when echo mode is off it does. if echo mode is on and the for loop printf statements are uncommented it will work.
I would like to figure out why this odd behavior occurs so that I can remove that for loop and avoid undefined behavior like this.
/*
* ======== empty.c ========
*/
/* For usleep() */
#include <unistd.h>
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
/* Driver Header files */
#include <ti/drivers/GPIO.h>
// #include <ti/drivers/I2C.h>
// #include <ti/drivers/SPI.h>
#include <ti/drivers/UART.h>
// #include <ti/drivers/Watchdog.h>
/* Driver configuration */
#include "ti_drivers_config.h"
#define MAX_CLI_LENGTH (8)
unsigned char rxBuffer[MAX_CLI_LENGTH];
/*
* ======== mainThread ========
*/
void txCallback(UART_Handle handle, char *buf, size_t count) {}
void rxCallback(UART_Handle handle, char *buf, size_t count);
void *mainThread(void *arg0)
{
const char echoPrompt[] = "Echoing characters:\r\n";
UART_Handle uart;
UART_Params uartParams;
/* 1 second delay */
uint32_t time = 1;
/* Call driver init functions */
GPIO_init();
// I2C_init();
// SPI_init();
UART_init();
// Watchdog_init();
/* Configure the LED pin */
GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
/* Turn on user LED */
GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);
/* Create a UART with data processing off. */
UART_Params_init(&uartParams);
uartParams.baudRate = 115200;
uartParams.readEcho = UART_ECHO_ON;
uartParams.readMode = UART_MODE_CALLBACK;
uartParams.readDataMode = UART_DATA_TEXT;
uartParams.readReturnMode = UART_RETURN_NEWLINE;
uartParams.readCallback = (UART_Callback)&rxCallback;
uartParams.writeDataMode = UART_DATA_TEXT;
uartParams.writeMode = UART_MODE_CALLBACK;
uartParams.writeCallback = (UART_Callback)&txCallback;
uart = UART_open(CONFIG_UART_0, &uartParams);
if (uart == NULL) {
/* UART_open() failed */
while (1);
}
UART_write(uart, echoPrompt, sizeof(echoPrompt));
UART_read(uart,&rxBuffer,sizeof(rxBuffer));
while (1) {
sleep(time);
GPIO_toggle(CONFIG_GPIO_LED_0);
}
}
void rxCallback(UART_Handle handle, char *buf, size_t count) {
// printf("Read %i = ", count);
// int i;
// for(i = 0; i < MAX_CLI_LENGTH; i++) {
// printf("%d ", (int)(*((buf + i))));
// }
char infoMSG[] = "NOT A VALID COMMAND\r\n";
UART_write(handle, infoMSG, sizeof(infoMSG));
UART_read(handle,buf,MAX_CLI_LENGTH);
}