Skip to content

Commit b689ae6

Browse files
authored
Merge pull request #80 from caternuson/no_cs
Refinements for no CS configs
2 parents 7a05fcc + 7961058 commit b689ae6

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

Adafruit_SPIDevice.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ Adafruit_SPIDevice::~Adafruit_SPIDevice() {
8888
* init
8989
*/
9090
bool Adafruit_SPIDevice::begin(void) {
91-
pinMode(_cs, OUTPUT);
92-
digitalWrite(_cs, HIGH);
91+
if (_cs != -1) {
92+
pinMode(_cs, OUTPUT);
93+
digitalWrite(_cs, HIGH);
94+
}
9395

9496
if (_spi) { // hardware SPI
9597
_spi->begin();
@@ -299,7 +301,7 @@ bool Adafruit_SPIDevice::write(uint8_t *buffer, size_t len,
299301
_spi->beginTransaction(*_spiSetting);
300302
}
301303

302-
digitalWrite(_cs, LOW);
304+
setChipSelect(LOW);
303305
// do the writing
304306
#if defined(ARDUINO_ARCH_ESP32)
305307
if (_spi) {
@@ -319,7 +321,7 @@ bool Adafruit_SPIDevice::write(uint8_t *buffer, size_t len,
319321
transfer(buffer[i]);
320322
}
321323
}
322-
digitalWrite(_cs, HIGH);
324+
setChipSelect(HIGH);
323325

324326
if (_spi) {
325327
_spi->endTransaction();
@@ -362,9 +364,10 @@ bool Adafruit_SPIDevice::read(uint8_t *buffer, size_t len, uint8_t sendvalue) {
362364
if (_spi) {
363365
_spi->beginTransaction(*_spiSetting);
364366
}
365-
digitalWrite(_cs, LOW);
367+
368+
setChipSelect(LOW);
366369
transfer(buffer, len);
367-
digitalWrite(_cs, HIGH);
370+
setChipSelect(HIGH);
368371

369372
if (_spi) {
370373
_spi->endTransaction();
@@ -406,7 +409,7 @@ bool Adafruit_SPIDevice::write_then_read(uint8_t *write_buffer,
406409
_spi->beginTransaction(*_spiSetting);
407410
}
408411

409-
digitalWrite(_cs, LOW);
412+
setChipSelect(LOW);
410413
// do the writing
411414
#if defined(ARDUINO_ARCH_ESP32)
412415
if (_spi) {
@@ -452,7 +455,7 @@ bool Adafruit_SPIDevice::write_then_read(uint8_t *write_buffer,
452455
DEBUG_SERIAL.println();
453456
#endif
454457

455-
digitalWrite(_cs, HIGH);
458+
setChipSelect(HIGH);
456459

457460
if (_spi) {
458461
_spi->endTransaction();
@@ -476,11 +479,9 @@ bool Adafruit_SPIDevice::write_and_read(uint8_t *buffer, size_t len) {
476479
_spi->beginTransaction(*_spiSetting);
477480
}
478481

479-
digitalWrite(_cs, LOW);
480-
482+
setChipSelect(LOW);
481483
transfer(buffer, len);
482-
483-
digitalWrite(_cs, HIGH);
484+
setChipSelect(HIGH);
484485

485486
if (_spi) {
486487
_spi->endTransaction();
@@ -489,4 +490,10 @@ bool Adafruit_SPIDevice::write_and_read(uint8_t *buffer, size_t len) {
489490
return true;
490491
}
491492

493+
void Adafruit_SPIDevice::setChipSelect(int value) {
494+
if (_cs == -1)
495+
return;
496+
digitalWrite(_cs, value);
497+
}
498+
492499
#endif // SPI exists

Adafruit_SPIDevice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class Adafruit_SPIDevice {
9595
uint32_t _freq;
9696
BusIOBitOrder _dataOrder;
9797
uint8_t _dataMode;
98+
void setChipSelect(int value);
9899

99100
int8_t _cs, _sck, _mosi, _miso;
100101
#ifdef BUSIO_USE_FAST_PINIO

0 commit comments

Comments
 (0)