|
@@ -1,21 +1,34 @@
|
|
|
#include "stm32g0xx_hal.h"
|
|
|
#include "encoder.h"
|
|
|
+#include "uart_bridge.h"
|
|
|
+#include <stdio.h>
|
|
|
+#include <string.h>
|
|
|
|
|
|
|
|
|
TIM_HandleTypeDef htim1;
|
|
|
|
|
|
-#define TEST_BUF_LEN 256
|
|
|
-uint32_t test_index;
|
|
|
-uint32_t test_buf[TEST_BUF_LEN];
|
|
|
+#define TX_BUF_SIZE 60
|
|
|
|
|
|
#define RESOLUTION 1024
|
|
|
|
|
|
-uint8_t direction;
|
|
|
-uint32_t forw_cnt;
|
|
|
-uint32_t back_cnt;
|
|
|
-uint32_t forw_turns;
|
|
|
-uint32_t back_turns;
|
|
|
+static uint8_t tx_buf[TX_BUF_SIZE];
|
|
|
|
|
|
+uint32_t forw_cnt_irq;
|
|
|
+uint32_t back_cnt_irq;
|
|
|
+
|
|
|
+uint8_t direction_irq;
|
|
|
+uint32_t forw_turns_irq;
|
|
|
+uint32_t back_turns_irq;
|
|
|
+
|
|
|
+uint8_t direction = 1;
|
|
|
+uint32_t forw_turns = 123456789;
|
|
|
+uint32_t back_turns = 987654321;
|
|
|
+
|
|
|
+bool access_turns = true;
|
|
|
+
|
|
|
+//
|
|
|
+static void create_data_pack_encoder(void);
|
|
|
+static void init_gpio_encoder(void);
|
|
|
|
|
|
//
|
|
|
void init_encoder(void)
|
|
@@ -55,19 +68,10 @@ void init_encoder(void)
|
|
|
|
|
|
HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);
|
|
|
|
|
|
- //HAL_NVIC_SetPriority(TIM1_BRK_UP_TRG_COM_IRQn, 6, 0);
|
|
|
- //HAL_NVIC_EnableIRQ(TIM1_BRK_UP_TRG_COM_IRQn);
|
|
|
-
|
|
|
- //__HAL_TIM_ENABLE_IT(&htim1, TIM_IT_UPDATE);
|
|
|
-
|
|
|
HAL_NVIC_SetPriority(TIM1_CC_IRQn, 6, 0);
|
|
|
HAL_NVIC_EnableIRQ(TIM1_CC_IRQn);
|
|
|
|
|
|
- //HAL_TIM_Encoder_Start(&htim1, TIM_CHANNEL_ALL);
|
|
|
- //HAL_TIM_Encoder_Start_IT(&htim1, TIM_CHANNEL_ALL);
|
|
|
HAL_TIM_Encoder_Start_IT(&htim1, TIM_CHANNEL_1);
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
|
|
|
//
|
|
@@ -91,81 +95,55 @@ void init_gpio_encoder(void)
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
|
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
|
|
|
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
|
-
|
|
|
-#if 0
|
|
|
- GPIO_InitStruct.Pin = GPIO_PIN_0;
|
|
|
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
|
|
- GPIO_InitStruct.Pull = GPIO_PULLUP;
|
|
|
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
|
|
- GPIO_InitStruct.Alternate = GPIO_AF1_TIM1; //GPIO_AF2_TIM1;
|
|
|
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
|
-
|
|
|
- GPIO_InitStruct.Pin = GPIO_PIN_3;
|
|
|
- GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
|
|
|
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
|
-#endif
|
|
|
-}
|
|
|
-
|
|
|
-void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
|
|
|
-{
|
|
|
- static uint32_t cnt = 0;
|
|
|
-
|
|
|
- cnt++;
|
|
|
}
|
|
|
|
|
|
+//
|
|
|
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
|
|
|
{
|
|
|
if (htim->Instance->CR1 & TIM_CR1_DIR) {
|
|
|
- direction = 1;
|
|
|
- back_cnt++;
|
|
|
- if (back_cnt >= RESOLUTION) {
|
|
|
- back_cnt = 0;
|
|
|
- back_turns++;
|
|
|
+ direction_irq = 1;
|
|
|
+ back_cnt_irq++;
|
|
|
+ if (back_cnt_irq >= RESOLUTION) {
|
|
|
+ back_cnt_irq = 0;
|
|
|
+ back_turns_irq++;
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
- direction = 0;
|
|
|
- forw_cnt++;
|
|
|
- if (forw_cnt >= RESOLUTION) {
|
|
|
- forw_cnt = 0;
|
|
|
- forw_turns++;
|
|
|
+ direction_irq = 0;
|
|
|
+ forw_cnt_irq++;
|
|
|
+ if (forw_cnt_irq >= RESOLUTION) {
|
|
|
+ forw_cnt_irq = 0;
|
|
|
+ forw_turns_irq++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ while (!access_turns) {}
|
|
|
+
|
|
|
#if 0
|
|
|
- //test_buf[test_index++] = (uint16_t)TIM1->CNT;
|
|
|
- test_buf[test_index++] = (uint16_t)TIM1->CCR1;
|
|
|
-
|
|
|
- if (test_index == TEST_BUF_LEN)
|
|
|
- test_index = 0;
|
|
|
-#endif
|
|
|
+ direction = direction_irq;
|
|
|
+ forw_turns = forw_turns_irq;
|
|
|
+ back_turns = forw_turns_irq;
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
+//
|
|
|
+static void create_data_pack_encoder(void)
|
|
|
+{
|
|
|
+ access_turns = false;
|
|
|
+ sprintf((char*)tx_buf, "{%u,%u,%u,}\n", forw_turns, back_turns, direction);
|
|
|
+ access_turns = true;
|
|
|
+}
|
|
|
|
|
|
-extern "C" {
|
|
|
-void TIM1_BRK_UP_TRG_COM_IRQHandler(void)
|
|
|
+//
|
|
|
+void send_data_pack_encoder(void)
|
|
|
{
|
|
|
- HAL_TIM_IRQHandler(&htim1);
|
|
|
-
|
|
|
-#if 0
|
|
|
- if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)
|
|
|
- {
|
|
|
- TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
|
|
|
-
|
|
|
-
|
|
|
- //encDir = (TIM1->CR1 & TIM_CR1_DIR ? true : false);
|
|
|
-
|
|
|
-#if 0
|
|
|
- if (!encInit)
|
|
|
- return;
|
|
|
-
|
|
|
- xSemaphoreGiveFromISR(semphEncoder, &xHigherPriorityTaskWoken);
|
|
|
-#endif
|
|
|
- }
|
|
|
-#endif
|
|
|
- //HAL_TIM_IRQHandler(&htim1);
|
|
|
+ create_data_pack_encoder();
|
|
|
+ ub_send_turns_pack(tx_buf, strlen((char*)tx_buf));
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+extern "C" {
|
|
|
+
|
|
|
void TIM1_CC_IRQHandler(void)
|
|
|
{
|
|
|
HAL_TIM_IRQHandler(&htim1);
|