Search code examples
c++cfreertosrtosesp32

Cannot write to serial


I am using the ESP32 DevKitC-v1 (clone) with FreeRTOS and attempting to write bytes to the serial port.

My code (below) causes the following exception:

Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC      : 0x400ea8fe  PS      : 0x00060730  A0      : 0x800eb825  A1      : 0x3ffdfdf0  
A2      : 0x00000001  A3      : 0x00000055  A4      : 0x00000001  A5      : 0x00000000  
A6      : 0x00000002  A7      : 0xff000000  A8      : 0x00000000  A9      : 0x60010000  
A10     : 0x00000055  A11     : 0x00000001  A12     : 0x3ffdfd54  A13     : 0x3ffd1068  
A14     : 0x00000000  A15     : 0x00000000  SAR     : 0x00000004  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000055  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xffffffff  

Backtrace: 0x400ea8fe:0x3ffdfdf0 0x400eb822:0x3ffdfe10 0x400ebf0d:0x3ffdfe50 0x400e1511:0x3ffdfe80

Rebooting...
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:6372
load:0x40078000,len:11276
load:0x40080000,len:6084
entry 0x4008032c

I have been able to find limited information regarding FreeRTOS and UART operations, I apologise in advance however if I have missed something obvious. I have been trying to self resolve the issue for about 3 days now.

My code:

#define ECHO_TEST_TXD  (GPIO_NUM_17)
#define ECHO_TEST_RXD  (GPIO_NUM_16)
#define ECHO_TEST_RTS  (UART_PIN_NO_CHANGE)
#define ECHO_TEST_CTS  (UART_PIN_NO_CHANGE)    



static void prvSerialRelayMQTTCommand()
{
    const TickType_t xFiveSeconds = pdMS_TO_TICKS( 5000UL );
    /* Configure parameters of an UART driver,
     * communication pins and install the driver */
    uart_config_t uart_config = {
        .baud_rate = 9600,
        .data_bits = UART_DATA_8_BITS,
        .parity    = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
    };
    uart_param_config(UART_NUM_2, &uart_config);
    uart_set_pin(UART_NUM_2, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
    uart_driver_install(UART_NUM_2, BUF_SIZE * 2, 0, 0, NULL, 0);

    while (1) {
        uart_write_bytes(UART_NUM_2, 0b00000000, 1);
        vTaskDelay(xFiveSeconds);
        uart_write_bytes(UART_NUM_2, 0b00000001, 1);
        vTaskDelay(xFiveSeconds);
        uart_write_bytes(UART_NUM_2, 0b00000011, 1);
        vTaskDelay(xFiveSeconds);
        uart_write_bytes(UART_NUM_2, 0b00000111, 1);
        vTaskDelay(xFiveSeconds);
        uart_write_bytes(UART_NUM_2, 0b00001111, 1);
        vTaskDelay(xFiveSeconds);
    }
}

Solution

  • Invalid pointer arguments

    The second argument to uart_write_bytes() should be a const char * (see reference). You are passing it an invalid pointer.

    Change

    uart_write_bytes(UART_NUM_2, 0b00000000, 1);
    

    to

    char c = '\0';
    uart_write_bytes(UART_NUM_2, &c, 1);
    

    and similarly for the other calls.

    However, this isn't necessarily your only problem.

    Stack size for FreeRTOS task

    A LoadProhibited error occurs when the ESP32 attempts to read or write an invalid memory address. The relevant address is stored in the EXCVADDR register - which in this case we can see is 0x00000055. This means that your calls to uart_write_bytes() (even though invalid) are not the cause of the reset - because you did not pass the address 0x00000055 in any of the calls.

    I suspect your problem is that you are not allocating a large enough stack for your FreeRTOS task. If you create a task with a stack size of configMINIMAL_STACK_SIZE, for example, that might not be enough for using the UART. A stack overflow would also result in a LoadProhibited error.

    The stack size is the third parameter passed to xTaskCreate(). Try verifying that it is at least 1024, and increase as needed.