This project walks you through the process building a FreeRTOS based AVR application to toggle a LED on the ATMega328p microcontroller. The FreeRTOS kernel is developed by leading chip manufacturers to provide a Real Time Operating System for microtrollers and microprocessors. Refer to your microcontroller's datasheet to determine optiomal memory footprint and stack size.
Installing avr-gcc on MacOS w/Brew
brew tap osx-cross/avr
brew install avr-gcc
verify installation
avr-gcc --version
brew install avrdude
or
brew install avrdude --with-usb
verify installation
avrdude -v
The vLEDFlashTask code below is the body of the thread that FreeRTOS will execute from the call stack. The for loop simply toggles the LED port on and off once per second.
void vLEDFlashTask(void *pvParms)
{
vLEDInit();
portTickType xLastWakeTime;
const portTickType xFrequency = 1000;
xLastWakeTime = xTaskGetTickCount();
for(;;) {
vLEDToggle();
vTaskDelayUntil(&xLastWakeTime, xFrequency);
}
}
The code below initializes the led port and prepares for use.
void vLEDInit(void)
{
DDRB |= _BV(PB5);
}
Much like the blinky code we wrote in avr_328p_blinky, the following method is called from the FreeRTOS vLEDFlashTask thread to toggle the LED on and off.
void vLEDToggle(void)
{
PORTB ^= _BV(PB5);
}
To compile the source code we need to create a Makefile to autmoate the process. Makefiles allow us to place all build/clean/flash commands into one easy to use file. Each microcontroller has a unique set of parameters prior to compiling and deploying. These commands are listed a the top of the Makefile and are easily changed.
Below is a snippet of Makefile parametes that would be changed to target your project.
# parameters (make changes accordingly)
# project name
PRJ = main
# avr mcu
MCU = atmega328p
# mcu clock frequency
CLK = 8000000
# avr programmer (and port if necessary)
# e.g. PRG = usbtiny -or- PRG = arduino -P /dev/tty.usbmodem411
PRG = usbtiny
Now that your Makefile is complete issue the make from the command start the build process.
make
Now that we compiled the code we can deploy to our microcontroller.
make flash
As you can see adding FreeRTOS support is pretty straight forward. FreeRTOS will allow you to program your microcontroller more like a computer using threads and task isolation. The tradeoff is more disciplined memory management of your threads. These skills only add to your ability to design more efficient code.