I am wondering if there is a way to detect if an interrupt occurred in section of code. I know in the M3/M4/M7 I can use the LDREX/STREX to see if an exception(interrupt) occurred, is there any other method? Specifically one of the Cortex M0?
Not sure what you want to detect exactly. You can read the Interrupt Control and State Register (ICSR), its fied VECTACTIVE indicates the current active exception. If you want to know whether there has been interrupt occurred before, your system needs to record such event. Otherwise, it's possible.
Haiyan said:If you want to know whether there has been interrupt occurred before, your system needs to record such event
For a peripheral interrupt, the peripheral may do that ...
I found a better way to solve my specific problem. What I was trying to do is preemptive scheduler, so I parse to see which task is the next task to run. Thus I wanted to clear a flag and then parse the next task to run, when done parsing task if the flag was set that an interrupt occurred then I would know I need to parse the tasks list again as we might have changed which task to run. I then realized that I can set the flag in my function that changes/sets tasks state and use that that flag which is better solution. That is the function to change task from suspended to running would be called from interrupt, and my flag would be set.
However I could see other cases where it might be nice to know if an exception had been called, but most of the ones I can think of are solved with the LDREX/STREX instructions.
As far as the ICSR register goes I use that a lot. For example I have a debug UART and if the user logs a message to the debug UART from an interrupt service routine then the UART write function switches from being interrupt driven to polled mode. That is the UART write uses a FIFO and interrupts to push out the bytes, however if the write is called from a high priority interrupt handler then we have to switch to polled mode to push out the bytes if needed. Hence I use the ICSR to see if the write function is called from an interrupt handler or not.
It would be nice if ARM also had a register to show what the current priority level is for the active interrupt. For my UART example if the interrupt calls write and and is lower priority than the UART interrupt I do not have to switch to poll mode, but if it is higher I do.
Of course as I am getting into interrupt priorities and nested interrupts my code is getting very messy to where I need a good RTOS. For example I was trying to use UART with some high speed data, which requires use of DMA. So I end up with UART write routine filling a buffer, then if buffer is full or it has been more than 10ms I would dump the buffer using DMA. This gets me into issues as to if the DMA interrupt priority is higher than the timer priority, and if the UART write function is called from a different interrupt service routine how to handle that. Which might be easier to deal with if the interrupts just schedule preemptive tasks to run which does the buffer processing. This way if UART write is called from a high priority task, the UART write code would get inherit the priority of the calling task to get the work done and be a lot cleaner code.
View all questions in Cortex-M / M-Profile forum