Skip to content

Commit

Permalink
Split pre and post communication methods for reads and writes
Browse files Browse the repository at this point in the history
  • Loading branch information
teemuatlut committed Jun 27, 2020
1 parent 64a1195 commit c714851
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/TMCStepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -999,10 +999,12 @@ class TMC2208Stepper : public TMCStepper {
SSwitch *sswitch = nullptr;

int available();
void preCommunication();
void preWriteCommunication();
void preReadCommunication();
int16_t serial_read();
uint8_t serial_write(const uint8_t data);
void postCommunication();
void postWriteCommunication();
void postReadCommunication();
void write(uint8_t, uint32_t);
uint32_t read(uint8_t);
const uint8_t slave_address;
Expand Down
23 changes: 17 additions & 6 deletions src/source/TMC2208Stepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,15 @@ int TMC2208Stepper::available() {
}

__attribute__((weak))
void TMC2208Stepper::preCommunication() {
void TMC2208Stepper::preWriteCommunication() {
if (HWSerial != nullptr) {
if (sswitch != nullptr)
sswitch->active();
}
}

__attribute__((weak))
void TMC2208Stepper::preReadCommunication() {
#if SW_CAPABLE_PLATFORM
if (SWSerial != nullptr) {
SWSerial->listen();
Expand Down Expand Up @@ -165,7 +173,10 @@ uint8_t TMC2208Stepper::serial_write(const uint8_t data) {
}

__attribute__((weak))
void TMC2208Stepper::postCommunication() {
void TMC2208Stepper::postWriteCommunication() {}

__attribute__((weak))
void TMC2208Stepper::postReadCommunication() {
#if SW_CAPABLE_PLATFORM
if (SWSerial != nullptr) {
SWSerial->stopListening();
Expand All @@ -180,12 +191,12 @@ void TMC2208Stepper::write(uint8_t addr, uint32_t regVal) {

datagram[len] = calcCRC(datagram, len);

preCommunication();
preWriteCommunication();

for(uint8_t i=0; i<=len; i++) {
bytesWritten += serial_write(datagram[i]);
}
postCommunication();
postWriteCommunication();

delay(replyDelay);
}
Expand Down Expand Up @@ -275,9 +286,9 @@ uint32_t TMC2208Stepper::read(uint8_t addr) {
uint64_t out = 0x00000000UL;

for (uint8_t i = 0; i < max_retries; i++) {
preCommunication();
preReadCommunication();
out = _sendDatagram(datagram, len, abort_window);
postCommunication();
postReadCommunication();

delay(replyDelay);

Expand Down

0 comments on commit c714851

Please sign in to comment.