1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - SPI - TP
3 File: tp.h
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-18#$
14 $Rev: 8573 $
15 $Author: okubata_ryoma $
16 *---------------------------------------------------------------------------*/
17
18 #ifndef NITRO_SPI_ARM9_TP_H_
19 #define NITRO_SPI_ARM9_TP_H_
20
21 #include <nitro/misc.h>
22 #include <nitro/types.h>
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28
29 #define TP_SAMPLING_FREQUENCY_MAX 16 // Sampling frequency limit
30
31 // Touch panel contact yes/no
32 #define TP_TOUCH_OFF 0 // Not being touched
33 #define TP_TOUCH_ON 1 // Being touched
34
35 // Touch panel data validity yes/no
36 #define TP_VALIDITY_VALID 0 // Enabled
37 #define TP_VALIDITY_INVALID_X 1 // Data with invalid x coordinate
38 #define TP_VALIDITY_INVALID_Y 2 // Data with invalid y coordinate
39 #define TP_VALIDITY_INVALID_XY (TP_VALIDITY_INVALID_X | TP_VALIDITY_INVALID_Y) // Data with invalid both x and y coordinates
40
41 // Types of commands issued to the touch panel
42 typedef enum
43 {
44 TP_REQUEST_COMMAND_SAMPLING = 0x0, // Performs sampling once
45 TP_REQUEST_COMMAND_AUTO_ON = 0x1, // Starts auto sampling
46 TP_REQUEST_COMMAND_AUTO_OFF = 0x2, // Stops auto sampling
47 TP_REQUEST_COMMAND_SET_STABILITY = 0x3, // Sets the chattering value
48 TP_REQUEST_COMMAND_AUTO_SAMPLING = 0x10 // Receive the result of auto sampling
49 }
50 TPRequestCommand;
51
52 // Command flags to touch panel
53 typedef enum
54 {
55 TP_REQUEST_COMMAND_FLAG_SAMPLING = 1 << TP_REQUEST_COMMAND_SAMPLING, // Performs sampling once
56 TP_REQUEST_COMMAND_FLAG_AUTO_ON = 1 << TP_REQUEST_COMMAND_AUTO_ON, // Starts auto sampling
57 TP_REQUEST_COMMAND_FLAG_AUTO_OFF = 1 << TP_REQUEST_COMMAND_AUTO_OFF, // Stops auto sampling
58 TP_REQUEST_COMMAND_FLAG_SET_STABILITY = 1 << TP_REQUEST_COMMAND_SET_STABILITY // Sets the chattering value
59 }
60 TPRequestCommandFlag;
61
62 // Types of results from touch panel
63 typedef enum
64 {
65 TP_RESULT_SUCCESS = 0, // Success
66 TP_RESULT_INVALID_PARAMETER, // Parameter is abnormal
67 TP_RESULT_ILLEGAL_STATUS, // State that cannot receive instructions
68 TP_RESULT_EXCLUSIVE, // SPI device is busy
69 TP_RESULT_PXI_BUSY // PXI communication with ARM7 is busy
70 }
71 TPRequestResult;
72
73 // Touch panel status
74 typedef enum
75 {
76 TP_STATE_READY = 0, // Ready status
77 TP_STATE_SAMPLING, // Currently requesting sampling
78 TP_STATE_AUTO_SAMPLING, // Currently auto sampling
79 TP_STATE_AUTO_WAIT_END // Waiting for auto sampling to end
80 }
81 TPState;
82
83 /*---------------------------------------------------------------------------*
84 Structure Definitions
85 *---------------------------------------------------------------------------*/
86
87 // Touch panel input structure
88 typedef struct
89 {
90 u16 x; // x coordinate ( 0 - 4095 )
91 u16 y; // y coordinate ( 0 - 4095 )
92 u16 touch; // Touch yes/no
93 u16 validity; // Valid yes/no
94 }
95 TPData;
96
97
98 #define TP_CALIBRATE_DOT_SCALE_SHIFT 8 // x dot size precision
99 #define TP_CALIBRATE_ORIGIN_SCALE_SHIFT 2 // Origin coordinate precision
100
101 // Touch Panel Calibration Data
102 typedef struct NvTpData
103 {
104 s16 x0; // x origin coordinate
105 s16 y0; // y origin coordinate
106 s16 xDotSize; // x dot size
107 s16 yDotSize; // y dot size
108 }
109 TPCalibrateParam; // 8 bytes
110
111
112 // User callback function
113 typedef void (*TPRecvCallback) (TPRequestCommand command, TPRequestResult result, u16 index);
114
115 /*===========================================================================*
116 Function Definitions
117 *===========================================================================*/
118 void TP_Init(void);
119 void TP_SetCallback(TPRecvCallback callback);
120 void TP_SetCalibrateParam(const TPCalibrateParam *param);
121
122 void TP_RequestSamplingAsync(void);
123 u32 TP_WaitRawResult(TPData *result);
124 u32 TP_WaitCalibratedResult(TPData *result);
125 u32 TP_GetCalibratedResult(TPData *result);
126 void TP_RequestAutoSamplingStartAsync(u16 vcount, u16 frequence, TPData samplingBufs[],
127 u16 bufSize);
128 void TP_RequestAutoSamplingStopAsync(void);
129 void TP_RequestSetStabilityAsync(u8 retry, u16 range);
130 void TP_WaitBusy(TPRequestCommandFlag command_flgs);
131 void TP_WaitAllBusy(void);
132 u32 TP_CheckBusy(TPRequestCommandFlag command_flgs);
133 u32 TP_CheckError(TPRequestCommandFlag command);
134
135 void TP_GetLatestRawPointInAuto(TPData *result);
136 void TP_GetLatestCalibratedPointInAuto(TPData *result);
137 u16 TP_GetLatestIndexInAuto(void);
138 u32 TP_CalcCalibrateParam(TPCalibrateParam *calibrate,
139 u16 raw_x1, u16 raw_y1,
140 u16 dx1, u16 dy1, u16 raw_x2, u16 raw_y2, u16 dx2, u16 dy2);
141 BOOL TP_GetUserInfo(TPCalibrateParam *calibrate);
142 void TP_GetCalibratedPoint(TPData *disp, const TPData *raw);
143 void TP_GetUnCalibratedPoint(u16 *raw_x, u16 *raw_y, u16 dx, u16 dy);
144
145 /*---------------------------------------------------------------------------*
146 Inline Function Definitions
147 *---------------------------------------------------------------------------*/
148
149 /*---------------------------------------------------------------------------*
150 Name: TP_RequestSetStability
151
152 Description: Sets touch panel anti-chattering parameters.
153 Sets the number of times to retry sampling before values stabilize, and a range for determining whether values have stabilized.
154
155
156 Arguments: retry: This argument is not used internally.
157 range: Range for determining whether the values have stabilized.
158 (Range: 0 to 255, Default value: 20)
159
160 Returns: u32: If request succeeds, 0
161 A non-zero value on failure.
162 *---------------------------------------------------------------------------*/
TP_RequestSetStability(u8 retry,u16 range)163 static inline u32 TP_RequestSetStability(u8 retry, u16 range)
164 {
165 TP_RequestSetStabilityAsync(retry, range);
166 TP_WaitBusy(TP_REQUEST_COMMAND_FLAG_SET_STABILITY);
167 return TP_CheckError(TP_REQUEST_COMMAND_FLAG_SET_STABILITY);
168 }
169
170
171 /*---------------------------------------------------------------------------*
172 Name: TP_RequestAutoSamplingStart
173
174 Description: Sends a request to ARM7 to start auto sampling touch panel values.
175 Data will be sampled 'frequence' times at equal intervals in one frame, and the results will be stored in the array specified by 'samplingBufs'.
176
177
178 Arguments: vcount: Sets the VCOUNT value, which is used as the standard for auto sampling
179 frequence: Setting for the number of times sampling will be done in one frame
180 samplingBufs: Sets the area where auto-sampling data will be stored
181 An area of the size "frequence" must be made available at minimum.
182
183
184 Returns: u32: If request succeeds, 0
185 A non-zero value on failure.
186 *---------------------------------------------------------------------------*/
TP_RequestAutoSamplingStart(u16 vcount,u16 frequence,TPData samplingBufs[],u16 bufSize)187 static inline u32 TP_RequestAutoSamplingStart(u16 vcount, u16 frequence, TPData samplingBufs[],
188 u16 bufSize)
189 {
190 TP_RequestAutoSamplingStartAsync(vcount, frequence, samplingBufs, bufSize);
191 TP_WaitBusy(TP_REQUEST_COMMAND_FLAG_AUTO_ON);
192 return TP_CheckError(TP_REQUEST_COMMAND_FLAG_AUTO_ON);
193 }
194
195
196 /*---------------------------------------------------------------------------*
197 Name: TP_RequestAutoSamplingStop
198
199 Description: Sends a request to ARM7 to stop auto sampling touch panel values.
200
201 Arguments: None.
202
203 Returns: u32: If request succeeds, 0
204 A non-zero value on failure.
205 *---------------------------------------------------------------------------*/
TP_RequestAutoSamplingStop(void)206 static inline u32 TP_RequestAutoSamplingStop(void)
207 {
208 TP_RequestAutoSamplingStopAsync();
209 TP_WaitBusy(TP_REQUEST_COMMAND_FLAG_AUTO_OFF);
210 return TP_CheckError(TP_REQUEST_COMMAND_FLAG_AUTO_OFF);
211 }
212
213
214 /*---------------------------------------------------------------------------*
215 Name: TP_RequestRawSampling
216
217 Description: Requests Touch Screen values from the ARM7 and then reads raw, uncalibrated values.
218
219 Data that is acquired here has already been subjected to chattering measures.
220 Cannot be used while auto sampling.
221
222 Arguments: None.
223
224 Returns: result: Pointer to a variable that is used to acquire touch panel values
225 Takes uncalibrated values (0 - 4095) as x, y coordinates.
226
227 u32: If request succeeds, 0
228 A non-zero value on failure.
229 *---------------------------------------------------------------------------*/
TP_RequestRawSampling(TPData * result)230 static inline u32 TP_RequestRawSampling(TPData *result)
231 {
232 TP_RequestSamplingAsync();
233 return TP_WaitRawResult(result);
234 }
235
236 /*---------------------------------------------------------------------------*
237 Name: TP_RequestCalibratedSampling
238
239 Description: Requests Touch Screen values from the ARM7 and then reads the values corresponding to calibrated screen coordinates.
240
241 Data that is acquired here has already been subjected to chattering measures.
242 Cannot be used while auto sampling.
243
244 Arguments: None.
245
246 Returns: result: Pointer to a variable that is used to acquire touch panel values
247 The x, y coordinates will be the values corresponding to the screen coordinates.
248 If calibration parameters have not been set, Touch Screen values between 0 and 4095 will be obtained.
249
250
251 u32: If request succeeds, 0
252 A non-zero value on failure.
253 *---------------------------------------------------------------------------*/
TP_RequestCalibratedSampling(TPData * result)254 static inline u32 TP_RequestCalibratedSampling(TPData *result)
255 {
256 TP_RequestSamplingAsync();
257 return TP_WaitCalibratedResult(result);
258 }
259
260
261
262 #ifdef __cplusplus
263 } /* extern "C" */
264 #endif
265
266 #endif
267 /* NITRO_SPI_ARM9_TP_H_ */
268