InPlay API
hal_keyboard.h
1 
12 #ifndef KEYBOARD_H
13 #define KEYBOARD_H
14 #include <stdint.h>
15 #include "./hal/hal_global.h"
16 
23 #define KB_MAX_CURR_KEY 6
24 
25 #define KB_INTR_SCAN_DONE 0x1
26 #define KB_INTR_UPDATE 0x2
27 #define KB_INTR_TOO_MANY_KEYS 0x4
28 #define KB_INTR_PAUSE 0x8
29 
30 #define KB_MAX_ROW_NUM 14
31 #define KB_MAX_COL_NUM 14
32 
33 enum kb_err_t{
34  KB_ERR_OK = 0,
35  KB_ERR_INVALID_PARAM = -1,
36  KB_ERR_INIT = -2,
37  KB_ERR_MUTEX = -3,
38 };
39 
40 
41 
42 
43 typedef enum
44 {
45  KB_PRESS = 0,
46  KB_RELEASE,
47  //KB_LONG_PRESS,
48 
49 } kb_event_t;
50 typedef struct {
51  uint8_t row;
52  uint8_t col;
53  uint8_t col_output;
54  uint32_t col_mask;
55  uint32_t row_mask;
56  int prio;
57  void * arg;
58  void (*cb)(void* arg, uint8_t row, uint8_t col, int event);
59 } kb_init_t;
60 
61 
62 
63 
64 
65 
66 
67 static __inline void kb_intr_mask_clear(uint32_t val)
68 {
69  WR_WORD(KEYBOARD_REG_INTR_MASK_CLEAR, val);
70 }
71 static __inline void kb_intr_clear(uint32_t val)
72 {
73  WR_WORD(KEYBOARD_REG_INTR_CLEAR, val);
74 }
75 static __inline void kb_intr_set(uint32_t val)
76 {
77  WR_WORD(KEYBOARD_REG_INTR_SET, val);
78 }
79 static __inline void kb_intr_mask_set(uint32_t val)
80 {
81  WR_WORD(KEYBOARD_REG_INTR_MASK_SET, val);
82 }
83 
84 static __inline void kb_enable(void)
85 {
86  uint32_t reg = RD_WORD(KEYBOARD_REG_MISC_CTRL);
87  reg |= KEYBOARD_REG_MISC_CTRL_CTL_ENABLE;
88  WR_WORD(KEYBOARD_REG_MISC_CTRL, reg);
89 }
90 static __inline void kb_disable(void)
91 {
92  uint32_t reg = RD_WORD(KEYBOARD_REG_MISC_CTRL);
93  reg &= ~KEYBOARD_REG_MISC_CTRL_CTL_ENABLE;
94  WR_WORD(KEYBOARD_REG_MISC_CTRL, reg);
95 }
96 static __inline void kb_wait_key(int wait)
97 {
98  uint32_t reg = RD_WORD(KEYBOARD_REG_MISC_CTRL);
99 
100  if (wait) {
101  reg |= KEYBOARD_REG_MISC_CTRL_CTL_WAIT_KEY_PRESS_BETWEEN_SCAN;
102  } else {
103  reg &= ~KEYBOARD_REG_MISC_CTRL_CTL_WAIT_KEY_PRESS_BETWEEN_SCAN;
104  }
105  WR_WORD(KEYBOARD_REG_MISC_CTRL, reg);
106 }
107 static __inline void kb_set_num_col(uint32_t val)
108 {
109  uint32_t reg = RD_WORD(KEYBOARD_REG_MISC_CTRL);
110  reg &= ~KEYBOARD_REG_MISC_CTRL_CTL_NUM_COL;
111  reg |= (val&KEYBOARD_REG_MISC_CTRL_CTL_NUM_COL_MASK)<<KEYBOARD_REG_MISC_CTRL_CTL_NUM_COL_SHIFT;
112  WR_WORD(KEYBOARD_REG_MISC_CTRL, reg);
113 }
114 static __inline void kb_set_num_row(uint32_t val)
115 {
116  uint32_t reg = RD_WORD(KEYBOARD_REG_MISC_CTRL);
117  reg &= ~KEYBOARD_REG_MISC_CTRL_CTL_NUM_ROW;
118  reg |= (val&KEYBOARD_REG_MISC_CTRL_CTL_NUM_ROW_MASK)<<KEYBOARD_REG_MISC_CTRL_CTL_NUM_ROW_SHIFT;
119  WR_WORD(KEYBOARD_REG_MISC_CTRL, reg);
120 }
121 static __inline void kb_set_max_num_key(uint32_t val)
122 {
123  uint32_t reg = RD_WORD(KEYBOARD_REG_MISC_CTRL);
124  reg &= ~KEYBOARD_REG_MISC_CTRL_CTL_MAX_NUM_KEY_PRESSED;
125  reg |= (val&KEYBOARD_REG_MISC_CTRL_CTL_MAX_NUM_KEY_PRESSED_MASK)<<KEYBOARD_REG_MISC_CTRL_CTL_MAX_NUM_KEY_PRESSED_SHIFT;
126  WR_WORD(KEYBOARD_REG_MISC_CTRL, reg);
127 }
128 static __inline void kb_col_output_inv(int val)
129 {
130  uint32_t reg = RD_WORD(KEYBOARD_REG_MISC_CTRL);
131 
132  if (val) {
133  reg |= KEYBOARD_REG_MISC_CTRL_CTL_COL_OUTPUT_INV;
134  } else {
135  reg &= ~KEYBOARD_REG_MISC_CTRL_CTL_COL_OUTPUT_INV;
136  }
137  WR_WORD(KEYBOARD_REG_MISC_CTRL, reg);
138 }
139 
140 
141 static __inline void kb_reset(void)
142 {
143  uint32_t val = RD_WORD(GLOBAL_REG_RESET_CTRL_1);
144 
145  WR_WORD(GLOBAL_REG_RESET_CTRL_1, val & (~0x2)); //GLOBAL_REG_RESET_CTRL_1_CTL_RESET_1_D0_CPU_KEYBOARD_RSTN_REG
146  delay_us(10);
147  WR_WORD(GLOBAL_REG_RESET_CTRL_1, val | 0x2);
148 }
149 
150 static __inline void kb_set_col_mask(uint32_t val)
151 {
152  uint32_t reg = RD_WORD(KEYBOARD_REG_COL_ROW_MASK);
153  reg &= ~KEYBOARD_REG_COL_ROW_MASK_CTL_KEYBOARD_COL_MASK;
154  reg |= (val&KEYBOARD_REG_COL_ROW_MASK_CTL_KEYBOARD_COL_MASK_MASK)<<KEYBOARD_REG_COL_ROW_MASK_CTL_KEYBOARD_COL_MASK_SHIFT;
155  WR_WORD(KEYBOARD_REG_COL_ROW_MASK, reg);
156 }
157 static __inline void kb_set_row_mask(uint32_t val)
158 {
159  uint32_t reg = RD_WORD(KEYBOARD_REG_COL_ROW_MASK);
160  reg &= ~KEYBOARD_REG_COL_ROW_MASK_CTL_KEYBOARD_ROW_MASK;
161  reg |= (val&KEYBOARD_REG_COL_ROW_MASK_CTL_KEYBOARD_ROW_MASK_MASK)<<KEYBOARD_REG_COL_ROW_MASK_CTL_KEYBOARD_ROW_MASK_SHIFT;
162  WR_WORD(KEYBOARD_REG_COL_ROW_MASK, reg);
163 }
164 static __inline void kb_set_scan_interval(uint32_t val)
165 {
166  WR_WORD(KEYBOARD_REG_SCAN_INTERVAL, val&KEYBOARD_REG_SCAN_INTERVAL_CTL_SCAN_INTERVAL_MASK);
167 }
168 static __inline void kb_set_us_cnt_lmt(uint32_t val)
169 {
170  uint32_t reg = RD_WORD(KEYBOARD_REG_SCAN_TIMING_CONTROL);
171  reg &= ~KEYBOARD_REG_SCAN_TIMING_CONTROL_CTL_US_CNT_LMT;
172  reg |= (val&KEYBOARD_REG_SCAN_TIMING_CONTROL_CTL_US_CNT_LMT_MASK)<<KEYBOARD_REG_SCAN_TIMING_CONTROL_CTL_US_CNT_LMT_SHIFT;
173  WR_WORD(KEYBOARD_REG_SCAN_TIMING_CONTROL, reg);
174 }
175 
176 static __inline void kb_set_deglitch(uint32_t val)
177 {
178  uint32_t reg = RD_WORD(KEYBOARD_REG_SCAN_TIMING_CONTROL);
179  reg &= ~KEYBOARD_REG_SCAN_TIMING_CONTROL_CTL_DEGLITCH_CNT_LMT;
180  reg |= (val&KEYBOARD_REG_SCAN_TIMING_CONTROL_CTL_DEGLITCH_CNT_LMT_MASK)<<KEYBOARD_REG_SCAN_TIMING_CONTROL_CTL_DEGLITCH_CNT_LMT_SHIFT;
181  WR_WORD(KEYBOARD_REG_SCAN_TIMING_CONTROL, reg);
182 }
183 
184 static __inline void kb_manual_start(int en)
185 {
186  uint32_t reg = RD_WORD(KEYBOARD_REG_MANUAL_START);
187  if (en) {
188  reg |= KEYBOARD_REG_MANUAL_START_CTL_MANUAL_START;
189  } else {
190  reg &= ~KEYBOARD_REG_MANUAL_START_CTL_MANUAL_START;
191  }
192  WR_WORD(KEYBOARD_REG_MANUAL_START, reg);
193 }
194 static __inline void kb_manual_set_pre_key(int en)
195 {
196  uint32_t reg = RD_WORD(KEYBOARD_REG_MANUAL_START);
197  if (en) {
198  reg |= KEYBOARD_REG_MANUAL_START_CTL_MANUAL_SET_PREV_KEY;
199  } else {
200  reg &= ~KEYBOARD_REG_MANUAL_START_CTL_MANUAL_SET_PREV_KEY;
201  }
202  WR_WORD(KEYBOARD_REG_MANUAL_START, reg);
203 }
204 static __inline void kb_manual_pre_num_key(uint8_t num)
205 {
206  uint32_t reg = RD_WORD(KEYBOARD_REG_MANUAL_START);
207  reg &= ~KEYBOARD_REG_MANUAL_START_CTL_MANUAL_PREV_NUM_KEY;
208  reg |= (num&KEYBOARD_REG_MANUAL_START_CTL_MANUAL_PREV_NUM_KEY_MASK)<<KEYBOARD_REG_MANUAL_START_CTL_MANUAL_PREV_NUM_KEY_SHIFT;
209  WR_WORD(KEYBOARD_REG_MANUAL_START, reg);
210 }
211 static __inline void kb_manual_key_press03(uint32_t val)
212 {
213  WR_WORD(KEYBOARD_REG_MANUAL_KEY_PRESS_0TO3, val);
214 }
215 static __inline void kb_manual_key_press45(uint32_t val)
216 {
217  WR_WORD(KEYBOARD_REG_MANUAL_KEY_PRESS_45, val);
218 }
219 static __inline uint32_t kb_get_curr_num(void)
220 {
221  return RD_WORD(KEYBOARD_REG_NUM_KEY_PRESS)&KEYBOARD_REG_NUM_KEY_PRESS_STS_CURR_NUM_KEY;
222 }
223 static __inline uint32_t kb_get_pre_num(void)
224 {
225  return (RD_WORD(KEYBOARD_REG_NUM_KEY_PRESS)&KEYBOARD_REG_NUM_KEY_PRESS_STS_PREV_NUM_KEY)>>KEYBOARD_REG_NUM_KEY_PRESS_STS_PREV_NUM_KEY_SHIFT;
226 }
227 static __inline void kb_pin_mux_0(int id, uint32_t mux)
228 {
229  uint32_t reg = RD_WORD(GLOBAL_REG_KEYBOARD_MUX_0);
230  reg &= ~(0x3<<(id*2));
231  reg |= (mux&0x3)<<(id*2);
232  WR_WORD(GLOBAL_REG_KEYBOARD_MUX_0, reg);
233 }
234 static __inline void kb_pin_mux_1(int id, uint32_t mux)
235 {
236  uint32_t reg = RD_WORD(GLOBAL_REG_KEYBOARD_MUX_1);
237  reg &= ~(0x3<<(id*2));
238  reg |= (mux&0x3)<<(id*2);
239  WR_WORD(GLOBAL_REG_KEYBOARD_MUX_1, reg);
240 }
241 
242 
243 /***********************************************************************************
244  * @brief Enable keyboad module
245  * @return KB_ERR_OK if successful, otherwise failed
246  * ************************************************************************************/
247 int hal_kb_enable(void);
248 
249 
250 /***********************************************************************************
251  * @brief Disable keyboad module
252  * @return KB_ERR_OK if successful, otherwise failed
253  * ************************************************************************************/
254 int hal_kb_disable(void);
255 
256 
257 /************************************************************************************
258  * @brief Open keyboard device
259  * @param[in] init init paramter, @see kb_init_t
260  * @return @see enum kb_err_t
261  * ***********************************************************************************/
262 int hal_kb_open(kb_init_t *init);
263 
264 /************************************************************************************
265  * @brief Close keyboard device
266  * @return None
267  * ***********************************************************************************/
268 void hal_kb_close(void);
269 
270 /************************************************************************************
271  * @brief Set scan interval
272  * @param[in] interval Scan interval after successful key detection, unit is us.
273  * @return @see enum kb_err_t
274  * ***********************************************************************************/
275 int hal_kb_set_scan_interval(uint32_t interval);
276 
277 /************************************************************************************
278  * @brief Set deglich count
279  * @param[in] cnt Number of scans with same result to claim a successful key detection.
280  * @return @see enum kb_err_t
281  * ***********************************************************************************/
282 int hal_kb_set_deglitch(uint32_t cnt);
284 
285 #endif
void RAM_PM delay_us(uint32_t us)
Delay in tight loop.