Skip to content

Commit 1fae13f

Browse files
committed
Redesiging SpiDispatcher to conform the write_then_read API of Adafruit_BusIO.
* All data returned by spi.transfer() before the end of the write buffer are directly written into the write buffer. * All data returned by spi.transer() after the end of the write buffer are written into the read buffer. * After exhaustion of the write buffer only the fill_value is being sent.
1 parent 414908d commit 1fae13f

File tree

9 files changed

+41
-45
lines changed

9 files changed

+41
-45
lines changed

Diff for: examples/Threadsafe_SPI/Threadsafe_SPI.ino

+4-9
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,17 @@ void loop()
6060

6161
byte bmp388_read_reg(byte const reg_addr)
6262
{
63-
byte const write_buf[3] =
64-
{
65-
static_cast<byte>(0x80 | reg_addr), /* REG_ADDR, if MSBit is set -> READ access */
66-
0, /* Dummy byte. */
67-
0 /* REG_VAL is output on SDO */
68-
};
69-
byte read_buf[3] = {0};
63+
/* REG_ADDR | DUMMY_BYTE | REG_VAL is on SDO */
64+
byte read_write_buf[] = {static_cast<byte>(0x80 | reg_addr), 0, 0};
7065

71-
IoRequest req(write_buf, sizeof(write_buf), read_buf, sizeof(read_buf));
66+
IoRequest req(read_write_buf, sizeof(read_write_buf), nullptr, 0);
7267
IoResponse rsp = bmp388.transfer(req);
7368

7469
/* Do other stuff */
7570

7671
rsp->wait();
7772

78-
return read_buf[2];
73+
return read_write_buf[2];
7974
}
8075

8176
void bmp388_thread_func()

Diff for: examples/Threadsafe_SPI_BusIO/Threadsafe_SPI_BusIO.ino

+5-10
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,12 @@ void loop()
6060

6161
byte bmp388_read_reg(byte const reg_addr)
6262
{
63-
byte const write_buf[3] =
64-
{
65-
static_cast<byte>(0x80 | reg_addr), /* REG_ADDR, if MSBit is set -> READ access */
66-
0, /* Dummy byte. */
67-
0 /* REG_VAL is output on SDO */
68-
};
69-
byte read_buf[3] = {0};
70-
71-
bmp388.spi().write_then_read(write_buf, sizeof(write_buf), read_buf, sizeof(read_buf));
63+
/* REG_ADDR | DUMMY_BYTE | REG_VAL is on SDO */
64+
byte write_buf[2] = {static_cast<byte>(0x80 | reg_addr), 0};
65+
byte read_buf = 0;
7266

73-
return read_buf[2];
67+
bmp388.spi().write_then_read(write_buf, sizeof(write_buf), &read_buf, sizeof(read_buf));
68+
return read_buf;
7469
}
7570

7671
void bmp388_thread_func()

Diff for: examples/Threadsafe_Wire_BusIO/Threadsafe_Wire_BusIO.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void loop()
5555
* FUNCTION DEFINITION
5656
**************************************************************************************/
5757

58-
byte lsm6dsox_read_reg(byte const reg_addr)
58+
byte lsm6dsox_read_reg(byte reg_addr)
5959
{
6060
byte read_buf = 0;
6161
lsm6dsox.wire().write_then_read(&reg_addr, 1, &read_buf, 1);

Diff for: src/IoTransaction.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,18 @@ class IoRequest
4141
{
4242
public:
4343

44-
IoRequest(byte const * const write_buf_, size_t const bytes_to_write_, byte * read_buf_, size_t const bytes_to_read_)
44+
IoRequest(byte * write_buf_, size_t const bytes_to_write_, byte * read_buf_, size_t const bytes_to_read_)
4545
: write_buf{write_buf_}
4646
, bytes_to_write{bytes_to_write_}
4747
, read_buf{read_buf_}
4848
, bytes_to_read{bytes_to_read_}
4949
{ }
5050

51-
IoRequest(byte const & write_buf_, byte & read_buf_)
51+
IoRequest(byte & write_buf_, byte & read_buf_)
5252
: IoRequest{&write_buf_, 1, &read_buf_, 1}
5353
{ }
5454

55-
byte const * const write_buf{nullptr};
55+
byte * write_buf{nullptr};
5656
size_t const bytes_to_write{0};
5757
byte * read_buf{nullptr};
5858
size_t const bytes_to_read{0};

Diff for: src/spi/SpiBusDevice.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ bool SpiBusDevice::read(uint8_t * buffer, size_t len, uint8_t sendvalue)
5252
return true;
5353
}
5454

55-
bool SpiBusDevice::write(const uint8_t * buffer, size_t len)
55+
bool SpiBusDevice::write(uint8_t * buffer, size_t len)
5656
{
5757
IoRequest req(buffer, len, nullptr, 0);
5858
IoResponse rsp = SpiDispatcher::instance().dispatch(&req, &_config);
5959
rsp->wait();
6060
return true;
6161
}
6262

63-
bool SpiBusDevice::write_then_read(const uint8_t * write_buffer, size_t write_len, uint8_t * read_buffer, size_t read_len, uint8_t sendvalue)
63+
bool SpiBusDevice::write_then_read(uint8_t * write_buffer, size_t write_len, uint8_t * read_buffer, size_t read_len, uint8_t sendvalue)
6464
{
6565
SpiBusDeviceConfig config(_config.spi(), _config.settings(), _config.select_func(), _config.deselect_func(), sendvalue);
6666
IoRequest req(write_buffer, write_len, read_buffer, read_len);

Diff for: src/spi/SpiBusDevice.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ class SpiBusDevice : public BusDeviceBase
4343

4444

4545
bool read(uint8_t * buffer, size_t len, uint8_t sendvalue = 0xFF);
46-
bool write(const uint8_t * buffer, size_t len);
47-
bool write_then_read(const uint8_t * write_buffer, size_t write_len, uint8_t * read_buffer, size_t read_len, uint8_t sendvalue = 0xFF);
46+
bool write(uint8_t * buffer, size_t len);
47+
bool write_then_read(uint8_t * write_buffer, size_t write_len, uint8_t * read_buffer, size_t read_len, uint8_t sendvalue = 0xFF);
4848

4949

5050
private:

Diff for: src/spi/SpiDispatcher.cpp

+20-14
Original file line numberDiff line numberDiff line change
@@ -140,24 +140,30 @@ void SpiDispatcher::processSpiIoRequest(SpiIoTransaction * spi_io_transaction)
140140

141141
config->spi().beginTransaction(config->settings());
142142

143-
size_t bytes_received = 0,
144-
bytes_sent = 0;
145-
for(;
146-
bytes_received < std::max(io_request->bytes_to_read, io_request->bytes_to_write);
147-
bytes_received++, bytes_sent++)
143+
/* In a first step transmit the complete write buffer and
144+
* write back the receive data directly into the write buffer
145+
*/
146+
size_t bytes_sent = 0;
147+
for(; bytes_sent < io_request->bytes_to_write; bytes_sent++)
148148
{
149-
byte tx_byte = 0;
149+
uint8_t const tx_byte = io_request->write_buf[bytes_sent];
150+
uint8_t const rx_byte = config->spi().transfer(tx_byte);
151+
152+
io_request->write_buf[bytes_sent] = rx_byte;
153+
}
150154

151-
if (io_request->write_buf && (bytes_sent < io_request->bytes_to_write))
152-
tx_byte = io_request->write_buf[bytes_sent];
153-
else
154-
tx_byte = config->fill_symbol();
155+
/* In a second step, transmit the fill symbol and write the
156+
* received data into the read buffer.
157+
*/
158+
size_t bytes_received = 0;
159+
for(; bytes_received < io_request->bytes_to_read; bytes_received++)
160+
{
161+
uint8_t const tx_byte = config->fill_symbol();
162+
uint8_t const rx_byte = config->spi().transfer(tx_byte);
155163

156-
byte const rx_byte = config->spi().transfer(tx_byte);
164+
io_request->read_buf[bytes_received] = rx_byte;
165+
}
157166

158-
if (io_request->read_buf && (bytes_received < io_request->bytes_to_read))
159-
io_request->read_buf[bytes_received] = rx_byte;
160-
}
161167
config->spi().endTransaction();
162168

163169
config->deselect();

Diff for: src/wire/WireBusDevice.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ bool WireBusDevice::read(uint8_t * buffer, size_t len, bool stop)
5252
return true;
5353
}
5454

55-
bool WireBusDevice::write(const uint8_t * buffer, size_t len, bool stop)
55+
bool WireBusDevice::write(uint8_t * buffer, size_t len, bool stop)
5656
{
5757
bool const restart = !stop;
5858
WireBusDeviceConfig config(_config.wire(), _config.slave_addr(), restart, _config.stop());
@@ -62,7 +62,7 @@ bool WireBusDevice::write(const uint8_t * buffer, size_t len, bool stop)
6262
return true;
6363
}
6464

65-
bool WireBusDevice::write_then_read(const uint8_t * write_buffer, size_t write_len, uint8_t * read_buffer, size_t read_len, bool stop)
65+
bool WireBusDevice::write_then_read(uint8_t * write_buffer, size_t write_len, uint8_t * read_buffer, size_t read_len, bool stop)
6666
{
6767
/* Copy the Wire parameters from the device and modify only those
6868
* which can be modified via the parameters of this function.

Diff for: src/wire/WireBusDevice.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ class WireBusDevice : public BusDeviceBase
4343

4444

4545
bool read(uint8_t * buffer, size_t len, bool stop = true);
46-
bool write(const uint8_t * buffer, size_t len, bool stop = true);
47-
bool write_then_read(const uint8_t * write_buffer, size_t write_len, uint8_t * read_buffer, size_t read_len, bool stop = false);
46+
bool write(uint8_t * buffer, size_t len, bool stop = true);
47+
bool write_then_read(uint8_t * write_buffer, size_t write_len, uint8_t * read_buffer, size_t read_len, bool stop = false);
4848

4949

5050
private:

0 commit comments

Comments
 (0)