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