kbi.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /******************************************************************************
  2. *
  3. * @brief providing APIs for configuring KBI.
  4. *
  5. *******************************************************************************
  6. *
  7. * provide APIs for configuring KBI
  8. ******************************************************************************/
  9. #include "common.h"
  10. #include "kbi.h"
  11. /******************************************************************************
  12. * External objects
  13. ******************************************************************************/
  14. /******************************************************************************
  15. * Global variables
  16. ******************************************************************************/
  17. KBI_CallbackType KBI_Callback[KBI_MAX_NO] = {(KBI_CallbackType)NULL};
  18. /******************************************************************************
  19. * Constants and macros
  20. ******************************************************************************/
  21. /******************************************************************************
  22. * Local types
  23. ******************************************************************************/
  24. /******************************************************************************
  25. * Local function prototypes
  26. ******************************************************************************/
  27. /******************************************************************************
  28. * Local variables
  29. ******************************************************************************/
  30. /******************************************************************************
  31. * Local functions
  32. ******************************************************************************/
  33. /******************************************************************************
  34. * KBI api list
  35. *
  36. *//*! @addtogroup kbi_api_list
  37. * @{
  38. *******************************************************************************/
  39. /******************************************************************************
  40. * Global functions
  41. ******************************************************************************/
  42. /*****************************************************************************//*!
  43. *
  44. * @brief initialize KBI module.
  45. *
  46. * @param[in] pKBI pointer to KBI module.
  47. * @param[in] pConfig pointer to KBI configuration structure.
  48. *
  49. * @return none.
  50. *
  51. * @ Pass/ Fail criteria: none.
  52. *
  53. * @see KBI_DeInit.
  54. *
  55. *****************************************************************************/
  56. void KBI_Init(KBI_Type *pKBI, KBI_ConfigType *pConfig)
  57. {
  58. #if defined(CPU_NV32)
  59. uint16_t i;
  60. uint8_t sc = 0;
  61. uint8_t u8Port;
  62. uint8_t u8PinPos;
  63. uint16_t u16PinMapping[KBI_MAX_NO][8] =
  64. {
  65. {
  66. 0, 1, 2, 3, 8, 9, 10, 11 /* KBI0 pins position in GPIOA register */
  67. },
  68. {
  69. 24, 25, 26, 27, 28, 29, 30, 31 /* KBI1 pins position in GPIOA register */
  70. }
  71. };
  72. #elif defined(CPU_NV32M3)
  73. uint16_t i;
  74. uint8_t sc = 0;
  75. uint8_t u8Port;
  76. uint8_t u8PinPos;
  77. uint16_t u16PinMapping[KBI_MAX_NO][8] =
  78. {
  79. {
  80. 0, 1, 2, 3, 8, 9, 10, 11 /* KBI0 pins position in GPIOA register */
  81. },
  82. {
  83. 20, 21, 16, 17, 18, 19, 12, 13 /* KBI1 pins position in GPIOA register */
  84. }
  85. };
  86. #elif defined(CPU_NV32M4)
  87. uint32_t i;
  88. uint32_t sc = 0;
  89. uint32_t u8Port;
  90. uint32_t u8PinPos;
  91. uint32_t u16PinMapping[KBI_MAX_NO][KBI_MAX_PINS_PER_PORT] =
  92. {
  93. {/* KBI0P0~KBI0P31 pins position in GPIOA register */
  94. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
  95. },
  96. {/* KBI1P0~KBI1P31 pins position in GPIOB register */
  97. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
  98. }
  99. };
  100. #endif
  101. if(KBI0 == pKBI)
  102. {
  103. SIM->SCGC |= SIM_SCGC_KBI0_MASK; /* enable clock to KBI0 */\
  104. u8Port = 0;
  105. }
  106. else if (KBI1 == pKBI)
  107. {
  108. SIM->SCGC |= SIM_SCGC_KBI1_MASK; /* enable clock to KBI1 */
  109. u8Port = 1;
  110. }
  111. /* mask keyboard interrupts first */
  112. sc = pConfig->sBits.bMode;
  113. pKBI->SC = sc;
  114. /* configure KBI pin polarity and others */
  115. for (i = 0; i < KBI_MAX_PINS_PER_PORT; i++)
  116. {
  117. if(pConfig->sPin[i].bEn)
  118. {
  119. pKBI->PE |= (1<<i); /* enable this KBI pin */
  120. pKBI->ES = (pKBI->ES & ~(1<<i)) | (pConfig->sPin[i].bEdge << i);
  121. u8PinPos = u16PinMapping[u8Port][i];
  122. ASSERT(!(u8PinPos & 0x80));
  123. #if defined(CPU_NV32)|| defined(CPU_NV32M3)
  124. FGPIOA->PIDR &= ~(1<<u8PinPos); /* enable GPIO input */
  125. FGPIOA->PDDR &= ~(1<<u8PinPos); /* configure pin as input */
  126. PORT->PUEL |= (1<<u8PinPos); /* enable pullup for the pin */
  127. #elif defined(CPU_NV32M4)
  128. if (u8Port == 0) /* KBI0 */
  129. {
  130. FGPIOA->PIDR &= ~(1<<u8PinPos); /* enable GPIO input */
  131. FGPIOA->PDDR &= ~(1<<u8PinPos); /* configure pin as input */
  132. PORT->PUE0 |= (1<<u8PinPos); /*enable pullup for the pin */
  133. }
  134. else if (u8Port == 1) /* KBI1 */
  135. {
  136. FGPIOB->PIDR &= ~(1<<u8PinPos); /* enable GPIO input */
  137. FGPIOB->PDDR &= ~(1<<u8PinPos); /* configure pin as input */
  138. PORT->PUE1 |= (1<<u8PinPos); /*enable pullup for the pin */
  139. }
  140. #endif
  141. }
  142. }
  143. #if defined(CPU_NV32M4)
  144. /*Reset KBI_SP register*/
  145. sc = pConfig->sBits.bRstKbsp<<KBI_SC_RSTKBSP_SHIFT;
  146. pKBI->SC |= sc;
  147. /*Real KBI_SP register enable*/
  148. sc = pConfig->sBits.bKbspEn<<KBI_SC_KBSPEN_SHIFT;
  149. pKBI->SC |= sc;
  150. #endif
  151. /* write to KBACK to clear any false interrupts */
  152. pKBI->SC = sc;
  153. /* enable interrupt if needed */
  154. if(pConfig->sBits.bIntEn)
  155. {
  156. pKBI->SC |= KBI_SC_KBIE_MASK;
  157. if(KBI0 == pKBI)
  158. {
  159. NVIC_EnableIRQ(KBI0_IRQn);
  160. }
  161. else
  162. {
  163. NVIC_EnableIRQ(KBI1_IRQn);
  164. }
  165. }
  166. }
  167. /*****************************************************************************//*!
  168. *
  169. * @brief set up KBI callback routine.
  170. *
  171. * @param[in] pKBI pointer to KBI module.
  172. * @param[in] pfnCallback pointer to callback routine.
  173. *
  174. * @return none.
  175. *
  176. * @ Pass/ Fail criteria: none.
  177. *
  178. *****************************************************************************/
  179. void KBI_SetCallback(KBI_Type *pKBI, KBI_CallbackType pfnCallback)
  180. {
  181. if(KBI0 == pKBI)
  182. {
  183. KBI_Callback[0] = pfnCallback;
  184. }
  185. else
  186. {
  187. KBI_Callback[1] = pfnCallback;
  188. }
  189. }
  190. /*****************************************************************************//*!
  191. *
  192. * @brief deinit the kbi module.
  193. *
  194. * @param[in] pKBI pointer to KBI module.
  195. *
  196. * @return none.
  197. *
  198. * @ Pass/ Fail criteria: none.
  199. *
  200. * @see KBI_Init.
  201. *
  202. *****************************************************************************/
  203. void KBI_DeInit(KBI_Type *pKBI)
  204. {
  205. if(KBI0 == pKBI)
  206. {
  207. NVIC_DisableIRQ(KBI0_IRQn);
  208. }
  209. else
  210. {
  211. NVIC_DisableIRQ(KBI1_IRQn);
  212. }
  213. pKBI->PE = 0;
  214. pKBI->SC = 0;
  215. pKBI->ES = 0;
  216. if(KBI0 == pKBI)
  217. {
  218. SIM->SCGC &= ~SIM_SCGC_KBI0_MASK; /* disable clock to KBI0 */
  219. }
  220. else
  221. {
  222. SIM->SCGC &= ~SIM_SCGC_KBI1_MASK; /* disable clock to KBI1 */
  223. }
  224. }
  225. /*! @} End of acmp_api_list */
  226. /*****************************************************************************//*!
  227. *
  228. * @brief button group 0 (KBI0) interrupt service routine.
  229. *
  230. * @param none.
  231. *
  232. * @return none.
  233. *
  234. * @ Pass/ Fail criteria: none.
  235. *
  236. *****************************************************************************/
  237. void KBI0_Isr(void)
  238. {
  239. KBI0->SC |= KBI_SC_KBACK_MASK; /* clear interrupt flag */
  240. if(KBI_Callback[0])
  241. {
  242. KBI_Callback[0]();
  243. }
  244. }
  245. /*****************************************************************************//*!
  246. *
  247. * @brief button group 0 (KBI0) interrupt service routine.
  248. *
  249. * @param none.
  250. *
  251. * @return none.
  252. *
  253. * @ Pass/ Fail criteria: none.
  254. *
  255. *****************************************************************************/
  256. void KBI1_Isr(void)
  257. {
  258. KBI1->SC |= KBI_SC_KBACK_MASK; /* clear interrupt flag */
  259. if(KBI_Callback[1])
  260. {
  261. KBI_Callback[1]();
  262. }
  263. }