Skip to content

Commit fd43405

Browse files
author
Deepika
committed
Allow user to set dummy tranfer byte for block read
1 parent aae62bd commit fd43405

File tree

6 files changed

+25
-8
lines changed

6 files changed

+25
-8
lines changed

drivers/SPI.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ SPI::SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel) :
3232
#endif
3333
_bits(8),
3434
_mode(0),
35-
_hz(1000000) {
35+
_hz(1000000),
36+
_dummy(0x00) {
3637
// No lock needed in the constructor
3738

3839
spi_init(&_spi, mosi, miso, sclk, ssel);
@@ -102,7 +103,7 @@ int SPI::write(int value) {
102103
int SPI::write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
103104
lock();
104105
_acquire();
105-
int ret = spi_master_block_write(&_spi, tx_buffer, tx_length, rx_buffer, rx_length);
106+
int ret = spi_master_block_write(&_spi, tx_buffer, tx_length, rx_buffer, rx_length, _dummy);
106107
unlock();
107108
return ret;
108109
}
@@ -115,6 +116,12 @@ void SPI::unlock() {
115116
_mutex->unlock();
116117
}
117118

119+
void SPI::dummy(char data) {
120+
lock();
121+
_dummy = data;
122+
unlock();
123+
}
124+
118125
#if DEVICE_SPI_ASYNCH
119126

120127
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)

drivers/SPI.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ class SPI : private NonCopyable<SPI> {
143143
*/
144144
virtual void unlock(void);
145145

146+
/** SPI block read dummy data
147+
* SPI requires master to send dummy data, in order to perform read operation.
148+
* Dummy bytes can be different for devices. Example SD Card require 0xFF.
149+
*
150+
* @param dummy Dummy character to be transmitted while read operation
151+
*/
152+
void dummy(char data);
153+
146154
#if DEVICE_SPI_ASYNCH
147155

148156
/** Start non-blocking SPI transfer using 8bit buffers.
@@ -271,6 +279,7 @@ class SPI : private NonCopyable<SPI> {
271279
int _bits;
272280
int _mode;
273281
int _hz;
282+
char _dummy;
274283

275284
private:
276285
/* Private acquire function without locking/unlocking

hal/spi_api.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,12 @@ int spi_master_write(spi_t *obj, int value);
127127
* @param[in] tx_length Number of bytes to write, may be zero
128128
* @param[in] rx_buffer Pointer to the byte-array of data to read from the device
129129
* @param[in] rx_length Number of bytes to read, may be zero
130+
* @param[in] dummy Dummy data transmitted while performing read
130131
* @returns
131132
* The number of bytes written and read from the device. This is
132133
* maximum of tx_length and rx_length.
133134
*/
134-
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length);
135+
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, char dummy);
135136

136137
/** Check if a value is available to read
137138
*

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/drivers/fsl_dspi.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,13 +576,13 @@ void DSPI_MasterTransferCreateHandle(SPI_Type *base,
576576
handle->userData = userData;
577577
}
578578

579-
status_t DSPI_MasterTransferBlocking(SPI_Type *base, dspi_transfer_t *transfer)
579+
status_t DSPI_MasterTransferBlocking(SPI_Type *base, dspi_transfer_t *transfer, char dummy)
580580
{
581581
assert(transfer);
582582

583583
uint16_t wordToSend = 0;
584584
uint16_t wordReceived = 0;
585-
uint8_t dummyData = DSPI_DUMMY_DATA;
585+
uint8_t dummyData = dummy;
586586
uint8_t bitsPerFrame;
587587

588588
uint32_t command;

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/drivers/fsl_dspi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ void DSPI_MasterTransferCreateHandle(SPI_Type *base,
10581058
* @param transfer Pointer to the dspi_transfer_t structure.
10591059
* @return status of status_t.
10601060
*/
1061-
status_t DSPI_MasterTransferBlocking(SPI_Type *base, dspi_transfer_t *transfer);
1061+
status_t DSPI_MasterTransferBlocking(SPI_Type *base, dspi_transfer_t *transfer, char dummy);
10621062

10631063
/*!
10641064
* @brief DSPI master transfer data using interrupts.

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/spi_api.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,15 @@ int spi_master_write(spi_t *obj, int value)
127127
return rx_data & 0xffff;
128128
}
129129

130-
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) {
130+
int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, char dummy) {
131131
int total = (tx_length > rx_length) ? tx_length : rx_length;
132132

133133
DSPI_MasterTransferBlocking(spi_address[obj->spi.instance], &(dspi_transfer_t){
134134
.txData = (uint8_t *)tx_buffer,
135135
.rxData = (uint8_t *)rx_buffer,
136136
.dataSize = total,
137137
.configFlags = kDSPI_MasterCtar0 | kDSPI_MasterPcs0 | kDSPI_MasterPcsContinuous,
138-
});
138+
}, dummy);
139139

140140
DSPI_ClearStatusFlags(spi_address[obj->spi.instance], kDSPI_RxFifoDrainRequestFlag | kDSPI_EndOfQueueFlag);
141141

0 commit comments

Comments
 (0)