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:: 2008-09-17#$
14   $Rev: 8556 $
15   $Author: okubata_ryoma $
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 on indicated point.
39 
40   Arguments:    x: X position
41                 y: Y position
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 OBJs 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     // unnecessary called.
149     // be able to use default parameter (retry,range).
150 #if 0
151     // Send parameter of revision noise.
152     if (TP_RequestSetStability(3, 15) != 0)
153     {
154         OS_Panic("SetStability request err!\n");
155     }
156 #endif
157 
158     while (TRUE)
159     {
160         // Draw Marker by calibrated point
161         while (TP_RequestRawSampling(&raw_point) != 0)
162         {
163         };
164         TP_GetCalibratedPoint(&disp_point, &raw_point);
165 
166         if (disp_point.touch)
167         {
168             SetPoint16x16(disp_point.x, disp_point.y);
169 
170             switch (disp_point.validity)
171             {
172             case TP_VALIDITY_VALID:
173                 OS_Printf("( %d, %d ) -> ( %d, %d )\n", raw_point.x, raw_point.y, disp_point.x,
174                           disp_point.y);
175                 break;
176             case TP_VALIDITY_INVALID_X:
177                 OS_Printf("( *%d, %d ) -> ( *%d, %d )\n", raw_point.x, raw_point.y, disp_point.x,
178                           disp_point.y);
179                 break;
180             case TP_VALIDITY_INVALID_Y:
181                 OS_Printf("( %d, *%d ) -> ( %d, *%d )\n", raw_point.x, raw_point.y, disp_point.x,
182                           disp_point.y);
183                 break;
184             case TP_VALIDITY_INVALID_XY:
185                 OS_Printf("( *%d, *%d ) -> ( *%d, *%d )\n", raw_point.x, raw_point.y, disp_point.x,
186                           disp_point.y);
187                 break;
188             }
189         }
190 
191         /* Flush cache of OAM buffers to main memory */
192         DC_FlushRange(gOam, sizeof(gOam));
193         /* I/O register is accessed using DMA operation, so cache wait is not needed */
194         // DC_WaitWriteBufferEmpty();
195         GX_LoadOAM(gOam, 0, sizeof(gOam));
196         MI_DmaFill32(3, gOam, 192, sizeof(gOam));       // Clear OAM buffer
197 
198         // Wait for V-Blank interrupt
199         OS_WaitVBlankIntr();
200 
201     }
202 }
203 
204 
205 /*---------------------------------------------------------------------------*
206   Name:         VBlankIntr
207 
208   Description:  V-Blank function.
209 
210   Arguments:    None.
211 
212   Returns:      None.
213  *---------------------------------------------------------------------------*/
VBlankIntr(void)214 static void VBlankIntr(void)
215 {
216 
217     // Set IRQ check flag
218     OS_SetIrqCheckFlag(OS_IE_V_BLANK);
219 }
220 
221 
222 /*---------------------------------------------------------------------------*
223   End of file
224  *---------------------------------------------------------------------------*/
225