Allow user to set dummy tranfer byte for block read

pull/4744/head
Deepika 2017-07-11 15:46:32 -05:00
parent aae62bd990
commit fd43405ffe
6 changed files with 25 additions and 8 deletions

View File

@ -32,7 +32,8 @@ SPI::SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel) :
#endif
_bits(8),
_mode(0),
_hz(1000000) {
_hz(1000000),
_dummy(0x00) {
// No lock needed in the constructor
spi_init(&_spi, mosi, miso, sclk, ssel);
@ -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, _dummy);
unlock();
return ret;
}
@ -115,6 +116,12 @@ void SPI::unlock() {
_mutex->unlock();
}
void SPI::dummy(char data) {
lock();
_dummy = 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)

View File

@ -143,6 +143,14 @@ public:
*/
virtual void unlock(void);
/** SPI block read dummy data
* SPI requires master to send dummy data, in order to perform read operation.
* Dummy bytes can be different for devices. Example SD Card require 0xFF.
*
* @param dummy Dummy character to be transmitted while read operation
*/
void dummy(char data);
#if DEVICE_SPI_ASYNCH
/** Start non-blocking SPI transfer using 8bit buffers.
@ -271,6 +279,7 @@ protected:
int _bits;
int _mode;
int _hz;
char _dummy;
private:
/* Private acquire function without locking/unlocking

View File

@ -127,11 +127,12 @@ int spi_master_write(spi_t *obj, int value);
* @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] dummy Dummy data transmitted while performing 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 dummy);
/** Check if a value is available to read
*

View File

@ -576,13 +576,13 @@ void DSPI_MasterTransferCreateHandle(SPI_Type *base,
handle->userData = userData;
}
status_t DSPI_MasterTransferBlocking(SPI_Type *base, dspi_transfer_t *transfer)
status_t DSPI_MasterTransferBlocking(SPI_Type *base, dspi_transfer_t *transfer, char dummy)
{
assert(transfer);
uint16_t wordToSend = 0;
uint16_t wordReceived = 0;
uint8_t dummyData = DSPI_DUMMY_DATA;
uint8_t dummyData = dummy;
uint8_t bitsPerFrame;
uint32_t command;

View File

@ -1058,7 +1058,7 @@ void DSPI_MasterTransferCreateHandle(SPI_Type *base,
* @param transfer Pointer to the dspi_transfer_t structure.
* @return status of status_t.
*/
status_t DSPI_MasterTransferBlocking(SPI_Type *base, dspi_transfer_t *transfer);
status_t DSPI_MasterTransferBlocking(SPI_Type *base, dspi_transfer_t *transfer, char dummy);
/*!
* @brief DSPI master transfer data using interrupts.

View File

@ -127,7 +127,7 @@ 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 dummy) {
int total = (tx_length > rx_length) ? tx_length : rx_length;
DSPI_MasterTransferBlocking(spi_address[obj->spi.instance], &(dspi_transfer_t){
@ -135,7 +135,7 @@ int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, cha
.rxData = (uint8_t *)rx_buffer,
.dataSize = total,
.configFlags = kDSPI_MasterCtar0 | kDSPI_MasterPcs0 | kDSPI_MasterPcsContinuous,
});
}, dummy);
DSPI_ClearStatusFlags(spi_address[obj->spi.instance], kDSPI_RxFifoDrainRequestFlag | kDSPI_EndOfQueueFlag);