Quality RTOS & Embedded Software

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


Loading

LPC2000Port: Error in Timer0Setup

Posted by Werner Schmitt on February 28, 2006
in the LPC2000 portable Files for every Compiler we find in the port.c file:

static void prvSetupTimerInterrupt( void )
{
unsigned portLONG ulCompareMatch;

/* A 1ms tick does not require the use of the timer prescale. This is
defaulted to zero but can be used if necessary. */
T0_PR = portPRESCALE_VALUE;

/* Calculate the match value required for our wanted tick rate. */
ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;

/* Protect against divide by zero. Using an if() statement still results
in a warning - hence the #if. */
#if portPRESCALE_VALUE != 0
{
ulCompareMatch /= portPRESCALE_VALUE;
ERROR: -> xxxxxxxxxxxxxxxxxxxxxxx
}
#endif



because in the manual is written:

The Prescale
Counter is incremented on every pclk. When it reaches the value stored in the Prescale Register, the Timer Counter is
incremented and the Prescale Counter is reset on the next pclk. This causes the TC to increment on every pclk when PR = 0,
every 2 pclks when PR = 1, etc.

so it should be:

ulCompareMatch /= portPRESCALE_VALUE+1;



RE: LPC2000Port: Error in Timer0Setup

Posted by Richard on February 28, 2006
Thanks for the correction. FreeRTOS V4.0.0 will be out in a couple of weeks, I will make the mod.

Regards.

RE: LPC2000Port: Error in Timer0Setup

Posted by Patrick on July 4, 2007
I believe there is another error (off-by-one) in Timer0Setup, but it is very small for typical LPC peripheral clock rates.

Currently, the Timer0 match register (MR0) is set as follows:

T0_PR = portPRESCALE_VALUE;

/* Calculate the match value required for our wanted tick rate. */
ulCompareMatch = configPCLK_HZ / configTICK_RATE_HZ;

/* Protect against divide by zero. Using an if() statement still results
in a warning - hence the #if. */
#if portPRESCALE_VALUE != 0
{
ulCompareMatch /= ( portPRESCALE_VALUE + 1 );
}
#endif
T0_MR0 = ulCompareMatch;

According to the LPC213x data sheets, the timer/counter resets to zero at then end of the match, so the total number of timer/counter ticks will be (T0_MR0 + 1) (except when T0_MR0 is 0). As a quick test, I used T1_MR0 with a prescaler such that the timer1 ticked at 1 Hz, and set the T1_MR0 register to 1 to verify that the reset happens after the match value has completed its cycle.

To fix this, T0_MR0 should be set as follows:

T0_MR0 = ulCompareMatch - 1;

Note that I did not include a check for validity of the final result (ie. non-zero match register) in the above assignment, but that some form of error reporting should be included there.


RE: LPC2000Port: Error in Timer0Setup

Posted by Oliver on April 17, 2009
I just found that this off-by-one error for T0_MR0 found by Patrick is still in the current 5.2.0 LPC200 port.

When fixing, I also suggest to simplify the function to:

/* A 1ms tick does not require the use of the timer prescale. This is
defaulted to zero but can be used if necessary. */
T0_PR = portPRESCALE_VALUE;

/* Calculate the match value required for our wanted tick rate. */
ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ / (portPRESCALE_VALUE + 1) - 1;

T0_MR0 = ulCompareMatch;

The #if portPRESCALE_VALUE != 0 is not needed because it is incremented by one anyway. Maybe check it against beeing -1.

Regards,
Oliver


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




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

Latest News

FreeRTOS kernel V10.0.1 is available for immediate download. Now MIT licensed.

Video: Watch James Gosling & Richard Barry at re:Invent, Las Vegas 2017.

New FAQ page about the FreeRTOS kernel and Amazon FreeRTOS.


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

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