Skip to content

Commit 605f631

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 605f631

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,22 +1129,26 @@ 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);
1139+
_spi.deselect();
11381140
}
11391141

11401142
void SDBlockDevice::_select()
11411143
{
1144+
_spi.select();
11421145
_spi.write(SPI_FILL_CHAR);
11431146
}
11441147

11451148
void SDBlockDevice::_deselect()
11461149
{
11471150
_spi.write(SPI_FILL_CHAR);
1151+
_spi.deselect();
11481152
}
11491153

11501154
#endif /* DEVICE_SPI */

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)