***ERROR*** A stack overflow in task main has been detected.

senudajayalath
Posts: 17
Joined: Sun Jan 03, 2021 8:00 am

***ERROR*** A stack overflow in task main has been detected.

Postby senudajayalath » Sat Dec 25, 2021 4:48 pm

I am trying to send data through espnow. This is my code
  1. /* ESPNOW Example
  2.  
  3.    This example code is in the Public Domain (or CC0 licensed, at your option.)
  4.  
  5.    Unless required by applicable law or agreed to in writing, this
  6.    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  7.    CONDITIONS OF ANY KIND, either express or implied.
  8. */
  9.  
  10. /*
  11.    This example shows how to use ESPNOW.
  12.    Prepare two device, one for sending ESPNOW data and another for receiving
  13.    ESPNOW data.
  14. */
  15. #include <stdlib.h>
  16. #include <time.h>
  17. #include <string.h>
  18. #include <assert.h>
  19. #include "freertos/FreeRTOS.h"
  20. #include "freertos/semphr.h"
  21. #include "freertos/timers.h"
  22. #include "nvs_flash.h"
  23. #include "esp_event.h"
  24. #include "esp_netif.h"
  25. #include "esp_wifi.h"
  26. #include "esp_log.h"
  27. #include "esp_system.h"
  28. #include "esp_now.h"
  29. #include "esp_crc.h"
  30. #include "espnow_example.h"
  31.  
  32. #define ESPNOW_MAXDELAY 512
  33. typedef enum
  34. {
  35.     EXAMPLE_ESPNOW_SEND_CB,
  36.     EXAMPLE_ESPNOW_RECV_CB,
  37. } example_espnow_event_id_t;
  38.  
  39. typedef struct
  40. {
  41.     uint8_t mac_addr[ESP_NOW_ETH_ALEN];
  42.     esp_now_send_status_t status;
  43. } example_espnow_event_send_cb_t;
  44.  
  45. typedef struct
  46. {
  47.     uint8_t mac_addr[ESP_NOW_ETH_ALEN];
  48.     uint8_t *data;
  49.     int data_len;
  50. } example_espnow_event_recv_cb_t;
  51.  
  52. typedef union
  53. {
  54.     example_espnow_event_send_cb_t send_cb;
  55.     example_espnow_event_recv_cb_t recv_cb;
  56. } example_espnow_event_info_t;
  57.  
  58. /* When ESPNOW sending or receiving callback function is called, post event to ESPNOW task. */
  59. typedef struct
  60. {
  61.     example_espnow_event_id_t id;
  62.     example_espnow_event_info_t info;
  63. } example_espnow_event_t;
  64. /* User defined field of ESPNOW data in this example. */
  65. typedef struct
  66. {
  67.     uint8_t type;        //Broadcast or unicast ESPNOW data.
  68.     uint8_t state;       //Indicate that if has received broadcast ESPNOW data or not.
  69.     uint16_t seq_num;    //Sequence number of ESPNOW data.
  70.     uint16_t crc;        //CRC16 value of ESPNOW data.
  71.     uint32_t magic;      //Magic number which is used to determine which device to send unicast ESPNOW data.
  72.     uint32_t payload[0]; //Real payload of ESPNOW data.
  73. } __attribute__((packed)) example_espnow_data_t;
  74.  
  75. /* Parameters of sending ESPNOW data. */
  76. typedef struct
  77. {
  78.     bool unicast;                       //Send unicast ESPNOW data.
  79.     bool broadcast;                     //Send broadcast ESPNOW data.
  80.     uint8_t state;                      //Indicate that if has received broadcast ESPNOW data or not.
  81.     uint32_t magic;                     //Magic number which is used to determine which device to send unicast ESPNOW data.
  82.     uint16_t count;                     //Total count of unicast ESPNOW data to be sent.
  83.     uint16_t delay;                     //Delay between sending two ESPNOW data, unit: ms.
  84.     int len;                            //Length of ESPNOW data to be sent, unit: byte.
  85.     uint8_t *buffer;                    //Buffer pointing to ESPNOW data.
  86.     uint8_t dest_mac[ESP_NOW_ETH_ALEN]; //MAC address of destination device.
  87. } example_espnow_send_param_t;
  88. static const char *TAG = "espnow_example";
  89.  
  90. static xQueueHandle s_example_espnow_queue;
  91.  
  92. static uint8_t s_example_broadcast_mac[ESP_NOW_ETH_ALEN] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  93. static uint16_t s_example_espnow_seq[EXAMPLE_ESPNOW_DATA_MAX] = {0, 0};
  94.  
  95. static void example_espnow_deinit(example_espnow_send_param_t *send_param);
  96.  
  97. /* WiFi should start before using ESPNOW */
  98. static void example_wifi_init(void)
  99. {
  100.     ESP_ERROR_CHECK(esp_netif_init());
  101.     ESP_ERROR_CHECK(esp_event_loop_create_default());
  102.     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  103.     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  104.     ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
  105.     ESP_ERROR_CHECK(esp_wifi_set_mode(ESPNOW_WIFI_MODE));
  106.     ESP_ERROR_CHECK(esp_wifi_start());
  107.  
  108. #if CONFIG_ESPNOW_ENABLE_LONG_RANGE
  109.     ESP_ERROR_CHECK(esp_wifi_set_protocol(ESPNOW_WIFI_IF, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_LR));
  110. #endif
  111. }
  112.  
  113. /* ESPNOW sending or receiving callback function is called in WiFi task.
  114.  * Users should not do lengthy operations from this task. Instead, post
  115.  * necessary data to a queue and handle it from a lower priority task. */
  116. static void example_espnow_send_cb(const uint8_t *mac_addr, esp_now_send_status_t status)
  117. {
  118.     example_espnow_event_t evt;
  119.     example_espnow_event_send_cb_t *send_cb = &evt.info.send_cb;
  120.  
  121.     if (mac_addr == NULL)
  122.     {
  123.         ESP_LOGE(TAG, "Send cb arg error");
  124.         return;
  125.     }
  126.  
  127.     evt.id = EXAMPLE_ESPNOW_SEND_CB;
  128.     memcpy(send_cb->mac_addr, mac_addr, ESP_NOW_ETH_ALEN);
  129.     send_cb->status = status;
  130.     if (xQueueSend(s_example_espnow_queue, &evt, ESPNOW_MAXDELAY) != pdTRUE)
  131.     {
  132.         ESP_LOGW(TAG, "Send send queue fail");
  133.     }
  134. }
  135. int example_espnow_data_parse(uint8_t *data, uint16_t data_len) //int example_espnow_data_parse(uint8_t *data, uint16_t data_len, uint8_t *state, uint16_t *seq, int *magic)
  136. {
  137.     example_espnow_data_t *buf = (example_espnow_data_t *)data;
  138.     uint16_t crc, crc_cal = 0;
  139.  
  140.     if (data_len < sizeof(example_espnow_data_t))
  141.     {
  142.         ESP_LOGE(TAG, "Receive ESPNOW data too short, len:%d", data_len);
  143.         return -1;
  144.     }
  145.  
  146.     // *state = buf->state;
  147.     // *seq = buf->seq_num;
  148.     // *magic = buf->magic;
  149.  
  150.     crc = buf->crc;
  151.     buf->crc = 0;
  152.     crc_cal = esp_crc16_le(UINT16_MAX, (uint8_t const *)buf, data_len);
  153.     ESP_LOGI(TAG, "This is seq_num:%d", buf->seq_num);
  154.     ESP_LOGI(TAG, "This is data:%u", buf->payload[0]);
  155.     ESP_LOGI(TAG, "This is data:%u", buf->payload[1]);
  156.     ESP_LOGI(TAG, "This is data:%u", buf->payload[2]);
  157.     ESP_LOGI(TAG, "This is data:%u", buf->payload[3]);
  158.     ESP_LOGI(TAG, "This is data:%u", buf->payload[4]);
  159.     ESP_LOGI(TAG, "This is magic:%d", buf->magic);
  160.     if (crc_cal == crc)
  161.     {
  162.         return buf->type;
  163.     }
  164.  
  165.     return -1;
  166. }
  167.  
  168. static void example_espnow_recv_cb(const uint8_t *mac_addr, const uint8_t *data, int len)
  169. {
  170.     example_espnow_event_t evt;
  171.     example_espnow_event_recv_cb_t *recv_cb = &evt.info.recv_cb;
  172.  
  173.     if (mac_addr == NULL || data == NULL || len <= 0)
  174.     {
  175.         ESP_LOGE(TAG, "Receive cb arg error");
  176.         return;
  177.     }
  178.  
  179.     evt.id = EXAMPLE_ESPNOW_RECV_CB;
  180.     memcpy(recv_cb->mac_addr, mac_addr, ESP_NOW_ETH_ALEN);
  181.     recv_cb->data = malloc(len);
  182.     if (recv_cb->data == NULL)
  183.     {
  184.         ESP_LOGE(TAG, "Malloc receive data fail");
  185.         return;
  186.     }
  187.     //ESP_LOGE(TAG, "This is data:%u", *data);
  188.     memcpy(recv_cb->data, data, len);
  189.  
  190.     recv_cb->data_len = len;
  191.     // if (xQueueSend(s_example_espnow_queue, &evt, ESPNOW_MAXDELAY) != pdTRUE)
  192.     // {
  193.     //     ESP_LOGW(TAG, "Send receive queue fail");
  194.     //     free(recv_cb->data);
  195.     // }
  196.     ESP_LOGI(TAG, "DATA HEREEE");
  197.     ESP_LOGI(TAG, "Receiveth broadcast data from: " MACSTR ", len: %d", MAC2STR(recv_cb->mac_addr), recv_cb->data_len);
  198.     int ret = example_espnow_data_parse(recv_cb->data, recv_cb->data_len);
  199.  
  200.     free(recv_cb->data);
  201.     // int ret = example_espnow_data_parse(recv_cb->data, recv_cb->data_len);
  202.     // free(recv_cb->data);
  203. }
  204.  
  205. /* Parse received ESPNOW data. */
  206.  
  207. /* Prepare ESPNOW data to be sent. */
  208. void example_espnow_data_prepare(example_espnow_send_param_t *send_param)
  209. {
  210.     example_espnow_data_t *buf = (example_espnow_data_t *)send_param->buffer;
  211.  
  212.     assert(send_param->len >= sizeof(example_espnow_data_t));
  213.  
  214.     buf->type = IS_BROADCAST_ADDR(send_param->dest_mac) ? EXAMPLE_ESPNOW_DATA_BROADCAST : EXAMPLE_ESPNOW_DATA_UNICAST;
  215.     buf->state = send_param->state;
  216.     buf->seq_num = s_example_espnow_seq[buf->type]++;
  217.     buf->crc = 0;
  218.     buf->magic = send_param->magic;
  219.  
  220.     buf->payload[0] = 3423; //data_Array[0]; // identifier that reader moved to new tree
  221.     buf->payload[1] = 454;  //data_Array[1]; //old tree id
  222.     buf->payload[2] = 354;  //data_Array[2]; //new tree id
  223.     buf->payload[3] = 654;  //data_Array[3]; //fruit1
  224.  
  225.  
  226.     buf->crc = esp_crc16_le(UINT16_MAX, (uint8_t const *)buf, send_param->len);
  227. }
  228.  
  229. static void example_espnow_task(void *pvParameter)
  230. {
  231.     example_espnow_event_t evt;
  232.     uint8_t recv_state = 0;
  233.     uint16_t recv_seq = 0;
  234.     int recv_magic = 0;
  235.     bool is_broadcast = false;
  236.     int ret;
  237.  
  238.     vTaskDelay(5000 / portTICK_RATE_MS);
  239.     ESP_LOGI(TAG, "Start sending broadcast data");
  240.  
  241.     /* Start sending broadcast ESPNOW data. */
  242.     example_espnow_send_param_t *send_param = (example_espnow_send_param_t *)pvParameter;
  243.     if (esp_now_send(send_param->dest_mac, send_param->buffer, send_param->len) != ESP_OK)
  244.     {
  245.         ESP_LOGE(TAG, "Send error");
  246.         example_espnow_deinit(send_param);
  247.         vTaskDelete(NULL);
  248.     }
  249.     vTaskDelay(send_param->delay / portTICK_RATE_MS);
  250.     vTaskDelete(NULL);
  251. }
  252.  
  253. esp_err_t send_data()
  254. {
  255.     example_espnow_send_param_t *send_param;
  256.  
  257.     /* Initialize sending parameters. */
  258.     send_param = malloc(sizeof(example_espnow_send_param_t));
  259.     memset(send_param, 0, sizeof(example_espnow_send_param_t));
  260.     if (send_param == NULL)
  261.     {
  262.         ESP_LOGE(TAG, "Malloc send parameter fail");
  263.         vSemaphoreDelete(s_example_espnow_queue);
  264.         esp_now_deinit();
  265.         return ESP_FAIL;
  266.     }
  267.     send_param->unicast = false;
  268.     send_param->broadcast = true;
  269.     send_param->state = 0;
  270.     send_param->magic = esp_random();
  271.     send_param->count = CONFIG_ESPNOW_SEND_COUNT;
  272.     send_param->delay = CONFIG_ESPNOW_SEND_DELAY;
  273.     send_param->len = 18; //CONFIG_ESPNOW_SEND_LEN;
  274.     send_param->buffer = malloc(18);
  275.     if (send_param->buffer == NULL)
  276.     {
  277.         ESP_LOGE(TAG, "Malloc send buffer fail");
  278.         free(send_param);
  279.         vSemaphoreDelete(s_example_espnow_queue);
  280.         esp_now_deinit();
  281.         return ESP_FAIL;
  282.     }
  283.     memcpy(send_param->dest_mac, s_example_broadcast_mac, ESP_NOW_ETH_ALEN);
  284.     example_espnow_data_prepare(send_param);
  285.     ESP_LOGI(TAG, "Came heree");
  286.     xTaskCreate(example_espnow_task, "example_espnow_task", 2048, send_param, 4, NULL);
  287.  
  288.     return ESP_OK;
  289. }
  290.  
  291. static void example_espnow_deinit(example_espnow_send_param_t *send_param)
  292. {
  293.     free(send_param->buffer);
  294.     free(send_param);
  295.     vSemaphoreDelete(s_example_espnow_queue);
  296.     esp_now_deinit();
  297. }
  298.  
  299. static void example_espnow_init(void)
  300. {
  301.  
  302.     s_example_espnow_queue = xQueueCreate(ESPNOW_QUEUE_SIZE, sizeof(example_espnow_event_t));
  303.     if (s_example_espnow_queue == NULL)
  304.     {
  305.         ESP_LOGE(TAG, "Create mutex fail");
  306.         return ESP_FAIL;
  307.     }
  308.  
  309.     /* Initialize ESPNOW and register sending and receiving callback function. */
  310.     ESP_ERROR_CHECK(esp_now_init());
  311.     ESP_ERROR_CHECK(esp_now_register_send_cb(example_espnow_send_cb));
  312.     ESP_ERROR_CHECK(esp_now_register_recv_cb(example_espnow_recv_cb));
  313.  
  314.     /* Set primary master key. */
  315.     ESP_ERROR_CHECK(esp_now_set_pmk((uint8_t *)CONFIG_ESPNOW_PMK));
  316.  
  317.     /* Add broadcast peer information to peer list. */
  318.     esp_now_peer_info_t *peer = malloc(sizeof(esp_now_peer_info_t));
  319.     if (peer == NULL)
  320.     {
  321.         ESP_LOGE(TAG, "Malloc peer information fail");
  322.         vSemaphoreDelete(s_example_espnow_queue);
  323.         esp_now_deinit();
  324.         return ESP_FAIL;
  325.     }
  326.     memset(peer, 0, sizeof(esp_now_peer_info_t));
  327.     peer->channel = CONFIG_ESPNOW_CHANNEL;
  328.     peer->ifidx = ESPNOW_WIFI_IF;
  329.     peer->encrypt = false;
  330.     memcpy(peer->peer_addr, s_example_broadcast_mac, ESP_NOW_ETH_ALEN);
  331.     ESP_ERROR_CHECK(esp_now_add_peer(peer));
  332.     free(peer);
  333. }
  334. void app_main()
  335. {
  336.     example_wifi_init();
  337.     example_espnow_init();
  338.     send_data();
  339.  
  340. }
  341.  
I am able to send data using espnow by writing the data to be sent to "buf" in the function "example_espnow_data_prepare". But the thing is when I increase the number of data to be sent this error comes up .( Here increasing the number of data means increasing the length of "buf" array

Code: Select all

 buf->payload[0] = 3423; 
    buf->payload[1] = 454;  
    buf->payload[2] = 354;  
    buf->payload[3] = 654;
    buf->payload[4] =34;
    buf->payload[5] = 564;
   
)
This is the error
***ERROR*** A stack overflow in task main has been detected.
I have seen from similar questions that this comes up because of not having the required stack depth. But increasing the value 2048 here

Code: Select all

xTaskCreate(example_espnow_task, "example_espnow_task", 2048, send_param, 4, NULL);
doesn't solve the problem. I even tried using 30,000.

Any suggestion would be really helpful. Thankss in advance

ESP_Sprite
Posts: 9757
Joined: Thu Nov 26, 2015 4:08 am

Re: ***ERROR*** A stack overflow in task main has been detected.

Postby ESP_Sprite » Sun Dec 26, 2021 2:57 am

Code: Select all

xTaskCreate(example_espnow_task, "example_espnow_task", 2048, send_param, 4, NULL);
Increasing the stack there does not help as it's not the task which stack is overflowing; per the error message, it's the 'main' task stack that is the problem. You can adjust the stack it gets in menuconfig.

senudajayalath
Posts: 17
Joined: Sun Jan 03, 2021 8:00 am

Re: ***ERROR*** A stack overflow in task main has been detected.

Postby senudajayalath » Sun Dec 26, 2021 4:43 am

ESP_Sprite wrote:
Sun Dec 26, 2021 2:57 am
it's the 'main' task stack that is the problem. You can adjust the stack it gets in menuconfig.
Where can I find the menuconfig in my project. There doesn't seem to be any menuconfig file in the project. And how can I adjust the stack it gets. Can you please explain that as well. Thank you.

chegewara
Posts: 2375
Joined: Wed Jun 14, 2017 9:00 pm

Re: ***ERROR*** A stack overflow in task main has been detected.

Postby chegewara » Sun Dec 26, 2021 6:18 am

I think the error is misleading and here is your error:

Code: Select all

uint32_t payload[0]; //Real payload of ESPNOW data.

senudajayalath
Posts: 17
Joined: Sun Jan 03, 2021 8:00 am

Re: ***ERROR*** A stack overflow in task main has been detected.

Postby senudajayalath » Sun Dec 26, 2021 9:59 am

chegewara wrote:
Sun Dec 26, 2021 6:18 am

Code: Select all

uint32_t payload[0]; //Real payload of ESPNOW data.
Thank you very much. That solved the problem

Who is online

Users browsing this forum: No registered users and 219 guests