Skip to content

Commit 53df708

Browse files
committed
Updated /lib
1 parent 5778c62 commit 53df708

11 files changed

Lines changed: 222 additions & 170 deletions

File tree

src/lib/axis/motor/servo/dcTmcSPI/DcTmcSPI.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,15 @@ bool ServoDcTmcSPI::init() {
8686
status.active = statusMode == ON;
8787

8888
// check to see if the driver is there and ok
89-
#ifndef DRIVER_TMC_STEPPER_HW_SPI
90-
if (Pins->miso != OFF)
89+
#ifdef MOTOR_DRIVER_DETECT
90+
#ifndef DRIVER_TMC_STEPPER_HW_SPI
91+
if (Pins->miso != OFF)
92+
#endif
93+
{
94+
readStatus();
95+
if (!status.standstill || status.overTemperature) { DF("ERR:"); D(axisPrefix); DLF("no driver detected!"); return false; }
96+
}
9197
#endif
92-
{
93-
readStatus();
94-
if (!status.standstill || status.overTemperature) { DF("ERR:"); D(axisPrefix); DLF("no driver detected!"); return false; }
95-
}
9698

9799
return true;
98100
}

src/lib/axis/motor/servo/tmc2209/Tmc2209.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,13 @@ bool ServoTmc2209::init() {
150150
status.active = statusMode == ON;
151151

152152
// check to see if the driver is there and ok
153-
readStatus();
154-
if (!status.standstill || status.overTemperature) {
155-
DF("ERR:"); D(axisPrefix); DLF("no driver detected!");
156-
return false;
157-
} else { VF("MSG:"); V(axisPrefix); VLF("motor driver device detected"); }
153+
#ifdef MOTOR_DRIVER_DETECT
154+
readStatus();
155+
if (!status.standstill || status.overTemperature) {
156+
DF("ERR:"); D(axisPrefix); DLF("no driver detected!");
157+
return false;
158+
} else { VF("MSG:"); V(axisPrefix); VLF("motor driver device detected"); }
159+
#endif
158160

159161
return true;
160162
}

src/lib/axis/motor/servo/tmc5160/Tmc5160.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,18 @@ bool ServoTmc5160::init() {
109109
status.active = statusMode == ON;
110110

111111
// check to see if the driver is there and ok
112-
#ifndef DRIVER_TMC_STEPPER_HW_SPI
113-
if (Pins->miso != OFF)
112+
#ifdef MOTOR_DRIVER_DETECT
113+
#ifndef DRIVER_TMC_STEPPER_HW_SPI
114+
if (Pins->miso != OFF)
115+
#endif
116+
{
117+
readStatus();
118+
if (!status.standstill || status.overTemperature) {
119+
DF("ERR:"); D(axisPrefix); DLF("no motor driver device detected!");
120+
return false;
121+
} else { VF("MSG:"); V(axisPrefix); VLF("motor driver device detected"); }
122+
}
114123
#endif
115-
{
116-
readStatus();
117-
if (!status.standstill || status.overTemperature) {
118-
DF("ERR:"); D(axisPrefix); DLF("no motor driver device detected!");
119-
return false;
120-
} else { VF("MSG:"); V(axisPrefix); VLF("motor driver device detected"); }
121-
}
122124

123125
return true;
124126
}

src/lib/axis/motor/stepDir/tmcLegacy/LegacySPI.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,16 @@ bool StepDirTmcSPI::init() {
6363

6464
// get driver ready and check to see if it's there (if possible)
6565
if (!driver.mode(settings.intpol, settings.decay, microstepCode, settings.currentRun, settings.currentHold, rSense)) {
66-
DF("ERR:"); D(axisPrefix); DLF("no motor driver device detected!");
67-
return false;
66+
#ifdef MOTOR_DRIVER_DETECT
67+
DF("ERR:"); D(axisPrefix); DLF("no motor driver device detected!");
68+
return false;
69+
#endif
6870
} else {
69-
if (Pins->m3 != OFF) {
70-
VF("MSG:"); V(axisPrefix); VLF("motor driver device detected");
71-
}
71+
#ifdef MOTOR_DRIVER_DETECT
72+
if (Pins->m3 != OFF) {
73+
VF("MSG:"); V(axisPrefix); VLF("motor driver device detected");
74+
}
75+
#endif
7276
}
7377

7478
// automatically set fault status for known drivers

src/lib/axis/motor/stepDir/tmcLegacy/LegacyUART.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,14 @@ bool StepDirTmcUART::init() {
9797

9898
// this driver automatically switches to one-way communications, even if a RX pin is set
9999
// so the following only returns false if communications are "half working"
100-
if (rxPin != OFF) {
101-
if (!driver->isSetupAndCommunicating()) {
102-
DF("ERR:"); D(axisPrefix); DLF("no motor driver device detected!");
103-
return false;
104-
} else { VF("MSG:"); V(axisPrefix); VLF("motor driver device detected"); }
105-
}
100+
#ifdef MOTOR_DRIVER_DETECT
101+
if (rxPin != OFF) {
102+
if (!driver->isSetupAndCommunicating()) {
103+
DF("ERR:"); D(axisPrefix); DLF("no motor driver device detected!");
104+
return false;
105+
} else { VF("MSG:"); V(axisPrefix); VLF("motor driver device detected"); }
106+
}
107+
#endif
106108

107109
driver->useExternalSenseResistors();
108110
driver->enableAnalogCurrentScaling();

src/lib/axis/motor/stepDir/tmcStepper/StepperSPI.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,18 @@ bool StepDirTmcSPI::init() {
127127

128128
// if we can, check to see if the driver is there
129129
// check to see if the driver is there and ok
130-
#ifndef DRIVER_TMC_STEPPER_HW_SPI
131-
if (Pins->miso != OFF)
130+
#ifdef MOTOR_DRIVER_DETECT
131+
#ifndef DRIVER_TMC_STEPPER_HW_SPI
132+
if (Pins->miso != OFF)
133+
#endif
134+
{
135+
readStatus();
136+
if (!status.standstill || status.overTemperature) {
137+
DF("ERR:"); D(axisPrefix); DLF("no motor driver device detected!");
138+
return false;
139+
} else { VF("MSG:"); V(axisPrefix); VLF("motor driver device detected"); }
140+
}
132141
#endif
133-
{
134-
readStatus();
135-
if (!status.standstill || status.overTemperature) {
136-
DF("ERR:"); D(axisPrefix); DLF("no motor driver device detected!");
137-
return false;
138-
} else { VF("MSG:"); V(axisPrefix); VLF("motor driver device detected"); }
139-
}
140-
141142

142143
// set fault pin mode
143144
if (settings.status == LOW) pinModeEx(Pins->fault, INPUT_PULLUP);

src/lib/axis/motor/stepDir/tmcStepper/StepperUART.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,13 @@ bool StepDirTmcUART::init() {
132132
status.active = settings.status != OFF;
133133

134134
// check to see if the driver is there and ok
135-
readStatus();
136-
if (!status.standstill || status.overTemperature) {
137-
DF("ERR:"); D(axisPrefix); DLF("no motor driver device detected!");
138-
return false;
139-
} else { VF("MSG:"); V(axisPrefix); VLF("motor driver device detected"); }
135+
#ifdef MOTOR_DRIVER_DETECT
136+
readStatus();
137+
if (!status.standstill || status.overTemperature) {
138+
DF("ERR:"); D(axisPrefix); DLF("no motor driver device detected!");
139+
return false;
140+
} else { VF("MSG:"); V(axisPrefix); VLF("motor driver device detected"); }
141+
#endif
140142

141143
// set fault pin mode
142144
if (settings.status == LOW) pinModeEx(Pins->fault, INPUT_PULLUP);

src/lib/gpioEx/pcf8574/Pcf8574.cpp

Lines changed: 57 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -5,124 +5,96 @@
55

66
#if defined(GPIO_DEVICE) && GPIO_DEVICE == X8574
77

8-
#ifndef GPIO_PCF8574_I2C_ADDRESS1
9-
#define GPIO_PCF8574_I2C_ADDRESS1 0x39
10-
#endif
11-
#ifndef GPIO_PCF8574_I2C_ADDRESS2
12-
#define GPIO_PCF8574_I2C_ADDRESS2 0x3A
13-
#endif
14-
#ifndef GPIO_PCF8574_I2C_ADDRESS3
15-
#define GPIO_PCF8574_I2C_ADDRESS3 0x3B
16-
#endif
17-
#ifndef GPIO_PCF8574_I2C_ADDRESS4
18-
#define GPIO_PCF8574_I2C_ADDRESS4 0x3C
19-
#endif
20-
#ifndef GPIO_PCF8574_I2C_NUM_DEVICES
21-
#define GPIO_PCF8574_I2C_NUM_DEVICES 1
22-
#endif
23-
24-
#if defined(ESP32) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_STM32) || defined(ARDUINO_ARCH_RENESAS)
25-
PCF8574 pcf0(&HAL_WIRE, GPIO_PCF8574_I2C_ADDRESS1);
26-
#if GPIO_PCF8574_I2C_NUM_DEVICES > 1
27-
PCF8574 pcf1(&HAL_WIRE, GPIO_PCF8574_I2C_ADDRESS2);
28-
#if GPIO_PCF8574_I2C_NUM_DEVICES > 2
29-
PCF8574 pcf2(&HAL_WIRE, GPIO_PCF8574_I2C_ADDRESS3);
30-
#if GPIO_PCF8574_I2C_NUM_DEVICES > 3
31-
PCF8574 pcf3(&HAL_WIRE, GPIO_PCF8574_I2C_ADDRESS4);
32-
#endif
33-
#endif
34-
#endif
35-
#else
36-
PCF8574 pcf0(GPIO_PCF8574_I2C_ADDRESS1);
37-
#if GPIO_PCF8574_I2C_NUM_DEVICES > 1
38-
PCF8574 pcf1(GPIO_PCF8574_I2C_ADDRESS2);
39-
#if GPIO_PCF8574_I2C_NUM_DEVICES > 2
40-
PCF8574 pcf2(GPIO_PCF8574_I2C_ADDRESS3);
41-
#if GPIO_PCF8574_I2C_NUM_DEVICES > 3
42-
PCF8574 pcf3(GPIO_PCF8574_I2C_ADDRESS4);
43-
#endif
44-
#endif
45-
#endif
46-
#endif
47-
488
// check for up to four PCF8574 devices on the I2C bus
499
bool GpioPcf8574::init() {
5010
static bool initialized = false;
5111
if (initialized) return found;
5212

53-
for (int i = 0; i < 4; i++) { pcf[i] = NULL; }
54-
pcf[0] = &pcf0;
55-
#if GPIO_PCF8574_I2C_NUM_DEVICES > 1
56-
pcf[1] = &pcf1;
57-
#if GPIO_PCF8574_I2C_NUM_DEVICES > 2
58-
pcf[2] = &pcf2;
59-
#if GPIO_PCF8574_I2C_NUM_DEVICES > 3
60-
pcf[3] = &pcf3;
61-
#endif
62-
#endif
63-
#endif
13+
for (int i = 0; i < 32; i++) mode[i] = -1;
14+
15+
HAL_WIRE.begin();
16+
HAL_WIRE_SET_CLOCK();
6417

65-
for (int i = 0; i < 4; i++) {
66-
if (pcf[i] != NULL) {
67-
if (pcf[i]->begin()) {
68-
found = true;
69-
} else {
18+
// check to be sure we find all devices
19+
for (int i = 0; i < GPIO_PCF8574_I2C_NUM_DEVICES; i++) {
20+
HAL_WIRE.beginTransmission(iicAddress[i]);
21+
uint8_t error = HAL_WIRE.endTransmission();
22+
if (error) {
23+
// try again
24+
HAL_WIRE.beginTransmission(iicAddress[i]);
25+
uint8_t error = HAL_WIRE.endTransmission();
26+
if (error) {
27+
if (DEBUG != OFF) { DF("WRN: Gpio.init(), PCF8574 (I2C 0x"); SERIAL_DEBUG.print(iicAddress[i], HEX); DLF(") not found"); }
7028
found = false;
71-
DF("WRN: Gpio.init(), PCF8574 (I2C 0x");
72-
if (DEBUG != OFF) {
73-
switch (i) {
74-
case 0: SERIAL_DEBUG.print(GPIO_PCF8574_I2C_ADDRESS1, HEX); break;
75-
#if GPIO_PCF8574_I2C_NUM_DEVICES > 1
76-
case 1: SERIAL_DEBUG.print(GPIO_PCF8574_I2C_ADDRESS2, HEX); break;
77-
#endif
78-
#if GPIO_PCF8574_I2C_NUM_DEVICES > 2
79-
case 2: SERIAL_DEBUG.print(GPIO_PCF8574_I2C_ADDRESS3, HEX); break;
80-
#endif
81-
#if GPIO_PCF8574_I2C_NUM_DEVICES > 3
82-
case 3: SERIAL_DEBUG.print(GPIO_PCF8574_I2C_ADDRESS4, HEX); break;
83-
#endif
84-
}
85-
}
86-
DLF(") not found");
29+
return false;
8730
}
8831
}
8932
}
90-
HAL_WIRE_SET_CLOCK();
91-
92-
for (int i = 0; i < 32; i++) { mode[i] = INPUT; state[i] = false; };
9333

34+
found = true;
9435
return found;
9536
}
9637

9738
// set GPIO pin (0 to 31) mode for INPUT or OUTPUT
9839
void GpioPcf8574::pinMode(int pin, int mode) {
9940
if (found && pin >= 0 && pin <= GPIO_PCF8574_I2C_NUM_DEVICES*8 - 1) {
41+
uint8_t device = pin >> 3;
42+
uint8_t devicePin = pin & 0b0111;
43+
10044
#ifdef INPUT_PULLDOWN
10145
if (mode == INPUT_PULLDOWN) mode = INPUT;
10246
#endif
10347
if (mode == INPUT_PULLUP) mode = INPUT;
104-
pcf[pin >> 3]->pinMode(pin & 0b111, mode);
105-
this->mode[pin] = mode;
48+
if (mode != INPUT && mode != OUTPUT) return;
49+
50+
if (this->mode[pin] != mode) {
51+
bitWrite(state[device], devicePin, (mode == INPUT) ? 1 : 0);
52+
53+
HAL_WIRE.beginTransmission(iicAddress[device]);
54+
HAL_WIRE.write(state[device]);
55+
HAL_WIRE.endTransmission();
56+
57+
this->mode[pin] = mode;
58+
}
10659
}
10760
}
10861

10962
// get GPIO pin (0 to 31) state
11063
int GpioPcf8574::digitalRead(int pin) {
111-
if (found && pin >= 0 && pin <= GPIO_PCF8574_I2C_NUM_DEVICES*8 - 1) {
64+
if (found && pin >= 0 && pin <= GPIO_PCF8574_I2C_NUM_DEVICES*8 - 1 && mode[pin] > 0) {
65+
uint8_t device = pin >> 3;
66+
uint8_t devicePin = pin & 0b0111;
67+
11268
if (mode[pin] == INPUT) {
113-
return pcf[pin >> 3]->digitalRead(pin & 0b111);
114-
} else return state[pin];
115-
} else return 0;
69+
70+
HAL_WIRE.requestFrom(iicAddress[device], (uint8_t)1);
71+
72+
unsigned long timeout = millis() + 1000UL;
73+
do { if ((long)(timeout - millis()) < 0) return 0; } while (Wire.available() < 1);
74+
75+
uint8_t data = HAL_WIRE.read();
76+
return bitRead(data, devicePin);
77+
} else
78+
return bitRead(state[device], devicePin);
79+
}
80+
81+
return 0;
11682
}
11783

11884
// set GPIO pin (0 to 31) state
11985
void GpioPcf8574::digitalWrite(int pin, int value) {
120-
if (found && pin >= 0 && pin <= GPIO_PCF8574_I2C_NUM_DEVICES*8 - 1) {
121-
state[pin] = value;
86+
if (found && pin >= 0 && pin <= GPIO_PCF8574_I2C_NUM_DEVICES*8 - 1 && mode[pin] > 0) {
12287
if (mode[pin] == OUTPUT) {
123-
pcf[pin >> 3]->digitalWrite(pin & 0b111, value);
88+
uint8_t device = pin >> 3;
89+
uint8_t devicePin = pin & 0b0111;
90+
91+
bitWrite(state[device], devicePin, (value == 0) ? 0 : 1);
92+
93+
HAL_WIRE.beginTransmission(iicAddress[device]);
94+
HAL_WIRE.write(state[device]);
95+
HAL_WIRE.endTransmission();
12496
}
125-
} else return;
97+
}
12698
}
12799

128100
GpioPcf8574 gpio;

src/lib/gpioEx/pcf8574/Pcf8574.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,41 @@
66

77
#if defined(GPIO_DEVICE) && GPIO_DEVICE == X8574
88

9-
#include <PCF8574.h> // https://github.com/xreef/PCF8574_library/tree/master
9+
#ifndef GPIO_PCF8574_I2C_NUM_DEVICES
10+
#define GPIO_PCF8574_I2C_NUM_DEVICES 1
11+
#endif
12+
13+
#ifndef GPIO_PCF8574_I2C_ADDRESS1
14+
#define GPIO_PCF8574_I2C_ADDRESS1 0x39
15+
#endif
16+
#ifndef GPIO_PCF8574_I2C_ADDRESS2
17+
#define GPIO_PCF8574_I2C_ADDRESS2 0x3A
18+
#endif
19+
#ifndef GPIO_PCF8574_I2C_ADDRESS3
20+
#define GPIO_PCF8574_I2C_ADDRESS3 0x3B
21+
#endif
22+
#ifndef GPIO_PCF8574_I2C_ADDRESS4
23+
#define GPIO_PCF8574_I2C_ADDRESS4 0x3C
24+
#endif
1025

1126
class GpioPcf8574 : public Gpio {
1227
public:
13-
// scan for PCF8575 device
28+
// scan for PCF8574 device
1429
bool init();
1530

1631
void pinMode(int pin, int mode);
1732

18-
// one sixteen channel PCF8575 GPIO is supported, this gets the last set value
1933
int digitalRead(int pin);
2034

21-
// one sixteen channel PCF8575 GPIO is supported, this sets each output on or off
2235
void digitalWrite(int pin, int value);
2336

2437
private:
2538
bool found = false;
2639

27-
PCF8574 *pcf[4];
40+
uint8_t iicAddress[4] = {GPIO_PCF8574_I2C_ADDRESS1, GPIO_PCF8574_I2C_ADDRESS2, GPIO_PCF8574_I2C_ADDRESS3, GPIO_PCF8574_I2C_ADDRESS4};
2841

2942
int mode[32];
30-
bool state[32];
43+
uint8_t state[4] = {0, 0, 0, 0};
3144
};
3245

3346
extern GpioPcf8574 gpio;

0 commit comments

Comments
 (0)