Skip to content

Commit

Permalink
Added block-level spi writes
Browse files Browse the repository at this point in the history
/** Write to the SPI Slave and obtain the response
 *
 *  The total number of bytes sent and recieved will be the maximum of
 *  outlength and inlength. The bytes written will be padded with the
 *  value 0xff.
 *
 *  @param outdata Pointer to the byte-array of data to write to the device
 *  @param outlength Number of bytes to write
 *  @param indata Pointer to the byte-array of data to read from the device
 *  @param inlength Number of bytes to read from the device
 *  @returns
 *      The number of bytes written and read from the device. This is
 *      maximum of outlength and inlength.
 */
virtual int write(const char *outdata, int outlength,
        char *indata, int inlength);
  • Loading branch information
geky committed Apr 24, 2017
1 parent cf87e7d commit e7db61e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
17 changes: 17 additions & 0 deletions drivers/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ int SPI::write(int value) {
return ret;
}

int SPI::write(const char *outdata, int outlength, char *indata, int inlength) {
int total = (outlength > inlength) ? outlength : inlength;

lock();
aquire();
for (int i = 0; i < total; i++) {
char out = (i < outlength) ? outdata[i] : 0xff;
char in = spi_master_write(&_spi, out);
if (i < inlength) {
indata[i] = in;
}
}
unlock();

return total;
}

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
* outlength and inlength. The bytes written will be padded with the
* value 0xff.
*
* @param outdata Pointer to the byte-array of data to write to the device
* @param outlength Number of bytes to write
* @param indata Pointer to the byte-array of data to read from the device
* @param inlength Number of bytes to read from the device
* @returns
* The number of bytes written and read from the device. This is
* maximum of outlength and inlength.
*/
virtual int write(const char *outdata, int outlength, char *indata, int inlength);

/** Acquire exclusive access to this SPI bus
*/
virtual void lock(void);
Expand Down

0 comments on commit e7db61e

Please sign in to comment.