Skip to content

Commit 064bd61

Browse files
author
Marcin Tomczyk
committed
[IOTSTOR-990] SPIFBlockDevice doesn't play nice on shared SPI bus. Back to use select and deselect
1 parent 116a8a7 commit 064bd61

File tree

5 files changed

+36
-28
lines changed

5 files changed

+36
-28
lines changed

components/storage/blockdevice/COMPONENT_DATAFLASH/DataFlashBlockDevice.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ int DataFlashBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
330330

331331
uint8_t *external_buffer = static_cast<uint8_t *>(buffer);
332332

333+
_spi.select();
334+
333335
/* send read opcode */
334336
_spi.write(DATAFLASH_OP_READ_LOW_FREQUENCY);
335337

@@ -348,6 +350,8 @@ int DataFlashBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
348350
external_buffer[index] = _spi.write(DATAFLASH_OP_NOP);
349351
}
350352

353+
_spi.deselect();
354+
351355
result = BD_ERROR_OK;
352356
}
353357

@@ -539,13 +543,17 @@ uint16_t DataFlashBlockDevice::_get_register(uint8_t opcode)
539543
_mutex.lock();
540544
DEBUG_PRINTF("_get_register: %" PRIX8 "\r\n", opcode);
541545

546+
_spi.select();
547+
542548
/* write opcode */
543549
_spi.write(opcode);
544550

545551
/* read and store result */
546552
int status = (_spi.write(DATAFLASH_OP_NOP));
547553
status = (status << 8) | (_spi.write(DATAFLASH_OP_NOP));
548554

555+
_spi.deselect()''
556+
549557
_mutex.unlock();
550558
return status;
551559
}
@@ -566,6 +574,8 @@ void DataFlashBlockDevice::_write_command(uint32_t command, const uint8_t *buffe
566574
{
567575
DEBUG_PRINTF("_write_command: %" PRIX32 " %p %" PRIX32 "\r\n", command, buffer, size);
568576

577+
_spi.select();
578+
569579
/* send command (opcode with data or 4 byte command) */
570580
_spi.write((command >> 24) & 0xFF);
571581
_spi.write((command >> 16) & 0xFF);
@@ -578,6 +588,8 @@ void DataFlashBlockDevice::_write_command(uint32_t command, const uint8_t *buffe
578588
_spi.write(buffer[index]);
579589
}
580590
}
591+
592+
_spi.deselect();
581593
}
582594

583595
/**

components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.cpp

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ int SDBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
538538
_spi.write(SPI_STOP_TRAN);
539539
}
540540

541-
_deselect();
541+
_spi.deselect();
542542
unlock();
543543
return status;
544544
}
@@ -585,7 +585,7 @@ int SDBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
585585
buffer += _block_size;
586586
--blockCnt;
587587
}
588-
_deselect();
588+
_spi.deselect();
589589

590590
// Send CMD12(0x00000000) to stop the transmission for multi-block transfer
591591
if (size > _block_size) {
@@ -753,7 +753,7 @@ int SDBlockDevice::_cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAc
753753

754754
// Select card and wait for card to be ready before sending next command
755755
// Note: next command will fail if card is not ready
756-
_select();
756+
_spi.select();
757757

758758
// No need to wait for card to be ready when sending the stop command
759759
if (CMD12_STOP_TRANSMISSION != cmd) {
@@ -789,17 +789,17 @@ int SDBlockDevice::_cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAc
789789

790790
// Process the response R1 : Exit on CRC/Illegal command error/No response
791791
if (R1_NO_RESPONSE == response) {
792-
_deselect();
792+
_spi.deselect();
793793
debug_if(SD_DBG, "No response CMD:%d response: 0x%" PRIx32 "\n", cmd, response);
794794
return SD_BLOCK_DEVICE_ERROR_NO_DEVICE; // No device
795795
}
796796
if (response & R1_COM_CRC_ERROR) {
797-
_deselect();
797+
_spi.deselect();
798798
debug_if(SD_DBG, "CRC error CMD:%d response 0x%" PRIx32 "\n", cmd, response);
799799
return SD_BLOCK_DEVICE_ERROR_CRC; // CRC error
800800
}
801801
if (response & R1_ILLEGAL_COMMAND) {
802-
_deselect();
802+
_spi.deselect();
803803
debug_if(SD_DBG, "Illegal command CMD:%d response 0x%" PRIx32 "\n", cmd, response);
804804
if (CMD8_SEND_IF_COND == cmd) { // Illegal command is for Ver1 or not SD Card
805805
_card_type = CARD_UNKNOWN;
@@ -857,7 +857,7 @@ int SDBlockDevice::_cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAc
857857
return BD_ERROR_OK;
858858
}
859859
// Deselect card
860-
_deselect();
860+
_spi.deselect();
861861
return status;
862862
}
863863

@@ -908,7 +908,7 @@ int SDBlockDevice::_read_bytes(uint8_t *buffer, uint32_t length)
908908
// read until start byte (0xFE)
909909
if (false == _wait_token(SPI_START_BLOCK)) {
910910
debug_if(SD_DBG, "Read timeout\n");
911-
_deselect();
911+
_spi.deselect();
912912
return SD_BLOCK_DEVICE_ERROR_NO_RESPONSE;
913913
}
914914

@@ -930,13 +930,13 @@ int SDBlockDevice::_read_bytes(uint8_t *buffer, uint32_t length)
930930
if (crc_result != crc) {
931931
debug_if(SD_DBG, "_read_bytes: Invalid CRC received 0x%" PRIx16 " result of computation 0x%" PRIx32 "\n",
932932
crc, crc_result);
933-
_deselect();
933+
_spi.deselect();
934934
return SD_BLOCK_DEVICE_ERROR_CRC;
935935
}
936936
}
937937
#endif
938938

939-
_deselect();
939+
_spi.deselect();
940940
return 0;
941941
}
942942

@@ -1129,22 +1129,14 @@ void SDBlockDevice::_spi_wait(uint8_t count)
11291129

11301130
void SDBlockDevice::_spi_init()
11311131
{
1132+
_spi.select();
11321133
// Set to SCK for initialization, and clock card with cs = 1
11331134
_spi.frequency(_init_sck);
11341135
_spi.format(8, 0);
11351136
_spi.set_default_write_value(SPI_FILL_CHAR);
11361137
// Initial 74 cycles required for few cards, before selecting SPI mode
11371138
_spi_wait(10);
1138-
}
1139-
1140-
void SDBlockDevice::_select()
1141-
{
1142-
_spi.write(SPI_FILL_CHAR);
1143-
}
1144-
1145-
void SDBlockDevice::_deselect()
1146-
{
1147-
_spi.write(SPI_FILL_CHAR);
1139+
_spi.deselect();
11481140
}
11491141

11501142
#endif /* DEVICE_SPI */

components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,6 @@ class SDBlockDevice : public mbed::BlockDevice {
273273
uint8_t _write(const uint8_t *buffer, uint8_t token, uint32_t length);
274274
int _freq(void);
275275

276-
/* Chip Select and SPI mode select */
277-
void _select();
278-
void _deselect();
279-
280276
virtual void lock()
281277
{
282278
_mutex.lock();

components/storage/blockdevice/COMPONENT_SPIF/SPIFBlockDevice.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,8 @@ spif_bd_error SPIFBlockDevice::_spi_send_read_command(int read_inst, uint8_t *bu
470470
uint32_t dummy_bytes = _dummy_and_mode_cycles / 8;
471471
int dummy_byte = 0;
472472

473+
_spi.select();
474+
473475
// Write 1 byte Instruction
474476
_spi.write(read_inst);
475477

@@ -488,6 +490,8 @@ spif_bd_error SPIFBlockDevice::_spi_send_read_command(int read_inst, uint8_t *bu
488490
buffer[i] = _spi.write(0);
489491
}
490492

493+
_spi.deselect();
494+
491495
return SPIF_BD_ERROR_OK;
492496
}
493497

@@ -514,6 +518,8 @@ spif_bd_error SPIFBlockDevice::_spi_send_program_command(int prog_inst, const vo
514518
int dummy_byte = 0;
515519
uint8_t *data = (uint8_t *)buffer;
516520

521+
_spi.select();
522+
517523
// Write 1 byte Instruction
518524
_spi.write(prog_inst);
519525

@@ -532,6 +538,8 @@ spif_bd_error SPIFBlockDevice::_spi_send_program_command(int prog_inst, const vo
532538
_spi.write(data[i]);
533539
}
534540

541+
_spi.deselect();
542+
535543
return SPIF_BD_ERROR_OK;
536544
}
537545

@@ -550,6 +558,8 @@ spif_bd_error SPIFBlockDevice::_spi_send_general_command(int instruction, bd_add
550558
uint32_t dummy_bytes = _dummy_and_mode_cycles / 8;
551559
uint8_t dummy_byte = 0x00;
552560

561+
_spi.select();
562+
553563
// Write 1 byte Instruction
554564
_spi.write(instruction);
555565

@@ -569,6 +579,8 @@ spif_bd_error SPIFBlockDevice::_spi_send_general_command(int instruction, bd_add
569579
// Read/Write Data
570580
_spi.write(tx_buffer, (int)tx_length, rx_buffer, (int)rx_length);
571581

582+
_spi.deselect();
583+
572584
return SPIF_BD_ERROR_OK;
573585
}
574586

drivers/source/SPI.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,17 +220,13 @@ void SPI::_acquire()
220220

221221
int SPI::write(int value)
222222
{
223-
select();
224223
int ret = spi_master_write(&_peripheral->spi, value);
225-
deselect();
226224
return ret;
227225
}
228226

229227
int SPI::write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length)
230228
{
231-
select();
232229
int ret = spi_master_block_write(&_peripheral->spi, tx_buffer, tx_length, rx_buffer, rx_length, _write_fill);
233-
deselect();
234230
return ret;
235231
}
236232

0 commit comments

Comments
 (0)