hal_callback.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "stm32g4xx_hal.h"
  2. #include "hal_callback.h"
  3. #include "terminal_usartbridge.h"
  4. #include "uart_bridge.h"
  5. #include "menu.h"
  6. #include "config.h"
  7. #include <stdio.h>
  8. #define DBG if (1)
  9. static void encoder_handler(void);
  10. // -------------------------------------------------------------------------- //
  11. // EXTI //
  12. // -------------------------------------------------------------------------- //
  13. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  14. {
  15. static uint32_t delay_cnt = 0;
  16. static bool push_flag = false;
  17. //uint32_t tick = HAL_GetTick();
  18. if (GPIO_Pin == GPIO_PIN_4)
  19. {
  20. //DBG HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_6);
  21. //menu_send_event(CONTROL_PUSH);
  22. #if 1
  23. if (push_flag == false) {
  24. push_flag = true;
  25. delay_cnt = HAL_GetTick();
  26. }
  27. if (push_flag == true) {
  28. if ((HAL_GetTick() - delay_cnt) > BUTTON_GLICH_TIMEOUT) {
  29. push_flag = false;
  30. DBG HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_6);
  31. menu_send_event(CONTROL_PUSH);
  32. }
  33. }
  34. #endif
  35. }
  36. }
  37. // -------------------------------------------------------------------------- //
  38. // UART //
  39. // -------------------------------------------------------------------------- //
  40. void UART_RxCpltCallback(UART_HandleTypeDef *huart);
  41. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  42. {
  43. switch((uint32_t)huart->Instance)
  44. {
  45. case USART3_BASE : // HAL USART Terminal
  46. HAL_UART_RxCpltCallbackTerminal();
  47. break;
  48. case USART2_BASE :
  49. usart_bridge_rx_cb();
  50. break;
  51. default : break;
  52. }
  53. }
  54. void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
  55. {
  56. switch((uint32_t)huart->Instance)
  57. {
  58. case USART3_BASE : // HAL USART Terminal
  59. HAL_UART_RxCpltCallbackTerminal();
  60. break;
  61. case USART2_BASE :
  62. usart_bridge_rx_cb();
  63. break;
  64. default : break;
  65. }
  66. }
  67. // -------------------------------------------------------------------------- //
  68. // TIM //
  69. // -------------------------------------------------------------------------- //
  70. //
  71. void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
  72. {
  73. switch((uint32_t)htim->Instance)
  74. {
  75. case TIM2_BASE :
  76. encoder_handler();
  77. break;
  78. default : break;
  79. }
  80. }
  81. //
  82. static void encoder_handler(void)
  83. {
  84. //static uint8_t frw_cnt = 0;
  85. //static uint8_t back_cnt = 0;
  86. if (TIM2->CR1 & TIM_CR1_DIR)
  87. {
  88. menu_send_event(CONTROL_BACK);
  89. #if 0
  90. if (++back_cnt >= ENC_SOFT_FILTER_CNT)
  91. {
  92. menu_send_event(CONTROL_BACK);
  93. DBG printf("Encoder back\r\n");
  94. back_cnt = 0;
  95. return;
  96. }
  97. back_cnt++;
  98. #endif
  99. }
  100. else
  101. {
  102. menu_send_event(CONTROL_FWD);
  103. #if 0
  104. if (++frw_cnt >= ENC_SOFT_FILTER_CNT) {
  105. menu_send_event(CONTROL_FWD);
  106. DBG printf("Encoder forward\r\n");
  107. frw_cnt = 0;
  108. return;
  109. }
  110. frw_cnt++;
  111. #endif
  112. }
  113. }