Tick Functions. More...
Go to the source code of this file.
Macros | |
#define | __INLINE_FUNCTION__ extern inline __attribute__((always_inline)) |
#define | nzINT_PRIORITY_TICK ( 5 ) |
#define | nzINT_PRIORITY_UTICK ( 6 ) |
#define | NZSYS_TICK_TMR2AND3_UTICK ( 0 ) |
#define | tickService() |
#define | tick16TestTmr(tmr) ( (((((tmr) - (tick_val.w[0]) ) & 0x8000))==0) ? 0 : 1) |
#define | tick16SetTmrMS(tmr, msVal) (tmr = (tick16Get() + msVal)) |
#define | tick16GetElapsedMS(oldTick) ((WORD)((tick_val.w[0]) - (oldTick))) |
#define | tick16UpdateTmrMS(tmr, msVal) (tmr += msVal) |
#define | tick16ConvertFromMS(msVal) (msVal) |
#define | tick32Get_noDisi() (tick_val.Val) |
#define | tick32SetTmrMS(tmr, msVal) (tmr = (tick32Get() + msVal)) |
#define | tick32TestTmr_noDisi(tmr) ((((tmr-tick_val.Val)&0x80000000)==0) ? 0 : 1) |
#define | tick32UpdateTmrMS(tmr, msVal) (tmr += msVal) |
#define | tick32UpdateTmrSec(tmr, secVal) (tmr += (((DWORD)secVal)*1000)) |
#define | tick32UpdateTmrMin(tmr, minVal) (tmr += ( ((DWORD)minVal)*60000)) |
#define | tick32ConvertFromMS(msVal) ((DWORD)(msVal)) |
#define | utick16TestTmr(tmr) ( (((((tmr) - (utick16Get()) ) & 0x8000))==0) ? 0 : 1) |
#define | utick16GetElapsedUS(oldTick) utick16ConvertToUS(((WORD)((utick16Get()) - (oldTick)))) |
#define | utick16ConvertToUS(utickVal) ( ((WORD)utickVal) / ((WORD)(NZ_UTICKS_PER_MS/1000)) ) |
#define | utick16ConvertFromUS(usVal) (WORD)( ((WORD)usVal) * (NZ_UTICKS_PER_MS/1000) ) |
#define | utick32TestTmr(tmr) ((((tmr-utick32Get())&0x80000000)==0) ? 0 : 1) |
#define | utick32ConvertToUS(utickVal) ( ((DWORD)utickVal) / ((DWORD)(NZ_UTICKS_PER_MS/1000)) ) |
#define | utick32ConvertFromUS(usVal) (DWORD)( ((DWORD)usVal) * (NZ_UTICKS_PER_MS/1000) ) |
Typedefs | |
typedef WORD | TICK16 |
typedef DWORD | TICK32 |
Functions | |
void | tickInit (void) |
TICK16 | tick16Get (void) |
DWORD | tick32Get (void) |
BOOL | tick32TestTmr (DWORD tmr) |
WORD | utick16Get (void) |
WORD | utick16Get_noDisi (void) |
DWORD | utick32Get (void) |
DWORD | utick32Get_noDisi (void) |
WORD | tick16Get_8us (void) |
WORD | tick16Get_8us_noDisi (void) |
WORD | tick32Get_8us (void) |
WORD | tick32Get_8us_noDisi (void) |
Variables | |
volatile DWORD_VAL | tick_val |
Tick Functions.
To use this module:
The system tick uses Timer 1, and has a period of 1ms. Each 1ms an interrupt is triggered, and the 32-bit system tick is incremented. To make code smaller and more efficient, default tick functions use the lower 16 bits of the 32-bit system tick. This limits the default functions to a maximum time of 32,768ms = 32.7 seconds. If this is not sufficient, the 32-bit tick functions (tick32Xxx) can be used, which have a maximum time of 4,294,967,296 ms = 1,193 hours = 49.7 Days.
The 16-bit variants are the most efficient, and create the smallest and fastest code. Only use the 32-bit variants when required!
Example default tick (16-bit, 1ms). Can be used for a maximum of 32,768 ms = 32 seconds delay. Requires 16-bit timer variable, which is the native data width of the processor! Produces fastest code.
An alternative method is:
Example using 32-bit, 1ms tick. Can be used for a maximum of 4,294,967,296 ms = 1,193 hours = 49.7 Days delay. Requires 32-bit timer variable, generates more code, and is slower than other 8 and 16-bit functions.
The following defines are used to configure this module, and should be placed in projdefs.h. Note that all items marked [-DEFAULT-] are defaults, and do not have to be placed in projdefs.h if they contain desired configuration! For details, see Project Configuration.
The software supplied herewith is owned by Modtronix Engineering, and is protected under applicable copyright laws. The software supplied herewith is intended and supplied to you, the Company customer, for use solely and exclusively on products manufactured by Modtronix Engineering. The code may be modified and can be used free of charge for commercial and non commercial applications. All rights are reserved. Any use in violation of the foregoing restrictions may subject the user to criminal sanctions under applicable laws, as well as to civil liability for the breach of the terms and conditions of this license.
THIS SOFTWARE IS PROVIDED IN AN 'AS IS' CONDITION. NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
File History
2012-08-08, David Hosken (DH):
#define tick16ConvertFromMS | ( | msVal) | (msVal) |
Convert given value to a tick millisecond value. Seeing that the system tick is 1ms, no conversion is required (1 to 1).
For example, to initialize a timer with a value that will expire in 500 ms, following code can be used.
msVal | Value to convert to a tick milliseconds value |
#define tick16GetElapsedMS | ( | oldTick) | ((WORD)((tick_val.w[0]) - (oldTick))) |
Returns the time in milliseconds that has elapsed since the given old tick value.
For example, to calculate how long some code takes:
oldTick | Old TICK16 value |
#define tick16SetTmrMS | ( | tmr, | |
msVal | |||
) | (tmr = (tick16Get() + msVal)) |
Set the given timer to a time in the future, as given by msVal parameter in milliseconds.
tmr | Timer to add set expirty time for |
msVal | Amount of milliseconds in which the timer will expire |
#define tick16TestTmr | ( | tmr) | ( (((((tmr) - (tick_val.w[0]) ) & 0x8000))==0) ? 0 : 1) |
Tests if the current 16-bit(1ms) tick has passed the given timer value.
Will return true once the 16 bit(1ms) tick is > given value. The given timer can have a maximum value of 32,768ms = 32 seconds.
tmr | Timer to test |
#define tick16UpdateTmrMS | ( | tmr, | |
msVal | |||
) | (tmr += msVal) |
Add the given amount of milliseconds to the given timer. Updates the time when it will expire.
The given time is added to the current timer value! Ensure current timer has a valid value before calling this function!
tmr | Timer to add the given milliseconds too |
msVal | Amount of milliseconds to add to timer |
#define tick32ConvertFromMS | ( | msVal) | ((DWORD)(msVal)) |
Convert given value to a tick millisecond value. Seeing that the system tick is 1ms, no conversion is required (1 to 1).
For example, to initialize a timer with a value that will expire in 500 ms, following code can be used.
msVal | Value to convert to a tick milliseconds value |
#define tick32Get_noDisi | ( | ) | (tick_val.Val) |
Same as tick32Get() but does not disable interrupts. Only call this function from an ISR with higher priority than the system tick. That is an ISR with priority 5 - 7, for default system tick with priority 4.
#define tick32SetTmrMS | ( | tmr, | |
msVal | |||
) | (tmr = (tick32Get() + msVal)) |
Set the given timer to a time in the future, as given by msVal parameter in milliseconds.
tmr | Timer to add set expirty time for |
msVal | Amount of milliseconds in which the timer will expire |
#define tick32TestTmr_noDisi | ( | tmr) | ((((tmr-tick_val.Val)&0x80000000)==0) ? 0 : 1) |
Same as tick32TestTmr, but does not disable interrupts Only call this function from an ISR with higher priority than the system tick. That is an ISR with priority 5 - 7, for default system tick with priority 4.
tmr | Given value to compare it |
#define tick32UpdateTmrMin | ( | tmr, | |
minVal | |||
) | (tmr += ( ((DWORD)minVal)*60000)) |
Update the given timer to expire in the given number of minutes.
tmr | Timer to add the given milliseconds too |
minVal | Amount of minutes to add to timer |
#define tick32UpdateTmrMS | ( | tmr, | |
msVal | |||
) | (tmr += msVal) |
Add the given amount of milliseconds to the given timer. Updates the time when it will expire.
The given time is added to the current timer value! Ensure current timer has a valid value before calling this function!
tmr | Timer to add the given milliseconds too |
msVal | Amount of milliseconds to add to timer |
#define tick32UpdateTmrSec | ( | tmr, | |
secVal | |||
) | (tmr += (((DWORD)secVal)*1000)) |
Update the given timer to expire in the given amount of seconds.
The given time is added to the current timer value! Ensure current timer has a valid value before calling this function!
tmr | Timer to add the given milliseconds too |
secVal | Amount of seconds to add to timer |
#define tickService | ( | ) |
Increments all tick values when called
#define utick16ConvertFromUS | ( | usVal) | (WORD)( ((WORD)usVal) * (NZ_UTICKS_PER_MS/1000) ) |
Converts the given micro second value to system "16-bit Micro Tick".
usVal | The micro second value |
#define utick16ConvertToUS | ( | utickVal) | ( ((WORD)utickVal) / ((WORD)(NZ_UTICKS_PER_MS/1000)) ) |
Convert given utick value to micro seconds.
utickVal | Value to convert to micro seconds |
#define utick16GetElapsedUS | ( | oldTick) | utick16ConvertToUS(((WORD)((utick16Get()) - (oldTick)))) |
Returns the time in microseconds that has elapsed since the given old tick value. Seeing that this is a 16-bit function, maximum value of difference in uticks is 32,768. For the default SBC66 boards there are 2 ticks per US (2000 ticks per millisecond), meaning maximum elapsed time that can be measured is 32,768/2 = 16,384
For example, to calculate how long some code takes:
oldTick | Old TICK16 value |
#define utick16TestTmr | ( | tmr) | ( (((((tmr) - (utick16Get()) ) & 0x8000))==0) ? 0 : 1) |
Tests if the current 16-bit utick(micro tick) has passed the given timer's value.
Will return true once the 16 bit utick is > given value. Can have a maximum value of 32,768
tmr | Timer to test |
#define utick32ConvertFromUS | ( | usVal) | (DWORD)( ((DWORD)usVal) * (NZ_UTICKS_PER_MS/1000) ) |
Converts the given micro second value to system "32-bit Micro Tick".
usVal | The micro second value |
#define utick32ConvertToUS | ( | utickVal) | ( ((DWORD)utickVal) / ((DWORD)(NZ_UTICKS_PER_MS/1000)) ) |
Convert given utick value to micro seconds.
utickVal | Value to convert to micro seconds |
#define utick32TestTmr | ( | tmr) | ((((tmr-utick32Get())&0x80000000)==0) ? 0 : 1) |
Tests if the current 32-bit utick(micro tick) has passed the given timer's value.
For efficiency and code size, try to use utick16 functions where possible (utick16TestTmr() for example).
Will return true once the 32 bit tick is > given value. Can have a maximum value of 2,147,483,648
tmr | Timer to test |
|
inline |
Current default tick, which is the 16-bit, 1ms tick. Has a resolution of 1ms. Has a maximum value of 32,768ms = 32.7 seconds
WORD tick16Get_8us | ( | void | ) |
Get the current 16 bit, 8us tick. Has a resolution of 8us. Has a maximum value of 262,144us = 262.1 milliseconds
|
inline |
Same as tick16Get_8us(), but does NOT disable interrupts! Only call this function from an ISR with higher priority than the system tick. That is an ISR with priority 5 - 7, for default system tick with priority 4.
DWORD tick32Get | ( | void | ) |
Get the current 32-bit, 1ms Tick. Can have a maximum value of: 4,294,967,296 ms = 1,193 hours = 49.7 Days
WORD tick32Get_8us | ( | void | ) |
Get the current 32 bit, 8us tick. Has a resolution of 8us. Has a maximum value of 17,179 sec = 286 Minutes = 4.77 Hours
|
inline |
Get the current 32 bit, 8us tick. Has a resolution of 8us. Has a maximum value of 17,179 sec = 286 Hours = 11.9 Days Only call this function from an ISR with higher priority than the system tick. That is an ISR with priority 5 - 7, for default system tick with priority 4.
BOOL tick32TestTmr | ( | DWORD | tmr) |
Tests if the current 32-bit(1ms) tick has passed the given timer's value.
For efficiency and code size, try to use tick16 functions where possible (tick16TestTmr() for example).
Will return true once the 32 bit(1ms) tick is > given value. Tick is a 1ms counter. Can have a maximum value of: 4,294,967,296 ms = 1,193 hours = 49.7 Days
tmr | Timer to test |
void tickInit | ( | void | ) |
Initializes tick values
WORD utick16Get | ( | void | ) |
A "Micro Tick" has a resolution of 1us or less. It will always have a whole number of ticks per millisecond, as defined by NZ_UTICKS_PER_MS. The following are common values: 16MHz timer with 8 prescaler = 0.5us resolution = NZ_UTICKS_PER_MS = 2000 60MHz timer with 8 prescaler = 0.13333us resolution = NZ_UTICKS_PER_MS = 7500 72MHz timer with 8 prescaler = 0.88888us resolution = NZ_UTICKS_PER_MS = 1125 80MHz timer with 8 prescaler = 0.1us resolution = NZ_UTICKS_PER_MS = 10000 80MHz timer with 64 prescaler = 0.8us resolution = NZ_UTICKS_PER_MS = 800 120MHz timer with 64 prescaler = 0.53333us resolution = NZ_UTICKS_PER_MS = 1875 Get the current 16 bit, micro tick. The NZ_UTICKS_PER_MS defines how many micro ticks there are per milli second. For current range of SBC66 boards it is always 2000.
For NZ_UTICKS_PER_MS=1000, Has a maximum value of 32,768us = 32.7 milliseconds For NZ_UTICKS_PER_MS=2000, Has a maximum value of 16,384us = 16 milliseconds
|
inline |
Same as utick16Get(), but does not disable interrupts. Only call this function from an ISR with higher priority than the system tick. That is an ISR with priority 5 - 7, for default system tick with priority 4.
DWORD utick32Get | ( | void | ) |
Get the current 32 bit, micro tick. The NZ_UTICKS_PER_MS defines how many micro ticks there are per milli second. For current range of SBC66 boards it is always 2000.
For NZ_UTICKS_PER_MS=1000, Has a maximum value of 2,147 Seconds = 35.7 Minutes For NZ_UTICKS_PER_MS=2000, Has a maximum value of 1,073 Seconds = 17.9 Minutes For NZ_UTICKS_PER_MS=10000, Has a maximum value of 214 Seconds = 3.57 Minutes
|
inline |
Same as utick32Get(), but does not disable interrupts. Only call this function from an ISR with higher priority than the system tick. That is an ISR with priority 5 - 7, for default system tick with priority 4.
volatile DWORD_VAL tick_val |
System 32-bit tick. Be very carefull if using this varialble. Ensure all operations are atomic. If not sure, use SETTO_XX macros below!