Improve SDIO handling

This commit is contained in:
Mario Hüttel 2020-08-17 22:10:04 +02:00
parent 543127b187
commit 4e9b28ce15
5 changed files with 60 additions and 14 deletions

View File

@ -417,6 +417,26 @@ static int sdio_send_go_idle_cmd0() {
return 0; return 0;
} }
static int sdio_send_stop_transmission_cmd12()
{
int res;
uint32_t response;
sdio_send_cmd(12, 0, SHORT_ANS);
res = sdio_get_response(12, SHORT_ANS, &response);
return res;
}
static int sdio_send_write_multiple_blocks_cmd25(uint32_t address)
{
int res;
uint32_t response;
sdio_send_cmd(25, address, SHORT_ANS);
res = sdio_get_response(25, SHORT_ANS, &response);
return res;
}
static enum cmd8_ret sdio_send_iface_condition_cmd8() static enum cmd8_ret sdio_send_iface_condition_cmd8()
{ {
uint32_t response; uint32_t response;
@ -606,6 +626,11 @@ DSTATUS sdio_initialize(){
return 0; return 0;
} }
void sdio_stop_clk()
{
SDIO->POWER = 0UL;
}
DRESULT sdio_disk_read(BYTE *buff, DWORD sector, UINT count){ DRESULT sdio_disk_read(BYTE *buff, DWORD sector, UINT count){
uint32_t addr; uint32_t addr;
uint32_t sdio_status; uint32_t sdio_status;
@ -713,27 +738,39 @@ DRESULT sdio_disk_write(const BYTE *buff, DWORD sector, UINT count)
union sdio_status_conv status; union sdio_status_conv status;
uint32_t buff_offset = 0; uint32_t buff_offset = 0;
int ret; int ret;
UINT count_backup = count;
if (sdio_check_write_protection()) if (sdio_check_write_protection())
return RES_WRPRT; return RES_WRPRT;
addr = (card_info.type == SD_V2_HC ? (sector) : (sector * 512)); addr = (card_info.type == SD_V2_HC ? (sector) : (sector * 512));
while (count) { ret = sdio_check_status_register_cmd13(card_info.rca, &status.value);
ret = sdio_check_status_register_cmd13(card_info.rca, &status.value); if (ret)
return RES_ERROR;
if (status.statusstruct.CURRENT_STATE == CURRENT_STATE_STBY) {
if (sdio_send_select_card_cmd7(card_info.rca))
return RES_ERROR;
}
while (status.statusstruct.READY_FOR_DATA != 1) {
ret = sdio_check_status_register_cmd13(card_info.rca, &status.value);
if (ret) if (ret)
return RES_ERROR; return RES_ERROR;
}
if (status.statusstruct.CURRENT_STATE == CURRENT_STATE_STBY) { if (count > 1)
if (sdio_send_select_card_cmd7(card_info.rca)) ret = sdio_send_write_multiple_blocks_cmd25(addr);
return RES_ERROR; else if (count == 1)
}
ret = sdio_send_write_block_cmd24(addr); ret = sdio_send_write_block_cmd24(addr);
if (ret) { else
return RES_ERROR; ret = RES_PARERR;
} if (ret)
return RES_ERROR;
while (count) {
sdio_write_buffer(512, 9, &buff[buff_offset]); sdio_write_buffer(512, 9, &buff[buff_offset]);
buff_offset += 512; buff_offset += 512;
@ -741,5 +778,8 @@ DRESULT sdio_disk_write(const BYTE *buff, DWORD sector, UINT count)
count--; count--;
} }
if (count_backup > 1)
(void)sdio_send_stop_transmission_cmd12();
return RES_OK; return RES_OK;
} }

View File

@ -21,7 +21,7 @@ DRESULT sdio_disk_ioctl(BYTE cmd, void* buff);
DWORD get_fattime(); DWORD get_fattime();
int sdio_check_inserted(); int sdio_check_inserted();
void sdio_stop_clk();
//Defines for Card Status in struct _CardStatus //Defines for Card Status in struct _CardStatus
#define CURRENT_STATE_IDLE 0 #define CURRENT_STATE_IDLE 0

View File

@ -69,7 +69,7 @@ enum analog_value_monitor {
#define WATCHDOG_HALT_DEBUG (0) #define WATCHDOG_HALT_DEBUG (0)
#endif #endif
#define WATCHDOG_PRESCALER 4 #define WATCHDOG_PRESCALER 8
#define SAFETY_MIN_STACK_FREE 0x100 #define SAFETY_MIN_STACK_FREE 0x100

View File

@ -115,13 +115,20 @@ static inline void setup_shell_uart(struct stm_uart *uart)
static bool mount_sd_card_if_avail(bool mounted) static bool mount_sd_card_if_avail(bool mounted)
{ {
FRESULT res; FRESULT res;
static uint8_t inserted_counter = 0;
if (sdio_check_inserted() && mounted) { if (sdio_check_inserted() && mounted) {
memset(fs_ptr, 0, sizeof(FATFS)); memset(fs_ptr, 0, sizeof(FATFS));
sdio_stop_clk();
inserted_counter = 0;
return false; return false;
} }
if (!sdio_check_inserted() && !mounted) { if (!sdio_check_inserted() && inserted_counter < 255)
inserted_counter++;
if (!sdio_check_inserted() && !mounted && inserted_counter > 4) {
inserted_counter = 0;
res = f_mount(fs_ptr, "0:/", 1); res = f_mount(fs_ptr, "0:/", 1);
if (res == FR_OK) if (res == FR_OK)
return true; return true;

View File

@ -230,7 +230,6 @@ static shellmatta_retCode_t shell_cmd_rot(const shellmatta_handle_t handle,
{ {
(void)arguments; (void)arguments;
(void)length; (void)length;
uint32_t rot_val; uint32_t rot_val;
rot_val = rotary_encoder_get_abs_val(); rot_val = rotary_encoder_get_abs_val();