1 /*
2  * Copyright 2017, 2019 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 #ifndef _FSL_KPP_H_
8 #define _FSL_KPP_H_
9 
10 #include "fsl_common.h"
11 
12 /*!
13  * @addtogroup kpp
14  * @{
15  */
16 
17 /*******************************************************************************
18  * Definitions
19  ******************************************************************************/
20 
21 /*! @name Driver version */
22 /*@{*/
23 /*! @brief KPP driver version 2.0.0. */
24 #define FSL_KPP_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
25 /*@}*/
26 
27 #define KPP_KEYPAD_COLUMNNUM_MAX (8U)
28 #define KPP_KEYPAD_ROWNUM_MAX    (8U)
29 
30 /*! @brief List of interrupts supported by the peripheral. This
31  * enumeration uses one-bot encoding to allow a logical OR of multiple
32  * members. Members usually map to interrupt enable bits in one or more
33  * peripheral registers.
34  */
35 typedef enum _kpp_interrupt_enable
36 {
37     kKPP_keyDepressInterrupt = KPP_KPSR_KDIE_MASK, /*!< Keypad depress interrupt source */
38     kKPP_keyReleaseInterrupt = KPP_KPSR_KRIE_MASK  /*!< Keypad release interrupt source */
39 } kpp_interrupt_enable_t;
40 
41 /*! @brief Lists of KPP synchronize chain operation. */
42 typedef enum _kpp_sync_operation
43 {
44     kKPP_ClearKeyDepressSyncChain = KPP_KPSR_KDSC_MASK, /*!< Keypad depress interrupt status. */
45     kKPP_SetKeyReleasesSyncChain  = KPP_KPSR_KRSS_MASK, /*!< Keypad release interrupt status. */
46 } kpp_sync_operation_t;
47 
48 /*! @brief Lists of KPP status. */
49 typedef struct _kpp_config
50 {
51     uint8_t activeRow;    /*!< The row number: bit 7 ~ 0 represents the row 7 ~ 0. */
52     uint8_t activeColumn; /*!< The column number: bit 7 ~ 0 represents the column 7 ~ 0. */
53     uint16_t interrupt;   /*!< KPP interrupt source. A logical OR of "kpp_interrupt_enable_t". */
54 } kpp_config_t;
55 
56 /*******************************************************************************
57  * API
58  ******************************************************************************/
59 
60 #if defined(__cplusplus)
61 extern "C" {
62 #endif
63 
64 /*!
65  * @name Initialization and De-initialization
66  * @{
67  */
68 
69 /*!
70  * @brief KPP initialize.
71  * This function ungates the KPP clock and initializes KPP.
72  * This function must be called before calling any other KPP driver functions.
73  *
74  * @param base KPP peripheral base address.
75  * @param configure The KPP configuration structure pointer.
76  */
77 void KPP_Init(KPP_Type *base, kpp_config_t *configure);
78 
79 /*!
80  * @brief Deinitializes the KPP module and gates the clock.
81  * This function gates the KPP clock. As a result, the KPP
82  * module doesn't work after calling this function.
83  *
84  * @param base KPP peripheral base address.
85  */
86 void KPP_Deinit(KPP_Type *base);
87 
88 /* @} */
89 
90 /*!
91  * @name KPP Basic Operation
92  * @{
93  */
94 
95 /*!
96  * @brief Enable the interrupt.
97  *
98  * @param base KPP peripheral base address.
99  * @param mask  KPP interrupts to enable. This is a logical OR of the
100  *             enumeration :: kpp_interrupt_enable_t.
101  */
KPP_EnableInterrupts(KPP_Type * base,uint16_t mask)102 static inline void KPP_EnableInterrupts(KPP_Type *base, uint16_t mask)
103 {
104     uint16_t data = (uint16_t)(base->KPSR & ~(KPP_KPSR_KPKR_MASK | KPP_KPSR_KPKD_MASK));
105     base->KPSR    = data | mask;
106 }
107 
108 /*!
109  * @brief Disable the interrupt.
110  *
111  * @param base KPP peripheral base address.
112  * @param mask  KPP interrupts to disable. This is a logical OR of the
113  *             enumeration :: kpp_interrupt_enable_t.
114  */
KPP_DisableInterrupts(KPP_Type * base,uint16_t mask)115 static inline void KPP_DisableInterrupts(KPP_Type *base, uint16_t mask)
116 {
117     base->KPSR &= ~(mask | KPP_KPSR_KPKR_MASK | KPP_KPSR_KPKD_MASK);
118 }
119 
120 /*!
121  * @brief Gets the KPP interrupt event status.
122  *
123  * @param base KPP peripheral base address.
124  * @return The status of the KPP. Application can use the enum type in the "kpp_interrupt_enable_t"
125  * to get the right status of the related event.
126  */
KPP_GetStatusFlag(KPP_Type * base)127 static inline uint16_t KPP_GetStatusFlag(KPP_Type *base)
128 {
129     return (base->KPSR & (KPP_KPSR_KPKR_MASK | KPP_KPSR_KPKD_MASK)) << KPP_KPSR_KDIE_SHIFT;
130 }
131 
132 /*!
133  * @brief Clears KPP status flag.
134  *
135  * @param base KPP peripheral base address.
136  * @param mask KPP mask to be cleared. This is a logical OR of the
137  *             enumeration :: kpp_interrupt_enable_t.
138  */
KPP_ClearStatusFlag(KPP_Type * base,uint16_t mask)139 static inline void KPP_ClearStatusFlag(KPP_Type *base, uint16_t mask)
140 {
141     base->KPSR |= (uint16_t)((mask) >> KPP_KPSR_KDIE_SHIFT);
142 }
143 
144 /*!
145  * @brief Set KPP synchronization chain.
146  *
147  * @param base KPP peripheral base address.
148  * @param mask KPP mask to be cleared. This is a logical OR of the
149  *             enumeration :: kpp_sync_operation_t.
150  */
KPP_SetSynchronizeChain(KPP_Type * base,uint16_t mask)151 static inline void KPP_SetSynchronizeChain(KPP_Type *base, uint16_t mask)
152 {
153     uint16_t data = base->KPSR & (KPP_KPSR_KRSS_MASK | KPP_KPSR_KDSC_MASK | KPP_KPSR_KRIE_MASK | KPP_KPSR_KDIE_MASK);
154     base->KPSR    = data | mask;
155 }
156 
157 /*!
158  * @brief Keypad press scanning.
159  *
160  * This function will scanning all columns and rows. so
161  * all scanning data will be stored in the data pointer.
162  *
163  * @param base  KPP peripheral base address.
164  * @param data  KPP key press scanning data. The data buffer should be prepared with
165  * length at least equal to KPP_KEYPAD_COLUMNNUM_MAX * KPP_KEYPAD_ROWNUM_MAX.
166  * the data pointer is recommended to be a array like uint8_t data[KPP_KEYPAD_COLUMNNUM_MAX].
167  * for example the data[2] = 4, that means in column 1 row 2 has a key press event.
168  * @param clockSrc_Hz Source clock.
169  */
170 void KPP_keyPressScanning(KPP_Type *base, uint8_t *data, uint32_t clockSrc_Hz);
171 
172 /* @} */
173 
174 #if defined(__cplusplus)
175 }
176 #endif
177 
178 /*! @}*/
179 
180 #endif /* _FSL_KPP_H_*/
181