Compare commits
5 Commits
409030ce8c
...
e41375a43d
Author | SHA1 | Date | |
---|---|---|---|
e41375a43d | |||
bed7092e0a | |||
aef6937a1c | |||
ed99ad9e63 | |||
7db7859e71 |
2
Makefile
2
Makefile
|
@ -8,7 +8,7 @@ COMFLAGS=-g -Os -pedantic -Wall
|
|||
CFLAGS=$(COMFLAGS) -std=c99 -D_POSIX_C_SOURCE=200809L
|
||||
CXXFLAGS=$(COMFLAGS) -std=c++17
|
||||
|
||||
OBJS = usb.o adc.o ringbuffer.o uart.o buttons.o printf.o
|
||||
OBJS = usb.o adc.o ringbuffer.o uart.o buttons.o printf.o encoder.o
|
||||
BINARY ?= main
|
||||
DEVICE=STM32F405RG
|
||||
|
||||
|
|
10
buttons.c
10
buttons.c
|
@ -19,13 +19,13 @@ static uint32_t open_ticks = 0;
|
|||
static uint32_t close_ticks = 0;
|
||||
|
||||
void buttons_open(uint32_t ticks) {
|
||||
gpio_set(BUTTON_PORT, OPEN_BUTTON);
|
||||
gpio_clear(BUTTON_PORT, OPEN_BUTTON);
|
||||
printf("Open %lu", ticks);
|
||||
open_ticks = ticks;
|
||||
}
|
||||
|
||||
void buttons_close(uint32_t ticks) {
|
||||
gpio_set(BUTTON_PORT, CLOSE_BUTTON);
|
||||
gpio_clear(BUTTON_PORT, CLOSE_BUTTON);
|
||||
printf("Close %lu", ticks);
|
||||
close_ticks = ticks;
|
||||
}
|
||||
|
@ -33,14 +33,14 @@ void buttons_close(uint32_t ticks) {
|
|||
void buttons_release(void) {
|
||||
open_ticks = close_ticks = 0;
|
||||
printf("Release");
|
||||
gpio_clear(BUTTON_PORT, OPEN_BUTTON | CLOSE_BUTTON);
|
||||
gpio_set(BUTTON_PORT, OPEN_BUTTON | CLOSE_BUTTON);
|
||||
}
|
||||
|
||||
void buttons_tick(void) {
|
||||
if (open_ticks) {
|
||||
open_ticks--;
|
||||
if (!open_ticks) {
|
||||
gpio_clear(BUTTON_PORT, OPEN_BUTTON);
|
||||
gpio_set(BUTTON_PORT, OPEN_BUTTON);
|
||||
printf("Release open");
|
||||
}
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ void buttons_tick(void) {
|
|||
close_ticks--;
|
||||
if (!close_ticks) {
|
||||
printf("Release close");
|
||||
gpio_clear(BUTTON_PORT, CLOSE_BUTTON);
|
||||
gpio_set(BUTTON_PORT, CLOSE_BUTTON);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
74
encoder.c
Normal file
74
encoder.c
Normal file
|
@ -0,0 +1,74 @@
|
|||
#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);
|
||||
|
||||
#define PIN1 GPIO4
|
||||
#define PIN1_IRQ NVIC_EXTI4_IRQ
|
||||
#define PIN2 GPIO5
|
||||
#define PIN2_IRQ NVIC_EXTI9_5_IRQ
|
||||
|
||||
void encoder_setup(void) {
|
||||
rcc_periph_clock_enable(RCC_GPIOA);
|
||||
|
||||
gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, PIN1 | PIN2);
|
||||
|
||||
/* Set up exti on A0 & A1 */
|
||||
exti_select_source(PIN1 | PIN2, GPIOA);
|
||||
exti_set_trigger(PIN1 | PIN2, EXTI_TRIGGER_BOTH);
|
||||
exti_enable_request(PIN1 | PIN2);
|
||||
|
||||
nvic_enable_irq(PIN1_IRQ);
|
||||
nvic_enable_irq(PIN2_IRQ);
|
||||
}
|
||||
|
||||
int encoder_get() {
|
||||
return pos;
|
||||
}
|
||||
|
||||
static int prev = 0;
|
||||
void check(void) {
|
||||
int pp = pos;
|
||||
int now = GPIO_IDR(GPIOA);
|
||||
now = (!!(now & PIN1))<<0 | (!!(now & PIN2)) << 1;
|
||||
|
||||
switch (prev << 2 | now) {
|
||||
case 0 * 4 + 2:
|
||||
case 1 * 4 + 0:
|
||||
case 2 * 4 + 3:
|
||||
case 3 * 4 + 1:
|
||||
pos++;
|
||||
break;
|
||||
case 0 * 4 + 1:
|
||||
case 1 * 4 + 3:
|
||||
case 2 * 4 + 0:
|
||||
case 3 * 4 + 2:
|
||||
pos--;
|
||||
break;
|
||||
default:
|
||||
prev = now;
|
||||
return;
|
||||
}
|
||||
|
||||
prev = now;
|
||||
if (pos < MIN_POS)
|
||||
pos = MIN_POS;
|
||||
if (pos > MAX_POS)
|
||||
pos = MAX_POS;
|
||||
}
|
||||
|
||||
void exti4_isr(void) {
|
||||
exti_reset_request(EXTI4);
|
||||
check();
|
||||
}
|
||||
void exti9_5_isr(void) {
|
||||
exti_reset_request(EXTI5);
|
||||
check();
|
||||
}
|
11
encoder.h
Normal file
11
encoder.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
#ifndef ENCODER_H
|
||||
#define ENCODER_H
|
||||
#define CLOSED_POS 220
|
||||
#define OPENED_POS 0
|
||||
|
||||
#define MAX_POS ((CLOSED_POS > OPENED_POS) ? CLOSED_POS : OPENED_POS)
|
||||
#define MIN_POS ((CLOSED_POS < OPENED_POS) ? CLOSED_POS : OPENED_POS)
|
||||
|
||||
void encoder_setup(void);
|
||||
int encoder_get(void);
|
||||
#endif
|
19
main.c
19
main.c
|
@ -9,6 +9,9 @@
|
|||
#include "usb.h"
|
||||
#include "ringbuffer.h"
|
||||
#include "uart.h"
|
||||
#include "encoder.h"
|
||||
|
||||
static const uint32_t button_time = 100;
|
||||
|
||||
volatile uint32_t tick = 0;
|
||||
RINGBUFFER_STORAGE(usb_to_uart_buf, 64)
|
||||
|
@ -46,10 +49,12 @@ int main(void) {
|
|||
uart_setup();
|
||||
sys_tick_setup();
|
||||
buttons_setup();
|
||||
encoder_setup();
|
||||
|
||||
nvic_enable_irq(NVIC_USART3_IRQ);
|
||||
|
||||
uint32_t last_tick = tick;
|
||||
int last_pos = 0;
|
||||
while (1) {
|
||||
/* Handle control messages through the comms CDC */
|
||||
char buf[64];
|
||||
|
@ -66,14 +71,24 @@ int main(void) {
|
|||
printf("%lu", (unsigned long)adc_bat_voltage());
|
||||
break;
|
||||
case 'O':
|
||||
buttons_open(15);
|
||||
buttons_open(button_time);
|
||||
break;
|
||||
case 'C':
|
||||
buttons_close(15);
|
||||
buttons_close(button_time);
|
||||
break;
|
||||
case 'R':
|
||||
printf("pos: %d", encoder_get());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int pos = encoder_get();
|
||||
if (pos != last_pos && ringbuffer_empty(comm_out_buf)) {
|
||||
printf("pos: %d", pos);
|
||||
last_pos = pos;
|
||||
}
|
||||
|
||||
|
||||
/* Send replies */
|
||||
if ((buf_len = ringbuffer_peek(comm_out_buf, &buf[0], sizeof buf))) {
|
||||
if (usb_write_cdcacm(ACM_COMM, (void *)buf, buf_len, 1)) {
|
||||
|
|
2
printf.c
2
printf.c
|
@ -196,6 +196,7 @@ static int getint(char **s) {
|
|||
static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, int *nl_type)
|
||||
{
|
||||
char *a, *z, *s=(char *)fmt;
|
||||
char buf[32];
|
||||
unsigned l10n=0, fl;
|
||||
int w, p, xp;
|
||||
union arg arg;
|
||||
|
@ -291,6 +292,7 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg,
|
|||
|
||||
if (!f) continue;
|
||||
|
||||
z = buf + sizeof(buf);
|
||||
prefix = "-+ 0X0x";
|
||||
pl = 0;
|
||||
t = s[-1];
|
||||
|
|
Loading…
Reference in New Issue
Block a user