File indexing completed on 2025-05-11 08:22:59
0001
0002
0003
0004
0005
0006
0007
0008 #include "fsl_kpp.h"
0009
0010
0011
0012
0013
0014
0015 #ifndef FSL_COMPONENT_ID
0016 #define FSL_COMPONENT_ID "platform.drivers.kpp"
0017 #endif
0018
0019 #define KPP_KEYPAD_SCAN_TIMES (3U)
0020
0021
0022
0023
0024
0025
0026
0027
0028 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
0029
0030 static const clock_ip_name_t s_kppClock[FSL_FEATURE_SOC_KPP_COUNT] = KPP_CLOCKS;
0031 #endif
0032
0033
0034 static KPP_Type *const s_kppBases[] = KPP_BASE_PTRS;
0035
0036
0037 static const IRQn_Type s_kppIrqs[] = KPP_IRQS;
0038
0039
0040
0041 static uint32_t KPP_GetInstance(KPP_Type *base)
0042 {
0043 uint32_t instance;
0044
0045
0046 for (instance = 0; instance < ARRAY_SIZE(s_kppBases); instance++)
0047 {
0048 if (s_kppBases[instance] == base)
0049 {
0050 break;
0051 }
0052 }
0053
0054 assert(instance < ARRAY_SIZE(s_kppBases));
0055
0056 return instance;
0057 }
0058 static void KPP_Mdelay(uint64_t tickets)
0059 {
0060 while ((tickets--) != 0UL)
0061 {
0062 __NOP();
0063 }
0064 }
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074 void KPP_Init(KPP_Type *base, kpp_config_t *configure)
0075 {
0076 assert(configure);
0077
0078 uint32_t instance = KPP_GetInstance(base);
0079
0080 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
0081
0082 CLOCK_EnableClock(s_kppClock[KPP_GetInstance(base)]);
0083 #endif
0084
0085
0086 base->KPSR &= (uint16_t)(~(KPP_KPSR_KRIE_MASK | KPP_KPSR_KDIE_MASK));
0087
0088
0089 base->KPCR = KPP_KPCR_KRE(configure->activeRow);
0090 base->KPDR = KPP_KPDR_KCD((uint8_t) ~(configure->activeColumn));
0091 base->KPCR |= KPP_KPCR_KCO(configure->activeColumn);
0092
0093
0094 base->KDDR = KPP_KDDR_KCDD(configure->activeColumn) | KPP_KDDR_KRDD((uint8_t) ~(configure->activeRow));
0095
0096
0097 base->KPSR = KPP_KPSR_KPKR_MASK | KPP_KPSR_KPKD_MASK | KPP_KPSR_KDSC_MASK | configure->interrupt;
0098
0099 if ((configure->interrupt) != 0U)
0100 {
0101
0102 (void)EnableIRQ(s_kppIrqs[instance]);
0103 }
0104 }
0105
0106
0107
0108
0109
0110
0111
0112
0113 void KPP_Deinit(KPP_Type *base)
0114 {
0115
0116 base->KPSR &= (uint16_t)(~(KPP_KPSR_KRIE_MASK | KPP_KPSR_KDIE_MASK));
0117 base->KPCR = 0;
0118
0119 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
0120
0121 CLOCK_DisableClock(s_kppClock[KPP_GetInstance(base)]);
0122 #endif
0123 }
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138 void KPP_keyPressScanning(KPP_Type *base, uint8_t *data, uint32_t clockSrc_Hz)
0139 {
0140 assert(data);
0141
0142 uint16_t kppKCO = base->KPCR & KPP_KPCR_KCO_MASK;
0143 uint8_t columIndex = 0;
0144 uint8_t activeColumn = (uint8_t)((base->KPCR & KPP_KPCR_KCO_MASK) >> KPP_KPCR_KCO_SHIFT);
0145 uint8_t times;
0146 uint8_t rowData[KPP_KEYPAD_SCAN_TIMES][KPP_KEYPAD_COLUMNNUM_MAX];
0147 bool press = false;
0148 uint8_t column;
0149
0150
0151 (void)memset(&rowData[0][0], 0, sizeof(rowData));
0152
0153
0154
0155 base->KPDR = KPP_KPDR_KCD_MASK;
0156
0157 base->KPCR &= (uint16_t)(((uint16_t)~kppKCO) | KPP_KPCR_KRE_MASK);
0158
0159 base->KPCR |= kppKCO;
0160
0161 for (times = 0; times < KPP_KEYPAD_SCAN_TIMES; times++)
0162 {
0163 for (columIndex = 0; columIndex < KPP_KEYPAD_COLUMNNUM_MAX; columIndex++)
0164 {
0165 column = activeColumn & (1U << columIndex);
0166 if (column != 0U)
0167 {
0168
0169 base->KPDR = KPP_KPDR_KCD(~(uint16_t)column);
0170
0171 KPP_Mdelay(((uint64_t)clockSrc_Hz / 10000000UL));
0172
0173 rowData[times][columIndex] = (uint8_t)(~(base->KPDR & KPP_KPDR_KRD_MASK));
0174 }
0175 else
0176 {
0177
0178 rowData[times][columIndex] = 0;
0179 }
0180 }
0181 }
0182
0183
0184 base->KPDR &= (uint16_t)(~KPP_KPDR_KCD_MASK);
0185
0186
0187 for (columIndex = 0; columIndex < KPP_KEYPAD_COLUMNNUM_MAX; columIndex++)
0188 {
0189 if (((uint8_t)(rowData[0][columIndex] & rowData[1][columIndex]) & rowData[2][columIndex]) != 0U)
0190 {
0191 press = true;
0192 }
0193 }
0194
0195 if (press)
0196 {
0197 (void)memcpy(data, &rowData[0][0], sizeof(rowData[0]));
0198 }
0199 else
0200 {
0201 (void)memset(data, 0, sizeof(rowData[0]));
0202 }
0203 }