Skip to content

Commit

Permalink
spiFlash: fixed set_quad_bit method
Browse files Browse the repository at this point in the history
  • Loading branch information
trabucayre committed Sep 22, 2024
1 parent 24a0548 commit 2a46ab9
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/spiFlash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ void SPIFlash::display_status_reg(uint8_t reg)
printf("IRL : %x\n", ((reg >> 4) & 0x0f));
break;
case 0x0102:
case 0x0120:
_spi->spi_put(FLASH_RDCR, NULL, &reg, 1);
printf("\nConfiguration Register\n");
printf("RDCR : %02x\n", reg);
Expand All @@ -630,6 +631,8 @@ void SPIFlash::display_status_reg(uint8_t reg)
printf("TBPARM : %d\n", ((reg >> 2) & 0x01));
printf("BPNV : %d\n", ((reg >> 3) & 0x01));
printf("TBPROT : %d\n", ((reg >> 5) & 0x01));
if (dev_id == 0x0120)
printf("LC : %d\n", ((reg >> 6) & 0x02));
break;
case 0x0160:
_spi->spi_put(FLASH_RDCR, NULL, &reg, 1);
Expand Down Expand Up @@ -891,11 +894,11 @@ int SPIFlash::enable_protection(uint32_t length)

bool SPIFlash::set_quad_bit(bool set_quad)
{
uint8_t reg_wr, reg_rd; // read/write registers code
uint16_t reg_val;
uint32_t nb_rd_byte = 1; // Number of bytes to read (may differ from Flash models).
uint32_t nb_wr_byte = 1; // Number of bytes to write.
uint16_t quad_bit = 0; // quad_mask copy when bit must be set
uint8_t reg_wr, reg_rd; // read/write registers code.
uint16_t reg_val = 0; // set to 0: avoid random when 8bits are read.
uint32_t nb_rd_byte = 1; // read bytes len (may differ from Flash models).
uint32_t nb_wr_byte = 1; // write bytes len.
uint16_t quad_bit = 0; // quad_mask copy when bit must be set.

if (!_flash_model) {
printError("spiFlash Error: can't configure Quad mode on unknown SPI Flash");
Expand Down Expand Up @@ -953,23 +956,23 @@ bool SPIFlash::set_quad_bit(bool set_quad)
case CONFR:
uint8_t status = read_status_reg();
reg_val = ((reg_val & 0xff) << 8) | status;
_spi->spi_put(reg_wr, (uint8_t*) &reg_val, NULL, nb_wr_byte);
break;
}

/* Write register with the updated value */
_spi->spi_put(reg_wr, (uint8_t *)&reg_val, NULL, nb_wr_byte);

/* Wait for completion */
if (_spi->spi_wait(FLASH_RDSR, FLASH_RDSR_WEL, 0x00, 1000) != 0) {
if (_spi->spi_wait(FLASH_RDSR, FLASH_RDSR_WEL, 0x00, 10000) != 0) {
printError("SPIFlash Error: failed to disable write");
return false;
}

/* Check if register is correctly updated */
reg_val = 0; // 16 bits but only LSB may be updated
_spi->spi_put(reg_rd, NULL, (uint8_t *)&reg_val, nb_rd_byte);

if ((reg_val & _flash_model->quad_mask) == quad_bit) {
if ((reg_val & _flash_model->quad_mask) != quad_bit) {
printf("%04x %04x %04x\n", reg_val, reg_val & _flash_model->quad_mask, quad_bit);
printError("SPIFlash Error: failed to update Quad bit");
return false;
Expand Down

0 comments on commit 2a46ab9

Please sign in to comment.