Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


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 ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers


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

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

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner