Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spi: Add SPI block-write to C++ and HAL for performance #4207

Merged
merged 5 commits into from
Jun 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions drivers/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ int SPI::write(int value) {
return ret;
}

int SPI::write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
lock();
aquire();
int ret = spi_master_block_write(&_spi, tx_buffer, tx_length, rx_buffer, rx_length);
unlock();
return ret;
}

void SPI::lock() {
_mutex->lock();
}
Expand Down
18 changes: 17 additions & 1 deletion drivers/SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,25 @@ class SPI {
*
* @returns
* Response from the SPI slave
*/
*/
virtual int write(int value);

/** Write to the SPI Slave and obtain the response
*
* The total number of bytes sent and recieved will be the maximum of
* tx_length and rx_length. The bytes written will be padded with the
* value 0xff.
*
* @param tx_buffer Pointer to the byte-array of data to write to the device
* @param tx_length Number of bytes to write, may be zero
* @param rx_buffer Pointer to the byte-array of data to read from the device
* @param rx_length Number of bytes to read, may be zero
* @returns
* The number of bytes written and read from the device. This is
* maximum of tx_length and rx_length.
*/
virtual int write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length);

/** Acquire exclusive access to this SPI bus
*/
virtual void lock(void);
Expand Down
17 changes: 17 additions & 0 deletions hal/spi_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,23 @@ void spi_frequency(spi_t *obj, int hz);
*/
int spi_master_write(spi_t *obj, int value);

/** Write a block out in master mode and receive a value
*
* The total number of bytes sent and recieved will be the maximum of
* 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
* @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);

/** Check if a value is available to read
*
* @param[in] obj The SPI peripheral to check
Expand Down
14 changes: 14 additions & 0 deletions targets/TARGET_ARM_SSG/TARGET_BEETLE/spi_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,20 @@ 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 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 in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
}
}

return total;
}

uint8_t spi_get_module(spi_t *obj) {
return obj->spi->MID;
}
Expand Down
14 changes: 14 additions & 0 deletions targets/TARGET_ARM_SSG/TARGET_IOTSS/spi_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,20 @@ 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 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 in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
}
}

return total;
}

int spi_slave_receive(spi_t *obj) {
return (ssp_readable(obj) && !ssp_busy(obj)) ? (1) : (0);
}
Expand Down
14 changes: 14 additions & 0 deletions targets/TARGET_ARM_SSG/TARGET_MPS2/spi_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,20 @@ 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 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 in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
}
}

return total;
}

int spi_slave_receive(spi_t *obj) {
return (ssp_readable(obj) && !ssp_busy(obj)) ? (1) : (0);
}
Expand Down
14 changes: 14 additions & 0 deletions targets/TARGET_Atmel/TARGET_SAM_CortexM0P/spi_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,20 @@ 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 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 in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
}
}

return total;
}

/** Check if a value is available to read
*
* @param[in] obj The SPI peripheral to check
Expand Down
14 changes: 14 additions & 0 deletions targets/TARGET_Atmel/TARGET_SAM_CortexM4/spi_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,20 @@ 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 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 in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
}
}

return total;
}

/** Check if a value is available to read
*
* @param[in] obj The SPI peripheral to check
Expand Down
14 changes: 14 additions & 0 deletions targets/TARGET_Freescale/TARGET_K20XX/spi_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,20 @@ 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 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 in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
}
}

return total;
}

int spi_slave_receive(spi_t *obj) {
return spi_readable(obj);
}
Expand Down
14 changes: 14 additions & 0 deletions targets/TARGET_Freescale/TARGET_KLXX/TARGET_KL05Z/spi_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,20 @@ 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 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 in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
}
}

return total;
}

int spi_slave_receive(spi_t *obj) {
return spi_readable(obj);
}
Expand Down
14 changes: 14 additions & 0 deletions targets/TARGET_Freescale/TARGET_KLXX/TARGET_KL25Z/spi_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ 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 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 in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
}
}

return total;
}

int spi_slave_receive(spi_t *obj) {
return spi_readable(obj);
}
Expand Down
14 changes: 14 additions & 0 deletions targets/TARGET_Freescale/TARGET_KLXX/TARGET_KL26Z/spi_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,20 @@ 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 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 in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
}
}

return total;
}

int spi_slave_receive(spi_t *obj) {
return spi_readable(obj);
}
Expand Down
14 changes: 14 additions & 0 deletions targets/TARGET_Freescale/TARGET_KLXX/TARGET_KL46Z/spi_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,20 @@ 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 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 in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
}
}

return total;
}

int spi_slave_receive(spi_t *obj) {
return spi_readable(obj);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,20 @@ 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 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 in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
}
}

return total;
}

int spi_slave_receive(spi_t *obj)
{
return spi_readable(obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,20 @@ 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 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 in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
}
}

return total;
}

int spi_slave_receive(spi_t *obj)
{
return spi_readable(obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ 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 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 in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
}
}

return total;
}

int spi_slave_receive(spi_t *obj)
{
return spi_readable(obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ 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 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 in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
}
}

return total;
}

int spi_slave_receive(spi_t *obj)
{
return spi_readable(obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,20 @@ 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 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 in = spi_master_write(obj, out);
if (i < rx_length) {
rx_buffer[i] = in;
}
}

return total;
}

int spi_slave_receive(spi_t *obj)
{
return spi_readable(obj);
Expand Down
Loading