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