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