Handle SD card inserts / ejects

This commit is contained in:
d0k3 2017-03-01 17:03:27 +01:00
parent 5c566ced14
commit 8535fcf163
3 changed files with 27 additions and 3 deletions

View File

@ -6,6 +6,7 @@ u32 InputWait() {
static u64 delay = 0; static u64 delay = 0;
u32 pad_state_old = HID_STATE; u32 pad_state_old = HID_STATE;
u32 cart_state_old = CART_STATE; u32 cart_state_old = CART_STATE;
u32 sd_state_old = SD_STATE;
delay = (delay) ? 72 : 128; delay = (delay) ? 72 : 128;
timer_start(); timer_start();
while (true) { while (true) {
@ -14,6 +15,9 @@ u32 InputWait() {
u32 cart_state = CART_STATE; u32 cart_state = CART_STATE;
if (cart_state != cart_state_old) if (cart_state != cart_state_old)
return cart_state ? CART_INSERT : CART_EJECT; return cart_state ? CART_INSERT : CART_EJECT;
u32 sd_state = SD_STATE;
if (sd_state != sd_state_old)
return sd_state ? SD_INSERT : SD_EJECT;
u32 special_key = i2cReadRegister(I2C_DEV_MCU, 0x10); u32 special_key = i2cReadRegister(I2C_DEV_MCU, 0x10);
if (special_key == 0x01) if (special_key == 0x01)
return pad_state | BUTTON_POWER; return pad_state | BUTTON_POWER;

View File

@ -2,8 +2,11 @@
#include "common.h" #include "common.h"
// see: http://3dbrew.org/wiki/CONFIG9_Registers
// see: http://3dbrew.org/wiki/EMMC_Registers
#define HID_STATE (~(*(volatile u32*)0x10146000) & BUTTON_ANY) #define HID_STATE (~(*(volatile u32*)0x10146000) & BUTTON_ANY)
#define CART_STATE (~(*(volatile u8*)0x10000010) & 0x1) #define CART_STATE (~(*(volatile u8*)0x10000010) & 0x1)
#define SD_STATE ((*(volatile u16*)0x1000601C) & (0x1<<5))
#define BUTTON_A (1 << 0) #define BUTTON_A (1 << 0)
@ -21,11 +24,13 @@
#define BUTTON_ANY 0x00000FFF #define BUTTON_ANY 0x00000FFF
#define BUTTON_ARROW (BUTTON_RIGHT|BUTTON_LEFT|BUTTON_UP|BUTTON_DOWN) #define BUTTON_ARROW (BUTTON_RIGHT|BUTTON_LEFT|BUTTON_UP|BUTTON_DOWN)
// special buttons / cart handling // special buttons / cart / sd
#define BUTTON_POWER (1 << 12) #define BUTTON_POWER (1 << 12)
#define BUTTON_HOME (1 << 13) #define BUTTON_HOME (1 << 13)
#define CART_INSERT (1 << 14) #define CART_INSERT (1 << 14)
#define CART_EJECT (1 << 15) #define CART_EJECT (1 << 15)
#define SD_INSERT (1 << 16)
#define SD_EJECT (1 << 17)
u32 InputWait(); u32 InputWait();
bool CheckButton(u32 button); bool CheckButton(u32 button);

View File

@ -1055,7 +1055,7 @@ u32 HomeMoreMenu(char* current_path, DirStruct* current_dir, DirStruct* clipboar
DeinitSDCardFS(); DeinitSDCardFS();
if ((SdFormatMenu() == 0) || sd_state) {; if ((SdFormatMenu() == 0) || sd_state) {;
while (!InitSDCardFS() && while (!InitSDCardFS() &&
ShowPrompt(true, "Reinitialising SD card failed! Retry?")); ShowPrompt(true, "Initialising SD card failed! Retry?"));
} }
ClearScreenF(true, true, COLOR_STD_BG); ClearScreenF(true, true, COLOR_STD_BG);
InitEmuNandBase(true); InitEmuNandBase(true);
@ -1260,7 +1260,7 @@ u32 GodMode() {
DeinitExtFS(); DeinitExtFS();
if (!CheckSDMountState()) { if (!CheckSDMountState()) {
while (!InitSDCardFS() && while (!InitSDCardFS() &&
ShowPrompt(true, "Reinitialising SD card failed! Retry?")); ShowPrompt(true, "Initialising SD card failed! Retry?"));
} else { } else {
DeinitSDCardFS(); DeinitSDCardFS();
if (clipboard->n_entries && (DriveType(clipboard->entry[0].path) & if (clipboard->n_entries && (DriveType(clipboard->entry[0].path) &
@ -1466,6 +1466,21 @@ u32 GodMode() {
ShowPrompt(false, "Cart init failed!"); ShowPrompt(false, "Cart init failed!");
if (!(*current_path) || (curr_drvtype & DRV_CART)) if (!(*current_path) || (curr_drvtype & DRV_CART))
GetDirContents(current_dir, current_path); // refresh dir contents GetDirContents(current_dir, current_path); // refresh dir contents
} else if (pad_state & SD_INSERT) {
while (!InitSDCardFS() && ShowPrompt(true, "Initialising SD card failed! Retry?"));
ClearScreenF(true, true, COLOR_STD_BG);
InitEmuNandBase(true);
InitExtFS();
GetDirContents(current_dir, current_path);
} else if ((pad_state & SD_EJECT) && CheckSDMountState()) {
ShowPrompt(false, "!Unexpected SD card removal!\n \nTo prevent data loss, unmount\nbefore ejecting the SD card.");
DeinitExtFS();
DeinitSDCardFS();
InitExtFS();
if (clipboard->n_entries && (DriveType(clipboard->entry[0].path) &
(DRV_SDCARD|DRV_ALIAS|DRV_EMUNAND|DRV_IMAGE)))
clipboard->n_entries = 0; // remove SD clipboard entries
GetDirContents(current_dir, current_path);
} }
} }