20200717

Committer:
Gongorou
Date:
Fri Aug 30 00:50:34 2019 +0000
Revision:
11:2c48a1479026
Parent:
9:d1e6284a13ab
Encoder

Who changed what in which revision?

UserRevisionLine numberNew contents of line
inst 9:d1e6284a13ab 1 #include "rotary_encoder_base_impl.hpp"
inst 9:d1e6284a13ab 2 #include "rotary_encoder.hpp"
inst 9:d1e6284a13ab 3 #include "mbed.h"
inst 9:d1e6284a13ab 4
Gongorou 11:2c48a1479026 5
Gongorou 11:2c48a1479026 6
inst 9:d1e6284a13ab 7 rotary_encoder_base_impl::rotary_encoder_base_impl(TIM_TypeDef* timer_type,
inst 9:d1e6284a13ab 8 uint32_t encoder_mode,
inst 9:d1e6284a13ab 9 size_t resolution) : rotary_encoder(resolution) {
Gongorou 11:2c48a1479026 10 TIM_MasterConfigTypeDef sMasterConfig;
inst 9:d1e6284a13ab 11
Gongorou 11:2c48a1479026 12 timer_handler_.Instance = timer_type;
Gongorou 11:2c48a1479026 13 timer_handler_.Init.Period = max_counts_;
Gongorou 11:2c48a1479026 14 timer_handler_.Init.CounterMode = TIM_COUNTERMODE_UP;
Gongorou 11:2c48a1479026 15 timer_handler_.Init.Prescaler = 0;
Gongorou 11:2c48a1479026 16 timer_handler_.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
Gongorou 11:2c48a1479026 17 timer_handler_.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
inst 9:d1e6284a13ab 18 TIM_Encoder_InitTypeDef encoder;
inst 9:d1e6284a13ab 19 encoder.EncoderMode = encoder_mode;
inst 9:d1e6284a13ab 20
Gongorou 11:2c48a1479026 21 encoder.IC1Filter = 0;
Gongorou 11:2c48a1479026 22 encoder.IC1Polarity = TIM_ICPOLARITY_RISING;
inst 9:d1e6284a13ab 23 encoder.IC1Prescaler = TIM_ICPSC_DIV4;
inst 9:d1e6284a13ab 24 encoder.IC1Selection = TIM_ICSELECTION_DIRECTTI;
inst 9:d1e6284a13ab 25
Gongorou 11:2c48a1479026 26 encoder.IC2Filter = 0;
Gongorou 11:2c48a1479026 27 encoder.IC2Polarity = TIM_ICPOLARITY_RISING;
inst 9:d1e6284a13ab 28 encoder.IC2Prescaler = TIM_ICPSC_DIV4;
inst 9:d1e6284a13ab 29 encoder.IC2Selection = TIM_ICSELECTION_DIRECTTI;
inst 9:d1e6284a13ab 30
inst 9:d1e6284a13ab 31 if (HAL_TIM_Encoder_Init(&timer_handler_, &encoder) != HAL_OK) {
inst 9:d1e6284a13ab 32 error("couldn't init encoder\n");
inst 9:d1e6284a13ab 33 }
Gongorou 11:2c48a1479026 34
Gongorou 11:2c48a1479026 35 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
Gongorou 11:2c48a1479026 36 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
Gongorou 11:2c48a1479026 37 if (HAL_TIMEx_MasterConfigSynchronization(&timer_handler_, &sMasterConfig) != HAL_OK)
Gongorou 11:2c48a1479026 38 {
Gongorou 11:2c48a1479026 39 error("couldn't init encoder\n");
Gongorou 11:2c48a1479026 40 }
Gongorou 11:2c48a1479026 41
inst 9:d1e6284a13ab 42 }
inst 9:d1e6284a13ab 43
inst 9:d1e6284a13ab 44 rotary_encoder_base_impl::~rotary_encoder_base_impl() {}
inst 9:d1e6284a13ab 45
inst 9:d1e6284a13ab 46 int32_t rotary_encoder_base_impl::get_counts() const {
inst 9:d1e6284a13ab 47 int32_t counts = timer_handler_.Instance->CNT;
inst 9:d1e6284a13ab 48
inst 9:d1e6284a13ab 49 if (counts > (max_counts_ >> 1)) {
inst 9:d1e6284a13ab 50 return counts - max_counts_;
inst 9:d1e6284a13ab 51 }
inst 9:d1e6284a13ab 52 return counts;
inst 9:d1e6284a13ab 53 }
inst 9:d1e6284a13ab 54
inst 9:d1e6284a13ab 55 void rotary_encoder_base_impl::reset() {
inst 9:d1e6284a13ab 56 timer_handler_.Instance->CNT = 0;
inst 9:d1e6284a13ab 57 }
inst 9:d1e6284a13ab 58
inst 9:d1e6284a13ab 59 void rotary_encoder_base_impl::start() {
inst 9:d1e6284a13ab 60 if(HAL_TIM_Encoder_Start(&timer_handler_, TIM_CHANNEL_1) != HAL_OK) {
inst 9:d1e6284a13ab 61 error("couldn't start encoder\r\n");
inst 9:d1e6284a13ab 62 }
inst 9:d1e6284a13ab 63 }
inst 9:d1e6284a13ab 64
inst 9:d1e6284a13ab 65 void rotary_encoder_base_impl::stop() {
inst 9:d1e6284a13ab 66 if(HAL_TIM_Encoder_Stop(&timer_handler_, TIM_CHANNEL_1) != HAL_OK) {
inst 9:d1e6284a13ab 67 error("couldn't start encoder\r\n");
inst 9:d1e6284a13ab 68 }
inst 9:d1e6284a13ab 69 }