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
65
66
67
70
71
74
75
78
79
82
83
84
87
88
91
92
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
118
119
120
121
122
126
127
128
140
141
142
143
144
148
155
156
160
161
162
163
164
165
169
170
171
172
173
176
177
178
179
180
181
182
183
184
185
186
187
188
191
197
198
199
200
201
202
203
204
205
206
217
218
219
220
221
222
223
224
225
226
237
238
245
257
258
265
277
278
284
295
296
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
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
402
406
407
408
413
418
419
424
428
429
434
438
439
444
448
449
453
457
458
462
466
467
471
475
476
479
480
483
484
487
488
/* ... */
/* ... */
#include "stm324x9i_eval_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_DISABLE;
uSdHandle.Init.ClockDiv = SDIO_TRANSFER_CLK_DIV;
BSP_IO_Init();
if(BSP_SD_IsDetected() != SD_PRESENT)
{
return MSD_ERROR;
}if (BSP_SD_IsDetected() != SD_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_ITConfig(void)
{
BSP_IO_ConfigPin(SD_DETECT_PIN, IO_MODE_IT_FALLING_EDGE);
return 0;
}{ ... }
/* ... */
uint8_t BSP_SD_IsDetected(void)
{
__IO uint8_t status = SD_PRESENT;
if(BSP_IO_ReadPin(SD_DETECT_PIN))
{
status = SD_NOT_PRESENT;
}if (BSP_IO_ReadPin(SD_DETECT_PIN)) { ... }
return status;
}{ ... }
/* ... */
void BSP_SD_DetectIT(void)
{
BSP_IO_ITClear();
BSP_SD_ITConfig();
BSP_SD_DetectCallback();
}{ ... }
/* ... */
__weak void BSP_SD_DetectCallback(void)
{
/* ... */
}{ ... }
/* ... */
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 dmaRxHandle;
static DMA_HandleTypeDef dmaTxHandle;
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);
dmaRxHandle.Init.Channel = SD_DMAx_Rx_CHANNEL;
dmaRxHandle.Init.Direction = DMA_PERIPH_TO_MEMORY;
dmaRxHandle.Init.PeriphInc = DMA_PINC_DISABLE;
dmaRxHandle.Init.MemInc = DMA_MINC_ENABLE;
dmaRxHandle.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
dmaRxHandle.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
dmaRxHandle.Init.Mode = DMA_PFCTRL;
dmaRxHandle.Init.Priority = DMA_PRIORITY_VERY_HIGH;
dmaRxHandle.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
dmaRxHandle.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
dmaRxHandle.Init.MemBurst = DMA_MBURST_INC4;
dmaRxHandle.Init.PeriphBurst = DMA_PBURST_INC4;
dmaRxHandle.Instance = SD_DMAx_Rx_STREAM;
__HAL_LINKDMA(hsd, hdmarx, dmaRxHandle);
HAL_DMA_DeInit(&dmaRxHandle);
HAL_DMA_Init(&dmaRxHandle);
dmaTxHandle.Init.Channel = SD_DMAx_Tx_CHANNEL;
dmaTxHandle.Init.Direction = DMA_MEMORY_TO_PERIPH;
dmaTxHandle.Init.PeriphInc = DMA_PINC_DISABLE;
dmaTxHandle.Init.MemInc = DMA_MINC_ENABLE;
dmaTxHandle.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
dmaTxHandle.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
dmaTxHandle.Init.Mode = DMA_PFCTRL;
dmaTxHandle.Init.Priority = DMA_PRIORITY_VERY_HIGH;
dmaTxHandle.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
dmaTxHandle.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
dmaTxHandle.Init.MemBurst = DMA_MBURST_INC4;
dmaTxHandle.Init.PeriphBurst = DMA_PBURST_INC4;
dmaTxHandle.Instance = SD_DMAx_Tx_STREAM;
__HAL_LINKDMA(hsd, hdmatx, dmaTxHandle);
HAL_DMA_DeInit(&dmaTxHandle);
HAL_DMA_Init(&dmaTxHandle);
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);
}{ ... }
/* ... */
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)
{
}{ ... }
/* ... */
/* ... */
/* ... */
/* ... */