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: 25
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!!!
dannyf
Posts: 73
Joined: Sat Jul 04, 2020 7:46 pm

Re: How to use Interrupts on STM8 with sduino

Post by dannyf »

GPIO_Init (an STM8-specific API function) to actually enable interrupts for a particular pin.
or you can set the CR2 bits. CR1 bits enable pull-up.
hmeijdam
Posts: 4
Joined: Fri Nov 27, 2020 7:41 pm

Re: How to use Interrupts on STM8 with sduino

Post by hmeijdam »

Pin change interrupts support has been added to sduino.

A little workaround was still needed for me, as the latest sduino version 0.5.0 does not yet includes it.
I had to manually copy the files Winterrupts.h and Arduino.h from the sduino Github page and move that into my Arduino IDE overwriting the current ones in the sduino folder:
E:\Arduino\arduino-1.8.13\portable\packages\sduino\hardware\stm8\0.5.0\cores\sduino
(I have a portable Arduino IDE installation, as I use many different versions of the IDE in parralel, so this may be somewhere else for you)

I also had to change the examplesketch from the sduino project page a little.
the digitalPinToInterrupt (dummy) macro was not yet defined, so that is the line I added in my sketch as workaround-fix.

Code: Select all

// the pin where the input button is attached. Change, if needed
#define BUTTON  PA2
#define digitalPinToInterrupt(p)  ((p)==(p)) // workaround-fix, empty macro is missing in header files

// volatile is important, because this variable is modified in IRQ routine
volatile uint8_t flag = 0;

void on_button_pressed(void) {
  flag = 1;
}

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, 1);   // turn off the LED
  pinMode(BUTTON, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(BUTTON), on_button_pressed, FALLING);
}

void loop() {
  if (flag) {
    digitalWrite(LED_BUILTIN, 0);
    delay(300);
    digitalWrite(LED_BUILTIN, 1);
    flag = 0;
  }
}
Now it compiled for me and when connecting a button between PA2 and GND the led on my stm8s103F3 board blinks for 300ms when the buton is pressed.
Post Reply

Return to “STM8 Core”