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;
}
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()
{
uint32_t response;
@ -606,6 +626,11 @@ DSTATUS sdio_initialize(){
return 0;
}
void sdio_stop_clk()
{
SDIO->POWER = 0UL;
}
DRESULT sdio_disk_read(BYTE *buff, DWORD sector, UINT count){
uint32_t addr;
uint32_t sdio_status;
@ -713,15 +738,14 @@ DRESULT sdio_disk_write(const BYTE *buff, DWORD sector, UINT count)
union sdio_status_conv status;
uint32_t buff_offset = 0;
int ret;
UINT count_backup = count;
if (sdio_check_write_protection())
return RES_WRPRT;
addr = (card_info.type == SD_V2_HC ? (sector) : (sector * 512));
while (count) {
ret = sdio_check_status_register_cmd13(card_info.rca, &status.value);
if (ret)
return RES_ERROR;
@ -730,10 +754,23 @@ DRESULT sdio_disk_write(const BYTE *buff, DWORD sector, UINT count)
return RES_ERROR;
}
ret = sdio_send_write_block_cmd24(addr);
if (ret) {
while (status.statusstruct.READY_FOR_DATA != 1) {
ret = sdio_check_status_register_cmd13(card_info.rca, &status.value);
if (ret)
return RES_ERROR;
}
if (count > 1)
ret = sdio_send_write_multiple_blocks_cmd25(addr);
else if (count == 1)
ret = sdio_send_write_block_cmd24(addr);
else
ret = RES_PARERR;
if (ret)
return RES_ERROR;
while (count) {
sdio_write_buffer(512, 9, &buff[buff_offset]);
buff_offset += 512;
@ -741,5 +778,8 @@ DRESULT sdio_disk_write(const BYTE *buff, DWORD sector, UINT count)
count--;
}
if (count_backup > 1)
(void)sdio_send_stop_transmission_cmd12();
return RES_OK;
}

View File

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

View File

@ -69,7 +69,7 @@ enum analog_value_monitor {
#define WATCHDOG_HALT_DEBUG (0)
#endif
#define WATCHDOG_PRESCALER 4
#define WATCHDOG_PRESCALER 8
#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)
{
FRESULT res;
static uint8_t inserted_counter = 0;
if (sdio_check_inserted() && mounted) {
memset(fs_ptr, 0, sizeof(FATFS));
sdio_stop_clk();
inserted_counter = 0;
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);
if (res == FR_OK)
return true;

View File

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