encoder.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "stm32g0xx_hal.h"
  2. #include "encoder.h"
  3. #include "uart_bridge.h"
  4. #include <stdio.h>
  5. #include <string.h>
  6. TIM_HandleTypeDef htim1;
  7. #define TX_BUF_SIZE 60
  8. #define RESOLUTION 1024
  9. static uint8_t tx_buf[TX_BUF_SIZE];
  10. uint32_t forw_cnt_irq;
  11. uint32_t back_cnt_irq;
  12. uint8_t direction_irq;
  13. uint32_t forw_turns_irq;
  14. uint32_t back_turns_irq;
  15. uint8_t direction = 1;
  16. uint32_t forw_turns = 123456789;
  17. uint32_t back_turns = 987654321;
  18. bool access_turns = true;
  19. //
  20. static void create_data_pack_encoder(void);
  21. static void init_gpio_encoder(void);
  22. //
  23. void init_encoder(void)
  24. {
  25. TIM_Encoder_InitTypeDef sConfig = {0};
  26. TIM_MasterConfigTypeDef sMasterConfig = {0};
  27. __HAL_RCC_TIM1_CLK_ENABLE();
  28. init_gpio_encoder();
  29. htim1.Instance = TIM1;
  30. htim1.Init.Prescaler = 0;
  31. htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  32. htim1.Init.Period = 65535;
  33. htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  34. htim1.Init.RepetitionCounter = 0;
  35. htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; //TIM_AUTORELOAD_PRELOAD_ENABLE;
  36. sConfig.EncoderMode = TIM_ENCODERMODE_TI1; //TIM_ENCODERMODE_TI12;
  37. sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
  38. sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
  39. sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
  40. sConfig.IC1Filter = 0;
  41. sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
  42. sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
  43. sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
  44. sConfig.IC2Filter = 0;
  45. HAL_TIM_Encoder_Init(&htim1, &sConfig);
  46. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  47. sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
  48. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  49. HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);
  50. HAL_NVIC_SetPriority(TIM1_CC_IRQn, 6, 0);
  51. HAL_NVIC_EnableIRQ(TIM1_CC_IRQn);
  52. HAL_TIM_Encoder_Start_IT(&htim1, TIM_CHANNEL_1);
  53. }
  54. //
  55. void init_gpio_encoder(void)
  56. {
  57. GPIO_InitTypeDef GPIO_InitStruct = {0};
  58. __HAL_RCC_GPIOA_CLK_ENABLE();
  59. __HAL_RCC_GPIOB_CLK_ENABLE();
  60. GPIO_InitStruct.Pin = GPIO_PIN_8;
  61. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  62. GPIO_InitStruct.Pull = GPIO_NOPULL;
  63. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  64. GPIO_InitStruct.Alternate = GPIO_AF2_TIM1;
  65. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  66. GPIO_InitStruct.Pin = GPIO_PIN_3;
  67. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  68. GPIO_InitStruct.Pull = GPIO_NOPULL;
  69. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  70. GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
  71. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  72. }
  73. //
  74. void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
  75. {
  76. if (htim->Instance->CR1 & TIM_CR1_DIR) {
  77. direction_irq = 1;
  78. back_cnt_irq++;
  79. if (back_cnt_irq >= RESOLUTION) {
  80. back_cnt_irq = 0;
  81. back_turns_irq++;
  82. }
  83. }
  84. else {
  85. direction_irq = 0;
  86. forw_cnt_irq++;
  87. if (forw_cnt_irq >= RESOLUTION) {
  88. forw_cnt_irq = 0;
  89. forw_turns_irq++;
  90. }
  91. }
  92. while (!access_turns) {}
  93. #if 0
  94. direction = direction_irq;
  95. forw_turns = forw_turns_irq;
  96. back_turns = forw_turns_irq;
  97. #endif
  98. }
  99. //
  100. static void create_data_pack_encoder(void)
  101. {
  102. access_turns = false;
  103. sprintf((char*)tx_buf, "{%u,%u,%u,}\n", forw_turns, back_turns, direction);
  104. access_turns = true;
  105. }
  106. //
  107. void send_data_pack_encoder(void)
  108. {
  109. create_data_pack_encoder();
  110. ub_send_turns_pack(tx_buf, strlen((char*)tx_buf));
  111. }
  112. extern "C" {
  113. void TIM1_CC_IRQHandler(void)
  114. {
  115. HAL_TIM_IRQHandler(&htim1);
  116. }
  117. }