I started to write a program for an ATXMega. This program should read a command via USART, and then execute this command (i.e. either sending data via SPI or sending data back via USART). But the µC should execute one command after another, i.e. if there is a current command running (for example sending bits via SPI), the received command should be put back into a waiting loop until the µC has finished doing this task. Now I was wondering how I should design this waiting loop. When writing a program for the same task on my pc, I simply created two threads, and one thread filled a buffer, and the second one emptied it and executed the command. But as far as I know multithreading is not possible on a µC.
What is the best way to approach the described behaviour without multithreading on a µC? Is my current approach (use a buffer with fixed length, fill it up via Interrupts, and let the µC check the buffer every 100 ms for new stuff) the best way to do that? Or is there a better way?
Programming an Atxmega for using UART
atmelprogramming
Related Topic
- Electronic – arduino – Need explanation on how to flash HEX files on ATmega32u4 via AVR109 protocol
- How to get data from one micro controller to another using UART
- Electronic – How to know usb connection is lost
- ATSamd10 Atmel ARM programming via UART bootloader
- Electrical – Programming Atmel SAM MCU using SAM-BA
- Electronic – How to change clock source dynamically on an ATtiny1634
- Electrical – Programming microcontroller using UART
Best Answer
Yes, that is one valid way to do it (also my preferred way). Store the characters in your interrupt, use them when you have time (usually in your normal infinite loop). What you will usually use is a ring buffer with read/write pointers to not lose or overwrite any characters.
Multithreading is actually possible, but you will have to implement it yourself (or use some OS for your µC), plus, it's not really different from what you do in your infinite loop. You might want to look at FreeRTOS or Micrium as examples.