mirror of https://github.com/ARMmbed/mbed-os.git
Allow user to set dummy tranfer byte for block read
parent
aae62bd990
commit
fd43405ffe
|
@ -32,7 +32,8 @@ SPI::SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel) :
|
||||||
#endif
|
#endif
|
||||||
_bits(8),
|
_bits(8),
|
||||||
_mode(0),
|
_mode(0),
|
||||||
_hz(1000000) {
|
_hz(1000000),
|
||||||
|
_dummy(0x00) {
|
||||||
// No lock needed in the constructor
|
// No lock needed in the constructor
|
||||||
|
|
||||||
spi_init(&_spi, mosi, miso, sclk, ssel);
|
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) {
|
int SPI::write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
|
||||||
lock();
|
lock();
|
||||||
_acquire();
|
_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();
|
unlock();
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -115,6 +116,12 @@ void SPI::unlock() {
|
||||||
_mutex->unlock();
|
_mutex->unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SPI::dummy(char data) {
|
||||||
|
lock();
|
||||||
|
_dummy = data;
|
||||||
|
unlock();
|
||||||
|
}
|
||||||
|
|
||||||
#if DEVICE_SPI_ASYNCH
|
#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)
|
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)
|
||||||
|
|
|
@ -143,6 +143,14 @@ public:
|
||||||
*/
|
*/
|
||||||
virtual void unlock(void);
|
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
|
#if DEVICE_SPI_ASYNCH
|
||||||
|
|
||||||
/** Start non-blocking SPI transfer using 8bit buffers.
|
/** Start non-blocking SPI transfer using 8bit buffers.
|
||||||
|
@ -271,6 +279,7 @@ protected:
|
||||||
int _bits;
|
int _bits;
|
||||||
int _mode;
|
int _mode;
|
||||||
int _hz;
|
int _hz;
|
||||||
|
char _dummy;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/* Private acquire function without locking/unlocking
|
/* Private acquire function without locking/unlocking
|
||||||
|
|
|
@ -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] 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_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] rx_length Number of bytes to read, may be zero
|
||||||
|
* @param[in] dummy Dummy data transmitted while performing read
|
||||||
* @returns
|
* @returns
|
||||||
* The number of bytes written and read from the device. This is
|
* The number of bytes written and read from the device. This is
|
||||||
* maximum of tx_length and rx_length.
|
* 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
|
/** Check if a value is available to read
|
||||||
*
|
*
|
||||||
|
|
|
@ -576,13 +576,13 @@ void DSPI_MasterTransferCreateHandle(SPI_Type *base,
|
||||||
handle->userData = userData;
|
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);
|
assert(transfer);
|
||||||
|
|
||||||
uint16_t wordToSend = 0;
|
uint16_t wordToSend = 0;
|
||||||
uint16_t wordReceived = 0;
|
uint16_t wordReceived = 0;
|
||||||
uint8_t dummyData = DSPI_DUMMY_DATA;
|
uint8_t dummyData = dummy;
|
||||||
uint8_t bitsPerFrame;
|
uint8_t bitsPerFrame;
|
||||||
|
|
||||||
uint32_t command;
|
uint32_t command;
|
||||||
|
|
|
@ -1058,7 +1058,7 @@ void DSPI_MasterTransferCreateHandle(SPI_Type *base,
|
||||||
* @param transfer Pointer to the dspi_transfer_t structure.
|
* @param transfer Pointer to the dspi_transfer_t structure.
|
||||||
* @return status of status_t.
|
* @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.
|
* @brief DSPI master transfer data using interrupts.
|
||||||
|
|
|
@ -127,7 +127,7 @@ int spi_master_write(spi_t *obj, int value)
|
||||||
return rx_data & 0xffff;
|
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;
|
int total = (tx_length > rx_length) ? tx_length : rx_length;
|
||||||
|
|
||||||
DSPI_MasterTransferBlocking(spi_address[obj->spi.instance], &(dspi_transfer_t){
|
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,
|
.rxData = (uint8_t *)rx_buffer,
|
||||||
.dataSize = total,
|
.dataSize = total,
|
||||||
.configFlags = kDSPI_MasterCtar0 | kDSPI_MasterPcs0 | kDSPI_MasterPcsContinuous,
|
.configFlags = kDSPI_MasterCtar0 | kDSPI_MasterPcs0 | kDSPI_MasterPcsContinuous,
|
||||||
});
|
}, dummy);
|
||||||
|
|
||||||
DSPI_ClearStatusFlags(spi_address[obj->spi.instance], kDSPI_RxFifoDrainRequestFlag | kDSPI_EndOfQueueFlag);
|
DSPI_ClearStatusFlags(spi_address[obj->spi.instance], kDSPI_RxFifoDrainRequestFlag | kDSPI_EndOfQueueFlag);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue