1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - OS - demos - thread-2
3 File: main.c
4
5 Copyright 2003-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-02#$
14 $Rev: 5266 $
15 $Author: yada $
16 *---------------------------------------------------------------------------*/
17 #include <nitro.h>
18
19 #define STACK_SIZE 1024
20 #define THREAD1_PRIO 1
21 #define THREAD2_PRIO 2
22
23 OSThread thread1;
24 OSThread thread2;
25
26 u32 stack1[STACK_SIZE / sizeof(u32)];
27 u32 stack2[STACK_SIZE / sizeof(u32)];
28
29 void VBlankIntr(void);
30 void proc1(void *arg);
31 void proc2(void *arg);
32
33 //================================================================================
34 // Sample of thread switch using interrupt
35 // Starts a thread from within the IRQ handler.
36 // However, the actual thread switching is postponed until the IRQ routine ends.
37 /*---------------------------------------------------------------------------*
38 Name: NitroMain
39
40 Description: Main.
41
42 Arguments: None.
43
44 Returns: None.
45 *---------------------------------------------------------------------------*/
NitroMain()46 void NitroMain()
47 {
48 s32 n;
49
50 OS_Init();
51 OS_InitThread();
52
53 OS_CreateThread(&thread1, proc1, NULL, stack1 + STACK_SIZE / sizeof(u32), STACK_SIZE, THREAD1_PRIO);
54 OS_CreateThread(&thread2, proc2, NULL, stack2 + STACK_SIZE / sizeof(u32), STACK_SIZE, THREAD2_PRIO);
55
56 OS_WakeupThreadDirect(&thread1);
57 OS_WakeupThreadDirect(&thread2);
58
59 //================ Settings
60 #ifdef SDK_ARM9
61 //---- Initialize graphics
62 GX_Init();
63 #endif // SDK_ARM9
64
65 //---- Enable V-Blank interrupt
66 (void)OS_SetIrqFunction(OS_IE_V_BLANK, VBlankIntr);
67 (void)OS_EnableIrqMask(OS_IE_V_BLANK);
68 (void)OS_EnableIrq();
69
70 //---- V-Blank occurrence settings
71 (void)GX_VBlankIntr(TRUE);
72
73 //================ Main loop
74 for (n = 0; n < 5; n++)
75 {
76 //---- Wait for V-Blank interrupt completion
77 OS_WaitVBlankIntr();
78
79 OS_Printf("main\n");
80 }
81
82 OS_Printf("==== Finish sample.\n");
83 OS_Terminate();
84 }
85
86 //--------------------------------------------------------------------------------
87 // V-Blank interrupt processing(processing in IRQ)
88 //
VBlankIntr(void)89 void VBlankIntr(void)
90 {
91 //---- Interrupt check flag
92 OS_SetIrqCheckFlag(OS_IE_V_BLANK);
93
94 //---- Start thread
95 OS_WakeupThreadDirect(&thread1);
96 OS_WakeupThreadDirect(&thread2);
97 }
98
99 //--------------------------------------------------------------------------------
100 // proc1
101 //
proc1(void * arg)102 void proc1(void *arg)
103 {
104 #pragma unused(arg)
105 while (1)
106 {
107 OS_Printf("---- Thread1 sleep\n");
108 OS_SleepThread(NULL);
109 OS_Printf("---- Thread1 wakeup\n");
110 }
111 }
112
113 //--------------------------------------------------------------------------------
114 // proc2
115 //
proc2(void * arg)116 void proc2(void *arg)
117 {
118 #pragma unused(arg)
119 while (1)
120 {
121 OS_Printf("---- Thread2 sleep\n");
122 OS_SleepThread(NULL);
123 OS_Printf("---- Thread2 wakeup\n");
124 }
125 }
126
127 /*====== End of main.c ======*/
128