Skip to content

Commit 30afe43

Browse files
committed
Use LL instead of HAL for GPIO
Note: BSP_SD_DetectITConfig() is currently not used Anyway use interrupt API for SD detection pin in IT mode for future. Signed-off-by: Frederic.Pillon <frederic.pillon@st.com>
1 parent b60bbe5 commit 30afe43

File tree

3 files changed

+76
-69
lines changed

3 files changed

+76
-69
lines changed

src/Sd2Card.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ uint8_t Sd2Card::init(uint32_t detectpin)
4343
PinName p = digitalPinToPinName(detectpin);
4444
if ((p == NC) || \
4545
BSP_SD_DetectPin(set_GPIO_Port_Clock(STM_PORT(p)),
46-
STM_GPIO_PIN(p)) != MSD_OK) {
46+
STM_LL_GPIO_PIN(p)) != MSD_OK) {
4747
return FALSE;
4848
}
4949
}

src/bsp_sd.c

+74-67
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535

3636
/* Includes ------------------------------------------------------------------*/
3737
#include "bsp_sd.h"
38+
#include "interrupt.h"
3839
#include "PeripheralPins.h"
40+
#include "stm32yyxx_ll_gpio.h"
3941

4042
#ifdef SDMMC1
4143
/* Definition for BSP SD */
@@ -75,7 +77,7 @@
7577

7678
/* BSP SD Private Variables */
7779
static SD_HandleTypeDef uSdHandle;
78-
static uint32_t SD_detect_gpio_pin = GPIO_PIN_All;
80+
static uint32_t SD_detect_ll_gpio_pin = LL_GPIO_PIN_ALL;
7981
static GPIO_TypeDef *SD_detect_gpio_port = GPIOA;
8082
#ifndef STM32L1xx
8183
#define SD_OK HAL_OK
@@ -106,7 +108,7 @@ uint8_t BSP_SD_Init(void)
106108
uSdHandle.Init.HardwareFlowControl = SD_HW_FLOW_CTRL;
107109
uSdHandle.Init.ClockDiv = SD_CLK_DIV;
108110

109-
if (SD_detect_gpio_pin != GPIO_PIN_All) {
111+
if (SD_detect_ll_gpio_pin != LL_GPIO_PIN_ALL) {
110112
/* Msp SD Detect pin initialization */
111113
BSP_SD_Detect_MspInit(&uSdHandle, NULL);
112114
if (BSP_SD_IsDetected() != SD_PRESENT) { /* Check if SD card is present */
@@ -170,7 +172,7 @@ uint8_t BSP_SD_DeInit(void)
170172
uint8_t BSP_SD_DetectPin(GPIO_TypeDef *port, uint32_t pin)
171173
{
172174
if (port != 0) {
173-
SD_detect_gpio_pin = pin;
175+
SD_detect_ll_gpio_pin = pin;
174176
SD_detect_gpio_port = port;
175177
return MSD_OK;
176178
}
@@ -181,61 +183,67 @@ uint8_t BSP_SD_DetectPin(GPIO_TypeDef *port, uint32_t pin)
181183
* @brief Configures Interrupt mode for SD detection pin.
182184
* @retval Status
183185
*/
184-
uint8_t BSP_SD_DetectITConfig(void)
186+
uint8_t BSP_SD_DetectITConfig(void (*callback)(void))
185187
{
186-
uint8_t sd_state = MSD_OK;
187-
GPIO_InitTypeDef gpio_init_structure;
188-
IRQn_Type sd_detect_EXTI_IRQn = EXTI0_IRQn;
189-
190-
/* Configure Interrupt mode for SD detection pin */
191-
gpio_init_structure.Pin = SD_detect_gpio_pin;
192-
gpio_init_structure.Pull = GPIO_PULLUP;
193-
gpio_init_structure.Speed = SD_GPIO_SPEED;
194-
gpio_init_structure.Mode = GPIO_MODE_IT_RISING_FALLING;
195-
HAL_GPIO_Init(SD_detect_gpio_port, &gpio_init_structure);
196-
197-
if (SD_detect_gpio_pin == GPIO_PIN_0) {
198-
sd_detect_EXTI_IRQn = EXTI0_IRQn;
199-
} else {
200-
if (SD_detect_gpio_pin == GPIO_PIN_1) {
201-
sd_detect_EXTI_IRQn = EXTI1_IRQn;
202-
} else {
203-
if (SD_detect_gpio_pin == GPIO_PIN_2) {
204-
sd_detect_EXTI_IRQn = EXTI2_IRQn;
205-
} else {
206-
if (SD_detect_gpio_pin == GPIO_PIN_3) {
207-
sd_detect_EXTI_IRQn = EXTI3_IRQn;
208-
} else {
209-
if (SD_detect_gpio_pin == GPIO_PIN_4) {
210-
sd_detect_EXTI_IRQn = EXTI4_IRQn;
211-
} else {
212-
if ((SD_detect_gpio_pin == GPIO_PIN_5) || \
213-
(SD_detect_gpio_pin == GPIO_PIN_6) || \
214-
(SD_detect_gpio_pin == GPIO_PIN_7) || \
215-
(SD_detect_gpio_pin == GPIO_PIN_8) || \
216-
(SD_detect_gpio_pin == GPIO_PIN_9)) {
217-
sd_detect_EXTI_IRQn = EXTI9_5_IRQn;
218-
} else {
219-
if ((SD_detect_gpio_pin == GPIO_PIN_10) || \
220-
(SD_detect_gpio_pin == GPIO_PIN_11) || \
221-
(SD_detect_gpio_pin == GPIO_PIN_12) || \
222-
(SD_detect_gpio_pin == GPIO_PIN_13) || \
223-
(SD_detect_gpio_pin == GPIO_PIN_14) || \
224-
(SD_detect_gpio_pin == GPIO_PIN_15)) {
225-
sd_detect_EXTI_IRQn = EXTI15_10_IRQn;
226-
} else {
227-
sd_state = MSD_ERROR;
228-
}
229-
}
230-
}
231-
}
232-
}
188+
uint8_t sd_state = MSD_ERROR;
189+
if (SD_detect_ll_gpio_pin != LL_GPIO_PIN_ALL) {
190+
LL_GPIO_SetPinPull(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_PULL_UP);
191+
uint16_t SD_detect_gpio_pin = GPIO_PIN_All;
192+
switch (SD_detect_ll_gpio_pin) {
193+
case LL_GPIO_PIN_0:
194+
SD_detect_gpio_pin = GPIO_PIN_0;
195+
break;
196+
case LL_GPIO_PIN_1:
197+
SD_detect_gpio_pin = GPIO_PIN_1;
198+
break;
199+
case LL_GPIO_PIN_2:
200+
SD_detect_gpio_pin = GPIO_PIN_2;
201+
break;
202+
case LL_GPIO_PIN_3:
203+
SD_detect_gpio_pin = GPIO_PIN_3;
204+
break;
205+
case LL_GPIO_PIN_4:
206+
SD_detect_gpio_pin = GPIO_PIN_4;
207+
break;
208+
case LL_GPIO_PIN_5:
209+
SD_detect_gpio_pin = GPIO_PIN_5;
210+
break;
211+
case LL_GPIO_PIN_6:
212+
SD_detect_gpio_pin = GPIO_PIN_6;
213+
break;
214+
case LL_GPIO_PIN_7:
215+
SD_detect_gpio_pin = GPIO_PIN_7;
216+
break;
217+
case LL_GPIO_PIN_8:
218+
SD_detect_gpio_pin = GPIO_PIN_8;
219+
break;
220+
case LL_GPIO_PIN_9:
221+
SD_detect_gpio_pin = GPIO_PIN_9;
222+
break;
223+
case LL_GPIO_PIN_10:
224+
SD_detect_gpio_pin = GPIO_PIN_10;
225+
break;
226+
case LL_GPIO_PIN_11:
227+
SD_detect_gpio_pin = GPIO_PIN_11;
228+
break;
229+
case LL_GPIO_PIN_12:
230+
SD_detect_gpio_pin = GPIO_PIN_12;
231+
break;
232+
case LL_GPIO_PIN_13:
233+
SD_detect_gpio_pin = GPIO_PIN_13;
234+
break;
235+
case LL_GPIO_PIN_14:
236+
SD_detect_gpio_pin = GPIO_PIN_14;
237+
break;
238+
case LL_GPIO_PIN_15:
239+
SD_detect_gpio_pin = GPIO_PIN_15;
240+
break;
241+
default:
242+
Error_Handler();
243+
break;
233244
}
234-
}
235-
if (sd_state == MSD_OK) {
236-
/* Enable and set SD detect EXTI Interrupt to the lowest priority */
237-
HAL_NVIC_SetPriority(sd_detect_EXTI_IRQn, 0x0F, 0x00);
238-
HAL_NVIC_EnableIRQ(sd_detect_EXTI_IRQn);
245+
stm32_interrupt_enable(SD_detect_gpio_port, SD_detect_gpio_pin, callback, GPIO_MODE_IT_RISING_FALLING);
246+
sd_state = MSD_OK;
239247
}
240248
return sd_state;
241249
}
@@ -246,13 +254,11 @@ uint8_t BSP_SD_DetectITConfig(void)
246254
*/
247255
uint8_t BSP_SD_IsDetected(void)
248256
{
249-
uint8_t status = SD_PRESENT;
250-
257+
uint8_t status = SD_NOT_PRESENT;
251258
/* Check SD card detect pin */
252-
if (HAL_GPIO_ReadPin(SD_detect_gpio_port, SD_detect_gpio_pin) == GPIO_PIN_SET) {
253-
status = SD_NOT_PRESENT;
259+
if (!LL_GPIO_IsInputPinSet(SD_detect_gpio_port, SD_detect_ll_gpio_pin)) {
260+
status = SD_PRESENT;
254261
}
255-
256262
return status;
257263
}
258264

@@ -371,14 +377,15 @@ __weak void BSP_SD_Detect_MspInit(SD_HandleTypeDef *hsd, void *Params)
371377
{
372378
UNUSED(hsd);
373379
UNUSED(Params);
374-
GPIO_InitTypeDef gpio_init_structure;
375380

376381
/* GPIO configuration in input for uSD_Detect signal */
377-
gpio_init_structure.Pin = SD_detect_gpio_pin;
378-
gpio_init_structure.Mode = GPIO_MODE_INPUT;
379-
gpio_init_structure.Pull = GPIO_PULLUP;
380-
gpio_init_structure.Speed = SD_GPIO_SPEED;
381-
HAL_GPIO_Init(SD_detect_gpio_port, &gpio_init_structure);
382+
#ifdef LL_GPIO_SPEED_FREQ_VERY_HIGH
383+
LL_GPIO_SetPinSpeed(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_SPEED_FREQ_VERY_HIGH);
384+
#else
385+
LL_GPIO_SetPinSpeed(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_SPEED_FREQ_HIGH);
386+
#endif
387+
LL_GPIO_SetPinMode(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_MODE_INPUT);
388+
LL_GPIO_SetPinPull(SD_detect_gpio_port, SD_detect_ll_gpio_pin, LL_GPIO_PULL_UP);
382389
}
383390

384391
/**

src/bsp_sd.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Please update the core or install previous libray version."
8181
uint8_t BSP_SD_Init(void);
8282
uint8_t BSP_SD_DeInit(void);
8383
uint8_t BSP_SD_DetectPin(GPIO_TypeDef *port, uint32_t pin);
84-
uint8_t BSP_SD_DetectITConfig(void);
84+
uint8_t BSP_SD_DetectITConfig(void (*callback)(void));
8585
#ifndef STM32L1xx
8686
uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout);
8787
uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout);

0 commit comments

Comments
 (0)