1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
67
68
71
72
75
76
77
80
83
84
87
90
91
94
97
98
101
102
103
106
107
110
113
114
117
118
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
144
145
146
147
148
149
153
154
155
167
168
169
170
174
175
176
177
178
179
180
181
185
186
187
188
189
190
191
192
193
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
219
220
221
222
223
224
228
229
230
231
232
233
234
235
236
237
238
239
240
251
252
253
254
255
256
257
258
259
260
271
272
279
291
292
299
311
312
318
329
330
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
475
476
477
478
479
481
482
483
490
494
495
496
501
506
507
512
516
517
522
526
527
532
536
537
541
545
546
550
554
555
559
563
564
567
568
571
572
575
576
/* ... */
/* ... */
#include "stm32469i_discovery_sd.h"
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
SD_HandleTypeDef uSdHandle;
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
uint8_t BSP_SD_Init(void)
{
uint8_t sd_state = MSD_OK;
uSdHandle.Instance = SDIO;
uSdHandle.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
uSdHandle.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
uSdHandle.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
uSdHandle.Init.BusWide = SDIO_BUS_WIDE_1B;
uSdHandle.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_ENABLE;
uSdHandle.Init.ClockDiv = SDIO_TRANSFER_CLK_DIV;
BSP_SD_Detect_MspInit(&uSdHandle, NULL);
if(BSP_SD_IsDetected() != SD_PRESENT)
{
return MSD_ERROR_SD_NOT_PRESENT;
...}
BSP_SD_MspInit(&uSdHandle, NULL);
if(HAL_SD_Init(&uSdHandle) != HAL_OK)
{
sd_state = MSD_ERROR;
}if (HAL_SD_Init(&uSdHandle) != HAL_OK) { ... }
if(sd_state == MSD_OK)
{
if(HAL_SD_ConfigWideBusOperation(&uSdHandle, SDIO_BUS_WIDE_4B) != HAL_OK)
{
sd_state = MSD_ERROR;
}if (HAL_SD_ConfigWideBusOperation(&uSdHandle, SDIO_BUS_WIDE_4B) != HAL_OK) { ... }
else
{
sd_state = MSD_OK;
}else { ... }
}if (sd_state == MSD_OK) { ... }
return sd_state;
}{ ... }
/* ... */
uint8_t BSP_SD_DeInit(void)
{
uint8_t sd_state = MSD_OK;
uSdHandle.Instance = SDIO;
if(HAL_SD_DeInit(&uSdHandle) != HAL_OK)
{
sd_state = MSD_ERROR;
}if (HAL_SD_DeInit(&uSdHandle) != HAL_OK) { ... }
uSdHandle.Instance = SDIO;
BSP_SD_MspDeInit(&uSdHandle, NULL);
return sd_state;
}{ ... }
/* ... */
uint8_t BSP_SD_ITConfig(void)
{
GPIO_InitTypeDef gpio_init_structure;
gpio_init_structure.Pin = SD_DETECT_PIN;
gpio_init_structure.Pull = GPIO_PULLUP;
gpio_init_structure.Speed = GPIO_SPEED_FAST;
gpio_init_structure.Mode = GPIO_MODE_IT_RISING_FALLING;
HAL_GPIO_Init(SD_DETECT_GPIO_PORT, &gpio_init_structure);
HAL_NVIC_SetPriority((IRQn_Type)(SD_DETECT_EXTI_IRQn), 0x0F, 0x00);
HAL_NVIC_EnableIRQ((IRQn_Type)(SD_DETECT_EXTI_IRQn));
return MSD_OK;
}{ ... }
/* ... */
uint8_t BSP_SD_IsDetected(void)
{
__IO uint8_t status = SD_PRESENT;
if (HAL_GPIO_ReadPin(SD_DETECT_GPIO_PORT, SD_DETECT_PIN) == GPIO_PIN_SET)
{
status = SD_NOT_PRESENT;
}if (HAL_GPIO_ReadPin(SD_DETECT_GPIO_PORT, SD_DETECT_PIN) == GPIO_PIN_SET) { ... }
return status;
}{ ... }
/* ... */
uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout)
{
if(HAL_SD_ReadBlocks(&uSdHandle, (uint8_t *)pData, ReadAddr, NumOfBlocks, Timeout) != HAL_OK)
{
return MSD_ERROR;
}if (HAL_SD_ReadBlocks(&uSdHandle, (uint8_t *)pData, ReadAddr, NumOfBlocks, Timeout) != HAL_OK) { ... }
else
{
return MSD_OK;
}else { ... }
}{ ... }
/* ... */
uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout)
{
if(HAL_SD_WriteBlocks(&uSdHandle, (uint8_t *)pData, WriteAddr, NumOfBlocks, Timeout) != HAL_OK)
{
return MSD_ERROR;
}if (HAL_SD_WriteBlocks(&uSdHandle, (uint8_t *)pData, WriteAddr, NumOfBlocks, Timeout) != HAL_OK) { ... }
else
{
return MSD_OK;
}else { ... }
}{ ... }
/* ... */
uint8_t BSP_SD_ReadBlocks_DMA(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks)
{
if(HAL_SD_ReadBlocks_DMA(&uSdHandle, (uint8_t *)pData, ReadAddr, NumOfBlocks) != HAL_OK)
{
return MSD_ERROR;
}if (HAL_SD_ReadBlocks_DMA(&uSdHandle, (uint8_t *)pData, ReadAddr, NumOfBlocks) != HAL_OK) { ... }
else
{
return MSD_OK;
}else { ... }
}{ ... }
/* ... */
uint8_t BSP_SD_WriteBlocks_DMA(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks)
{
if(HAL_SD_WriteBlocks_DMA(&uSdHandle, (uint8_t *)pData, WriteAddr, NumOfBlocks) != HAL_OK)
{
return MSD_ERROR;
}if (HAL_SD_WriteBlocks_DMA(&uSdHandle, (uint8_t *)pData, WriteAddr, NumOfBlocks) != HAL_OK) { ... }
else
{
return MSD_OK;
}else { ... }
}{ ... }
/* ... */
uint8_t BSP_SD_Erase(uint32_t StartAddr, uint32_t EndAddr)
{
if(HAL_SD_Erase(&uSdHandle, StartAddr, EndAddr) != HAL_OK)
{
return MSD_ERROR;
}if (HAL_SD_Erase(&uSdHandle, StartAddr, EndAddr) != HAL_OK) { ... }
else
{
return MSD_OK;
}else { ... }
}{ ... }
/* ... */
__weak void BSP_SD_MspInit(SD_HandleTypeDef *hsd, void *Params)
{
static DMA_HandleTypeDef dma_rx_handle;
static DMA_HandleTypeDef dma_tx_handle;
GPIO_InitTypeDef gpio_init_structure;
__HAL_RCC_SDIO_CLK_ENABLE();
__DMAx_TxRx_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
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);
HAL_NVIC_SetPriority(SDIO_IRQn, 0x0E, 0);
HAL_NVIC_EnableIRQ(SDIO_IRQn);
dma_rx_handle.Init.Channel = SD_DMAx_Rx_CHANNEL;
dma_rx_handle.Init.Direction = DMA_PERIPH_TO_MEMORY;
dma_rx_handle.Init.PeriphInc = DMA_PINC_DISABLE;
dma_rx_handle.Init.MemInc = DMA_MINC_ENABLE;
dma_rx_handle.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
dma_rx_handle.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
dma_rx_handle.Init.Mode = DMA_PFCTRL;
dma_rx_handle.Init.Priority = DMA_PRIORITY_VERY_HIGH;
dma_rx_handle.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
dma_rx_handle.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
dma_rx_handle.Init.MemBurst = DMA_MBURST_INC4;
dma_rx_handle.Init.PeriphBurst = DMA_PBURST_INC4;
dma_rx_handle.Instance = SD_DMAx_Rx_STREAM;
__HAL_LINKDMA(hsd, hdmarx, dma_rx_handle);
HAL_DMA_DeInit(&dma_rx_handle);
HAL_DMA_Init(&dma_rx_handle);
dma_tx_handle.Init.Channel = SD_DMAx_Tx_CHANNEL;
dma_tx_handle.Init.Direction = DMA_MEMORY_TO_PERIPH;
dma_tx_handle.Init.PeriphInc = DMA_PINC_DISABLE;
dma_tx_handle.Init.MemInc = DMA_MINC_ENABLE;
dma_tx_handle.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
dma_tx_handle.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
dma_tx_handle.Init.Mode = DMA_PFCTRL;
dma_tx_handle.Init.Priority = DMA_PRIORITY_VERY_HIGH;
dma_tx_handle.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
dma_tx_handle.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
dma_tx_handle.Init.MemBurst = DMA_MBURST_INC4;
dma_tx_handle.Init.PeriphBurst = DMA_PBURST_INC4;
dma_tx_handle.Instance = SD_DMAx_Tx_STREAM;
__HAL_LINKDMA(hsd, hdmatx, dma_tx_handle);
HAL_DMA_DeInit(&dma_tx_handle);
HAL_DMA_Init(&dma_tx_handle);
HAL_NVIC_SetPriority(SD_DMAx_Rx_IRQn, 0x0F, 0);
HAL_NVIC_EnableIRQ(SD_DMAx_Rx_IRQn);
HAL_NVIC_SetPriority(SD_DMAx_Tx_IRQn, 0x0F, 0);
HAL_NVIC_EnableIRQ(SD_DMAx_Tx_IRQn);
}{ ... }
/* ... */
__weak void BSP_SD_Detect_MspInit(SD_HandleTypeDef *hsd, void *Params)
{
GPIO_InitTypeDef gpio_init_structure;
SD_DETECT_GPIO_CLK_ENABLE();
gpio_init_structure.Pin = SD_DETECT_PIN;
gpio_init_structure.Mode = GPIO_MODE_INPUT;
gpio_init_structure.Pull = GPIO_PULLUP;
gpio_init_structure.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(SD_DETECT_GPIO_PORT, &gpio_init_structure);
}{ ... }
/* ... */
__weak void BSP_SD_MspDeInit(SD_HandleTypeDef *hsd, void *Params)
{
static DMA_HandleTypeDef dma_rx_handle;
static DMA_HandleTypeDef dma_tx_handle;
HAL_NVIC_DisableIRQ(SD_DMAx_Rx_IRQn);
HAL_NVIC_DisableIRQ(SD_DMAx_Tx_IRQn);
dma_rx_handle.Instance = SD_DMAx_Rx_STREAM;
HAL_DMA_DeInit(&dma_rx_handle);
dma_tx_handle.Instance = SD_DMAx_Tx_STREAM;
HAL_DMA_DeInit(&dma_tx_handle);
HAL_NVIC_DisableIRQ(SDIO_IRQn);
/* ... */
__HAL_RCC_SDIO_CLK_DISABLE();
/* ... */
}{ ... }
/* ... */
uint8_t BSP_SD_GetCardState(void)
{
return((HAL_SD_GetCardState(&uSdHandle) == HAL_SD_CARD_TRANSFER ) ? SD_TRANSFER_OK : SD_TRANSFER_BUSY);
}{ ... }
/* ... */
void BSP_SD_GetCardInfo(HAL_SD_CardInfoTypeDef *CardInfo)
{
HAL_SD_GetCardInfo(&uSdHandle, CardInfo);
}{ ... }
/* ... */
void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
{
BSP_SD_AbortCallback();
}{ ... }
/* ... */
void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
{
BSP_SD_WriteCpltCallback();
}{ ... }
/* ... */
void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
{
BSP_SD_ReadCpltCallback();
}{ ... }
/* ... */
__weak void BSP_SD_AbortCallback(void)
{
}{ ... }
/* ... */
__weak void BSP_SD_WriteCpltCallback(void)
{
}{ ... }
/* ... */
__weak void BSP_SD_ReadCpltCallback(void)
{
}{ ... }
/* ... */
/* ... */
/* ... */
/* ... */