nx_sd: add sd_get_card_mounted() for screenshot and other callers

Made-with: Cursor
This commit is contained in:
2026-03-01 14:44:37 +01:00
parent 9a2307a8ee
commit b0523ada6c

View File

@@ -1,228 +1,233 @@
/* /*
* Copyright (c) 2018 naehrwert * Copyright (c) 2018 naehrwert
* Copyright (c) 2018-2019 CTCaer * Copyright (c) 2018-2019 CTCaer
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License, * under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation. * version 2, as published by the Free Software Foundation.
* *
* This program is distributed in the hope it will be useful, but WITHOUT * This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details. * more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <storage/nx_sd.h> #include <storage/nx_sd.h>
#include <storage/sdmmc.h> #include <storage/sdmmc.h>
#include <storage/sdmmc_driver.h> #include <storage/sdmmc_driver.h>
#include <gfx_utils.h> #include <gfx_utils.h>
#include <libs/fatfs/ff.h> #include <libs/fatfs/ff.h>
#include <mem/heap.h> #include <mem/heap.h>
bool sd_mounted = false; bool sd_mounted = false;
static u16 sd_errors[3] = { 0 }; // Init and Read/Write errors. static u16 sd_errors[3] = { 0 }; // Init and Read/Write errors.
static u32 sd_mode = SD_UHS_SDR82; static u32 sd_mode = SD_UHS_SDR82;
sdmmc_t sd_sdmmc; sdmmc_t sd_sdmmc;
sdmmc_storage_t sd_storage; sdmmc_storage_t sd_storage;
FATFS sd_fs; FATFS sd_fs;
void sd_error_count_increment(u8 type) void sd_error_count_increment(u8 type)
{ {
switch (type) switch (type)
{ {
case SD_ERROR_INIT_FAIL: case SD_ERROR_INIT_FAIL:
sd_errors[0]++; sd_errors[0]++;
break; break;
case SD_ERROR_RW_FAIL: case SD_ERROR_RW_FAIL:
sd_errors[1]++; sd_errors[1]++;
break; break;
case SD_ERROR_RW_RETRY: case SD_ERROR_RW_RETRY:
sd_errors[2]++; sd_errors[2]++;
break; break;
} }
} }
u16 *sd_get_error_count() u16 *sd_get_error_count()
{ {
return sd_errors; return sd_errors;
} }
bool sd_get_card_removed() bool sd_get_card_removed()
{ {
if (!sdmmc_get_sd_inserted()) if (!sdmmc_get_sd_inserted())
return true; return true;
return false; return false;
} }
u32 sd_get_mode() u32 sd_get_mode()
{ {
return sd_mode; return sd_mode;
} }
int sd_init_retry(bool power_cycle) int sd_init_retry(bool power_cycle)
{ {
u32 bus_width = SDMMC_BUS_WIDTH_4; u32 bus_width = SDMMC_BUS_WIDTH_4;
u32 type = SDHCI_TIMING_UHS_SDR82; u32 type = SDHCI_TIMING_UHS_SDR82;
// Power cycle SD card. // Power cycle SD card.
if (power_cycle) if (power_cycle)
{ {
sd_mode--; sd_mode--;
sdmmc_storage_end(&sd_storage); sdmmc_storage_end(&sd_storage);
} }
// Get init parameters. // Get init parameters.
switch (sd_mode) switch (sd_mode)
{ {
case SD_INIT_FAIL: // Reset to max. case SD_INIT_FAIL: // Reset to max.
return 0; return 0;
case SD_1BIT_HS25: case SD_1BIT_HS25:
bus_width = SDMMC_BUS_WIDTH_1; bus_width = SDMMC_BUS_WIDTH_1;
type = SDHCI_TIMING_SD_HS25; type = SDHCI_TIMING_SD_HS25;
break; break;
case SD_4BIT_HS25: case SD_4BIT_HS25:
type = SDHCI_TIMING_SD_HS25; type = SDHCI_TIMING_SD_HS25;
break; break;
case SD_UHS_SDR82: case SD_UHS_SDR82:
type = SDHCI_TIMING_UHS_SDR82; type = SDHCI_TIMING_UHS_SDR82;
break; break;
default: default:
sd_mode = SD_UHS_SDR82; sd_mode = SD_UHS_SDR82;
} }
return sdmmc_storage_init_sd(&sd_storage, &sd_sdmmc, bus_width, type); return sdmmc_storage_init_sd(&sd_storage, &sd_sdmmc, bus_width, type);
} }
bool sd_initialize(bool power_cycle) bool sd_initialize(bool power_cycle)
{ {
if (power_cycle) if (power_cycle)
sdmmc_storage_end(&sd_storage); sdmmc_storage_end(&sd_storage);
int res = !sd_init_retry(false); int res = !sd_init_retry(false);
while (true) while (true)
{ {
if (!res) if (!res)
return true; return true;
else if (!sdmmc_get_sd_inserted()) // SD Card is not inserted. else if (!sdmmc_get_sd_inserted()) // SD Card is not inserted.
{ {
sd_mode = SD_UHS_SDR82; sd_mode = SD_UHS_SDR82;
break; break;
} }
else else
{ {
sd_errors[SD_ERROR_INIT_FAIL]++; sd_errors[SD_ERROR_INIT_FAIL]++;
if (sd_mode == SD_INIT_FAIL) if (sd_mode == SD_INIT_FAIL)
break; break;
else else
res = !sd_init_retry(true); res = !sd_init_retry(true);
} }
} }
sdmmc_storage_end(&sd_storage); sdmmc_storage_end(&sd_storage);
return false; return false;
} }
bool is_sd_inited = false; bool is_sd_inited = false;
bool sd_mount() bool sd_mount()
{ {
if (sd_mounted) if (sd_mounted)
return true; return true;
int res = !sd_initialize(false); int res = !sd_initialize(false);
is_sd_inited = !res; is_sd_inited = !res;
if (res) if (res)
{ {
gfx_con.mute = false; gfx_con.mute = false;
EPRINTF("Failed to init SD card."); EPRINTF("Failed to init SD card.");
if (!sdmmc_get_sd_inserted()) if (!sdmmc_get_sd_inserted())
EPRINTF("Make sure that it is inserted."); EPRINTF("Make sure that it is inserted.");
else else
EPRINTF("SD Card Reader is not properly seated!"); EPRINTF("SD Card Reader is not properly seated!");
} }
else else
{ {
res = f_mount(&sd_fs, "", 1); res = f_mount(&sd_fs, "", 1);
if (res == FR_OK) if (res == FR_OK)
{ {
sd_mounted = true; sd_mounted = true;
return true; return true;
} }
else else
{ {
gfx_con.mute = false; gfx_con.mute = false;
EPRINTFARGS("Failed to mount SD card (FatFS Error %d).\nMake sure that a FAT partition exists..", res); EPRINTFARGS("Failed to mount SD card (FatFS Error %d).\nMake sure that a FAT partition exists..", res);
} }
} }
return false; return false;
} }
static void _sd_deinit() static void _sd_deinit()
{ {
if (sd_mode == SD_INIT_FAIL) if (sd_mode == SD_INIT_FAIL)
sd_mode = SD_UHS_SDR82; sd_mode = SD_UHS_SDR82;
if (sd_mounted) if (sd_mounted)
{ {
f_mount(NULL, "", 1); f_mount(NULL, "", 1);
sdmmc_storage_end(&sd_storage); sdmmc_storage_end(&sd_storage);
sd_mounted = false; sd_mounted = false;
is_sd_inited = false; is_sd_inited = false;
} }
} }
void sd_unmount() { _sd_deinit(); } void sd_unmount() { _sd_deinit(); }
void sd_end() { _sd_deinit(); } void sd_end() { _sd_deinit(); }
void *sd_file_read(const char *path, u32 *fsize) bool sd_get_card_mounted(void)
{ {
FIL fp; return sd_mounted;
if (f_open(&fp, path, FA_READ) != FR_OK) }
return NULL;
void *sd_file_read(const char *path, u32 *fsize)
u32 size = f_size(&fp); {
if (fsize) FIL fp;
*fsize = size; if (f_open(&fp, path, FA_READ) != FR_OK)
return NULL;
char *buf = malloc(size + 1);
buf[size] = '\0'; u32 size = f_size(&fp);
if (fsize)
if (f_read(&fp, buf, size, NULL) != FR_OK) *fsize = size;
{
free(buf); char *buf = malloc(size + 1);
f_close(&fp); buf[size] = '\0';
return NULL; if (f_read(&fp, buf, size, NULL) != FR_OK)
} {
free(buf);
f_close(&fp); f_close(&fp);
return buf; return NULL;
} }
int sd_save_to_file(void *buf, u32 size, const char *filename) f_close(&fp);
{
FIL fp; return buf;
u32 res = 0; }
res = f_open(&fp, filename, FA_CREATE_ALWAYS | FA_WRITE);
if (res) int sd_save_to_file(void *buf, u32 size, const char *filename)
{ {
EPRINTFARGS("Error (%d) creating file\n%s.\n", res, filename); FIL fp;
return res; u32 res = 0;
} res = f_open(&fp, filename, FA_CREATE_ALWAYS | FA_WRITE);
if (res)
f_write(&fp, buf, size, NULL); {
f_close(&fp); EPRINTFARGS("Error (%d) creating file\n%s.\n", res, filename);
return res;
return 0; }
}
f_write(&fp, buf, size, NULL);
f_close(&fp);
return 0;
}