1 /**
2 ******************************************************************************
3 * @file stm32h5xx_ll_gpio.c
4 * @author MCD Application Team
5 * @brief GPIO LL module driver.
6 ******************************************************************************
7 * @attention
8 *
9 * Copyright (c) 2022 STMicroelectronics.
10 * All rights reserved.
11 *
12 * This software is licensed under terms that can be found in the LICENSE file
13 * in the root directory of this software component.
14 * If no LICENSE file comes with this software, it is provided AS-IS.
15 *
16 ******************************************************************************
17 */
18 #if defined(USE_FULL_LL_DRIVER)
19
20 /* Includes ------------------------------------------------------------------*/
21 #include "stm32h5xx_ll_gpio.h"
22 #include "stm32h5xx_ll_bus.h"
23 #ifdef USE_FULL_ASSERT
24 #include "stm32_assert.h"
25 #else
26 #define assert_param(expr) ((void)0U)
27 #endif /* USE_FULL_ASSERT */
28
29 /** @addtogroup STM32H5xx_LL_Driver
30 * @{
31 */
32
33 #if defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || \
34 defined (GPIOG) || defined (GPIOH) || defined (GPIOI)
35
36 /** @addtogroup GPIO_LL
37 * @{
38 */
39
40 /* Private types -------------------------------------------------------------*/
41 /* Private variables ---------------------------------------------------------*/
42 /* Private constants ---------------------------------------------------------*/
43 /* Private macros ------------------------------------------------------------*/
44 /** @addtogroup GPIO_LL_Private_Macros
45 * @{
46 */
47 #define IS_LL_GPIO_PIN(__VALUE__) (((0x00000000U) < (__VALUE__)) && ((__VALUE__) <= (LL_GPIO_PIN_ALL)))
48
49 #define IS_LL_GPIO_MODE(__VALUE__) (((__VALUE__) == LL_GPIO_MODE_INPUT) ||\
50 ((__VALUE__) == LL_GPIO_MODE_OUTPUT) ||\
51 ((__VALUE__) == LL_GPIO_MODE_ALTERNATE) ||\
52 ((__VALUE__) == LL_GPIO_MODE_ANALOG))
53
54 #define IS_LL_GPIO_OUTPUT_TYPE(__VALUE__) (((__VALUE__) == LL_GPIO_OUTPUT_PUSHPULL) ||\
55 ((__VALUE__) == LL_GPIO_OUTPUT_OPENDRAIN))
56
57 #define IS_LL_GPIO_SPEED(__VALUE__) (((__VALUE__) == LL_GPIO_SPEED_FREQ_LOW) ||\
58 ((__VALUE__) == LL_GPIO_SPEED_FREQ_MEDIUM) ||\
59 ((__VALUE__) == LL_GPIO_SPEED_FREQ_HIGH) ||\
60 ((__VALUE__) == LL_GPIO_SPEED_FREQ_VERY_HIGH))
61
62 #define IS_LL_GPIO_PULL(__VALUE__) (((__VALUE__) == LL_GPIO_PULL_NO) ||\
63 ((__VALUE__) == LL_GPIO_PULL_UP) ||\
64 ((__VALUE__) == LL_GPIO_PULL_DOWN))
65
66 #define IS_LL_GPIO_ALTERNATE(__VALUE__) (((__VALUE__) == LL_GPIO_AF_0 ) ||\
67 ((__VALUE__) == LL_GPIO_AF_1 ) ||\
68 ((__VALUE__) == LL_GPIO_AF_2 ) ||\
69 ((__VALUE__) == LL_GPIO_AF_3 ) ||\
70 ((__VALUE__) == LL_GPIO_AF_4 ) ||\
71 ((__VALUE__) == LL_GPIO_AF_5 ) ||\
72 ((__VALUE__) == LL_GPIO_AF_6 ) ||\
73 ((__VALUE__) == LL_GPIO_AF_7 ) ||\
74 ((__VALUE__) == LL_GPIO_AF_8 ) ||\
75 ((__VALUE__) == LL_GPIO_AF_9 ) ||\
76 ((__VALUE__) == LL_GPIO_AF_10 ) ||\
77 ((__VALUE__) == LL_GPIO_AF_11 ) ||\
78 ((__VALUE__) == LL_GPIO_AF_12 ) ||\
79 ((__VALUE__) == LL_GPIO_AF_13 ) ||\
80 ((__VALUE__) == LL_GPIO_AF_14 ) ||\
81 ((__VALUE__) == LL_GPIO_AF_15 ))
82 /**
83 * @}
84 */
85
86 /* Private function prototypes -----------------------------------------------*/
87
88 /* Exported functions --------------------------------------------------------*/
89 /** @addtogroup GPIO_LL_Exported_Functions
90 * @{
91 */
92
93 /** @addtogroup GPIO_LL_EF_Init
94 * @{
95 */
96
97 /**
98 * @brief De-initialize GPIO registers (Registers restored to their default values).
99 * @param GPIOx GPIO Port
100 * @retval An ErrorStatus enumeration value:
101 * - SUCCESS: GPIO registers are de-initialized
102 * - ERROR: Wrong GPIO Port
103 */
LL_GPIO_DeInit(GPIO_TypeDef * GPIOx)104 ErrorStatus LL_GPIO_DeInit(GPIO_TypeDef *GPIOx)
105 {
106 ErrorStatus status = SUCCESS;
107
108 /* Check the parameters */
109 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
110
111 /* Force and Release reset on clock of GPIOx Port */
112 if (GPIOx == GPIOA)
113 {
114 LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOA);
115 LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOA);
116 }
117 else if (GPIOx == GPIOB)
118 {
119 LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOB);
120 LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOB);
121 }
122 else if (GPIOx == GPIOC)
123 {
124 LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOC);
125 LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOC);
126 }
127 #if defined(GPIOD)
128 else if (GPIOx == GPIOD)
129 {
130 LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOD);
131 LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOD);
132 }
133 #endif /* GPIOD */
134 #if defined(GPIOE)
135 else if (GPIOx == GPIOE)
136 {
137 LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOE);
138 LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOE);
139 }
140 #endif /* GPIOE */
141 #if defined(GPIOF)
142 else if (GPIOx == GPIOF)
143 {
144 LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOF);
145 LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOF);
146 }
147 #endif /* GPIOF */
148 #if defined(GPIOG)
149 else if (GPIOx == GPIOG)
150 {
151 LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOG);
152 LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOG);
153 }
154 #endif /* GPIOG */
155 #if defined(GPIOH)
156 else if (GPIOx == GPIOH)
157 {
158 LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOH);
159 LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOH);
160 }
161 #endif /* GPIOH */
162 #if defined(GPIOI)
163 else if (GPIOx == GPIOI)
164 {
165 LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOI);
166 LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOI);
167 }
168 #endif /* GPIOI */
169 else
170 {
171 status = ERROR;
172 }
173
174 return (status);
175 }
176
177 /**
178 * @brief Initialize GPIO registers according to the specified parameters in GPIO_InitStruct.
179 * @param GPIOx GPIO Port
180 * @param GPIO_InitStruct: pointer to a @ref LL_GPIO_InitTypeDef structure
181 * that contains the configuration information for the specified GPIO peripheral.
182 * @retval An ErrorStatus enumeration value:
183 * - SUCCESS: GPIO registers are initialized according to GPIO_InitStruct content
184 * - ERROR: Not applicable
185 */
LL_GPIO_Init(GPIO_TypeDef * GPIOx,LL_GPIO_InitTypeDef * GPIO_InitStruct)186 ErrorStatus LL_GPIO_Init(GPIO_TypeDef *GPIOx, LL_GPIO_InitTypeDef *GPIO_InitStruct)
187 {
188 uint32_t pinpos;
189 uint32_t currentpin;
190
191 /* Check the parameters */
192 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
193 assert_param(IS_LL_GPIO_PIN(GPIO_InitStruct->Pin));
194 assert_param(IS_LL_GPIO_MODE(GPIO_InitStruct->Mode));
195 assert_param(IS_LL_GPIO_PULL(GPIO_InitStruct->Pull));
196
197 /* ------------------------- Configure the port pins ---------------- */
198 /* Initialize pinpos on first pin set */
199 pinpos = POSITION_VAL(GPIO_InitStruct->Pin);
200
201 /* Configure the port pins */
202 while (((GPIO_InitStruct->Pin) >> pinpos) != 0U)
203 {
204 /* Get current io position */
205 currentpin = (GPIO_InitStruct->Pin) & (1UL << pinpos);
206
207 if (currentpin != 0U)
208 {
209 if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
210 {
211 /* Check Speed mode parameters */
212 assert_param(IS_LL_GPIO_SPEED(GPIO_InitStruct->Speed));
213
214 /* Speed mode configuration */
215 LL_GPIO_SetPinSpeed(GPIOx, currentpin, GPIO_InitStruct->Speed);
216
217 /* Check Output mode parameters */
218 assert_param(IS_LL_GPIO_OUTPUT_TYPE(GPIO_InitStruct->OutputType));
219
220 /* Output mode configuration*/
221 LL_GPIO_SetPinOutputType(GPIOx, GPIO_InitStruct->Pin, GPIO_InitStruct->OutputType);
222 }
223
224 /* Pull-up Pull down resistor configuration*/
225 LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
226
227 if (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE)
228 {
229 /* Check Alternate parameter */
230 assert_param(IS_LL_GPIO_ALTERNATE(GPIO_InitStruct->Alternate));
231
232 /* Speed mode configuration */
233 if (POSITION_VAL(currentpin) < 8U)
234 {
235 LL_GPIO_SetAFPin_0_7(GPIOx, currentpin, GPIO_InitStruct->Alternate);
236 }
237 else
238 {
239 LL_GPIO_SetAFPin_8_15(GPIOx, currentpin, GPIO_InitStruct->Alternate);
240 }
241 }
242 /* Pin Mode configuration */
243 LL_GPIO_SetPinMode(GPIOx, currentpin, GPIO_InitStruct->Mode);
244 }
245 pinpos++;
246 }
247
248 return (SUCCESS);
249 }
250
251 /**
252 * @brief Set each @ref LL_GPIO_InitTypeDef field to default value.
253 * @param GPIO_InitStruct: pointer to a @ref LL_GPIO_InitTypeDef structure
254 * whose fields will be set to default values.
255 * @retval None
256 */
257
LL_GPIO_StructInit(LL_GPIO_InitTypeDef * GPIO_InitStruct)258 void LL_GPIO_StructInit(LL_GPIO_InitTypeDef *GPIO_InitStruct)
259 {
260 /* Reset GPIO init structure parameters values */
261 GPIO_InitStruct->Pin = LL_GPIO_PIN_ALL;
262 GPIO_InitStruct->Mode = LL_GPIO_MODE_ANALOG;
263 GPIO_InitStruct->Speed = LL_GPIO_SPEED_FREQ_LOW;
264 GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
265 GPIO_InitStruct->Pull = LL_GPIO_PULL_NO;
266 GPIO_InitStruct->Alternate = LL_GPIO_AF_0;
267 }
268
269 /**
270 * @}
271 */
272
273 /**
274 * @}
275 */
276
277 /**
278 * @}
279 */
280
281 #endif /* defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || \
282 defined (GPIOF) || defined (GPIOG) || defined (GPIOH) || defined (GPIOI) */
283
284 /**
285 * @}
286 */
287
288 #endif /* USE_FULL_LL_DRIVER */
289