Crash when running http_request+uart_events example

temp4eb
Posts: 35
Joined: Tue Jun 06, 2017 8:59 am

Crash when running http_request+uart_events example

Postby temp4eb » Fri Jul 21, 2017 3:54 am

I integrated the http_request and uart_events examples into one project and got crash ramdonly when running on ESP32 DevKitC.
Here is the full code:
http_request_example_main.c

Code: Select all

/* HTTP GET Example using plain POSIX sockets

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"

#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"

/* The examples use simple WiFi configuration that you can set via
   'make menuconfig'.

   If you'd rather not, just change the below entries to strings with
   the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
*/
#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD

/* FreeRTOS event group to signal when we are connected & ready to make a request */
static EventGroupHandle_t wifi_event_group;

/* The event group allows multiple bits for each event,
   but we only care about one event - are we connected
   to the AP with an IP? */
const int CONNECTED_BIT = BIT0;

/* Constants that aren't configurable in menuconfig */
#define WEB_SERVER "example.com"
#define WEB_PORT 80
#define WEB_URL "http://example.com/"

static const char *TAG = "example";

static const char *REQUEST = "GET " WEB_URL " HTTP/1.0\r\n"
    "Host: "WEB_SERVER"\r\n"
    "User-Agent: esp-idf/1.0 esp32\r\n"
    "\r\n";

static esp_err_t event_handler(void *ctx, system_event_t *event)
{
    switch(event->event_id) {
    case SYSTEM_EVENT_STA_START:
        esp_wifi_connect();
        break;
    case SYSTEM_EVENT_STA_GOT_IP:
        xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
        break;
    case SYSTEM_EVENT_STA_DISCONNECTED:
        /* This is a workaround as ESP32 WiFi libs don't currently
           auto-reassociate. */
        esp_wifi_connect();
        xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
        break;
    default:
        break;
    }
    return ESP_OK;
}

static void initialise_wifi(void)
{
    tcpip_adapter_init();
    wifi_event_group = xEventGroupCreate();
    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) );
    wifi_config_t wifi_config = {
        .sta = {
            .ssid = EXAMPLE_WIFI_SSID,
            .password = EXAMPLE_WIFI_PASS,
        },
    };
    ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
    ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
    ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
    ESP_ERROR_CHECK( esp_wifi_start() );
}

static void http_get_task(void *pvParameters)
{
    const struct addrinfo hints = {
        .ai_family = AF_INET,
        .ai_socktype = SOCK_STREAM,
    };
    struct addrinfo *res;
    struct in_addr *addr;
    int s, r;
    char recv_buf[64];

    while(1) {
        /* Wait for the callback to set the CONNECTED_BIT in the
           event group.
        */
        xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
                            false, true, portMAX_DELAY);
        ESP_LOGI(TAG, "Connected to AP");

        int err = getaddrinfo(WEB_SERVER, "80", &hints, &res);

        if(err != 0 || res == NULL) {
            ESP_LOGE(TAG, "DNS lookup failed err=%d res=%p", err, res);
            vTaskDelay(1000 / portTICK_PERIOD_MS);
            continue;
        }

        /* Code to print the resolved IP.

           Note: inet_ntoa is non-reentrant, look at ipaddr_ntoa_r for "real" code */
        addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
        ESP_LOGI(TAG, "DNS lookup succeeded. IP=%s", inet_ntoa(*addr));

        s = socket(res->ai_family, res->ai_socktype, 0);
        if(s < 0) {
            ESP_LOGE(TAG, "... Failed to allocate socket.");
            freeaddrinfo(res);
            vTaskDelay(1000 / portTICK_PERIOD_MS);
            continue;
        }
        ESP_LOGI(TAG, "... allocated socket\r\n");

        if(connect(s, res->ai_addr, res->ai_addrlen) != 0) {
            ESP_LOGE(TAG, "... socket connect failed errno=%d", errno);
            close(s);
            freeaddrinfo(res);
            vTaskDelay(4000 / portTICK_PERIOD_MS);
            continue;
        }

        ESP_LOGI(TAG, "... connected");
        freeaddrinfo(res);

        if (write(s, REQUEST, strlen(REQUEST)) < 0) {
            ESP_LOGE(TAG, "... socket send failed");
            close(s);
            vTaskDelay(4000 / portTICK_PERIOD_MS);
            continue;
        }
        ESP_LOGI(TAG, "... socket send success");

        /* Read HTTP response */
        do {
            bzero(recv_buf, sizeof(recv_buf));
            r = read(s, recv_buf, sizeof(recv_buf)-1);
            for(int i = 0; i < r; i++) {
                putchar(recv_buf[i]);
            }
        } while(r > 0);

        ESP_LOGI(TAG, "... done reading from socket. Last read return=%d errno=%d\r\n", r, errno);
        close(s);
        for(int countdown = 10; countdown >= 0; countdown--) {
            ESP_LOGI(TAG, "%d... ", countdown);
            vTaskDelay(1000 / portTICK_PERIOD_MS);
        }
        ESP_LOGI(TAG, "Starting again!");
    }
}

extern void uart_main();
void app_main()
{
    ESP_ERROR_CHECK( nvs_flash_init() );
    initialise_wifi();
    //xTaskCreate(&http_get_task, "http_get_task", 4096, NULL, 5, NULL);
    vTaskDelay(2000 / portTICK_PERIOD_MS);
    uart_main();
}
uart_events_forwifi.c

Code: Select all

/* Uart Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "driver/uart.h"
#include "freertos/queue.h"
#include "esp_log.h"
#include "soc/uart_struct.h"

static const char *TAG = "uart_events";

/**
 * This example shows how to use the UART driver to handle special UART events.
 *
 * It also reads data from UART0 directly, and echoes it to console.
 *
 * - port: UART0
 * - rx buffer: on
 * - tx buffer: on
 * - flow control: off
 * - event queue: on
 * - pin assignment: txd(default), rxd(default)
 */

#define EX_UART_NUM UART_NUM_1

#define BUF_SIZE (1024)
static QueueHandle_t uart0_queue;

static void uart_event_task(void *pvParameters)
{
    uart_event_t event;
    size_t buffered_size;
    uint8_t* dtmp = (uint8_t*) malloc(BUF_SIZE);
    for(;;) {
        //Waiting for UART event.
        if(xQueueReceive(uart0_queue, (void * )&event, (portTickType)portMAX_DELAY)) {
            ESP_LOGI(TAG, "uart[%d] event:", EX_UART_NUM);
            switch(event.type) {
                //Event of UART receving data
                /*We'd better handler data event fast, there would be much more data events than
                other types of events. If we take too much time on data event, the queue might
                be full.
                in this example, we don't process data in event, but read data outside.*/
                case UART_DATA:
                    uart_get_buffered_data_len(EX_UART_NUM, &buffered_size);
                    ESP_LOGI(TAG, "data, len: %d; buffered len: %d", event.size, buffered_size);
                    break;
                //Event of HW FIFO overflow detected
                case UART_FIFO_OVF:
                    ESP_LOGI(TAG, "hw fifo overflow\n");
                    //If fifo overflow happened, you should consider adding flow control for your application.
                    //We can read data out out the buffer, or directly flush the rx buffer.
                    uart_flush(EX_UART_NUM);
                    break;
                //Event of UART ring buffer full
                case UART_BUFFER_FULL:
                    ESP_LOGI(TAG, "ring buffer full\n");
                    //If buffer full happened, you should consider encreasing your buffer size
                    //We can read data out out the buffer, or directly flush the rx buffer.
                    uart_flush(EX_UART_NUM);
                    break;
                //Event of UART RX break detected
                case UART_BREAK:
                    ESP_LOGI(TAG, "uart rx break\n");
                    break;
                //Event of UART parity check error
                case UART_PARITY_ERR:
                    ESP_LOGI(TAG, "uart parity error\n");
                    break;
                //Event of UART frame error
                case UART_FRAME_ERR:
                    ESP_LOGI(TAG, "uart frame error\n");
                    break;
                //UART_PATTERN_DET
                case UART_PATTERN_DET:
                    ESP_LOGI(TAG, "uart pattern detected\n");
                    break;
                //Others
                default:
                    ESP_LOGI(TAG, "uart event type: %d\n", event.type);
                    break;
            }
        }
    }
    free(dtmp);
    dtmp = NULL;
    vTaskDelete(NULL);
}

/* Configure the UART events example */
void uart_main()
{
    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,
    };
    //Set UART parameters
    uart_param_config(EX_UART_NUM, &uart_config);
    //Set UART log level
    esp_log_level_set(TAG, ESP_LOG_INFO);
    //Install UART driver, and get the queue.
    uart_driver_install(EX_UART_NUM, BUF_SIZE * 2, BUF_SIZE * 2, 10, &uart0_queue, 0);

    //Set UART pins (using UART0 default pins ie no changes.)
    uart_set_pin(EX_UART_NUM, 10, 9, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);

    //Set uart pattern detect function.
    uart_enable_pattern_det_intr(EX_UART_NUM, '+', 3, 10000, 10, 10);
    //Create a task to handler UART event from ISR
    xTaskCreate(uart_event_task, "uart_event_task", 2048, NULL, 12, NULL);
    //process data
    uint8_t* data = (uint8_t*) malloc(BUF_SIZE);
    do {
        int len = uart_read_bytes(EX_UART_NUM, data, BUF_SIZE, 100 / portTICK_RATE_MS);
        if(len > 0) {
            ESP_LOGI(TAG, "uart read : %d", len);
            uart_write_bytes(EX_UART_NUM, (const char*)data, len);
        }
    } while(1);
}
And log:

Code: Select all

ets Jun  8 2016 00:22:57

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

rst:0x10 (RTCWDT_RTC_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:0x3fff0008,len:8
load:0x3fff0010,len:4408
load:0x40078000,len:11072
load:0x40080000,len:252
entry 0x40080034
[0;32mI (45) boot: ESP-IDF v3.0-dev-114-g4ec2abbf-dirty 2nd stage bootloader[0m
[0;32mI (45) boot: compile time 10:38:48[0m
[0;32mI (48) boot: Enabling RNG early entropy source...[0m
[0;32mI (65) boot: SPI Speed      : 40MHz[0m
[0;32mI (77) boot: SPI Mode       : DIO[0m
[0;32mI (90) boot: SPI Flash Size : 4MB[0m
[0;32mI (102) boot: Partition Table:[0m
[0;32mI (113) boot: ## Label            Usage          Type ST Offset   Length[0m
[0;32mI (136) boot:  0 nvs              WiFi data        01 02 00009000 00006000[0m
[0;32mI (159) boot:  1 phy_init         RF data          01 01 0000f000 00001000[0m
[0;32mI (183) boot:  2 factory          factory app      00 00 00010000 00100000[0m
[0;32mI (206) boot: End of partition table[0m
[0;32mI (219) boot: Disabling RNG early entropy source...[0m
[0;32mI (236) boot: Loading app partition at offset 00010000[0m
[0;32mI (1031) boot: segment 0: paddr=0x00010018 vaddr=0x00000000 size=0x0ffe8 ( 65512) [0m
[0;32mI (1032) boot: segment 1: paddr=0x00020008 vaddr=0x3f400010 size=0x0f398 ( 62360) map[0m
[0;32mI (1049) boot: segment 2: paddr=0x0002f3a8 vaddr=0x3ffb0000 size=0x02e54 ( 11860) load[0m
[0;32mI (1080) boot: segment 3: paddr=0x00032204 vaddr=0x40080000 size=0x00400 (  1024) load[0m
[0;32mI (1102) boot: segment 4: paddr=0x0003260c vaddr=0x40080400 size=0x136ec ( 79596) load[0m
[0;32mI (1166) boot: segment 5: paddr=0x00045d00 vaddr=0x400c0000 size=0x00000 (     0) load[0m
[0;32mI (1167) boot: segment 6: paddr=0x00045d08 vaddr=0x00000000 size=0x0a300 ( 41728) [0m
[0;32mI (1187) boot: segment 7: paddr=0x00050010 vaddr=0x400d0018 size=0x47d0c (294156) map[0m
[0;32mI (1213) cpu_start: Pro cpu up.[0m
[0;32mI (1224) cpu_start: Starting app cpu, entry point is 0x40080f00[0m
[0;32mI (0) cpu_start: App cpu up.[0m
[0;32mI (1257) heap_init: Initializing. RAM available for dynamic allocation:[0m
[0;32mI (1278) heap_init: At 3FFAE2A0 len 00001D60 (7 KiB): DRAM[0m
[0;32mI (1297) heap_init: At 3FFB7D60 len 000282A0 (160 KiB): DRAM[0m
[0;32mI (1316) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM[0m
[0;32mI (1336) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM[0m
[0;32mI (1356) heap_init: At 40093AEC len 0000C514 (49 KiB): IRAM[0m
[0;32mI (1376) cpu_start: Pro cpu start user code[0m
[0;32mI (1434) cpu_start: Starting scheduler on PRO CPU.[0m
[0;32mI (193) cpu_start: Starting scheduler on APP CPU.[0m
I (223) wifi: wifi firmware version: f092575
I (223) wifi: config NVS flash: enabled
I (223) wifi: config nano formating: disabled
[0;32mI (223) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE[0m
[0;32mI (233) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE[0m
I (263) wifi: Init dynamic tx buffer num: 32
I (263) wifi: Init dynamic rx buffer num: 32
I (263) wifi: wifi driver task: 3ffaf204, prio:23, stack:4096
I (263) wifi: Init static rx buffer num: 10
I (273) wifi: Init dynamic rx buffer num: 32
I (273) wifi: Init rx ampdu len mblock:7
I (273) wifi: Init lldesc rx ampdu entry mblock:4
I (283) wifi: wifi power manager task: 0x3ffc2eec prio: 21 stack: 2560
[0;32mI (283) example: Setting WiFi configuration SSID myssid...[0m
I (293) wifi: wifi timer task: 3ffc3f44, prio:22, stack:3584
[0;32mI (333) phy: phy_version: 355.1, 59464c5, Jun 14 2017, 20:25:06, 0, 0[0m
I (333) wifi: mode : sta (24:0a:c4:05:8f:6c)
[0;32mI (2333) uart: queue free spaces: 10[0m
[0;32mI (33833) uart_events: uart[1] event:[0m
[0;32mI (33833) uart_events: data, len: 27; buffered len: 27[0m
[0;32mI (33933) uart_events: uart read : 27[0m
[0;32mI (37363) uart_events: uart[1] event:[0m
[0;32mI (37363) uart_events: data, len: 27; buffered len: 27[0m
[0;32mI (37463) uart_events: uart read : 27[0m
Guru Meditation Error of type LoadProhibited occurred on core  1. Exception was unhandled.
Register dump:
PC      : 0x4008471f  PS      : 0x00060430  A0      : 0x8008479a  A1      : 0x3ffb90d0  
A2      : 0x3ffb596c  A3      : 0x00000000  A4      : 0x00060023  A5      : 0x3ffafc24  
A6      : 0x00000000  A7      : 0x00000001  A8      : 0x3ffb5990  A9      : 0x3ffb90b0  
A10     : 0x3ffb1950  A11     : 0x00000000  A12     : 0x00060023  A13     : 0x3ffb9d00  
A14     : 0x3ffb1034  A15     : 0x00060020  SAR     : 0x00000000  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000048  LBEG    : 0x00000000  LEND    : 0x00000000  LCOUNT  : 0x00000000  

Backtrace: 0x4008471f:0x3ffb90d0 0x40084797:0x3ffb90f0

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:0x3fff0008,len:8
load:0x3fff0010,len:4408
load:0x40078000,len:11072
load:0x40080000,len:252
entry 0x40080034
[0;32mI (45) boot: ESP-IDF v3.0-dev-114-g4ec2abbf-dirty 2nd stage bootloader[0m
[0;32mI (45) boot: compile time 10:38:48[0m
[0;32mI (49) boot: Enabling RNG early entropy source...[0m
[0;32mI (66) boot: SPI Speed      : 40MHz[0m
[0;32mI (78) boot: SPI Mode       : DIO[0m
[0;32mI (91) boot: SPI Flash Size : 4MB[0m
[0;32mI (103) boot: Partition Table:[0m
[0;32mI (114) boot: ## Label            Usage          Type ST Offset   Length[0m
[0;32mI (137) boot:  0 nvs              WiFi data        01 02 00009000 00006000[0m
[0;32mI (160) boot:  1 phy_init         RF data          01 01 0000f000 00001000[0m
[0;32mI (184) boot:  2 factory          factory app      00 00 00010000 00100000[0m
[0;32mI (207) boot: End of partition table[0m
[0;32mI (220) boot: Disabling RNG early entropy source...[0m
[0;32mI (237) boot: Loading app partition at offset 00010000[0m
[0;31mE (1032) esp_image: checksum failed. Calculated 0x5b read 0x29[0m
[0;31mE (1033) boot: Failed to verify app image @ 0x10000 (8194)[0m
user code done
The test showed that the UART could not run with the Wi-Fi?But it seems that the official AT firmware(ESP32-AT) could work well with Uart+Wi-Fi?
Any suggestions?

f.h-f.s.
Posts: 214
Joined: Thu Dec 08, 2016 2:53 pm

Re: Crash when running http_request+uart_events example

Postby f.h-f.s. » Fri Jul 21, 2017 4:07 am

can you resolve the backtrace with addr2line? http://esp-idf.readthedocs.io/en/latest ... nitor.html

Who is online

Users browsing this forum: Vineethad and 221 guests