fsl_kpp.h 6.8 KB
Newer Older
T
tanek liang 已提交
1
/*
2
 * The Clear BSD License
T
tanek liang 已提交
3
 * Copyright 2017 NXP
4 5
 * All rights reserved.
 * 
T
tanek liang 已提交
6
 * Redistribution and use in source and binary forms, with or without modification,
7 8
 * are permitted (subject to the limitations in the disclaimer below) provided
 *  that the following conditions are met:
T
tanek liang 已提交
9 10 11 12 13 14 15 16 17 18 19 20
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * o Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
21
 * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
T
tanek liang 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#ifndef _FSL_KPP_H_
#define _FSL_KPP_H_

#include "fsl_common.h"

/*!
 * @addtogroup kpp
 * @{
 */

/*******************************************************************************
 * Definitions
 ******************************************************************************/

/*! @name Driver version */
/*@{*/
/*! @brief KPP driver version 2.0.0. */
#define FSL_KPP_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
/*@}*/

#define KPP_KEYPAD_COLUMNNUM_MAX (8U)
#define KPP_KEYPAD_ROWNUM_MAX (8U)

/*! @brief List of interrupts supported by the peripheral. This
 * enumeration uses one-bot encoding to allow a logical OR of multiple
 * members. Members usually map to interrupt enable bits in one or more
 * peripheral registers.
 */
typedef enum _kpp_interrupt_enable {
    kKPP_keyDepressInterrupt = KPP_KPSR_KDIE_MASK, /*!< Keypad depress interrupt source */
    kKPP_keyReleaseInterrupt = KPP_KPSR_KRIE_MASK  /*!< Keypad release interrupt source */
} kpp_interrupt_enable_t;

/*! @brief Lists of KPP synchronize chain operation. */
typedef enum _kpp_sync_operation {
    kKPP_ClearKeyDepressSyncChain = KPP_KPSR_KDSC_MASK, /*!< Keypad depress interrupt status. */
    kKPP_SetKeyReleasesSyncChain = KPP_KPSR_KRSS_MASK,  /*!< Keypad release interrupt status. */
} kpp_sync_operation_t;

/*! @brief Lists of KPP status. */
typedef struct _kpp_config
{
    uint8_t activeRow;    /*!< The row number: bit 7 ~ 0 represents the row 7 ~ 0. */
    uint8_t activeColumn; /*!< The column number: bit 7 ~ 0 represents the column 7 ~ 0. */
    uint16_t interrupt;   /*!< KPP interrupt source. A logical OR of "kpp_interrupt_enable_t". */
} kpp_config_t;

/*******************************************************************************
 * API
 ******************************************************************************/

#if defined(__cplusplus)
extern "C" {
#endif

/*!
  * @name Initialization and De-initialization
  * @{
  */

/*!
 * @brief KPP initialize.
 * This function ungates the KPP clock and initializes KPP.
 * This function must be called before calling any other KPP driver functions.
 *
 * @param base KPP peripheral base address.
 * @param configure The KPP configuration structure pointer.
 */
void KPP_Init(KPP_Type *base, kpp_config_t *configure);

/*!
 * @brief Deinitializes the KPP module and gates the clock.
 * This function gates the KPP clock. As a result, the KPP
 * module doesn't work after calling this function.
 *
 * @param base KPP peripheral base address.
 */
void KPP_Deinit(KPP_Type *base);

/* @} */

/*!
 * @name KPP Basic Operation
 * @{
 */

/*!
 * @brief Enable the interrupt.
 *
 * @param base KPP peripheral base address.
 * @param mask  KPP interrupts to enable. This is a logical OR of the
 *             enumeration :: kpp_interrupt_enable_t.
 */
static inline void KPP_EnableInterrupts(KPP_Type *base, uint16_t mask)
{
    uint16_t data = base->KPSR & ~(KPP_KPSR_KPKR_MASK | KPP_KPSR_KPKD_MASK);
    base->KPSR = data | mask;
}

/*!
 * @brief Disable the interrupt.
 *
 * @param base KPP peripheral base address.
 * @param mask  KPP interrupts to disable. This is a logical OR of the
 *             enumeration :: kpp_interrupt_enable_t.
 */
static inline void KPP_DisableInterrupts(KPP_Type *base, uint16_t mask)
{
    base->KPSR &= ~(mask | KPP_KPSR_KPKR_MASK | KPP_KPSR_KPKD_MASK);
}

/*!
 * @brief Gets the KPP interrupt event status.
 *
 * @param base KPP peripheral base address.
 * @return The status of the KPP. Application can use the enum type in the "kpp_interrupt_enable_t"
 * to get the right status of the related event.
 */
static inline uint16_t KPP_GetStatusFlag(KPP_Type *base)
{
    return (base->KPSR & (KPP_KPSR_KPKR_MASK | KPP_KPSR_KPKD_MASK)) << KPP_KPSR_KDIE_SHIFT;
}

/*!
 * @brief Clears KPP status flag.
 *
 * @param base KPP peripheral base address.
 * @param mask KPP mask to be cleared. This is a logical OR of the
 *             enumeration :: kpp_interrupt_enable_t.
 */
static inline void KPP_ClearStatusFlag(KPP_Type *base, uint16_t mask)
{
    base->KPSR |= (uint16_t)((mask) >> KPP_KPSR_KDIE_SHIFT);
}

/*!
 * @brief Set KPP synchronization chain.
 *
 * @param base KPP peripheral base address.
 * @param mask KPP mask to be cleared. This is a logical OR of the
 *             enumeration :: kpp_sync_operation_t.
 */
static inline void KPP_SetSynchronizeChain(KPP_Type *base, uint16_t mask)
{
    uint16_t data = base->KPSR & (KPP_KPSR_KRSS_MASK | KPP_KPSR_KDSC_MASK | KPP_KPSR_KRIE_MASK | KPP_KPSR_KDIE_MASK);
    base->KPSR = data | mask;
}

/*!
 * @brief Keypad press scanning.
 *
 * This function will scanning all columns and rows. so
 * all scanning data will be stored in the data pointer.
 *
 * @param base  KPP peripheral base address.
 * @param data  KPP key press scanning data. The data buffer should be prepared with
 * length at least equal to KPP_KEYPAD_COLUMNNUM_MAX * KPP_KEYPAD_ROWNUM_MAX.
 * the data pointer is recommended to be a array like uint8_t data[KPP_KEYPAD_COLUMNNUM_MAX].
 * for example the data[2] = 4, that means in column 1 row 2 has a key press event.
 */
void KPP_keyPressScanning(KPP_Type *base, uint8_t *data, uint32_t clockSrc_Hz);

/* @} */

#if defined(__cplusplus)
}
#endif

/*! @}*/

#endif /* _FSL_KPP_H_*/