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




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) 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