Skip to content

Commit b949d28

Browse files
authored
Merge pull request adafruit#93 from eringerli/cs-transaction-management
better transaction management
2 parents 204c1de + f82f699 commit b949d28

File tree

2 files changed

+40
-48
lines changed

2 files changed

+40
-48
lines changed

Adafruit_SPIDevice.cpp

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,36 @@ void Adafruit_SPIDevice::endTransaction(void) {
282282
}
283283
}
284284

285+
/*!
286+
* @brief Assert/Deassert the CS pin if it is defined
287+
* @param value The state the CS is set to
288+
*/
289+
void Adafruit_SPIDevice::setChipSelect(int value) {
290+
if (_cs != -1) {
291+
digitalWrite(_cs, value);
292+
}
293+
}
294+
295+
/*!
296+
* @brief Write a buffer or two to the SPI device, with transaction
297+
* management.
298+
* @brief Manually begin a transaction (calls beginTransaction if hardware
299+
* SPI) with asserting the CS pin
300+
*/
301+
void Adafruit_SPIDevice::beginTransactionWithAssertingCS() {
302+
beginTransaction();
303+
setChipSelect(LOW);
304+
}
305+
306+
/*!
307+
* @brief Manually end a transaction (calls endTransaction if hardware SPI)
308+
* with deasserting the CS pin
309+
*/
310+
void Adafruit_SPIDevice::endTransactionWithDeassertingCS() {
311+
setChipSelect(HIGH);
312+
endTransaction();
313+
}
314+
285315
/*!
286316
* @brief Write a buffer or two to the SPI device, with transaction
287317
* management.
@@ -296,11 +326,8 @@ void Adafruit_SPIDevice::endTransaction(void) {
296326
bool Adafruit_SPIDevice::write(const uint8_t *buffer, size_t len,
297327
const uint8_t *prefix_buffer,
298328
size_t prefix_len) {
299-
if (_spi) {
300-
_spi->beginTransaction(*_spiSetting);
301-
}
329+
beginTransactionWithAssertingCS();
302330

303-
setChipSelect(LOW);
304331
// do the writing
305332
#if defined(ARDUINO_ARCH_ESP32)
306333
if (_spi) {
@@ -320,11 +347,7 @@ bool Adafruit_SPIDevice::write(const uint8_t *buffer, size_t len,
320347
transfer(buffer[i]);
321348
}
322349
}
323-
setChipSelect(HIGH);
324-
325-
if (_spi) {
326-
_spi->endTransaction();
327-
}
350+
endTransactionWithDeassertingCS();
328351

329352
#ifdef DEBUG_SERIAL
330353
DEBUG_SERIAL.print(F("\tSPIDevice Wrote: "));
@@ -361,17 +384,10 @@ bool Adafruit_SPIDevice::write(const uint8_t *buffer, size_t len,
361384
*/
362385
bool Adafruit_SPIDevice::read(uint8_t *buffer, size_t len, uint8_t sendvalue) {
363386
memset(buffer, sendvalue, len); // clear out existing buffer
364-
if (_spi) {
365-
_spi->beginTransaction(*_spiSetting);
366-
}
367387

368-
setChipSelect(LOW);
388+
beginTransactionWithAssertingCS();
369389
transfer(buffer, len);
370-
setChipSelect(HIGH);
371-
372-
if (_spi) {
373-
_spi->endTransaction();
374-
}
390+
endTransactionWithDeassertingCS();
375391

376392
#ifdef DEBUG_SERIAL
377393
DEBUG_SERIAL.print(F("\tSPIDevice Read: "));
@@ -405,11 +421,7 @@ bool Adafruit_SPIDevice::read(uint8_t *buffer, size_t len, uint8_t sendvalue) {
405421
bool Adafruit_SPIDevice::write_then_read(const uint8_t *write_buffer,
406422
size_t write_len, uint8_t *read_buffer,
407423
size_t read_len, uint8_t sendvalue) {
408-
if (_spi) {
409-
_spi->beginTransaction(*_spiSetting);
410-
}
411-
412-
setChipSelect(LOW);
424+
beginTransactionWithAssertingCS();
413425
// do the writing
414426
#if defined(ARDUINO_ARCH_ESP32)
415427
if (_spi) {
@@ -455,11 +467,7 @@ bool Adafruit_SPIDevice::write_then_read(const uint8_t *write_buffer,
455467
DEBUG_SERIAL.println();
456468
#endif
457469

458-
setChipSelect(HIGH);
459-
460-
if (_spi) {
461-
_spi->endTransaction();
462-
}
470+
endTransactionWithDeassertingCS();
463471

464472
return true;
465473
}
@@ -475,29 +483,11 @@ bool Adafruit_SPIDevice::write_then_read(const uint8_t *write_buffer,
475483
* writes
476484
*/
477485
bool Adafruit_SPIDevice::write_and_read(uint8_t *buffer, size_t len) {
478-
if (_spi) {
479-
_spi->beginTransaction(*_spiSetting);
480-
}
481-
482-
setChipSelect(LOW);
486+
beginTransactionWithAssertingCS();
483487
transfer(buffer, len);
484-
setChipSelect(HIGH);
485-
486-
if (_spi) {
487-
_spi->endTransaction();
488-
}
488+
endTransactionWithDeassertingCS();
489489

490490
return true;
491491
}
492492

493-
/*!
494-
* @brief Assert/Deassert the CS pin if it is defined
495-
* @param value The state the CS is set to
496-
*/
497-
void Adafruit_SPIDevice::setChipSelect(int value) {
498-
if (_cs == -1)
499-
return;
500-
digitalWrite(_cs, value);
501-
}
502-
503493
#endif // SPI exists

Adafruit_SPIDevice.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ class Adafruit_SPIDevice {
8888
void transfer(uint8_t *buffer, size_t len);
8989
void beginTransaction(void);
9090
void endTransaction(void);
91+
void beginTransactionWithAssertingCS();
92+
void endTransactionWithDeassertingCS();
9193

9294
private:
9395
SPIClass *_spi;

0 commit comments

Comments
 (0)