Skip to content

Commit 1f94ede

Browse files
Merge pull request #4744 from deepikabhavnani/spi_issue_4743
Allow user to set default transfer byte for block read
2 parents d5108e5 + 1b797e9 commit 1f94ede

File tree

51 files changed

+176
-111
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+176
-111
lines changed

drivers/SPI.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ SPI::SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel) :
3232
#endif
3333
_bits(8),
3434
_mode(0),
35-
_hz(1000000) {
35+
_hz(1000000),
36+
_write_fill(SPI_FILL_CHAR) {
3637
// No lock needed in the constructor
3738

3839
spi_init(&_spi, mosi, miso, sclk, ssel);
@@ -102,7 +103,7 @@ int SPI::write(int value) {
102103
int SPI::write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
103104
lock();
104105
_acquire();
105-
int ret = spi_master_block_write(&_spi, tx_buffer, tx_length, rx_buffer, rx_length);
106+
int ret = spi_master_block_write(&_spi, tx_buffer, tx_length, rx_buffer, rx_length, _write_fill);
106107
unlock();
107108
return ret;
108109
}
@@ -115,6 +116,12 @@ void SPI::unlock() {
115116
_mutex->unlock();
116117
}
117118

119+
void SPI::set_default_write_value(char data) {
120+
lock();
121+
_write_fill = data;
122+
unlock();
123+
}
124+
118125
#if DEVICE_SPI_ASYNCH
119126

120127
int SPI::transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t& callback, int event)

drivers/SPI.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ class SPI : private NonCopyable<SPI> {
143143
*/
144144
virtual void unlock(void);
145145

146+
/** Set default write data
147+
* SPI requires the master to send some data during a read operation.
148+
* Different devices may require different default byte values.
149+
* For example: A SD Card requires default bytes to be 0xFF.
150+
*
151+
* @param data Default character to be transmitted while read operation
152+
*/
153+
void set_default_write_value(char data);
154+
146155
#if DEVICE_SPI_ASYNCH
147156

148157
/** Start non-blocking SPI transfer using 8bit buffers.
@@ -271,6 +280,7 @@ class SPI : private NonCopyable<SPI> {
271280
int _bits;
272281
int _mode;
273282
int _hz;
283+
char _write_fill;
274284

275285
private:
276286
/* Private acquire function without locking/unlocking

hal/spi_api.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#define SPI_EVENT_INTERNAL_TRANSFER_COMPLETE (1 << 30) // Internal flag to report that an event occurred
3434

3535
#define SPI_FILL_WORD (0xFFFF)
36+
#define SPI_FILL_CHAR (0xFF)
3637

3738
#if DEVICE_SPI_ASYNCH
3839
/** Asynch SPI HAL structure
@@ -122,16 +123,17 @@ int spi_master_write(spi_t *obj, int value);
122123
* tx_length and rx_length. The bytes written will be padded with the
123124
* value 0xff.
124125
*
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
126+
* @param[in] obj The SPI peripheral to use for sending
127+
* @param[in] tx_buffer Pointer to the byte-array of data to write to the device
128+
* @param[in] tx_length Number of bytes to write, may be zero
129+
* @param[in] rx_buffer Pointer to the byte-array of data to read from the device
130+
* @param[in] rx_length Number of bytes to read, may be zero
131+
* @param[in] write_fill Default data transmitted while performing a read
130132
* @returns
131133
* The number of bytes written and read from the device. This is
132134
* maximum of tx_length and rx_length.
133135
*/
134-
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length);
136+
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, char write_fill);
135137

136138
/** Check if a value is available to read
137139
*

targets/TARGET_ARM_SSG/TARGET_BEETLE/spi_api.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,12 @@ int spi_master_write(spi_t *obj, int value) {
262262
return data;
263263
}
264264

265-
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
265+
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
266+
char *rx_buffer, int rx_length, char write_fill) {
266267
int total = (tx_length > rx_length) ? tx_length : rx_length;
267268

268269
for (int i = 0; i < total; i++) {
269-
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
270+
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
270271
char in = spi_master_write(obj, out);
271272
if (i < rx_length) {
272273
rx_buffer[i] = in;

targets/TARGET_ARM_SSG/TARGET_CM3DS_MPS2/spi_api.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,13 @@ int spi_master_write(spi_t *obj, int value)
256256
}
257257

258258
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
259-
char *rx_buffer, int rx_length)
259+
char *rx_buffer, int rx_length, char write_fill)
260260
{
261261
int total = (tx_length > rx_length) ? tx_length : rx_length;
262262
char out, in;
263263

264264
for (int i = 0; i < total; i++) {
265-
out = (i < tx_length) ? tx_buffer[i] : 0xff;
265+
out = (i < tx_length) ? tx_buffer[i] : write_fill;
266266
in = spi_master_write(obj, out);
267267
if (i < rx_length) {
268268
rx_buffer[i] = in;

targets/TARGET_ARM_SSG/TARGET_IOTSS/spi_api.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,12 @@ int spi_master_write(spi_t *obj, int value) {
268268
return (ssp_read(obj));
269269
}
270270

271-
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
271+
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
272+
char *rx_buffer, int rx_length, char write_fill) {
272273
int total = (tx_length > rx_length) ? tx_length : rx_length;
273274

274275
for (int i = 0; i < total; i++) {
275-
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
276+
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
276277
char in = spi_master_write(obj, out);
277278
if (i < rx_length) {
278279
rx_buffer[i] = in;

targets/TARGET_ARM_SSG/TARGET_MPS2/spi_api.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,12 @@ int spi_master_write(spi_t *obj, int value) {
268268
return (ssp_read(obj));
269269
}
270270

271-
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
271+
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
272+
char *rx_buffer, int rx_length, char write_fill) {
272273
int total = (tx_length > rx_length) ? tx_length : rx_length;
273274

274275
for (int i = 0; i < total; i++) {
275-
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
276+
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
276277
char in = spi_master_write(obj, out);
277278
if (i < rx_length) {
278279
rx_buffer[i] = in;

targets/TARGET_Atmel/TARGET_SAM_CortexM0P/spi_api.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,11 +555,12 @@ int spi_master_write(spi_t *obj, int value)
555555
return rx_data;
556556
}
557557

558-
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
558+
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
559+
char *rx_buffer, int rx_length, char write_fill) {
559560
int total = (tx_length > rx_length) ? tx_length : rx_length;
560561

561562
for (int i = 0; i < total; i++) {
562-
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
563+
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
563564
char in = spi_master_write(obj, out);
564565
if (i < rx_length) {
565566
rx_buffer[i] = in;

targets/TARGET_Atmel/TARGET_SAM_CortexM4/spi_api.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,12 @@ int spi_master_write(spi_t *obj, int value)
296296
return 0;
297297
}
298298

299-
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
299+
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
300+
char *rx_buffer, int rx_length, char _write_fill) {
300301
int total = (tx_length > rx_length) ? tx_length : rx_length;
301302

302303
for (int i = 0; i < total; i++) {
303-
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
304+
char out = (i < tx_length) ? tx_buffer[i] : _write_fill;
304305
char in = spi_master_write(obj, out);
305306
if (i < rx_length) {
306307
rx_buffer[i] = in;

targets/TARGET_Freescale/TARGET_K20XX/spi_api.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,12 @@ int spi_master_write(spi_t *obj, int value) {
140140
return obj->spi->POPR;
141141
}
142142

143-
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
143+
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
144+
char *rx_buffer, int rx_length, char write_fill) {
144145
int total = (tx_length > rx_length) ? tx_length : rx_length;
145146

146147
for (int i = 0; i < total; i++) {
147-
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
148+
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
148149
char in = spi_master_write(obj, out);
149150
if (i < rx_length) {
150151
rx_buffer[i] = in;

0 commit comments

Comments
 (0)