summaryrefslogtreecommitdiffstatshomepage
path: root/stmhal/sdcard.c
diff options
context:
space:
mode:
Diffstat (limited to 'stmhal/sdcard.c')
-rw-r--r--stmhal/sdcard.c33
1 files changed, 18 insertions, 15 deletions
diff --git a/stmhal/sdcard.c b/stmhal/sdcard.c
index a2aa4c84d8..a93b2b056d 100644
--- a/stmhal/sdcard.c
+++ b/stmhal/sdcard.c
@@ -30,6 +30,7 @@
#include "py/runtime.h"
#include "lib/fatfs/ff.h"
#include "extmod/fsusermount.h"
+#include "mphalport.h"
#include "sdcard.h"
#include "pin.h"
@@ -80,30 +81,25 @@ static SD_HandleTypeDef sd_handle;
static DMA_HandleTypeDef sd_rx_dma, sd_tx_dma;
void sdcard_init(void) {
- GPIO_InitTypeDef GPIO_Init_Structure;
-
// invalidate the sd_handle
sd_handle.Instance = NULL;
// configure SD GPIO
// we do this here an not in HAL_SD_MspInit because it apparently
// makes it more robust to have the pins always pulled high
- GPIO_Init_Structure.Mode = GPIO_MODE_AF_PP;
- GPIO_Init_Structure.Pull = GPIO_PULLUP;
- GPIO_Init_Structure.Speed = GPIO_SPEED_HIGH;
- GPIO_Init_Structure.Alternate = GPIO_AF12_SDIO;
- GPIO_Init_Structure.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;
- HAL_GPIO_Init(GPIOC, &GPIO_Init_Structure);
- GPIO_Init_Structure.Pin = GPIO_PIN_2;
- HAL_GPIO_Init(GPIOD, &GPIO_Init_Structure);
+ // Note: the mp_hal_pin_config function will configure the GPIO in
+ // fast mode which can do up to 50MHz. This should be plenty for SDIO
+ // which clocks up to 25MHz maximum.
+ mp_hal_pin_config(&pin_C8, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, GPIO_AF12_SDIO);
+ mp_hal_pin_config(&pin_C9, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, GPIO_AF12_SDIO);
+ mp_hal_pin_config(&pin_C10, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, GPIO_AF12_SDIO);
+ mp_hal_pin_config(&pin_C11, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, GPIO_AF12_SDIO);
+ mp_hal_pin_config(&pin_C12, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, GPIO_AF12_SDIO);
+ mp_hal_pin_config(&pin_D2, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_UP, GPIO_AF12_SDIO);
// configure the SD card detect pin
// we do this here so we can detect if the SD card is inserted before powering it on
- GPIO_Init_Structure.Mode = GPIO_MODE_INPUT;
- GPIO_Init_Structure.Pull = MICROPY_HW_SDCARD_DETECT_PULL;
- GPIO_Init_Structure.Speed = GPIO_SPEED_HIGH;
- GPIO_Init_Structure.Pin = MICROPY_HW_SDCARD_DETECT_PIN.pin_mask;
- HAL_GPIO_Init(MICROPY_HW_SDCARD_DETECT_PIN.gpio, &GPIO_Init_Structure);
+ mp_hal_pin_config(&MICROPY_HW_SDCARD_DETECT_PIN, MP_HAL_PIN_MODE_INPUT, MICROPY_HW_SDCARD_DETECT_PULL, 0);
}
void HAL_SD_MspInit(SD_HandleTypeDef *hsd) {
@@ -221,6 +217,10 @@ mp_uint_t sdcard_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blo
dma_init(&sd_rx_dma, &dma_SDIO_0_RX, &sd_handle);
sd_handle.hdmarx = &sd_rx_dma;
+ // make sure cache is flushed and invalidated so when DMA updates the RAM
+ // from reading the peripheral the CPU then reads the new data
+ MP_HAL_CLEANINVALIDATE_DCACHE(dest, num_blocks * SDCARD_BLOCK_SIZE);
+
err = HAL_SD_ReadBlocks_BlockNumber_DMA(&sd_handle, (uint32_t*)dest, block_num, SDCARD_BLOCK_SIZE, num_blocks);
if (err == SD_OK) {
// wait for DMA transfer to finish, with a large timeout
@@ -277,6 +277,9 @@ mp_uint_t sdcard_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t n
dma_init(&sd_tx_dma, &dma_SDIO_0_TX, &sd_handle);
sd_handle.hdmatx = &sd_tx_dma;
+ // make sure cache is flushed to RAM so the DMA can read the correct data
+ MP_HAL_CLEAN_DCACHE(src, num_blocks * SDCARD_BLOCK_SIZE);
+
err = HAL_SD_WriteBlocks_BlockNumber_DMA(&sd_handle, (uint32_t*)src, block_num, SDCARD_BLOCK_SIZE, num_blocks);
if (err == SD_OK) {
// wait for DMA transfer to finish, with a large timeout