To quick start, grab the source code in https://github.com/brunolalb/FreeRTOS_ArduinoMEGA-Demo_AVR323
For that, first we'll need a few extra files from the FreeRTOS.
- From the folder "FreeRTOSv9.0.0\FreeRTOS\Demo\Common\include", copy the following files to the project folder "FreeRTOS_ArduinoMEGA-Demo_AVR323\FreeRTOS\Demo\Common\include":
- integer.h,
- comtest.h,
- serial.h,
- PollQ.h,
- crflash.h.
- From the folder "FreeRTOSv9.0.0\FreeRTOS\Demo\Common\Minimal", copy the following files to the project folder "FreeRTOS_ArduinoMEGA-Demo_AVR323\FreeRTOS\Demo\Common\Minimal":
- integer.c,
- comtest.c,
- PollQ.c,
- crflash.c.
- A few other source files will have to be created (or adapted from the existing ones), so copy the folder "FreeRTOSv9.0.0\FreeRTOS\Demo\AVR_ATMega323_WinAVR\serial" to your project's root (it has the implementation of the serial port routines).
- The registers test routine, present in the ATmega323 Demo project can also be used, so I created a "RegTest" folder in the root of my project and added the regtest.c and regtest.h available in "FreeRTOSv9.0.0\FreeRTOS\Demo\AVR_ATMega323_WinAVR".
- Finally, for the co-routine capability be present, the source file croutine.c, available in "FreeRTOSv9.0.0\FreeRTOS\Source" must be copied to your projects folder "FreeRTOS_ArduinoMEGA-Demo_AVR323\FreeRTOS\Source".
On the following paragraphs, I'll describe the changes I made in the AVR323 Demo to run in our ArduinoMEGA port.
serial.c
As I told before, the serial routines are implemented in this file and they're very platform dependent, as many internal registers will be manipulated. This file is located in your project folder (if you copied the files I told you to) "FreeRTOS_ArduinoMEGA-Demo_AVR323\serial\serial.c". I understand the ATmega2560 has 4 serial ports (serial 0 to 3), but right now, I'm only implementing the Serial0, which is the one available on the USB connection. In the future, the serial.c can be modified to implement all the 4 hardware UART.Basically what is needed is to correct the registers and interrupt vector names to the right ones, according to the microcontroller's datasheet. Besides that, I'm also using the option to double the communication speed, which will reduce the baud rate error rate (see table 22-12, page 226 of the datasheet), specially when using 115200 bps. Also, to enhance the routines, I added some error detection to the reception interrupt by checking the UCSRnA register prior to reading the data.
main.c
This file had several changes. First of all, I wanted it to keep the Demo Blinky routines, as I knew they're already working and blinking its LED, so it would be a good way of knowing if the program halted for some reason. Also, my approach on merging both projects (Demo Blinky and Demo AVR323) was to add each task creation call and compile. It wouldn't work, of course, so I would go after the missing headers and source files and compile again until it worked. So I added a bunch of flags to enable or disable each functionality. In the end, I got everything to work almost out of the box. Almost, as I realized a few things:- The coroutines use the first 3 LEDs (LEDs 0, 1 and 2) and this isn't easily configurable, so I made them available in the ParTest.c.
- The Demo Blinky was configured to blink the LED 3
- The Communication Tests uses the configured LED (4, for reporting a TX) and the next one (5, for reporting a RX) and expect to receive the same value it sent (a loop connection)
- The Error Check task was configured to blink the LED 6 while the program worked fine (it stops blinking if something goes wrong).
- The Assert Called function kept its LED number 7.
To see all that working, I had to place all LEDs in a protoboard, but don't forget to check Arduino's pin mapping to connect everything. The whole PORTB will blink LEDs now. The connection is as follows:
LED number | Pin Name | Mapped Pin | Function |
---|---|---|---|
0
|
PB0
|
Digital 53
|
Co routine
|
1
|
PB1
|
Digital 52
|
Co routine
|
2
|
PB2
|
Digital 51
|
Co routine
|
3
|
PB3
|
Digital 50
|
Demo Blinky
|
4
|
PB4
|
Digital 10
|
Com Test - TX
|
5
|
PB5
|
Digital 11
|
Com Test - RX
|
6
|
PB6
|
Digital 12
|
Erro Check
|
7
|
PB7
|
Digital 13
(on board LED)
|
Assert Called
|
The project was uploaded to GitHub.