@@ -55,6 +55,7 @@ typedef struct
5555{
5656 EXTI_Port_TypeDef EXTI_port ;
5757 GPIO_Pin_TypeDef pin ;
58+ uint8_t current_level ;
5859#if defined(STM8Sxx )
5960 EXTI_Sensitivity_TypeDef EXTI_mode ;
6061#elif defined(STM8Lxx )
@@ -75,9 +76,9 @@ typedef struct
7576
7677#define NB_EXTI ((uint8_t)16)
7778
78- /**
79- * @}
80- */
79+ /*As this port are not usable with exti, use dummy values*/
80+ #define EXTI_Port_A_Int ((uint8_t)0xFD)
81+ #define EXTI_Port_C_Int ((uint8_t)0xFE)
8182
8283/** @addtogroup stm8sxx_System_Private_Macros
8384* @{
@@ -114,9 +115,9 @@ static uint8_t get_pin_id(GPIO_TypeDef *port, GPIO_Pin_TypeDef pin, uint8_t init
114115*/
115116static uint8_t get_pin_id (GPIO_TypeDef * port , GPIO_Pin_TypeDef pin , uint8_t init )
116117{
117- uint8_t id = 0xFF ;
118+ uint8_t id = NC ;
118119 uint8_t i = 0 ;
119- EXTI_Port_TypeDef EXTI_port ;
120+ EXTI_Port_TypeDef EXTI_port = 0xFF ;
120121
121122#if defined(STM8Sxx )
122123 if (port == GPIOA )
@@ -145,11 +146,18 @@ static uint8_t get_pin_id(GPIO_TypeDef *port, GPIO_Pin_TypeDef pin, uint8_t init
145146 }
146147#elif defined(STM8Lxx )
147148 EXTI_Pin_TypeDef EXTI_pin ;
148-
149- if (port == GPIOB )
149+ if (port == GPIOA )
150+ {
151+ EXTI_port = EXTI_Port_A_Int ;
152+ }
153+ else if (port == GPIOB )
150154 {
151155 EXTI_port = EXTI_Port_B ;
152156 }
157+ else if (port == GPIOC )
158+ {
159+ EXTI_port = EXTI_Port_C_Int ;
160+ }
153161 else if (port == GPIOD )
154162 {
155163 EXTI_port = EXTI_Port_D ;
@@ -220,7 +228,7 @@ static uint8_t get_pin_id(GPIO_TypeDef *port, GPIO_Pin_TypeDef pin, uint8_t init
220228 id = i ;
221229 break ;
222230 }
223- else if (init == 1 )
231+ else if (( init == 1 ) && ( gpio_irq_conf [ id ]. pin == 0 ) )
224232 {
225233 gpio_irq_conf [id ].EXTI_port = EXTI_port ;
226234 gpio_irq_conf [id ].pin = pin ;
@@ -229,11 +237,9 @@ static uint8_t get_pin_id(GPIO_TypeDef *port, GPIO_Pin_TypeDef pin, uint8_t init
229237 gpio_irq_conf [id ].port_IT = port ;
230238 gpio_irq_conf [id ].EXTI_pin = EXTI_pin ;
231239#endif
232- id = i ;
240+ break ;
233241 }
234- break ;
235242 }
236-
237243 return id ;
238244}
239245
@@ -267,38 +273,29 @@ void stm8_interrupt_enable(GPIO_TypeDef *port, GPIO_Pin_TypeDef pin,
267273 gpio_irq_conf [id ].callback = callback ;
268274 gpio_irq_conf [id ].EXTI_mode = EXTI_mode ;
269275
276+ // Enable and set EXTI Interrupt
277+ disableInterrupts ();
270278#if defined(STM8Sxx )
271- if ((port -> CR1 & pin ) == pin )
272- {
273- GPIO_mode = GPIO_MODE_IN_PU_IT ;
274- }
275- else
276- {
277- GPIO_mode = GPIO_MODE_IN_FL_IT ;
278- }
279- #endif
280- #if defined(STM8Lxx )
281- if ((port -> CR1 & pin ) == pin )
282- {
283- GPIO_mode = GPIO_Mode_In_PU_IT ;
284- }
285- else
286- {
287- GPIO_mode = GPIO_Mode_In_FL_IT ;
279+ GPIO_Init (port , pin , GPIO_MODE_IN_PU_IT );
280+ gpio_irq_conf [id ].current_level = GPIO_ReadInputPin (port , pin );
281+ EXTI_SetExtIntSensitivity (gpio_irq_conf [id ].EXTI_port , EXTI_SENSITIVITY_RISE_FALL /*EXTI_mode*/ );
282+
283+ #elif defined(STM8Lxx )
284+ GPIO_Init (port , pin , GPIO_Mode_In_PU_IT );
285+ gpio_irq_conf [id ].current_level = GPIO_ReadInputDataBit (port , pin );
286+ if (EXTI_GetPinSensitivity (gpio_irq_conf [id ].EXTI_pin ) != EXTI_Trigger_Rising_Falling ) {
287+ EXTI_SetPinSensitivity (gpio_irq_conf [id ].EXTI_pin , EXTI_Trigger_Rising_Falling );
288288 }
289289#endif
290290
291- // Enable and set EXTI Interrupt
292- disableInterrupts ();
293- GPIO_Init (port , pin , GPIO_mode );
291+ enableInterrupts ();
294292#if defined(STM8Sxx )
295- EXTI_SetExtIntSensitivity ( gpio_irq_conf [id ].EXTI_port , EXTI_mode );
293+ gpio_irq_conf [id ].current_level = GPIO_ReadInputPin ( port , pin );
296294#elif defined(STM8Lxx )
297- EXTI_SetPortSensitivity ( gpio_irq_conf [id ].EXTI_port , EXTI_mode );
295+ gpio_irq_conf [id ].current_level = GPIO_ReadInputDataBit ( port , pin );
298296#endif
299297
300- enableInterrupts ();
301- }
298+ }
302299
303300/**
304301* @brief This function disable the interruption on the selected port/pin
@@ -364,6 +361,7 @@ void GPIO_EXTI_Callback(EXTI_IT_TypeDef EXTI_port_pin)
364361 GPIO_Pin_TypeDef pin ;
365362 BitStatus status = RESET ;
366363 uint8_t i = 0 ;
364+ uint8_t old_level = SET ;
367365#if defined(STM8Sxx )
368366 if (EXTI_port == EXTI_PORT_GPIOA )
369367 {
@@ -394,18 +392,22 @@ void GPIO_EXTI_Callback(EXTI_IT_TypeDef EXTI_port_pin)
394392 {
395393 if (gpio_irq_conf [i ].EXTI_port == EXTI_port )
396394 {
395+ status = RESET ;
396+ old_level = gpio_irq_conf [i ].current_level ;
397+ gpio_irq_conf [i ].current_level = GPIO_ReadInputPin (port , gpio_irq_conf [i ].pin );
398+
397399 switch (gpio_irq_conf [i ].EXTI_mode )
398400 {
399401 case EXTI_SENSITIVITY_FALL_LOW :
400402 case EXTI_SENSITIVITY_FALL_ONLY :
401- if ( GPIO_ReadInputPin ( port , gpio_irq_conf [i ].pin ) == RESET )
403+ if (( old_level != RESET ) && ( gpio_irq_conf [i ].current_level == RESET ) )
402404 {
403405 status = SET ;
404406 }
405407 break ;
406408
407409 case EXTI_SENSITIVITY_RISE_ONLY :
408- if ( GPIO_ReadInputPin ( port , gpio_irq_conf [i ].pin ) != RESET )
410+ if (( old_level == RESET ) && ( gpio_irq_conf [i ].current_level != RESET ) )
409411 {
410412 status = SET ;
411413 }
@@ -418,7 +420,6 @@ void GPIO_EXTI_Callback(EXTI_IT_TypeDef EXTI_port_pin)
418420 default :
419421 break ;
420422 }
421-
422423 if ((gpio_irq_conf [i ].callback != 0 ) && (status == SET ))
423424 {
424425 gpio_irq_conf [i ].callback ();
@@ -429,37 +430,9 @@ void GPIO_EXTI_Callback(EXTI_IT_TypeDef EXTI_port_pin)
429430#if defined(STM8Lxx )
430431 EXTI_Port_TypeDef EXTI_port ;
431432 EXTI_Pin_TypeDef EXTI_pin ;
432- if (EXTI_port_pin == EXTI_IT_PortB )
433- {
434- port = GPIOB ;
435- EXTI_port = EXTI_Port_B ;
436- }
437- else if (EXTI_port_pin == EXTI_IT_PortD )
438- {
439- port = GPIOD ;
440- EXTI_port = EXTI_Port_D ;
441- }
442- else if (EXTI_port_pin == EXTI_IT_PortE )
443- {
444- port = GPIOE ;
445- EXTI_port = EXTI_Port_E ;
446- }
447- else if (EXTI_port_pin == EXTI_IT_PortF )
448- {
449- port = GPIOF ;
450- EXTI_port = EXTI_Port_F ;
451- }
452- else if (EXTI_port_pin == EXTI_IT_PortG )
453- {
454- port = GPIOG ;
455- EXTI_port = EXTI_Port_G ;
456- }
457- else if (EXTI_port_pin == EXTI_IT_PortH )
458- {
459- port = GPIOH ;
460- EXTI_port = EXTI_Port_H ;
461- }
462- else if (EXTI_port_pin == EXTI_IT_Pin0 )
433+ EXTI_ClearITPendingBit (EXTI_port_pin );
434+
435+ if (EXTI_port_pin == EXTI_IT_Pin0 )
463436 {
464437 pin = GPIO_Pin_0 ;
465438 EXTI_pin = EXTI_Pin_0 ;
@@ -506,20 +479,24 @@ void GPIO_EXTI_Callback(EXTI_IT_TypeDef EXTI_port_pin)
506479
507480 for (i = 0 ; i < NB_EXTI ; i ++ )
508481 {
509- if (( gpio_irq_conf [i ].EXTI_port == EXTI_port ) || ( gpio_irq_conf [ i ]. EXTI_pin == EXTI_pin ) )
482+ if (gpio_irq_conf [i ].EXTI_pin == EXTI_pin )
510483 {
484+ status = RESET ;
485+ old_level = gpio_irq_conf [i ].current_level ;
486+ gpio_irq_conf [i ].current_level = GPIO_ReadInputDataBit (gpio_irq_conf [i ].port_IT , gpio_irq_conf [i ].pin );
487+
511488 switch (gpio_irq_conf [i ].EXTI_mode )
512489 {
513490 case EXTI_Trigger_Falling_Low :
514491 case EXTI_Trigger_Falling :
515- if ( GPIO_ReadInputDataBit ( gpio_irq_conf [i ].port_IT , pin ) == RESET )
492+ if (( old_level != RESET ) && ( gpio_irq_conf [i ].current_level == RESET ) )
516493 {
517494 status = SET ;
518495 }
519496 break ;
520497
521498 case EXTI_Trigger_Rising :
522- if ( GPIO_ReadInputDataBit ( gpio_irq_conf [i ].port_IT , pin ) != RESET )
499+ if (( old_level == RESET ) && ( gpio_irq_conf [i ].current_level != RESET ) )
523500 {
524501 status = SET ;
525502 }
@@ -536,7 +513,6 @@ void GPIO_EXTI_Callback(EXTI_IT_TypeDef EXTI_port_pin)
536513 if ((gpio_irq_conf [i ].callback != 0 ) && (status == SET ))
537514 {
538515 gpio_irq_conf [i ].callback ();
539- EXTI_ClearITPendingBit (EXTI_port_pin );
540516 }
541517 }
542518 }
0 commit comments