Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training


Changint the timer in the PIC32 Port

Posted by Gray on June 22, 2009
I was just wondering if anybody had managed to successfully change the timer from timer 1 to another timer (2 or 3). I have tried altering the Port files and have changed the set-up routine (see below) to use timer 2, I have also looked at the ISR ASM code (complete novice here) and can’t see how I would change this if it is indeed need changing, watching the interrupt control reg, you see the timer sets setup correctly, and after the interrupt the reg value goes from 0x8102 to 0x8100 (when operating with timer one reg reads 0x8012 when entering the ISR, this is I believe expected).

The effect when changed is that FreeRTOS does not register the tasks and as such even basically loops only within the RTOS code. I believe that that the timer is working because it will debug within it, but I must be missing something within the rest of the port to cause this behaviour.

I know this seems a bit OTT, but I want to use the 64 pin PIC and need an external pin to drive a timer/counter so have to use timer 1 for this.

Sample from Port.asm

.set noreorder

.extern pxCurrentTCB
.extern vTaskSwitchContext
.extern vPortIncrementTick
.extern xISRStackTop

.global vPortStartFirstTask
.global vPortYieldISR
.global vT1InterruptHandler


.section .FreeRTOS, "ax", @progbits
.set noat



jal vPortIncrementTick


.end vT1InterruptHandler

Clears the interrupt

/* Clear the interrupt in the interrupt controller. */
las6, IFS0CLR
addius4, zero, 2
sws4, (s6)


Setup code

/* Place the prototype here to ensure the interrupt vector is correctly installed. */
extern void __attribute__( (interrupt(ipl1), vector(_TIMER_2_VECTOR))) vT1InterruptHandler( void );

* Setup a timer for a regular tick.
void prvSetupTimerInterrupt( void )
const unsigned portLONG ulCompareMatch = ( (configPERIPHERAL_CLOCK_HZ / portTIMER_PRESCALE) / configTICK_RATE_HZ ) - 1;

OpenTimer2( ( T2_ON | T2_PS_1_8 | T2_SOURCE_INT ), ulCompareMatch );

RE: Changint the timer in the PIC32 Port

Posted by Mariusz Murawski on June 28, 2009
Either way, there's a small bug in a port.c file for PIC32:

void prvSetupTimerInterrupt( void )
const unsigned portLONG ulCompareMatch = ( (configPERIPHERAL_CLOCK_HZ / portTIMER_PRESCALE) / configTICK_RATE_HZ ) - 1;

OpenTimer1( ( T1_ON | T1_PS_1_8 | T1_SOURCE_INT ), ulCompareMatch );


According to Microchip libraries documentation, OpenTimerX expects 16bit value as a second parameter thus maximum value is 65535.

@PeripheralClockHz == 80000000 (80MHz) maximum configTICK_RATE_HZ would be then 152 Hz. If you use a value lower than 152, then the real tick rate will be different than expected.

I guess it would be good to give people chance to use 1/64 divider.

[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]

Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.

Free TCP/IP and file system demos for the RTOS

Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists