1 /******************************************************************************
2 **
3 * @brief header file for KBI.
4 *
5 *******************************************************************************
6 *
7 * provide APIs for accessing KBI
8 ******************************************************************************/
9 #ifndef _KBI_H_
10 #define _KBI_H_
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 /******************************************************************************
15 * Includes
16 ******************************************************************************/
17 
18 /******************************************************************************
19 * Constants
20 ******************************************************************************/
21 
22 /*!
23 * @brief KBI MODE select enum.
24 *
25 */
26 typedef enum
27 {
28     KBI_MODE_EDGE_ONLY = 0,             /*!< select  edge only mode  */
29     KBI_MODE_EDGE_LEVEL                 /*!< select  both edge and level  mode */
30 }KBI_ModeType;
31 
32 
33  /*!
34  * @brief KBI Edge select enum.
35  *
36  */
37 typedef enum
38 {
39     KBI_FALLING_EDGE_LOW_LEVEL = 0,     /*!< select falling edge and/or low level  */
40     KBI_RISING_EDGE_HIGH_LEVEL          /*!< select rising edge and/or high level  */
41 }KBI_EdgeType;
42 
43 
44 /******************************************************************************
45 * Macros
46 ******************************************************************************/
47 
48 /******************************************************************************
49 * KBI module max number and port pins definition
50 *
51 *//*! @addtogroup kbi_macro
52 * @{
53 *******************************************************************************/
54 #define KBI_MAX_NO              2                  /*!< max number of modules */
55 
56 #if defined(CPU_NV32)|| defined(CPU_NV32M3)
57    #define KBI_MAX_PINS_PER_PORT   8                  /*!< max number of pins */
58 #elif defined(CPU_NV32M4)
59    #define KBI_MAX_PINS_PER_PORT   32                  /*!< max number of pins */
60 #endif
61 /*! @} End of kbi_macro                                                    */
62 
63 
64 /******************************************************************************
65 * Types
66 ******************************************************************************/
67 
68 /*! @brief KBI_CALLBACK function declaration                                  */
69 typedef void (*KBI_CallbackType)(void);
70 /*! @} End of kbi_callback                                                    */
71 
72 
73 
74 /******************************************************************************
75 * KBI pin config struct
76 *
77 *//*! @addtogroup kbi_pinconfigstruct
78 * @{
79 *******************************************************************************/
80 /*!
81 * @brief KBI pin enable and edge select struct.
82 *
83 */
84 
85 typedef struct
86 {
87     uint8_t     bEdge   : 1;                                /*!< edge/level select bit */
88     uint8_t     bEn     : 1;                                /*!< pin enable bit */
89     uint8_t     bRsvd   : 6;                                /*!< reserved */
90 } KBI_PinConfigType;
91 /*! @} End of kbi_pinconfigstruct                                             */
92 
93 
94 /******************************************************************************
95 * KBI config struct
96 *
97 *//*! @addtogroup kbi_configstruct
98 * @{
99 *******************************************************************************/
100 /*!
101  * @brief KBI status and control struct.
102  *
103  */
104 
105 typedef struct
106 {
107 #if defined(CPU_NV32)|| defined(CPU_NV32M3)
108     struct
109     {
110         uint8_t     bMode   : 1;                            /*!< KBI detection mode select */
111         uint8_t     bIntEn  : 1;                            /*!< KBI interrupt enable bit */
112         uint8_t     bRsvd   : 6;                            /*!< reserved */
113     } sBits;
114 #elif  defined(CPU_NV32M4)
115     struct
116     {
117         uint32_t     bMode   : 1;                            /*!< KBI detection mode select */
118         uint32_t     bIntEn  : 1;                            /*!< KBI interrupt enable bit */
119         uint32_t     bRsvd2  : 2;                           /*!< reserved */
120         uint32_t     bKbspEn : 1;							  /*!<Real KBI_SP register enable*/
121 		uint32_t     bRstKbsp: 1;                            /*!<Reset KBI_SP register*/
122         uint32_t     bRsvd26 : 26;                           /*!< reserved */
123     } sBits;
124 #endif
125     KBI_PinConfigType   sPin[KBI_MAX_PINS_PER_PORT];
126 } KBI_ConfigType, *KBI_ConfigTypePtr;
127 /*! @} End of kbi_configstruct                                                */
128 
129 /******************************************************************************
130 * Global variables
131 ******************************************************************************/
132 /*!
133  * inline functions
134  */
135 /******************************************************************************
136 * KBI api list
137 *
138 *//*! @addtogroup kbi_api_list
139 * @{
140 *******************************************************************************/
141 /*****************************************************************************//*!
142 *
143 * @brief set detect falling edge only.
144 *
145 * @param[in]   pKBI          pointer to KBI module.
146 * @param[in]   PinMasks      indicate pin numbers.
147 *
148 * @return none.
149 *
150 * @ Pass/ Fail criteria: none.
151 *
152 * @see KBI_DetectRisingEdge.
153 *
154 *****************************************************************************/
155 #if defined(CPU_NV32)|| defined(CPU_NV32M3)
KBI_DetectFallingEdge(KBI_Type * pKBI,uint8_t PinMasks)156 __STATIC_INLINE  void KBI_DetectFallingEdge(KBI_Type *pKBI, uint8_t PinMasks)
157 #elif defined(CPU_NV32M4)
158 __STATIC_INLINE  void KBI_DetectFallingEdge(KBI_Type *pKBI, uint32_t PinMasks)
159 #endif
160 {
161     pKBI->SC &= ~KBI_SC_KBMOD_MASK;
162     pKBI->ES &= ~(PinMasks);
163 }
164 
165 /*****************************************************************************//*!
166 *
167 * @brief set detect falling edge only.
168 *
169 * @param[in]   pKBI         pointer to KBI module.
170 * @param[in]   PinMasks     indicate pin numbers.
171 *
172 * @return none.
173 *
174 * @ Pass/ Fail criteria: none.
175 *
176 * @see KBI_DetectFallingEdge.
177 *
178 *****************************************************************************/
179 #if defined(CPU_NV32)|| defined(CPU_NV32M3)
KBI_DetectRisingEdge(KBI_Type * pKBI,uint8_t PinMasks)180 __STATIC_INLINE  void KBI_DetectRisingEdge(KBI_Type *pKBI, uint8_t PinMasks)
181 #elif defined(CPU_NV32M4)
182 __STATIC_INLINE  void KBI_DetectRisingEdge(KBI_Type *pKBI, uint32_t PinMasks)
183 #endif
184 {
185     pKBI->SC &= ~KBI_SC_KBMOD_MASK;
186     pKBI->ES |= (PinMasks);
187 }
188 
189 /*****************************************************************************//*!
190 *
191 * @brief set detect falling edge only.
192 *
193 * @param[in]   pKBI         pointer to KBI module.
194 * @param[in]   PinMasks     indicate pin number/mask.
195 *
196 * @return none.
197 *
198 * @ Pass/ Fail criteria: none.
199 *
200 * @see KBI_DetectFallingEdgeLowLevel.
201 *
202 *****************************************************************************/
203 #if defined(CPU_NV32)|| defined(CPU_NV32M3)
KBI_DetectRisingEdgeHighLevel(KBI_Type * pKBI,uint8_t PinMasks)204 __STATIC_INLINE  void KBI_DetectRisingEdgeHighLevel(KBI_Type *pKBI, uint8_t PinMasks)
205 #elif defined(CPU_NV32M4)
206 __STATIC_INLINE  void KBI_DetectRisingEdgeHighLevel(KBI_Type *pKBI, uint32_t PinMasks)
207 #endif
208 {
209     pKBI->SC |= KBI_SC_KBMOD_MASK;
210     pKBI->ES |= (PinMasks);
211 }
212 
213 /*****************************************************************************//*!
214 *
215 * @brief set detect falling edge only.
216 *
217 * @param[in]   pKBI         pointer to KBI module.
218 * @param[in]   PinMasks     indicate pin number/mask.
219 *
220 * @return none.
221 *
222 * @ Pass/ Fail criteria: none.
223 *
224 * @see KBI_DetectRisingEdgeHighLevel.
225 *
226 *****************************************************************************/
227 #if defined(CPU_NV32)|| defined(CPU_NV32M3)
KBI_DetectFallingEdgeLowLevel(KBI_Type * pKBI,uint8_t PinMasks)228 __STATIC_INLINE  void KBI_DetectFallingEdgeLowLevel(KBI_Type *pKBI, uint8_t PinMasks)
229 #elif defined(CPU_NV32M4)
230 __STATIC_INLINE  void KBI_DetectFallingEdgeLowLevel(KBI_Type *pKBI, uint32_t PinMasks)
231 #endif
232 {
233     pKBI->SC |= KBI_SC_KBMOD_MASK;
234     pKBI->ES &= ~(PinMasks);
235 }
236 
237 /*****************************************************************************//*!
238 *
239 * @brief enable the pin specified.
240 *
241 * @param[in]   pKBI         pointer to KBI module.
242 * @param[in]   PinMasks     indicate pin number/mask.
243 *
244 * @return none.
245 *
246 * @ Pass/ Fail criteria: none.
247 *
248 * @see KBI_Disable.
249 *
250 *****************************************************************************/
251 #if defined(CPU_NV32)|| defined(CPU_NV32M3)
KBI_Enable(KBI_Type * pKBI,uint8_t PinMasks)252 __STATIC_INLINE  void KBI_Enable(KBI_Type *pKBI, uint8_t PinMasks)
253 #elif defined(CPU_NV32M4)
254 __STATIC_INLINE  void KBI_Enable(KBI_Type *pKBI, uint32_t PinMasks)
255 #endif
256 {
257     pKBI->PE |= (PinMasks);
258 }
259 
260 /*****************************************************************************//*!
261 *
262 * @brief disable the pin specified.
263 *
264 * @param[in]   pKBI         pointer to KBI module.
265 * @param[in]   PinMasks     indicate pin number/mask.
266 *
267 * @return none.
268 *
269 * @ Pass/ Fail criteria: none.
270 *
271 * @see KBI_Enable.
272 *
273 *****************************************************************************/
274 #if defined(CPU_NV32)|| defined(CPU_NV32M3)
KBI_Disable(KBI_Type * pKBI,uint8_t PinMasks)275 __STATIC_INLINE  void KBI_Disable(KBI_Type *pKBI, uint8_t PinMasks)
276 #elif defined(CPU_NV32M4)
277 __STATIC_INLINE  void KBI_Disable(KBI_Type *pKBI, uint32_t PinMasks)
278 #endif
279 {
280     pKBI->PE &= ~(PinMasks);
281 }
282 
283 /*****************************************************************************//*!
284 *
285 * @brief enable the corresponding interrupt.
286 *
287 * @param[in]   pKBI         pointer to KBI module.
288 *
289 * @return none.
290 *
291 * @ Pass/ Fail criteria: none.
292 *
293 * @see KBI_DisableInt.
294 *
295 *****************************************************************************/
KBI_EnableInt(KBI_Type * pKBI)296 __STATIC_INLINE  void KBI_EnableInt(KBI_Type *pKBI)
297 {
298     pKBI->SC |= KBI_SC_KBIE_MASK;
299 }
300 
301 
302 /*****************************************************************************//*!
303 *
304 * @brief disable the corresponding interrupt.
305 *
306 * @param[in]  pKBI         pointer to KBI module.
307 *
308 * @return none.
309 *
310 * @ Pass/ Fail criteria: none
311 *
312 * @see KBI_EnableInt.
313 *
314 *****************************************************************************/
KBI_DisableInt(KBI_Type * pKBI)315 __STATIC_INLINE  void KBI_DisableInt(KBI_Type *pKBI)
316 {
317     pKBI->SC &= ~KBI_SC_KBIE_MASK;
318 }
319 
320 /*****************************************************************************//*!
321 *
322 * @brief Get the corresponding status flag bits.
323 *
324 * @param[in]   pKBI         pointer to KBI module.
325 *
326 * @return uint8_t.
327 *
328 * @ Pass/ Fail criteria: none.
329 *
330 * @see KBI_ClrFlags.
331 *
332 *****************************************************************************/
333 #if defined(CPU_NV32)|| defined(CPU_NV32M3)
KBI_GetFlags(KBI_Type * pKBI)334 __STATIC_INLINE  uint8_t KBI_GetFlags(KBI_Type *pKBI)
335 #elif defined(CPU_NV32M4)
336 __STATIC_INLINE  uint32_t KBI_GetFlags(KBI_Type *pKBI)
337 #endif
338 {
339     return (pKBI->SC & KBI_SC_KBF_MASK);
340 }
341 
342 /*****************************************************************************//*!
343 *
344 * @brief clear the corresponding status flag bits.
345 *
346 * @param[in]   pKBI         pointer to KBI module
347 *
348 * @return none.
349 *
350 * @ Pass/ Fail criteria: none
351 *
352 * @see KBI_GetFlags.
353 *
354 *****************************************************************************/
KBI_ClrFlags(KBI_Type * pKBI)355 __STATIC_INLINE  void KBI_ClrFlags(KBI_Type *pKBI)
356 {
357     pKBI->SC |= KBI_SC_KBACK_MASK;
358 }
359 
360 #if defined(CPU_NV32M4)
361 /*****************************************************************************//*!
362 *
363 * @brief Real KBI_SP register enable.
364 *
365 * @param[in]   pKBI         pointer to KBI module
366 *
367 * @return none.
368 *
369 * @ Pass/ Fail criteria: none
370 *
371 * @see The real ETMe value of Keyboard source pin to be read.
372 *
373 *****************************************************************************/
KBI_SPEnable(KBI_Type * pKBI)374 __STATIC_INLINE  void KBI_SPEnable(KBI_Type *pKBI)
375 {
376     pKBI->SC |= KBI_SC_KBSPEN_MASK;
377 }
378 
379 /*****************************************************************************//*!
380 *
381 * @brief Get KBI source pin register fields.
382 *
383 * @param[in]   pKBI         pointer to KBI module.
384 *
385 * @return uint32_t.
386 *
387 * @ Pass/ Fail criteria: none.
388 *
389 * @see KBI_GetSP.
390 *
391 *****************************************************************************/
KBI_GetSP(KBI_Type * pKBI)392 __STATIC_INLINE  uint32_t KBI_GetSP(KBI_Type *pKBI)
393 {
394     return (pKBI->SP & KBI_SP_SP_MASK);
395 }
396 
397 /*****************************************************************************//*!
398 *
399 * @brief Reset KBI_SP register.
400 *
401 * @param[in]   pKBI         pointer to KBI module
402 *
403 * @return none.
404 *
405 * @ Pass/ Fail criteria: none
406 *
407 * @see KBI_RstSP.
408 *
409 *****************************************************************************/
KBI_RstSP(KBI_Type * pKBI)410 __STATIC_INLINE  void KBI_RstSP(KBI_Type *pKBI)
411 {
412     pKBI->SC |= KBI_SC_RSTKBSP_MASK;
413 }
414 #endif
415 
416 /*! @} End of kbi_api_list                                                  */
417 
418 /******************************************************************************
419 * Global functions
420 ******************************************************************************/
421 
422 void KBI_Init(KBI_Type *pKBI, KBI_ConfigType *pConfig);
423 void KBI_SetCallback(KBI_Type *pKBI, KBI_CallbackType pfnCallback);
424 
425 #ifdef __cplusplus
426 }
427 #endif
428 #endif
429