Skip to content

Commit fb431bf

Browse files
committed
stmhal: Add SD card support.
Just low-level read/write support. No filesystem yet.
1 parent c44115f commit fb431bf

5 files changed

Lines changed: 267 additions & 22 deletions

File tree

stmhal/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ SRC_C = \
7878
exti.c \
7979
usrsw.c \
8080
rtc.c \
81+
sdcard.c \
8182

8283
# lcd.c \
8384
# servo.c \
@@ -86,7 +87,6 @@ SRC_C = \
8687
# accel.c \
8788
# timer.c \
8889
# audio.c \
89-
# sdcard.c \
9090
# i2c.c \
9191
# adc.c \
9292
# file.c \
@@ -106,9 +106,11 @@ SRC_HAL = $(addprefix $(HAL_DIR)/src/,\
106106
stm32f4xx_hal_rcc_ex.c \
107107
stm32f4xx_hal_rtc.c \
108108
stm32f4xx_hal_rtc_ex.c \
109+
stm32f4xx_hal_sd.c \
109110
stm32f4xx_hal_tim.c \
110111
stm32f4xx_hal_tim_ex.c \
111112
stm32f4xx_hal_uart.c \
113+
stm32f4xx_ll_sdmmc.c \
112114
stm32f4xx_ll_usb.c \
113115
)
114116

stmhal/main.c

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@
2929
#include "usrsw.h"
3030
#include "usb.h"
3131
#include "rtc.h"
32+
#include "sdcard.h"
3233
#if 0
3334
#include "ff.h"
3435
#include "lexerfatfs.h"
3536
#include "servo.h"
3637
#include "lcd.h"
3738
#include "storage.h"
38-
#include "sdcard.h"
3939
#include "accel.h"
4040
#include "timer.h"
4141
#include "pybwlan.h"
@@ -194,23 +194,6 @@ int main(void) {
194194
#endif
195195

196196
#if 0
197-
#if MICROPY_HW_HAS_SDCARD
198-
{
199-
// configure SDIO pins to be high to start with (apparently makes it more robust)
200-
// FIXME this is not making them high, it just makes them outputs...
201-
GPIO_InitTypeDef GPIO_InitStructure;
202-
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
203-
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
204-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
205-
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
206-
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
207-
GPIO_Init(GPIOC, &GPIO_InitStructure);
208-
209-
// Configure PD.02 CMD line
210-
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
211-
GPIO_Init(GPIOD, &GPIO_InitStructure);
212-
}
213-
#endif
214197
#if defined(NETDUINO_PLUS_2)
215198
{
216199
GPIO_InitTypeDef GPIO_InitStructure;
@@ -245,11 +228,11 @@ int main(void) {
245228
rtc_init();
246229
#endif
247230

248-
#if 0
249231
// more sub-system init
250232
#if MICROPY_HW_HAS_SDCARD
251233
sdcard_init();
252234
#endif
235+
#if 0
253236
storage_init();
254237
#endif
255238

stmhal/pybmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
#include "usrsw.h"
2323
#include "rtc.h"
2424
#include "usart.h"
25+
#include "sdcard.h"
2526
#if 0
2627
#include "servo.h"
2728
#include "storage.h"
2829
#include "usb.h"
29-
#include "sdcard.h"
3030
#include "accel.h"
3131
#include "i2c.h"
3232
#include "adc.h"
@@ -258,11 +258,11 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
258258
{ MP_OBJ_NEW_QSTR(MP_QSTR_switch), (mp_obj_t)&pyb_switch_obj },
259259
#endif
260260

261-
#if 0
262261
#if MICROPY_HW_HAS_SDCARD
263262
{ MP_OBJ_NEW_QSTR(MP_QSTR_SD), (mp_obj_t)&pyb_sdcard_obj },
264263
#endif
265264

265+
#if 0
266266
#if MICROPY_HW_HAS_MMA7660
267267
{ MP_OBJ_NEW_QSTR(MP_QSTR_accel), (mp_obj_t)&pyb_accel_read_obj },
268268
{ MP_OBJ_NEW_QSTR(MP_QSTR_accel_read), (mp_obj_t)&pyb_accel_read_all_obj },

stmhal/sdcard.c

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
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};

stmhal/sdcard.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// this is a fixed size and should not be changed
2+
#define SDCARD_BLOCK_SIZE (512)
3+
4+
void sdcard_init(void);
5+
bool sdcard_is_present(void);
6+
bool sdcard_power_on(void);
7+
void sdcard_power_off(void);
8+
uint64_t sdcard_get_capacity_in_bytes(void);
9+
bool sdcard_read_block(uint8_t *dest, uint32_t block_num);
10+
bool sdcard_write_block(const uint8_t *src, uint32_t block_num);
11+
12+
extern const struct _mp_obj_base_t pyb_sdcard_obj;

0 commit comments

Comments
 (0)