Skip to content

Commit 31bf0d5

Browse files
committed
spi: Added block-level SPI writes at the HAL level
Poking the block-level SPI writes through the HAL level gives more freedom to targets to improve SPI transaction speed.
1 parent 58491aa commit 31bf0d5

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

drivers/SPI.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,11 @@ int SPI::write(int value) {
7979
}
8080

8181
int SPI::write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
82-
int total = (tx_length > rx_length) ? tx_length : rx_length;
83-
8482
lock();
8583
aquire();
86-
for (int i = 0; i < total; i++) {
87-
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
88-
char in = spi_master_write(&_spi, out);
89-
if (i < rx_length) {
90-
rx_buffer[i] = in;
91-
}
92-
}
84+
int ret = spi_master_block_write(&_spi, tx_buffer, tx_length, rx_buffer, rx_length);
9385
unlock();
94-
95-
return total;
86+
return ret;
9687
}
9788

9889
void SPI::lock() {

hal/spi_api.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,23 @@ void spi_frequency(spi_t *obj, int hz);
116116
*/
117117
int spi_master_write(spi_t *obj, int value);
118118

119+
/** Write a block out in master mode and receive a value
120+
*
121+
* The total number of bytes sent and recieved will be the maximum of
122+
* tx_length and rx_length. The bytes written will be padded with the
123+
* value 0xff.
124+
*
125+
* @param[in] obj The SPI peripheral to use for sending
126+
* @param[in] tx_buffer Pointer to the byte-array of data to write to the device
127+
* @param[in] tx_length Number of bytes to write, may be zero
128+
* @param[in] rx_buffer Pointer to the byte-array of data to read from the device
129+
* @param[in] rx_length Number of bytes to read, may be zero
130+
* @returns
131+
* The number of bytes written and read from the device. This is
132+
* maximum of tx_length and rx_length.
133+
*/
134+
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length);
135+
119136
/** Check if a value is available to read
120137
*
121138
* @param[in] obj The SPI peripheral to check

0 commit comments

Comments
 (0)