module HplAtm128Timer0AsyncP {
provides {
- interface Init @atleastonce();
// 8-bit Timers
interface HplAtm128Timer<uint8_t> as Timer;
interface HplAtm128TimerCtrl8 as TimerCtrl;
interface HplAtm128Compare<uint8_t> as Compare;
interface McuPowerOverride;
+ interface HplAtm128TimerAsync as TimerAsync;
}
}
implementation
{
- command error_t Init.init() {
- SET_BIT(ASSR, AS0); // set Timer/Counter0 to asynchronous mode
- return SUCCESS;
- }
-
//=== Read the current timer value. ===================================
async command uint8_t Timer.get() { return TCNT0; }
//=== Set/clear the current timer value. ==============================
async command void Timer.set(uint8_t t) {
- while (ASSR & 1 << TCN0UB)
- ;
TCNT0 = t;
}
//=== Write the control registers. ====================================
async command void TimerCtrl.setControl( Atm128TimerControl_t x ) {
- while (ASSR & 1 << TCR0UB)
- ;
TCCR0 = x.flat;
}
//=== Write the compare registers. ====================================
async command void Compare.set(uint8_t t) {
- atomic
- {
- while (ASSR & 1 << OCR0UB)
- ;
- OCR0 = t;
- }
+ OCR0 = t;
}
//=== Timer interrupts signals ========================================
- void stabiliseTimer0() {
+ inline void stabiliseTimer0() {
TCCR0 = TCCR0;
while (ASSR & 1 << TCR0UB)
;
// need to wait for timer 0 updates propagate before sleeping
// (we don't need to worry about reentering sleep mode too early,
// as the wake ups from timer0 wait at least one TOSC1 cycle
- // anyway - see the stabiliseTimer0 function in HplAtm128Timer0AsyncC)
+ // anyway - see the stabiliseTimer0 function)
while (ASSR & (1 << TCN0UB | 1 << OCR0UB | 1 << TCR0UB))
;
diff = OCR0 - TCNT0;
stabiliseTimer0();
signal Timer.overflow();
}
+
+ // Asynchronous status register support
+ async command Atm128Assr_t TimerAsync.getAssr() {
+ return *(Atm128Assr_t *)&ASSR;
+ }
+
+ async command void TimerAsync.setAssr(Atm128Assr_t x) {
+ ASSR = x.flat;
+ }
+
+ async command void TimerAsync.setTimer0Asynchronous() {
+ ASSR |= 1 << AS0;
+ }
+
+ async command bool TimerAsync.controlBusy() {
+ return (ASSR & (1 << TCR0UB)) != 0;
+ }
+
+ async command bool TimerAsync.compareBusy() {
+ return (ASSR & (1 << OCR0UB)) != 0;
+ }
+
+ async command bool TimerAsync.countBusy() {
+ return (ASSR & (1 << TCN0UB)) != 0;
+ }
}