uart_write_bytes or uart_tx_chars crashing

nasiraod
Posts: 1
Joined: Sat Sep 02, 2017 9:35 am

uart_write_bytes or uart_tx_chars crashing

Postby nasiraod » Sat Sep 02, 2017 9:44 am

Hi
I am trying to write a simple code where the UART1 data gets sent to a TCP server. However, I am having an issue writing anything to UART1, or UART0 for that matter. The code works just fine if I don't write anything to UART1, just read. However, I can't read anything from UART0.
Basically invoking uart_write_bytes or uart_tx_chars to UART1 causes a reset, some watchdog timer.
Can anyone help with this?

Thanks in advance :)


Console log:

Code: Select all

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:0x3fff0010,len:4
load:0x3fff0014,len:4348
load:0x40078000,len:0
load:0x40078000,len:11392
entry 0x40078cb8
I (931) cpu_start: Pro cpu up.
I (931) cpu_start: Single core mode
I (934) heap_init: Initializing. RAM available for dynamic allocation:
I (967) heap_init: At 3FFAFF10 len 000000F0 (0 KiB): DRAM
I (1023) heap_init: At 3FFC7AC8 len 00018538 (97 KiB): DRAM
I (1080) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM
I (1140) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (1200) heap_init: At 40093310 len 0000CCF0 (51 KiB): IRAM
I (1258) cpu_start: Pro cpu start user code
I (1418) cpu_start: Starting scheduler on PRO CPU.
I (1475) wifi: wifi firmware version: 8bd4b47
I (1475) wifi: config NVS flash: enabled
I (1475) wifi: config nano formating: disabled
I (1476) system_api: Base MAC address is not set, read default base MAC address from BLK0 of E
I (1486) system_api: Base MAC address is not set, read default base MAC address from BLK0 of E
I (1500) wifi: Init dynamic tx buffer num: 32
I (1500) wifi: Init data frame dynamic rx buffer num: 32
I (1504) wifi: Init management frame dynamic rx buffer num: 32
I (1510) wifi: wifi driver task: 3ffcf370, prio:23, stack:4096
I (1516) wifi: Init static rx buffer num: 10
I (1520) wifi: Init dynamic rx buffer num: 32
I (1524) wifi: Init rx ampdu len mblock:7
I (1527) wifi: Init lldesc rx ampdu entry mblock:4
I (1532) wifi: wifi power manager task: 0x3ffd47e0 prio: 21 stack: 2560
I (1555) phy: phy_version: 357.0, a6bf95b, Jul 25 2017, 12:28:06, 0, 0
I (1556) wifi: Init Ampdu: 1 tx baw=6 rx baw=6
I (1556) wifi: mode : sta (24:0a:c4:81:eb:58)
ets Jun  8 2016 00:22:57

rst:0x7 (TG0WDT_SYS_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
ets Jun  8 2016 00:22:57

rst:0x7 (TG0WDT_SYS_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:0x3fff0010,len:4
load:0x3fff0014,len:4348
load:0x40078000,len:0
load:0x40078000,len:11392
entry 0x40078cb8
W (266) boot: PRO CPU has been reset by WDT.
W (266) boot: WDT reset info: PRO CPU PC=0x40086e8e
W (267) boot: WDT reset info: APP CPU PC=0x16bc92c1
I (1210) cpu_start: Pro cpu up.
I (1211) cpu_start: Single core mode
I (1214) heap_init: Initializing. RAM available for dynamic allocation:
I (1249) heap_init: At 3FFAFF10 len 000000F0 (0 KiB): DRAM
I (1305) heap_init: At 3FFC7AC8 len 00018538 (97 KiB): DRAM
I (1363) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM
I (1422) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (1483) heap_init: At 40093310 len 0000CCF0 (51 KiB): IRAM
I (1540) cpu_start: Pro cpu start user code
I (1700) cpu_start: Starting scheduler on PRO CPU.
I (1758) wifi: wifi firmware version: 8bd4b47
I (1758) wifi: config NVS flash: enabled
I (1758) wifi: config nano formating: disabled
I (1759) system_api: Base MAC address is not set, read default base MAC address from BLK0 of E
I (1769) system_api: Base MAC address is not set, read default base MAC address from BLK0 of E
I (1783) wifi: Init dynamic tx buffer num: 32
I (1783) wifi: Init data frame dynamic rx buffer num: 32
I (1787) wifi: Init management frame dynamic rx buffer num: 32
I (1793) wifi: wifi driver task: 3ffcf370, prio:23, stack:4096
I (1799) wifi: Init static rx buffer num: 10
I (1803) wifi: Init dynamic rx buffer num: 32
I (1807) wifi: Init rx ampdu len mblock:7
I (1810) wifi: Init lldesc rx ampdu entry mblock:4
I (1815) wifi: wifi power manager task: 0x3ffd47e0 prio: 21 stack: 2560
I (1839) phy: phy_version: 357.0, a6bf95b, Jul 25 2017, 12:28:06, 0, 0
I (1840) wifi: Init Ampdu: 1 tx baw=6 rx baw=6
I (1840) wifi: mode : sta (24:0a:c4:81:eb:58)
ets Jun  8 2016 00:22:57
My code:

Code: Select all

#include "freertos/FreeRTOS.h"
#include "esp_wifi.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_event_loop.h"
#include "nvs_flash.h"
#include "driver/gpio.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "freertos/task.h"
#include "driver/uart.h"
#include "freertos/queue.h"
#include "esp_log.h"
#include "soc/uart_struct.h"

#include "sdkconfig.h"

#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
//#define ECHO_TEST_TXD  (4)
//#define ECHO_TEST_RXD  (5)
#define ECHO_TEST_TXD  (16)
#define ECHO_TEST_RXD  (17)

#define ECHO_TEST_RTS  (18)
#define ECHO_TEST_CTS  (19)

#define BUF_SIZE (1024)
char temp[40];
char temp1[BUF_SIZE];
esp_err_t event_handler(void *ctx, system_event_t *event)
{
    return ESP_OK;
}

//an example of echo test with hardware flow control on UART1
void sendToServer() {
	int sockfd, n;
	struct sockaddr_in serv_addr;
	struct hostent *server;
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	server = gethostbyname("192.168.1.24");
	bzero((char *) &serv_addr, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
	serv_addr.sin_port = htons(43333);
	if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
		printf("ERROR connecting");
	n = write(sockfd, temp1, strlen(temp1));
	shutdown(sockfd, SHUT_RDWR);
	close(sockfd);

}
static void echo_task()
{


    const int uart_num = UART_NUM_1;
    //const int uart_num = UART_NUM_2;
    uart_config_t uart_config = {
        .baud_rate = 115200,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .rx_flow_ctrl_thresh = 122,
    };
    //Configure UART1 parameters
    uart_param_config(uart_num, &uart_config);
    //Set UART1 pins(TX: IO4, RX: I05, RTS: IO18, CTS: IO19)
    uart_set_pin(uart_num, GPIO_NUM_9, GPIO_NUM_10, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
    //Install UART driver (we don't need an event queue here)
    //In this example we don't even use a buffer for sending data.
    uart_driver_install(uart_num, BUF_SIZE * 2, 0, 0, NULL, 0);
    uint8_t* data = (uint8_t*) malloc(BUF_SIZE);
    uart_write_bytes(uart_num, (const char*) "Ready", strlen("Ready!"));
    //uart_tx_chars(uart_num, "\n\rReady!\n\r", strlen("\n\rReady!\n\r"));
    bzero(temp1, BUF_SIZE);
    while(1) {
        //Read data from UART
        int len = uart_read_bytes(uart_num, data, BUF_SIZE, 20 / portTICK_RATE_MS);
        //uart_flush(uart_num);
        bzero(temp, 40);

        memcpy(temp, data, len);
        //Write data back to UART
        if (len >= 1)
        	{
			if (temp[0] != 0x0D )
				strcat(temp1, temp);
			else {
				printf("Sending to server: %s \n\r", temp1);
				sendToServer();
				bzero(temp1, BUF_SIZE);
			}
        }
        //uart_write_bytes(uart_num, (const char*) data, len);
    }
}

void app_main(void)
{
    nvs_flash_init();
    tcpip_adapter_init();
    ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
    ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
    ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
    wifi_config_t sta_config = {
        .sta = {
            .ssid = "",
            .password = "",
            .bssid_set = false
        }
    };
    ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &sta_config) );
    ESP_ERROR_CHECK( esp_wifi_start() );
    ESP_ERROR_CHECK( esp_wifi_connect() );

    echo_task();

}


tobewinner
Posts: 30
Joined: Thu Dec 10, 2015 5:27 am

Re: uart_write_bytes or uart_tx_chars crashing

Postby tobewinner » Mon Sep 04, 2017 7:09 am

you should check if len >=0
the type of third argment in uart_write_bytes is size_t not int

Who is online

Users browsing this forum: Google [Bot] and 119 guests