Library for Trinamic TMC2209 stepper modules to drive bipolar stepper motors. Ported and adapted from https://github.com/teemuatlut/TMCStepper
Revision 1:5ba0c258c4ed, committed 2021-03-22
- Comitter:
- charly
- Date:
- Mon Mar 22 21:35:09 2021 +0000
- Parent:
- 0:f4343071c8b1
- Child:
- 2:b34e91b54373
- Commit message:
- Timeout in Serial read-operations to prevent blocking reads
Changed in this revision
| TMC2208Stepper.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/TMC2208Stepper.cpp Thu Mar 18 20:50:27 2021 +0000
+++ b/TMC2208Stepper.cpp Mon Mar 22 21:35:09 2021 +0000
@@ -43,7 +43,7 @@
/* parity */ BufferedSerial::None,
/* stop bit */ 1
);
- SWSerial->set_blocking(true);
+ SWSerial->set_blocking(false); //set to non-blocking read
}
// #if defined(ARDUINO_ARCH_AVR)
// if (RXTX_pin > 0) {
@@ -265,6 +265,10 @@
// uint32_t ms = millis();
uint32_t sync_target = (static_cast<uint32_t>(datagram[0])<<16) | 0xFF00 | datagram[2];
uint32_t sync = 0;
+
+ // 64-bit time doesn't wrap for half a billion years, at least
+ uint64_t start_ms, now;
+ start_ms = Kernel::get_ms_count();
do {
@@ -276,6 +280,10 @@
}
if (!timeout) return 0;
*/
+ // 64-bit time doesn't wrap for half a billion years, at least
+ now = Kernel::get_ms_count();
+ if (now - start_ms > timeout) return 0;
+
int16_t res = serial_read();
if (res < 0) continue;
@@ -288,7 +296,9 @@
uint64_t out = sync;
// ms = millis();
// timeout = this->abort_window;
-
+
+ start_ms = Kernel::get_ms_count();
+
for(uint8_t i=0; i<5;) {
/* uint32_t ms2 = millis();
if (ms2 != ms) {
@@ -298,6 +308,9 @@
}
if (!timeout) return 0;
*/
+ now = Kernel::get_ms_count();
+ if (now - start_ms > timeout) return 0;
+
int16_t res = serial_read();
if (res < 0) continue;