1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - MB - demos - fake_child
3   File:     common.c
4 
5   Copyright 2006-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:: 2009-02-19#$
14   $Rev: 10047 $
15   $Author: nishimoto_takashi $
16 *---------------------------------------------------------------------------*/
17 
18 #ifdef SDK_TWL
19 #include	<twl.h>
20 #else
21 #include	<nitro.h>
22 #endif
23 
24 #include "common.h"
25 #include "disp.h"
26 
27 
28 static void VBlankIntr(void);
29 
30 /*
31  * Common features used by this entire demo
32  */
33 static u16 padPress;
34 static u16 padTrig;
35 
36 /*---------------------------------------------------------------------------*
37   Name:         ReadKey
38 
39   Description:  Processing for reading keys.
40 
41   Arguments:    None.
42 
43   Returns:      None.
44  *---------------------------------------------------------------------------*/
ReadKey(void)45 void ReadKey(void)
46 {
47     u16     currData = PAD_Read();
48 
49     padTrig = (u16)(~padPress & currData);
50     padPress = currData;
51 }
52 
53 /*---------------------------------------------------------------------------*
54   Name:         GetPressKey
55 
56   Description:  Gets pressed key.
57 
58   Arguments:    None.
59 
60   Returns:      Bitmap of the pressed key.
61  *---------------------------------------------------------------------------*/
GetPressKey(void)62 u16 GetPressKey(void)
63 {
64     return padPress;
65 }
66 
67 
68 /*---------------------------------------------------------------------------*
69   Name:         GetTrigKey
70 
71   Description:  Gets key trigger.
72 
73   Arguments:    None.
74 
75   Returns:      A bitmap of key triggers.
76  *---------------------------------------------------------------------------*/
GetTrigKey(void)77 u16 GetTrigKey(void)
78 {
79     return padTrig;
80 }
81 
82 
83 /*---------------------------------------------------------------------------*
84   Name:         CommonInit
85 
86   Description:  Common initialization functions.
87 
88   Arguments:    None.
89 
90   Returns:      None.
91  *---------------------------------------------------------------------------*/
CommonInit(void)92 void CommonInit(void)
93 {
94     /* OS initialization */
95     OS_Init();
96     OS_InitTick();
97     OS_InitAlarm();
98     FX_Init();
99 
100     /* GX initialization */
101     GX_Init();
102     GX_DispOff();
103     GXS_DispOff();
104 
105     // Configure interrupts
106     (void)OS_SetIrqFunction(OS_IE_V_BLANK, VBlankIntr);
107     (void)OS_EnableIrqMask(OS_IE_V_BLANK);
108     (void)OS_EnableIrqMask(OS_IE_FIFO_RECV);
109     (void)GX_VBlankIntr(TRUE);
110 
111     // Read the key once as empty
112     ReadKey();
113 }
114 
115 
116 /*---------------------------------------------------------------------------*
117   Name:         InitAllocateSystem
118 
119   Description:  Initializes the memory allocation system within the main memory arena.
120 
121   Arguments:    None.
122 
123   Returns:      None.
124  *---------------------------------------------------------------------------*/
InitAllocateSystem(void)125 void InitAllocateSystem(void)
126 {
127     void   *tempLo;
128     OSHeapHandle hh;
129 
130     // Based on the premise that OS_Init has been already called
131     tempLo = OS_InitAlloc(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi(), 1);
132     OS_SetArenaLo(OS_ARENA_MAIN, tempLo);
133     hh = OS_CreateHeap(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi());
134     if (hh < 0)
135     {
136         OS_Panic("ARM9: Fail to create heap...\n");
137     }
138     hh = OS_SetCurrentHeap(OS_ARENA_MAIN, hh);
139 }
140 
141 
142 
143 
144 /*---------------------------------------------------------------------------*
145   Name:         VBlankIntr
146 
147   Description:  Gets key trigger.
148 
149   Arguments:    None.
150 
151   Returns:      None.
152  *---------------------------------------------------------------------------*/
VBlankIntr(void)153 static void VBlankIntr(void)
154 {
155     DispVBlankFunc();
156 
157     //---- Interrupt check flag
158     OS_SetIrqCheckFlag(OS_IE_V_BLANK);
159 }
160