fsl_kpp.h 6.6 KB

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