cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 Good Practices: Interrupts

KMew
Senior III

Hello,

I am preparing a much larger code that handles a slew of tasks (gathers CAN data from up to 5 different nodes, reads ADCs, sends GPIO PWM signals and ON/OFF signals, LCD display screen via TouchGFX).

I have interrupts for specific tasks (when a CAN message is received, timer interrupt, etc.). Additionally, I will want to periodically send information to different CAN nodes. I understand that it is considered a good practice to minimize the amount of code in the interrupt functions, so I picture the code sequence being as follows (for the CAN receive portion, for example):

  1. CAN Message is received
  2. Interrupt is enabled (enter interrupt function)
  3. Read RxData and store it in the the variables relevant to the data sent
  4. Do arithmetic or logic based on data received <------------------------------

What I want to know is the best practice to do step 4. The arithmetic and/or logic could be a fair bit of code, so I know I shouldn't do it in the interrupt function, but the TouchGFX display portion will cause it to be in a specific task. Should I write the logic in the TouchGFX task function, or create a different task/function? If so, how will it be entered without another interrupt?

10 REPLIES 10
gregstm
Senior III

I tend to use circular buffers (or ring buffers as others have called them). The interrupt adds to the buffer and updates the "in index", the main program only alters the "out_index" and comparing the two indexes lets you know if new data is available (you probably know how this works...). Sometimes if the main processing is periodically sizeable (eg. an FFT), I even transfer the data from the interrupt buffer to another larger circular buffer. But if the processing is reasonable and predictable, I do it within the interrupt handler. eg. when I am reading data from my microphone (using the DFSDM), I do the dc-offset removal (assembly routine) and decimation low-pass filter (assembly routine) in the interrupt handler before adding it to the circular buffer - the main routine adds that to a larger buffer so I can do multiple FFTs to do spectograms etc.