Negli ultimi due articoli di questo tutorial, vi ho mostrato come aggiornare over the air il firmware in esecuzione nel chip esp32.
A volte è però necessario ritornare al firmware di fabbrica, ovvero quello memorizzato nella flash al momento della programmazione del chip. Molti dispositivi elettronici dispongono di un pulsante o pin che, se premuto per qualche secondo, effettua questo reset:
In questo articolo vi mostrerò come aggiungere questa possibilità al vostro progetto.
Partizioni
Come spiegato in un precedente articolo, la memoria flash collegata al chip esp32 è suddivisa in diverse partizioni, secondo un layout configurato in fase di programmazione del chip.
Le partizioni che possono contenere un firmware sono di tipo app. La partizione che contiene il firmware memorizzato in fase di programmazione ha il sottotipo factory.
Il framework esp-idf mette a disposizione una funzione per cercare le partizioni all’interno della memoria flash:
#include "esp_partition.h" [...] esp_partition_iterator_t esp_partition_find( esp_partition_type_t type, esp_partition_subtype_t subtype, const char* label); |
E’ possibile indicare alcuni filtri per restringere la ricerca: il tipo (type) di partizione, l’eventuale sottotipo (subtype) e infine una etichetta (label).
Per cercare la partizione che contiene il firmware di fabbrica possiamo quindi scrivere:
esp_partition_iterator_t pi = esp_partition_find( ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_FACTORY, NULL); |
La funzione restituisce un partition iterator, ovvero un oggetto che consente di scorrere tra le diverse partizioni trovate.
Se la ricerca ha avuto successo, tale oggetto è diverso da NULL ed è possibile ottenere un puntatore alla partizione con la funzione:
const esp_partition_t* esp_partition_get(esp_partition_iterator_t iterator); |
Una volta terminato l’uso, è importante rilasciare l’oggetto iterator con:
void esp_partition_iterator_release(esp_partition_iterator_t iterator); |
Ottenuta la partizione con il firmware di fabbrica, non rimane che configurarla nuovamente come partizione di boot e riavviare il chip:
if(pi != NULL) { const esp_partition_t* factory = esp_partition_get(pi); esp_partition_iterator_release(pi); if(esp_ota_set_boot_partition(factory) == ESP_OK) esp_restart(); } |
Demo
Nel video seguente (sottotitoli in italiano disponibili), vi mostro come effettuare il factory reset del chip. Nel video spiego inoltre come poter “contare” il numero di secondi in cui un pulsante è premuto per attivare la procedura di reset solo oltre una soglia fissata (3 secondi in questo esempio). Buona visione!
Ciao Luca. I cannot find video link on this post. Anyway I’ve found it on Google. Good job! I didn’t know this function. Thank you
hi
it’s possibile to make a tool like this
http://handheldsci.com/kb
with esp32?
transform a usb keyboard in a bluetooth one
thanks
not an easy way because of esp32 doesn’t include an hardware usb host controller (needed to connect to usb keyboard/mouse)
USB host isn’t required for a keyboard or mouse. The vast majority of USB keyboards and mice are backwards compatible with ps/2. Those little USB to ps/2 converters that used to be bundled with usb keyboards and mice are purely mechanical.
USB data + becomes the ps/2 clock and USB data – becomes ps/2 clock. No idea of the voltages, I’d assume about 4.5~5 volts but after that problems been handled I’m almost certain you can interface almost any USB keyboard or mouse to an ESP32 and read the keystrokes / mouse input.
USB data – becomes ps/2 data.***
Hello Luca, thank you, this is a very useful tutorial!
I could not find the source code in your repository, maybe I am missing something, or will it be available soon? thanks!
hi, as the code to perform the reset is only 6-7 lines (described in the article) I didn’t prepare a full example
Hi Luca, a great set of tutorials for an ESP32 newbie like me. I started learning with a Joy-It board. I use the Arduino IDE to upload sketches (.ino). I do not completely grasp the difference between firmware and an app, which is, in my view, the code in the uploaded .ino. This is because of the following: when I power down after uploading and running a sketch and then power up again, it is the sketch code that starts running again. So, apparently, the code is still in the memory. In my understanding this is a property of flash memory. So: what is the difference between flashing new firmaware and uploading a sketch/app? For me it looks the same.
Hi Fred, thanks for your comment. In Arduino world, the two are the same (app/firmware). But with the esp32 you can “store” different firmwares (in the APP partitions) and decide which one you want to execute. The common way to use it is when you store a factory firmware when programming the chip and then send updated firmwares later… you can have at the same time in your flash both the original firmware and the new one.