2019-08-30 15:38:05 +08:00
|
|
|
/*
|
2023-01-09 10:20:16 +08:00
|
|
|
* Copyright (c) 2006-2023, RT-Thread Development Team
|
2019-08-30 15:38:05 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2019-08-23 balanceTWK first version
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "board.h"
|
|
|
|
#include "drv_config.h"
|
|
|
|
#ifdef RT_USING_PULSE_ENCODER
|
|
|
|
|
|
|
|
//#define DRV_DEBUG
|
|
|
|
#define LOG_TAG "drv.pulse_encoder"
|
|
|
|
#include <drv_log.h>
|
|
|
|
|
|
|
|
#if !defined(BSP_USING_PULSE_ENCODER1) && !defined(BSP_USING_PULSE_ENCODER2) && !defined(BSP_USING_PULSE_ENCODER3) \
|
|
|
|
&& !defined(BSP_USING_PULSE_ENCODER4) && !defined(BSP_USING_PULSE_ENCODER5) && !defined(BSP_USING_PULSE_ENCODER6)
|
|
|
|
#error "Please define at least one BSP_USING_PULSE_ENCODERx"
|
2022-03-24 08:30:41 +08:00
|
|
|
/* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
|
2019-08-30 15:38:05 +08:00
|
|
|
#endif
|
|
|
|
|
2019-09-24 11:54:05 +08:00
|
|
|
#define AUTO_RELOAD_VALUE 0x7FFF
|
|
|
|
|
2019-08-30 15:38:05 +08:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
#ifdef BSP_USING_PULSE_ENCODER1
|
|
|
|
PULSE_ENCODER1_INDEX,
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_PULSE_ENCODER2
|
|
|
|
PULSE_ENCODER2_INDEX,
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_PULSE_ENCODER3
|
|
|
|
PULSE_ENCODER3_INDEX,
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_PULSE_ENCODER4
|
|
|
|
PULSE_ENCODER4_INDEX,
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_PULSE_ENCODER5
|
|
|
|
PULSE_ENCODER5_INDEX,
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_PULSE_ENCODER6
|
|
|
|
PULSE_ENCODER6_INDEX,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
struct stm32_pulse_encoder_device
|
|
|
|
{
|
|
|
|
struct rt_pulse_encoder_device pulse_encoder;
|
|
|
|
TIM_HandleTypeDef tim_handler;
|
2019-09-24 11:54:05 +08:00
|
|
|
IRQn_Type encoder_irqn;
|
2019-09-24 13:36:09 +08:00
|
|
|
rt_int32_t over_under_flowcount;
|
2019-08-30 15:38:05 +08:00
|
|
|
char *name;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct stm32_pulse_encoder_device stm32_pulse_encoder_obj[] =
|
|
|
|
{
|
|
|
|
#ifdef BSP_USING_PULSE_ENCODER1
|
|
|
|
PULSE_ENCODER1_CONFIG,
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_PULSE_ENCODER2
|
|
|
|
PULSE_ENCODER2_CONFIG,
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_PULSE_ENCODER3
|
|
|
|
PULSE_ENCODER3_CONFIG,
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_PULSE_ENCODER4
|
|
|
|
PULSE_ENCODER4_CONFIG,
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_PULSE_ENCODER5
|
|
|
|
PULSE_ENCODER5_CONFIG,
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_PULSE_ENCODER6
|
|
|
|
PULSE_ENCODER6_CONFIG,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
rt_err_t pulse_encoder_init(struct rt_pulse_encoder_device *pulse_encoder)
|
|
|
|
{
|
|
|
|
TIM_Encoder_InitTypeDef sConfig;
|
|
|
|
TIM_MasterConfigTypeDef sMasterConfig;
|
2019-09-24 11:54:05 +08:00
|
|
|
struct stm32_pulse_encoder_device *stm32_device;
|
2019-09-24 13:36:09 +08:00
|
|
|
stm32_device = (struct stm32_pulse_encoder_device*)pulse_encoder;
|
2019-08-30 15:38:05 +08:00
|
|
|
|
2019-09-24 11:54:05 +08:00
|
|
|
stm32_device->tim_handler.Init.Prescaler = 0;
|
|
|
|
stm32_device->tim_handler.Init.CounterMode = TIM_COUNTERMODE_UP;
|
|
|
|
stm32_device->tim_handler.Init.Period = AUTO_RELOAD_VALUE;
|
|
|
|
stm32_device->tim_handler.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
|
|
|
stm32_device->tim_handler.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
|
2019-08-30 15:38:05 +08:00
|
|
|
|
|
|
|
sConfig.EncoderMode = TIM_ENCODERMODE_TI12;
|
|
|
|
sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
|
|
|
|
sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
|
2019-09-24 11:54:05 +08:00
|
|
|
sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
|
2019-08-30 15:38:05 +08:00
|
|
|
sConfig.IC1Filter = 3;
|
|
|
|
sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
|
|
|
|
sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
|
2019-09-24 11:54:05 +08:00
|
|
|
sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
|
2019-08-30 15:38:05 +08:00
|
|
|
sConfig.IC2Filter = 3;
|
|
|
|
|
2019-09-24 11:54:05 +08:00
|
|
|
if (HAL_TIM_Encoder_Init(&stm32_device->tim_handler, &sConfig) != HAL_OK)
|
2019-08-30 15:38:05 +08:00
|
|
|
{
|
|
|
|
LOG_E("pulse_encoder init failed");
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
|
|
|
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
|
|
|
|
2019-09-24 11:54:05 +08:00
|
|
|
if (HAL_TIMEx_MasterConfigSynchronization(&stm32_device->tim_handler, &sMasterConfig))
|
2019-08-30 15:38:05 +08:00
|
|
|
{
|
|
|
|
LOG_E("TIMx master config failed");
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
2019-09-24 11:54:05 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
HAL_NVIC_SetPriority(stm32_device->encoder_irqn, 3, 0);
|
|
|
|
|
|
|
|
/* enable the TIMx global Interrupt */
|
|
|
|
HAL_NVIC_EnableIRQ(stm32_device->encoder_irqn);
|
|
|
|
|
|
|
|
/* clear update flag */
|
|
|
|
__HAL_TIM_CLEAR_FLAG(&stm32_device->tim_handler, TIM_FLAG_UPDATE);
|
|
|
|
/* enable update request source */
|
|
|
|
__HAL_TIM_URS_ENABLE(&stm32_device->tim_handler);
|
|
|
|
}
|
2019-08-30 15:38:05 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2019-09-20 15:57:26 +08:00
|
|
|
rt_err_t pulse_encoder_clear_count(struct rt_pulse_encoder_device *pulse_encoder)
|
|
|
|
{
|
2019-09-24 11:54:05 +08:00
|
|
|
struct stm32_pulse_encoder_device *stm32_device;
|
2019-09-24 13:36:09 +08:00
|
|
|
stm32_device = (struct stm32_pulse_encoder_device*)pulse_encoder;
|
2019-09-24 11:54:05 +08:00
|
|
|
stm32_device->over_under_flowcount = 0;
|
|
|
|
__HAL_TIM_SET_COUNTER(&stm32_device->tim_handler, 0);
|
2019-09-20 15:57:26 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2019-08-30 15:38:05 +08:00
|
|
|
rt_int32_t pulse_encoder_get_count(struct rt_pulse_encoder_device *pulse_encoder)
|
|
|
|
{
|
2019-09-24 11:54:05 +08:00
|
|
|
struct stm32_pulse_encoder_device *stm32_device;
|
2019-09-24 13:36:09 +08:00
|
|
|
stm32_device = (struct stm32_pulse_encoder_device*)pulse_encoder;
|
|
|
|
return (rt_int32_t)((rt_int16_t)__HAL_TIM_GET_COUNTER(&stm32_device->tim_handler) + stm32_device->over_under_flowcount * AUTO_RELOAD_VALUE);
|
2019-08-30 15:38:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rt_err_t pulse_encoder_control(struct rt_pulse_encoder_device *pulse_encoder, rt_uint32_t cmd, void *args)
|
|
|
|
{
|
|
|
|
rt_err_t result;
|
2019-09-24 11:54:05 +08:00
|
|
|
struct stm32_pulse_encoder_device *stm32_device;
|
2019-09-24 13:36:09 +08:00
|
|
|
stm32_device = (struct stm32_pulse_encoder_device*)pulse_encoder;
|
2019-08-30 15:38:05 +08:00
|
|
|
|
|
|
|
result = RT_EOK;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case PULSE_ENCODER_CMD_ENABLE:
|
2022-10-05 13:07:26 +08:00
|
|
|
__HAL_TIM_ENABLE_IT(&stm32_device->tim_handler, TIM_IT_UPDATE);
|
2019-09-24 11:54:05 +08:00
|
|
|
HAL_TIM_Encoder_Start(&stm32_device->tim_handler, TIM_CHANNEL_ALL);
|
|
|
|
HAL_TIM_Encoder_Start_IT(&stm32_device->tim_handler, TIM_CHANNEL_ALL);
|
2019-08-30 15:38:05 +08:00
|
|
|
break;
|
|
|
|
case PULSE_ENCODER_CMD_DISABLE:
|
2022-10-05 13:07:26 +08:00
|
|
|
__HAL_TIM_DISABLE_IT(&stm32_device->tim_handler, TIM_IT_UPDATE);
|
2019-09-24 11:54:05 +08:00
|
|
|
HAL_TIM_Encoder_Stop(&stm32_device->tim_handler, TIM_CHANNEL_ALL);
|
|
|
|
HAL_TIM_Encoder_Stop_IT(&stm32_device->tim_handler, TIM_CHANNEL_ALL);
|
2019-08-30 15:38:05 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result = -RT_ENOSYS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-09-24 11:54:05 +08:00
|
|
|
void pulse_encoder_update_isr(struct stm32_pulse_encoder_device *device)
|
|
|
|
{
|
2019-09-24 13:36:09 +08:00
|
|
|
/* TIM Update event */
|
|
|
|
if (__HAL_TIM_GET_FLAG(&device->tim_handler, TIM_FLAG_UPDATE) != RESET)
|
|
|
|
{
|
2019-09-24 11:54:05 +08:00
|
|
|
__HAL_TIM_CLEAR_IT(&device->tim_handler, TIM_IT_UPDATE);
|
2019-09-24 13:36:09 +08:00
|
|
|
if (__HAL_TIM_IS_TIM_COUNTING_DOWN(&device->tim_handler))
|
2019-09-24 11:54:05 +08:00
|
|
|
{
|
|
|
|
device->over_under_flowcount--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
device->over_under_flowcount++;
|
|
|
|
}
|
2019-09-24 13:36:09 +08:00
|
|
|
}
|
|
|
|
/* Capture compare 1 event */
|
|
|
|
if (__HAL_TIM_GET_FLAG(&device->tim_handler, TIM_FLAG_CC1) != RESET)
|
|
|
|
{
|
2019-09-24 11:54:05 +08:00
|
|
|
__HAL_TIM_CLEAR_IT(&device->tim_handler, TIM_IT_CC1);
|
2019-09-24 13:36:09 +08:00
|
|
|
}
|
|
|
|
/* Capture compare 2 event */
|
|
|
|
if (__HAL_TIM_GET_FLAG(&device->tim_handler, TIM_FLAG_CC2) != RESET)
|
|
|
|
{
|
|
|
|
__HAL_TIM_CLEAR_IT(&device->tim_handler, TIM_IT_CC2);
|
|
|
|
}
|
|
|
|
/* Capture compare 3 event */
|
|
|
|
if (__HAL_TIM_GET_FLAG(&device->tim_handler, TIM_FLAG_CC3) != RESET)
|
|
|
|
{
|
|
|
|
__HAL_TIM_CLEAR_IT(&device->tim_handler, TIM_IT_CC3);
|
|
|
|
}
|
|
|
|
/* Capture compare 4 event */
|
|
|
|
if (__HAL_TIM_GET_FLAG(&device->tim_handler, TIM_FLAG_CC4) != RESET)
|
|
|
|
{
|
|
|
|
__HAL_TIM_CLEAR_IT(&device->tim_handler, TIM_IT_CC4);
|
|
|
|
}
|
|
|
|
/* TIM Break input event */
|
|
|
|
if (__HAL_TIM_GET_FLAG(&device->tim_handler, TIM_FLAG_BREAK) != RESET)
|
|
|
|
{
|
|
|
|
__HAL_TIM_CLEAR_IT(&device->tim_handler, TIM_IT_BREAK);
|
|
|
|
}
|
|
|
|
/* TIM Trigger detection event */
|
|
|
|
if (__HAL_TIM_GET_FLAG(&device->tim_handler, TIM_FLAG_TRIGGER) != RESET)
|
|
|
|
{
|
|
|
|
__HAL_TIM_CLEAR_IT(&device->tim_handler, TIM_IT_TRIGGER);
|
|
|
|
}
|
|
|
|
/* TIM commutation event */
|
|
|
|
if (__HAL_TIM_GET_FLAG(&device->tim_handler, TIM_FLAG_COM) != RESET)
|
|
|
|
{
|
|
|
|
__HAL_TIM_CLEAR_IT(&device->tim_handler, TIM_FLAG_COM);
|
|
|
|
}
|
2019-09-24 11:54:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef BSP_USING_PULSE_ENCODER1
|
2019-09-24 13:36:09 +08:00
|
|
|
#if defined(SOC_SERIES_STM32F4)
|
2019-09-29 18:05:39 +08:00
|
|
|
void TIM1_UP_TIM10_IRQHandler(void)
|
2019-09-24 13:36:09 +08:00
|
|
|
#elif defined(SOC_SERIES_STM32F1)
|
2019-09-29 18:05:39 +08:00
|
|
|
void TIM1_UP_IRQHandler(void)
|
2019-09-24 13:36:09 +08:00
|
|
|
#else
|
|
|
|
#error "Please check TIM1's IRQHandler"
|
|
|
|
#endif
|
2019-09-24 11:54:05 +08:00
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
pulse_encoder_update_isr(&stm32_pulse_encoder_obj[PULSE_ENCODER1_INDEX]);
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_PULSE_ENCODER2
|
|
|
|
void TIM2_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
pulse_encoder_update_isr(&stm32_pulse_encoder_obj[PULSE_ENCODER2_INDEX]);
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_PULSE_ENCODER3
|
|
|
|
void TIM3_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
pulse_encoder_update_isr(&stm32_pulse_encoder_obj[PULSE_ENCODER3_INDEX]);
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef BSP_USING_PULSE_ENCODER4
|
|
|
|
void TIM4_IRQHandler(void)
|
|
|
|
{
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
|
|
|
pulse_encoder_update_isr(&stm32_pulse_encoder_obj[PULSE_ENCODER4_INDEX]);
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-08-30 15:38:05 +08:00
|
|
|
static const struct rt_pulse_encoder_ops _ops =
|
|
|
|
{
|
|
|
|
.init = pulse_encoder_init,
|
|
|
|
.get_count = pulse_encoder_get_count,
|
2019-09-20 15:57:26 +08:00
|
|
|
.clear_count = pulse_encoder_clear_count,
|
2019-08-30 15:38:05 +08:00
|
|
|
.control = pulse_encoder_control,
|
|
|
|
};
|
|
|
|
|
|
|
|
int hw_pulse_encoder_init(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
result = RT_EOK;
|
|
|
|
for (i = 0; i < sizeof(stm32_pulse_encoder_obj) / sizeof(stm32_pulse_encoder_obj[0]); i++)
|
|
|
|
{
|
|
|
|
stm32_pulse_encoder_obj[i].pulse_encoder.type = AB_PHASE_PULSE_ENCODER;
|
|
|
|
stm32_pulse_encoder_obj[i].pulse_encoder.ops = &_ops;
|
|
|
|
|
2019-09-24 11:54:05 +08:00
|
|
|
if (rt_device_pulse_encoder_register(&stm32_pulse_encoder_obj[i].pulse_encoder, stm32_pulse_encoder_obj[i].name, RT_NULL) != RT_EOK)
|
2019-08-30 15:38:05 +08:00
|
|
|
{
|
|
|
|
LOG_E("%s register failed", stm32_pulse_encoder_obj[i].name);
|
|
|
|
result = -RT_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
INIT_BOARD_EXPORT(hw_pulse_encoder_init);
|
|
|
|
|
|
|
|
#endif
|