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