fsl_kpp.h 5.3 KB

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