From 84e351d15450ca85165076fae3e9989c29494c6a Mon Sep 17 00:00:00 2001 From: AlexanderHD27 Date: Fri, 27 Sep 2024 23:21:04 +0200 Subject: [PATCH] Fixed Race-Condtion problem with arming of done_flag --- motor-control/firmware/src/main.cpp | 55 ++++++++++++------- motor-control/firmware/src/tmc2209/pulser.pio | 6 +- motor-control/firmware/src/tmc2209/step.cpp | 7 +++ 3 files changed, 44 insertions(+), 24 deletions(-) diff --git a/motor-control/firmware/src/main.cpp b/motor-control/firmware/src/main.cpp index 7c715e7..78b8675 100755 --- a/motor-control/firmware/src/main.cpp +++ b/motor-control/firmware/src/main.cpp @@ -21,6 +21,9 @@ #define ENABLE0_PIN 2 #define ENABLE1_PIN 3 +#define LIMIT0_PIN 10 +#define LIMIT1_PIN 11 + #define STEP0_PIN 6 #define STEP1_PIN 7 #define DIR0_PIN 8 @@ -58,40 +61,50 @@ int main() { sleep_ms(5); - step_driver.set_conf0(10000, 0); + step_driver.set_conf1(10000, 0); - step_driver.pulse0(1000); - step_driver.pulse1(1000); - step_driver.wait0(0); - step_driver.wait1(0); - sleep_ms(200); + + gpio_init(LIMIT0_PIN); + gpio_set_dir(LIMIT0_PIN, GPIO_IN); + gpio_pull_up(LIMIT0_PIN); + + gpio_init(LIMIT1_PIN); + gpio_set_dir(LIMIT1_PIN, GPIO_IN); + gpio_pull_up(LIMIT1_PIN); + + step_driver.set_conf0(10000, 1); + step_driver.pulse0(10000); + step_driver.wait0(0); + + step_driver.set_conf0(10000, 0); + step_driver.pulse0(100000); + while(!gpio_get(LIMIT1_PIN)) { + sleep_ms(1); + } + step_driver.pulse0(0); + sleep_ms(500); step_driver.set_conf0(10000, 1); - step_driver.set_conf1(10000, 1); - step_driver.pulse0(1000); - step_driver.pulse1(1000); + step_driver.pulse0(200); step_driver.wait0(0); - step_driver.wait1(0); - - volatile uint t = 500 - step_driver.get_remaining_steps0(); - volatile uint s = step_driver.get_remaining_steps1(); - step_driver.set_conf0(1000, 1); - step_driver.set_conf1(1000, 1); - - - sleep_ms(100); + step_driver.set_conf0(500, 0); + step_driver.pulse0(5000); + while(!gpio_get(LIMIT1_PIN)) { + sleep_ms(1); + } + step_driver.pulse0(0); + sleep_ms(1); gpio_init(LED_PIN); gpio_set_dir(LED_PIN, GPIO_OUT); while (true) { - sleep_ms(50); - gpio_put(LED_PIN, 1); - sleep_ms(50); gpio_put(LED_PIN, 0); + sleep_ms(50); + gpio_put(LED_PIN, 1); } diff --git a/motor-control/firmware/src/tmc2209/pulser.pio b/motor-control/firmware/src/tmc2209/pulser.pio index 747ecab..5368f1f 100644 --- a/motor-control/firmware/src/tmc2209/pulser.pio +++ b/motor-control/firmware/src/tmc2209/pulser.pio @@ -23,14 +23,14 @@ counter: l1: PULL NOBLOCK MOV Y, OSR + MOV X, OSR JMP !Y l1_end - IRQ WAIT 2 REL + IRQ WAIT 2 REL SET PINS, 1 IRQ WAIT 2 REL - SET PINS, 0 - MOV X, OSR + JMP X-- l1 l1_end: diff --git a/motor-control/firmware/src/tmc2209/step.cpp b/motor-control/firmware/src/tmc2209/step.cpp index 82bd785..10bde8e 100644 --- a/motor-control/firmware/src/tmc2209/step.cpp +++ b/motor-control/firmware/src/tmc2209/step.cpp @@ -167,6 +167,8 @@ void TMC2209_step_dual::set_conf1(uint frequency, bool dir) { void TMC2209_step_dual::pulse0(uint n) { this->remaining_step_count0 = n; this->done_flag0_arm = true; + this->done_flag0 = false; + pio_sm_put_blocking( this->pio_core, this->sm_num_counter0, n ); @@ -175,12 +177,15 @@ void TMC2209_step_dual::pulse0(uint n) { void TMC2209_step_dual::pulse1(uint n) { this->remaining_step_count1 = n; this->done_flag1_arm = true; + this->done_flag1 = false; + pio_sm_put_blocking( this->pio_core, this->sm_num_counter1, n ); }; void TMC2209_step_dual::pulse0_int(int n) { + this->remaining_step_count0 = n; this->done_flag0_arm = true; if(n < 0) { n = abs(n); @@ -193,6 +198,7 @@ void TMC2209_step_dual::pulse0_int(int n) { }; void TMC2209_step_dual::pulse1_int(int n) { + this->remaining_step_count1 = n; this->done_flag1_arm = true; if(n < 0) { n = abs(n); @@ -212,6 +218,7 @@ void TMC2209_step_dual::irq_handler0() { } else { done_flag0 = false; } + pio_interrupt_clear(this->pio_core, 2); }