Skip to content

Commit 1c83f02

Browse files
fikinmarcelstoer
authored andcommitted
Expose CPU CCOUNT register as tmr function (#2906)
1 parent 5278944 commit 1c83f02

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

app/modules/tmr.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ static int tmr_now(lua_State* L){
142142
return 1;
143143
}
144144

145+
// Lua: tmr.ccount() , returns CCOUNT register
146+
static int tmr_ccount( lua_State* L )
147+
{
148+
lua_pushinteger(L, CCOUNT_REG);
149+
return 1;
150+
}
151+
145152
static tmr_t tmr_get( lua_State *L, int stack ) {
146153
tmr_t t = (tmr_t)luaL_checkudata(L, stack, "tmr.timer");
147154
if (t == NULL)
@@ -392,6 +399,7 @@ LROT_BEGIN(tmr)
392399
LROT_FUNCENTRY( wdclr, tmr_wdclr )
393400
LROT_FUNCENTRY( softwd, tmr_softwd )
394401
LROT_FUNCENTRY( time, tmr_time )
402+
LROT_FUNCENTRY( ccount, tmr_ccount )
395403
#ifdef TIMER_SUSPEND_ENABLE
396404
LROT_FUNCENTRY( suspend_all, tmr_suspend_all )
397405
LROT_FUNCENTRY( resume_all, tmr_resume_all )

app/platform/platform.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,4 +372,7 @@ platform_task_handle_t platform_task_get_id(platform_task_callback_t t);
372372

373373
bool platform_post(uint8 prio, platform_task_handle_t h, platform_task_param_t par);
374374

375+
// Get current value of CCOUNt register
376+
#define CCOUNT_REG ({ int32_t r; asm volatile("rsr %0, ccount" : "=r"(r)); r;})
377+
375378
#endif

docs/modules/tmr.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,42 @@ none
145145
#### Returns
146146
`nil`
147147

148+
## tmr.ccount()
149+
150+
Get value of CPU CCOUNT register which contains CPU ticks. The register is 32-bit and rolls over.
151+
152+
Converting the register's CPU ticks to us is done by dividing it to 80 or 160 (CPU80/CPU160) i.e. `tmr.ccount() / node.getcpufreq()`.
153+
154+
Register arithmetic works without need to account for roll over, unlike `tmr.now()`. Because of same reason when CCOUNT is having its 32nd bit set, it appears in Lua as negative number.
155+
156+
#### Syntax
157+
`tmr.ccount()`
158+
159+
#### Returns
160+
The current value of CCOUNT register.
161+
162+
#### Example
163+
```lua
164+
function timeIt(fnc, cnt)
165+
local function loopIt(f2)
166+
local t0 = tmr.ccount()
167+
for i=1,cnt do
168+
f2()
169+
end
170+
local t1 = tmr.ccount()
171+
return math.ceil((t1-t0)/cnt)
172+
end
173+
assert(type(fnc) == "function", "function to test missing")
174+
cnt = cnt or 1000
175+
local emptyTime = loopIt(function()end)
176+
local deltaCPUTicks = math.abs(loopIt(fnc) - emptyTime)
177+
local deltaUS = math.ceil(deltaCPUTicks/node.getcpufreq())
178+
return deltaCPUTicks, deltaUS
179+
end
180+
181+
print( timeIt(function() tmr.ccount() end) )
182+
```
183+
148184
## Timer Object Methods
149185

150186
### tobj:alarm()

0 commit comments

Comments
 (0)