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