3838#include " interrupt.h"
3939#include " lock_resource.h"
4040#include " stm32yyxx_ll_exti.h"
41+ #include " stm32yyxx_ll_gpio.h"
42+
4143#if !defined(HAL_EXTI_MODULE_DISABLED)
4244
4345/* Private Types */
@@ -133,68 +135,24 @@ static uint8_t get_pin_id(uint16_t pin)
133135
134136 return id;
135137}
136- void stm32_interrupt_enable (GPIO_TypeDef *port, uint16_t pin, callback_function_t callback, uint32_t mode)
138+
139+ void stm32_interrupt_enable (PinName pn, callback_function_t callback, uint32_t mode)
137140{
138141 GPIO_InitTypeDef GPIO_InitStruct;
142+ uint16_t pin = STM_GPIO_PIN (pn);
139143 uint8_t id = get_pin_id (pin);
140-
141- #ifdef STM32F1xx
142- uint8_t position;
143- uint32_t CRxRegOffset = 0 ;
144- uint32_t ODRRegOffset = 0 ;
145- volatile uint32_t *CRxRegister;
146- const uint32_t ConfigMask = 0x00000008 ; // MODE0 == 0x0 && CNF0 == 0x2
147- #else
148- uint32_t pull;
149- #endif /* STM32F1xx */
150-
151- // GPIO pin configuration
152- GPIO_InitStruct.Pin = pin;
153- GPIO_InitStruct.Mode = mode;
154-
155- // read the pull mode directly in the register as no function exists to get it.
156- // Do it in case the user already defines the IO through the digital io
157- // interface
158- #ifndef STM32F1xx
159- pull = port->PUPDR ;
160- #ifdef GPIO_PUPDR_PUPD0
161- pull &= (GPIO_PUPDR_PUPD0 << (id * 2 ));
162- GPIO_InitStruct.Pull = (GPIO_PUPDR_PUPD0 & (pull >> (id * 2 )));
163- #else
164- pull &= (GPIO_PUPDR_PUPDR0 << (id * 2 ));
165- GPIO_InitStruct.Pull = (GPIO_PUPDR_PUPDR0 & (pull >> (id * 2 )));
166- #endif /* GPIO_PUPDR_PUPD0 */
167- #else
168- CRxRegister = (pin < GPIO_PIN_8) ? &port->CRL : &port->CRH ;
169-
170- for (position = 0 ; position < 16 ; position++) {
171- if (pin == (0x0001 << position)) {
172- CRxRegOffset = (pin < GPIO_PIN_8) ? (position << 2 ) : ((position - 8 ) << 2 );
173- ODRRegOffset = position;
174- }
144+ GPIO_TypeDef *port = set_GPIO_Port_Clock (STM_PORT (pn));
145+ if (port) {
146+ // GPIO pin configuration
147+ GPIO_InitStruct.Pin = pin;
148+ GPIO_InitStruct.Mode = mode;
149+ GPIO_InitStruct.Pull = LL_GPIO_GetPinPull (port, STM_LL_GPIO_PIN (pn));
150+ GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
151+ hsem_lock (CFG_HW_GPIO_SEMID, HSEM_LOCK_DEFAULT_RETRY);
152+ HAL_GPIO_Init (port, &GPIO_InitStruct);
153+ hsem_unlock (CFG_HW_GPIO_SEMID);
175154 }
176-
177- if ((*CRxRegister & ((GPIO_CRL_MODE0 | GPIO_CRL_CNF0) << CRxRegOffset)) == (ConfigMask << CRxRegOffset)) {
178- if ((port->ODR & (GPIO_ODR_ODR0 << ODRRegOffset)) == (GPIO_ODR_ODR0 << ODRRegOffset)) {
179- GPIO_InitStruct.Pull = GPIO_PULLUP;
180- } else {
181- GPIO_InitStruct.Pull = GPIO_PULLDOWN;
182- }
183- } else {
184- GPIO_InitStruct.Pull = GPIO_NOPULL;
185- }
186- #endif /* STM32F1xx */
187-
188- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
189-
190- hsem_lock (CFG_HW_GPIO_SEMID, HSEM_LOCK_DEFAULT_RETRY);
191-
192- HAL_GPIO_Init (port, &GPIO_InitStruct);
193-
194- hsem_unlock (CFG_HW_GPIO_SEMID);
195-
196155 gpio_irq_conf[id].callback = callback;
197-
198156 // Enable and set EXTI Interrupt
199157 HAL_NVIC_SetPriority (gpio_irq_conf[id].irqnb , EXTI_IRQ_PRIO, EXTI_IRQ_SUBPRIO);
200158 HAL_NVIC_EnableIRQ (gpio_irq_conf[id].irqnb );
@@ -208,10 +166,10 @@ void stm32_interrupt_enable(GPIO_TypeDef *port, uint16_t pin, callback_function_
208166 * @param mode : one of the supported interrupt mode defined in stm32_hal_gpio
209167 * @retval None
210168 */
211- void stm32_interrupt_enable (GPIO_TypeDef *port, uint16_t pin , void (*callback)(void ), uint32_t mode)
169+ void stm32_interrupt_enable (PinName pn , void (*callback)(void ), uint32_t mode)
212170{
213171 std::function<void (void )> _c = callback;
214- stm32_interrupt_enable (port, pin , _c, mode);
172+ stm32_interrupt_enable (pn , _c, mode);
215173
216174}
217175
@@ -226,14 +184,12 @@ void stm32_interrupt_disable(GPIO_TypeDef *port, uint16_t pin)
226184 UNUSED (port);
227185 uint8_t id = get_pin_id (pin);
228186 gpio_irq_conf[id].callback = NULL ;
229-
230187 for (int i = 0 ; i < NB_EXTI; i++) {
231188 if (gpio_irq_conf[id].irqnb == gpio_irq_conf[i].irqnb
232189 && gpio_irq_conf[i].callback != NULL ) {
233190 return ;
234191 }
235192 }
236-
237193 LL_EXTI_DisableIT_0_31 (ll_exti_lines[id]);
238194 HAL_NVIC_DisableIRQ (gpio_irq_conf[id].irqnb );
239195}
0 commit comments