Added Wait for compleation funcionality

This commit is contained in:
AlexanderHD27
2024-09-25 19:23:24 +02:00
parent bba35c9622
commit e97472c2a3
3 changed files with 74 additions and 8 deletions

View File

@@ -1,8 +1,19 @@
#include "step.hpp"
#include "pico/stdio.h"
#include "hardware/pio.h"
#include "hardware/irq.h"
#include <pico/time.h>
TMC2209_step_dual * g_step_driver_instance;
void step_irq0_handler() {
g_step_driver_instance->irq_handler0();
};
void step_irq1_handler() {
g_step_driver_instance->irq_handler1();
};
TMC2209_step_dual::TMC2209_step_dual(
uint step0_pin,
uint step1_pin,
@@ -20,7 +31,14 @@ TMC2209_step_dual::TMC2209_step_dual(
this->enable1_pin = enable1_pin;
this->pio_core = pio_core;
this->init_gpio();
g_step_driver_instance = this;
this->done_flag0 = false;
this->done_flag1 = false;
this->done_flag0_arm = false;
this->done_flag1_arm = false;
init_gpio();
init_pio();
};
@@ -102,6 +120,23 @@ void TMC2209_step_dual::init_pio() {
pio_sm_set_enabled(this->pio_core, this->sm_num_counter0, true);
pio_sm_set_enabled(this->pio_core, this->sm_num_counter1, true);
pio_set_irq0_source_enabled(this->pio_core, pis_interrupt2, true);
pio_set_irq1_source_enabled(this->pio_core, pis_interrupt3, true);
if(this->pio_core == pio0) {
irq_set_exclusive_handler(PIO0_IRQ_0, step_irq0_handler);
irq_set_exclusive_handler(PIO0_IRQ_1, step_irq1_handler);
irq_set_enabled(PIO0_IRQ_0, true);
irq_set_enabled(PIO0_IRQ_1, true);
} else if(this->pio_core == pio1) {
irq_set_exclusive_handler(PIO1_IRQ_0, step_irq0_handler);
irq_set_exclusive_handler(PIO1_IRQ_1, step_irq1_handler);
irq_set_enabled(PIO1_IRQ_0, true);
irq_set_enabled(PIO1_IRQ_1, true);
}
set_conf0(1000, 0);
set_conf1(1000, 0);
};
@@ -140,13 +175,33 @@ void TMC2209_step_dual::pulse1(uint n) {
);
};
void TMC2209_step_dual::irq_handler0() {
if(this->done_flag0_arm) {
done_flag0 = true;
} else {
done_flag0 = false;
}
pio_interrupt_clear(this->pio_core, 2);
}
void TMC2209_step_dual::irq_handler1() {
if(this->done_flag1_arm) {
done_flag1 = true;
} else {
done_flag1 = false;
}
pio_interrupt_clear(this->pio_core, 3);
}
bool TMC2209_step_dual::wait0(int timeout_ms) {
int t = timeout_ms*1000;
bool continues = timeout_ms <= 0;
this->done_flag0_arm = true;
while(t > 0 || continues) {
if(done_flag0) {
this->done_flag0_arm = false;
done_flag0 = false;
return true;
}
@@ -163,10 +218,12 @@ bool TMC2209_step_dual::wait0(int timeout_ms) {
bool TMC2209_step_dual::wait1(int timeout_ms) {
int t = timeout_ms*1000;
bool continues = timeout_ms <= 0;
this->done_flag1_arm = true;
while(t > 0 || continues) {
if(done_flag0) {
this->done_flag1_arm = false;
done_flag0 = false;
return true;
}