@@ -55,6 +55,7 @@ typedef struct
55
55
{
56
56
EXTI_Port_TypeDef EXTI_port ;
57
57
GPIO_Pin_TypeDef pin ;
58
+ uint8_t current_level ;
58
59
#if defined(STM8Sxx )
59
60
EXTI_Sensitivity_TypeDef EXTI_mode ;
60
61
#elif defined(STM8Lxx )
@@ -75,9 +76,9 @@ typedef struct
75
76
76
77
#define NB_EXTI ((uint8_t)16)
77
78
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)
81
82
82
83
/** @addtogroup stm8sxx_System_Private_Macros
83
84
* @{
@@ -114,9 +115,9 @@ static uint8_t get_pin_id(GPIO_TypeDef *port, GPIO_Pin_TypeDef pin, uint8_t init
114
115
*/
115
116
static uint8_t get_pin_id (GPIO_TypeDef * port , GPIO_Pin_TypeDef pin , uint8_t init )
116
117
{
117
- uint8_t id = 0xFF ;
118
+ uint8_t id = NC ;
118
119
uint8_t i = 0 ;
119
- EXTI_Port_TypeDef EXTI_port ;
120
+ EXTI_Port_TypeDef EXTI_port = 0xFF ;
120
121
121
122
#if defined(STM8Sxx )
122
123
if (port == GPIOA )
@@ -145,11 +146,18 @@ static uint8_t get_pin_id(GPIO_TypeDef *port, GPIO_Pin_TypeDef pin, uint8_t init
145
146
}
146
147
#elif defined(STM8Lxx )
147
148
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 )
150
154
{
151
155
EXTI_port = EXTI_Port_B ;
152
156
}
157
+ else if (port == GPIOC )
158
+ {
159
+ EXTI_port = EXTI_Port_C_Int ;
160
+ }
153
161
else if (port == GPIOD )
154
162
{
155
163
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
220
228
id = i ;
221
229
break ;
222
230
}
223
- else if (init == 1 )
231
+ else if (( init == 1 ) && ( gpio_irq_conf [ id ]. pin == 0 ) )
224
232
{
225
233
gpio_irq_conf [id ].EXTI_port = EXTI_port ;
226
234
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
229
237
gpio_irq_conf [id ].port_IT = port ;
230
238
gpio_irq_conf [id ].EXTI_pin = EXTI_pin ;
231
239
#endif
232
- id = i ;
240
+ break ;
233
241
}
234
- break ;
235
242
}
236
-
237
243
return id ;
238
244
}
239
245
@@ -267,38 +273,29 @@ void stm8_interrupt_enable(GPIO_TypeDef *port, GPIO_Pin_TypeDef pin,
267
273
gpio_irq_conf [id ].callback = callback ;
268
274
gpio_irq_conf [id ].EXTI_mode = EXTI_mode ;
269
275
276
+ // Enable and set EXTI Interrupt
277
+ disableInterrupts ();
270
278
#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 );
288
288
}
289
289
#endif
290
290
291
- // Enable and set EXTI Interrupt
292
- disableInterrupts ();
293
- GPIO_Init (port , pin , GPIO_mode );
291
+ enableInterrupts ();
294
292
#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 );
296
294
#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 );
298
296
#endif
299
297
300
- enableInterrupts ();
301
- }
298
+ }
302
299
303
300
/**
304
301
* @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)
364
361
GPIO_Pin_TypeDef pin ;
365
362
BitStatus status = RESET ;
366
363
uint8_t i = 0 ;
364
+ uint8_t old_level = SET ;
367
365
#if defined(STM8Sxx )
368
366
if (EXTI_port == EXTI_PORT_GPIOA )
369
367
{
@@ -394,18 +392,22 @@ void GPIO_EXTI_Callback(EXTI_IT_TypeDef EXTI_port_pin)
394
392
{
395
393
if (gpio_irq_conf [i ].EXTI_port == EXTI_port )
396
394
{
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
+
397
399
switch (gpio_irq_conf [i ].EXTI_mode )
398
400
{
399
401
case EXTI_SENSITIVITY_FALL_LOW :
400
402
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 ) )
402
404
{
403
405
status = SET ;
404
406
}
405
407
break ;
406
408
407
409
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 ) )
409
411
{
410
412
status = SET ;
411
413
}
@@ -418,7 +420,6 @@ void GPIO_EXTI_Callback(EXTI_IT_TypeDef EXTI_port_pin)
418
420
default :
419
421
break ;
420
422
}
421
-
422
423
if ((gpio_irq_conf [i ].callback != 0 ) && (status == SET ))
423
424
{
424
425
gpio_irq_conf [i ].callback ();
@@ -429,37 +430,9 @@ void GPIO_EXTI_Callback(EXTI_IT_TypeDef EXTI_port_pin)
429
430
#if defined(STM8Lxx )
430
431
EXTI_Port_TypeDef EXTI_port ;
431
432
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 )
463
436
{
464
437
pin = GPIO_Pin_0 ;
465
438
EXTI_pin = EXTI_Pin_0 ;
@@ -506,20 +479,24 @@ void GPIO_EXTI_Callback(EXTI_IT_TypeDef EXTI_port_pin)
506
479
507
480
for (i = 0 ; i < NB_EXTI ; i ++ )
508
481
{
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 )
510
483
{
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
+
511
488
switch (gpio_irq_conf [i ].EXTI_mode )
512
489
{
513
490
case EXTI_Trigger_Falling_Low :
514
491
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 ) )
516
493
{
517
494
status = SET ;
518
495
}
519
496
break ;
520
497
521
498
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 ) )
523
500
{
524
501
status = SET ;
525
502
}
@@ -536,7 +513,6 @@ void GPIO_EXTI_Callback(EXTI_IT_TypeDef EXTI_port_pin)
536
513
if ((gpio_irq_conf [i ].callback != 0 ) && (status == SET ))
537
514
{
538
515
gpio_irq_conf [i ].callback ();
539
- EXTI_ClearITPendingBit (EXTI_port_pin );
540
516
}
541
517
}
542
518
}
0 commit comments