[IOTSTOR-990] SPIFBlockDevice doesn't play nice on shared SPI bus. Fix by use valid constructor SPI

pull/12681/head
Marcin Tomczyk 2020-03-24 01:22:59 -07:00
parent e805b582c0
commit 116a8a75b6
6 changed files with 9 additions and 56 deletions

View File

@ -140,8 +140,7 @@ DataFlashBlockDevice::DataFlashBlockDevice(PinName mosi,
PinName cs,
int freq,
PinName nwp)
: _spi(mosi, miso, sclk),
_cs(cs, 1),
: _spi(mosi, miso, sclk, cs),
_nwp(nwp),
_device_size(0),
_page_size(0),
@ -331,9 +330,6 @@ int DataFlashBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
uint8_t *external_buffer = static_cast<uint8_t *>(buffer);
/* activate device */
_cs = 0;
/* send read opcode */
_spi.write(DATAFLASH_OP_READ_LOW_FREQUENCY);
@ -352,9 +348,6 @@ int DataFlashBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
external_buffer[index] = _spi.write(DATAFLASH_OP_NOP);
}
/* deactivate device */
_cs = 1;
result = BD_ERROR_OK;
}
@ -546,9 +539,6 @@ uint16_t DataFlashBlockDevice::_get_register(uint8_t opcode)
_mutex.lock();
DEBUG_PRINTF("_get_register: %" PRIX8 "\r\n", opcode);
/* activate device */
_cs = 0;
/* write opcode */
_spi.write(opcode);
@ -556,9 +546,6 @@ uint16_t DataFlashBlockDevice::_get_register(uint8_t opcode)
int status = (_spi.write(DATAFLASH_OP_NOP));
status = (status << 8) | (_spi.write(DATAFLASH_OP_NOP));
/* deactivate device */
_cs = 1;
_mutex.unlock();
return status;
}
@ -579,9 +566,6 @@ void DataFlashBlockDevice::_write_command(uint32_t command, const uint8_t *buffe
{
DEBUG_PRINTF("_write_command: %" PRIX32 " %p %" PRIX32 "\r\n", command, buffer, size);
/* activate device */
_cs = 0;
/* send command (opcode with data or 4 byte command) */
_spi.write((command >> 24) & 0xFF);
_spi.write((command >> 16) & 0xFF);
@ -594,9 +578,6 @@ void DataFlashBlockDevice::_write_command(uint32_t command, const uint8_t *buffe
_spi.write(buffer[index]);
}
}
/* deactivate device */
_cs = 1;
}
/**

View File

@ -182,7 +182,6 @@ public:
private:
// Master side hardware
mbed::SPI _spi;
mbed::DigitalOut _cs;
mbed::DigitalOut _nwp;
// Device configuration

View File

@ -252,15 +252,15 @@ const uint32_t SDBlockDevice::_block_size = BLOCK_SIZE_HC;
#if MBED_CONF_SD_CRC_ENABLED
SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on)
: _sectors(0), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0),
: _sectors(0), _spi(mosi, miso, sclk, cs), _is_initialized(0),
_init_ref_count(0), _crc_on(crc_on)
#else
SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on)
: _sectors(0), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0),
: _sectors(0), _spi(mosi, miso, sclk, cs), _is_initialized(0),
_init_ref_count(0)
#endif
{
_cs = 1;
_spi.deselect();
_card_type = SDCARD_NONE;
// 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
#if MBED_CONF_SD_CRC_ENABLED
SDBlockDevice::SDBlockDevice(const spi_pinmap_t &spi_pinmap, PinName cs, uint64_t hz, bool crc_on)
: _sectors(0), _spi(spi_pinmap), _cs(cs), _is_initialized(0),
: _sectors(0), _spi(spi_pinmap, cs), _is_initialized(0),
_init_ref_count(0), _crc_on(crc_on)
#else
SDBlockDevice::SDBlockDevice(const spi_pinmap_t &spi_pinmap, PinName cs, uint64_t hz, bool crc_on)
: _sectors(0), _spi(spi_pinmap), _cs(cs), _is_initialized(0),
: _sectors(0), _spi(spi_pinmap, cs), _is_initialized(0),
_init_ref_count(0)
#endif
{
_cs = 1;
_spi.deselect();
_card_type = SDCARD_NONE;
// Set default to 100kHz for initialisation and 1MHz for data transfer
@ -1129,29 +1129,22 @@ void SDBlockDevice::_spi_wait(uint8_t count)
void SDBlockDevice::_spi_init()
{
_spi.lock();
// Set to SCK for initialization, and clock card with cs = 1
_spi.frequency(_init_sck);
_spi.format(8, 0);
_spi.set_default_write_value(SPI_FILL_CHAR);
// Initial 74 cycles required for few cards, before selecting SPI mode
_cs = 1;
_spi_wait(10);
_spi.unlock();
}
void SDBlockDevice::_select()
{
_spi.lock();
_spi.write(SPI_FILL_CHAR);
_cs = 0;
}
void SDBlockDevice::_deselect()
{
_cs = 1;
_spi.write(SPI_FILL_CHAR);
_spi.unlock();
}
#endif /* DEVICE_SPI */

View File

@ -274,7 +274,6 @@ private:
int _freq(void);
/* Chip Select and SPI mode select */
mbed::DigitalOut _cs;
void _select();
void _deselect();

View File

@ -87,7 +87,7 @@ SingletonPtr<PlatformMutex> SPIFBlockDevice::_mutex;
//***********************
SPIFBlockDevice::SPIFBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName csel, int freq)
:
_spi(mosi, miso, sclk), _cs(csel), _prog_instruction(0), _erase_instruction(0),
_spi(mosi, miso, sclk, csel), _prog_instruction(0), _erase_instruction(0),
_page_size_bytes(0), _init_ref_count(0), _is_initialized(false)
{
_address_size = SPIF_ADDR_SIZE_3_BYTES;
@ -110,7 +110,7 @@ SPIFBlockDevice::SPIFBlockDevice(PinName mosi, PinName miso, PinName sclk, PinNa
tr_error("SPI Set Frequency Failed");
}
_cs = 1;
_spi.deselect();
}
int SPIFBlockDevice::init()
@ -470,9 +470,6 @@ spif_bd_error SPIFBlockDevice::_spi_send_read_command(int read_inst, uint8_t *bu
uint32_t dummy_bytes = _dummy_and_mode_cycles / 8;
int dummy_byte = 0;
// csel must go low for the entire command (Inst, Address and Data)
_cs = 0;
// Write 1 byte Instruction
_spi.write(read_inst);
@ -491,8 +488,6 @@ spif_bd_error SPIFBlockDevice::_spi_send_read_command(int read_inst, uint8_t *bu
buffer[i] = _spi.write(0);
}
// csel back to high
_cs = 1;
return SPIF_BD_ERROR_OK;
}
@ -519,9 +514,6 @@ spif_bd_error SPIFBlockDevice::_spi_send_program_command(int prog_inst, const vo
int dummy_byte = 0;
uint8_t *data = (uint8_t *)buffer;
// csel must go low for the entire command (Inst, Address and Data)
_cs = 0;
// Write 1 byte Instruction
_spi.write(prog_inst);
@ -540,9 +532,6 @@ spif_bd_error SPIFBlockDevice::_spi_send_program_command(int prog_inst, const vo
_spi.write(data[i]);
}
// csel back to high
_cs = 1;
return SPIF_BD_ERROR_OK;
}
@ -561,9 +550,6 @@ spif_bd_error SPIFBlockDevice::_spi_send_general_command(int instruction, bd_add
uint32_t dummy_bytes = _dummy_and_mode_cycles / 8;
uint8_t dummy_byte = 0x00;
// csel must go low for the entire command (Inst, Address and Data)
_cs = 0;
// Write 1 byte Instruction
_spi.write(instruction);
@ -583,9 +569,6 @@ spif_bd_error SPIFBlockDevice::_spi_send_general_command(int instruction, bd_add
// Read/Write Data
_spi.write(tx_buffer, (int)tx_length, rx_buffer, (int)rx_length);
// csel back to high
_cs = 1;
return SPIF_BD_ERROR_OK;
}

View File

@ -266,8 +266,6 @@ private:
private:
// Master side hardware
mbed::SPI _spi;
// Enable CS control (low/high) for SPI driver operations
mbed::DigitalOut _cs;
// Mutex is used to protect Flash device for some SPI Driver commands that must be done sequentially with no other commands in between
// e.g. (1)Set Write Enable, (2)Program, (3)Wait Memory Ready