fsl_kpp.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * The Clear BSD License
  3. * Copyright 2017 NXP
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted (subject to the limitations in the disclaimer below) provided
  8. * that the following conditions are met:
  9. *
  10. * o Redistributions of source code must retain the above copyright notice, this list
  11. * of conditions and the following disclaimer.
  12. *
  13. * o Redistributions in binary form must reproduce the above copyright notice, this
  14. * list of conditions and the following disclaimer in the documentation and/or
  15. * other materials provided with the distribution.
  16. *
  17. * o Neither the name of the copyright holder nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  26. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  29. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #ifndef _FSL_KPP_H_
  34. #define _FSL_KPP_H_
  35. #include "fsl_common.h"
  36. /*!
  37. * @addtogroup kpp
  38. * @{
  39. */
  40. /*******************************************************************************
  41. * Definitions
  42. ******************************************************************************/
  43. /*! @name Driver version */
  44. /*@{*/
  45. /*! @brief KPP driver version 2.0.0. */
  46. #define FSL_KPP_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
  47. /*@}*/
  48. #define KPP_KEYPAD_COLUMNNUM_MAX (8U)
  49. #define KPP_KEYPAD_ROWNUM_MAX (8U)
  50. /*! @brief List of interrupts supported by the peripheral. This
  51. * enumeration uses one-bot encoding to allow a logical OR of multiple
  52. * members. Members usually map to interrupt enable bits in one or more
  53. * peripheral registers.
  54. */
  55. typedef enum _kpp_interrupt_enable {
  56. kKPP_keyDepressInterrupt = KPP_KPSR_KDIE_MASK, /*!< Keypad depress interrupt source */
  57. kKPP_keyReleaseInterrupt = KPP_KPSR_KRIE_MASK /*!< Keypad release interrupt source */
  58. } kpp_interrupt_enable_t;
  59. /*! @brief Lists of KPP synchronize chain operation. */
  60. typedef enum _kpp_sync_operation {
  61. kKPP_ClearKeyDepressSyncChain = KPP_KPSR_KDSC_MASK, /*!< Keypad depress interrupt status. */
  62. kKPP_SetKeyReleasesSyncChain = KPP_KPSR_KRSS_MASK, /*!< Keypad release interrupt status. */
  63. } kpp_sync_operation_t;
  64. /*! @brief Lists of KPP status. */
  65. typedef struct _kpp_config
  66. {
  67. uint8_t activeRow; /*!< The row number: bit 7 ~ 0 represents the row 7 ~ 0. */
  68. uint8_t activeColumn; /*!< The column number: bit 7 ~ 0 represents the column 7 ~ 0. */
  69. uint16_t interrupt; /*!< KPP interrupt source. A logical OR of "kpp_interrupt_enable_t". */
  70. } kpp_config_t;
  71. /*******************************************************************************
  72. * API
  73. ******************************************************************************/
  74. #if defined(__cplusplus)
  75. extern "C" {
  76. #endif
  77. /*!
  78. * @name Initialization and De-initialization
  79. * @{
  80. */
  81. /*!
  82. * @brief KPP initialize.
  83. * This function ungates the KPP clock and initializes KPP.
  84. * This function must be called before calling any other KPP driver functions.
  85. *
  86. * @param base KPP peripheral base address.
  87. * @param configure The KPP configuration structure pointer.
  88. */
  89. void KPP_Init(KPP_Type *base, kpp_config_t *configure);
  90. /*!
  91. * @brief Deinitializes the KPP module and gates the clock.
  92. * This function gates the KPP clock. As a result, the KPP
  93. * module doesn't work after calling this function.
  94. *
  95. * @param base KPP peripheral base address.
  96. */
  97. void KPP_Deinit(KPP_Type *base);
  98. /* @} */
  99. /*!
  100. * @name KPP Basic Operation
  101. * @{
  102. */
  103. /*!
  104. * @brief Enable the interrupt.
  105. *
  106. * @param base KPP peripheral base address.
  107. * @param mask KPP interrupts to enable. This is a logical OR of the
  108. * enumeration :: kpp_interrupt_enable_t.
  109. */
  110. static inline void KPP_EnableInterrupts(KPP_Type *base, uint16_t mask)
  111. {
  112. uint16_t data = base->KPSR & ~(KPP_KPSR_KPKR_MASK | KPP_KPSR_KPKD_MASK);
  113. base->KPSR = data | mask;
  114. }
  115. /*!
  116. * @brief Disable the interrupt.
  117. *
  118. * @param base KPP peripheral base address.
  119. * @param mask KPP interrupts to disable. This is a logical OR of the
  120. * enumeration :: kpp_interrupt_enable_t.
  121. */
  122. static inline void KPP_DisableInterrupts(KPP_Type *base, uint16_t mask)
  123. {
  124. base->KPSR &= ~(mask | KPP_KPSR_KPKR_MASK | KPP_KPSR_KPKD_MASK);
  125. }
  126. /*!
  127. * @brief Gets the KPP interrupt event status.
  128. *
  129. * @param base KPP peripheral base address.
  130. * @return The status of the KPP. Application can use the enum type in the "kpp_interrupt_enable_t"
  131. * to get the right status of the related event.
  132. */
  133. static inline uint16_t KPP_GetStatusFlag(KPP_Type *base)
  134. {
  135. return (base->KPSR & (KPP_KPSR_KPKR_MASK | KPP_KPSR_KPKD_MASK)) << KPP_KPSR_KDIE_SHIFT;
  136. }
  137. /*!
  138. * @brief Clears KPP status flag.
  139. *
  140. * @param base KPP peripheral base address.
  141. * @param mask KPP mask to be cleared. This is a logical OR of the
  142. * enumeration :: kpp_interrupt_enable_t.
  143. */
  144. static inline void KPP_ClearStatusFlag(KPP_Type *base, uint16_t mask)
  145. {
  146. base->KPSR |= (uint16_t)((mask) >> KPP_KPSR_KDIE_SHIFT);
  147. }
  148. /*!
  149. * @brief Set KPP synchronization chain.
  150. *
  151. * @param base KPP peripheral base address.
  152. * @param mask KPP mask to be cleared. This is a logical OR of the
  153. * enumeration :: kpp_sync_operation_t.
  154. */
  155. static inline void KPP_SetSynchronizeChain(KPP_Type *base, uint16_t mask)
  156. {
  157. uint16_t data = base->KPSR & (KPP_KPSR_KRSS_MASK | KPP_KPSR_KDSC_MASK | KPP_KPSR_KRIE_MASK | KPP_KPSR_KDIE_MASK);
  158. base->KPSR = data | mask;
  159. }
  160. /*!
  161. * @brief Keypad press scanning.
  162. *
  163. * This function will scanning all columns and rows. so
  164. * all scanning data will be stored in the data pointer.
  165. *
  166. * @param base KPP peripheral base address.
  167. * @param data KPP key press scanning data. The data buffer should be prepared with
  168. * length at least equal to KPP_KEYPAD_COLUMNNUM_MAX * KPP_KEYPAD_ROWNUM_MAX.
  169. * the data pointer is recommended to be a array like uint8_t data[KPP_KEYPAD_COLUMNNUM_MAX].
  170. * for example the data[2] = 4, that means in column 1 row 2 has a key press event.
  171. */
  172. void KPP_keyPressScanning(KPP_Type *base, uint8_t *data, uint32_t clockSrc_Hz);
  173. /* @} */
  174. #if defined(__cplusplus)
  175. }
  176. #endif
  177. /*! @}*/
  178. #endif /* _FSL_KPP_H_*/