Skip to content

Commit

Permalink
feat: add menu to format sd card
Browse files Browse the repository at this point in the history
  • Loading branch information
DeimosHall committed Sep 26, 2024
1 parent fcf8adf commit 195c855
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 25 deletions.
17 changes: 14 additions & 3 deletions firmware/components/sd_card/include/sd_card.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,24 @@ esp_err_t sd_card_mount();
esp_err_t sd_card_unmount();

/**
* Format the SD card if mount failed.
* Mount the SD card.
*
* @return esp_err_t
*
* @note return ESP_ERR_NOT_MOUNTED if the SD card is not mounted.
* @note return ESP_ERR_NOT_FOUND if the SD card is not found.
* @note return ESP_ERR_NO_MEM if failed to initialize the spi bus.
* @note return ESP_ERR_NOT_SUPPORTED if the SD card is not formatted with FAT.
* @note return ESP_ERR_INVALID_ARG if the arguments are invalid.
* @note return ESP_FAIL if the operation failed.
* @note return ESP_OK if the operation was successful.
* @note return ESP_OK if the operation was successful or the card is already
* mounted.
*/
esp_err_t sd_card_check_format();

/**
* Format the SD card.
*
* @return esp_err_t
*/
esp_err_t sd_card_format();

Expand Down
75 changes: 73 additions & 2 deletions firmware/components/sd_card/sd_card.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ int mount(int argc, char** argv) {
}
}
/* print card info if mount successfully */
sdmmc_card_print_info(stdout, card);
// sdmmc_card_print_info(stdout, card);
sd_card_fill_info(card);
}
return ESP_OK;
Expand Down Expand Up @@ -224,14 +224,85 @@ esp_err_t sd_card_unmount() {
return err;
}

esp_err_t sd_card_format() {
esp_err_t sd_card_check_format() {
ESP_LOGI(TAG, "Formatting SD Card...");
_format_if_mount_failed = true;
esp_err_t err = sd_card_mount();
_format_if_mount_failed = false;
return err;
}

esp_err_t sd_card_format() {
sd_card_unmount();
esp_err_t ret;
/* mount sd card */
ESP_LOGI(TAG, "Initializing SD card, format: %s",
_format_if_mount_failed ? "true" : "false");
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
.format_if_mount_failed = true,
.max_files = 4,
.allocation_unit_size = 16 * 1024};

// initialize SD card and mount FAT filesystem.
sdmmc_card_t* card;

ESP_LOGI(TAG, "Using SPI peripheral");
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
spi_bus_config_t bus_cfg = {
.mosi_io_num = PIN_NUM_MOSI,
.miso_io_num = PIN_NUM_MISO,
.sclk_io_num = PIN_NUM_CLK,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 4000,
};
ret = spi_bus_initialize(host.slot, &bus_cfg, SPI_DMA_CH_AUTO);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize bus.");
return ESP_ERR_NO_MEM;
}

// This initializes the slot without card detect (CD) and write protect (WP)
// signals. Modify slot_config.gpio_cd and slot_config.gpio_wp if your board
// has these signals.
sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
slot_config.gpio_cs = PIN_NUM_CS;
slot_config.host_id = host.slot;

ret = esp_vfs_fat_sdspi_mount(MOUNT_POINT, &host, &slot_config, &mount_config,
&card);

if (ret != ESP_OK) {
if (ret == ESP_FAIL) {
ESP_LOGE(TAG,
"Failed to mount filesystem. "
"If you want the card to be formatted, set "
"format_if_mount_failed = true.");
spi_bus_free(host.slot);
return ESP_ERR_NOT_SUPPORTED;
} else {
ESP_LOGE(TAG,
"Failed to initialize the card (%s). "
"Make sure SD card lines have pull-up resistors in place.",
esp_err_to_name(ret));
// Free the bus after mounting failed
spi_bus_free(host.slot);
return ESP_ERR_NOT_MOUNTED;
}
}
/* print card info if mount successfully */
// sdmmc_card_print_info(stdout, card);
sd_card_fill_info(card);
ret = esp_vfs_fat_sdcard_format(MOUNT_POINT, card);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to format SD card");
return ESP_FAIL;
}

return ESP_OK;
// return err;
}

bool sd_card_is_mounted() {
return _sd_card_mounted;
}
Expand Down
2 changes: 1 addition & 1 deletion firmware/main/modules/gps/wardriving/wardriving_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ void wardriving_module_keyboard_cb(uint8_t button_name, uint8_t button_event) {
} else if (wardriving_module_state ==
WARDRIVING_MODULE_STATE_INVALID_SD_CARD) {
wardriving_screens_module_formating_sd_card();
esp_err_t err = sd_card_format();
esp_err_t err = sd_card_check_format();
if (err == ESP_OK) {
ESP_LOGI(TAG, "Format done");
wardriving_module_start_scan();
Expand Down
8 changes: 8 additions & 0 deletions firmware/main/modules/menus_module/menus_include/menus.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ typedef enum {
MENU_SETTINGS_WIFI,
MENU_SETTINGS_SD_CARD,
MENU_SETTINGS_SD_CARD_INFO,
MENU_SETTINGS_SD_CARD_CHECK_FORMAT,
MENU_SETTINGS_SD_CARD_FORMAT,
MENU_STEALTH_MODE,
/* Menu count */
Expand Down Expand Up @@ -467,6 +468,13 @@ menu_t menus[] = { //////////////////////////////////
.on_exit_cb = NULL,
.is_visible = true},
{.display_name = "Check Format",
.menu_idx = MENU_SETTINGS_SD_CARD_CHECK_FORMAT,
.parent_idx = MENU_SETTINGS_SD_CARD,
.last_selected_submenu = 0,
.on_enter_cb = sd_card_settings_verify_sd_card,
.on_exit_cb = NULL,
.is_visible = true},
{.display_name = "Format",
.menu_idx = MENU_SETTINGS_SD_CARD_FORMAT,
.parent_idx = MENU_SETTINGS_SD_CARD,
.last_selected_submenu = 0,
Expand Down
69 changes: 52 additions & 17 deletions firmware/main/modules/settings/sd_card/sd_card_settings_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const char* TAG = "sd_card_settings_module";

typedef enum {
SD_CARD_SETTINGS_VERIFYING = 0,
SD_CARD_SETTINGS_OK,
SD_CARD_SETTINGS_MOUNT_OK,
SD_CARD_SETTINGS_WRONG_FORMAT,
SD_CARD_SETTINGS_FORMAT_QUESTION,
SD_CARD_SETTINGS_FORMATTING,
SD_CARD_SETTINGS_FORMAT_DONE,
Expand All @@ -19,9 +20,8 @@ typedef enum {
} sd_card_settings_state_t;

const char* sd_card_state_to_name[] = {
"Verifying", "OK", "Format question",
"Formatting", "Format done", "Failed format",
"No SD card",
"Verifying", "Mount OK", "Wrong format", "Format question",
"Formatting", "Format done", "Failed format", "No SD card",
};

sd_card_settings_state_t state = SD_CARD_SETTINGS_VERIFYING;
Expand All @@ -30,17 +30,30 @@ void sd_card_settings_verify_sd_card() {
menus_module_set_app_state(true, sd_card_settings_keyboard_cb);
ESP_LOGI(TAG, "Verifying SD card...");
state = SD_CARD_SETTINGS_VERIFYING;
bool format = false;
if (menus_module_get_current_menu() == MENU_SETTINGS_SD_CARD_FORMAT) {
format = true;
}

esp_err_t err = sd_card_mount();
if (err == ESP_ERR_NOT_SUPPORTED) {
state = SD_CARD_SETTINGS_FORMAT_QUESTION;
sd_card_settings_screens_module_format_question();
} else if (err == ESP_OK) {
state = SD_CARD_SETTINGS_OK;
sd_card_settings_screens_module_sd_card_ok();
} else if (err != ESP_OK) {
state = SD_CARD_SETTINGS_NO_SD_CARD;
sd_card_settings_screens_module_no_sd_card();
switch (err) {
case ESP_ERR_NOT_SUPPORTED:
state = SD_CARD_SETTINGS_WRONG_FORMAT;
sd_card_settings_screens_module_wrong_format();
break;
case ESP_OK:
if (format) {
state = SD_CARD_SETTINGS_FORMAT_QUESTION;
sd_card_settings_screens_module_format_question();
} else {
state = SD_CARD_SETTINGS_MOUNT_OK;
sd_card_settings_screens_module_sd_card_ok();
}
break;
default:
state = SD_CARD_SETTINGS_NO_SD_CARD;
sd_card_settings_screens_module_no_sd_card();
break;
}
sd_card_unmount();
}
Expand All @@ -56,10 +69,11 @@ void sd_card_settings_keyboard_cb(uint8_t button_name, uint8_t button_event) {
break;
case BUTTON_RIGHT:
ESP_LOGI(TAG, "State: %s", sd_card_state_to_name[state]);
esp_err_t err;
switch (state) {
case SD_CARD_SETTINGS_FORMAT_QUESTION:
case SD_CARD_SETTINGS_WRONG_FORMAT:
sd_card_settings_screens_module_formating_sd_card();
esp_err_t err = sd_card_format();
err = sd_card_check_format();
if (err == ESP_OK) {
ESP_LOGI(TAG, "Format done");
state = SD_CARD_SETTINGS_FORMAT_DONE;
Expand All @@ -69,9 +83,30 @@ void sd_card_settings_keyboard_cb(uint8_t button_name, uint8_t button_event) {
sd_card_settings_screens_module_failed_format_sd_card();
}
break;
case SD_CARD_SETTINGS_OK:
menus_module_exit_app();
case SD_CARD_SETTINGS_FORMAT_QUESTION:
sd_card_settings_screens_module_formating_sd_card();
err = sd_card_check_format();
if (err == ESP_OK) {
ESP_LOGI(TAG, "Mount ok, formatting...");
state = SD_CARD_SETTINGS_FORMATTING;
esp_err_t err_format = sd_card_format();
if (err_format == ESP_OK) {
state = SD_CARD_SETTINGS_FORMAT_DONE;
sd_card_settings_screens_module_format_done();
} else {
state = SD_CARD_SETTINGS_FAILED_FORMAT;
sd_card_settings_screens_module_failed_format_sd_card();
}
} else {
state = SD_CARD_SETTINGS_FAILED_FORMAT;
sd_card_settings_screens_module_failed_format_sd_card();
}
break;
case SD_CARD_SETTINGS_FORMATTING:
// TODO: implement on LEFT button
ESP_LOGI(TAG, "Formatting...");
break;
case SD_CARD_SETTINGS_MOUNT_OK:
default:
menus_module_exit_app();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void sd_card_settings_screens_module_sd_card_ok() {
oled_screen_display_text_center("Ok", screen_64 ? 5 : 3, OLED_DISPLAY_INVERT);
}

void sd_card_settings_screens_module_format_question() {
void sd_card_settings_screens_module_wrong_format() {
oled_screen_clear();
oled_screen_display_text_center("SD Card is not", screen_64 ? 1 : 0,
OLED_DISPLAY_NORMAL);
Expand All @@ -35,6 +35,17 @@ void sd_card_settings_screens_module_format_question() {
oled_screen_display_text_center("Ok", screen_64 ? 5 : 3, OLED_DISPLAY_INVERT);
}

void sd_card_settings_screens_module_format_question() {
oled_screen_clear();
oled_screen_display_text_center("SD Card data", screen_64 ? 1 : 0,
OLED_DISPLAY_NORMAL);
oled_screen_display_text_center("will be lost!", screen_64 ? 2 : 1,
OLED_DISPLAY_NORMAL);
oled_screen_display_text_center("Format?", screen_64 ? 3 : 2,
OLED_DISPLAY_NORMAL);
oled_screen_display_text_center("Ok", screen_64 ? 5 : 3, OLED_DISPLAY_INVERT);
}

void sd_card_settings_screens_module_formating_sd_card() {
oled_screen_clear();
oled_screen_display_text_center("Formating SD", screen_64 ? 2 : 0,
Expand All @@ -58,5 +69,5 @@ void sd_card_settings_screens_module_format_done() {
oled_screen_clear();
oled_screen_display_text_center("Format done!", screen_64 ? 3 : 1,
OLED_DISPLAY_NORMAL);
oled_screen_display_text_center("Ok", screen_64 ? 3 : 0, OLED_DISPLAY_INVERT);
oled_screen_display_text_center("Ok", screen_64 ? 5 : 3, OLED_DISPLAY_INVERT);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ void sd_card_settings_screens_module_no_sd_card();

void sd_card_settings_screens_module_sd_card_ok();

void sd_card_settings_screens_module_wrong_format();

void sd_card_settings_screens_module_format_question();

void sd_card_settings_screens_module_formating_sd_card();
Expand Down

0 comments on commit 195c855

Please sign in to comment.