Skip to content

Commit c43e0ae

Browse files
committed
added write_and_read()
This method adds a wrapper for transfer() with CS-pin and transaction management
1 parent 9c5fcb7 commit c43e0ae

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Adafruit_SPIDevice.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,4 +440,32 @@ bool Adafruit_SPIDevice::write_then_read(uint8_t *write_buffer,
440440
return true;
441441
}
442442

443+
/*!
444+
* @brief Write some data and read some data at the same time from SPI
445+
* into the same buffer. This is basicaly a wrapper for transfer() with
446+
* CS-pin and transaction management.
447+
* This /does/ transmit-receive at the same time!
448+
* @param buffer Pointer to buffer of data to write/read to/from
449+
* @param len Number of bytes from buffer to write/read.
450+
* @return Always returns true because there's no way to test success of SPI
451+
* writes
452+
*/
453+
bool Adafruit_SPIDevice::write_and_read(uint8_t *buffer, size_t len) {
454+
if (_spi) {
455+
_spi->beginTransaction(*_spiSetting);
456+
}
457+
458+
digitalWrite(_cs, LOW);
459+
460+
transfer(buffer, len);
461+
462+
digitalWrite(_cs, HIGH);
463+
464+
if (_spi) {
465+
_spi->endTransaction();
466+
}
467+
468+
return true;
469+
}
470+
443471
#endif // SPI exists

Adafruit_SPIDevice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class Adafruit_SPIDevice {
8282
bool write_then_read(uint8_t *write_buffer, size_t write_len,
8383
uint8_t *read_buffer, size_t read_len,
8484
uint8_t sendvalue = 0xFF);
85+
bool write_and_read(uint8_t *buffer, size_t len);
8586

8687
uint8_t transfer(uint8_t send);
8788
void transfer(uint8_t *buffer, size_t len);

0 commit comments

Comments
 (0)