Skip to content

Commit 02be492

Browse files
committed
Pin conflict detection with SPI
1 parent b79a4b5 commit 02be492

File tree

1 file changed

+67
-54
lines changed

1 file changed

+67
-54
lines changed

src/DriverPins.h

Lines changed: 67 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ using GpioPin = int16_t;
1919
*/
2020

2121
enum class PinLogic {
22-
InputActiveHigh, InputActiveLow, InputActiveTouch, Input, Output,
22+
InputActiveHigh,
23+
InputActiveLow,
24+
InputActiveTouch,
25+
Input,
26+
Output,
2327
};
2428

2529
/**
@@ -41,16 +45,15 @@ enum class PinFunction {
4145
MCLK_SOURCE,
4246
};
4347

44-
4548
/**
4649
* @brief I2S pins
4750
* @author Phil Schatzmann
4851
* @copyright GPLv3
4952
*/
5053
struct PinsI2S {
5154
PinsI2S() = default;
52-
PinsI2S(PinFunction function, GpioPin mclk, GpioPin bck, GpioPin ws, GpioPin data_out,
53-
GpioPin data_in = -1, int port = 0) {
55+
PinsI2S(PinFunction function, GpioPin mclk, GpioPin bck, GpioPin ws,
56+
GpioPin data_out, GpioPin data_in = -1, int port = 0) {
5457
this->function = function;
5558
this->mclk = mclk;
5659
this->bck = bck;
@@ -75,8 +78,8 @@ struct PinsI2S {
7578
*/
7679
struct PinsSPI {
7780
PinsSPI() = default;
78-
PinsSPI(PinFunction function, GpioPin clk, GpioPin miso, GpioPin mosi, GpioPin cs,
79-
SPIClass &spi = SPI) {
81+
PinsSPI(PinFunction function, GpioPin clk, GpioPin miso, GpioPin mosi,
82+
GpioPin cs, SPIClass &spi = SPI) {
8083
this->function = function;
8184
this->clk = clk;
8285
this->miso = miso;
@@ -108,16 +111,16 @@ struct PinsSPI {
108111
} else {
109112
// begin spi and set up pins if supported
110113
#if defined(ARDUINO_ARCH_STM32)
111-
AD_LOGI("setting up SPI miso:%d,mosi:%d, clk:%d, cs:%d", miso,
112-
mosi, clk, cs);
114+
AD_LOGI("setting up SPI miso:%d,mosi:%d, clk:%d, cs:%d", miso, mosi,
115+
clk, cs);
113116
p_spi->setMISO(miso);
114117
p_spi->setMOSI(mosi);
115118
p_spi->setSCLK(clk);
116119
p_spi->setSSEL(cs);
117120
p_spi->begin();
118121
#elif defined(ESP32)
119-
AD_LOGI("setting up SPI miso:%d,mosi:%d, clk:%d, cs:%d", miso,
120-
mosi, clk, cs);
122+
AD_LOGI("setting up SPI miso:%d,mosi:%d, clk:%d, cs:%d", miso, mosi,
123+
clk, cs);
121124
p_spi->begin(clk, miso, mosi, cs);
122125
#elif defined(ARDUINO_ARCH_AVR)
123126
AD_LOGW("setting up SPI w/o pins");
@@ -220,14 +223,14 @@ struct PinsFunction {
220223
class DriverPins {
221224
public:
222225
void addI2S(PinsI2S pin) { i2s.push_back(pin); }
223-
void addI2S(PinFunction function, GpioPin mclk, GpioPin bck, GpioPin ws, GpioPin data_out,
224-
GpioPin data_in = -1, int port = 0) {
226+
void addI2S(PinFunction function, GpioPin mclk, GpioPin bck, GpioPin ws,
227+
GpioPin data_out, GpioPin data_in = -1, int port = 0) {
225228
PinsI2S pin{function, mclk, bck, ws, data_out, data_in, port};
226229
addI2S(pin);
227230
}
228231
void addSPI(PinsSPI pin) { spi.push_back(pin); }
229-
void addSPI(PinFunction function, GpioPin clk, GpioPin miso, GpioPin mosi, GpioPin cs,
230-
SPIClass &spi = SPI) {
232+
void addSPI(PinFunction function, GpioPin clk, GpioPin miso, GpioPin mosi,
233+
GpioPin cs, SPIClass &spi = SPI) {
231234
PinsSPI pin(function, clk, miso, mosi, cs, spi);
232235
addSPI(pin);
233236
}
@@ -309,7 +312,7 @@ class DriverPins {
309312
// setup spi
310313
bool result = true;
311314
for (auto &tmp : spi) {
312-
if (tmp.function==PinFunction::SD && sd_active)
315+
if (tmp.function == PinFunction::SD && sd_active)
313316
result &= tmp.begin();
314317
else
315318
result &= tmp.begin();
@@ -321,22 +324,26 @@ class DriverPins {
321324
// setup pins
322325
for (auto &tmp : pins) {
323326
if (tmp.pin != -1) {
324-
switch (tmp.pin_logic) {
325-
case PinLogic::InputActiveHigh:
326-
pinMode(tmp.pin, INPUT);
327-
break;
328-
case PinLogic::InputActiveLow:
329-
pinMode(tmp.pin, INPUT_PULLUP);
330-
break;
331-
case PinLogic::Input:
332-
pinMode(tmp.pin, INPUT);
333-
break;
334-
case PinLogic::Output:
335-
pinMode(tmp.pin, OUTPUT);
336-
break;
337-
default:
338-
// do nothing
339-
break;
327+
if (!hasConflict(tmp.pin)) {
328+
switch (tmp.pin_logic) {
329+
case PinLogic::InputActiveHigh:
330+
pinMode(tmp.pin, INPUT);
331+
break;
332+
case PinLogic::InputActiveLow:
333+
pinMode(tmp.pin, INPUT_PULLUP);
334+
break;
335+
case PinLogic::Input:
336+
pinMode(tmp.pin, INPUT);
337+
break;
338+
case PinLogic::Output:
339+
pinMode(tmp.pin, OUTPUT);
340+
break;
341+
default:
342+
// do nothing
343+
break;
344+
}
345+
} else {
346+
AD_LOGW("Pin '%d' not set up because of conflict", tmp.pin);
340347
}
341348
}
342349
}
@@ -355,26 +362,32 @@ class DriverPins {
355362
}
356363

357364
/// Defines if SPI for SD should be started (by default true)
358-
void setSPIActiveForSD(bool active){
359-
sd_active = active;
360-
}
365+
void setSPIActiveForSD(bool active) { sd_active = active; }
361366

362367
/// Check if SPI for SD should be started automatically
363-
bool isSPIActiveForSD(){
364-
return sd_active;
365-
}
368+
bool isSPIActiveForSD() { return sd_active; }
366369

367370
/// Returns true if some function pins have been defined
368-
bool hasPins(){
369-
return !pins.empty();
370-
}
371+
bool hasPins() { return !pins.empty(); }
371372

372373
protected:
373374
Vector<PinsI2S> i2s{0};
374375
Vector<PinsSPI> spi{0};
375376
Vector<PinsI2C> i2c{0};
376377
Vector<PinsFunction> pins{0};
377378
bool sd_active = true;
379+
380+
bool hasConflict(int pin) {
381+
if (sd_active) {
382+
auto sd = getSPIPins(PinFunction::SD);
383+
if (!sd)
384+
return false;
385+
PinsSPI spi = sd.value();
386+
return spi.cs == pin || spi.clk == pin || spi.miso == pin ||
387+
spi.mosi == pin;
388+
}
389+
return false;
390+
}
378391
};
379392

380393
/**
@@ -422,12 +435,12 @@ class PinsLyrat42Class : public DriverPins {
422435
addI2S(PinFunction::CODEC, 0, 5, 25, 26, 35);
423436

424437
// add other pins
425-
addPin(PinFunction::KEY, 36, PinLogic::InputActiveLow,1);
426-
addPin(PinFunction::KEY, 39, PinLogic::InputActiveLow,2);
427-
addPin(PinFunction::KEY, 33, PinLogic::InputActiveLow,3);
428-
addPin(PinFunction::KEY, 32, PinLogic::InputActiveLow,4);
429-
addPin(PinFunction::KEY, 13, PinLogic::InputActiveLow,5);
430-
addPin(PinFunction::KEY, 27, PinLogic::InputActiveLow,6);
438+
addPin(PinFunction::KEY, 36, PinLogic::InputActiveLow, 1);
439+
addPin(PinFunction::KEY, 39, PinLogic::InputActiveLow, 2);
440+
addPin(PinFunction::KEY, 33, PinLogic::InputActiveLow, 3);
441+
addPin(PinFunction::KEY, 32, PinLogic::InputActiveLow, 4);
442+
addPin(PinFunction::KEY, 13, PinLogic::InputActiveLow, 5);
443+
addPin(PinFunction::KEY, 27, PinLogic::InputActiveLow, 6);
431444
addPin(PinFunction::AUXIN_DETECT, 12, PinLogic::InputActiveLow);
432445
addPin(PinFunction::PA, 21, PinLogic::Output);
433446
addPin(PinFunction::LED, 22, PinLogic::Output, 1);
@@ -549,7 +562,7 @@ class PinsAudioKitAC101Class : public DriverPins {
549562
addPin(PinFunction::LED, 22, PinLogic::Output, 0);
550563
addPin(PinFunction::LED, 19, PinLogic::Output, 1);
551564
addPin(PinFunction::HEADPHONE_DETECT, 5, PinLogic::InputActiveLow);
552-
addPin(PinFunction::PA, 21, PinLogic::Output );
565+
addPin(PinFunction::PA, 21, PinLogic::Output);
553566
}
554567
};
555568

@@ -569,13 +582,13 @@ class PinsSTM32F411DiscoClass : public DriverPins {
569582
addI2S(PinFunction::CODEC, PC7, PC10, PA4, PC3, PC12);
570583

571584
// add other pins
572-
addPin(PinFunction::KEY, PA0, PinLogic::Output); // user button
573-
addPin(PinFunction::LED, PD12, PinLogic::Output, 0); // green
574-
addPin(PinFunction::LED, PD5, PinLogic::Output, 1); // red
575-
addPin(PinFunction::LED, PD13, PinLogic::Output, 2); // orange
576-
addPin(PinFunction::LED, PD14, PinLogic::Output, 3); // red
577-
addPin(PinFunction::LED, PD15, PinLogic::Output, 4); // blue
578-
addPin(PinFunction::PA, PD4, PinLogic::Output, ); // reset pin (active high)
585+
addPin(PinFunction::KEY, PA0, PinLogic::Output); // user button
586+
addPin(PinFunction::LED, PD12, PinLogic::Output, 0); // green
587+
addPin(PinFunction::LED, PD5, PinLogic::Output, 1); // red
588+
addPin(PinFunction::LED, PD13, PinLogic::Output, 2); // orange
589+
addPin(PinFunction::LED, PD14, PinLogic::Output, 3); // red
590+
addPin(PinFunction::LED, PD15, PinLogic::Output, 4); // blue
591+
addPin(PinFunction::PA, PD4, PinLogic::Output, ); // reset pin (active high)
579592
addPin(PinFunction::CODEC_ADC, PC3, PinLogic::Input); // Microphone
580593
}
581594
} PinsSTM32F411Disco;

0 commit comments

Comments
 (0)