-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathbsp_sd.c
728 lines (682 loc) · 24.5 KB
/
bsp_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
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/**
******************************************************************************
* @file bsp_sd.c
* @brief This file includes the uSD card driver mounted on stm32
* board.
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "bsp_sd.h"
#include "core_debug.h"
#include "interrupt.h"
#include "PeripheralPins.h"
#include "stm32yyxx_ll_gpio.h"
/* Definition for BSP SD */
#if defined(SDMMC1) || defined(SDMMC2)
#ifndef SD_INSTANCE
#if defined(SDMMC1)
#define SD_INSTANCE SDMMC1
#else
#define SD_INSTANCE SDMMC2
#endif
#endif
#define SD_CLK_EDGE SDMMC_CLOCK_EDGE_RISING
#if defined(SDMMC_CLOCK_BYPASS_DISABLE)
#define SD_CLK_BYPASS SDMMC_CLOCK_BYPASS_DISABLE
#endif
#define SD_CLK_PWR_SAVE SDMMC_CLOCK_POWER_SAVE_DISABLE
#define SD_BUS_WIDE_1B SDMMC_BUS_WIDE_1B
#define SD_BUS_WIDE_4B SDMMC_BUS_WIDE_4B
#define SD_HW_FLOW_CTRL_ENABLE SDMMC_HARDWARE_FLOW_CONTROL_ENABLE
#define SD_HW_FLOW_CTRL_DISABLE SDMMC_HARDWARE_FLOW_CONTROL_DISABLE
#ifndef SD_CLK_DIV
#if defined(SDMMC_TRANSFER_CLK_DIV)
#define SD_CLK_DIV SDMMC_TRANSFER_CLK_DIV
#else
#define SD_CLK_DIV SDMMC_NSpeed_CLK_DIV
#endif
#endif
#if defined(USE_SD_TRANSCEIVER) && (USE_SD_TRANSCEIVER != 0U)
#if defined(SDMMC_TRANSCEIVER_ENABLE)
#define SD_TRANSCEIVER_ENABLE SDMMC_TRANSCEIVER_ENABLE
#define SD_TRANSCEIVER_DISABLE SDMMC_TRANSCEIVER_DISABLE
#else
#define SD_TRANSCEIVER_ENABLE SDMMC_TRANSCEIVER_PRESENT
#define SD_TRANSCEIVER_DISABLE SDMMC_TRANSCEIVER_NOT_PRESENT
#endif
#endif
#elif defined(SDIO)
#define SD_INSTANCE SDIO
#define SD_CLK_EDGE SDIO_CLOCK_EDGE_RISING
#if defined(SDIO_CLOCK_BYPASS_DISABLE)
#define SD_CLK_BYPASS SDIO_CLOCK_BYPASS_DISABLE
#endif
#define SD_CLK_PWR_SAVE SDIO_CLOCK_POWER_SAVE_DISABLE
#define SD_BUS_WIDE_1B SDIO_BUS_WIDE_1B
#define SD_BUS_WIDE_4B SDIO_BUS_WIDE_4B
#define SD_HW_FLOW_CTRL_ENABLE SDIO_HARDWARE_FLOW_CONTROL_ENABLE
#define SD_HW_FLOW_CTRL_DISABLE SDIO_HARDWARE_FLOW_CONTROL_DISABLE
#ifndef SD_CLK_DIV
#define SD_CLK_DIV SDIO_TRANSFER_CLK_DIV
#endif
#else
#error "Unknown SD_INSTANCE"
#endif
#ifndef SD_HW_FLOW_CTRL
#define SD_HW_FLOW_CTRL SD_HW_FLOW_CTRL_DISABLE
#endif
#ifndef SD_BUS_WIDE
#define SD_BUS_WIDE SD_BUS_WIDE_4B
#endif
/* BSP SD Private Variables */
static SD_HandleTypeDef uSdHandle;
static uint32_t SD_detect_ll_gpio_pin = LL_GPIO_PIN_ALL;
static GPIO_TypeDef *SD_detect_gpio_port = GPIOA;
static uint32_t SD_detect_level = SD_DETECT_LEVEL;
#if defined(USE_SD_TRANSCEIVER) && (USE_SD_TRANSCEIVER != 0U)
static uint32_t SD_trans_en_ll_gpio_pin = LL_GPIO_PIN_ALL;
static GPIO_TypeDef *SD_trans_en_gpio_port = GPIOA;
static uint32_t SD_trans_sel_ll_gpio_pin = LL_GPIO_PIN_ALL;
static GPIO_TypeDef *SD_trans_sel_gpio_port = GPIOA;
#endif
#define SD_TRANSFER_OK ((uint8_t)0x00)
#define SD_TRANSFER_BUSY ((uint8_t)0x01)
SD_PinName_t SD_PinNames = {
.pin_d0 = NC,
.pin_d1 = NC,
.pin_d2 = NC,
.pin_d3 = NC,
.pin_cmd = NC,
.pin_ck = NC,
#if defined(SDMMC1) || defined(SDMMC2)
.pin_ckin = NC,
.pin_cdir = NC,
.pin_d0dir = NC,
.pin_d123dir = NC
#endif
};
#if defined(STM32_CORE_VERSION) && (STM32_CORE_VERSION > 0x02050000)
/**
* @brief Get the SD card device instance from pins
* @retval boolean true if successful, false otherwise
*/
bool BSP_SD_GetInstance(void)
{
SD_TypeDef *sd_d0 = NP;
SD_TypeDef *sd_d1 = NP;
SD_TypeDef *sd_d2 = NP;
SD_TypeDef *sd_d3 = NP;
SD_TypeDef *sd_cmd = NP;
SD_TypeDef *sd_ck = NP;
bool res = true;
/* If a pin is not defined, use the first pin available in the associated PinMap_SD_* arrays */
if (SD_PinNames.pin_d0 == NC) {
SD_PinNames.pin_d0 = PinMap_SD_DATA0[0].pin;
#if SD_BUS_WIDE == SD_BUS_WIDE_4B
SD_PinNames.pin_d1 = PinMap_SD_DATA1[0].pin;
SD_PinNames.pin_d2 = PinMap_SD_DATA2[0].pin;
SD_PinNames.pin_d3 = PinMap_SD_DATA3[0].pin;
#endif
}
if (SD_PinNames.pin_cmd == NC) {
SD_PinNames.pin_cmd = PinMap_SD_CMD[0].pin;
}
if (SD_PinNames.pin_ck == NC) {
SD_PinNames.pin_ck = PinMap_SD_CK[0].pin;
}
#if defined(SDMMC1) || defined(SDMMC2)
#if !defined(SDMMC_CKIN_NA)
if (SD_PinNames.pin_ckin == NC) {
SD_PinNames.pin_ckin = PinMap_SD_CKIN[0].pin;
}
#endif
#if !defined(SDMMC_CDIR_NA)
if (SD_PinNames.pin_cdir == NC) {
SD_PinNames.pin_cdir = PinMap_SD_CDIR[0].pin;
}
#endif
#if !defined(SDMMC_D0DIR_NA)
if (SD_PinNames.pin_d0dir == NC) {
SD_PinNames.pin_d0dir = PinMap_SD_D0DIR[0].pin;
}
#endif
#if !defined(SDMMC_D123DIR_NA)
if (SD_PinNames.pin_d123dir == NC) {
SD_PinNames.pin_d123dir = PinMap_SD_D123DIR[0].pin;
}
#endif
#endif /* SDMMC1 || SDMMC2 */
/* Get SD instance from pins */
sd_d0 = pinmap_peripheral(SD_PinNames.pin_d0, PinMap_SD_DATA0);
#if SD_BUS_WIDE == SD_BUS_WIDE_4B
sd_d1 = pinmap_peripheral(SD_PinNames.pin_d1, PinMap_SD_DATA1);
sd_d2 = pinmap_peripheral(SD_PinNames.pin_d2, PinMap_SD_DATA2);
sd_d3 = pinmap_peripheral(SD_PinNames.pin_d3, PinMap_SD_DATA3);
#endif
sd_cmd = pinmap_peripheral(SD_PinNames.pin_cmd, PinMap_SD_CMD);
sd_ck = pinmap_peripheral(SD_PinNames.pin_ck, PinMap_SD_CK);
/* Pins Dx/cmd/CK must not be NP. */
if (sd_d0 == NP ||
#if SD_BUS_WIDE == SD_BUS_WIDE_4B
sd_d1 == NP || sd_d2 == NP || sd_d3 == NP ||
#endif
sd_cmd == NP || sd_ck == NP) {
core_debug("ERROR: at least one SD pin has no peripheral\n");
res = false;
} else {
SD_TypeDef *sd_d01 = pinmap_merge_peripheral(sd_d0, sd_d1);
SD_TypeDef *sd_d23 = pinmap_merge_peripheral(sd_d2, sd_d3);
SD_TypeDef *sd_cx = pinmap_merge_peripheral(sd_cmd, sd_ck);
SD_TypeDef *sd_dx = pinmap_merge_peripheral(sd_d01, sd_d23);
SD_TypeDef *sd_base = pinmap_merge_peripheral(sd_dx, sd_cx);
if (sd_d01 == NP ||
#if SD_BUS_WIDE == SD_BUS_WIDE_4B
sd_d23 == NP ||
#endif
sd_cx == NP || sd_dx == NP || sd_base == NP) {
core_debug("ERROR: SD pins mismatch\n");
res = false;
}
uSdHandle.Instance = sd_base;
#if defined(SDMMC1) || defined(SDMMC2)
#if !defined(SDMMC_CKIN_NA)
if (res && (SD_PinNames.pin_ckin != NC)) {
SD_TypeDef *sd_ckin = pinmap_peripheral(SD_PinNames.pin_ckin, PinMap_SD_CKIN);
if (pinmap_merge_peripheral(sd_ckin, sd_base) == NP) {
core_debug("ERROR: SD CKIN pin mismatch\n");
res = false;
}
}
#endif
#if !defined(SDMMC_CDIR_NA)
if ((res && SD_PinNames.pin_cdir != NC)) {
SD_TypeDef *sd_cdir = pinmap_peripheral(SD_PinNames.pin_cdir, PinMap_SD_CDIR);
if (pinmap_merge_peripheral(sd_cdir, sd_base) == NP) {
core_debug("ERROR: SD CDIR pin mismatch\n");
res = false;
}
}
#endif
#if !defined(SDMMC_D0DIR_NA)
if (res && (SD_PinNames.pin_cdir != NC)) {
SD_TypeDef *sd_d0dir = pinmap_peripheral(SD_PinNames.pin_d0dir, PinMap_SD_D0DIR);
if (pinmap_merge_peripheral(sd_d0dir, sd_base) == NP) {
core_debug("ERROR: SD DODIR pin mismatch\n");
res = false;
}
}
#endif
#if !defined(SDMMC_D123DIR_NA)
if (res && (SD_PinNames.pin_cdir != NC)) {
SD_TypeDef *sd_d123dir = pinmap_peripheral(SD_PinNames.pin_d123dir, PinMap_SD_D123DIR);
if (pinmap_merge_peripheral(sd_d123dir, sd_base) == NP) {
core_debug("ERROR: SD D123DIR pin mismatch\n");
res = false;
}
}
#endif
#endif /* SDMMC1 || SDMMC2 */
/* Are all pins connected to the same SDx instance? */
if (uSdHandle.Instance == NP) {
core_debug("ERROR: SD pins mismatch\n");
res = false;
}
}
return res;
}
#endif /* STM32_CORE_VERSION && (STM32_CORE_VERSION > 0x02050000) */
/**
* @brief Initializes the SD card device with CS check if any.
* @retval SD status
*/
uint8_t BSP_SD_Init(void)
{
uint8_t sd_state = MSD_OK;
/* Check if SD is not yet initialized */
if (uSdHandle.State == HAL_SD_STATE_RESET) {
/* uSD device interface configuration */
#if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION <= 0x02050000)
uSdHandle.Instance = SD_INSTANCE;
#else
if (!BSP_SD_GetInstance()) {
sd_state = MSD_ERROR;
}
#endif /* !STM32_CORE_VERSION || (STM32_CORE_VERSION <= 0x02050000) */
uSdHandle.Init.ClockEdge = SD_CLK_EDGE;
#if defined(SD_CLK_BYPASS)
uSdHandle.Init.ClockBypass = SD_CLK_BYPASS;
#endif
uSdHandle.Init.ClockPowerSave = SD_CLK_PWR_SAVE;
uSdHandle.Init.BusWide = SD_BUS_WIDE_1B;
uSdHandle.Init.HardwareFlowControl = SD_HW_FLOW_CTRL;
uSdHandle.Init.ClockDiv = SD_CLK_DIV;
#if defined(USE_SD_TRANSCEIVER) && (USE_SD_TRANSCEIVER != 0U)
#if defined(SDMMC_TRANSCEIVER_ENABLE)
uSdHandle.Init.Transceiver = SD_TRANSCEIVER_ENABLE;
#else
uSdHandle.Init.TranceiverPresent = SD_TRANSCEIVER_ENABLE;
#endif
if (sd_state == MSD_OK) {
BSP_SD_Transceiver_MspInit(&uSdHandle, NULL);
}
#endif
if ((sd_state == MSD_OK) && (SD_detect_ll_gpio_pin != LL_GPIO_PIN_ALL)) {
/* Msp SD Detect pin initialization */
BSP_SD_Detect_MspInit(&uSdHandle, NULL);
if (BSP_SD_IsDetected() != SD_PRESENT) { /* Check if SD card is present */
sd_state = MSD_ERROR_SD_NOT_PRESENT;
}
}
if (sd_state == MSD_OK) {
/* Msp SD initialization */
BSP_SD_MspInit(&uSdHandle, NULL);
/* HAL SD initialization */
if (HAL_SD_Init(&uSdHandle) != HAL_OK) {
sd_state = MSD_ERROR;
}
/* Configure SD Bus width */
if (sd_state == MSD_OK) {
/* Enable wide operation */
if (HAL_SD_ConfigWideBusOperation(&uSdHandle, SD_BUS_WIDE) != 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;
#if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION <= 0x02050000)
uSdHandle.Instance = SD_INSTANCE;
#else
if (!BSP_SD_GetInstance()) {
sd_state = MSD_ERROR;
} else
#endif
{
/* HAL SD deinitialization */
if (HAL_SD_DeInit(&uSdHandle) != HAL_OK) {
sd_state = MSD_ERROR;
}
/* Msp SD deinitialization */
BSP_SD_MspDeInit(&uSdHandle, NULL);
if (SD_detect_ll_gpio_pin != LL_GPIO_PIN_ALL) {
BSP_SD_Detect_MspDeInit(&uSdHandle, NULL);
}
#if defined(USE_SD_TRANSCEIVER) && (USE_SD_TRANSCEIVER != 0U)
BSP_SD_Transceiver_MspDeInit(&uSdHandle, NULL);
#endif
}
return sd_state;
}
#if defined(USE_SD_TRANSCEIVER) && (USE_SD_TRANSCEIVER != 0U)
/**
* @brief Set the transceiver pin and port.
* @param enport enable gpio port
* @param enpin enable gpio pin
* @param selport select gpio port
* @param selpin select gpio pin
* @retval SD status
*/
uint8_t BSP_SD_TransceiverPin(GPIO_TypeDef *enport, uint32_t enpin, GPIO_TypeDef *selport, uint32_t selpin)
{
uint8_t sd_state = MSD_OK;
if ((enport != 0) && (selport != 0)) {
SD_trans_en_ll_gpio_pin = enpin;
SD_trans_en_gpio_port = enport;
SD_trans_sel_ll_gpio_pin = selpin;
SD_trans_sel_gpio_port = selport;
} else {
sd_state = MSD_ERROR;
}
return sd_state;
}
#endif
/**
* @brief Set the SD card device detect pin, port and level.
* @param p PinName of the detect pin
* @param level the level of the detect pin (HIGH or LOW)
* @retval SD status
*/
uint8_t BSP_SD_DetectPin(PinName p, uint32_t level)
{
uint8_t sd_state = MSD_OK;
GPIO_TypeDef *port = set_GPIO_Port_Clock(STM_PORT(p));
uint32_t pin = STM_LL_GPIO_PIN(p);
if (port != 0) {
SD_detect_ll_gpio_pin = pin;
SD_detect_gpio_port = port;
SD_detect_level = level;
} else {
sd_state = MSD_ERROR;
}
return sd_state;
}
/**
* @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)
{
/* Check SD card detect pin */
return (LL_GPIO_IsInputPinSet(SD_detect_gpio_port, SD_detect_ll_gpio_pin) == SD_detect_level) ? SD_PRESENT : SD_NOT_PRESENT;
}
/**
* @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)
{
return (HAL_SD_ReadBlocks(&uSdHandle, (uint8_t *)pData, ReadAddr, NumOfBlocks, Timeout) != HAL_OK) ? MSD_ERROR : MSD_OK;
}
/**
* @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)
{
return (HAL_SD_WriteBlocks(&uSdHandle, (uint8_t *)pData, WriteAddr, NumOfBlocks, Timeout) != HAL_OK) ? MSD_ERROR : MSD_OK;
}
/**
* @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(uint64_t StartAddr, uint64_t EndAddr)
{
return (HAL_SD_Erase(&uSdHandle, StartAddr, EndAddr) != HAL_OK) ? MSD_ERROR : MSD_OK;
}
/**
* @brief Initializes the SD MSP.
* @param hsd: SD handle
* @param Params : pointer on additional configuration parameters, can be NULL.
*/
__weak void BSP_SD_MspInit(SD_HandleTypeDef *hsd, void *Params)
{
UNUSED(Params);
#if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION <= 0x02050000)
/* Configure SD GPIOs */
const PinMap *map = PinMap_SD;
while (map->pin != NC) {
pin_function(map->pin, map->function);
map++;
}
#else
/* Configure SD GPIO pins */
pinmap_pinout(SD_PinNames.pin_d0, PinMap_SD_DATA0);
#if SD_BUS_WIDE == SD_BUS_WIDE_4B
pinmap_pinout(SD_PinNames.pin_d1, PinMap_SD_DATA1);
pinmap_pinout(SD_PinNames.pin_d2, PinMap_SD_DATA2);
pinmap_pinout(SD_PinNames.pin_d3, PinMap_SD_DATA3);
#endif
pinmap_pinout(SD_PinNames.pin_cmd, PinMap_SD_CMD);
pinmap_pinout(SD_PinNames.pin_ck, PinMap_SD_CK);
#if defined(SDMMC1) || defined(SDMMC2)
#if !defined(SDMMC_CKIN_NA)
if (SD_PinNames.pin_ckin != NC) {
pinmap_pinout(SD_PinNames.pin_ckin, PinMap_SD_CKIN);
}
#endif
#if !defined(SDMMC_CDIR_NA)
if (SD_PinNames.pin_cdir != NC) {
pinmap_pinout(SD_PinNames.pin_cdir, PinMap_SD_CDIR);
}
#endif
#if !defined(SDMMC_D0DIR_NA)
if (SD_PinNames.pin_d0dir != NC) {
pinmap_pinout(SD_PinNames.pin_d0dir, PinMap_SD_D0DIR);
}
#endif
#if !defined(SDMMC_D123DIR_NA)
if (SD_PinNames.pin_d123dir != NC) {
pinmap_pinout(SD_PinNames.pin_d123dir, PinMap_SD_D123DIR);
}
#endif
#endif /* SDMMC1 || SDMMC2 */
#endif /* !STM32_CORE_VERSION || (STM32_CORE_VERSION <= 0x02050000) */
/* Enable SD clock */
#if defined(SDMMC1) || defined(SDMMC2)
#if defined(SDMMC1)
if (hsd->Instance == SDMMC1) {
__HAL_RCC_SDMMC1_CLK_ENABLE();
}
#endif
#if defined(SDMMC2)
if (hsd->Instance == SDMMC2) {
__HAL_RCC_SDMMC2_CLK_ENABLE();
}
#endif
#else
UNUSED(hsd);
__HAL_RCC_SDIO_CLK_ENABLE();
#endif
}
/**
* @brief DeInitializes the SD MSP.
* @param hsd: SD handle
* @param Params : pointer on additional configuration parameters, can be NULL.
*/
__weak void BSP_SD_MspDeInit(SD_HandleTypeDef *hsd, void *Params)
{
UNUSED(Params);
/* DeInit GPIO pins can be done in the application
(by surcharging this __weak function) */
#if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION <= 0x02050000)
const PinMap *map = PinMap_SD;
while (map->pin != NC) {
HAL_GPIO_DeInit((GPIO_TypeDef *)STM_PORT(map->pin), STM_GPIO_PIN(map->pin));
map++;
}
#else
HAL_GPIO_DeInit((GPIO_TypeDef *)get_GPIO_Port(STM_PORT(SD_PinNames.pin_d0)), STM_GPIO_PIN(SD_PinNames.pin_d0));
HAL_GPIO_DeInit((GPIO_TypeDef *)get_GPIO_Port(STM_PORT(SD_PinNames.pin_d1)), STM_GPIO_PIN(SD_PinNames.pin_d1));
HAL_GPIO_DeInit((GPIO_TypeDef *)get_GPIO_Port(STM_PORT(SD_PinNames.pin_d2)), STM_GPIO_PIN(SD_PinNames.pin_d2));
HAL_GPIO_DeInit((GPIO_TypeDef *)get_GPIO_Port(STM_PORT(SD_PinNames.pin_d3)), STM_GPIO_PIN(SD_PinNames.pin_d3));
HAL_GPIO_DeInit((GPIO_TypeDef *)get_GPIO_Port(STM_PORT(SD_PinNames.pin_cmd)), STM_GPIO_PIN(SD_PinNames.pin_cmd));
HAL_GPIO_DeInit((GPIO_TypeDef *)get_GPIO_Port(STM_PORT(SD_PinNames.pin_ck)), STM_GPIO_PIN(SD_PinNames.pin_ck));
#if defined(SDMMC1) || defined(SDMMC2)
#if !defined(SDMMC_CKIN_NA)
if (SD_PinNames.pin_ckin != NC) {
HAL_GPIO_DeInit((GPIO_TypeDef *)get_GPIO_Port(STM_PORT(SD_PinNames.pin_ckin)), STM_GPIO_PIN(SD_PinNames.pin_ckin));
}
#endif
#if !defined(SDMMC_CDIR_NA)
if (SD_PinNames.pin_cdir != NC) {
HAL_GPIO_DeInit((GPIO_TypeDef *)get_GPIO_Port(STM_PORT(SD_PinNames.pin_cdir)), STM_GPIO_PIN(SD_PinNames.pin_cdir));
}
#endif
#if !defined(SDMMC_D0DIR_NA)
if (SD_PinNames.pin_d0dir != NC) {
HAL_GPIO_DeInit((GPIO_TypeDef *)get_GPIO_Port(STM_PORT(SD_PinNames.pin_d0dir)), STM_GPIO_PIN(SD_PinNames.pin_d0dir));
}
#endif
#if !defined(SDMMC_D123DIR_NA)
if (SD_PinNames.pin_d123dir != NC) {
HAL_GPIO_DeInit((GPIO_TypeDef *)get_GPIO_Port(STM_PORT(SD_PinNames.pin_d123dir)), STM_GPIO_PIN(SD_PinNames.pin_d123dir));
}
#endif
#endif /* SDMMC1 || SDMMC2 */
#endif /* !STM32_CORE_VERSION || (STM32_CORE_VERSION <= 0x02050000) */
/* Disable SD clock */
#if defined(SDMMC1) || defined(SDMMC2)
#if defined(SDMMC1)
if (hsd->Instance == SDMMC1) {
__HAL_RCC_SDMMC1_CLK_DISABLE();
}
#endif
#if defined(SDMMC2)
if (hsd->Instance == SDMMC2) {
__HAL_RCC_SDMMC2_CLK_DISABLE();
}
#endif
#else
UNUSED(hsd);
__HAL_RCC_SDIO_CLK_DISABLE();
#endif
}
/**
* @brief Initializes the SD Detect pin MSP.
* @param hsd: SD handle
* @param Params : pointer on additional configuration parameters, can be NULL.
*/
__weak void BSP_SD_Detect_MspInit(SD_HandleTypeDef *hsd, void *Params)
{
UNUSED(hsd);
UNUSED(Params);
/* GPIO configuration in input for uSD_Detect signal */
#ifdef LL_GPIO_SPEED_FREQ_VERY_HIGH
LL_GPIO_SetPinSpeed(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_SPEED_FREQ_VERY_HIGH);
#else
LL_GPIO_SetPinSpeed(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_SPEED_FREQ_HIGH);
#endif
LL_GPIO_SetPinMode(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_MODE_INPUT);
LL_GPIO_SetPinPull(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_PULL_UP);
}
/**
* @brief DeInitializes the SD Detect pin MSP.
* @param hsd: SD handle
* @param Params : pointer on additional configuration parameters, can be NULL.
*/
__weak void BSP_SD_Detect_MspDeInit(SD_HandleTypeDef *hsd, void *Params)
{
UNUSED(hsd);
UNUSED(Params);
/* GPIO configuration in analog to saves the consumption */
LL_GPIO_SetPinSpeed(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_SPEED_FREQ_LOW);
#ifndef LL_GPIO_PULL_NO
/* For STM32F1xx */
LL_GPIO_SetPinPull(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_MODE_FLOATING);
#else
LL_GPIO_SetPinPull(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_PULL_NO);
#endif
LL_GPIO_SetPinMode(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_MODE_ANALOG);
}
#if defined(USE_SD_TRANSCEIVER) && (USE_SD_TRANSCEIVER != 0U)
/**
* @brief Initializes the SD Transceiver pin MSP.
* @param hsd: SD handle
* @param Params : pointer on additional configuration parameters, can be NULL.
*/
__weak void BSP_SD_Transceiver_MspInit(SD_HandleTypeDef *hsd, void *Params)
{
UNUSED(hsd);
UNUSED(Params);
LL_GPIO_SetPinSpeed(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinMode(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin, LL_GPIO_MODE_OUTPUT);
#ifndef LL_GPIO_PULL_NO
/* For STM32F1xx */
LL_GPIO_SetPinPull(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_MODE_FLOATING);
#else
LL_GPIO_SetPinPull(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin, LL_GPIO_PULL_NO);
#endif
LL_GPIO_SetPinSpeed(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinMode(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin, LL_GPIO_MODE_OUTPUT);
#ifndef LL_GPIO_PULL_NO
/* For STM32F1xx */
LL_GPIO_SetPinPull(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_MODE_FLOATING);
#else
LL_GPIO_SetPinPull(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin, LL_GPIO_PULL_NO);
#endif
/* Enable the level shifter */
LL_GPIO_SetOutputPin(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin);
/* By default start with the default voltage */
LL_GPIO_ResetOutputPin(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin);
}
/**
* @brief DeInitializes the SD Transceiver pin MSP.
* @param hsd: SD handle
* @param Params : pointer on additional configuration parameters, can be NULL.
*/
__weak void BSP_SD_Transceiver_MspDeInit(SD_HandleTypeDef *hsd, void *Params)
{
UNUSED(hsd);
UNUSED(Params);
LL_GPIO_SetPinSpeed(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin, LL_GPIO_SPEED_FREQ_LOW);
LL_GPIO_SetPinMode(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin, LL_GPIO_MODE_ANALOG);
LL_GPIO_SetPinPull(SD_trans_en_gpio_port, SD_trans_en_ll_gpio_pin, LL_GPIO_PULL_NO);
LL_GPIO_SetPinSpeed(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin, LL_GPIO_SPEED_FREQ_LOW);
LL_GPIO_SetPinMode(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin, LL_GPIO_MODE_ANALOG);
LL_GPIO_SetPinPull(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin, LL_GPIO_PULL_NO);
}
/**
* @brief Enable/Disable the SD Transceiver 1.8V Mode Callback.
* @param status: Voltage Switch State
* @retval None
*/
#if defined(SDMMC_TRANSCEIVER_ENABLE)
void HAL_SDEx_DriveTransceiver_1_8V_Callback(FlagStatus status)
#else
void HAL_SD_DriveTransceiver_1_8V_Callback(FlagStatus status)
#endif
{
if (status == SET) {
LL_GPIO_SetOutputPin(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin);
} else {
LL_GPIO_ResetOutputPin(SD_trans_sel_gpio_port, SD_trans_sel_ll_gpio_pin);
}
}
#endif /* USE_SD_TRANSCEIVER && (USE_SD_TRANSCEIVER != 0U) */
/**
* @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(&uSdHandle) == 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 boolean true if successful, false otherwise
*/
bool BSP_SD_GetCardInfo(HAL_SD_CardInfoTypeDef *CardInfo)
{
/* Get SD card Information */
return (HAL_SD_GetCardInfo(&uSdHandle, CardInfo) == HAL_OK);
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/