From 2b2517c7186f4ea7acaa3aad91d8cb22210b1e75 Mon Sep 17 00:00:00 2001 From: nakasima Date: Tue, 16 Dec 2008 11:21:05 +0000 Subject: [PATCH] =?UTF-8?q?=E3=82=A2=E3=83=A9=E3=83=BC=E3=83=A0=E3=83=A9?= =?UTF-8?q?=E3=82=A4=E3=83=96=E3=83=A9=E3=83=AA=E3=83=BB=E3=83=97=E3=83=AD?= =?UTF-8?q?=E3=83=88=E3=82=BF=E3=82=A4=E3=83=97=E8=BF=BD=E5=8A=A0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-09-30%20-%20paladin.7z/paladin/ctr_firmware@109 b871894f-2f95-9b40-918c-086798483c85 --- .../bootrom/build/libraries/os/ARM11/Makefile | 3 +- .../build/libraries/os/ARM11/os_timer.c | 46 +- .../bootrom/build/libraries/os/ARM9/Makefile | 1 + .../build/libraries/os/ARM9/os_timer.c | 52 +- .../build/libraries/os/common/os_alarm.c | 695 ++++++++++++++++++ .../build/libraries/os/common/os_init.c | 5 +- .../build/libraries/os/common/os_tick.c | 18 +- trunk/bootrom/include/brom/os/ARM11/timer.h | 51 +- trunk/bootrom/include/brom/os/ARM9/timer.h | 18 +- trunk/bootrom/include/brom/os/common/alarm.h | 2 +- trunk/bootrom/include/brom/os/common/tick.h | 6 +- 11 files changed, 841 insertions(+), 56 deletions(-) create mode 100644 trunk/bootrom/build/libraries/os/common/os_alarm.c diff --git a/trunk/bootrom/build/libraries/os/ARM11/Makefile b/trunk/bootrom/build/libraries/os/ARM11/Makefile index 7a97110..128c4e6 100644 --- a/trunk/bootrom/build/libraries/os/ARM11/Makefile +++ b/trunk/bootrom/build/libraries/os/ARM11/Makefile @@ -28,10 +28,11 @@ BROM_CODEGEN_ALL ?= TRUE SRCDIR = . ../common SRCS = \ - os_tick.c \ + os_alarm.c \ os_init.c \ os_system.c \ os_timer.c \ + os_tick.c \ os_irqHandler.c \ os_interrupt.c \ os_interrupt_common.c \ diff --git a/trunk/bootrom/build/libraries/os/ARM11/os_timer.c b/trunk/bootrom/build/libraries/os/ARM11/os_timer.c index 8739197..51f8b7f 100644 --- a/trunk/bootrom/build/libraries/os/ARM11/os_timer.c +++ b/trunk/bootrom/build/libraries/os/ARM11/os_timer.c @@ -23,7 +23,7 @@ static void i_osTimerInterruptHandler(void); static void i_osWatchdogInterruptHandler( void ); #endif static void i_osStartTimer( u32 count, u8 preScale, OSTimerRepeat repeat, OSTimerIntrReq ireq ); -static void i_osStartWatchDog( u32 count, u8 preScale, OSTimerRepeat repeat, OSTimerIntrReq ireq, +static void i_osStartWatchdog( u32 count, u8 preScale, OSTimerRepeat repeat, OSTimerIntrReq ireq, OSWatchdogMode watchdogMode ); static u32 osTimerClock = OS_TIMER_CLOCK_DEFAULT; @@ -50,7 +50,7 @@ void osInitTimer( void ) osTimerClock = OS_TIMER_CLOCK_DEFAULT; osStopTimer(); - osStopWatchDog(); + osStopWatchdog(); } } @@ -137,6 +137,40 @@ void osStartTimer( u32 count, u8 preScale ) i_osStartTimer( count, preScale, OS_TM_AUTO_RELOAD, OS_TM_INTR_REQ_ENABLE ); } +/*---------------------------------------------------------------------------* + Name: osStartWatchdogWithUSec + + Description: Start Watchdog + + Arguments: micro second + + Returns: None + *---------------------------------------------------------------------------*/ + +void osStartWatchdogWithUSec( u32 usec, u8 preScale, OSWatchdogMode watchdogMode ) +{ + u32 count = ((usec) * (osTimerClock / 1000)) / (preScale+1) / 1000; + + i_osStartWatchdog( count, preScale, OS_TM_SINGLE_SHOT, OS_TM_INTR_REQ_ENABLE, watchdogMode ); +} + +/*---------------------------------------------------------------------------* + Name: osStartWatchdogWithMSec + + Description: Start Watchdog + + Arguments: milli second + + Returns: None + *---------------------------------------------------------------------------*/ + +void osStartWatchdogWithMSec( u32 msec, u8 preScale, OSWatchdogMode watchdogMode ) +{ + u32 count = (((msec) * osTimerClock / 1000)) / (preScale+1); + + i_osStartWatchdog( count, preScale, OS_TM_SINGLE_SHOT, OS_TM_INTR_REQ_ENABLE, watchdogMode ); +} + /*---------------------------------------------------------------------------* Name: osStartWatchdog @@ -148,9 +182,9 @@ void osStartTimer( u32 count, u8 preScale ) Returns: None *---------------------------------------------------------------------------*/ -void osStartWatchDog( u32 count, u8 preScale, OSWatchdogMode watchdogMode ) +void osStartWatchdog( u32 count, u8 preScale, OSWatchdogMode watchdogMode ) { - i_osStartWatchDog( count, preScale, OS_TM_SINGLE_SHOT, OS_TM_INTR_REQ_DISABLE, watchdogMode ); + i_osStartWatchdog( count, preScale, OS_TM_SINGLE_SHOT, OS_TM_INTR_REQ_DISABLE, watchdogMode ); } /*---------------------------------------------------------------------------* @@ -186,7 +220,7 @@ static void i_osStartTimer( u32 count, u8 preScale, OSTimerRepeat repeat, OSTime Returns: None *---------------------------------------------------------------------------*/ -static void i_osStartWatchDog( u32 count, u8 preScale, OSTimerRepeat repeat, OSTimerIntrReq ireq, +static void i_osStartWatchdog( u32 count, u8 preScale, OSTimerRepeat repeat, OSTimerIntrReq ireq, OSWatchdogMode watchdogMode ) { reg_OS_WD_CNT = 0; @@ -223,7 +257,7 @@ void osStopTimer( void ) Returns: None *---------------------------------------------------------------------------*/ -void osStopWatchDog( void ) +void osStopWatchdog( void ) { reg_OS_WD_CNT = 0; reg_OS_WD_IF = REG_OS_WD_IF_IF_MASK; diff --git a/trunk/bootrom/build/libraries/os/ARM9/Makefile b/trunk/bootrom/build/libraries/os/ARM9/Makefile index c365378..53a400a 100644 --- a/trunk/bootrom/build/libraries/os/ARM9/Makefile +++ b/trunk/bootrom/build/libraries/os/ARM9/Makefile @@ -31,6 +31,7 @@ BROM_PROC = ARM9 SRCDIR = . ../common SRCS = \ + os_alarm.c \ os_init.c \ os_system.c \ os_timer.c \ diff --git a/trunk/bootrom/build/libraries/os/ARM9/os_timer.c b/trunk/bootrom/build/libraries/os/ARM9/os_timer.c index 0060df0..564dd57 100644 --- a/trunk/bootrom/build/libraries/os/ARM9/os_timer.c +++ b/trunk/bootrom/build/libraries/os/ARM9/os_timer.c @@ -99,7 +99,7 @@ void i_osUnsetTimerReserved( int timer_id ) //================================================================================ /*---------------------------------------------------------------------------* - Name: i_osStartTimer + Name: osStartTimer Description: set timer(s) and start @@ -112,21 +112,21 @@ void i_osUnsetTimerReserved( int timer_id ) // // use 1 timer, 16bit counter, timer interrupt occurs by overflow // -void i_osStartTimer( OSTimer id, u16 count, OSTimerPrescaler preScale ) +void osStartTimer( OSTimer id, u16 count, OSTimerPrescaler preScale ) { SDK_ASSERT(OS_TIMER_0 <= id && id <= OS_TIMER_3); SDK_ASSERT(OS_TIMER_PRESCALER_1 <= preScale && preScale <= OS_TIMER_PRESCALER_1024); //---- check if system reserved SDK_ASSERT(!i_osIsTimerReserved(id)); - i_osSetTimerCount(id, (u16)~count); - i_osSetTimerControl(id, (u16)(REG_OS_TMCNT_H_E_MASK | REG_OS_TMCNT_H_I_MASK | preScale)); + osSetTimerCount(id, (u16)~count); + osSetTimerControl(id, (u16)(REG_OS_TMCNT_H_E_MASK | REG_OS_TMCNT_H_I_MASK | preScale)); } // // use 2 timers, 32bit counter, timer interrupt occurs by overflow // -void i_osStartTimer32( OSTimer32 id, u32 count, OSTimerPrescaler preScale ) +void osStartTimer32( OSTimer32 id, u32 count, OSTimerPrescaler preScale ) { SDK_ASSERT(OS_TIMER32_01 <= id && id <= OS_TIMER32_23); SDK_ASSERT(OS_TIMER_PRESCALER_1 <= preScale && preScale <= OS_TIMER_PRESCALER_1024); @@ -134,18 +134,18 @@ void i_osStartTimer32( OSTimer32 id, u32 count, OSTimerPrescaler preScale ) SDK_ASSERT(!i_osIsTimerReserved(id)); SDK_ASSERT(!i_osIsTimerReserved(id + 1)); - i_osSetTimerCount((OSTimer)((int)id + 1), (u16)((~count >> 16) & 0xffff)); - i_osSetTimerCount((OSTimer)id, (u16)(~count & 0xffff)); + osSetTimerCount((OSTimer)((int)id + 1), (u16)((~count >> 16) & 0xffff)); + osSetTimerCount((OSTimer)id, (u16)(~count & 0xffff)); - i_osSetTimerControl((OSTimer)((int)id + 1), + osSetTimerControl((OSTimer)((int)id + 1), REG_OS_TMCNT_H_E_MASK | REG_OS_TMCNT_H_I_MASK | REG_OS_TMCNT_H_CH_MASK); - i_osSetTimerControl((OSTimer)id, (u16)(REG_OS_TMCNT_H_E_MASK | preScale)); + osSetTimerControl((OSTimer)id, (u16)(REG_OS_TMCNT_H_E_MASK | preScale)); } // // use 3 timers, 48bit counter, timer interrupt occurs by overflow // -void i_osStartTimer48( OSTimer48 id, u64 count, OSTimerPrescaler preScale ) +void osStartTimer48( OSTimer48 id, u64 count, OSTimerPrescaler preScale ) { SDK_ASSERT(OS_TIMER48_012 <= id && id <= OS_TIMER48_123); SDK_ASSERT(OS_TIMER_PRESCALER_1 <= preScale && preScale <= OS_TIMER_PRESCALER_1024); @@ -154,20 +154,20 @@ void i_osStartTimer48( OSTimer48 id, u64 count, OSTimerPrescaler preScale ) SDK_ASSERT(!i_osIsTimerReserved(id + 1)); SDK_ASSERT(!i_osIsTimerReserved(id + 2)); - i_osSetTimerCount((OSTimer)((int)id + 2), (u16)((~count >> 32) & 0xffff)); - i_osSetTimerCount((OSTimer)((int)id + 1), (u16)((~count >> 16) & 0xffff)); - i_osSetTimerCount((OSTimer)id, (u16)(~count & 0xffff)); + osSetTimerCount((OSTimer)((int)id + 2), (u16)((~count >> 32) & 0xffff)); + osSetTimerCount((OSTimer)((int)id + 1), (u16)((~count >> 16) & 0xffff)); + osSetTimerCount((OSTimer)id, (u16)(~count & 0xffff)); - i_osSetTimerControl((OSTimer)((int)id + 2), + osSetTimerControl((OSTimer)((int)id + 2), REG_OS_TMCNT_H_E_MASK | REG_OS_TMCNT_H_I_MASK | REG_OS_TMCNT_H_CH_MASK); - i_osSetTimerControl((OSTimer)((int)id + 1), REG_OS_TMCNT_H_E_MASK | REG_OS_TMCNT_H_CH_MASK); - i_osSetTimerControl((OSTimer)id, (u16)(REG_OS_TMCNT_H_E_MASK | preScale)); + osSetTimerControl((OSTimer)((int)id + 1), REG_OS_TMCNT_H_E_MASK | REG_OS_TMCNT_H_CH_MASK); + osSetTimerControl((OSTimer)id, (u16)(REG_OS_TMCNT_H_E_MASK | preScale)); } // // use all 4 timers, 64bit counter, timer3 interrupt occurs by overflow // -void i_osStartTimer64( u64 count, OSTimerPrescaler preScale ) +void osStartTimer64( u64 count, OSTimerPrescaler preScale ) { SDK_ASSERT(OS_TIMER_PRESCALER_1 <= preScale && preScale <= OS_TIMER_PRESCALER_1024); //---- check if system reserved @@ -176,16 +176,16 @@ void i_osStartTimer64( u64 count, OSTimerPrescaler preScale ) SDK_ASSERT(!i_osIsTimerReserved(OS_TIMER_2)); SDK_ASSERT(!i_osIsTimerReserved(OS_TIMER_3)); - i_osSetTimerCount(OS_TIMER_3, (u16)((~count >> 48) & 0xffff)); - i_osSetTimerCount(OS_TIMER_2, (u16)((~count >> 32) & 0xffff)); - i_osSetTimerCount(OS_TIMER_1, (u16)((~count >> 16) & 0xffff)); - i_osSetTimerCount(OS_TIMER_0, (u16)(~count & 0xffff)); + osSetTimerCount(OS_TIMER_3, (u16)((~count >> 48) & 0xffff)); + osSetTimerCount(OS_TIMER_2, (u16)((~count >> 32) & 0xffff)); + osSetTimerCount(OS_TIMER_1, (u16)((~count >> 16) & 0xffff)); + osSetTimerCount(OS_TIMER_0, (u16)(~count & 0xffff)); - i_osSetTimerControl(OS_TIMER_3, + osSetTimerControl(OS_TIMER_3, REG_OS_TMCNT_H_E_MASK | REG_OS_TMCNT_H_I_MASK | REG_OS_TMCNT_H_CH_MASK); - i_osSetTimerControl(OS_TIMER_2, REG_OS_TMCNT_H_E_MASK | REG_OS_TMCNT_H_CH_MASK); - i_osSetTimerControl(OS_TIMER_1, REG_OS_TMCNT_H_E_MASK | REG_OS_TMCNT_H_CH_MASK); - i_osSetTimerControl(OS_TIMER_0, (u16)(REG_OS_TMCNT_H_E_MASK | preScale)); + osSetTimerControl(OS_TIMER_2, REG_OS_TMCNT_H_E_MASK | REG_OS_TMCNT_H_CH_MASK); + osSetTimerControl(OS_TIMER_1, REG_OS_TMCNT_H_E_MASK | REG_OS_TMCNT_H_CH_MASK); + osSetTimerControl(OS_TIMER_0, (u16)(REG_OS_TMCNT_H_E_MASK | preScale)); } @@ -207,7 +207,7 @@ void i_osStopTimer( OSTimer id ) //---- check if system reserved SDK_ASSERT(!i_osIsTimerReserved(id)); - i_osSetTimerControl(id, 0); + osSetTimerControl(id, 0); } // diff --git a/trunk/bootrom/build/libraries/os/common/os_alarm.c b/trunk/bootrom/build/libraries/os/common/os_alarm.c new file mode 100644 index 0000000..188f5bb --- /dev/null +++ b/trunk/bootrom/build/libraries/os/common/os_alarm.c @@ -0,0 +1,695 @@ +/*---------------------------------------------------------------------------* + Project: CtrBrom - libraries - OS + File: os_alarm.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 + +#define osPanic(...) ((void)0) + +//---------------------------------------------------------------------- +#ifdef SDK_ARM11 +//---- timer interrupt ID +#define OSi_ALARM_IE_TIMER_ID OS_INTR_ID_WATCHDOG + +#else // SDK_ARM9 +//---- timer interrupt ID +#define OSi_ALARM_IE_TIMER_ID OS_INTR_ID_TIMER1 + +//---- timer interrupt mask (must be same number with OSi_ALARM_TIMER) +#define OSi_ALARM_IE_TIMER OS_IE_TIMER1 + +//---- timer number alarm uses +#define OSi_ALARM_TIMER OS_TIMER_1 + +//---- timer control setting for alarm +#define OSi_ALARM_TIMERCONTROL ( REG_OS_TM0CNT_H_E_MASK | REG_OS_TM0CNT_H_I_MASK | OS_TIMER_PRESCALER_64 ) + +#endif // SDK_ARM9 + +//---- flag for initialization alarm +static u16 i_osUseAlarm = FALSE; + +//---- alarm queue +static struct OSiAlarmQueue i_osAlarmQueue; + + +u16 i_osIsTimerReserved(int timerNum); +void i_osSetTimerReserved(int timerNum); +void i_osUnsetTimerReserved(int timerNum); + +static void i_osSetTimer(OSAlarm *alarm); +static void i_osInsertAlarm(OSAlarm *alarm, OSTick fire); + +static void i_osAlarmHandler(void *arg); +void i_osArrangeTimer(void); + + +/*---------------------------------------------------------------------------* + Name: i_osSetTimer + + Description: set Timer + + Arguments: alarm pointer to alarm to set timer + + Returns: None. + *---------------------------------------------------------------------------*/ +static void i_osSetTimer(OSAlarm *alarm) +{ + OSTick tick = osGetTick(); + + //---- let timer be disable +#ifdef SDK_ARM11 + osStopWatchdog(); +#else // SDK_ARM9 + s64 delta; + u16 timerCount; + + delta = (s64)(alarm->fire - tick); + + osSetTimerControl(OSi_ALARM_TIMER, 0); +#endif // SDK_ARM9 + + //---- set interrupt callback + osSetInterruptHandler( OSi_ALARM_IE_TIMER_ID, i_osArrangeTimer ); +// osEnterTimerCallback(OSi_ALARM_TIMER, i_osAlarmHandler, NULL); + + //---- set count and let timer be enable +#ifdef SDK_ARM11 + osStartWatchdogWithMSec(1, 0, OS_WD_TIMER_MODE); +#else // SDK_ARM9 + if (delta < 0) + { + timerCount = (u16)~1; + } + else if (delta < 0x10000) + { + timerCount = (u16)(~delta); + } + else + { + timerCount = 0; + } + +//osPrintf( "**i_osSetTimer alarm=%x, fire=%llx time=%llx delta=%lld timeCount=%x \n", alarm, alarm->fire, time, delta, (int)timerCount ); + + osSetTimerCount((OSTimer)OSi_ALARM_TIMER, timerCount); + osSetTimerControl(OSi_ALARM_TIMER, (u16)OSi_ALARM_TIMERCONTROL); +#endif // SDK_ARM9 + + //---- TIMER IRQ Enable + (void)osEnableInterruptID(OSi_ALARM_IE_TIMER_ID); +} + +/*---------------------------------------------------------------------------* + Name: osInitAlarm + + Description: Initialize alarm system + + Arguments: None. + + Returns: None. + *---------------------------------------------------------------------------*/ +void osInitAlarm(void) +{ + if (!i_osUseAlarm) + { + i_osUseAlarm = TRUE; + + //---- check if tick system is available + SDK_ASSERTMSG(osIsTickAvailable(), "osInitAlarm: alarm system needs of tick system."); + +#ifdef SDK_ARM9 + //---- OS reserves OSi_ALARM_TIMER + SDK_ASSERT(!i_osIsTimerReserved(OSi_ALARM_TIMER)); + i_osSetTimerReserved(OSi_ALARM_TIMER); +#endif // SDK_ARM9 + + //---- clear alarm list + i_osAlarmQueue.head = NULL; + i_osAlarmQueue.tail = NULL; + + //---- TIMER IRQ Disable + (void)osDisableInterruptID(OSi_ALARM_IE_TIMER_ID); + } +} + + +/*---------------------------------------------------------------------------* + Name: osEndAlarm + + Description: end alarm system + + Arguments: None + + Returns: None + *---------------------------------------------------------------------------*/ +void osEndAlarm(void) +{ + OSIntrMode enabled; + + SDK_ASSERT(i_osUseAlarm); + enabled = osDisableInterrupts(); + + //---- check if any alarm exists + if (i_osUseAlarm) + { + SDK_ASSERTMSG(!i_osAlarmQueue.head, + "osEndAlarm: Cannot end alarm system while using alarm."); + +#ifdef SDK_ARM9 + //---- unset timer reservation by OS + SDK_ASSERT(i_osIsTimerReserved(OSi_ALARM_TIMER)); + i_osUnsetTimerReserved(OSi_ALARM_TIMER); +#endif // SDK_ARM9 + + i_osUseAlarm = FALSE; + } + + (void)osRestoreInterrupts(enabled); +} + + +/*---------------------------------------------------------------------------* + Name: osIsAlarmAvailable + + Description: check alarm system is available + + Arguments: None + + Returns: if available, TRUE. + *---------------------------------------------------------------------------*/ +BOOL osIsAlarmAvailable(void) +{ + return i_osUseAlarm; +} + + +/*---------------------------------------------------------------------------* + Name: osCreateAlarm + + Description: Create alarm + + Arguments: alarm pointer to alarm to be initialized + + Returns: None. + *---------------------------------------------------------------------------*/ +void osCreateAlarm(OSAlarm *alarm) +{ + SDK_ASSERT(i_osUseAlarm); + SDK_ASSERT(alarm); + + alarm->handler = 0; + alarm->tag = 0; +} + + +/*---------------------------------------------------------------------------* + Name: i_osInsertAlarm + + Description: Insert alarm. Needs to be called interrupts disabled. + + Arguments: alarm pointer to alarm to be set + fire tick to fire (only for one shot alarm) + + Returns: None. + *---------------------------------------------------------------------------*/ +static void i_osInsertAlarm(OSAlarm *alarm, OSTick fire) +{ + OSAlarm *prev; + OSAlarm *next; + + //---- caluculate next fire for periodic alarm + if (alarm->period > 0) + { + OSTick tick = osGetTick(); + + fire = alarm->start; + if (alarm->start < tick) + { + fire += alarm->period * ((tick - alarm->start) / alarm->period + 1); + } + } + + //---- set tick to fire + alarm->fire = fire; + + //---- insert to list + for (next = i_osAlarmQueue.head; next; next = next->next) + { +// if ( next->fire <= fire ) + if ((s64)(fire - next->fire) >= 0) + { + continue; + } + + //---- insert alarm before 'next' + alarm->prev = next->prev; + next->prev = alarm; + alarm->next = next; + prev = alarm->prev; + if (prev) + { + prev->next = alarm; + } + else + { + i_osAlarmQueue.head = alarm; + i_osSetTimer(alarm); + } + + return; + } + + //---- insert alarm after tail + alarm->next = 0; + prev = i_osAlarmQueue.tail; + i_osAlarmQueue.tail = alarm; + alarm->prev = prev; + if (prev) + { + prev->next = alarm; + } + else + { + i_osAlarmQueue.head = i_osAlarmQueue.tail = alarm; + i_osSetTimer(alarm); + } +} + + +/*---------------------------------------------------------------------------* + Name: osSetAlarm + + Description: Set alarm as a relative tick + + Arguments: alarm pointer to alarm to be set + tick ticks to count before firing + handler alarm handler to be called + arg argument of handler + + Returns: None. + *---------------------------------------------------------------------------*/ +void osSetAlarm(OSAlarm *alarm, OSTick tick, OSAlarmHandler handler, void *arg) +{ + OSIntrMode enabled; + + //osPrintf( "**osSetAlarm e=%x alarm=%x tick=%x handler%x\n", enabled, alarm, tick, handler ); + SDK_ASSERT(i_osUseAlarm); + SDK_ASSERTMSG(handler, "osSetAlarm: handler must not be NULL."); + if (!alarm || alarm->handler) + { +#ifndef SDK_FINALROM + osPanic("alarm could be already used."); +#else + osPanic(""); +#endif + } + + enabled = osDisableInterrupts(); + + //---- clear periodic info + alarm->period = 0; + + //---- set handler + alarm->handler = handler; + alarm->arg = arg; + + //---- insert alarm + i_osInsertAlarm(alarm, osGetTick() + tick); + + (void)osRestoreInterrupts(enabled); +} + +/*---------------------------------------------------------------------------* + Name: osSetPeriodicAlarm + + Description: set periodic alarm + + Arguments: alarm pointer to alarm to be set + start origin of the period in absolute tick + period ticks to count for each period + handler alarm handler to be called + arg argument of handler + + Returns: None. + *---------------------------------------------------------------------------*/ +void osSetPeriodicAlarm(OSAlarm *alarm, OSTick start, OSTick period, OSAlarmHandler handler, + void *arg) +{ + u32 enabled; + + //osPrintf( "**SetPeriodicAlarm s=%llx p=%llx\n", start, period ); + SDK_ASSERT(i_osUseAlarm); + SDK_ASSERTMSG(handler, "osSetPeriodicAlarm: handler must not be NULL\n"); + SDK_ASSERTMSG(period > 0, "osSetPeriodicAlarm: bad period specified."); + if (!alarm || alarm->handler) + { +#ifndef SDK_FINALROM + osPanic("alarm could be already used."); +#else + osPanic(""); +#endif + } + + enabled = osDisableInterrupts(); + + //---- set periodic info + alarm->period = period; + alarm->start = start; + + //---- set handler + alarm->handler = handler; + alarm->arg = arg; + + //---- insert periodic alarm + i_osInsertAlarm(alarm, 0); + + (void)osRestoreInterrupts(enabled); +} + +/*---------------------------------------------------------------------------* + Name: osCancelAlarm + + Description: Cancel alarm + + Arguments: alarm pointer to alarm to be canceled + + Returns: None. + *---------------------------------------------------------------------------*/ +void osCancelAlarm(OSAlarm *alarm) +{ + OSAlarm *next; + u32 enabled; + + SDK_ASSERT(i_osUseAlarm); + SDK_ASSERT(alarm); + + enabled = osDisableInterrupts(); + + if (alarm->handler == NULL) + { + (void)osRestoreInterrupts(enabled); + return; + } + + //---- remove alarm + next = alarm->next; + if (next == NULL) + { + i_osAlarmQueue.tail = alarm->prev; + } + else + { + next->prev = alarm->prev; + } + + if (alarm->prev) + { + alarm->prev->next = next; + } + else + { + i_osAlarmQueue.head = next; + if (next) + { + i_osSetTimer(next); + } + } + + alarm->handler = NULL; + alarm->period = 0; // not periodic alarm + + (void)osRestoreInterrupts(enabled); +} + + +/*---------------------------------------------------------------------------* + Name: i_osAlarmHandler + + Description: handler timer interrupt + + Arguments: arg dummy + + Returns: None. + *---------------------------------------------------------------------------*/ +#include +asm void i_osAlarmHandler( void* arg ) +{ + INASM_EXTERN( i_osArrangeTimer ) + + stmfd sp!, {r0, lr} /* コールスタックを 8 バイト整合 */ + bl i_osArrangeTimer + ldmfd sp!, {r0, lr} /* コールスタックを 8 バイト整合 */ + bx lr +} +#include + + +/*---------------------------------------------------------------------------* + Name: i_osArrangeTimer + + Description: handler timer interrupt. called from i_osAlarmHandler + + Arguments: None + + Returns: None. + *---------------------------------------------------------------------------*/ +void i_osArrangeTimer(void) +{ + OSTick tick; + OSAlarm *alarm; + OSAlarm *next; + OSAlarmHandler handler; + + //---- To be timer-irq Disable + (void)osDisableInterruptID(OSi_ALARM_IE_TIMER_ID); + + //---- let timer be disable +#ifdef SDK_ARM11 + osStopWatchdog(); + +#else // SDK_ARM9 + osSetTimerControl(OSi_ALARM_TIMER, 0); + + //---- set check flag timer interrupt +// osSetIrqCheckFlag(OSi_ALARM_IE_TIMER); + +#endif // SDK_ARM9 + + tick = osGetTick(); + alarm = i_osAlarmQueue.head; + + //osPrintf( "**Arrange alarm=%x time=%llx file=%llx\n", alarm, time, alarm->fire ); + + //---- no alarm + if (alarm == NULL) + { + return; + } + + //---- not reach to time of top alarm + if (tick < alarm->fire) + { + i_osSetTimer(alarm); + return; + } + + //---- move next alarm to top + next = alarm->next; + i_osAlarmQueue.head = next; + + if (next == NULL) + { + i_osAlarmQueue.tail = NULL; + } + else + { + next->prev = NULL; + } + + //---- call user alarm handler + handler = alarm->handler; + + if (alarm->period == 0) + { + alarm->handler = NULL; + } + + if (handler) + { + (handler) (alarm->arg); + } + + //---- if alarm is periodic, re-inter to list + if (alarm->period > 0) + { + alarm->handler = handler; + i_osInsertAlarm(alarm, 0); + } + + //---- set timer + if (i_osAlarmQueue.head) + { + i_osSetTimer(i_osAlarmQueue.head); + } +} + + +/*---------------------------------------------------------------------------* + Name: osSetAlarmTag + + Description: set tag which is used osCancelAlarms + + Arguments: alarm alarm to be set tag + tag tagNo + + Returns: None. + *---------------------------------------------------------------------------*/ +void osSetAlarmTag(OSAlarm *alarm, u32 tag) +{ + SDK_ASSERT(i_osUseAlarm); + SDK_ASSERT(alarm); + SDK_ASSERTMSG(tag > 0, "osSetAlarmTag: Tag must be >0."); + + alarm->tag = tag; +} + + +/*---------------------------------------------------------------------------* + Name: osCancelAlarms + + Description: cancel alarms which have specified tag + + Arguments: tag tagNo. to be cancelled. not 0 + + Returns: None. + *---------------------------------------------------------------------------*/ +void osCancelAlarms(u32 tag) +{ + u32 enabled; + OSAlarm *alarm; + OSAlarm *next; + + SDK_ASSERT(i_osUseAlarm); + SDK_ASSERTMSG(tag > 0, "OSCancelAlarms: Tag must be >0."); + + if (tag == 0) + { + return; + } + + enabled = osDisableInterrupts(); + + for (alarm = i_osAlarmQueue.head, next = alarm ? alarm->next : NULL; + alarm; alarm = next, next = alarm ? alarm->next : NULL) + { + if (alarm->tag == tag) + { + //---- cancel alarm + osCancelAlarm(alarm); + } + } + + (void)osRestoreInterrupts(enabled); +} + + +/*---------------------------------------------------------------------------* + Name: osCancelAllAlarms + + Description: cancel all alarms + + Arguments: None + + Returns: None. + *---------------------------------------------------------------------------*/ +void osCancelAllAlarms(void) +{ + u32 enabled; + OSAlarm *alarm; + OSAlarm *next; + + SDK_ASSERT(i_osUseAlarm); + enabled = osDisableInterrupts(); + + for (alarm = i_osAlarmQueue.head, next = alarm ? alarm->next : NULL; + alarm; alarm = next, next = alarm ? alarm->next : NULL) + { + //---- cancel alarm + osCancelAlarm(alarm); + } + + (void)osRestoreInterrupts(enabled); +} + +/*---------------------------------------------------------------------------* + Name: i_osGetAlarmQueue + + Description: get alarm queue + + Arguments: None + + Returns: alarm queue. + *---------------------------------------------------------------------------*/ +struct OSiAlarmQueue *i_osGetAlarmQueue(void) +{ + return &i_osAlarmQueue; +} + +//================================================================================ +// FOR DEBUG +//================================================================================ +/*---------------------------------------------------------------------------* + Name: osGetNumberOfAlarm + + Description: get number of alarm + + Arguments: None + + Returns: number of alarm + *---------------------------------------------------------------------------*/ +int osGetNumberOfAlarm(void) +{ + OSIntrMode enabled = osDisableInterrupts(); + OSAlarm* p = i_osAlarmQueue.head; + int num = 0; + + while(p) + { + num ++; + p = p->next; + } + + (void)osRestoreInterrupts(enabled); + return num; +} + +/*---------------------------------------------------------------------------* + Name: osGetAlarmResource + + Description: store resources of alarm to specified pointer + + Arguments: resource pointer to store alarm resources + + Returns: TRUE ... success (always return this now) + FALSE ... fail + *---------------------------------------------------------------------------*/ +BOOL osGetAlarmResource(OSAlarmResource *resource) +{ + resource->num = osGetNumberOfAlarm(); + + return TRUE; +} + diff --git a/trunk/bootrom/build/libraries/os/common/os_init.c b/trunk/bootrom/build/libraries/os/common/os_init.c index f1c9f76..db61d86 100644 --- a/trunk/bootrom/build/libraries/os/common/os_init.c +++ b/trunk/bootrom/build/libraries/os/common/os_init.c @@ -40,7 +40,7 @@ void osInitBROM(void) #ifdef SDK_ARM9 #ifdef BROM_DEBUG_ITCM - MI_CpuFillFast( (void*)HW_ITCM, 0, HW_ITCM_SIZE ); + MI_CpuFillFast( (void*)HW_ITCM, 0, HW_ITCM_SIZE ); #endif // BROM_DEBUG_ITCM #endif // SDK_ARM9 @@ -50,6 +50,9 @@ void osInitBROM(void) //---- Init Tick osInitTick(); + + //---- Init Alarm +// osInitAlarm(); } } diff --git a/trunk/bootrom/build/libraries/os/common/os_tick.c b/trunk/bootrom/build/libraries/os/common/os_tick.c index 24c3fe7..2dc66f6 100644 --- a/trunk/bootrom/build/libraries/os/common/os_tick.c +++ b/trunk/bootrom/build/libraries/os/common/os_tick.c @@ -82,9 +82,9 @@ void osInitTick(void) i_osSetTimerReserved(OSi_TICK_TIMER); //---- setting timer - i_osSetTimerControl(OSi_TICK_TIMER, 0); - i_osSetTimerCount((OSTimer)OSi_TICK_TIMER, (u16)0); - i_osSetTimerControl(OSi_TICK_TIMER, (u16)OSi_TICK_TIMERCONTROL); + 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 @@ -133,9 +133,9 @@ static void i_osCountUpTick(void) osStopTimer(); osStartTimerWithMSec(1, 0); #else // SDK_ARM9 - i_osSetTimerControl(OSi_TICK_TIMER, 0); - i_osSetTimerCount((OSTimer)OSi_TICK_TIMER, (u16)0); - i_osSetTimerControl(OSi_TICK_TIMER, (u16)OSi_TICK_TIMERCONTROL); + 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; @@ -228,9 +228,9 @@ void osSetTick(u64 count) 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); + osSetTimerControl(OSi_TICK_TIMER, 0); + osSetTimerCount((OSTimer)OSi_TICK_TIMER, (u16)(count & 0xffff)); + osSetTimerControl(OSi_TICK_TIMER, (u16)OSi_TICK_TIMERCONTROL); #endif // SDK_ARM9 (void)osRestoreInterrupts(prev); diff --git a/trunk/bootrom/include/brom/os/ARM11/timer.h b/trunk/bootrom/include/brom/os/ARM11/timer.h index d3141bf..c02ac7a 100644 --- a/trunk/bootrom/include/brom/os/ARM11/timer.h +++ b/trunk/bootrom/include/brom/os/ARM11/timer.h @@ -108,10 +108,57 @@ void osStartTimer( u32 count, u8 preScale ); void osStopTimer( void ); +/*---------------------------------------------------------------------------* + Name: osStartWatchdogWithUSec + + Description: Start Watchdog + + Arguments: micro second + + Returns: None + *---------------------------------------------------------------------------*/ + +void osStartWatchdogWithUSec( u32 usec, u8 preScale, OSWatchdogMode watchdogMode ); + +/*---------------------------------------------------------------------------* + Name: osStartWatchdogWithMSec + + Description: Start Watchdog + + Arguments: milli second + + Returns: None + *---------------------------------------------------------------------------*/ + +void osStartWatchdogWithMSec( u32 msec, u8 preScale, OSWatchdogMode watchdogMode ); + +/*---------------------------------------------------------------------------* + Name: osStartWatchdog + + Description: Start Watchdog + + Arguments: interval + watchdogMode + + Returns: None + *---------------------------------------------------------------------------*/ + +void osStartWatchDog( u32 count, u8 preScale, OSWatchdogMode watchdogMode ); + +/*---------------------------------------------------------------------------* + Name: osStopWatchdog + + Description: Stop Watchdog + + Arguments: None + + Returns: None + *---------------------------------------------------------------------------*/ + +void osStopWatchdog( void ); + BOOL osEnableTimerAndWatchdog( void ); BOOL osDisableTimerAndWatchdog( void ); -void osStartWatchDog( u32 count, u8 preScale, OSWatchdogMode watchdogMode ); -void osStopWatchDog( void ); void osResetWatchdog( void ); void osDisableWatchdog( void ); diff --git a/trunk/bootrom/include/brom/os/ARM9/timer.h b/trunk/bootrom/include/brom/os/ARM9/timer.h index 835531a..4f30eb1 100644 --- a/trunk/bootrom/include/brom/os/ARM9/timer.h +++ b/trunk/bootrom/include/brom/os/ARM9/timer.h @@ -84,7 +84,7 @@ OSTimer48; void osInitTimer( void ); /*---------------------------------------------------------------------------* - Name: i_osSetTimerCount + Name: osSetTimerCount Description: set timer count @@ -93,14 +93,14 @@ void osInitTimer( void ); Returns: None *---------------------------------------------------------------------------*/ -static inline void i_osSetTimerCount( OSTimer id, u16 count ) +static inline void osSetTimerCount( OSTimer id, u16 count ) { SDK_ASSERT(OS_TIMER_0 <= id && id <= OS_TIMER_3); *((REGType16 *)((u32)REG_TM0CNT_L_ADDR + id * 4)) = count; } /*---------------------------------------------------------------------------* - Name: i_osSetTimerControl + Name: osSetTimerControl Description: set timer control @@ -109,14 +109,14 @@ static inline void i_osSetTimerCount( OSTimer id, u16 count ) Returns: None *---------------------------------------------------------------------------*/ -static inline void i_osSetTimerControl( OSTimer id, u16 control ) +static inline void osSetTimerControl( OSTimer id, u16 control ) { SDK_ASSERT(OS_TIMER_0 <= id && id <= OS_TIMER_3); *((REGType16 *)((u32)REG_TM0CNT_H_ADDR + id * 4)) = control; } /*---------------------------------------------------------------------------* - Name: i_osStartTimer + Name: osStartTimer Description: set timer(s) and start @@ -129,19 +129,19 @@ static inline void i_osSetTimerControl( OSTimer id, u16 control ) // // use 1 timer, 16bit counter, timer interrupt occurs by overflow // -void i_osStartTimer( OSTimer id, u16 count, OSTimerPrescaler preScale ); +void osStartTimer( OSTimer id, u16 count, OSTimerPrescaler preScale ); // // use 2 timers, 32bit counter, timer interrupt occurs by overflow // -void i_osStartTimer32( OSTimer32 id, u32 count, OSTimerPrescaler preScale ); +void osStartTimer32( OSTimer32 id, u32 count, OSTimerPrescaler preScale ); // // use 3 timers, 48bit counter, timer interrupt occurs by overflow // -void i_osStartTimer48( OSTimer48 id, u64 count, OSTimerPrescaler preScale ); +void osStartTimer48( OSTimer48 id, u64 count, OSTimerPrescaler preScale ); // // use all 4 timers, 64bit counter, timer3 interrupt occurs by overflow // -void i_osStartTimer64( u64 count, OSTimerPrescaler preScale ); +void osStartTimer64( u64 count, OSTimerPrescaler preScale ); /*---------------------------------------------------------------------------* diff --git a/trunk/bootrom/include/brom/os/common/alarm.h b/trunk/bootrom/include/brom/os/common/alarm.h index 9432329..41e1967 100644 --- a/trunk/bootrom/include/brom/os/common/alarm.h +++ b/trunk/bootrom/include/brom/os/common/alarm.h @@ -220,7 +220,7 @@ struct OSiAlarmQueue OSAlarm *head; OSAlarm *tail; }; -struct OSiAlarmQueue *OSi_GetAlarmQueue(void); +struct OSiAlarmQueue *i_osGetAlarmQueue(void); #ifdef __cplusplus diff --git a/trunk/bootrom/include/brom/os/common/tick.h b/trunk/bootrom/include/brom/os/common/tick.h index 3f4acde..3299c30 100644 --- a/trunk/bootrom/include/brom/os/common/tick.h +++ b/trunk/bootrom/include/brom/os/common/tick.h @@ -24,7 +24,11 @@ extern "C" { #include #include #include -//#include +#ifdef SDK_ARM11 +#include +#else // SDK_ARM9 +#include +#endif // SDK_ARM9 //---- unit of tick