Access an sdCard during http GET

GilbertW
Posts: 4
Joined: Sat Oct 02, 2021 11:15 am

Access an sdCard during http GET

Postby GilbertW » Fri Dec 03, 2021 9:40 am

In their examples, Espressif provide a simple http server. https://github.com/espressif/esp-idf/tr ... ver/simple
When a GET-request arrives, a handler replies with a string:

Code: Select all

// GET request handler  (pseudo code)
static esp_err_t basic_auth_get_handler(httpd_req_t *req) {
 …
	// Send a string reply
	httpd_resp_send(“some string”));   //etc.
}
(The handler is a callback registered via the httpd_register_uri_handler() ESP32 library API https://docs.espressif.com/projects/esp ... ttpd_uri_t)

In my case however, the string is stored on an sdCard. So I want to do something like

Code: Select all

// GET request handler (pseudo-code)
static esp_err_t basic_auth_get_handler(httpd_req_t *req) {
…
	// Fetch reply-string from sd-card 
	myString = read_sdcard()
	httpd_resp_send(myString));   //etc.
}
Elsewhere however, I have a freeRTOS task that occasionally writes to the same sdCard, and I'm worried about problems should read_sdcard() occur whilst a write is ongoing.
To solve this, I am thinking to use a mutex to co-ordinate reads/writes. Something like:

Code: Select all

//GET request handler (pseudo-code)
static esp_err_t basic_auth_get_handler(httpd_req_t *req) {
…
	// Use a mutex to safely access sdCard.  
	if (xSemaphoreTake(myMutex, portMAX_DELAY) == pdTRUE) {
		myString = read_sd_card(myFile)
		xSemaphoreGive(myMutex);
	}
	httpd_resp_send(myString));  // etc
}
Is this a sensible approach? Is it safe to add RTOS "stuff" like this inside a get_handler() ?

FreeRTOS say " xSemaphoreTake() must not be called from an ISR“
I don’t know how Espressif’s http server works ‘under the hood’.
Can I be confident that basic_auth_get_handler() isn’t an ISR?

thanks for any advice

Who is online

Users browsing this forum: Sang_Huynh and 134 guests