1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - demos - spi - tp-1
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:: 2009-06-08#$
14 $Rev: 10700 $
15 $Author: yosizaki $
16 *---------------------------------------------------------------------------*/
17
18 #include <nitro.h>
19 #include <nitro/spi/ARM9/tp.h>
20 #include "data.h"
21
22
23 /*---------------------------------------------------------------------------*
24 Prototype definitions
25 *---------------------------------------------------------------------------*/
26 static void VBlankIntr(void);
27
28
29 /*---------------------------------------------------------------------------*
30 Static variable definitions
31 *---------------------------------------------------------------------------*/
32 static GXOamAttr gOam[128];
33
34
35 /*---------------------------------------------------------------------------*
36 Name: SetPoint16x16
37
38 Description: Displays a 16x16 OBJ at indicated point.
39
40 Arguments: x: Position X
41 y: Position Y
42
43 Returns: None.
44 *---------------------------------------------------------------------------*/
SetPoint16x16(u16 pos_x,u16 pos_y)45 static inline void SetPoint16x16(u16 pos_x, u16 pos_y)
46 {
47 G2_SetOBJAttr(&gOam[0], // OAM number
48 pos_x - 8, // X position
49 pos_y - 8, // Y position
50 0, // Priority
51 GX_OAM_MODE_BITMAPOBJ, // Bitmap mode
52 FALSE, // Mosaic off
53 GX_OAM_EFFECT_NONE, // Affine off
54 GX_OAM_SHAPE_16x16, // 16x16 size
55 GX_OAM_COLOR_16, // 16 color
56 1, // Character
57 15, // Alpha
58 0);
59 }
60
61
62 /*---------------------------------------------------------------------------*
63 Name: DisplayInit
64
65 Description: Graphics initialization.
66
67 Arguments: None.
68
69 Returns: None.
70 *---------------------------------------------------------------------------*/
DisplayInit()71 static void DisplayInit()
72 {
73 GX_Init();
74 FX_Init();
75
76 GX_DispOff();
77 GXS_DispOff();
78 GX_SetDispSelect(GX_DISP_SELECT_SUB_MAIN);
79
80 OS_SetIrqFunction(OS_IE_V_BLANK, VBlankIntr);
81 (void)OS_EnableIrqMask(OS_IE_V_BLANK);
82 (void)OS_EnableIrq();
83 (void)GX_VBlankIntr(TRUE); // To generate V-Blank interrupt request
84
85 GX_SetBankForLCDC(GX_VRAM_LCDC_ALL);
86 MI_CpuClearFast((void *)HW_LCDC_VRAM, HW_LCDC_VRAM_SIZE);
87
88 (void)GX_DisableBankForLCDC();
89
90 MI_CpuFillFast((void *)HW_OAM, 192, HW_OAM_SIZE); // Clear OAM
91 MI_CpuClearFast((void *)HW_PLTT, HW_PLTT_SIZE); // Clear the standard palette
92
93 MI_CpuFillFast((void *)HW_DB_OAM, 192, HW_DB_OAM_SIZE); // Clear OAM
94 MI_CpuClearFast((void *)HW_DB_PLTT, HW_DB_PLTT_SIZE); // Clear the standard palette
95
96 GX_SetBankForOBJ(GX_VRAM_OBJ_256_AB); // Set VRAM-A, B for OBJ
97
98 GX_SetGraphicsMode(GX_DISPMODE_GRAPHICS, // 2D / 3D Mode
99 GX_BGMODE_0, // BGMODE 0
100 GX_BG0_AS_2D); // Set BG0 as 2D
101
102 GX_SetVisiblePlane(GX_PLANEMASK_OBJ); // Make OBJ visible
103 GX_SetOBJVRamModeBmp(GX_OBJVRAMMODE_BMP_1D_128K); // 1D mapping OBJ
104
105 /* Load character bitmap data */
106 GX_LoadOBJ((const void *)IMAGE_DATA, 0, IMAGE_DATA_SIZE); // Transfer OBJ bitmap data to VRAM
107
108 OS_WaitVBlankIntr(); // Waiting for the end of the V-Blank interrupt
109 GX_DispOn();
110
111 }
112
113
114
115 /*---------------------------------------------------------------------------*
116 Name: NitroMain
117
118 Description: Initialization and main loop.
119
120 Arguments: None.
121
122 Returns: None.
123 *---------------------------------------------------------------------------*/
NitroMain(void)124 void NitroMain(void)
125 {
126 TPData raw_point;
127 TPData disp_point;
128 TPCalibrateParam calibrate;
129
130 // Initialization
131 OS_Init();
132 TP_Init();
133
134 // Get CalibrateParameter from FlashMemory
135 if (!TP_GetUserInfo(&calibrate))
136 {
137 OS_Panic("FATAL ERROR: can't read UserOwnerInfo\n");
138 }
139 else
140 {
141 OS_Printf("Get Calibration Parameter from NVRAM\n");
142 }
143
144 TP_SetCalibrateParam(&calibrate);
145
146 DisplayInit();
147
148 // Parameters can be changed, but it is also acceptable to keep the default settings
149 #if 0
150 // Send parameter of revision noise
151 if (TP_RequestSetStability(3, 15) != 0)
152 {
153 OS_Panic("SetStability request err!\n");
154 }
155 #endif
156
157 while (TRUE)
158 {
159 // Draw Marker by calibrated point
160 while (TP_RequestRawSampling(&raw_point) != 0)
161 {
162 };
163 TP_GetCalibratedPoint(&disp_point, &raw_point);
164
165 if (disp_point.touch)
166 {
167 SetPoint16x16(disp_point.x, disp_point.y);
168
169 switch (disp_point.validity)
170 {
171 case TP_VALIDITY_VALID:
172 OS_Printf("( %d, %d ) -> ( %d, %d )\n", raw_point.x, raw_point.y, disp_point.x,
173 disp_point.y);
174 break;
175 case TP_VALIDITY_INVALID_X:
176 OS_Printf("( *%d, %d ) -> ( *%d, %d )\n", raw_point.x, raw_point.y, disp_point.x,
177 disp_point.y);
178 break;
179 case TP_VALIDITY_INVALID_Y:
180 OS_Printf("( %d, *%d ) -> ( %d, *%d )\n", raw_point.x, raw_point.y, disp_point.x,
181 disp_point.y);
182 break;
183 case TP_VALIDITY_INVALID_XY:
184 OS_Printf("( *%d, *%d ) -> ( *%d, *%d )\n", raw_point.x, raw_point.y, disp_point.x,
185 disp_point.y);
186 break;
187 }
188 }
189
190 /* Flush cache of OAM buffers to main memory */
191 DC_FlushRange(gOam, sizeof(gOam));
192 /* I/O register is accessed using DMA operation, so cache wait is not needed */
193 // DC_WaitWriteBufferEmpty();
194 GX_LoadOAM(gOam, 0, sizeof(gOam));
195 MI_DmaFill32(3, gOam, 192, sizeof(gOam)); // Clear OAM buffer
196
197 // Wait for V-Blank interrupt
198 OS_WaitVBlankIntr();
199
200 }
201 }
202
203
204 /*---------------------------------------------------------------------------*
205 Name: VBlankIntr
206
207 Description: V-Blank function.
208
209 Arguments: None.
210
211 Returns: None.
212 *---------------------------------------------------------------------------*/
VBlankIntr(void)213 static void VBlankIntr(void)
214 {
215
216 // Set IRQ check flag
217 OS_SetIrqCheckFlag(OS_IE_V_BLANK);
218 }
219
220
221 /*---------------------------------------------------------------------------*
222 End of file
223 *---------------------------------------------------------------------------*/
224