1 /**
2 ******************************************************************************
3 * @file stm32wbaxx_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 "stm32wbaxx_ll_gpio.h"
22 #include "stm32wbaxx_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 STM32WBAxx_LL_Driver
30 * @{
31 */
32
33 #if defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || 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
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_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOA);
118 LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOA);
119 }
120 else if (GPIOx == GPIOB)
121 {
122 LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOB);
123 LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOB);
124 }
125 else if (GPIOx == GPIOC)
126 {
127 LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOC);
128 LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOC);
129 }
130 else if (GPIOx == GPIOH)
131 {
132 LL_AHB2_GRP1_ForceReset(LL_AHB2_GRP1_PERIPH_GPIOH);
133 LL_AHB2_GRP1_ReleaseReset(LL_AHB2_GRP1_PERIPH_GPIOH);
134 }
135 else
136 {
137 status = ERROR;
138 }
139
140 return (status);
141 }
142
143 /**
144 * @brief Initialize GPIO registers according to the specified parameters in GPIO_InitStruct.
145 * @param GPIOx GPIO Port
146 * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
147 * that contains the configuration information for the specified GPIO peripheral.
148 * @retval An ErrorStatus enumeration value:
149 * - SUCCESS: GPIO registers are initialized according to GPIO_InitStruct content
150 * - ERROR: Not applicable
151 */
LL_GPIO_Init(GPIO_TypeDef * GPIOx,LL_GPIO_InitTypeDef * GPIO_InitStruct)152 ErrorStatus LL_GPIO_Init(GPIO_TypeDef *GPIOx, LL_GPIO_InitTypeDef *GPIO_InitStruct)
153 {
154 uint32_t pinpos;
155 uint32_t currentpin;
156
157 /* Check the parameters */
158 assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
159 assert_param(IS_LL_GPIO_PIN(GPIO_InitStruct->Pin));
160 assert_param(IS_LL_GPIO_MODE(GPIO_InitStruct->Mode));
161 assert_param(IS_LL_GPIO_PULL(GPIO_InitStruct->Pull));
162
163 /* ------------------------- Configure the port pins ---------------- */
164 /* Initialize pinpos on first pin set */
165 pinpos = POSITION_VAL(GPIO_InitStruct->Pin);
166
167 /* Configure the port pins */
168 while (((GPIO_InitStruct->Pin) >> pinpos) != 0x00u)
169 {
170 /* Get current io position */
171 currentpin = (GPIO_InitStruct->Pin) & (0x00000001uL << pinpos);
172
173 if (currentpin != 0x00u)
174 {
175 if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
176 {
177 /* Check Speed mode parameters */
178 assert_param(IS_LL_GPIO_SPEED(GPIO_InitStruct->Speed));
179
180 /* Speed mode configuration */
181 LL_GPIO_SetPinSpeed(GPIOx, currentpin, GPIO_InitStruct->Speed);
182
183 /* Check Output mode parameters */
184 assert_param(IS_LL_GPIO_OUTPUT_TYPE(GPIO_InitStruct->OutputType));
185
186 /* Output mode configuration*/
187 LL_GPIO_SetPinOutputType(GPIOx, currentpin, GPIO_InitStruct->OutputType);
188 }
189
190 /* Pull-up Pull down resistor configuration*/
191 LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
192
193 if (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE)
194 {
195 /* Check Alternate parameter */
196 assert_param(IS_LL_GPIO_ALTERNATE(GPIO_InitStruct->Alternate));
197
198 /* Speed mode configuration */
199 if (currentpin < LL_GPIO_PIN_8)
200 {
201 LL_GPIO_SetAFPin_0_7(GPIOx, currentpin, GPIO_InitStruct->Alternate);
202 }
203 else
204 {
205 LL_GPIO_SetAFPin_8_15(GPIOx, currentpin, GPIO_InitStruct->Alternate);
206 }
207 }
208
209 /* Pin Mode configuration */
210 LL_GPIO_SetPinMode(GPIOx, currentpin, GPIO_InitStruct->Mode);
211 }
212 pinpos++;
213 }
214
215 return (SUCCESS);
216 }
217
218 /**
219 * @brief Set each @ref LL_GPIO_InitTypeDef field to default value.
220 * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
221 * whose fields will be set to default values.
222 * @retval None
223 */
224
LL_GPIO_StructInit(LL_GPIO_InitTypeDef * GPIO_InitStruct)225 void LL_GPIO_StructInit(LL_GPIO_InitTypeDef *GPIO_InitStruct)
226 {
227 /* Reset GPIO init structure parameters values */
228 GPIO_InitStruct->Pin = LL_GPIO_PIN_ALL;
229 GPIO_InitStruct->Mode = LL_GPIO_MODE_ANALOG;
230 GPIO_InitStruct->Speed = LL_GPIO_SPEED_FREQ_LOW;
231 GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
232 GPIO_InitStruct->Pull = LL_GPIO_PULL_NO;
233 GPIO_InitStruct->Alternate = LL_GPIO_AF_0;
234 }
235
236 /**
237 * @}
238 */
239
240 /**
241 * @}
242 */
243
244 /**
245 * @}
246 */
247
248 #endif /* defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOH) */
249
250 /**
251 * @}
252 */
253
254 #endif /* USE_FULL_LL_DRIVER */
255
256