Add code for rotary encoder

This commit is contained in:
Mario Hüttel 2020-02-16 16:38:31 +01:00
parent 6fa071e1d1
commit a35f66f2cd
3 changed files with 80 additions and 3 deletions

View File

@ -18,8 +18,25 @@
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __ROTARY_ENCODER_H__
#define __ROTARY_ENCODER_H__
#include <stm32/stm32f4xx.h>
#define ROTARY_ENCODER_TIMER TIM5
#define ROTARY_ENCODER_TIMER_RCC_MASK RCC_APB1ENR_TIM5EN
#define ROTARY_ENCODER_PIN1 0
#define ROTARY_ENCODER_PIN2 1
#define ROTARY_ENCODER_PORT GPIOA
#define ROTARY_ENCODER_PORT_ALTFUNC 2
#define ROTARY_ENCODER_RCC_MASK RCC_AHB1ENR_GPIOAEN
void rotary_encoder_setup(void);
uint32_t rotary_encoder_get_abs_val(void);
int32_t rotary_encoder_get_chage_val(void);
void rotary_encoder_stop(void);
#endif /* __ROTARY_ENCODER_H__ */

View File

@ -28,6 +28,7 @@
#include <reflow-controller/adc-meas.h>
#include <reflow-controller/shell.h>
#include <reflow-controller/digio.h>
#include <reflow-controller/rotary-encoder.h>
#include <stm-periph/stm32-gpio-macros.h>
#include <stm-periph/clock-enable-manager.h>
#include <stm-periph/uart.h>
@ -45,6 +46,8 @@ static void setup_nvic_priorities()
static float pt1000_value;
static volatile int pt1000_value_status;
static uint32_t rot;
int main()
{
@ -61,6 +64,7 @@ int main()
digio_setup_default_all();
led_setup();
loudspeaker_setup();
rotary_encoder_setup();
uart_init_with_dma();
@ -68,7 +72,7 @@ int main()
while(1) {
pt1000_value_status = adc_pt1000_get_current_resistance(&pt1000_value);
rot = rotary_encoder_get_abs_val();
uart_receive_status = uart_receive_data_with_dma(&uart_input, &uart_input_len);
if (uart_receive_status >= 1)
shell_handle_input(shell_handle, uart_input, uart_input_len);

View File

@ -18,5 +18,61 @@
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <reflow-controller/rotary-encoder.h>
#include <stm-periph/clock-enable-manager.h>
#include <stm-periph/stm32-gpio-macros.h>
#include <reflow-controller/rotary-encoder.h>
static inline void rotary_encoder_setup_pins(void)
{
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(ROTARY_ENCODER_RCC_MASK));
ROTARY_ENCODER_PORT->MODER &= MODER_DELETE(ROTARY_ENCODER_PIN1) & MODER_DELETE(ROTARY_ENCODER_PIN2);
ROTARY_ENCODER_PORT->MODER |= ALTFUNC(ROTARY_ENCODER_PIN1) | ALTFUNC(ROTARY_ENCODER_PIN2);
SETAF(ROTARY_ENCODER_PORT, ROTARY_ENCODER_PIN1, ROTARY_ENCODER_PORT_ALTFUNC);
SETAF(ROTARY_ENCODER_PORT, ROTARY_ENCODER_PIN2, ROTARY_ENCODER_PORT_ALTFUNC);
}
void rotary_encoder_setup(void)
{
rcc_manager_enable_clock(&RCC->APB1ENR, BITMASK_TO_BITNO(ROTARY_ENCODER_TIMER_RCC_MASK));
rotary_encoder_setup_pins();
ROTARY_ENCODER_TIMER->ARR = 0xFFFF;
ROTARY_ENCODER_TIMER->CNT = 0;
ROTARY_ENCODER_TIMER->CR2 = 0;
ROTARY_ENCODER_TIMER->SMCR = TIM_SMCR_SMS_0 | TIM_SMCR_SMS_1;
ROTARY_ENCODER_TIMER->CCMR1 = TIM_CCMR1_CC1S_0 | TIM_CCMR1_CC2S_0;
ROTARY_ENCODER_TIMER->CCER = TIM_CCER_CC1P | TIM_CCER_CC2P;
ROTARY_ENCODER_TIMER->PSC = 0;
ROTARY_ENCODER_TIMER->CR1 = TIM_CR1_CEN;
}
uint32_t rotary_encoder_get_abs_val(void)
{
return (uint32_t)ROTARY_ENCODER_TIMER->CNT;
}
int32_t rotary_encoder_get_chage_val(void)
{
static uint32_t last_val = 0;
uint32_t val;
int32_t diff;
val = rotary_encoder_get_abs_val();
diff = val - last_val;
if (diff > 0xEFFF) {
diff = 0xFFFF - diff;
}
return diff;
}
void rotary_encoder_stop(void)
{
ROTARY_ENCODER_TIMER->CR1 = 0;
ROTARY_ENCODER_TIMER->CR2 = 0;
ROTARY_ENCODER_TIMER->ARR = 0;
rcc_manager_disable_clock(&RCC->APB1ENR, BITMASK_TO_BITNO(ROTARY_ENCODER_TIMER_RCC_MASK));
rcc_manager_disable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(ROTARY_ENCODER_RCC_MASK));
}