Changint the timer in the PIC32 Port

gadkins wrote on Monday, 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    nomips16
     .set     noreorder
     .extern pxCurrentTCB
     .extern vTaskSwitchContext
     .extern vPortIncrementTick
    .extern xISRStackTop
     .global vPortStartFirstTask
    .global vPortYieldISR
    .global vT1InterruptHandler


     .section     .FreeRTOS, "ax", @progbits
     .set        noreorder
    .set         noat
     .ent        vT1InterruptHandler


    jal         vPortIncrementTick


    .end vT1InterruptHandler

Clears the interrupt

    /* Clear the interrupt in the interrupt controller. */
    la            s6, IFS0CLR
    addiu        s4, zero, 2
    sw            s4, (s6)

    jal            vTaskSwitchContext

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 );
    ConfigIntTimer2( T2_INT_ON | configKERNEL_INTERRUPT_PRIORITY );

marmur99 wrote on Sunday, 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 );

    ConfigIntTimer1( T1_INT_ON | configKERNEL_INTERRUPT_PRIORITY );

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.