1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - OS - demos - valarm-1
3   File:     main.c
4 
5   Copyright 2008 Nintendo.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2008-04-01#$
14   $Rev: 5205 $
15   $Author: yada $
16  *---------------------------------------------------------------------------*/
17 #include <nitro.h>
18 
19 void    v_alarmCallback1(void *arg);
20 void    v_alarmCallback2(void *arg);
21 
22 void    VBlankIntr(void);
23 
24 static OSVAlarm v_alarm1;
25 static OSVAlarm v_alarm2;
26 static OSVAlarm v_alarm3;
27 static OSVAlarm pvAlarm1;
28 static OSVAlarm pvAlarm2;
29 
30 
31 static int num_valarms = 0;
32 static BOOL pvAlarm1_sw = FALSE;
33 static BOOL pvAlarm2_sw = FALSE;
34 
35 static u16 keyData;
36 static u16 preData;
37 static u16 trigger;
38 
39 //================================================================================
40 /*---------------------------------------------------------------------------*
41   Name:         NitroMain
42 
43   Description:  main
44 
45   Arguments:    None
46 
47   Returns:      None
48  *---------------------------------------------------------------------------*/
NitroMain()49 void NitroMain()
50 {
51     OS_Init();
52 #ifdef SDK_ARM9
53     GX_Init();
54 #endif
55 
56     //---- init tick system
57     OS_InitTick();
58 
59     //---- init alarm system (but OS_InitVAlarm called in OS_Init, so not necessary)
60     OS_InitAlarm();
61     //OS_InitVAlarm(); /*called in OS_Init()*/
62 
63     //---- setting vblank
64     (void)OS_SetIrqFunction(OS_IE_V_BLANK, VBlankIntr);
65     (void)OS_EnableIrqMask(OS_IE_V_BLANK);
66     (void)OS_EnableIrq();
67     (void)GX_VBlankIntr(TRUE);
68 
69     //---- Dummy READ workaround for pushing 'A' at IPL
70     preData = PAD_Read();
71 
72     OS_Printf("Push [A]    to start 3 valarms.\n");
73     OS_Printf("Push [UP]   to start periodic valarm1. ([DOWN]  to stop)\n");
74     OS_Printf("Push [LEFT] to start periodic valarm2. ([RIGHT] to stop)\n");
75 
76     //---- setting vblank
77     while (1)
78     {
79         OS_WaitVBlankIntr();
80 
81         keyData = PAD_Read();
82         trigger = (u16)(keyData & (keyData ^ preData));
83         preData = keyData;
84 
85         //---- PUSH A to start valarm
86         if (trigger & PAD_BUTTON_A && num_valarms == 0)
87         {
88             num_valarms = 3;
89 
90             OS_Printf("set 3 alarms");
91             //---- setting alarm
92             OS_CreateVAlarm(&v_alarm1);
93             OS_SetVAlarm(&v_alarm1, 100, OS_VALARM_DELAY_MAX, &v_alarmCallback1, (void *)1);
94 
95             OS_CreateVAlarm(&v_alarm2);
96             OS_SetVAlarm(&v_alarm2, 150, OS_VALARM_DELAY_MAX, &v_alarmCallback1, (void *)2);
97 
98             OS_CreateVAlarm(&v_alarm3);
99             OS_SetVAlarm(&v_alarm3, 250, 50, &v_alarmCallback1, (void *)3);
100 
101             OS_Printf("set valarms\n");
102         }
103 
104         //---- PUSH UP to start periodic valarm1
105         if (trigger & PAD_KEY_UP && !pvAlarm1_sw)
106         {
107             pvAlarm1_sw = TRUE;
108 
109             //---- setting periodic valarm
110             OS_CreateVAlarm(&pvAlarm1);
111             OS_SetPeriodicVAlarm(&pvAlarm1, 120, 10, &v_alarmCallback2, (void *)10);
112 
113             OS_Printf("set periodic valarm 1\n");
114         }
115 
116         //---- PUSH DOWN to stop periodic valarm1
117         if (trigger & PAD_KEY_DOWN && pvAlarm1_sw)
118         {
119             pvAlarm1_sw = FALSE;
120             OS_CancelVAlarm(&pvAlarm1);
121 
122             OS_Printf("stop periodic valarm 1\n");
123         }
124 
125 
126         //---- PUSH LEFT to start periodic valarm2
127         if (trigger & PAD_KEY_LEFT && !pvAlarm2_sw)
128         {
129             pvAlarm2_sw = TRUE;
130 
131             //---- setting periodic valarm
132             OS_CreateVAlarm(&pvAlarm2);
133             OS_SetPeriodicVAlarm(&pvAlarm2, 121, 50, &v_alarmCallback2, (void *)20);
134 
135             OS_Printf("set periodic valarm 2\n");
136         }
137 
138         //---- PUSH RIGHT to stop periodic valarm2
139         if (trigger & PAD_KEY_RIGHT && pvAlarm2_sw)
140         {
141             pvAlarm2_sw = FALSE;
142             OS_CancelVAlarm(&pvAlarm2);
143 
144             OS_Printf("stop periodic valarm 2\n");
145         }
146     }
147 }
148 
149 
150 //----------------------------------------------------------------
151 //  V-Alarm callback
152 //
v_alarmCallback1(void * arg)153 void v_alarmCallback1(void *arg)
154 {
155 #ifdef SDK_FINALROM
156 #pragma unused( arg )
157 #endif
158     OS_Printf("[%x]>>> VLINE=%d\n", arg, GX_GetVCount());
159     num_valarms--;
160 
161 #ifdef	TEST
162     OS_SpinWait(66000000 / 60 * 30 / 263);
163 #endif
164 }
165 
166 //----------------------------------------------------------------
167 //  V-Alarm callback
168 //
v_alarmCallback2(void * arg)169 void v_alarmCallback2(void *arg)
170 {
171 #ifdef SDK_FINALROM
172 #pragma unused( arg )
173 #endif
174     OS_Printf("[%d] VLINE=%d, TICK=%llx\n", arg, GX_GetVCount(), OS_GetTick());
175 }
176 
177 
178 //----------------------------------------------------------------
179 //  VBlank interrupt handler
VBlankIntr(void)180 void VBlankIntr(void)
181 {
182 #ifdef	TEST
183     s32     before = GX_GetVCount();
184 #endif
185 
186     //---- check interrupt flag
187     OS_SetIrqCheckFlag(OS_IE_V_BLANK);
188 
189 #ifdef	TEST
190     if (PAD_Read() & PAD_BUTTON_X)
191     {
192         OS_SpinWait(66000000 / 60 * 80 / 263);
193     }
194     OS_Printf("VB VLINE=%d-%d\n", before, GX_GetVCount());
195 #endif
196 }
197 
198 /*====== End of main.c ======*/
199