How to use Interrupts on STM8 with sduino

The official STMicroelectronics Arduino core
Post Reply
leif
Posts: 6
Joined: Wed Oct 14, 2020 1:55 am

How to use Interrupts on STM8 with sduino

Post by leif »

With some googling, sleuthing, and trial-and-error, I figured it out!

With STM8 sduino, attachInterrupt works but not like on a normal arduino.

Interrupts are separate per PORT, not per pin. Each port has multiple pins. PA1, PA2 are on port A, PC3-PC7 are on port C etc.
Use pinMode to designate the pin as input, so that digitalRead can keep working in case you need it.
After that, you must call GPIO_Init (an STM8-specific API function) to actually enable interrupts for a particular pin.
Then, you disable interrupts and call EXTI_SetExtIntSensitivity to set the interrupt trigger type on that entire port.
Finally attachInterrupt(). See my example below, this code works for me on my STM8S003F3.

Hope this saves someone some time. :)

Code: Select all

void ISR()
{
  //your code here
}

void setup()
{
  pinMode(2,INPUT);

  GPIO_Init(GPIOA, GPIO_PIN_3, GPIO_MODE_IN_FL_IT);
  disableInterrupts();
  EXTI_SetExtIntSensitivity( EXTI_PORT_GPIOA, EXTI_SENSITIVITY_RISE_ONLY);  
  enableInterrupts();

  attachInterrupt(INT_PORTA & 0xFF,ISR,0);
}
///Leif

zoomx
Posts: 17
Joined: Fri Dec 20, 2019 10:12 am
Location: Near Mt.Etna

Re: How to use Interrupts on STM8 with sduino

Post by zoomx »

+1!!!

Post Reply

Return to “STM8 Core”