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.
发现潜在的错误。 当我在文本模式下启用 UART 时、使用 TX 和 Rx 回调函数实现非阻塞。 如果启用了回显模式、则只有当我有一个以某种方式操纵读取缓冲区的函数(这可能只是未定义的行为)时、回显模式才会使用 UART_WRITE 向终端打印文本。 值得注意的是、当回波模式被禁用时、我的代码100%完全正常工作。 随附的是所使用的代码。 我在 RX 回调函数中为环路注释了 A。 当回波模式打开时、当使用 UART_WRITE 时、它不会打印任何内容。 当回波模式关闭时、它确实是这样。 如果回波模式打开且未注释 for loop printf 语句、则会起作用。
我想弄清楚为什么会发生这种奇怪的行为、以便我可以针对循环删除这种行为、并避免类似这样的未定义行为。
/* * ======== 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); }
您好!Dylan、
我必须了解回波模式在下周假期后的工作原理。
我很好奇、如果您的代码在没有回波模式的情况下运行良好、您为什么认为在不更改代码中的任何其他内容的情况下添加回波模式也可以正常运行? 如果不仔细研究细节、我的直觉是、回波模式与非回波模式的处理方式会有所不同、因此您必须相应地更改回调。
此致、
Ralph Jacobi
您好、Ralph、
到目前为止、我在回波模式中唯一能找到的信息是头文件、我遵循了它所说的操作。 写入和读取均设置为文本模式、并进行非阻塞回调、UART 在 回调期间不应接收任何输入。 另一个显而易见的问题是、在非阻塞回调期间使用 UART_WRITE、但我有一个更长的代码版本、 它只是在信标之后和 GPIO 切换之前运行 UART 写入、并且行为相同。
/*! * @brief UART echo settings * * This enumeration defines if the driver will echo data when uses in * #UART_DATA_TEXT mode. This only applies to data received by the UART. * * #UART_ECHO_ON will echo back characters it received while in #UART_DATA_TEXT * mode. * #UART_ECHO_OFF will not echo back characters it received in #UART_DATA_TEXT * mode. * * @pre UART driver must be used in #UART_DATA_TEXT mode. */ typedef enum { UART_ECHO_OFF = 0, /*!< Data is not echoed */ UART_ECHO_ON = 1 /*!< Data is echoed */ } UART_Echo; .... * Options for the readEcho parameter are #UART_ECHO_OFF and #UART_ECHO_ON. * This parameter determines whether the driver echoes data back to the * UART. When echo is turned on, each character that is read by the target * is written back, independent of any write operations. If data is * received in the middle of a write and echo is turned on, the echoed * characters will be mixed in with the write data. ...