/*---------------------------------------------------------------------------* 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 control setting for tick #define OSi_TICK_TIMERCONTROL ( REG_OS_TM0CNT_H_E_MASK | REG_OS_TM0CNT_H_I_MASK | OS_TIMER_PRESCALER_64 ) //---- timer number tick uses #define OSi_TICK_TIMER OS_TIMER_3 //---- timer interrupt mask (must be same number with OSi_TICK_TIMER) #define OSi_TICK_IE_TIMER REG_OS_IF_T3_MASK //---- timer interrupt ID #define OSi_TICK_IE_TIMER_ID OS_INTR_ID_TIMER3 //---- 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; //---- OS reserves OSi_TICK_TIMER timer SDK_ASSERT(!i_osIsTimerReserved(OSi_TICK_TIMER)); i_osSetTimerReserved(OSi_TICK_TIMER); //---- setting timer i_osTickCounter = 0; i_osSetTimerControl(OSi_TICK_TIMER, 0); i_osSetTimerCount((OSTimer)OSi_TICK_TIMER, (u16)0); i_osSetTimerControl(OSi_TICK_TIMER, (u16)OSi_TICK_TIMERCONTROL); //---- set interrupt callback osSetInterruptHandler( OS_INTR_ID_TIMER3, i_osCountUpTick ); //---- enable timer interrupt osEnableInterruptID(OSi_TICK_IE_TIMER_ID); //---- need to reset i_osNeedResetTimer = FALSE; } } /*---------------------------------------------------------------------------* 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) { i_osSetTimerControl(OSi_TICK_TIMER, 0); i_osSetTimerCount((OSTimer)OSi_TICK_TIMER, (u16)0); i_osSetTimerControl(OSi_TICK_TIMER, (u16)OSi_TICK_TIMERCONTROL); 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) { vu16 countL; vu64 countH; OSIntrMode prev = osDisableInterrupts(); SDK_ASSERT(i_osUseTick); countL = *(REGType16 *)((u32)REG_TM0CNT_L_ADDR + OSi_TICK_TIMER * 4); countH = i_osTickCounter & 0xffffffffffffULL; //---- check if timer interrupt bit is on if (reg_OS_IF & OSi_TICK_IE_TIMER && !(countL & 0x8000)) { countH++; } (void)osRestoreInterrupts(prev); return (countH << 16) | countL; } /*---------------------------------------------------------------------------* Name: osGetTickLo Description: get tick value (only u16 part) Arguments: None Returns: tick value (only u16 part) *---------------------------------------------------------------------------*/ u16 osGetTickLo(void) { SDK_ASSERT(OSi_UseTick); return *(REGType16 *)((u32)REG_TM0CNT_L_ADDR + OSi_TICK_TIMER * 4); } /*---------------------------------------------------------------------------* Name: osSetTick Description: set tick value Arguments: count value of tick to be set Returns: None *---------------------------------------------------------------------------*/ void osSetTick(u64 count) { OSIntrMode prev; SDK_ASSERT(i_osUseTick); prev = osDisableInterrupts(); reg_OS_IF = OSi_TICK_IE_TIMER; i_osNeedResetTimer = TRUE; i_osTickCounter = (u64)(count >> 16); i_osSetTimerControl(OSi_TICK_TIMER, 0); i_osSetTimerCount((OSTimer)OSi_TICK_TIMER, (u16)(count & 0xffff)); i_osSetTimerControl(OSi_TICK_TIMER, (u16)OSi_TICK_TIMERCONTROL); (void)osRestoreInterrupts(prev); }