Bætti við interrupt (pin D2) til að skoða encoder lestur, umb fyrir Part 1 í verkefni
This commit is contained in:
@@ -3,6 +3,13 @@
|
||||
#include <errno.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
#include <zephyr/devicetree.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
|
||||
//#define ISR_PROBE_NODE DT_PATH(zephyr_user)
|
||||
|
||||
static const struct gpio_dt_spec isr_probe =
|
||||
GPIO_DT_SPEC_GET(DT_ALIAS(encoder_isr_probe), gpios);
|
||||
|
||||
static const struct gpio_dt_spec encoder_c1 =
|
||||
GPIO_DT_SPEC_GET(DT_ALIAS(encoder_c1), gpios);
|
||||
@@ -24,11 +31,17 @@ RotaryEncoderDriver::RotaryEncoderDriver()
|
||||
int RotaryEncoderDriver::begin()
|
||||
{
|
||||
if (!gpio_is_ready_dt(&encoder_c1) ||
|
||||
!gpio_is_ready_dt(&encoder_c2)) {
|
||||
!gpio_is_ready_dt(&encoder_c2) ||
|
||||
!gpio_is_ready_dt(&isr_probe)) {
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
int ret = gpio_pin_configure_dt(&encoder_c1, GPIO_INPUT);
|
||||
int ret = gpio_pin_configure_dt(&isr_probe, GPIO_OUTPUT_INACTIVE);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = gpio_pin_configure_dt(&encoder_c1, GPIO_INPUT);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
@@ -48,7 +61,6 @@ int RotaryEncoderDriver::begin()
|
||||
return ret;
|
||||
}
|
||||
|
||||
// C1 is active high, so “to active” means a rising edge.
|
||||
ret = gpio_pin_interrupt_configure_dt(
|
||||
&encoder_c1,
|
||||
GPIO_INT_EDGE_TO_ACTIVE);
|
||||
@@ -58,8 +70,8 @@ int RotaryEncoderDriver::begin()
|
||||
|
||||
timer.set_callback(timer_expired, this);
|
||||
timer.init(SAMPLE_PERIOD_US);
|
||||
|
||||
reset_speed_measurement();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -76,7 +88,9 @@ void RotaryEncoderDriver::gpio_edge_handler(
|
||||
encoder_c1_callback);
|
||||
|
||||
if ((pins & BIT(encoder_c1.pin)) != 0U) {
|
||||
encoder->update();
|
||||
encoder->update(); // Reads C2 and updates the count
|
||||
gpio_pin_set_dt(&isr_probe, 1); // Marker rising edge
|
||||
gpio_pin_set_dt(&isr_probe, 0); // Reset for the next interrupt
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user