Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 40 additions & 9 deletions src/Adafruit_SPIFlashBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ static SPIFlash_Device_t const *findDevice(SPIFlash_Device_t const *device_list,
return NULL;
}

void Adafruit_SPIFlashBase::write_status_register(uint8_t *status) {
if (_flash_dev->write_status_register_split) {
_trans->writeCommand(SFLASH_CMD_WRITE_STATUS2, status + 1, 1);
} else if (_flash_dev->single_status_byte) {
_trans->writeCommand(SFLASH_CMD_WRITE_STATUS, status + 1, 1);
} else {
_trans->writeCommand(SFLASH_CMD_WRITE_STATUS, status, 2);
}
}

bool Adafruit_SPIFlashBase::begin(SPIFlash_Device_t const *flash_devs,
size_t count) {
if (_trans == NULL) {
Expand Down Expand Up @@ -219,19 +229,40 @@ bool Adafruit_SPIFlashBase::begin(SPIFlash_Device_t const *flash_devs,
writeEnable();

uint8_t full_status[2] = {0x00, _flash_dev->quad_enable_bit_mask};

if (_flash_dev->write_status_register_split) {
_trans->writeCommand(SFLASH_CMD_WRITE_STATUS2, full_status + 1, 1);
} else if (_flash_dev->single_status_byte) {
_trans->writeCommand(SFLASH_CMD_WRITE_STATUS, full_status + 1, 1);
} else {
_trans->writeCommand(SFLASH_CMD_WRITE_STATUS, full_status, 2);
}
write_status_register(full_status);
}
} else {
/*
* Most of QSPI flash memory ICs have non-volatile QE bit in a status
* register. If it was set once - we need to apply a separate procedure to
* clear it off when the device is connected to a non-QSPI capable bus or it
* has _flash_dev->supports_qspi setting in 'false' state
*/
// Disable Quad Mode if not available
if (!_trans->supportQuadMode() || !_flash_dev->supports_qspi) {
// Verify that QSPI mode is not enabled.
uint8_t status =
_flash_dev->single_status_byte ? readStatus() : readStatus2();

// Check the quad enable bit.
if ((status & _flash_dev->quad_enable_bit_mask) != 0) {
writeEnable();

uint8_t full_status[2] = {0x00, 0x00};
write_status_register(full_status);
}
}

// Single mode, use fast read if supported
if (_flash_dev->supports_fast_read) {
_trans->setReadCommand(SFLASH_CMD_FAST_READ);
if (_trans->supportQuadMode() && !_flash_dev->supports_qspi) {
/* Re-init QSPI with READOC_FASTREAD and WRITEOC_PP */
_trans->end();
_trans->setReadCommand(SFLASH_CMD_FAST_READ);
_trans->begin();
} else {
_trans->setReadCommand(SFLASH_CMD_FAST_READ);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/Adafruit_SPIFlashBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class Adafruit_SPIFlashBase {
int _ind_pin;
bool _ind_active;

void write_status_register(uint8_t *);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the prototype should be

void write_status_register(uint8_t const full_status[2]);

Copy link
Contributor Author

@lyusupov lyusupov Dec 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in c38a7ca

This PR is intentionally designed to create of no or least harm possible:

  • it uses current API only
    • setReadCommand() on an intermediate level and
    • supports_qspi=false on a 'user level' (in flash device descriptor)
  • default behavior remains the same as before - QSPI will operate at quad data rate unless user have not supplied supports_qspi=false argument intentionally.

It is out of the scopes for this PR to alter current API of the library.
I suppose that any changes in the API are only necessary when there are high demands of users to use extended features.
I doubt that this 1-bit access feature is currently so important that we need to change internal and external API of the library.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for confirmation, you don't have to alter the current API. IMHO, the flash device table is flash description, if the flash itself support quad mode, we should just leave it as it is. Instead We could update the actual transport layer. E.g 1bit mode or 2bit mode can be activated when declaring an object with IO1-IO3 = -1

https://github.com/adafruit/Adafruit_SPIFlash/blob/master/src/qspi/Adafruit_FlashTransport_QSPI_NRF.cpp#L35

the rest of the code can still apply, and it is transparent to existing user. That also allow using existing flash dev database and the n-bit mode is dictated by the user sketch only. Let me know if this makes sense to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will be happy to see (and use) your own PR that will supersede this one

Copy link
Member

@hathach hathach Dec 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to, but I don't have motivation (and time) unless adafruit make board that need this. Meanwhile, we can leave this PR pending as reference.


void _indicator_on(void) {
if (_ind_pin >= 0) {
digitalWrite(_ind_pin, _ind_active ? HIGH : LOW);
Expand Down
10 changes: 9 additions & 1 deletion src/qspi/Adafruit_FlashTransport_QSPI_NRF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,19 @@ void Adafruit_FlashTransport_QSPI::begin(void) {
},
.irq_priority = 7};

if (_cmd_read != SFLASH_CMD_QUAD_READ) {
qspi_cfg.prot_if.readoc = NRF_QSPI_READOC_FASTREAD; // 0x0B read command
qspi_cfg.prot_if.writeoc = NRF_QSPI_WRITEOC_PP; // 0x02 write command
}

// No callback for blocking API
nrfx_qspi_init(&qspi_cfg, NULL, NULL);
}

void Adafruit_FlashTransport_QSPI::end(void) { nrfx_qspi_uninit(); }
void Adafruit_FlashTransport_QSPI::end(void) {
nrfx_qspi_uninit();
_cmd_read = SFLASH_CMD_QUAD_READ;
}

void Adafruit_FlashTransport_QSPI::setClockSpeed(uint32_t clock_hz,
uint32_t read_hz) {
Expand Down
32 changes: 21 additions & 11 deletions src/qspi/Adafruit_FlashTransport_QSPI_SAMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,19 @@ bool Adafruit_FlashTransport_QSPI::eraseCommand(uint8_t command,

bool Adafruit_FlashTransport_QSPI::readMemory(uint32_t addr, uint8_t *data,
uint32_t len) {
// Command 0x6B 1 line address, 4 line Data
// Quad output mode, read memory type
uint32_t iframe = QSPI_INSTRFRAME_WIDTH_QUAD_OUTPUT |
QSPI_INSTRFRAME_ADDRLEN_24BITS |
uint32_t width = (_cmd_read == SFLASH_CMD_QUAD_READ)
? QSPI_INSTRFRAME_WIDTH_QUAD_OUTPUT
: QSPI_INSTRFRAME_WIDTH_SINGLE_BIT_SPI;

// Command 0x6B (or 0x0B) 1 line address, 4 (or 1) line Data
// Quad (or single) output mode, read memory type
uint32_t iframe = width | QSPI_INSTRFRAME_ADDRLEN_24BITS |
QSPI_INSTRFRAME_TFRTYPE_READMEMORY |
QSPI_INSTRFRAME_INSTREN | QSPI_INSTRFRAME_ADDREN |
QSPI_INSTRFRAME_DATAEN | QSPI_INSTRFRAME_DUMMYLEN(8);

samd_peripherals_disable_and_clear_cache();
_run_instruction(SFLASH_CMD_QUAD_READ, iframe, addr, data, len);
_run_instruction(_cmd_read, iframe, addr, data, len);
samd_peripherals_enable_cache();

return true;
Expand All @@ -159,14 +162,21 @@ bool Adafruit_FlashTransport_QSPI::readMemory(uint32_t addr, uint8_t *data,
bool Adafruit_FlashTransport_QSPI::writeMemory(uint32_t addr,
uint8_t const *data,
uint32_t len) {
uint32_t iframe =
QSPI_INSTRFRAME_WIDTH_QUAD_OUTPUT | QSPI_INSTRFRAME_ADDRLEN_24BITS |
QSPI_INSTRFRAME_TFRTYPE_WRITEMEMORY | QSPI_INSTRFRAME_INSTREN |
QSPI_INSTRFRAME_ADDREN | QSPI_INSTRFRAME_DATAEN;
uint32_t width = (_cmd_read == SFLASH_CMD_QUAD_READ)
? QSPI_INSTRFRAME_WIDTH_QUAD_OUTPUT
: QSPI_INSTRFRAME_WIDTH_SINGLE_BIT_SPI;

uint32_t iframe = width | QSPI_INSTRFRAME_ADDRLEN_24BITS |
QSPI_INSTRFRAME_TFRTYPE_WRITEMEMORY |
QSPI_INSTRFRAME_INSTREN | QSPI_INSTRFRAME_ADDREN |
QSPI_INSTRFRAME_DATAEN;

uint8_t cmd = (_cmd_read == SFLASH_CMD_QUAD_READ)
? SFLASH_CMD_QUAD_PAGE_PROGRAM
: SFLASH_CMD_PAGE_PROGRAM;

samd_peripherals_disable_and_clear_cache();
_run_instruction(SFLASH_CMD_QUAD_PAGE_PROGRAM, iframe, addr, (uint8_t *)data,
len);
_run_instruction(cmd, iframe, addr, (uint8_t *)data, len);
samd_peripherals_enable_cache();

return true;
Expand Down