1 /**
2   ******************************************************************************
3   * @file    stm32g0xx_ll_gpio.c
4   * @author  MCD Application Team
5   * @brief   GPIO LL module driver.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2018 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 "stm32g0xx_ll_gpio.h"
22 #include "stm32g0xx_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 STM32G0xx_LL_Driver
30   * @{
31   */
32 
33 #if defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF)
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 #if defined(GPIOE)
71 #define IS_LL_GPIO_ALTERNATE(__VALUE__)    (((__VALUE__) == LL_GPIO_AF_0  )   ||\
72                                             ((__VALUE__) == LL_GPIO_AF_1  )   ||\
73                                             ((__VALUE__) == LL_GPIO_AF_2  )   ||\
74                                             ((__VALUE__) == LL_GPIO_AF_3  )   ||\
75                                             ((__VALUE__) == LL_GPIO_AF_4  )   ||\
76                                             ((__VALUE__) == LL_GPIO_AF_5  )   ||\
77                                             ((__VALUE__) == LL_GPIO_AF_6  )   ||\
78                                             ((__VALUE__) == LL_GPIO_AF_7  )   ||\
79                                             ((__VALUE__) == LL_GPIO_AF_8  )   ||\
80                                             ((__VALUE__) == LL_GPIO_AF_9  )   ||\
81                                             ((__VALUE__) == LL_GPIO_AF_10 ))
82 #else
83 #define IS_LL_GPIO_ALTERNATE(__VALUE__)    (((__VALUE__) == LL_GPIO_AF_0  )   ||\
84                                             ((__VALUE__) == LL_GPIO_AF_1  )   ||\
85                                             ((__VALUE__) == LL_GPIO_AF_2  )   ||\
86                                             ((__VALUE__) == LL_GPIO_AF_3  )   ||\
87                                             ((__VALUE__) == LL_GPIO_AF_4  )   ||\
88                                             ((__VALUE__) == LL_GPIO_AF_5  )   ||\
89                                             ((__VALUE__) == LL_GPIO_AF_6  )   ||\
90                                             ((__VALUE__) == LL_GPIO_AF_7 ))
91 #endif /* GPIOE */
92 /**
93   * @}
94   */
95 
96 /* Private function prototypes -----------------------------------------------*/
97 
98 /* Exported functions --------------------------------------------------------*/
99 /** @addtogroup GPIO_LL_Exported_Functions
100   * @{
101   */
102 
103 /** @addtogroup GPIO_LL_EF_Init
104   * @{
105   */
106 
107 /**
108   * @brief  De-initialize GPIO registers (Registers restored to their default values).
109   * @param  GPIOx GPIO Port
110   * @retval An ErrorStatus enumeration value:
111   *          - SUCCESS: GPIO registers are de-initialized
112   *          - ERROR:   Wrong GPIO Port
113   */
LL_GPIO_DeInit(GPIO_TypeDef * GPIOx)114 ErrorStatus LL_GPIO_DeInit(GPIO_TypeDef *GPIOx)
115 {
116   ErrorStatus status = SUCCESS;
117 
118   /* Check the parameters */
119   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
120 
121   /* Force and Release reset on clock of GPIOx Port */
122   if (GPIOx == GPIOA)
123   {
124     LL_IOP_GRP1_ForceReset(LL_IOP_GRP1_PERIPH_GPIOA);
125     LL_IOP_GRP1_ReleaseReset(LL_IOP_GRP1_PERIPH_GPIOA);
126   }
127   else if (GPIOx == GPIOB)
128   {
129     LL_IOP_GRP1_ForceReset(LL_IOP_GRP1_PERIPH_GPIOB);
130     LL_IOP_GRP1_ReleaseReset(LL_IOP_GRP1_PERIPH_GPIOB);
131   }
132   else if (GPIOx == GPIOC)
133   {
134     LL_IOP_GRP1_ForceReset(LL_IOP_GRP1_PERIPH_GPIOC);
135     LL_IOP_GRP1_ReleaseReset(LL_IOP_GRP1_PERIPH_GPIOC);
136   }
137 #if defined(GPIOD)
138   else if (GPIOx == GPIOD)
139   {
140     LL_IOP_GRP1_ForceReset(LL_IOP_GRP1_PERIPH_GPIOD);
141     LL_IOP_GRP1_ReleaseReset(LL_IOP_GRP1_PERIPH_GPIOD);
142   }
143 #endif /* GPIOD */
144 #if defined(GPIOE)
145   else if (GPIOx == GPIOE)
146   {
147     LL_IOP_GRP1_ForceReset(LL_IOP_GRP1_PERIPH_GPIOE);
148     LL_IOP_GRP1_ReleaseReset(LL_IOP_GRP1_PERIPH_GPIOE);
149   }
150 #endif /* GPIOE */
151 #if defined(GPIOF)
152   else if (GPIOx == GPIOF)
153   {
154     LL_IOP_GRP1_ForceReset(LL_IOP_GRP1_PERIPH_GPIOF);
155     LL_IOP_GRP1_ReleaseReset(LL_IOP_GRP1_PERIPH_GPIOF);
156   }
157 #endif /* GPIOF */
158   else
159   {
160     status = ERROR;
161   }
162 
163   return (status);
164 }
165 
166 /**
167   * @brief  Initialize GPIO registers according to the specified parameters in GPIO_InitStruct.
168   * @param  GPIOx GPIO Port
169   * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
170   *         that contains the configuration information for the specified GPIO peripheral.
171   * @retval An ErrorStatus enumeration value:
172   *          - SUCCESS: GPIO registers are initialized according to GPIO_InitStruct content
173   *          - ERROR:   Not applicable
174   */
LL_GPIO_Init(GPIO_TypeDef * GPIOx,LL_GPIO_InitTypeDef * GPIO_InitStruct)175 ErrorStatus LL_GPIO_Init(GPIO_TypeDef *GPIOx, LL_GPIO_InitTypeDef *GPIO_InitStruct)
176 {
177   uint32_t pinpos;
178   uint32_t currentpin;
179 
180   /* Check the parameters */
181   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
182   assert_param(IS_LL_GPIO_PIN(GPIO_InitStruct->Pin));
183   assert_param(IS_LL_GPIO_MODE(GPIO_InitStruct->Mode));
184   assert_param(IS_LL_GPIO_PULL(GPIO_InitStruct->Pull));
185 
186   /* ------------------------- Configure the port pins ---------------- */
187   /* Initialize  pinpos on first pin set */
188   pinpos = 0;
189 
190   /* Configure the port pins */
191   while (((GPIO_InitStruct->Pin) >> pinpos) != 0x00u)
192   {
193     /* Get current io position */
194     currentpin = (GPIO_InitStruct->Pin) & (0x00000001uL << pinpos);
195 
196     if (currentpin != 0x00u)
197     {
198       if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
199       {
200         /* Check Speed mode parameters */
201         assert_param(IS_LL_GPIO_SPEED(GPIO_InitStruct->Speed));
202 
203         /* Speed mode configuration */
204         LL_GPIO_SetPinSpeed(GPIOx, currentpin, GPIO_InitStruct->Speed);
205 
206         /* Check Output mode parameters */
207         assert_param(IS_LL_GPIO_OUTPUT_TYPE(GPIO_InitStruct->OutputType));
208 
209         /* Output mode configuration*/
210         LL_GPIO_SetPinOutputType(GPIOx, currentpin, GPIO_InitStruct->OutputType);
211       }
212 
213       /* Pull-up Pull down resistor configuration*/
214       LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
215 
216       if (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE)
217       {
218         /* Check Alternate parameter */
219         assert_param(IS_LL_GPIO_ALTERNATE(GPIO_InitStruct->Alternate));
220 
221         /* Speed mode configuration */
222         if (currentpin < LL_GPIO_PIN_8)
223         {
224           LL_GPIO_SetAFPin_0_7(GPIOx, currentpin, GPIO_InitStruct->Alternate);
225         }
226         else
227         {
228           LL_GPIO_SetAFPin_8_15(GPIOx, currentpin, GPIO_InitStruct->Alternate);
229         }
230       }
231 
232       /* Pin Mode configuration */
233       LL_GPIO_SetPinMode(GPIOx, currentpin, GPIO_InitStruct->Mode);
234     }
235     pinpos++;
236   }
237 
238   return (SUCCESS);
239 }
240 
241 /**
242   * @brief Set each @ref LL_GPIO_InitTypeDef field to default value.
243   * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
244   *                          whose fields will be set to default values.
245   * @retval None
246   */
247 
LL_GPIO_StructInit(LL_GPIO_InitTypeDef * GPIO_InitStruct)248 void LL_GPIO_StructInit(LL_GPIO_InitTypeDef *GPIO_InitStruct)
249 {
250   /* Reset GPIO init structure parameters values */
251   GPIO_InitStruct->Pin        = LL_GPIO_PIN_ALL;
252   GPIO_InitStruct->Mode       = LL_GPIO_MODE_ANALOG;
253   GPIO_InitStruct->Speed      = LL_GPIO_SPEED_FREQ_LOW;
254   GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
255   GPIO_InitStruct->Pull       = LL_GPIO_PULL_NO;
256   GPIO_InitStruct->Alternate  = LL_GPIO_AF_0;
257 }
258 
259 /**
260   * @}
261   */
262 
263 /**
264   * @}
265   */
266 
267 /**
268   * @}
269   */
270 
271 #endif /* defined (GPIOA) || defined (GPIOB) || defined (GPIOC) || defined (GPIOD) || defined (GPIOE) || defined (GPIOF) */
272 
273 /**
274   * @}
275   */
276 
277 #endif /* USE_FULL_LL_DRIVER */
278 
279