/*---------------------------------------------------------------------------* Project: CtrBrom - libraries - OS File: os_tick.c Copyright 2008 Nintendo. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Date:: $ $Rev$ $Author$ *---------------------------------------------------------------------------*/ #include //---------------------------------------------------------------------- //---- timer number tick uses #define OSi_TICK_TIMER OS_TIMER_0 #ifdef SDK_ARM11 //---- timer interrupt ID #define OSi_TICK_IE_TIMER_ID OS_INTR_ID_TIMER #else // SDK_ARM9 //---- timer interrupt ID #define OSi_TICK_IE_TIMER_ID OS_INTR_ID_TIMER0 //---- timer interrupt mask (must be same number with OSi_TICK_TIMER) #define OSi_TICK_IE_TIMER REG_OS_IF_T0_MASK //---- timer control setting for tick #define OSi_TICK_TIMERCONTROL ( REG_OS_TM0CNT_H_E_MASK | REG_OS_TM0CNT_H_I_MASK | OS_TIMER_PRESCALER_64 ) #endif // SDK_ARM9 //---- flag for initialization tick static u16 i_osUseTick = FALSE; //---- tick counter vu64 i_osTickCounter; //---- flag for need to re-set timer BOOL i_osNeedResetTimer = FALSE; extern u16 i_osIsTimerReserved(int timerNum); extern void i_osSetTimerReserved(int timerNum); static void i_osCountUpTick(void); /*---------------------------------------------------------------------------* Name: osInitTick Description: initialize 'tick system' Arguments: None Returns: None *---------------------------------------------------------------------------*/ void osInitTick(void) { if (!i_osUseTick) { i_osUseTick = TRUE; //---- disable timer interrupt osDisableInterruptID(OSi_TICK_IE_TIMER_ID); osInitInterrupt(); osInitTimer(); i_osTickCounter = 0; //---- OS reserves OSi_TICK_TIMER timer SDK_ASSERT(!i_osIsTimerReserved(OSi_TICK_TIMER)); i_osSetTimerReserved(OSi_TICK_TIMER); #ifdef SDK_ARM11 osStopTimer(OSi_TICK_TIMER); osEnableTimerReload(OSi_TICK_TIMER); osStartTimer(OSi_TICK_TIMER, OS_TICK_LO_LIMIT, 0); #else // SDK_ARM9 //---- setting timer osSetTimerControl(OSi_TICK_TIMER, 0); osSetTimerCount((OSTimer)OSi_TICK_TIMER, (u16)0); osSetTimerControl(OSi_TICK_TIMER, (u16)OSi_TICK_TIMERCONTROL); #endif // SDK_ARM9 //---- set interrupt callback osSetInterruptHandler( OSi_TICK_IE_TIMER_ID, i_osCountUpTick ); //---- enable timer interrupt osEnableInterruptID(OSi_TICK_IE_TIMER_ID); //---- need to reset i_osNeedResetTimer = TRUE; } } /*---------------------------------------------------------------------------* Name: osIsTickAvailable Description: check tick system is available Arguments: None Returns: if available, TRUE. *---------------------------------------------------------------------------*/ BOOL osIsTickAvailable(void) { return i_osUseTick; } /*---------------------------------------------------------------------------* Name: i_osCountUpTick Description: timer interrupt handle. Arguments: None Returns: None *---------------------------------------------------------------------------*/ static void i_osCountUpTick(void) { i_osTickCounter++; //---- setting for timer if (i_osNeedResetTimer) { #ifdef SDK_ARM11 osStopTimer(OSi_TICK_TIMER); osStartTimer(OSi_TICK_TIMER, OS_TICK_LO_LIMIT, 0); #else // SDK_ARM9 osSetTimerControl(OSi_TICK_TIMER, 0); osSetTimerCount((OSTimer)OSi_TICK_TIMER, (u16)0); osSetTimerControl(OSi_TICK_TIMER, (u16)OSi_TICK_TIMERCONTROL); #endif // SDK_ARM9 i_osNeedResetTimer = FALSE; } // これはやらなくても、毎回コールバックがかかるから問題ない? // //---- reset callback // OSi_EnterTimerCallback(OSi_TICK_TIMER, (void (*)(void *))i_osCountUpTick, 0); } /*---------------------------------------------------------------------------* Name: osGetTick Description: get tick value Arguments: None Returns: tick value *---------------------------------------------------------------------------*/ u64 osGetTick(void) { vu32 countL; vu64 countH; OSIntrMode prev = osDisableInterrupts(); SDK_ASSERT(i_osUseTick); countL = osGetTimerCount(OSi_TICK_TIMER); #ifdef SDK_ARM11 countL = OS_TICK_LO_LIMIT - countL; #endif // SDK_ARM11 countH = i_osTickCounter; //---- check if timer interrupt bit is on if (osIsInterruptPending(OSi_TICK_IE_TIMER_ID) && // countLが0xFFFFで直後に割り込み要求が来た場合の対策 #ifdef SDK_ARM11 // ダウンカウンタなので (countL & OS_TICK_LO_MSB) #else // SDK_ARM9 // アップカウンタなので !(countL & OS_TICK_LO_MSB) #endif // SDK_ARM9 ) { countH++; } countH <<= OS_TICK_HI_SHIFT; (void)osRestoreInterrupts(prev); return countH | countL; } /*---------------------------------------------------------------------------* Name: osGetTickLo Description: get tick value (only u16 part) Arguments: None Returns: tick value (only u16 part) *---------------------------------------------------------------------------*/ OSTimerCount osGetTickLo(void) { SDK_ASSERT(OSi_UseTick); return osGetTimerCount(OSi_TICK_TIMER); } /*---------------------------------------------------------------------------* Name: i_osSetTick Description: set tick value Arguments: count value of tick to be set Returns: None *---------------------------------------------------------------------------*/ void i_osSetTick(u64 count) { OSIntrMode prev; SDK_ASSERT(i_osUseTick); prev = osDisableInterrupts(); i_osNeedResetTimer = TRUE; i_osTickCounter = (u64)(count >> OS_TICK_HI_SHIFT); #ifdef SDK_ARM11 osStopTimer(OSi_TICK_TIMER); osClearInterruptPendingID(OSi_TICK_IE_TIMER_ID); osStartTimer(OSi_TICK_TIMER, (u32)(count & OS_TICK_LO_MASK), 0); #else // SDK_ARM9 osSetTimerControl(OSi_TICK_TIMER, 0); osClearInterruptPendingID(OSi_TICK_IE_TIMER_ID); osSetTimerCount((OSTimer)OSi_TICK_TIMER, (u16)(count & OS_TICK_LO_MASK)); osSetTimerControl(OSi_TICK_TIMER, (u16)OSi_TICK_TIMERCONTROL); #endif // SDK_ARM9 (void)osRestoreInterrupts(prev); }