Skip to content

Commit

Permalink
Added option to format SD card
Browse files Browse the repository at this point in the history
  • Loading branch information
ducalex committed Feb 26, 2021
1 parent 91c283d commit 7ebc009
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 23 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ _Note: There is no risk in flashing your Odroid GO and you can easily return to


# Compilation
The official esp-idf version 3.1 or newer is required and you need to apply the following patches:
The official esp-idf version 3.1 or newer is required and you need to apply the following patches (found in tools/patches):

- [Allow clearing internal partition list](https://github.com/OtherCrashOverride/esp-idf/commit/49fbef73c300920d2f63c9afb705eefabe3dac87) (Required)
- [Improve SD card compatibility](https://github.com/OtherCrashOverride/esp-idf/commit/a83e557538a033e25c376eedac79663c9b7b75da) (Recommended)

You can also find the patches in the tools/patches folder.
- esp-idf-partition-patch.diff
- esp-idf-sdcard-patch.diff

_Note: Those patches do not introduce breaking changes to non-GO (standard ESP32) projects and can safely be applied to your global esp-idf installation._

Expand Down
34 changes: 28 additions & 6 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <rom/crc.h>
#endif

#include <sys/stat.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
Expand Down Expand Up @@ -56,7 +57,8 @@
#define APP_SORT_DIR_ASC 0b0000
#define APP_SORT_DIR_DESC 0b0001

#define FLASH_SIZE (16 * 1024 * 1024)
// #define FLASH_SIZE (16 * 1024 * 1024)
#define FLASH_SIZE ((int)spi_flash_get_chip_size())
#define FLASH_BLOCK_SIZE (64 * 1024)
#define ERASE_BLOCK_SIZE (4 * 1024)

Expand All @@ -77,8 +79,8 @@
#define LED_ON() gpio_set_level(GPIO_NUM_2, 1);
#define LED_OFF() gpio_set_level(GPIO_NUM_2, 0);

const char* SD_CARD = "/sd";
const char* FIRMWARE_PATH = "/sd/odroid/firmware";
#define SD_CARD "/sd"
#define FIRMWARE_PATH "/sd/odroid/firmware"

//const char* HEADER = "ODROIDGO_FIRMWARE_V00_00";
const char* HEADER_V00_01 = "ODROIDGO_FIRMWARE_V00_01";
Expand Down Expand Up @@ -280,7 +282,7 @@ static void DisplayMessage(char* message)
UpdateDisplay();
}

static void DisplayNotification(char* message)
static void DisplayNotification(const char* message)
{
UG_FontSelect(&FONT_8X12);
short left = (320 / 2) - (strlen(message) * 9 / 2);
Expand Down Expand Up @@ -1470,11 +1472,11 @@ void ui_choose_app()
{1, "Erase selected app", apps_count > 0},
{2, "Erase selected NVS", apps_count > 0},
{3, "Erase all apps", apps_count > 0},
// {4, "Format SD Card", true},
{4, "Format SD Card", true},
{5, "Restart System", true}
};

int choice = ui_choose_dialog(options, 5, true);
int choice = ui_choose_dialog(options, 6, true);
char* fileName;

switch(choice) {
Expand Down Expand Up @@ -1509,6 +1511,26 @@ void ui_choose_app()
write_partition_table(NULL, 0, 0);
break;
case 4: // Format SD Card
ui_draw_title("Format SD Card", PROJECT_VER);
DisplayMessage("Press start to begin");
if (wait_for_button_press(50000) != ODROID_INPUT_START) {
break;
}
DisplayMessage("Formatting... (be patient)");
sdcardret = odroid_sdcard_format(0);
if (sdcardret == ESP_OK) {
sdcardret = odroid_sdcard_open(SD_CARD);
}
if (sdcardret == ESP_OK) {
char path[32] = SD_CARD "/odroid";
mkdir(path, 0777);
strcat(path, "/firmware");
mkdir(path, 0777);
DisplayMessage("Card formatted!");
} else {
DisplayError("Format failed!");
}
wait_for_button_press(50000);
break;
case 5: // Restart
cleanup_and_restart();
Expand Down
97 changes: 91 additions & 6 deletions main/odroid_sdcard.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <driver/sdmmc_host.h>
#include <driver/sdspi_host.h>
#include <sdmmc_cmd.h>
#include <diskio.h>
#include <esp_heap_caps.h>
#include <esp_log.h>

Expand All @@ -23,7 +24,6 @@
static bool isOpen = false;



inline static void swap(char** a, char** b)
{
char* t = *a;
Expand Down Expand Up @@ -77,7 +77,6 @@ static void sort_files(char** files, int count)
}



int odroid_sdcard_files_get(const char* path, const char* extension, char*** filesOut)
{
const int MAX_FILES = 1024;
Expand Down Expand Up @@ -216,7 +215,6 @@ esp_err_t odroid_sdcard_open(const char* base_path)
return ret;
}


esp_err_t odroid_sdcard_close()
{
esp_err_t ret;
Expand All @@ -230,16 +228,19 @@ esp_err_t odroid_sdcard_close()
{
ret = esp_vfs_fat_sdmmc_unmount();

if (ret != ESP_OK)
if (ret == ESP_OK)
{
ESP_LOGE(__func__, "esp_vfs_fat_sdmmc_unmount failed (%d)", ret);
isOpen = false;
}
else
{
ESP_LOGE(__func__, "esp_vfs_fat_sdmmc_unmount failed (%d)", ret);
}
}

return ret;
}


size_t odroid_sdcard_get_filesize(const char* path)
{
size_t ret = 0;
Expand Down Expand Up @@ -308,3 +309,87 @@ size_t odroid_sdcard_copy_file_to_memory(const char* path, void* ptr)

return ret;
}

esp_err_t odroid_sdcard_format(int fs_type)
{
esp_err_t err = ESP_FAIL;
const char *errmsg = "success!";
sdmmc_card_t card;
void *buffer = malloc(4096);
DWORD partitions[] = {100, 0, 0, 0};
BYTE drive = 0xFF;

sdmmc_host_t host_config = SDSPI_HOST_DEFAULT();
host_config.slot = HSPI_HOST;

sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
slot_config.gpio_miso = (gpio_num_t)SD_PIN_NUM_MISO;
slot_config.gpio_mosi = (gpio_num_t)SD_PIN_NUM_MOSI;
slot_config.gpio_sck = (gpio_num_t)SD_PIN_NUM_CLK;
slot_config.gpio_cs = (gpio_num_t)SD_PIN_NUM_CS;

if (buffer == NULL) {
return false;
}

if (isOpen) {
odroid_sdcard_close();
}

err = ff_diskio_get_drive(&drive);
if (drive == 0xFF) {
errmsg = "ff_diskio_get_drive() failed";
goto _cleanup;
}

err = (*host_config.init)();
if (err != ESP_OK) {
errmsg = "host_config.init() failed";
goto _cleanup;
}

err = sdspi_host_init_slot(host_config.slot, &slot_config);
if (err != ESP_OK) {
errmsg = "sdspi_host_init_slot() failed";
goto _cleanup;
}

err = sdmmc_card_init(&host_config, &card);
if (err != ESP_OK) {
errmsg = "sdmmc_card_init() failed";
goto _cleanup;
}

ff_diskio_register_sdmmc(drive, &card);

ESP_LOGI(__func__, "partitioning card %d", drive);
if (f_fdisk(drive, partitions, buffer) != FR_OK) {
errmsg = "f_fdisk() failed";
err = ESP_FAIL;
goto _cleanup;
}

ESP_LOGI(__func__, "formatting card %d", drive);
char path[3] = {(char)('0' + drive), ':', 0};
if (f_mkfs(path, fs_type ? FM_EXFAT : FM_FAT32, 0, buffer, 4096) != FR_OK) {
errmsg = "f_mkfs() failed";
err = ESP_FAIL;
goto _cleanup;
}

err = ESP_OK;

_cleanup:

if (err == ESP_OK) {
ESP_LOGI(__func__, "%s", errmsg);
} else {
ESP_LOGE(__func__, "%s (%d)", errmsg, err);
}

free(buffer);
host_config.deinit();
ff_diskio_unregister(drive);

return err;
}
1 change: 1 addition & 0 deletions main/odroid_sdcard.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ esp_err_t odroid_sdcard_open();
esp_err_t odroid_sdcard_close();
size_t odroid_sdcard_get_filesize(const char* path);
size_t odroid_sdcard_copy_file_to_memory(const char* path, void* ptr);
esp_err_t odroid_sdcard_format(int fs_type);
19 changes: 13 additions & 6 deletions tools/patches/esp-idf-sdcard-patch.diff
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
diff --git a/components/driver/sdspi_host.c b/components/driver/sdspi_host.c
index 4975b857d..c31286047 100644
index e823ee7fa..79aef3130 100644
--- a/components/driver/sdspi_host.c
+++ b/components/driver/sdspi_host.c
@@ -622,7 +622,9 @@ static esp_err_t poll_cmd_response(int slot, sdspi_hw_cmd_t *cmd)
*/
@@ -278,7 +278,7 @@ esp_err_t sdspi_host_init_slot(int slot, const sdspi_slot_config_t* slot_config)
// Initialize SPI bus
esp_err_t ret = spi_bus_initialize((spi_host_device_t)slot, &buscfg,
slot_config->dma_channel);
- if (ret != ESP_OK) {
+ if (ret != ESP_OK && ret != ESP_ERR_INVALID_STATE) {
ESP_LOGD(TAG, "spi_bus_initialize failed with rc=0x%x", ret);
return ret;
}
@@ -623,6 +623,8 @@ static esp_err_t poll_cmd_response(int slot, sdspi_hw_cmd_t *cmd)
static esp_err_t start_command_read_blocks(int slot, sdspi_hw_cmd_t *cmd,
uint8_t *data, uint32_t rx_length)
-{
+{
{
+ go_idle_clockout(slot);
+
bool need_stop_command = rx_length > SDSPI_MAX_DATA_LEN;
spi_transaction_t* t_command = get_transaction(slot);
*t_command = (spi_transaction_t) {
*t_command = (spi_transaction_t) {

0 comments on commit 7ebc009

Please sign in to comment.