FreeRTOS is a really cool real-time operating system (preemptive scheduler) for various architectures. It has been ported to the PIC18 platform (using MPLAB C18 compiler). However, the port is not very good.
Isaac Marino Bavaresco (isaacbavaresco AT yahoo DOT com DOT br) of the PICLIST has greatly improved and optimized the port for PIC18. His PICLIST page is here.
It took me a long time to get everything set up and working properly, so I’m writing this tutorial as an easy quick-start guide on getting FreeRTOS running on the PIC18.
Things you’ll need to get started:
- A PIC18 device
- MPLAB & MPLAB C18 compiler
- FreeRTOS
- I have made a convenient FreeRTOS modified package available for download (FreeRTOS 5.3.0) for the PIC18 containing Isaac’s improved port. DOWNLOAD
- My sample project. It’s the fastest way to get started. However, I recommend that you download this sample project just for the source files (don’t actually use the project file) and you set up your own project by reading this tutorial. DOWNLOAD
Step 1 – Setting up your directories
Configure your directories in this structure:
- PIC_Projects
- FreeRTOS
- Project_1
- Project_2
- Project_3
Step 2 – Download FreeRTOS_mod for PIC18 from the link above.
Unzip in your projects directory and you will get a directory named “FreeRTOS”.
Step 3 – Create a project
- Open MPLAB and click Project –> New…
- Create a project with the name of your choice corresponding to the directory structure outlined above.
- Click Configure –> Select Device… and select your PIC18 device
- Click Project –> Build Options –> Project…
- In the Directories tab
- select Library Search Path from the combo box and add the lib folder from your C18 installation. For me, it was C:\MCC18\lib.
- Next, select Include Search Path from the same combo box and add the following directories:
- .
- ..\FreeRTOS\Source\Portable\PIC18
- ..\FreeRTOS\Source\include
- In the MPASM/C17/C18 Suite tab
- Enable the checkbox for Extended mode.
- In the MPLAB C18 tab
- In the General category, add the following macro: MPLAB_PIC18F_PORT
- In the Memory Model category, select Large code model, Large data model, Multi-bank model
- Apply the changes.
- Add the following files to the project Source Files:
- Everything in the FreeRTOS\Source directory
- Everything in the FreeRTOS\Source\portable\PIC18 directory
- My main.c file from my sample project
- Add the following files to the project Header Files:
- FreeRTOSConfig.h from my sample project
- Add FreeRTOS\Source\portable\PIC18\18f2620_e_FreeRTOS.lkr file to the Linker Script section. If you are using a different device, you need to make your own linker script. Use the one provided as a reference and compare it to the original one.
- You should now be ready to compile your project. Go ahead and click Project –> Build All and watch the magic happen.
Explanation
All that this project does is create two tasks that constantly print “abcd” and “1234” to the UART. It uses a mutex to ensure that the non-thread-safe puts function is only being used in one thread at a time.
The RTOS configuration file is FreeRTOSConfig.h. The only thing non-standard in there is the #define CUSTOM_HEAP macro. I created this little modification to replace the standard malloc()/free() calls in the RTOS source to instead use Isaac’s functions which are far more optimized.