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