Saturday, June 27, 2009

FreeRTOS on the dsPIC33

I recently ordered a bunch of dsPIC33F samples – specifically the dsPIC33FJ128GP802. These devices are amazing. They’re fast, have lots of RAM, and very very easy to use with FreeRTOS.

Things to keep in mind when setting up a project for the dsPIC:

  • Ensure you define the MPLAB_DSPIC_PORT macro (Project options –> MPLAB C30 –> Add… (in the preprocessor macros box)
  • I prefer to use heap_3.c so that the compiler’s own malloc() and free() functions can be used. To use them, define a heap size (Project options –> MPLAB LINK30 –> Heap size: (text box)). I just put 5000. Adjust yours accordingly.
  • C30 optimization: (Project Options –> MPLAB C30 –> Categories: Optimization) – I selected –O3 level optimization. Adjust yours accordingly. The port page on freertos.org says to enable the Omit frame pointer checkbox. Strangely enough, the demo application does not have the checkbox enabled.
  • Also remember to tweak your FreeRTOSconfig.h file and set up all the include directories.

I had no trouble getting the project to work. I even made a simple LED blinker. No modifications to the linker script had to be made (what a relief). The sample project is provided below:

Download Project

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.

FreeRTOS Finally Working!

With a lot of help from Isaac Marino Bavaresco (PICLIST), I finally got FreeRTOS up and running perfectly on my PIC18F2620. It’s a fun chip loaded with features and perfect for FreeRTOS. I’ll write a complete tutorial shortly to get the RTOS working on this chip. Hopefully, when I get my dsPIC33 and PIC32, it will be a whole lot easier to get the RTOS working.

Monday, June 8, 2009

Some Starter C18 Code for the PIC18

I haven’t posted anything in a while, so here is some C18 code for a starting off a project.

#include 
#include 
#include 
#include 

#pragma config OSC = INTIO67	//Internal oscillator
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = ON
#pragma config BOREN = OFF
#pragma config WDT = OFF
#pragma config MCLRE = OFF		//MCLR is disabled
#pragma config PBADEN = OFF
#pragma config LVP = OFF
#pragma config XINST = ON		//extended mode enabled

long int i = 0;

void delay1s() {
	Delay10KTCYx(200);
	Delay10KTCYx(200);
	Delay10KTCYx(200);
	Delay10KTCYx(200);
}

void main() {
	OSCCON = 0b01110000; 	//8 MHz
	OSCTUNE = 0b01011111;	//enable PLL
	TRISC = 0x00;			//PORTC is all output
	stdout = _H_USART; 		//Set to user-defined output stream via _user_putc
	
	OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 51);


	while(1) {
		delay1s();
		printf("ping! %li\n", i);
		i++;
	}	
}

Here’s the project itself: Click to Download