1 /**
2   ******************************************************************************
3   * @file    stm32wb0x_ll_gpio.c
4   * @author  MCD Application Team
5   * @brief   GPIO LL module driver.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2024 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 "stm32wb0x_ll_gpio.h"
22 #include "stm32wb0x_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 STM32WB0x_LL_Driver
30   * @{
31   */
32 
33 #if defined (GPIOA) || defined (GPIOB)
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 #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 /**
79   * @}
80   */
81 
82 /* Private function prototypes -----------------------------------------------*/
83 
84 /* Exported functions --------------------------------------------------------*/
85 /** @addtogroup GPIO_LL_Exported_Functions
86   * @{
87   */
88 
89 /** @addtogroup GPIO_LL_EF_Init
90   * @{
91   */
92 
93 /**
94   * @brief  De-initialize GPIO registers (Registers restored to their default values).
95   * @param  GPIOx GPIO Port
96   * @retval An ErrorStatus enumeration value:
97   *          - SUCCESS: GPIO registers are de-initialized
98   *          - ERROR:   Wrong GPIO Port
99   */
LL_GPIO_DeInit(GPIO_TypeDef * GPIOx)100 ErrorStatus LL_GPIO_DeInit(GPIO_TypeDef *GPIOx)
101 {
102   ErrorStatus status = SUCCESS;
103 
104   /* Check the parameters */
105   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
106 
107   /* Force and Release reset on clock of GPIOx Port */
108   if (GPIOx == GPIOA)
109   {
110     LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_GPIOA);
111     LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_GPIOA);
112   }
113   else if (GPIOx == GPIOB)
114   {
115     LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_GPIOB);
116     LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_GPIOB);
117   }
118   else
119   {
120     status = ERROR;
121   }
122 
123   return (status);
124 }
125 
126 /**
127   * @brief  Initialize GPIO registers according to the specified parameters in GPIO_InitStruct.
128   * @note   The application must ensure that the PWRC_CR1_APC bit is reset via LL PWRC services.
129   * @param  GPIOx GPIO Port
130   * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
131   *         that contains the configuration information for the specified GPIO peripheral.
132   * @retval An ErrorStatus enumeration value:
133   *          - SUCCESS: GPIO registers are initialized according to GPIO_InitStruct content
134   *          - ERROR:   Not applicable
135   */
LL_GPIO_Init(GPIO_TypeDef * GPIOx,LL_GPIO_InitTypeDef * GPIO_InitStruct)136 ErrorStatus LL_GPIO_Init(GPIO_TypeDef *GPIOx, LL_GPIO_InitTypeDef *GPIO_InitStruct)
137 {
138   uint32_t pinpos;
139   uint32_t currentpin;
140 
141   /* Check the parameters */
142   assert_param(IS_GPIO_ALL_INSTANCE(GPIOx));
143   assert_param(IS_LL_GPIO_PIN(GPIO_InitStruct->Pin));
144   assert_param(IS_LL_GPIO_MODE(GPIO_InitStruct->Mode));
145   assert_param(IS_LL_GPIO_PULL(GPIO_InitStruct->Pull));
146 
147   /* ------------------------- Configure the port pins ---------------- */
148   /* Initialize  pinpos on first pin set */
149   pinpos = 0;
150 
151   /* Configure the port pins */
152   while (((GPIO_InitStruct->Pin) >> pinpos) != 0x00u)
153   {
154     /* Get current io position */
155     currentpin = (GPIO_InitStruct->Pin) & (0x00000001uL << pinpos);
156 
157     if (currentpin != 0x00u)
158     {
159       if ((GPIO_InitStruct->Mode == LL_GPIO_MODE_OUTPUT) || (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE))
160       {
161         /* Check Speed mode parameters */
162         assert_param(IS_LL_GPIO_SPEED(GPIO_InitStruct->Speed));
163 
164         /* Speed mode configuration */
165         LL_GPIO_SetPinSpeed(GPIOx, currentpin, GPIO_InitStruct->Speed);
166 
167         /* Check Output mode parameters */
168         assert_param(IS_LL_GPIO_OUTPUT_TYPE(GPIO_InitStruct->OutputType));
169 
170         /* Output mode configuration*/
171         LL_GPIO_SetPinOutputType(GPIOx, currentpin, GPIO_InitStruct->OutputType);
172       }
173 
174       /* Pull-up Pull down resistor configuration*/
175       LL_GPIO_SetPinPull(GPIOx, currentpin, GPIO_InitStruct->Pull);
176 
177       if (GPIO_InitStruct->Mode == LL_GPIO_MODE_ALTERNATE)
178       {
179         /* Check Alternate parameter */
180         assert_param(IS_LL_GPIO_ALTERNATE(GPIO_InitStruct->Alternate));
181 
182         /* Speed mode configuration */
183         if (currentpin < LL_GPIO_PIN_8)
184         {
185           LL_GPIO_SetAFPin_0_7(GPIOx, currentpin, GPIO_InitStruct->Alternate);
186         }
187         else
188         {
189           LL_GPIO_SetAFPin_8_15(GPIOx, currentpin, GPIO_InitStruct->Alternate);
190         }
191       }
192 
193       /* Pin Mode configuration */
194       LL_GPIO_SetPinMode(GPIOx, currentpin, GPIO_InitStruct->Mode);
195     }
196     pinpos++;
197   }
198 
199   return (SUCCESS);
200 }
201 
202 /**
203   * @brief Set each @ref LL_GPIO_InitTypeDef field to default value.
204   * @param GPIO_InitStruct pointer to a @ref LL_GPIO_InitTypeDef structure
205   *                          whose fields will be set to default values.
206   * @retval None
207   */
208 
LL_GPIO_StructInit(LL_GPIO_InitTypeDef * GPIO_InitStruct)209 void LL_GPIO_StructInit(LL_GPIO_InitTypeDef *GPIO_InitStruct)
210 {
211   /* Reset GPIO init structure parameters values */
212   GPIO_InitStruct->Pin        = LL_GPIO_PIN_ALL;
213   GPIO_InitStruct->Mode       = LL_GPIO_MODE_ANALOG;
214   GPIO_InitStruct->Speed      = LL_GPIO_SPEED_FREQ_LOW;
215   GPIO_InitStruct->OutputType = LL_GPIO_OUTPUT_PUSHPULL;
216   GPIO_InitStruct->Pull       = LL_GPIO_PULL_NO;
217   GPIO_InitStruct->Alternate  = LL_GPIO_AF_0;
218 }
219 
220 /**
221   * @}
222   */
223 
224 /**
225   * @}
226   */
227 
228 /**
229   * @}
230   */
231 
232 #endif /* defined (GPIOA) || defined (GPIOB) */
233 
234 /**
235   * @}
236   */
237 
238 #endif /* USE_FULL_LL_DRIVER */
239