Telink TLSR8232 BLE SDK Developer Handbook
AN-19112700-E1
39
Ver.1.0.0
2.2.2 System Timer Usage
After the Main
function “cpu_wakeup_init” is initialized System Timer starts running, and
users can read the counter value of System Timer
(“System Timer tick” for short).
The 32-bit System Timer tick will increase by 1 for each clock cycle (i.e. 1/16us). It takes
268 seconds or so (i.e. (1/16) us * (2^32)) for the system tick to loop from the initial value
0x00000000 to the maximum value 0xffffffff.
The System Timer
tick won’t stop counting during MCU running process.
The System Timer tick value can be obtained by
function “clock_time()”, for instance,
recording current system tick:
u32 current_tick = clock_time();
F
unction “clock_time()” actually reads the value counted by System Timer.
5316 BLE SDK uses System Timer tick massively to time and judge timeout.
It’s highly
recommended to use the System Timer tick to implement simple timing and timeout
judgment.
The software timer based on query mechanism cannot ensure high real-time and
accuracy. Generally it applies to applications which have not very harsh error
requirement. The usage of the software timer is shown as below:
1) Start timing: Set an u32 variable, read and record current System Timer tick.
u32 start_tick = clock_time();
// clock_time() returns System Timer tick value
2) Continuously query if the difference between current System Timer tick and start_tick
exceeds the timing value at somewhere of the firmware. If yes, the timer is triggered
to execute corresponding operation, and clear timer or start a new timing cycle as
needed. Suppose the timing value is 100ms, for 16MHz system clock, the following
sentence can be used to query the timer:
if( (u32) ( clock_time() - start_tick) > 100 * 1000 * 16)
The difference is switched to u32 type to slove the extreme case that System Timer
tick counts from 0xffffffff to 0.
In SDK, a unified calling function to solve the u32 switching problem caused by
different system clocks. No matter how many system clocks there are, the function
below can be used for query:
if( clock_time_exceed(start_tick,100 * 1000))
// unit of the second parameter is
us, it’s unnecessary to worry about “16” and “32”.
Please note that for 16M clock this function only applies to timing within 268s, if
exceeds, a counter must be added to the software.
Application example: 2 seconds after condition A is triggered (only once), B()
operation is executed.
u32 a_trig_tick;
int a_trig_flg = 0;
while(1)
{
if(A){