Skip to content

Commit f82f699

Browse files
committed
reuse beginTransactionWithAssertingCS()/endTransactionWithDeassertingCS()
1 parent c00ec13 commit f82f699

File tree

1 file changed

+8
-38
lines changed

1 file changed

+8
-38
lines changed

Adafruit_SPIDevice.cpp

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -326,11 +326,8 @@ void Adafruit_SPIDevice::endTransactionWithDeassertingCS() {
326326
bool Adafruit_SPIDevice::write(const uint8_t *buffer, size_t len,
327327
const uint8_t *prefix_buffer,
328328
size_t prefix_len) {
329-
if (_spi) {
330-
_spi->beginTransaction(*_spiSetting);
331-
}
329+
beginTransactionWithAssertingCS();
332330

333-
setChipSelect(LOW);
334331
// do the writing
335332
#if defined(ARDUINO_ARCH_ESP32)
336333
if (_spi) {
@@ -350,11 +347,7 @@ bool Adafruit_SPIDevice::write(const uint8_t *buffer, size_t len,
350347
transfer(buffer[i]);
351348
}
352349
}
353-
setChipSelect(HIGH);
354-
355-
if (_spi) {
356-
_spi->endTransaction();
357-
}
350+
endTransactionWithDeassertingCS();
358351

359352
#ifdef DEBUG_SERIAL
360353
DEBUG_SERIAL.print(F("\tSPIDevice Wrote: "));
@@ -391,17 +384,10 @@ bool Adafruit_SPIDevice::write(const uint8_t *buffer, size_t len,
391384
*/
392385
bool Adafruit_SPIDevice::read(uint8_t *buffer, size_t len, uint8_t sendvalue) {
393386
memset(buffer, sendvalue, len); // clear out existing buffer
394-
if (_spi) {
395-
_spi->beginTransaction(*_spiSetting);
396-
}
397387

398-
setChipSelect(LOW);
388+
beginTransactionWithAssertingCS();
399389
transfer(buffer, len);
400-
setChipSelect(HIGH);
401-
402-
if (_spi) {
403-
_spi->endTransaction();
404-
}
390+
endTransactionWithDeassertingCS();
405391

406392
#ifdef DEBUG_SERIAL
407393
DEBUG_SERIAL.print(F("\tSPIDevice Read: "));
@@ -435,11 +421,7 @@ bool Adafruit_SPIDevice::read(uint8_t *buffer, size_t len, uint8_t sendvalue) {
435421
bool Adafruit_SPIDevice::write_then_read(const uint8_t *write_buffer,
436422
size_t write_len, uint8_t *read_buffer,
437423
size_t read_len, uint8_t sendvalue) {
438-
if (_spi) {
439-
_spi->beginTransaction(*_spiSetting);
440-
}
441-
442-
setChipSelect(LOW);
424+
beginTransactionWithAssertingCS();
443425
// do the writing
444426
#if defined(ARDUINO_ARCH_ESP32)
445427
if (_spi) {
@@ -485,11 +467,7 @@ bool Adafruit_SPIDevice::write_then_read(const uint8_t *write_buffer,
485467
DEBUG_SERIAL.println();
486468
#endif
487469

488-
setChipSelect(HIGH);
489-
490-
if (_spi) {
491-
_spi->endTransaction();
492-
}
470+
endTransactionWithDeassertingCS();
493471

494472
return true;
495473
}
@@ -505,17 +483,9 @@ bool Adafruit_SPIDevice::write_then_read(const uint8_t *write_buffer,
505483
* writes
506484
*/
507485
bool Adafruit_SPIDevice::write_and_read(uint8_t *buffer, size_t len) {
508-
if (_spi) {
509-
_spi->beginTransaction(*_spiSetting);
510-
}
511-
512-
setChipSelect(LOW);
486+
beginTransactionWithAssertingCS();
513487
transfer(buffer, len);
514-
setChipSelect(HIGH);
515-
516-
if (_spi) {
517-
_spi->endTransaction();
518-
}
488+
endTransactionWithDeassertingCS();
519489

520490
return true;
521491
}

0 commit comments

Comments
 (0)