1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - OS - demos - thread-3
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 17
21 #define MESSAGE_EXIT (OSMessage)100
22
23 OSMessage mesgBuffer[10];
24 OSMessageQueue mesgQueue;
25
26 OSThread thread1;
27 u32 stack1[STACK_SIZE / sizeof(u32)];
28
29 void VBlankIntr(void);
30 void proc1(void *p1);
31
32 void OS_WaitInterrupt(BOOL clearEnable, OSIrqMask intrFlags);
33
34 //================================================================================
35 /*---------------------------------------------------------------------------*
36 Name: NitroMain
37
38 Description: main
39
40 Arguments: None
41
42 Returns: None
43 *---------------------------------------------------------------------------*/
NitroMain()44 void NitroMain()
45 {
46 OS_Init();
47 OS_InitThread();
48
49 #ifdef SDK_ARM9
50 GX_Init();
51 #endif // SDK_ARM9
52
53 (void)OS_SetIrqFunction(OS_IE_V_BLANK, VBlankIntr);
54 (void)OS_EnableIrqMask(OS_IE_V_BLANK);
55 (void)OS_EnableIrq();
56 (void)GX_VBlankIntr(TRUE);
57
58 OS_InitMessageQueue(&mesgQueue, &mesgBuffer[0], 10);
59
60 //================ test 1
61 //---- create thread
62 OS_CreateThread(&thread1, proc1, NULL, stack1 + STACK_SIZE / sizeof(u32), STACK_SIZE, THREAD1_PRIO);
63 OS_WakeupThreadDirect(&thread1);
64
65 //---- send message to be die for thread1
66 OS_Printf("terminate thread1.\n");
67 (void)OS_SendMessage(&mesgQueue, (OSMessage)MESSAGE_EXIT, OS_MESSAGE_BLOCK);
68 OS_JoinThread(&thread1);
69 OS_Printf("terminated.\n");
70
71
72 //================ test 2
73 //---- create thread
74 OS_CreateThread(&thread1, proc1, NULL, stack1 + STACK_SIZE / sizeof(u32), STACK_SIZE, THREAD1_PRIO);
75 OS_WakeupThreadDirect(&thread1);
76
77 //---- send message to be die for thread1
78 OS_Printf("terminate thread1.\n");
79 (void)OS_SendMessage(&mesgQueue, (OSMessage)MESSAGE_EXIT, OS_MESSAGE_BLOCK);
80
81 //---- wait till thread1 die
82 while (!OS_IsThreadTerminated(&thread1))
83 {
84 OS_WaitIrq(1, OS_IE_V_BLANK);
85
86 // Notice: if you write OS_WaitInterrupt() instead of OS_WaitIrq(),
87 // thread1 will not die because OS_WaitInterrupt() doesn't
88 // cause to switch thread. OS_WaitIrq() do.
89 }
90
91 OS_Printf("terminated.\n");
92
93
94 OS_Printf("==== Finish sample.\n");
95 OS_Terminate();
96 }
97
98 //--------------------------------------------------------------------------------
99 // VBlank interrupt handler
100 //
VBlankIntr(void)101 void VBlankIntr(void)
102 {
103 OS_SetIrqCheckFlag(OS_IE_V_BLANK);
104 }
105
106 //--------------------------------------------------------------------------------
107 // proc1, which is procedure of thread1
108 //
proc1(void * p1)109 void proc1(void *p1)
110 {
111 #pragma unused(p1)
112 OSMessage message;
113
114 while (1)
115 {
116 (void)OS_ReceiveMessage(&mesgQueue, &message, OS_MESSAGE_BLOCK);
117
118 if (message == MESSAGE_EXIT)
119 {
120 OS_Printf("thread1 will die...\n");
121 OS_ExitThread();
122 }
123 }
124 }
125
126
127 /*====== End of main.c ======*/
128