-
Notifications
You must be signed in to change notification settings - Fork 0
/
stm32l4r9i_eval_sd.c
517 lines (438 loc) · 13.9 KB
/
stm32l4r9i_eval_sd.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
/**
******************************************************************************
* @file stm32l4r9i_eval_sd.c
* @author MCD Application Team
* @brief This file includes the uSD card driver.
@verbatim
==============================================================================
##### How to use this driver #####
==============================================================================
(#) This driver is used to drive the micro SD external card mounted on STM32L4R9I_EVAL
evaluation board.
(#) This driver does not need a specific component driver for the micro SD device
to be included with.
(#) Initialization steps:
(++) Initialize the micro SD card using the BSP_SD_Init() function. This
function includes the MSP layer hardware resources initialization (BSP_SD_MspInit())
and the SDMMC1 interface configuration to interface with the external micro SD. It
also includes the micro SD initialization sequence.
(++) To check the SD card presence you can use the function BSP_SD_IsDetected() which
returns the detection status.
(++) If SD presence detection interrupt mode is desired, you must configure the
SD detection interrupt mode by calling the functions BSP_SD_ITConfig().
The interrupt is generated as an external interrupt whenever the
micro SD card is plugged/unplugged in/from the evaluation board. The SD detection
is managed by MFX, so the SD detection interrupt has to be treated by MFX_IRQOUT
gpio pin IRQ handler.
(++) The function BSP_SD_GetCardInfo() is used to get the micro SD card information
which is stored in the structure "HAL_SD_CardInfoTypedef".
(#) Micro SD card operations
(++) The micro SD card can be accessed with read/write block(s) operations once
it is reay for access. The access can be performed whether using the polling
mode by calling the functions BSP_SD_ReadBlocks()/BSP_SD_WriteBlocks(),
or by DMA transfer using the functions BSP_SD_ReadBlocks_DMA()/BSP_SD_WriteBlocks_DMA().
(++) The DMA transfer complete is used with interrupt mode. Once the SD transfer
is complete, the DMA Tx/Rx transfer complete are handled using the
BSP_SD_WriteCpltCallback()/BSP_SD_ReadCpltCallback() user callback functions implemented
by the user at application level.
(++) The SD erase block(s) is performed using the function BSP_SD_Erase() with specifying
the number of blocks to erase.
(++) The SD runtime status is returned when calling the function BSP_SD_GetStatus().
[..]
@endverbatim
******************************************************************************
* @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.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32l4r9i_eval_io.h"
#include "stm32l4r9i_eval_sd.h"
/** @addtogroup BSP
* @{
*/
/** @addtogroup STM32L4R9I_EVAL
* @{
*/
/** @defgroup STM32L4R9I_EVAL_SD STM32L4R9I_EVAL SD
* @{
*/
/* Private variables ---------------------------------------------------------*/
/** @defgroup STM32L4R9I_EVAL_SD_Private_Variables Private Variables
* @{
*/
SD_HandleTypeDef hsd_eval;
static uint8_t UseExtiModeDetection = 0;
/**
* @}
*/
/* Private function prototypes -----------------------------------------------*/
/* Exported functions ---------------------------------------------------------*/
/** @addtogroup STM32L4R9I_EVAL_SD_Exported_Functions
* @{
*/
/**
* @brief Initializes the SD card device.
* @retval SD status
*/
uint8_t BSP_SD_Init(void)
{
uint8_t sd_state = MSD_OK;
/* uSD device interface configuration */
hsd_eval.Instance = SDMMC1;
hsd_eval.Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
hsd_eval.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
hsd_eval.Init.BusWide = SDMMC_BUS_WIDE_4B;
hsd_eval.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_ENABLE;
hsd_eval.Init.ClockDiv = 1;
hsd_eval.Init.Transceiver = SDMMC_TRANSCEIVER_DISABLE;
/* Initialize IO functionalities (MFX) used by SD detect pin */
BSP_IO_Init();
/* Check if the SD card is plugged in the slot */
BSP_IO_ConfigPin(SD_DETECT_PIN, IO_MODE_INPUT_PU);
/* Check if the SD card is plugged in the slot */
if(BSP_SD_IsDetected() != SD_PRESENT)
{
return MSD_ERROR_SD_NOT_PRESENT;
}
/* Msp SD initialization */
BSP_SD_MspInit(&hsd_eval, NULL);
/* HAL SD initialization */
if(HAL_SD_Init(&hsd_eval) != HAL_OK)
{
sd_state = MSD_ERROR;
}
return sd_state;
}
/**
* @brief DeInitializes the SD card device.
* @retval SD status
*/
uint8_t BSP_SD_DeInit(void)
{
uint8_t sd_state = MSD_OK;
hsd_eval.Instance = SDMMC1;
/* Set back Mfx pin to INPUT mode in case it was in exti */
UseExtiModeDetection = 0;
/* HAL SD deinitialization */
if(HAL_SD_DeInit(&hsd_eval) != HAL_OK)
{
sd_state = MSD_ERROR;
}
/* Msp SD deinitialization */
BSP_SD_MspDeInit(&hsd_eval, NULL);
return sd_state;
}
/**
* @brief Configures Interrupt mode for SD detection pin.
* @retval IO_OK: if all initializations are OK. Other value if error.
*/
uint8_t BSP_SD_ITConfig(void)
{
/* Configure Interrupt mode for SD detection pin */
/* Note: disabling exti mode can be done calling BSP_SD_DeInit() */
UseExtiModeDetection = 1;
/* Check SD card detect pin */
if(BSP_IO_ReadPin(SD_DETECT_PIN) == IO_PIN_RESET)
{
/* Card present */
return BSP_IO_ConfigPin(SD_DETECT_PIN, IO_MODE_IT_RISING_EDGE_PU);
}
else
{
/* Card not present */
return BSP_IO_ConfigPin(SD_DETECT_PIN, IO_MODE_IT_FALLING_EDGE_PU);
}
}
/**
* @brief Detects if SD card is correctly plugged in the memory slot or not.
* @retval Returns if SD is detected or not
*/
uint8_t BSP_SD_IsDetected(void)
{
__IO uint8_t status = SD_PRESENT;
/* Check SD card detect pin */
if (BSP_IO_ReadPin(SD_DETECT_PIN) == IO_PIN_RESET)
{
/* Card present */
if (UseExtiModeDetection)
{
BSP_IO_ConfigPin(SD_DETECT_PIN, IO_MODE_IT_RISING_EDGE_PU);
}
}
else
{
/* Card not present */
status = SD_NOT_PRESENT;
if (UseExtiModeDetection)
{
BSP_IO_ConfigPin(SD_DETECT_PIN, IO_MODE_IT_FALLING_EDGE_PU);
}
}
return status;
}
/**
* @brief Reads block(s) from a specified address in an SD card, in polling mode.
* @param pData: Pointer to the buffer that will contain the data to transmit
* @param ReadAddr: Address from where data is to be read
* @param NumOfBlocks: Number of SD blocks to read
* @param Timeout: Timeout for read operation
* @retval SD status
*/
uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout)
{
HAL_StatusTypeDef sd_state = HAL_OK;
sd_state = HAL_SD_ReadBlocks(&hsd_eval, (uint8_t *)pData, ReadAddr, NumOfBlocks, Timeout);
if (sd_state == HAL_OK)
{
return MSD_OK;
}
else
{
return MSD_ERROR;
}
}
/**
* @brief Writes block(s) to a specified address in an SD card, in polling mode.
* @param pData: Pointer to the buffer that will contain the data to transmit
* @param WriteAddr: Address from where data is to be written
* @param NumOfBlocks: Number of SD blocks to write
* @param Timeout: Timeout for write operation
* @retval SD status
*/
uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout)
{
HAL_StatusTypeDef sd_state = HAL_OK;
sd_state = HAL_SD_WriteBlocks(&hsd_eval, (uint8_t *)pData, WriteAddr, NumOfBlocks, Timeout);
if( sd_state == HAL_OK)
{
return MSD_OK;
}
else
{
return MSD_ERROR;
}
}
/**
* @brief Reads block(s) from a specified address in an SD card, in DMA mode.
* @param pData: Pointer to the buffer that will contain the data to transmit
* @param ReadAddr: Address from where data is to be read
* @param NumOfBlocks: Number of SD blocks to read
* @retval SD status
*/
uint8_t BSP_SD_ReadBlocks_DMA(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks)
{
HAL_StatusTypeDef sd_state = HAL_OK;
/* Read block(s) in DMA transfer mode */
sd_state = HAL_SD_ReadBlocks_DMA(&hsd_eval, (uint8_t *)pData, ReadAddr, NumOfBlocks);
if( sd_state == HAL_OK)
{
return MSD_OK;
}
else
{
return MSD_ERROR;
}
}
/**
* @brief Writes block(s) to a specified address in an SD card, in DMA mode.
* @param pData: Pointer to the buffer that will contain the data to transmit
* @param WriteAddr: Address from where data is to be written
* @param NumOfBlocks: Number of SD blocks to write
* @retval SD status
*/
uint8_t BSP_SD_WriteBlocks_DMA(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks)
{
HAL_StatusTypeDef sd_state = HAL_OK;
/* Write block(s) in DMA transfer mode */
sd_state = HAL_SD_WriteBlocks_DMA(&hsd_eval, (uint8_t *)pData, WriteAddr, NumOfBlocks);
if( sd_state == HAL_OK)
{
return MSD_OK;
}
else
{
return MSD_ERROR;
}
}
/**
* @brief Erases the specified memory area of the given SD card.
* @param StartAddr: Start byte address
* @param EndAddr: End byte address
* @retval SD status
*/
uint8_t BSP_SD_Erase(uint32_t StartAddr, uint32_t EndAddr)
{
HAL_StatusTypeDef sd_state = HAL_OK;
sd_state = HAL_SD_Erase(&hsd_eval, StartAddr, EndAddr);
if( sd_state == HAL_OK)
{
return MSD_OK;
}
else
{
return MSD_ERROR;
}
}
/**
* @brief Gets the current SD card data status.
* @retval Data transfer state.
* This value can be one of the following values:
* @arg SD_TRANSFER_OK: No data transfer is acting
* @arg SD_TRANSFER_BUSY: Data transfer is acting
*/
uint8_t BSP_SD_GetCardState(void)
{
return((HAL_SD_GetCardState(&hsd_eval) == HAL_SD_CARD_TRANSFER ) ? SD_TRANSFER_OK : SD_TRANSFER_BUSY);
}
/**
* @brief Get SD information about specific SD card.
* @param CardInfo: Pointer to HAL_SD_CardInfoTypedef structure
* @retval None
*/
void BSP_SD_GetCardInfo(BSP_SD_CardInfo *CardInfo)
{
/* Get SD card Information */
HAL_SD_GetCardInfo(&hsd_eval, CardInfo);
}
/**
* @brief Initializes the SD MSP.
* @retval None
*/
__weak void BSP_SD_MspInit(SD_HandleTypeDef *hsd, void *Params)
{
GPIO_InitTypeDef gpioinitstruct = {0};
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;
/* Check whether HSI48 is enabled or not */
HAL_RCC_GetOscConfig(&RCC_OscInitStruct);
if(RCC_OscInitStruct.HSI48State != RCC_HSI48_ON)
{
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48;
RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
while(1) {}
}
}
/* Configure the Eval SDMMC1 clock source. The clock is derived from the HSI48 */
RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_SDMMC1;
RCC_PeriphClkInit.Sdmmc1ClockSelection = RCC_SDMMC1CLKSOURCE_HSI48;
if(HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit) != HAL_OK)
{
while(1) {}
}
/* Enable SDMMC1 clock */
__HAL_RCC_SDMMC1_CLK_ENABLE();
/* Enable GPIOs clock */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
/* Common GPIO configuration */
gpioinitstruct.Mode = GPIO_MODE_AF_PP;
gpioinitstruct.Pull = GPIO_NOPULL;
gpioinitstruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
gpioinitstruct.Alternate = GPIO_AF12_SDMMC1;
/* GPIOC configuration */
gpioinitstruct.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;
HAL_GPIO_Init(GPIOC, &gpioinitstruct);
/* GPIOD configuration */
gpioinitstruct.Pin = GPIO_PIN_2;
HAL_GPIO_Init(GPIOD, &gpioinitstruct);
/* NVIC configuration for SDMMC1 interrupts */
HAL_NVIC_SetPriority(SDMMCx_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(SDMMCx_IRQn);
}
/**
* @brief De-Initializes the SD MSP.
* @retval None
*/
__weak void BSP_SD_MspDeInit(SD_HandleTypeDef *hsd, void *Params)
{
GPIO_InitTypeDef gpioinitstruct = {0};
/* NVIC configuration for SDMMC1 interrupts */
HAL_NVIC_DisableIRQ(SDMMCx_IRQn);
/* Disable SDMMC1 clock */
__HAL_RCC_SDMMC1_CLK_DISABLE();
/* Enable GPIOs clock */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
/* Common GPIO configuration */
gpioinitstruct.Mode = GPIO_MODE_ANALOG;
gpioinitstruct.Pull = GPIO_NOPULL;
gpioinitstruct.Speed = GPIO_SPEED_FREQ_LOW;
gpioinitstruct.Alternate = 0;
/* GPIOC configuration */
gpioinitstruct.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;
HAL_GPIO_Init(GPIOC, &gpioinitstruct);
/* GPIOD configuration */
gpioinitstruct.Pin = GPIO_PIN_2;
HAL_GPIO_Init(GPIOD, &gpioinitstruct);
}
/**
* @brief BSP SD Abort callback
* @retval None
*/
__weak void BSP_SD_AbortCallback(void)
{
}
/**
* @brief BSP Tx Transfer completed callback
* @retval None
*/
__weak void BSP_SD_WriteCpltCallback(void)
{
}
/**
* @brief BSP Rx Transfer completed callback
* @retval None
*/
__weak void BSP_SD_ReadCpltCallback(void)
{
}
/**
* @brief SD Abort callback
* @param hsd: SD handle
* @retval None
*/
void HAL_SD_AbortCallback(SD_HandleTypeDef *hsd)
{
BSP_SD_AbortCallback();
}
/**
* @brief Tx Transfer completed callback
* @param hsd: SD handle
* @retval None
*/
void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
{
BSP_SD_WriteCpltCallback();
}
/**
* @brief Rx Transfer completed callback
* @param hsd: SD handle
* @retval None
*/
void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
{
BSP_SD_ReadCpltCallback();
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/