Friday, June 19, 2009

Setting Up FreeRTOS for the PIC18 Using MPLAB C18

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.

13 comments:

  1. Just wanted to say THANK YOU! Took all of 2 minutes to work out the changes I needed to make for my PIC and your example app is up and running!

    ReplyDelete
  2. Thanks for your post! It greatly helped me customizing the RTOS for use in a PIC18F87J11.

    ReplyDelete
  3. Great :) A good info for getting started! A++

    ReplyDelete
  4. Hi Anonymous,

    I was porting a code to PIC18F87J11.

    Can you please let me know what steps I have to take.

    Thanks for your help,
    Amit

    ReplyDelete
  5. Hey there,

    just got it working this morning. finally found a worthy summer project :D

    i have it compiling properly for an 18F4685. Most everything worked right off the bat, but it took a little modification of my 18f4685 linker file to make the entire heap fit.

    thanks a bunch,

    Andrew

    ReplyDelete
  6. can you please explain about a PIC18 device?

    ReplyDelete
  7. Hi,
    Thanks for it. But I am having some problem in porting freertos to PIC18F6680. This PIC does not support Extended mode so I had to uncheck it but now I am not able to compile your code as I get error in free.asm

    Any suggestion ??

    ReplyDelete
  8. Thank you! A good thing for getting started!

    ReplyDelete
  9. Thanks for your help ...

    ReplyDelete
  10. Why is #define USING_FREE_RTOS = 0 in heap_config.h?
    Shouldn't this be 1?

    ReplyDelete
  11. there is no download link...any body please help to find out the source code

    ReplyDelete
  12. Hey, I removed the link because it was old and irrelevant. You can download the FreeRTOS package directly from their site and it includes a PIC18 example.

    ReplyDelete