An Adventure in FreeRTOS

My final dice game example for the new book uses FreeRTOS. There are three independent (non communicating) tasks, to keep things simple, rather than breaking down the game itself into multiple communicating tasks. There is the game task, a flashing light (“blinky”) task, and a background task used to measure performance, like I did with the earlier interrupt driven version. Because of the size of FreeRTOS, this version is several times larger, but the code itself simplifies slightly.

As mentioned in an earlier blog post, FreeRTOS does not support the ATmega4809 microcontroller used in the Arduino Nano Every, my favorite Arduino board. I did hack the code some months ago so that it would work on the 4809. Unfortunately, my game program did not work on my Arduino Nano Every with my hacked FreeRTOS.

While not an issue with the other microcontrollers, for some reason I could not use both vTaskDelay() and taskYIELD() functions in the same task. I wanted to wait for my SPI interface to be ready using taskYIELD calls so that if there were nothing else to run it would return immediately. But I had to settle on using vTaskDelay(1) instead, which always waits until the next clock tick. As it turns out, just using vTaskDelay makes more efficient use of the processor, and the extra added delay (no more than 33 display updates per second) is not noticeable.

So with the game out of the way, I’m now on to improved SPI and I2C interfaces. These have no buffers (all buffering is done as needed in application code) and provide callback functions upon completion of the operation. I feel that they should have always been written this way. Of course this will again be two sets of code since the ATmega4809 has completely different SPI and I2C interfaces from the other AVR microcontroller.

Since writing this post I uncovered the problem with taskYIELD, which was caused by the differences in handling of interrupt enabling in the ATmega4809. The modifications necessary to FreeRTOS to run on the Arduino Nano Every can be found on my website here: https://almy.us/FreeRTOSModsForATmega4809.pdf