2021-02-19 20:30:23 +00:00
|
|
|
#include <libopencm3/cm3/nvic.h>
|
|
|
|
#include <libopencm3/stm32/exti.h>
|
|
|
|
#include <libopencm3/stm32/gpio.h>
|
|
|
|
#include <libopencm3/stm32/rcc.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include "encoder.h"
|
|
|
|
|
|
|
|
static volatile int pos;
|
|
|
|
|
|
|
|
static void check(void);
|
|
|
|
|
2021-07-16 19:21:54 +00:00
|
|
|
#define PIN_OPEN GPIO1
|
|
|
|
#define PIN_OPEN_IRQ NVIC_EXTI1_IRQ
|
|
|
|
#define PIN_CLOSE GPIO2
|
|
|
|
#define PIN_CLOSE_IRQ NVIC_EXTI2_IRQ
|
|
|
|
|
|
|
|
#define PIN_PORT GPIOC
|
|
|
|
#define PIN_RCC RCC_GPIOC
|
2021-02-19 20:30:23 +00:00
|
|
|
|
|
|
|
void encoder_setup(void) {
|
2021-07-16 19:21:54 +00:00
|
|
|
rcc_periph_clock_enable(PIN_RCC);
|
2021-02-19 20:30:23 +00:00
|
|
|
|
2021-07-16 19:21:54 +00:00
|
|
|
gpio_mode_setup(PIN_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, PIN_OPEN | PIN_CLOSE);
|
2021-02-19 20:30:23 +00:00
|
|
|
|
2021-07-16 19:21:54 +00:00
|
|
|
exti_select_source(PIN_OPEN | PIN_CLOSE, PIN_PORT);
|
|
|
|
exti_set_trigger(PIN_OPEN | PIN_CLOSE, EXTI_TRIGGER_BOTH);
|
|
|
|
exti_enable_request(PIN_OPEN | PIN_CLOSE);
|
2021-02-19 20:30:23 +00:00
|
|
|
|
2021-07-16 19:21:54 +00:00
|
|
|
nvic_enable_irq(PIN_OPEN_IRQ);
|
|
|
|
nvic_enable_irq(PIN_CLOSE_IRQ);
|
2021-02-19 20:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int encoder_get() {
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int prev = 0;
|
|
|
|
void check(void) {
|
|
|
|
int pp = pos;
|
2021-07-16 19:21:54 +00:00
|
|
|
int now = GPIO_IDR(PIN_PORT);
|
|
|
|
now = (!!(now & PIN_OPEN))<<0 | (!!(now & PIN_CLOSE)) << 1;
|
2021-02-19 20:30:23 +00:00
|
|
|
|
2021-07-16 19:21:54 +00:00
|
|
|
#define NEITHER 0
|
|
|
|
#define JUST_OPEN 1
|
|
|
|
#define JUST_CLOSE 2
|
|
|
|
#define BOTH 3
|
|
|
|
#define WAS(x) ((x)<<2)
|
|
|
|
#define IS(x) (x)
|
|
|
|
|
|
|
|
switch (WAS(prev) + IS(now)) {
|
|
|
|
case WAS(NEITHER) + IS(JUST_CLOSE):
|
|
|
|
case WAS(JUST_OPEN) + IS(NEITHER):
|
|
|
|
case WAS(JUST_CLOSE) + IS(BOTH):
|
|
|
|
case WAS(BOTH) + IS(JUST_OPEN):
|
2021-02-19 20:30:23 +00:00
|
|
|
pos--;
|
|
|
|
break;
|
2021-07-16 19:21:54 +00:00
|
|
|
case WAS(NEITHER) + IS(JUST_OPEN):
|
|
|
|
case WAS(JUST_OPEN) + IS(BOTH):
|
|
|
|
case WAS(BOTH) + IS(JUST_CLOSE):
|
|
|
|
case WAS(JUST_CLOSE) + IS(NEITHER):
|
|
|
|
pos++;
|
|
|
|
break;
|
2021-02-19 20:30:23 +00:00
|
|
|
default:
|
|
|
|
prev = now;
|
|
|
|
return;
|
|
|
|
}
|
2021-07-16 19:21:54 +00:00
|
|
|
#undef NEITHER
|
|
|
|
#undef JUST_OPEN
|
|
|
|
#undef JUST_CLOSE
|
|
|
|
#undef BOTH
|
|
|
|
#undef WAS
|
|
|
|
#undef IS
|
2021-02-19 20:30:23 +00:00
|
|
|
prev = now;
|
|
|
|
if (pos < MIN_POS)
|
|
|
|
pos = MIN_POS;
|
|
|
|
if (pos > MAX_POS)
|
|
|
|
pos = MAX_POS;
|
|
|
|
}
|
|
|
|
|
2021-07-16 19:21:54 +00:00
|
|
|
void exti1_isr(void) {
|
|
|
|
exti_reset_request(EXTI1);
|
2021-02-19 20:30:23 +00:00
|
|
|
check();
|
|
|
|
}
|
2021-07-16 19:21:54 +00:00
|
|
|
void exti2_isr(void) {
|
|
|
|
exti_reset_request(EXTI2);
|
2021-02-19 20:30:23 +00:00
|
|
|
check();
|
|
|
|
}
|