-
Notifications
You must be signed in to change notification settings - Fork 0
/
stm32h745i_discovery_mmc.c
659 lines (585 loc) · 16.9 KB
/
stm32h745i_discovery_mmc.c
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
/**
******************************************************************************
* @file stm32h745i_discovery_mmc.c
* @author MCD Application Team
* @brief This file includes the EMMC driver mounted on STM32H745I-DISCOVERY
* board.
******************************************************************************
* @attention
*
* Copyright (c) 2017 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* File Info : -----------------------------------------------------------------
User NOTES
1. How To use this driver:
--------------------------
- This driver is used to drive the EMMC mounted on STM32H745I-DISCOVERY
board.
- This driver does not need a specific component driver for the EMMC device
to be included with.
2. Driver description:
---------------------
+ Initialization steps:
o Initialize the external EMMC memory using the BSP_MMC_Init() function. This
function includes the MSP layer hardware resources initialization and the
SDIO interface configuration to interface with the external EMMC. It
also includes the EMMC initialization sequence.
o The function BSP_MMC_GetCardInfo() is used to get the MMC information
which is stored in the structure "HAL_MMC_CardInfoTypedef".
+ Micro MMC card operations
o The micro MMC card can be accessed with read/write block(s) operations once
it is ready for access. The access can be performed whether using the polling
mode by calling the functions BSP_MMC_ReadBlocks()/BSP_MMC_WriteBlocks(), or by DMA
transfer using the functions BSP_MMC_ReadBlocks_DMA()/BSP_MMC_WriteBlocks_DMA()
o The DMA transfer complete is used with interrupt mode. Once the MMC transfer
is complete, the MMC interrupt is handled using the function BSP_MMC_IRQHandler(),
the DMA Tx/Rx transfer complete are handled using the functions
MMC_DMA_Tx_IRQHandler()/MMC_DMA_Rx_IRQHandler() that should be defined by user.
The corresponding user callbacks are implemented by the user at application level.
o The MMC erase block(s) is performed using the function BSP_MMC_Erase() with specifying
the number of blocks to erase.
o The MMC runtime status is returned when calling the function BSP_MMC_GetStatus().
------------------------------------------------------------------------------*/
/* Includes ------------------------------------------------------------------*/
#include "stm32h745i_discovery_mmc.h"
/** @addtogroup BSP
* @{
*/
/** @addtogroup STM32H745I_DISCO
* @{
*/
/** @defgroup STM32H745I_DISCO_MMC MMC
* @{
*/
/** @defgroup STM32H745I_DISCO_MMC_Exported_Variables Exported Variables
* @{
*/
MMC_HandleTypeDef hsd_sdmmc[MMC_INSTANCES_NBR];
/**
* @}
*/
/** @defgroup STM32H745I_DISCO_MMC_Private_FunctionsPrototypes Private Functions Prototypes
* @{
*/
static void MMC_MspInit(MMC_HandleTypeDef *hmmc);
static void MMC_MspDeInit(MMC_HandleTypeDef *hmmc);
#if (USE_HAL_MMC_REGISTER_CALLBACKS == 1)
static void MMC_AbortCallback(MMC_HandleTypeDef *hmmc);
static void MMC_TxCpltCallback(MMC_HandleTypeDef *hmmc);
static void MMC_RxCpltCallback(MMC_HandleTypeDef *hmmc);
#endif
/**
* @}
*/
/** @defgroup STM32H745I_DISCO_MMC_Exported_Functions Exported Functions
* @{
*/
/**
* @brief Initializes the MMC card device.
* @param Instance SDMMC Instance
* @retval BSP status
*/
int32_t BSP_MMC_Init(uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
if(Instance >= MMC_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
#if (USE_HAL_MMC_REGISTER_CALLBACKS == 0)
/* Msp MMC initialization */
MMC_MspInit(&hsd_sdmmc[Instance]);
#else
/* Register the MMC MSP Callbacks */
if(IsMspCallbacksValid[Instance] == 0UL)
{
if(BSP_MMC_RegisterDefaultMspCallbacks(Instance) != BSP_ERROR_NONE)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
}
if(ret == BSP_ERROR_NONE)
{
#endif
/* HAL MMC initialization */
if(MX_MMC_SD_Init(&hsd_sdmmc[Instance]) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
#if (USE_HAL_MMC_REGISTER_CALLBACKS == 1)
/* Register MMC TC, HT and Abort callbacks */
else if(HAL_MMC_RegisterCallback(&hsd_sdmmc[Instance], HAL_MMC_TX_CPLT_CB_ID, MMC_TxCpltCallback) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else if(HAL_MMC_RegisterCallback(&hsd_sdmmc[Instance], HAL_MMC_RX_CPLT_CB_ID, MMC_RxCpltCallback) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
if(HAL_MMC_RegisterCallback(&hsd_sdmmc[Instance], HAL_MMC_ABORT_CB_ID, MMC_AbortCallback) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
}
}
#endif /* USE_HAL_MMC_REGISTER_CALLBACKS */
}
return ret;
}
/**
* @brief DeInitializes the MMC card device.
* @param Instance SDMMC Instance
* @retval BSP status
*/
int32_t BSP_MMC_DeInit(uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
if(Instance >= MMC_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}/* HAL MMC deinitialization */
else if(HAL_MMC_DeInit(&hsd_sdmmc[Instance]) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
/* Msp MMC de-initialization */
#if (USE_HAL_MMC_REGISTER_CALLBACKS == 0)
MMC_MspDeInit(&hsd_sdmmc[Instance]);
#endif /* (USE_HAL_MMC_REGISTER_CALLBACKS == 0) */
}
return ret;
}
/**
* @brief Initializes the SDMMC1 peripheral.
* @param hmmc SD handle
* @retval HAL status
*/
__weak HAL_StatusTypeDef MX_MMC_SD_Init(MMC_HandleTypeDef *hmmc)
{
HAL_StatusTypeDef ret = HAL_OK;
hmmc->Instance = SDMMC1;
hmmc->Init.ClockDiv = 8;
hmmc->Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
hmmc->Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
hmmc->Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
hmmc->Init.BusWide = SDMMC_BUS_WIDE_8B;
/* HAL SD initialization */
if(HAL_MMC_Init(hmmc) != HAL_OK)
{
ret = HAL_ERROR;
}
return ret;
}
#if (USE_HAL_MMC_REGISTER_CALLBACKS == 1)
/**
* @brief Default BSP MMC Msp Callbacks
* @param Instance SDMMC Instance
* @retval BSP status
*/
int32_t BSP_MMC_RegisterDefaultMspCallbacks(uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
if(Instance >= MMC_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Register MspInit/MspDeInit Callbacks */
if(HAL_MMC_RegisterCallback(&hsd_sdmmc[Instance], HAL_MMC_MSP_INIT_CB_ID, MMC_MspInit) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else if(HAL_MMC_RegisterCallback(&hsd_sdmmc[Instance], HAL_MMC_MSP_DEINIT_CB_ID, MMC_MspDeInit) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
IsMspCallbacksValid[Instance] = 1U;
}
}
/* Return BSP status */
return ret;
}
/**
* @brief BSP MMC Msp Callback registering
* @param Instance SDMMC Instance
* @param CallBacks pointer to MspInit/MspDeInit callbacks functions
* @retval BSP status
*/
int32_t BSP_MMC_RegisterMspCallbacks(uint32_t Instance, BSP_MMC_Cb_t *CallBacks)
{
int32_t ret = BSP_ERROR_NONE;
if(Instance >= MMC_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Register MspInit/MspDeInit Callbacks */
if(HAL_MMC_RegisterCallback(&hsd_sdmmc[Instance], HAL_MMC_MSP_INIT_CB_ID, CallBacks->pMspInitCb) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else if(HAL_MMC_RegisterCallback(&hsd_sdmmc[Instance], HAL_MMC_MSP_DEINIT_CB_ID, CallBacks->pMspDeInitCb) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
IsMspCallbacksValid[Instance] = 1U;
}
}
/* Return BSP status */
return ret;
}
#endif /* (USE_HAL_MMC_REGISTER_CALLBACKS == 1) */
/**
* @brief Reads block(s) from a specified address in an SD card, in polling mode.
* @param Instance MMC Instance
* @param pData Pointer to the buffer that will contain the data to transmit
* @param BlockIdx Block index from where data is to be read
* @param BlocksNbr Number of MMC blocks to read
* @retval BSP status
*/
int32_t BSP_MMC_ReadBlocks(uint32_t Instance, uint32_t *pData, uint32_t BlockIdx, uint32_t BlocksNbr)
{
uint32_t timeout = MMC_READ_TIMEOUT*BlocksNbr;
int32_t ret;
if(Instance >= MMC_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else if(HAL_MMC_ReadBlocks(&hsd_sdmmc[Instance], (uint8_t *)pData, BlockIdx, BlocksNbr, timeout) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
ret = BSP_ERROR_NONE;
}
/* Return BSP status */
return ret;
}
/**
* @brief Writes block(s) to a specified address in an SD card, in polling mode.
* @param Instance MMC Instance
* @param pData Pointer to the buffer that will contain the data to transmit
* @param BlockIdx Block index from where data is to be written
* @param BlocksNbr Number of MMC blocks to write
* @retval BSP status
*/
int32_t BSP_MMC_WriteBlocks(uint32_t Instance, uint32_t *pData, uint32_t BlockIdx, uint32_t BlocksNbr)
{
uint32_t timeout = MMC_READ_TIMEOUT*BlocksNbr;
int32_t ret;
if(Instance >= MMC_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else if(HAL_MMC_WriteBlocks(&hsd_sdmmc[Instance], (uint8_t *)pData, BlockIdx, BlocksNbr, timeout) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
ret = BSP_ERROR_NONE;
}
/* Return BSP status */
return ret;
}
/**
* @brief Reads block(s) from a specified address in an SD card, in DMA mode.
* @param Instance MMC Instance
* @param pData Pointer to the buffer that will contain the data to transmit
* @param BlockIdx Block index from where data is to be read
* @param BlocksNbr Number of MMC blocks to read
* @retval BSP status
*/
int32_t BSP_MMC_ReadBlocks_DMA(uint32_t Instance, uint32_t *pData, uint32_t BlockIdx, uint32_t BlocksNbr)
{
int32_t ret;
if(Instance >= MMC_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else if(HAL_MMC_ReadBlocks_DMA(&hsd_sdmmc[Instance], (uint8_t *)pData, BlockIdx, BlocksNbr) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
ret = BSP_ERROR_NONE;
}
/* Return BSP status */
return ret;
}
/**
* @brief Writes block(s) to a specified address in an SD card, in DMA mode.
* @param Instance MMC Instance
* @param pData Pointer to the buffer that will contain the data to transmit
* @param BlockIdx Block index from where data is to be written
* @param BlocksNbr Number of MMC blocks to write
* @retval BSP status
*/
int32_t BSP_MMC_WriteBlocks_DMA(uint32_t Instance, uint32_t *pData, uint32_t BlockIdx, uint32_t BlocksNbr)
{
int32_t ret;
if(Instance >= MMC_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else if(HAL_MMC_WriteBlocks_DMA(&hsd_sdmmc[Instance], (uint8_t *)pData, BlockIdx, BlocksNbr) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
ret = BSP_ERROR_NONE;
}
/* Return BSP status */
return ret;
}
/**
* @brief Erases the specified memory area of the given MMC card.
* @param Instance MMC Instance
* @param StartAddr : Start byte address
* @param EndAddr : End byte address
* @retval BSP status
*/
int32_t BSP_MMC_Erase(uint32_t Instance, uint32_t StartAddr, uint32_t EndAddr)
{
int32_t ret;
if(Instance >= MMC_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else if( HAL_MMC_Erase(&hsd_sdmmc[Instance], StartAddr, EndAddr) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
ret = BSP_ERROR_NONE;
}
return ret ;
}
/**
* @brief Handles MMC card interrupt request.
* @param Instance MMC Instance
* @retval None
*/
void BSP_MMC_IRQHandler(uint32_t Instance)
{
HAL_MMC_IRQHandler(&hsd_sdmmc[Instance]);
}
/**
* @brief Gets the current MMC card data status.
* @param Instance MMC Instance
* @retval Data transfer state.
* This value can be one of the following values:
* @arg MMC_TRANSFER_OK: No data transfer is acting
* @arg MMC_TRANSFER_BUSY: Data transfer is acting
* @arg MMC_TRANSFER_ERROR: Data transfer error
*/
int32_t BSP_MMC_GetCardState(uint32_t Instance)
{
return((HAL_MMC_GetCardState(&hsd_sdmmc[Instance]) == HAL_MMC_CARD_TRANSFER ) ? MMC_TRANSFER_OK : MMC_TRANSFER_BUSY);
}
/**
* @brief Get MMC information about specific MMC card.
* @param Instance MMC Instance
* @param CardInfo : Pointer to HAL_MMC_CardInfoTypedef structure
* @retval None
*/
int32_t BSP_MMC_GetCardInfo(uint32_t Instance, BSP_MMC_CardInfo *CardInfo)
{
int32_t ret;
if(Instance >= MMC_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else if(HAL_MMC_GetCardInfo(&hsd_sdmmc[Instance], CardInfo) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
ret = BSP_ERROR_NONE;
}
/* Return BSP status */
return ret;
}
/**
* @brief BSP MMC Abort callbacks
* @param Instance MMC Instance
* @retval None
*/
__weak void BSP_MMC_AbortCallback(uint32_t Instance)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(Instance);
}
/**
* @brief BSP Tx Transfer completed callbacks
* @param Instance MMC Instance
* @retval None
*/
__weak void BSP_MMC_WriteCpltCallback(uint32_t Instance)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(Instance);
}
/**
* @brief BSP Rx Transfer completed callbacks
* @param Instance MMC Instance
* @retval None
*/
__weak void BSP_MMC_ReadCpltCallback(uint32_t Instance)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(Instance);
}
#if (USE_HAL_MMC_REGISTER_CALLBACKS == 0)
/**
* @brief MMC Abort callbacks
* @param hmmc : MMC handle
* @retval None
*/
void HAL_MMC_AbortCallback(MMC_HandleTypeDef *hmmc)
{
BSP_MMC_AbortCallback(0);
}
/**
* @brief Tx Transfer completed callbacks
* @param hmmc: MMC handle
* @retval None
*/
void HAL_MMC_TxCpltCallback(MMC_HandleTypeDef *hmmc)
{
BSP_MMC_WriteCpltCallback(0);
}
/**
* @brief Rx Transfer completed callbacks
* @param hmmc: MMC handle
* @retval None
*/
void HAL_MMC_RxCpltCallback(MMC_HandleTypeDef *hmmc)
{
BSP_MMC_ReadCpltCallback(0);
}
#endif
/**
* @}
*/
/** @defgroup STM32H745I_DISCO_MMC_Private_Functions Private Functions
* @{
*/
/**
* @brief Initializes the MMC MSP.
* @param hmmc MMC handle
* @retval None
*/
static void MMC_MspInit(MMC_HandleTypeDef *hmmc)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(hmmc);
/* __weak function can be modified by the application */
GPIO_InitTypeDef gpio_init_structure;
/* Enable SDIO clock */
__HAL_RCC_SDMMC1_CLK_ENABLE();
/* Enable GPIOs clock */
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
/* Common GPIO configuration */
gpio_init_structure.Mode = GPIO_MODE_AF_PP;
gpio_init_structure.Pull = GPIO_PULLUP;
gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
gpio_init_structure.Alternate = GPIO_AF12_SDIO1;
/* SDMMC GPIO CLKIN PB8, D0 PC8, D1 PC9, D2 PC10, D3 PC11, CK PC12, CMD PD2 */
/* GPIOC configuration */
gpio_init_structure.Pin = GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;
HAL_GPIO_Init(GPIOC, &gpio_init_structure);
/* GPIOD configuration */
gpio_init_structure.Pin = GPIO_PIN_2;
HAL_GPIO_Init(GPIOD, &gpio_init_structure);
gpio_init_structure.Pin = GPIO_PIN_8 | GPIO_PIN_9;
HAL_GPIO_Init(GPIOB, &gpio_init_structure);
/* NVIC configuration for SDIO interrupts */
HAL_NVIC_SetPriority(SDMMC1_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(SDMMC1_IRQn);
}
/**
* @brief DeInitializes the MMC MSP.
* @param hmmc : MMC handle
* @retval None
*/
static void MMC_MspDeInit(MMC_HandleTypeDef *hmmc)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(hmmc);
/* Disable NVIC for SDIO interrupts */
HAL_NVIC_DisableIRQ(SDMMC1_IRQn);
/* DeInit GPIO pins can be done in the application
(by surcharging this __weak function) */
/* Disable SDMMC1 clock */
__HAL_RCC_SDMMC1_CLK_DISABLE();
/* GPIO pins clock and DMA clocks can be shut down in the application
by surcharging this __weak function */
}
#if (USE_HAL_MMC_REGISTER_CALLBACKS == 1)
/**
* @brief MMC Abort callbacks
* @param hmmc : MMC handle
* @retval None
*/
static void MMC_AbortCallback(MMC_HandleTypeDef *hmmc)
{
BSP_MMC_AbortCallback(0);
}
/**
* @brief Tx Transfer completed callbacks
* @param hmmc : MMC handle
* @retval None
*/
static void MMC_TxCpltCallback(MMC_HandleTypeDef *hmmc)
{
BSP_MMC_WriteCpltCallback(0);
}
/**
* @brief Rx Transfer completed callbacks
* @param hmmc : MMC handle
* @retval None
*/
static void MMC_RxCpltCallback(MMC_HandleTypeDef *hmmc)
{
BSP_MMC_ReadCpltCallback(0);
}
#endif
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/