Interrupting register write operation on LPC microcontroller

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Interrupting register write operation on LPC microcontroller

296 Views
Jordan09
Contributor I

These days I have solved the issue of ocasional false register write. The problem was, that I was writing a lot in the GPIO output register (LPC_GPIO_PORT->SET[1]) in the main loop. In the interrupt routine I was writing in these same registers, and when interrupt happened just in time when these registers were being writen in the main loop, upon return from interrupt, the changes to those registers were discarded and replaced with those writen into register before entering interrupt.

I am using LPC1549 microcontroller. The register writes in interrupts are used for BLDC motor controll, so you could hear loud bang from the motor every 10-30 seconds. By reducing writing registers in the main loop, i have completly eliminated the problem. The question is, is it the same with all registers in microcontroller? I cant find anything describing this problem, which can be a serious issue, and also, hard to find, once it starts causing trouble.

0 Kudos
2 Replies

280 Views
ErichStyger
Senior Contributor V

Hi @Jordan09 ,

What it sounds to me is that you are just facing a standard challenge in the world of embedded systems (or physical computing).

If you are accessing the same resources (registers, variables, etc) both from your main program and your interrupts, then you need to care about reentracy (search for this, you will find plenty of articles). In principle you need to create a 'cricitical section' (CS) for anything which could be interrupted and result in wrong code. The most basic CS can be established with disabling interrupts, but this comes with lots of other considerations (duration, how to do it, impact on realtime characteristics, etc.).

As a general advice, you should consider writing these registers impacted just from a single location/code, to ensure reentrany and serialization.

0 Kudos

285 Views
frank_m
Senior Contributor III

No, I think the problem is your limited understanding of interrupts.

Interrupts are asynchronous events, caused by "external" sources, which can happen at any time.
Asynchronous means not in any way related to or caused by the machine instruction sequence currently executing. Sources could be truly external (serial communication, GPIO transitions), or internal peripherals like timers, ADC, DMA, etc.
In your case (motor control), I assume a timer or ADC will trigger the interrupts.

The proper solution is to keep the write access limited to one location, in your case preferably in the interrupt routine.

In realtime processing systems, the PLC working principle has proven to be a successful method to avoid inconsistencies and runtime effects (race conditions).
This principle works on a process image, and consists of three sequential steps: reading all inputs, then do calculations and processing, and finally write all outputs.
One can surely adapt this approach to the application, and needs only to consider inputs & outputs that are relevent for the realtime process.

And, interrupt handler should be kept as short as possible, and avoid invoking other interrupts. 

0 Kudos