Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:22:59

0001 /*
0002  * Copyright 2017, 2019 NXP
0003  * All rights reserved.
0004  *
0005  * SPDX-License-Identifier: BSD-3-Clause
0006  */
0007 
0008 #include "fsl_kpp.h"
0009 
0010 /*******************************************************************************
0011  * Definitions
0012  ******************************************************************************/
0013 
0014 /* Component ID definition, used by tools. */
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  * Prototypes
0022  ******************************************************************************/
0023 
0024 /*******************************************************************************
0025  * Variables
0026  ******************************************************************************/
0027 
0028 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
0029 /*! @brief Pointers to SEMC clocks for each instance. */
0030 static const clock_ip_name_t s_kppClock[FSL_FEATURE_SOC_KPP_COUNT] = KPP_CLOCKS;
0031 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
0032 
0033 /*! @brief Pointers to SEMC bases for each instance. */
0034 static KPP_Type *const s_kppBases[] = KPP_BASE_PTRS;
0035 
0036 /*! @brief Pointers to KPP IRQ number for each instance. */
0037 static const IRQn_Type s_kppIrqs[] = KPP_IRQS;
0038 /*******************************************************************************
0039  * Code
0040  ******************************************************************************/
0041 static uint32_t KPP_GetInstance(KPP_Type *base)
0042 {
0043     uint32_t instance;
0044 
0045     /* Find the instance index from base address mappings. */
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  * brief KPP initialize.
0068  * This function ungates the KPP clock and initializes KPP.
0069  * This function must be called before calling any other KPP driver functions.
0070  *
0071  * param base KPP peripheral base address.
0072  * param configure The KPP configuration structure pointer.
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     /* Un-gate sdram controller clock. */
0082     CLOCK_EnableClock(s_kppClock[KPP_GetInstance(base)]);
0083 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
0084 
0085     /* Clear all. */
0086     base->KPSR &= (uint16_t)(~(KPP_KPSR_KRIE_MASK | KPP_KPSR_KDIE_MASK));
0087 
0088     /* Enable the keypad row and set the column strobe output to open drain. */
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     /* Set the input direction for row and output direction for column. */
0094     base->KDDR = KPP_KDDR_KCDD(configure->activeColumn) | KPP_KDDR_KRDD((uint8_t) ~(configure->activeRow));
0095 
0096     /* Clear the status flag and enable the interrupt. */
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         /* Enable at the Interrupt */
0102         (void)EnableIRQ(s_kppIrqs[instance]);
0103     }
0104 }
0105 
0106 /*!
0107  * brief Deinitializes the KPP module and gates the clock.
0108  * This function gates the KPP clock. As a result, the KPP
0109  * module doesn't work after calling this function.
0110  *
0111  * param base KPP peripheral base address.
0112  */
0113 void KPP_Deinit(KPP_Type *base)
0114 {
0115     /* Disable interrupts and disable all rows. */
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     /* Disable KPP clock. */
0121     CLOCK_DisableClock(s_kppClock[KPP_GetInstance(base)]);
0122 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
0123 }
0124 
0125 /*!
0126  * brief Keypad press scanning.
0127  *
0128  * This function will scanning all columns and rows. so
0129  * all scanning data will be stored in the data pointer.
0130  *
0131  * param base  KPP peripheral base address.
0132  * param data  KPP key press scanning data. The data buffer should be prepared with
0133  * length at least equal to KPP_KEYPAD_COLUMNNUM_MAX * KPP_KEYPAD_ROWNUM_MAX.
0134  * the data pointer is recommended to be a array like uint8_t data[KPP_KEYPAD_COLUMNNUM_MAX].
0135  * for example the data[2] = 4, that means in column 1 row 2 has a key press event.
0136  * param clockSrc_Hz Source clock.
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     /* Initialize row data to zero. */
0151     (void)memset(&rowData[0][0], 0, sizeof(rowData));
0152 
0153     /* Scanning. */
0154     /* Configure the column data to 1 according to column numbers. */
0155     base->KPDR = KPP_KPDR_KCD_MASK;
0156     /* Configure column to totem pole for quick discharge of keypad capacitance. */
0157     base->KPCR &= (uint16_t)(((uint16_t)~kppKCO) | KPP_KPCR_KRE_MASK);
0158     /* Recover. */
0159     base->KPCR |= kppKCO;
0160     /* Three times scanning. */
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                 /* Set the single column line to 0. */
0169                 base->KPDR = KPP_KPDR_KCD(~(uint16_t)column);
0170                 /* Take 100us delays. */
0171                 KPP_Mdelay(((uint64_t)clockSrc_Hz / 10000000UL));
0172                 /* Read row data. */
0173                 rowData[times][columIndex] = (uint8_t)(~(base->KPDR & KPP_KPDR_KRD_MASK));
0174             }
0175             else
0176             {
0177                 /* Read row data. */
0178                 rowData[times][columIndex] = 0;
0179             }
0180         }
0181     }
0182 
0183     /* Return all columns to 0 in preparation for standby mode. */
0184     base->KPDR &= (uint16_t)(~KPP_KPDR_KCD_MASK);
0185 
0186     /* Check if three time scan data is the same. */
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 }