1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - PAD - include
3   File:     pad.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-17#$
14   $Rev: 8556 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef NITRO_PAD_PAD_H_
19 #define NITRO_PAD_PAD_H_
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 #include <nitro/misc.h>
26 #include <nitro/types.h>
27 #ifdef SDK_NITRO
28 #include <nitro/hw/common/mmap_shared.h>
29 #else
30 #ifdef SDK_ARM9
31 #include <twl/hw/ARM9/mmap_global.h>
32 #else  // SDK_ARM7
33 #include <twl/hw/ARM7/mmap_global.h>
34 #endif
35 #include <twl/hw/common/mmap_shared.h>
36 #endif
37 #ifdef SDK_ARM9
38 #include <nitro/hw/ARM9/ioreg_PAD.h>
39 #else  // SDK_ARM7
40 #include <nitro/hw/ARM7/ioreg_PAD.h>
41 #endif
42 
43 //================================================================================
44 //    BUTTONS
45 
46 //---- masked value
47 #define PAD_PLUS_KEY_MASK       0x00f0 // mask : cross keys
48 #define PAD_BUTTON_MASK         0x2f0f // mask : buttons
49 #define PAD_DEBUG_BUTTON_MASK   0x2000 // mask : debug button
50 #define PAD_ALL_MASK            0x2fff // mask : all buttons
51 #define PAD_RCNTPORT_MASK       0x2c00 // mask : factors ARM7 can read from RCNT register
52 #define PAD_KEYPORT_MASK        0x03ff // mask : factors ARM7/9 can read from KEY register
53 
54 #define PAD_DETECT_FOLD_MASK    0x8000 // mask : folding
55 
56 //---- button and key
57 #define PAD_BUTTON_A            0x0001 // A
58 #define PAD_BUTTON_B            0x0002 // B
59 #define PAD_BUTTON_SELECT       0x0004 // SELECT
60 #define PAD_BUTTON_START        0x0008 // START
61 #define PAD_KEY_RIGHT           0x0010 // RIGHT of cross key
62 #define PAD_KEY_LEFT            0x0020 // LEFT  of cross key
63 #define PAD_KEY_UP              0x0040 // UP    of cross key
64 #define PAD_KEY_DOWN            0x0080 // DOWN  of cross key
65 #define PAD_BUTTON_R            0x0100 // R
66 #define PAD_BUTTON_L            0x0200 // L
67 #define PAD_BUTTON_X            0x0400 // X
68 #define PAD_BUTTON_Y            0x0800 // Y
69 #define PAD_BUTTON_DEBUG        0x2000 // Debug button
70 
71 //================================================================================
72 //    INTERNAL FUNCTIONS prototype
73 static inline u16 PADi_ReadRaw(void);
74 
75 
76 /*---------------------------------------------------------------------------*
77   Name:         PAD_Read
78 
79   Description:  read pad data.
80                 X and Y button data from ARM7 are added.
81 
82   Arguments:    None.
83 
84   Returns:      pad data.
85                 See above defines to know the meaning of each bit.
86  *---------------------------------------------------------------------------*/
PAD_Read(void)87 static inline u16 PAD_Read(void)
88 {
89     u16 paddata = PADi_ReadRaw();
90     return (u16)(paddata & ~((paddata & PAD_KEY_UP) << 1) & ~((paddata & PAD_KEY_LEFT) >> 1));
91 }
92 
PAD_ReadNoFilter(void)93 static inline u16 PAD_ReadNoFilter(void)
94 {
95     return PADi_ReadRaw();
96 }
97 
98 /*---------------------------------------------------------------------------*
99   Name:         PAD_DetectFold
100 
101   Description:  detect folding Nitro.
102 
103   Arguments:    None.
104 
105   Returns:      TRUE if fold, FALSE if not.
106  *---------------------------------------------------------------------------*/
PAD_DetectFold(void)107 static inline BOOL PAD_DetectFold(void)
108 {
109     return (BOOL)((*(vu16 *)HW_BUTTON_XY_BUF & PAD_DETECT_FOLD_MASK) >> 15);
110 }
111 
112 
113 //================================================================================
114 //         INTERNAL FUNCTIONS
115 //================================================================================
116 //   Don't use PADi_ internal functions as you can.
117 //   Using key interrupt may cause some bugs
118 //   because there is the problem about key chattering
119 
120 //---------------- interrupt key logic
121 typedef enum
122 {
123     // condition for occurring interrupt
124     PAD_LOGIC_OR = (0 << REG_PAD_KEYCNT_LOGIC_SHIFT),   //   pushing ONE of keys you specified
125     PAD_LOGIC_AND = (1 << REG_PAD_KEYCNT_LOGIC_SHIFT),  //   pushing ALL of keys at a time you specified
126 
127     //---- for compatibility to old description
128     PAD_OR_INTR = PAD_LOGIC_OR,
129     PAD_AND_INTR = PAD_LOGIC_AND
130 }
131 PADLogic;
132 
133 /*---------------------------------------------------------------------------*
134   Name:         PADi_ReadRaw
135 
136   Description:  read pad data.
137                 Xand Y button data from ARM7 are added.
138 
139   Arguments:    None.
140 
141   Returns:      pad data.
142                 See above defines to know the meaning of each bit.
143  *---------------------------------------------------------------------------*/
PADi_ReadRaw(void)144 static inline u16 PADi_ReadRaw(void)
145 {
146     return (u16)(((reg_PAD_KEYINPUT | *(vu16 *)HW_BUTTON_XY_BUF) ^
147                   (PAD_PLUS_KEY_MASK | PAD_BUTTON_MASK)) & (PAD_PLUS_KEY_MASK | PAD_BUTTON_MASK));
148 }
149 
150 /*---------------------------------------------------------------------------*
151   Name:         PADi_SetIrq
152 
153   Description:  set key interrupt
154 
155   Arguments:    logic   : key logic
156                           PAD_OR_INTR / PAD_AND_INTR
157                 enable  : TRUE if enable interrupt, FALSE if not
158                 padMask : buttons to occur interrupt
159 
160   Returns:      None.
161  *---------------------------------------------------------------------------*/
PADi_SetIrq(PADLogic logic,BOOL enable,u16 padMask)162 static inline void PADi_SetIrq(PADLogic logic, BOOL enable, u16 padMask)
163 {
164     SDK_ASSERT(!(padMask & (~PAD_KEYPORT_MASK)));       // check X, Y, Debug button
165     reg_PAD_KEYCNT = (u16)(((u16)logic
166                             | (enable ? REG_PAD_KEYCNT_INTR_MASK : 0)
167                             | (PAD_KEYPORT_MASK & padMask)));
168 }
169 
170 /*---------------------------------------------------------------------------*
171   Name:         PADi_ClearIrq
172 
173   Description:  unset key interrupt
174 
175   Arguments:    None.
176 
177   Returns:      None.
178  *---------------------------------------------------------------------------*/
PADi_ClearIrq(void)179 static inline void PADi_ClearIrq(void)
180 {
181     reg_PAD_KEYCNT = 0;
182 }
183 
184 //================================================================================
185 
186 #ifdef __cplusplus
187 } /* extern "C" */
188 #endif
189 
190 /* NITRO_PAD_PAD_H_ */
191 #endif
192