|
| 1 | +// TODO make it work with DMA |
| 2 | + |
| 3 | +#include <stm32f4xx_hal.h> |
| 4 | + |
| 5 | +#include "misc.h" |
| 6 | +#include "mpconfig.h" |
| 7 | +#include "qstr.h" |
| 8 | +#include "obj.h" |
| 9 | +#include "runtime.h" |
| 10 | +#include "sdcard.h" |
| 11 | + |
| 12 | +static SD_HandleTypeDef sd_handle; |
| 13 | + |
| 14 | +#define SDCARD_DETECT_GPIO_PORT (GPIOA) |
| 15 | +#define SDCARD_DETECT_GPIO_PIN (GPIO_PIN_8) |
| 16 | +#define SDCARD_DETECT_GPIO_PULL (GPIO_PULLUP) |
| 17 | +#define SDCARD_DETECT_GPIO_PRESENT (GPIO_PIN_RESET) |
| 18 | +#define __SDCARD_DETECT_GPIO_CLK_ENABLE() __GPIOA_CLK_ENABLE() |
| 19 | + |
| 20 | +void sdcard_init(void) { |
| 21 | + GPIO_InitTypeDef GPIO_Init_Structure; |
| 22 | + |
| 23 | + // invalidate the sd_handle |
| 24 | + sd_handle.Instance = NULL; |
| 25 | + |
| 26 | + // configure SD GPIO |
| 27 | + // we do this here an not in HAL_SD_MspInit because it apparently |
| 28 | + // makes it more robust to have the pins always pulled high |
| 29 | + GPIO_Init_Structure.Mode = GPIO_MODE_AF_PP; |
| 30 | + GPIO_Init_Structure.Pull = GPIO_PULLUP; |
| 31 | + GPIO_Init_Structure.Speed = GPIO_SPEED_HIGH; |
| 32 | + GPIO_Init_Structure.Alternate = GPIO_AF12_SDIO; |
| 33 | + GPIO_Init_Structure.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12; |
| 34 | + HAL_GPIO_Init(GPIOC, &GPIO_Init_Structure); |
| 35 | + GPIO_Init_Structure.Pin = GPIO_PIN_2; |
| 36 | + HAL_GPIO_Init(GPIOD, &GPIO_Init_Structure); |
| 37 | + |
| 38 | + // configure the SD card detect pin |
| 39 | + // we do this here so we can detect if the SD card is inserted before powering it on |
| 40 | + GPIO_Init_Structure.Mode = GPIO_MODE_INPUT; |
| 41 | + GPIO_Init_Structure.Pull = SDCARD_DETECT_GPIO_PULL; |
| 42 | + GPIO_Init_Structure.Speed = GPIO_SPEED_HIGH; |
| 43 | + GPIO_Init_Structure.Pin = SDCARD_DETECT_GPIO_PIN; |
| 44 | + HAL_GPIO_Init(SDCARD_DETECT_GPIO_PORT, &GPIO_Init_Structure); |
| 45 | +} |
| 46 | + |
| 47 | +void HAL_SD_MspInit(SD_HandleTypeDef *hsd) { |
| 48 | + // enable SDIO clock |
| 49 | + __SDIO_CLK_ENABLE(); |
| 50 | + |
| 51 | + // GPIO have already been initialised by sdcard_init |
| 52 | + |
| 53 | + // interrupts are not used at the moment |
| 54 | + // they are needed only for DMA transfer (I think...) |
| 55 | +} |
| 56 | + |
| 57 | +void HAL_SD_MspDeInit(SD_HandleTypeDef *hsd) { |
| 58 | + __SDIO_CLK_DISABLE(); |
| 59 | +} |
| 60 | + |
| 61 | +bool sdcard_is_present(void) { |
| 62 | + return HAL_GPIO_ReadPin(SDCARD_DETECT_GPIO_PORT, SDCARD_DETECT_GPIO_PIN) == SDCARD_DETECT_GPIO_PRESENT; |
| 63 | +} |
| 64 | + |
| 65 | +bool sdcard_power_on(void) { |
| 66 | + if (!sdcard_is_present()) { |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + // SD device interface configuration |
| 71 | + sd_handle.Instance = SDIO; |
| 72 | + sd_handle.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING; |
| 73 | + sd_handle.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE; |
| 74 | + sd_handle.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE; |
| 75 | + sd_handle.Init.BusWide = SDIO_BUS_WIDE_1B; |
| 76 | + sd_handle.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE; |
| 77 | + sd_handle.Init.ClockDiv = SDIO_TRANSFER_CLK_DIV; |
| 78 | + |
| 79 | + // init the SD interface |
| 80 | + HAL_SD_CardInfoTypedef cardinfo; |
| 81 | + if (HAL_SD_Init(&sd_handle, &cardinfo) != SD_OK) { |
| 82 | + goto error; |
| 83 | + } |
| 84 | + |
| 85 | + // configure the SD bus width for wide operation |
| 86 | + if (HAL_SD_WideBusOperation_Config(&sd_handle, SDIO_BUS_WIDE_4B) != SD_OK) { |
| 87 | + HAL_SD_DeInit(&sd_handle); |
| 88 | + goto error; |
| 89 | + } |
| 90 | + |
| 91 | + return true; |
| 92 | + |
| 93 | +error: |
| 94 | + sd_handle.Instance = NULL; |
| 95 | + return false; |
| 96 | +} |
| 97 | + |
| 98 | +void sdcard_power_off(void) { |
| 99 | + HAL_SD_DeInit(&sd_handle); |
| 100 | + sd_handle.Instance = NULL; |
| 101 | +} |
| 102 | + |
| 103 | +uint64_t sdcard_get_capacity_in_bytes(void) { |
| 104 | + if (sd_handle.Instance == NULL) { |
| 105 | + return 0; |
| 106 | + } |
| 107 | + HAL_SD_CardInfoTypedef cardinfo; |
| 108 | + HAL_SD_Get_CardInfo(&sd_handle, &cardinfo); |
| 109 | + return cardinfo.CardCapacity; |
| 110 | +} |
| 111 | + |
| 112 | +bool sdcard_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) { |
| 113 | + // check that dest pointer is aligned on a 4-byte boundary |
| 114 | + if (((uint32_t)dest & 3) != 0) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + // check that SD card is initialised |
| 119 | + if (sd_handle.Instance == NULL) { |
| 120 | + return false; |
| 121 | + } |
| 122 | + |
| 123 | + if (HAL_SD_ReadBlocks(&sd_handle, (uint32_t*)dest, block_num * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE, num_blocks) != SD_OK) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + return true; |
| 128 | +} |
| 129 | + |
| 130 | +bool sdcard_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks) { |
| 131 | + // check that src pointer is aligned on a 4-byte boundary |
| 132 | + if (((uint32_t)src & 3) != 0) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + // check that SD card is initialised |
| 137 | + if (sd_handle.Instance == NULL) { |
| 138 | + return false; |
| 139 | + } |
| 140 | + |
| 141 | + if (HAL_SD_WriteBlocks(&sd_handle, (uint32_t*)src, block_num * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE, num_blocks) != SD_OK) { |
| 142 | + return false; |
| 143 | + } |
| 144 | + |
| 145 | + return true; |
| 146 | +} |
| 147 | + |
| 148 | +#if 0 |
| 149 | +DMA not implemented |
| 150 | +bool sdcard_read_blocks_dma(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) { |
| 151 | + // check that dest pointer is aligned on a 4-byte boundary |
| 152 | + if (((uint32_t)dest & 3) != 0) { |
| 153 | + return false; |
| 154 | + } |
| 155 | + |
| 156 | + // check that SD card is initialised |
| 157 | + if (sd_handle.Instance == NULL) { |
| 158 | + return false; |
| 159 | + } |
| 160 | + |
| 161 | + // do the read |
| 162 | + if (HAL_SD_ReadBlocks_DMA(&sd_handle, (uint32_t*)dest, block_num * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE) != SD_OK) { |
| 163 | + return false; |
| 164 | + } |
| 165 | + |
| 166 | + // wait for DMA transfer to finish, with a large timeout |
| 167 | + if (HAL_SD_CheckReadOperation(&sd_handle, 100000000) != SD_OK) { |
| 168 | + return false; |
| 169 | + } |
| 170 | + |
| 171 | + return true; |
| 172 | +} |
| 173 | + |
| 174 | +bool sdcard_write_blocks_dma(const uint8_t *src, uint32_t block_num, uint32_t num_blocks) { |
| 175 | + // check that src pointer is aligned on a 4-byte boundary |
| 176 | + if (((uint32_t)src & 3) != 0) { |
| 177 | + return false; |
| 178 | + } |
| 179 | + |
| 180 | + // check that SD card is initialised |
| 181 | + if (sd_handle.Instance == NULL) { |
| 182 | + return false; |
| 183 | + } |
| 184 | + |
| 185 | + SD_Error status; |
| 186 | + |
| 187 | + status = HAL_SD_WriteBlock_DMA(&sd_handle, (uint32_t*)src, block_num * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE, num_blocks); |
| 188 | + if (status != SD_OK) { |
| 189 | + return false; |
| 190 | + } |
| 191 | + |
| 192 | + // wait for DMA transfer to finish, with a large timeout |
| 193 | + status = HAL_SD_CheckWriteOperation(&sd_handle, 100000000); |
| 194 | + if (status != SD_OK) { |
| 195 | + return false; |
| 196 | + } |
| 197 | + |
| 198 | + return true; |
| 199 | +} |
| 200 | +#endif |
| 201 | + |
| 202 | +/******************************************************************************/ |
| 203 | +// Micro Python bindings |
| 204 | + |
| 205 | +static mp_obj_t sd_present(mp_obj_t self) { |
| 206 | + return MP_BOOL(sdcard_is_present()); |
| 207 | +} |
| 208 | + |
| 209 | +static MP_DEFINE_CONST_FUN_OBJ_1(sd_present_obj, sd_present); |
| 210 | + |
| 211 | +static mp_obj_t sd_power(mp_obj_t self, mp_obj_t state) { |
| 212 | + bool result; |
| 213 | + if (rt_is_true(state)) { |
| 214 | + result = sdcard_power_on(); |
| 215 | + } else { |
| 216 | + sdcard_power_off(); |
| 217 | + result = true; |
| 218 | + } |
| 219 | + return MP_BOOL(result); |
| 220 | +} |
| 221 | + |
| 222 | +static MP_DEFINE_CONST_FUN_OBJ_2(sd_power_obj, sd_power); |
| 223 | + |
| 224 | +static mp_obj_t sd_read(mp_obj_t self, mp_obj_t block_num) { |
| 225 | + uint8_t *dest = m_new(uint8_t, SDCARD_BLOCK_SIZE); |
| 226 | + if (!sdcard_read_blocks(dest, mp_obj_get_int(block_num), 1)) { |
| 227 | + m_free(dest, SDCARD_BLOCK_SIZE); |
| 228 | + return mp_const_none; |
| 229 | + } |
| 230 | + return mp_obj_new_bytearray_by_ref(SDCARD_BLOCK_SIZE, dest); |
| 231 | +} |
| 232 | + |
| 233 | +static MP_DEFINE_CONST_FUN_OBJ_2(sd_read_obj, sd_read); |
| 234 | + |
| 235 | +static const mp_method_t sdcard_methods[] = { |
| 236 | + { "present", &sd_present_obj }, |
| 237 | + { "power", &sd_power_obj }, |
| 238 | + { "read", &sd_read_obj }, |
| 239 | + { NULL, NULL }, |
| 240 | +}; |
| 241 | + |
| 242 | +static const mp_obj_type_t sdcard_type = { |
| 243 | + { &mp_type_type }, |
| 244 | + .name = MP_QSTR_SDcard, |
| 245 | + .methods = sdcard_methods, |
| 246 | +}; |
| 247 | + |
| 248 | +const mp_obj_base_t pyb_sdcard_obj = {&sdcard_type}; |
0 commit comments