1 /**
2 ******************************************************************************
3 * @file stm32n6xx_ll_gpio.c
4 * @author GPM Application Team
5 * @brief GPIO LL module driver.
6 ******************************************************************************
7 * @attention
8 *
9 * Copyright (c) 2023 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 "stm32n6xx_ll_gpio.h"
22 #include "stm32n6xx_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 STM32N6xx_LL_Driver
30 * @{
31 */
32
33 #if defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG) || defined (GPIOH) || defined (GPION) || defined (GPIOO) || defined (GPIOP) || defined (GPIOQ)
34 /** @addtogroup GPIO_LL
35 * @{
36 */
37 /** MISRA C:2012 deviation rule has been granted for following rules:
38 * Rule-12.2 - Medium: RHS argument is in interval [0,INF] which is out of
39 * range of the shift operator in following API :
40 * LL_GPIO_Init
41 */
42
43 /* Private types -------------------------------------------------------------*/
44 /* Private variables ---------------------------------------------------------*/
45 /* Private constants ---------------------------------------------------------*/
46 /* Private macros ------------------------------------------------------------*/
47 /** @addtogroup GPIO_LL_Private_Macros
48 * @{
49 */
50 #define IS_LL_GPIO_PIN(__VALUE__) (((0x00u) < (__VALUE__)) && ((__VALUE__) <= (LL_GPIO_PIN_ALL)))
51
52 #define IS_LL_GPIO_MODE(__VALUE__) (((__VALUE__) == LL_GPIO_MODE_INPUT) ||\
53 ((__VALUE__) == LL_GPIO_MODE_OUTPUT) ||\
54 ((__VALUE__) == LL_GPIO_MODE_ALTERNATE) ||\
55 ((__VALUE__) == LL_GPIO_MODE_ANALOG))
56
57 #define IS_LL_GPIO_OUTPUT_TYPE(__VALUE__) (((__VALUE__) == LL_GPIO_OUTPUT_PUSHPULL) ||\
58 ((__VALUE__) == LL_GPIO_OUTPUT_OPENDRAIN))
59
60 #define IS_LL_GPIO_SPEED(__VALUE__) (((__VALUE__) == LL_GPIO_SPEED_FREQ_LOW) ||\
61 ((__VALUE__) == LL_GPIO_SPEED_FREQ_MEDIUM) ||\
62 ((__VALUE__) == LL_GPIO_SPEED_FREQ_HIGH) ||\
63 ((__VALUE__) == LL_GPIO_SPEED_FREQ_VERY_HIGH))
64
65 #define IS_LL_GPIO_PULL(__VALUE__) (((__VALUE__) == LL_GPIO_PULL_NO) ||\
66 ((__VALUE__) == LL_GPIO_PULL_UP) ||\
67 ((__VALUE__) == LL_GPIO_PULL_DOWN))
68
69 #define IS_LL_GPIO_ALTERNATE(__VALUE__) (((__VALUE__) == LL_GPIO_AF_0 ) ||\
70 ((__VALUE__) == LL_GPIO_AF_1 ) ||\
71 ((__VALUE__) == LL_GPIO_AF_2 ) ||\
72 ((__VALUE__) == LL_GPIO_AF_3 ) ||\
73 ((__VALUE__) == LL_GPIO_AF_4 ) ||\
74 ((__VALUE__) == LL_GPIO_AF_5 ) ||\
75 ((__VALUE__) == LL_GPIO_AF_6 ) ||\
76 ((__VALUE__) == LL_GPIO_AF_7 ) ||\
77 ((__VALUE__) == LL_GPIO_AF_8 ) ||\
78 ((__VALUE__) == LL_GPIO_AF_9 ) ||\
79 ((__VALUE__) == LL_GPIO_AF_10 ) ||\
80 ((__VALUE__) == LL_GPIO_AF_11 ) ||\
81 ((__VALUE__) == LL_GPIO_AF_12 ) ||\
82 ((__VALUE__) == LL_GPIO_AF_13 ) ||\
83 ((__VALUE__) == LL_GPIO_AF_14 ) ||\
84 ((__VALUE__) == LL_GPIO_AF_15 ))
85 /**
86 * @}
87 */
88
89 /* Private function prototypes -----------------------------------------------*/
90
91 /* Exported functions --------------------------------------------------------*/
92 /** @addtogroup GPIO_LL_Exported_Functions
93 * @{
94 */
95
96 /** @addtogroup GPIO_LL_EF_Init
97 * @{
98 */
99
100 /**
101 * @brief De-initialize GPIO registers (Registers restored to their default values).
102 * @param GPIOx GPIO Port
103 * @retval An ErrorStatus enumeration value:
104 * - SUCCESS: GPIO registers are de-initialized
105 * - ERROR: Wrong GPIO Port
106 */
LL_GPIO_DeInit(const GPIO_TypeDef * GPIOx)107 ErrorStatus LL_GPIO_DeInit(const GPIO_TypeDef *GPIOx)
108 {
109 ErrorStatus status = SUCCESS;
110
111 /* Check the parameters */
112 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
113
114 /* Force and Release reset on clock of GPIOx Port */
115 if (GPIOx == GPIOA)
116 {
117 LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOA);
118 LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOA);
119 }
120 else if (GPIOx == GPIOB)
121 {
122 LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOB);
123 LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOB);
124 }
125 else if (GPIOx == GPIOC)
126 {
127 LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOC);
128 LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOC);
129 }
130 else if (GPIOx == GPIOD)
131 {
132 LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOD);
133 LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOD);
134 }
135 else if (GPIOx == GPIOE)
136 {
137 LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOE);
138 LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOE);
139 }
140 else if (GPIOx == GPIOF)
141 {
142 LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOF);
143 LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOF);
144 }
145 else if (GPIOx == GPIOG)
146 {
147 LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOG);
148 LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOG);
149 }
150 else if (GPIOx == GPIOH)
151 {
152 LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOH);
153 LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOH);
154 }
155 else if (GPIOx == GPION)
156 {
157 LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPION);
158 LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPION);
159 }
160 else if (GPIOx == GPIOO)
161 {
162 LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOO);
163 LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOO);
164 }
165 else if (GPIOx == GPIOP)
166 {
167 LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOP);
168 LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOP);
169 }
170 else if (GPIOx == GPIOQ)
171 {
172 LL_AHB4_GRP1_ForceReset(LL_AHB4_GRP1_PERIPH_GPIOQ);
173 LL_AHB4_GRP1_ReleaseReset(LL_AHB4_GRP1_PERIPH_GPIOQ);
174 }
175 else
176 {
177 status = ERROR;
178 }
179
180 return (status);
181 }
182
183 /**
184 * @brief Initialize GPIO registers according to the specified parameters in GPIO_InitStruct.
185 * @param GPIOx GPIO Port
186 * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
187 * that contains the configuration information for the specified GPIO peripheral.
188 * @retval An ErrorStatus enumeration value:
189 * - SUCCESS: GPIO registers are initialized according to GPIO_InitStruct content
190 * - ERROR: Not applicable
191 */
LL_GPIO_Init(GPIO_TypeDef * GPIOx,const LL_GPIO_InitTypeDef * GPIO_InitStruct)192 ErrorStatus LL_GPIO_Init(GPIO_TypeDef *GPIOx, const LL_GPIO_InitTypeDef *GPIO_InitStruct)
193 {
194 uint32_t pinpos;
195 uint32_t currentpin;
196
197 /* Check the parameters */
198 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
199 assert_param(IS_LL_GPIO_PIN(GPIO_InitStruct->Pin));
200 assert_param(IS_LL_GPIO_MODE(GPIO_InitStruct->Mode));
201 assert_param(IS_LL_GPIO_PULL(GPIO_InitStruct->Pull));
202
203 /* ------------------------- Configure the port pins ---------------- */
204 /* Initialize pinpos on first pin set */
205 pinpos = 0;
206
207 /* Configure the port pins */
208 while (((GPIO_InitStruct->Pin) >> pinpos) != 0x00u)
209 {
210 /* Get current io position */
211 currentpin = (GPIO_InitStruct->Pin) & (0x00000001uL << pinpos);
212
213 if (currentpin != 0x00u)
214 {
215 if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
216 {
217 /* Check Speed mode parameters */
218 assert_param(IS_LL_GPIO_SPEED(GPIO_InitStruct->Speed));
219
220 /* Speed mode configuration */
221 LL_GPIO_SetPinSpeed(GPIOx, currentpin, GPIO_InitStruct->Speed);
222
223 /* Check Output mode parameters */
224 assert_param(IS_LL_GPIO_OUTPUT_TYPE(GPIO_InitStruct->OutputType));
225
226 /* Output mode configuration*/
227 LL_GPIO_SetPinOutputType(GPIOx, currentpin, GPIO_InitStruct->OutputType);
228 }
229
230 /* Pull-up Pull down resistor configuration*/
231 LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
232
233 if (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE)
234 {
235 /* Check Alternate parameter */
236 assert_param(IS_LL_GPIO_ALTERNATE(GPIO_InitStruct->Alternate));
237
238 /* Speed mode configuration */
239 if (currentpin < LL_GPIO_PIN_8)
240 {
241 LL_GPIO_SetAFPin_0_7(GPIOx, currentpin, GPIO_InitStruct->Alternate);
242 }
243 else
244 {
245 LL_GPIO_SetAFPin_8_15(GPIOx, currentpin, GPIO_InitStruct->Alternate);
246 }
247 }
248
249 /* Pin Mode configuration */
250 LL_GPIO_SetPinMode(GPIOx, currentpin, GPIO_InitStruct->Mode);
251 }
252 pinpos++;
253 }
254
255 return (SUCCESS);
256 }
257
258 /**
259 * @brief Set each @ref LL_GPIO_InitTypeDef field to default value.
260 * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
261 * whose fields will be set to default values.
262 * @retval None
263 */
264
LL_GPIO_StructInit(LL_GPIO_InitTypeDef * GPIO_InitStruct)265 void LL_GPIO_StructInit(LL_GPIO_InitTypeDef *GPIO_InitStruct)
266 {
267 /* Reset GPIO init structure parameters values */
268 GPIO_InitStruct->Pin = LL_GPIO_PIN_ALL;
269 GPIO_InitStruct->Mode = LL_GPIO_MODE_ANALOG;
270 GPIO_InitStruct->Speed = LL_GPIO_SPEED_FREQ_LOW;
271 GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
272 GPIO_InitStruct->Pull = LL_GPIO_PULL_NO;
273 GPIO_InitStruct->Alternate = LL_GPIO_AF_0;
274 }
275
276 /**
277 * @}
278 */
279
280 /**
281 * @}
282 */
283
284 /**
285 * @}
286 */
287
288 #endif /* defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) || defined (GPIOG) || defined (GPIOH) || defined (GPION) || defined (GPIOO) || defined (GPIOP) || defined (GPIOQ) */
289
290 /**
291 * @}
292 */
293
294 #endif /* USE_FULL_LL_DRIVER */
295