Skip to content

Commit 116a8a7

Browse files
author
Marcin Tomczyk
committed
[IOTSTOR-990] SPIFBlockDevice doesn't play nice on shared SPI bus. Fix by use valid constructor SPI
1 parent e805b58 commit 116a8a7

File tree

6 files changed

+9
-56
lines changed

6 files changed

+9
-56
lines changed

components/storage/blockdevice/COMPONENT_DATAFLASH/DataFlashBlockDevice.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ DataFlashBlockDevice::DataFlashBlockDevice(PinName mosi,
140140
PinName cs,
141141
int freq,
142142
PinName nwp)
143-
: _spi(mosi, miso, sclk),
144-
_cs(cs, 1),
143+
: _spi(mosi, miso, sclk, cs),
145144
_nwp(nwp),
146145
_device_size(0),
147146
_page_size(0),
@@ -331,9 +330,6 @@ int DataFlashBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
331330

332331
uint8_t *external_buffer = static_cast<uint8_t *>(buffer);
333332

334-
/* activate device */
335-
_cs = 0;
336-
337333
/* send read opcode */
338334
_spi.write(DATAFLASH_OP_READ_LOW_FREQUENCY);
339335

@@ -352,9 +348,6 @@ int DataFlashBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
352348
external_buffer[index] = _spi.write(DATAFLASH_OP_NOP);
353349
}
354350

355-
/* deactivate device */
356-
_cs = 1;
357-
358351
result = BD_ERROR_OK;
359352
}
360353

@@ -546,19 +539,13 @@ uint16_t DataFlashBlockDevice::_get_register(uint8_t opcode)
546539
_mutex.lock();
547540
DEBUG_PRINTF("_get_register: %" PRIX8 "\r\n", opcode);
548541

549-
/* activate device */
550-
_cs = 0;
551-
552542
/* write opcode */
553543
_spi.write(opcode);
554544

555545
/* read and store result */
556546
int status = (_spi.write(DATAFLASH_OP_NOP));
557547
status = (status << 8) | (_spi.write(DATAFLASH_OP_NOP));
558548

559-
/* deactivate device */
560-
_cs = 1;
561-
562549
_mutex.unlock();
563550
return status;
564551
}
@@ -579,9 +566,6 @@ void DataFlashBlockDevice::_write_command(uint32_t command, const uint8_t *buffe
579566
{
580567
DEBUG_PRINTF("_write_command: %" PRIX32 " %p %" PRIX32 "\r\n", command, buffer, size);
581568

582-
/* activate device */
583-
_cs = 0;
584-
585569
/* send command (opcode with data or 4 byte command) */
586570
_spi.write((command >> 24) & 0xFF);
587571
_spi.write((command >> 16) & 0xFF);
@@ -594,9 +578,6 @@ void DataFlashBlockDevice::_write_command(uint32_t command, const uint8_t *buffe
594578
_spi.write(buffer[index]);
595579
}
596580
}
597-
598-
/* deactivate device */
599-
_cs = 1;
600581
}
601582

602583
/**

components/storage/blockdevice/COMPONENT_DATAFLASH/DataFlashBlockDevice.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ class DataFlashBlockDevice : public mbed::BlockDevice {
182182
private:
183183
// Master side hardware
184184
mbed::SPI _spi;
185-
mbed::DigitalOut _cs;
186185
mbed::DigitalOut _nwp;
187186

188187
// Device configuration

components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,15 @@ const uint32_t SDBlockDevice::_block_size = BLOCK_SIZE_HC;
252252

253253
#if MBED_CONF_SD_CRC_ENABLED
254254
SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on)
255-
: _sectors(0), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0),
255+
: _sectors(0), _spi(mosi, miso, sclk, cs), _is_initialized(0),
256256
_init_ref_count(0), _crc_on(crc_on)
257257
#else
258258
SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on)
259-
: _sectors(0), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0),
259+
: _sectors(0), _spi(mosi, miso, sclk, cs), _is_initialized(0),
260260
_init_ref_count(0)
261261
#endif
262262
{
263-
_cs = 1;
263+
_spi.deselect();
264264
_card_type = SDCARD_NONE;
265265

266266
// Set default to 100kHz for initialisation and 1MHz for data transfer
@@ -274,15 +274,15 @@ SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName c
274274

275275
#if MBED_CONF_SD_CRC_ENABLED
276276
SDBlockDevice::SDBlockDevice(const spi_pinmap_t &spi_pinmap, PinName cs, uint64_t hz, bool crc_on)
277-
: _sectors(0), _spi(spi_pinmap), _cs(cs), _is_initialized(0),
277+
: _sectors(0), _spi(spi_pinmap, cs), _is_initialized(0),
278278
_init_ref_count(0), _crc_on(crc_on)
279279
#else
280280
SDBlockDevice::SDBlockDevice(const spi_pinmap_t &spi_pinmap, PinName cs, uint64_t hz, bool crc_on)
281-
: _sectors(0), _spi(spi_pinmap), _cs(cs), _is_initialized(0),
281+
: _sectors(0), _spi(spi_pinmap, cs), _is_initialized(0),
282282
_init_ref_count(0)
283283
#endif
284284
{
285-
_cs = 1;
285+
_spi.deselect();
286286
_card_type = SDCARD_NONE;
287287

288288
// Set default to 100kHz for initialisation and 1MHz for data transfer
@@ -1129,29 +1129,22 @@ void SDBlockDevice::_spi_wait(uint8_t count)
11291129

11301130
void SDBlockDevice::_spi_init()
11311131
{
1132-
_spi.lock();
11331132
// Set to SCK for initialization, and clock card with cs = 1
11341133
_spi.frequency(_init_sck);
11351134
_spi.format(8, 0);
11361135
_spi.set_default_write_value(SPI_FILL_CHAR);
11371136
// Initial 74 cycles required for few cards, before selecting SPI mode
1138-
_cs = 1;
11391137
_spi_wait(10);
1140-
_spi.unlock();
11411138
}
11421139

11431140
void SDBlockDevice::_select()
11441141
{
1145-
_spi.lock();
11461142
_spi.write(SPI_FILL_CHAR);
1147-
_cs = 0;
11481143
}
11491144

11501145
void SDBlockDevice::_deselect()
11511146
{
1152-
_cs = 1;
11531147
_spi.write(SPI_FILL_CHAR);
1154-
_spi.unlock();
11551148
}
11561149

11571150
#endif /* DEVICE_SPI */

components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,6 @@ class SDBlockDevice : public mbed::BlockDevice {
274274
int _freq(void);
275275

276276
/* Chip Select and SPI mode select */
277-
mbed::DigitalOut _cs;
278277
void _select();
279278
void _deselect();
280279

components/storage/blockdevice/COMPONENT_SPIF/SPIFBlockDevice.cpp

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ SingletonPtr<PlatformMutex> SPIFBlockDevice::_mutex;
8787
//***********************
8888
SPIFBlockDevice::SPIFBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName csel, int freq)
8989
:
90-
_spi(mosi, miso, sclk), _cs(csel), _prog_instruction(0), _erase_instruction(0),
90+
_spi(mosi, miso, sclk, csel), _prog_instruction(0), _erase_instruction(0),
9191
_page_size_bytes(0), _init_ref_count(0), _is_initialized(false)
9292
{
9393
_address_size = SPIF_ADDR_SIZE_3_BYTES;
@@ -110,7 +110,7 @@ SPIFBlockDevice::SPIFBlockDevice(PinName mosi, PinName miso, PinName sclk, PinNa
110110
tr_error("SPI Set Frequency Failed");
111111
}
112112

113-
_cs = 1;
113+
_spi.deselect();
114114
}
115115

116116
int SPIFBlockDevice::init()
@@ -470,9 +470,6 @@ 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-
// csel must go low for the entire command (Inst, Address and Data)
474-
_cs = 0;
475-
476473
// Write 1 byte Instruction
477474
_spi.write(read_inst);
478475

@@ -491,8 +488,6 @@ spif_bd_error SPIFBlockDevice::_spi_send_read_command(int read_inst, uint8_t *bu
491488
buffer[i] = _spi.write(0);
492489
}
493490

494-
// csel back to high
495-
_cs = 1;
496491
return SPIF_BD_ERROR_OK;
497492
}
498493

@@ -519,9 +514,6 @@ spif_bd_error SPIFBlockDevice::_spi_send_program_command(int prog_inst, const vo
519514
int dummy_byte = 0;
520515
uint8_t *data = (uint8_t *)buffer;
521516

522-
// csel must go low for the entire command (Inst, Address and Data)
523-
_cs = 0;
524-
525517
// Write 1 byte Instruction
526518
_spi.write(prog_inst);
527519

@@ -540,9 +532,6 @@ spif_bd_error SPIFBlockDevice::_spi_send_program_command(int prog_inst, const vo
540532
_spi.write(data[i]);
541533
}
542534

543-
// csel back to high
544-
_cs = 1;
545-
546535
return SPIF_BD_ERROR_OK;
547536
}
548537

@@ -561,9 +550,6 @@ spif_bd_error SPIFBlockDevice::_spi_send_general_command(int instruction, bd_add
561550
uint32_t dummy_bytes = _dummy_and_mode_cycles / 8;
562551
uint8_t dummy_byte = 0x00;
563552

564-
// csel must go low for the entire command (Inst, Address and Data)
565-
_cs = 0;
566-
567553
// Write 1 byte Instruction
568554
_spi.write(instruction);
569555

@@ -583,9 +569,6 @@ spif_bd_error SPIFBlockDevice::_spi_send_general_command(int instruction, bd_add
583569
// Read/Write Data
584570
_spi.write(tx_buffer, (int)tx_length, rx_buffer, (int)rx_length);
585571

586-
// csel back to high
587-
_cs = 1;
588-
589572
return SPIF_BD_ERROR_OK;
590573
}
591574

components/storage/blockdevice/COMPONENT_SPIF/SPIFBlockDevice.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,6 @@ class SPIFBlockDevice : public mbed::BlockDevice {
266266
private:
267267
// Master side hardware
268268
mbed::SPI _spi;
269-
// Enable CS control (low/high) for SPI driver operations
270-
mbed::DigitalOut _cs;
271269

272270
// Mutex is used to protect Flash device for some SPI Driver commands that must be done sequentially with no other commands in between
273271
// e.g. (1)Set Write Enable, (2)Program, (3)Wait Memory Ready

0 commit comments

Comments
 (0)