FreeRTOS for PIC18F4550

kelrayes wrote on Tuesday, June 01, 2010:

Hi All

I’m trying to modify FreeRTOS for Microchip PIC18F family to work with PIC18F4550 microcontroller, I got error message concerning udata_heap_1.o file, seems to me the error in the linker script that the heap is too large and can’t fit within the memory allocated to in the PIC18F4550, please if anyone faced similar problem and was able to solve it let me know.

kpettit1 wrote on Saturday, September 25, 2010:

Hi kelrayes,

Did you ever resolve this porting issue?  I have FreeRTOS running on a PIC18F4682 (which has more RAM), but I have also used the 18F4550 before.


kelrayes wrote on Saturday, September 25, 2010:

Hi Ken

Actually no, I moved to another RTOS called PICos18, there is a port of it for the PIC18F4550.
However I will be interested to know how did you solve the FreeRTOS issue with the PIC18F4550.


kpettit1 wrote on Monday, September 27, 2010:

The heap_1.c, heap_2.c, etc. files have a statically defined variable called xHeap with a big portCHAR array in them.  This is where the “heap” is located.  The size of the portCHAR array is determined by the configTOTAL_HEAP_SIZE (plus 4 or 8 based on portBYTE_ALIGNMENT).  The static variable xHeap will be located somewhere in RAM by the linker, and must be small enough to fit in the available ram on the PIC. 

The PIC18F4550 has 2048 bytes of RAM.  Assuming you want to reserve bank zero for fast-access variables, Bank 1 for your stack and Bank 2 for global variables, this would leave 1280 bytes for “heap” (probably less because the PicKIT2 probably wants 10 bytes for debugging).  So your configTOTAL_HEAP_SIZE should be something like 1260 or less (or much less if you have a lot of global variables).

I typically copy the default linker script (18f4550_e.lkr, etc.) into my project directory and make custom modifications to control the link process.  I create a DATABANK NAME=freertos_heap and give it a specific RAM range in the linker script.  Then in the heap_2.c file, just before the static declaration of xHeap, I add a line like:  #pragma udata freertos_heap = 0x300  (or whatever).


robinjee wrote on Friday, October 08, 2010:

May i know whether FreeRTOS works fine with 18F4550. I mean to ask does it work with all different priorities and multiple tasks.

kpettit1 wrote on Friday, October 08, 2010:

I don’t see any reason why it wouldn’t.  As I mentioned, I have it running on a PIC18F4682 which is fairly similar to the 4550 (which I have also used recently with no RTOS).  The biggest differences with the 4682 are the increased RAM & FLASH and the substitution of the USB controller in favor of a CAN controller.  Otherwise, the peripherals are basically the same, at least any that would affect the operation of FreeRTOS.


robinjee wrote on Friday, October 08, 2010:

But, can you see the tasks in RTOS viewer in MPLAB? And please can you help me with modification of  18f4550 linker.

kelrayes wrote on Friday, October 08, 2010:

Hi All

Thanks for your replies, but I moved to another RTOS more convenient for the PIC18F4550 called PICos18 and so far its working fine.

Have a great day

kpettit1 wrote on Friday, October 08, 2010:

Hi robinjee,

Well your linker script will depend heavily on your application.  Are you using USB?  If so, how much RAM does your USB solution require?  The default PIC18F4550 linker script uses RAM from 0x400 - 0x7FF for USB, but you may not require this much.  So you need to remove some of the DATABANK declarations to free up space for your heap.  Keep as much for USB, global variables, etc as needed, then assign the rest of RAM to FreeRTOS, something like:

// $Id: 18f4550_e.lkr,v 1.4 2004/08/23 18:08:22 curtiss Exp $
// File: 18f4550_e.lkr
// Sample linker script for the PIC18F4550 processor
FILES c018iz_e.o
FILES clib_e.lib
FILES p18f4550_e.lib
CODEPAGE   NAME=vectors    START=0x0            END=0x29           PROTECTED
CODEPAGE   NAME=page       START=0x2A           END=0x7FFF
CODEPAGE   NAME=idlocs     START=0x200000       END=0x200007       PROTECTED
CODEPAGE   NAME=config     START=0x300000       END=0x30000D       PROTECTED
CODEPAGE   NAME=eedata     START=0xF00000       END=0xF000FF       PROTECTED
DATABANK   NAME=gpr0       START=0x0            END=0xFF
DATABANK   NAME=gpr1       START=0x100          END=0x1FF
DATABANK   NAME=freertos_heap       START=0x200          END=0x5FF                 PROTECTED
DATABANK   NAME=usb6       START=0x600          END=0x6FF          PROTECTED
DATABANK   NAME=usb7       START=0x700          END=0x7FF          PROTECTED
ACCESSBANK NAME=accesssfr  START=0xF60          END=0xFFF          PROTECTED
STACK SIZE=0x60 RAM=gpr1

In this example, the FreeRTOS heap and it’s control variables occupy 0x200-0x5FF = 1K RAM.  Next you need to link in one of the heap source files (heap1.c, heap2.c or heap3.c depending on your need to free memory dynamically).  In the selected heap#.c file, before the declaration of xHeap, add a pragma line telling the linker to locate the variables in the freertos_heap section like so:

#pragma udata freertos_heap = 0x200
static union xRTOS_HEAP
	#if portBYTE_ALIGNMENT == 8
		volatile portDOUBLE dDummy;
		volatile unsigned portLONG ulDummy;
	unsigned portCHAR ucHeap[ configTOTAL_HEAP_SIZE ];
} xHeap;

Next set the configTOTAL_HEAP_SIZE value in your FreeRTOSConfig.h file so it is 8 bytes less than the memory allocation size (1K in this case).  It must be 8 bytes less than 1K because of the heap management variables.  Of course you need to specify the actual RAM addresses (0x200 vs 0x300, etc.) and assign the memory in case you need more or less than 1K.


rfkfun wrote on Monday, December 13, 2010:

Please reply !!!

I need the example code of PIC18F4550 with Free RTOS!!
Any clue? Please help!!