# FreeRTOS for PIC18F4550

**URL:** <https://forums.freertos.org/t/freertos-for-pic18f4550/163>\
**Category:** Kernel\
**Created:** [June 1, 2010, 9:37pm UTC](https://forums.freertos.org/t/freertos-for-pic18f4550/163 "2010-06-01T21:37:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 1, 2010, 9:37pm UTC](https://forums.freertos.org/t/freertos-for-pic18f4550/163/1 "2010-06-01T21:37:48Z")

</div>

**[kelrayes](https://sourceforge.net/u/kelrayes/)** wrote on [Tuesday, June 01, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/06915de1/#eacf):

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.  
Thanks

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 25, 2010, 4:06pm UTC](https://forums.freertos.org/t/freertos-for-pic18f4550/163/2 "2010-09-25T16:06:16Z")

</div>

**[kpettit1](https://sourceforge.net/u/kpettit1/)** wrote on [Saturday, September 25, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/06915de1/#c426):

Hi kelrayes,

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

Ken

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 25, 2010, 4:11pm UTC](https://forums.freertos.org/t/freertos-for-pic18f4550/163/3 "2010-09-25T16:11:22Z")

</div>

**[kelrayes](https://sourceforge.net/u/kelrayes/)** wrote on [Saturday, September 25, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/06915de1/#80a8):

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.

Thanks

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 27, 2010, 3:54pm UTC](https://forums.freertos.org/t/freertos-for-pic18f4550/163/4 "2010-09-27T15:54:19Z")

</div>

**[kpettit1](https://sourceforge.net/u/kpettit1/)** wrote on [Monday, September 27, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/06915de1/#e8f5):

The heap\_1.c, heap\_2.c, etc. files have a statically defined variable called xHeap with a big portCHAR array in them.&nbsp; This is where the “heap” is located.&nbsp; The size of the portCHAR array is determined by the configTOTAL\_HEAP\_SIZE (plus 4 or 8 based on portBYTE\_ALIGNMENT).&nbsp; 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.&nbsp;

The PIC18F4550 has 2048 bytes of RAM.&nbsp; 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).&nbsp; 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.&nbsp; I create a DATABANK NAME=freertos\_heap and give it a specific RAM range in the linker script.&nbsp; Then in the heap\_2.c file, just before the static declaration of xHeap, I add a line like:&nbsp; #pragma udata freertos\_heap = 0x300&nbsp; (or whatever).

Ken

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 8, 2010, 6:01am UTC](https://forums.freertos.org/t/freertos-for-pic18f4550/163/5 "2010-10-08T06:01:21Z")

</div>

**[robinjee](https://sourceforge.net/u/robinjee/)** wrote on [Friday, October 08, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/06915de1/#8949):

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

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 8, 2010, 7:14am UTC](https://forums.freertos.org/t/freertos-for-pic18f4550/163/6 "2010-10-08T07:14:14Z")

</div>

**[kpettit1](https://sourceforge.net/u/kpettit1/)** wrote on [Friday, October 08, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/06915de1/#3fb5):

I don’t see any reason why it wouldn’t.&nbsp; 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).&nbsp; The biggest differences with the 4682 are the increased RAM & FLASH and the substitution of the USB controller in favor of a CAN controller.&nbsp; Otherwise, the peripherals are basically the same, at least any that would affect the operation of FreeRTOS.

Ken

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 8, 2010, 10:26am UTC](https://forums.freertos.org/t/freertos-for-pic18f4550/163/7 "2010-10-08T10:26:52Z")

</div>

**[robinjee](https://sourceforge.net/u/robinjee/)** wrote on [Friday, October 08, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/06915de1/#11fb):

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

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 8, 2010, 6:46pm UTC](https://forums.freertos.org/t/freertos-for-pic18f4550/163/8 "2010-10-08T18:46:57Z")

</div>

**[kelrayes](https://sourceforge.net/u/kelrayes/)** wrote on [Friday, October 08, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/06915de1/#1845):

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  
El-Rayes

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 8, 2010, 11:52pm UTC](https://forums.freertos.org/t/freertos-for-pic18f4550/163/9 "2010-10-08T23:52:26Z")

</div>

**[kpettit1](https://sourceforge.net/u/kpettit1/)** wrote on [Friday, October 08, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/06915de1/#6b07):

Hi robinjee,

Well your linker script will depend heavily on your application.&nbsp; Are you using USB?&nbsp; If so, how much RAM does your USB solution require?&nbsp; The default PIC18F4550 linker script uses RAM from 0x400 - 0x7FF for USB, but you may not require this much.&nbsp; So you need to remove some of the DATABANK declarations to free up space for your heap.&nbsp; 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
LIBPATH .
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=devid START=0x3FFFFE END=0x3FFFFF 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
SECTION NAME=CONFIG ROM=config
STACK SIZE=0x60 RAM=gpr1

```

In this example, the FreeRTOS heap and it’s control variables occupy 0x200-0x5FF = 1K RAM.&nbsp; 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).&nbsp; 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;
	#else
		volatile unsigned portLONG ulDummy;
	#endif	
	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).&nbsp; It must be 8 bytes less than 1K because of the heap management variables.&nbsp; 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.

Ken

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [December 13, 2010, 11:49am UTC](https://forums.freertos.org/t/freertos-for-pic18f4550/163/10 "2010-12-13T11:49:36Z")

</div>

**[rfkfun](https://sourceforge.net/u/rfkfun/)** wrote on [Monday, December 13, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/06915de1/#9bcb):

Please reply !!!

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