Author Topic: STM32 interrupt priority  (Read 1913 times)

0 Members and 1 Guest are viewing this topic.

Online NiHaoMikeTopic starter

  • Super Contributor
  • ***
  • Posts: 9116
  • Country: us
  • "Don't turn it on - Take it apart!"
    • Facebook Page
STM32 interrupt priority
« on: April 07, 2024, 12:56:15 pm »
I have a STM32F103 that I'm using to sniff I2C traffic, it mostly works but misses an interrupt occasionally causing it to read the wrong data. I read that STM32 supports setting the interrupt priority but I can't find any references to how to do that in PlatformIO with the Maple core. (I have to use the Maple core as it's the only one that supports the USBComposite library.)

I have considered adding another microcontroller just for sniffing I2C but that seems excessive when the STM32 should be able to handle it on its own.
Cryptocurrency has taught me to love math and at the same time be baffled by it.

Cryptocurrency lesson 0: Altcoins and Bitcoin are not the same thing.
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6038
  • Country: es
Re: STM32 interrupt priority
« Reply #1 on: April 07, 2024, 01:38:44 pm »
« Last Edit: April 07, 2024, 01:56:46 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Online pcprogrammer

  • Super Contributor
  • ***
  • Posts: 3937
  • Country: nl
Re: STM32 interrupt priority
« Reply #2 on: April 07, 2024, 02:40:19 pm »
Or just write directly to the registers like below.

Code: [Select]
  //Set priority for USB interrupt to be lower then the other interrupts
  //This is an array of 8 bit registers, of which only the upper 4 bits are used for the priority allowing for 16 levels
  //By grouping this is separated to allow for having sub priorities within a single group.
  //The higher the number the lower the priority
  NVIC->IP[USB_LP_CAN1_RX0_IRQn] = 0xC0;  //(1100b) Group priority 3, sub priority 0

More on this can be found in the attached manual.

Offline ajb

  • Super Contributor
  • ***
  • Posts: 2656
  • Country: us
Re: STM32 interrupt priority
« Reply #3 on: April 15, 2024, 02:41:43 pm »
The NVIC is part of the ARM architecture, not specific to the STM32 family, so if you want the gritty details those are in the ARM documentation.  It's a bit heavier reading that requires a lot of flipping back and forth to interpret everything, but worth knowing where to find this stuff when you need an in-depth answer.  The other information linked above is probably adequate to get what you need done, though.

The NVIC itself is described in the Cortex-M3 Technical Reference Manual: https://developer.arm.com/documentation/ddi0337/h/nested-vectored-interrupt-controller?_ga=2.258143811.839925519.1629395464-2030874199.1629395464
And the way the architecture handles exceptions/interrupts is described in the ARMv7-M Architecture Reference Manual: https://developer.arm.com/documentation/ddi0403/d/System-Level-Architecture/System-Level-Programmers--Model/ARMv7-M-exception-model?lang=en
 

Online NiHaoMikeTopic starter

  • Super Contributor
  • ***
  • Posts: 9116
  • Country: us
  • "Don't turn it on - Take it apart!"
    • Facebook Page
Re: STM32 interrupt priority
« Reply #4 on: April 17, 2024, 03:53:26 am »
Got it working, for reference with the Maple core, use this for interrupt pins 5-9:
nvic_irq_set_priority(NVIC_EXTI_9_5, 0);

And it helps to redefine the interrupt handler to only check the pins you're actually using.
Code: [Select]
extern "C" void __irq_exti9_5(void) {
digitalWrite(PC13, 0);
register uint32 pr = EXTI_BASE->PR;
register uint32 handled_msk = 0;
if(pr & 0x100) {
scl_s();
handled_msk = 0x100;
}
if(pr & 0x200) {
sda_s();
handled_msk |= 0x200;
}
EXTI_BASE->PR = (handled_msk);
asm volatile("nop");
asm volatile("nop");
digitalWrite(PC13, 1);
}
The "digitalWrite" lines are only for verification using a logic analyzer, they can be commented out when no longer needed. Not sure how necessary the "nop" lines are, they were part of the original interrupt handler and I'm sure there's a good reason why they're there.
Cryptocurrency has taught me to love math and at the same time be baffled by it.

Cryptocurrency lesson 0: Altcoins and Bitcoin are not the same thing.
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6038
  • Country: es
Re: STM32 interrupt priority
« Reply #5 on: April 23, 2024, 07:36:37 pm »
16 levels.
Read RM0008, page 197.
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Online pcprogrammer

  • Super Contributor
  • ***
  • Posts: 3937
  • Country: nl
Re: STM32 interrupt priority
« Reply #6 on: April 24, 2024, 08:14:13 am »
Better to look into this manual and read the section about NVIC.

Edit: Fixed the link to the manual.
« Last Edit: April 24, 2024, 02:01:01 pm by pcprogrammer »
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6038
  • Country: es
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 



Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf