I need to reset (set to 0) Special Purpose Register 527 (Alt Time Base register upper). But this function sysAltTimeBaseUreset crashes my VxWorks target's PowerPC (Freescale P2020):
FUNC_BEGIN(sysAltTimeBaseUreset)
mtspr 527, 0x0000 /* reset high 32-bits of Alt. Time Base register */
bclr 20,0 /* Return to caller */
FUNC_END(sysAltTimeBaseUreset)
The following function works ok, and returns the contents of the AltTimeBase upper register.
FUNC_BEGIN(sysAltTimeBaseUGet)
mfspr r3, 527 /* high 32-bits of Alt. Time Base register */
bclr 20,0 /* Return to caller */
FUNC_END(sysAltTimeBaseUGet)
Writing to the time base registers is reserved for supervisor-level software. User-level software is not permitted to write to the time base registers, and it is generally sufficient to read the registers at two different times and subtract to find the elapsed time.
Additionally, you should not just write the upper time base register, because it changes whenever the lower time base register wraps. The recommended procedure for setting the registers is to set the lower time base register to zero (which prevents a wrap from occurring within the next 232 ticks of the time base), then set the upper time base register to the desired value, then set the lower time base register to the desired value. (When reading, you should read the upper register, read the lower register, and read the upper register again. If the upper register changed, you must discard the value from the lower register and repeat.)
Also, it is recommended to use the mnemonics for writing to the time base registers, mttbu
and mttbl
, rather than hard-coded special register numbers. I do not recall the details, but some PowerPC implementations had echoes of the time base registers at alternate register numbers, where I think they could be read but not written.