Author Topic: Interrupts on STM32F4  (Read 6954 times)

0 Members and 1 Guest are viewing this topic.

Offline farsiTopic starter

  • Regular Contributor
  • *
  • Posts: 66
Interrupts on STM32F4
« on: November 18, 2015, 02:50:09 pm »
Hi,

I am a bit puzzled on how to write interrupt functions for the STM32F4. I copy and pasted some code from examples on the net, and I think to understand the basic setup of

  NVIC_Init(&NVIC_InitStructure);

But still, I don't understand exactly how to write interrupt handlers for UART and EXTI events.
How can I improve learning these things (books, examples, ... ) ?

Thanks!
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4067
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Interrupts on STM32F4
« Reply #1 on: November 18, 2015, 06:48:39 pm »
With the manuals:  ;) http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0553a/CIHIGCIF.html

But this is the basics:
An interrupt originates from the peripheral, to work the peripheral needs power and clock. This is fixed in RCC, but you'd probably already knew that.
Then the interrupt source must be enabled in the peripherals interrupt control register. Such as USART_ISR.
Next the NVIC must have the interrupt channel of the peripheral enabled.
See CMSIS http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0553a/CIHIGCIF.html You probably have something similar available.

Also in the NVIC you can adjust the preemptive priority. Since arm allows higher priority interrupts to start inside lower priority interrupts. This gets you nested interrupts, and you should be aware of this. For example, RESET and HARDFAULT always executes. The order of the table is the default priority iirc. Watch your stack.

When you didn't remap the interrupt vector table, it will still be at the top of your bootable region. This is a lookup table with vectors (function pointers) to where the interrupt routine code is located, this can be in RAM if you've put them there to make things fast. Your (assembly) startup file should have this list, with WEAK defined functions. Weak means that they are defined, but they can be overridden. Also there is a infinite loop to catch any interrupts that fired, but don't have a valid address in the table. (eg: still weak)

In this list you find names "USART1_IRQHandler" or something more generic.
Simply creating the function will do the work of the vector table.
Code: [Select]
void USART1_IRQHandler(void) {
//Check if interrupt was because data is received
if (USART_GetITStatus(USART1, USART_IT_RXNE)) {
//Do your stuff here

//Clear interrupt flag
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}
In the function you then have to check why you are in the interrupt. There may be 254 interrupt channels, the number of wires going to the nvic are limited and peripheral interrupts are often grouped. You check this by reading the peripheral flags.
Then you do your stuff, for that flag.
Next you clear the peripheral flag, to notify you're done. This is done by writing a 1 to that bit. Simply do something like USART_ISR |= (1<<bitnumber); Or use driver. "USART_ClearITPendingBit".
Then you return.

Now what happens when you do not clear the status flag OR'd to the nvic? Well, you'll end up in an infinite loop of the interrupt, with any higher priority as nested interrupts.
You can also fire the interrupt from software using the NVIC, but the USART will not have its status flag set.
Refer to the "STM32F3xxx and STM32F4xxx Cortex-M4 programming manual" for details about the nvic used in the f4's.

Obviously, this is totally redundant information that can be found if you look right for websites of universities and such... But hey, It's my thousandth post, why not make it useful?
« Last Edit: November 18, 2015, 06:50:52 pm by Jeroen3 »
 

Offline richardman

  • Frequent Contributor
  • **
  • Posts: 427
  • Country: us
Re: Interrupts on STM32F4
« Reply #2 on: November 19, 2015, 02:32:19 am »
Hi,

I am a bit puzzled on how to write interrupt functions for the STM32F4. I copy and pasted some code from examples on the net, and I think to understand the basic setup of

  NVIC_Init(&NVIC_InitStructure);

But still, I don't understand exactly how to write interrupt handlers for UART and EXTI events.
How can I improve learning these things (books, examples, ... ) ?

Thanks!

It's a multiple step process:
  • hook up the vector table entry to an interrupt handler
  • set priority in the NVIC
  • enable interrupt in the NVIC
  • for shared interrupts, make sure to check for the correct interrupt bits in the interrupt handler
  • for EXTI, you need to also enable SYSCFG
// richard http://imagecraft.com/
JumpStart C++ for Cortex (compiler/IDE/debugger): the fastest easiest way to get productive on Cortex-M.
Smart.IO: phone App for embedded systems with no app or wireless coding
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4196
  • Country: us
Re: Interrupts on STM32F4
« Reply #3 on: November 19, 2015, 06:22:58 am »
Quote
hook up the vector table entry to an interrupt handler
Note that this is USUALLY done by creating a normal C function whose name matches the pre-defined handler name in some startup file.
Unlike many architectures, cortex M interrupt handlers to not need to be tagged with any special "attributes" to save additional registers or flags; that's all done by the hardware (whether you'd want it to or not!)
 

Offline richardman

  • Frequent Contributor
  • **
  • Posts: 427
  • Country: us
Re: Interrupts on STM32F4
« Reply #4 on: November 19, 2015, 06:43:09 am »
Quote
hook up the vector table entry to an interrupt handler
Note that this is USUALLY done by creating a normal C function whose name matches the pre-defined handler name in some startup file.
Unlike many architectures, cortex M interrupt handlers to not need to be tagged with any special "attributes" to save additional registers or flags; that's all done by the hardware (whether you'd want it to or not!)

Yes, interrupt handlers for Cortex-M are just plain C functions.

How to do this part is compiler vendor specific. For us (ImageCraft), you include a copy of the vector file, and just modify as needed. Keil wants you to provide a "non-weak" definition for the handlers (if you don't, then their "weak" handlers will be linked in by default.
// richard http://imagecraft.com/
JumpStart C++ for Cortex (compiler/IDE/debugger): the fastest easiest way to get productive on Cortex-M.
Smart.IO: phone App for embedded systems with no app or wireless coding
 

Offline farsiTopic starter

  • Regular Contributor
  • *
  • Posts: 66
Re: Interrupts on STM32F4
« Reply #5 on: November 19, 2015, 01:11:24 pm »
Thanks, how nice! Wow, looks like I'll have some options to explore now. One reason I was asking is that experimenting on a Windows PC without Keil is quite difficult. Also, working with STM32 feels like seeing the trees without seeing the forrests. Well, maybe I played a bit too much with gcc and Arduino last year (which I liked, but this ARM cores are rather popular for "professional" embedded systems)
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Interrupts on STM32F4
« Reply #6 on: November 21, 2015, 07:04:31 pm »
Quote
I don't understand exactly how to write interrupt handlers for UART and EXTI events.

Handlers are generally defined in the start-up file -> but they can be device / tool specific.

The start-up files themselves can be assembly (typically) or C.
================================
https://dannyelectronics.wordpress.com/
 

Online cncjerry

  • Supporter
  • ****
  • Posts: 1269
Re: Interrupts on STM32F4
« Reply #7 on: November 23, 2015, 01:30:11 am »
Go to the site if you haven't already:

http://stm32f4-discovery.com/

He also has free GCC compiler/toolchain instructions that really work. His libraries all work if you have the latest versions.  What I liked about his code examples was that all the source was there and he seemed to keep it simple.   The STM32F4 learning curve is ridiculously steep compared to arduino but the performance is far worth it.  It takes a lot of persistence to get your first working code but gets easier.  I gave up several times and went back to arduino for later projects but now that I got it all working I'll stick with the F4.  If you drift over to the nucleo boards stay away from mbed and learn how to program it using the toolchain.
 

Online mikerj

  • Super Contributor
  • ***
  • Posts: 3233
  • Country: gb
Re: Interrupts on STM32F4
« Reply #8 on: November 23, 2015, 01:13:02 pm »

It's a multiple step process:
  • hook up the vector table entry to an interrupt handler
  • set priority in the NVIC
  • enable interrupt in the NVIC
  • for shared interrupts, make sure to check for the correct interrupt bits in the interrupt handler
  • for EXTI, you need to also enable SYSCFG

A peripheral may also have it's own interrupt enable bit:  e.g. the DMA would need the TEIE bit set, as well as the appropriate NVIC enable bit.  Some peripheral only require the NVIC bit to be set e.g. Systick.
 

Offline farsiTopic starter

  • Regular Contributor
  • *
  • Posts: 66
Re: Interrupts on STM32F4
« Reply #9 on: November 23, 2015, 02:33:20 pm »
Quote
http://stm32f4-discovery.com/

He also has free GCC compiler/toolchain instructions that really work. His libraries all work if you have the latest versions.

That is an impressive collection of blog posts. On quick glance, he uses the free Keil toolchain and CooCox for the first posts.
One of his latest tweets was 5k unique visitors a day. So, I guess I am not the only one who is taking the jump from Arduino to MCUs with more features.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf