ESP32 variable scope problem
Posted: Thu Mar 25, 2021 6:15 am
I am working on converting a project from Microchip PIc32 to ESP32. Because I am using different peripherals - WiFi, SD Card, UART, I am using different "C" source files.
And here I came across the problem with variables not retaining values between different source files.
Here is an example: I have header file def.h and two source files sdcard.c and hub_main.c.
I will only show part of relevant code:
In def.h
In hub_main.c
And in sdcard.c
I have noticed that the value read from SD Card in FcmMsg1 is not retained.
That's why I log it both in (sdcard.c) SD_Read() and in (hub_main.c) app_main()
Here is part of the log:
Another thing that I noticed is that if I create a task in one source file like this:
xTaskCreate(udp_server_task, "udp_server", 4096, (void*)AF_INET, 5, NULL);
And then define function:
static void udp_server_task(void *pvParameters)
in another file, it doesn't compile, with the message that it can't find this function. Even though I define a prototype for this function.
I am working using Eclipse with IDF plugin.
What am I doing wrong? What is so different with ESP32 programming?
Any suggestions?
Wes
And here I came across the problem with variables not retaining values between different source files.
Here is an example: I have header file def.h and two source files sdcard.c and hub_main.c.
I will only show part of relevant code:
In def.h
Code: Select all
#define MAX_FCMMSG1 17
#define MAX_FCMMSG2 17
char FcmMsg1[MAX_FCMMSG1][20];
char FcmMsg2[MAX_FCMMSG2][10];
Code: Select all
#include "sdcard.h"
#include "def.h"
void app_main(void){
//Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
initData();
board_init();
initSD_Card();
SD_Read();
ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
wifi_init_softap();
uart_init();
xTaskCreate(uart_event_task, "uart_event_task", 2048, NULL, 12, NULL);
//added this to check the value in FcmMsg1[2]
ESP_LOGI(TAG, "*** msg1 2 %s", FcmMsg1[2]);
Code: Select all
#include "sdcard.h"
#include "def.h"
void SD_Read(void){
struct stat st;
FILE* f;
int size;
stat(MOUNT_POINT"/msg1.hex", &st);
size = st.st_size;
ESP_LOGI(TAG, "file msg1.hex size %d", size);
if(size > 0){
f = fopen(MOUNT_POINT"/msg1.hex", "r");
if (f == NULL) {
ESP_LOGE(TAG, "Failed to open file msg1.hex for reading");
return;
}
memset(&FcmMsg1, 0, sizeof(FcmMsg1)-1);
fread((void*) FcmMsg1, 1, size, f);
startupState.insteon = 1;
fclose(f);
}
ESP_LOGI(TAG, "msg1 2 %s", FcmMsg1[2]);
f = fopen(MOUNT_POINT"/msg2.hex", "r");
if (f == NULL) {
ESP_LOGE(TAG, "Failed to open file msg2.hex for reading");
return;
}
memset(&FcmMsg1, 0, sizeof(FcmMsg2)-1);
stat(MOUNT_POINT"/msg2.hex", &st);
size = st.st_size;
ESP_LOGI(TAG, "file msg2.hex size %d", size);
if(size > 0){
fread((void*) FcmMsg2, 1, size, f);
startupState.insteon = 1;
fclose(f);
}
ESP_LOGI(TAG, "msg2 2 %s", FcmMsg2[3]);
// All done, unmount partition and disable SDMMC or SPI peripheral
esp_vfs_fat_sdcard_unmount(mount_point, card);
ESP_LOGI(TAG, "Card unmounted");
//deinitialize the bus after all devices are removed
spi_bus_free(host.slot);
}
That's why I log it both in (sdcard.c) SD_Read() and in (hub_main.c) app_main()
Here is part of the log:
I have been working with microchip PIC for many years and I have never seen a problem like this.Name: SL64G
Type: SDHC/SDXC
Speed: 20 MHz
Size: 60906MB
.[0;32mI (762) SDCARD: file msg1.hex size 340.[0m
.[0;32mI (762) SDCARD: msg1 2 Kitchen Alarm .[0m
.[0;32mI (772) SDCARD: file msg2.hex size 170.[0m
.[0;32mI (772) SDCARD: msg2 2 CLOSED .[0m
.[0;32mI (822) SDCARD: Card unmounted.[0m
.[0;32mI (822) Hub1: ESP_WIFI_MODE_AP.[0m
...
...
.[0;32mI (1052) Hub1: wifi_init_softap finished.
.[0;32mI (1062) uart: queue free spaces: 24.[0m
.[0;32mI (1062) Hub1: *** msg1 2 .[0m
Another thing that I noticed is that if I create a task in one source file like this:
xTaskCreate(udp_server_task, "udp_server", 4096, (void*)AF_INET, 5, NULL);
And then define function:
static void udp_server_task(void *pvParameters)
in another file, it doesn't compile, with the message that it can't find this function. Even though I define a prototype for this function.
I am working using Eclipse with IDF plugin.
What am I doing wrong? What is so different with ESP32 programming?
Any suggestions?
Wes