made encoder work for zephyr
This commit is contained in:
@@ -1,22 +1,28 @@
|
||||
#include <encoder.h>
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
static Encoder* activeEncoder = nullptr;
|
||||
#define ENCODER_C1_NODE DT_ALIAS(encoder_c1)
|
||||
#define ENCODER_C2_NODE DT_ALIAS(encoder_c2)
|
||||
|
||||
ISR(PCINT0_vect)
|
||||
static const struct gpio_dt_spec encoderC1 =
|
||||
GPIO_DT_SPEC_GET(ENCODER_C1_NODE, gpios);
|
||||
|
||||
static const struct gpio_dt_spec encoderC2 =
|
||||
GPIO_DT_SPEC_GET(ENCODER_C2_NODE, gpios);
|
||||
|
||||
|
||||
|
||||
void Encoder::callback(const struct device *port,struct gpio_callback *cb,uint32_t pins)
|
||||
{
|
||||
if (activeEncoder != nullptr)
|
||||
{
|
||||
activeEncoder->update();
|
||||
}
|
||||
Encoder *encoder = CONTAINER_OF(cb, Encoder, gpioCallback);
|
||||
encoder->update();
|
||||
}
|
||||
|
||||
Encoder::Encoder(uint8_t c1Pin, uint8_t c2Pin)
|
||||
: c1(c1Pin),
|
||||
c2(c2Pin),
|
||||
c1Pin(c1Pin),
|
||||
Encoder::Encoder()
|
||||
: c1(&encoderC1),
|
||||
c2(&encoderC2),
|
||||
previousC1(false),
|
||||
count(0)
|
||||
{
|
||||
@@ -33,13 +39,13 @@ void Encoder::init()
|
||||
count = 0;
|
||||
|
||||
// Clear pending PORTB pin-change interrupt
|
||||
PCIFR |= (1 << PCIF0);
|
||||
gpio_init_callback(&gpioCallback,Encoder::callback,BIT(encoderC1.pin));
|
||||
|
||||
// Enable PORTB pin-change interrupts
|
||||
PCICR |= (1 << PCIE0);
|
||||
gpio_add_callback(encoderC1.port, &gpioCallback);
|
||||
|
||||
// Enable interrupt on C1
|
||||
PCMSK0 |= (1 << c1Pin);
|
||||
gpio_pin_interrupt_configure_dt(&encoderC1, GPIO_INT_EDGE_BOTH);
|
||||
}
|
||||
|
||||
void Encoder::update()
|
||||
@@ -65,11 +71,9 @@ void Encoder::update()
|
||||
|
||||
int32_t Encoder::position() const
|
||||
{
|
||||
uint8_t oldSREG = SREG;
|
||||
|
||||
cli();
|
||||
unsigned int key = irq_lock();
|
||||
int32_t currentCount = count;
|
||||
SREG = oldSREG;
|
||||
irq_unlock(key);
|
||||
|
||||
return currentCount;
|
||||
}
|
||||
Reference in New Issue
Block a user