Skip to content

Commit

Permalink
Merge pull request #4744 from deepikabhavnani/spi_issue_4743
Browse files Browse the repository at this point in the history
Allow user to set default transfer byte for block read
  • Loading branch information
theotherjimmy committed Jul 24, 2017
2 parents d5108e5 + 1b797e9 commit 1f94ede
Show file tree
Hide file tree
Showing 51 changed files with 176 additions and 111 deletions.
11 changes: 9 additions & 2 deletions drivers/SPI.cpp
Expand Up @@ -32,7 +32,8 @@ SPI::SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel) :
#endif
_bits(8),
_mode(0),
_hz(1000000) {
_hz(1000000),
_write_fill(SPI_FILL_CHAR) {
// No lock needed in the constructor

spi_init(&_spi, mosi, miso, sclk, ssel);
Expand Down Expand Up @@ -102,7 +103,7 @@ int SPI::write(int value) {
int SPI::write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
lock();
_acquire();
int ret = spi_master_block_write(&_spi, tx_buffer, tx_length, rx_buffer, rx_length);
int ret = spi_master_block_write(&_spi, tx_buffer, tx_length, rx_buffer, rx_length, _write_fill);
unlock();
return ret;
}
Expand All @@ -115,6 +116,12 @@ void SPI::unlock() {
_mutex->unlock();
}

void SPI::set_default_write_value(char data) {
lock();
_write_fill = data;
unlock();
}

#if DEVICE_SPI_ASYNCH

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)
Expand Down
10 changes: 10 additions & 0 deletions drivers/SPI.h
Expand Up @@ -143,6 +143,15 @@ class SPI : private NonCopyable<SPI> {
*/
virtual void unlock(void);

/** Set default write data
* SPI requires the master to send some data during a read operation.
* Different devices may require different default byte values.
* For example: A SD Card requires default bytes to be 0xFF.
*
* @param data Default character to be transmitted while read operation
*/
void set_default_write_value(char data);

#if DEVICE_SPI_ASYNCH

/** Start non-blocking SPI transfer using 8bit buffers.
Expand Down Expand Up @@ -271,6 +280,7 @@ class SPI : private NonCopyable<SPI> {
int _bits;
int _mode;
int _hz;
char _write_fill;

private:
/* Private acquire function without locking/unlocking
Expand Down
14 changes: 8 additions & 6 deletions hal/spi_api.h
Expand Up @@ -33,6 +33,7 @@
#define SPI_EVENT_INTERNAL_TRANSFER_COMPLETE (1 << 30) // Internal flag to report that an event occurred

#define SPI_FILL_WORD (0xFFFF)
#define SPI_FILL_CHAR (0xFF)

#if DEVICE_SPI_ASYNCH
/** Asynch SPI HAL structure
Expand Down Expand Up @@ -122,16 +123,17 @@ int spi_master_write(spi_t *obj, int value);
* tx_length and rx_length. The bytes written will be padded with the
* value 0xff.
*
* @param[in] obj The SPI peripheral to use for sending
* @param[in] tx_buffer Pointer to the byte-array of data to write to the device
* @param[in] tx_length Number of bytes to write, may be zero
* @param[in] rx_buffer Pointer to the byte-array of data to read from the device
* @param[in] rx_length Number of bytes to read, may be zero
* @param[in] obj The SPI peripheral to use for sending
* @param[in] tx_buffer Pointer to the byte-array of data to write to the device
* @param[in] tx_length Number of bytes to write, may be zero
* @param[in] rx_buffer Pointer to the byte-array of data to read from the device
* @param[in] rx_length Number of bytes to read, may be zero
* @param[in] write_fill Default data transmitted while performing a read
* @returns
* The number of bytes written and read from the device. This is
* maximum of tx_length and rx_length.
*/
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length);
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, char write_fill);

/** Check if a value is available to read
*
Expand Down
5 changes: 3 additions & 2 deletions targets/TARGET_ARM_SSG/TARGET_BEETLE/spi_api.c
Expand Up @@ -262,11 +262,12 @@ int spi_master_write(spi_t *obj, int value) {
return data;
}

int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
char *rx_buffer, int rx_length, char write_fill) {
int total = (tx_length > rx_length) ? tx_length : rx_length;

for (int i = 0; i < total; i++) {
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
char in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
Expand Down
4 changes: 2 additions & 2 deletions targets/TARGET_ARM_SSG/TARGET_CM3DS_MPS2/spi_api.c
Expand Up @@ -256,13 +256,13 @@ int spi_master_write(spi_t *obj, int value)
}

int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
char *rx_buffer, int rx_length)
char *rx_buffer, int rx_length, char write_fill)
{
int total = (tx_length > rx_length) ? tx_length : rx_length;
char out, in;

for (int i = 0; i < total; i++) {
out = (i < tx_length) ? tx_buffer[i] : 0xff;
out = (i < tx_length) ? tx_buffer[i] : write_fill;
in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
Expand Down
5 changes: 3 additions & 2 deletions targets/TARGET_ARM_SSG/TARGET_IOTSS/spi_api.c
Expand Up @@ -268,11 +268,12 @@ int spi_master_write(spi_t *obj, int value) {
return (ssp_read(obj));
}

int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
char *rx_buffer, int rx_length, char write_fill) {
int total = (tx_length > rx_length) ? tx_length : rx_length;

for (int i = 0; i < total; i++) {
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
char in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
Expand Down
5 changes: 3 additions & 2 deletions targets/TARGET_ARM_SSG/TARGET_MPS2/spi_api.c
Expand Up @@ -268,11 +268,12 @@ int spi_master_write(spi_t *obj, int value) {
return (ssp_read(obj));
}

int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
char *rx_buffer, int rx_length, char write_fill) {
int total = (tx_length > rx_length) ? tx_length : rx_length;

for (int i = 0; i < total; i++) {
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
char in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
Expand Down
5 changes: 3 additions & 2 deletions targets/TARGET_Atmel/TARGET_SAM_CortexM0P/spi_api.c
Expand Up @@ -555,11 +555,12 @@ int spi_master_write(spi_t *obj, int value)
return rx_data;
}

int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
char *rx_buffer, int rx_length, char write_fill) {
int total = (tx_length > rx_length) ? tx_length : rx_length;

for (int i = 0; i < total; i++) {
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
char in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
Expand Down
5 changes: 3 additions & 2 deletions targets/TARGET_Atmel/TARGET_SAM_CortexM4/spi_api.c
Expand Up @@ -296,11 +296,12 @@ int spi_master_write(spi_t *obj, int value)
return 0;
}

int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
char *rx_buffer, int rx_length, char _write_fill) {
int total = (tx_length > rx_length) ? tx_length : rx_length;

for (int i = 0; i < total; i++) {
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
char out = (i < tx_length) ? tx_buffer[i] : _write_fill;
char in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
Expand Down
5 changes: 3 additions & 2 deletions targets/TARGET_Freescale/TARGET_K20XX/spi_api.c
Expand Up @@ -140,11 +140,12 @@ int spi_master_write(spi_t *obj, int value) {
return obj->spi->POPR;
}

int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
char *rx_buffer, int rx_length, char write_fill) {
int total = (tx_length > rx_length) ? tx_length : rx_length;

for (int i = 0; i < total; i++) {
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
char in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
Expand Down
5 changes: 3 additions & 2 deletions targets/TARGET_Freescale/TARGET_KLXX/TARGET_KL05Z/spi_api.c
Expand Up @@ -141,11 +141,12 @@ int spi_master_write(spi_t *obj, int value) {
return obj->spi->D & 0xff;
}

int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
char *rx_buffer, int rx_length, char write_fill) {
int total = (tx_length > rx_length) ? tx_length : rx_length;

for (int i = 0; i < total; i++) {
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
char in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
Expand Down
5 changes: 3 additions & 2 deletions targets/TARGET_Freescale/TARGET_KLXX/TARGET_KL25Z/spi_api.c
Expand Up @@ -120,11 +120,12 @@ int spi_master_write(spi_t *obj, int value) {
return obj->spi->D & 0xff;
}

int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
char *rx_buffer, int rx_length, char write_fill) {
int total = (tx_length > rx_length) ? tx_length : rx_length;

for (int i = 0; i < total; i++) {
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
char in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
Expand Down
5 changes: 3 additions & 2 deletions targets/TARGET_Freescale/TARGET_KLXX/TARGET_KL26Z/spi_api.c
Expand Up @@ -199,11 +199,12 @@ int spi_master_write(spi_t *obj, int value) {
return ret;
}

int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
char *rx_buffer, int rx_length, char write_fill) {
int total = (tx_length > rx_length) ? tx_length : rx_length;

for (int i = 0; i < total; i++) {
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
char in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
Expand Down
5 changes: 3 additions & 2 deletions targets/TARGET_Freescale/TARGET_KLXX/TARGET_KL46Z/spi_api.c
Expand Up @@ -199,11 +199,12 @@ int spi_master_write(spi_t *obj, int value) {
return ret;
}

int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
char *rx_buffer, int rx_length, char write_fill) {
int total = (tx_length > rx_length) ? tx_length : rx_length;

for (int i = 0; i < total; i++) {
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
char in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
Expand Down
Expand Up @@ -118,11 +118,12 @@ int spi_master_write(spi_t *obj, int value)
return rx_data & 0xffff;
}

int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
char *rx_buffer, int rx_length, char write_fill) {
int total = (tx_length > rx_length) ? tx_length : rx_length;

for (int i = 0; i < total; i++) {
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
char in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
Expand Down
Expand Up @@ -118,11 +118,12 @@ int spi_master_write(spi_t *obj, int value)
return rx_data & 0xffff;
}

int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
char *rx_buffer, int rx_length, char write_fill) {
int total = (tx_length > rx_length) ? tx_length : rx_length;

for (int i = 0; i < total; i++) {
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
char in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
Expand Down
Expand Up @@ -115,11 +115,12 @@ int spi_master_write(spi_t *obj, int value)
return rx_data & 0xffff;
}

int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
char *rx_buffer, int rx_length, char write_fill) {
int total = (tx_length > rx_length) ? tx_length : rx_length;

for (int i = 0; i < total; i++) {
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
char in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
Expand Down
Expand Up @@ -115,11 +115,12 @@ int spi_master_write(spi_t *obj, int value)
return rx_data & 0xffff;
}

int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
char *rx_buffer, int rx_length, char write_fill) {
int total = (tx_length > rx_length) ? tx_length : rx_length;

for (int i = 0; i < total; i++) {
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
char in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
Expand Down
Expand Up @@ -117,11 +117,12 @@ int spi_master_write(spi_t *obj, int value)
return rx_data & 0xffff;
}

int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
char *rx_buffer, int rx_length, char write_fill) {
int total = (tx_length > rx_length) ? tx_length : rx_length;

for (int i = 0; i < total; i++) {
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
char in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
Expand Down
Expand Up @@ -117,11 +117,12 @@ int spi_master_write(spi_t *obj, int value)
return rx_data & 0xffff;
}

int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
char *rx_buffer, int rx_length, char write_fill) {
int total = (tx_length > rx_length) ? tx_length : rx_length;

for (int i = 0; i < total; i++) {
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
char in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
Expand Down
Expand Up @@ -117,11 +117,12 @@ int spi_master_write(spi_t *obj, int value)
return rx_data & 0xffff;
}

int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
char *rx_buffer, int rx_length, char write_fill) {
int total = (tx_length > rx_length) ? tx_length : rx_length;

for (int i = 0; i < total; i++) {
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
char in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
Expand Down
Expand Up @@ -117,11 +117,12 @@ int spi_master_write(spi_t *obj, int value)
return rx_data & 0xffff;
}

int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
char *rx_buffer, int rx_length, char write_fill) {
int total = (tx_length > rx_length) ? tx_length : rx_length;

for (int i = 0; i < total; i++) {
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
char in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
Expand Down
Expand Up @@ -127,11 +127,12 @@ int spi_master_write(spi_t *obj, int value)
return rx_data & 0xffff;
}

int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length,
char *rx_buffer, int rx_length, char write_fill) {
int total = (tx_length > rx_length) ? tx_length : rx_length;

for (int i = 0; i < total; i++) {
char out = (i < tx_length) ? tx_buffer[i] : 0xff;
char out = (i < tx_length) ? tx_buffer[i] : write_fill;
char in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
Expand Down

0 comments on commit 1f94ede

Please sign in to comment.