core_keyboard.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "user.h"
  22. typedef struct
  23. {
  24. const I2C_TypeDef *RegBase;
  25. const int IrqLine;
  26. CBFuncEx_t CB;
  27. void *pParam;
  28. }KB_CtrlStruct;
  29. static KB_CtrlStruct prvKB = {
  30. KCU,
  31. KBD_IRQn,
  32. };
  33. static int32_t prvKB_DummyCB(void *pData, void *pParam)
  34. {
  35. return 0;
  36. }
  37. static void KB_IrqHandle(int32_t IrqLine, void *pData)
  38. {
  39. uint32_t Status, Event, i, type, key;
  40. Status = KCU->KCU_STATUS;
  41. Event = KCU->KCU_EVENT;
  42. ISR_Clear(IrqLine);
  43. for(i = 0; i < 4; i++)
  44. {
  45. if (Status & (1 << (2 * i + 5)))
  46. {
  47. type = (Status & (1 << (2 * i + 4)))?1:0;
  48. key = (Event & (0x000000ff << (i * 8))) >> (i * 8);
  49. key |= (type << 16);
  50. prvKB.CB(key, prvKB.pParam);
  51. }
  52. }
  53. }
  54. void KB_Setup(uint16_t PinConfigMap, uint16_t Debounce, CBFuncEx_t CB, void *pParam)
  55. {
  56. uint32_t Dummy;
  57. ISR_OnOff(prvKB.IrqLine, 0);
  58. KCU->KCU_CTRL1 &= KCU_CTRL1_KCU_RUNING;
  59. while ((KCU->KCU_CTRL1 & KCU_CTRL1_KCU_RUNING));
  60. Dummy = KCU->KCU_EVENT;
  61. Debounce &= 0x7;
  62. KCU->KCU_CTRL0 = (PinConfigMap & 0x01ff) | (Debounce << 9);
  63. if (CB)
  64. {
  65. prvKB.CB = CB;
  66. }
  67. else
  68. {
  69. prvKB.CB = prvKB_DummyCB;
  70. }
  71. prvKB.pParam = pParam;
  72. KCU->KCU_CTRL1 |= (KCU_CTRL1_KBD_EN|KCU_CTRL1_PUSH_IT|KCU_CTRL1_RELEASE_IT|KCU_CTRL1_OVERRUN_IT);
  73. ISR_SetHandler(prvKB.IrqLine, KB_IrqHandle, NULL);
  74. #ifdef __BUILD_OS__
  75. ISR_SetPriority(prvKB.IrqLine, IRQ_LOWEST_PRIORITY - 1);
  76. #else
  77. ISR_SetPriority(prvKB.IrqLine, 7);
  78. #endif
  79. ISR_OnOff(prvKB.IrqLine, 1);
  80. }
  81. void KB_Stop(void)
  82. {
  83. ISR_OnOff(prvKB.IrqLine, 0);
  84. KCU->KCU_CTRL1 &= KCU_CTRL1_KCU_RUNING;
  85. }