Fixup SPI. (#17534)

This commit is contained in:
Nick Brassel 2022-07-06 06:41:35 +10:00 committed by GitHub
parent 0e5d67145a
commit 29a2bac469
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 23 deletions

View file

@ -58,14 +58,20 @@ static bool spi_eeprom_start(void) {
static spi_status_t spi_eeprom_wait_while_busy(int timeout) { static spi_status_t spi_eeprom_wait_while_busy(int timeout) {
uint32_t deadline = timer_read32() + timeout; uint32_t deadline = timer_read32() + timeout;
spi_status_t response; spi_status_t response = SR_WIP;
do { while (response & SR_WIP) {
if (!spi_eeprom_start()) {
return SPI_STATUS_ERROR;
}
spi_write(CMD_RDSR); spi_write(CMD_RDSR);
response = spi_read(); response = spi_read();
spi_stop();
if (timer_read32() >= deadline) { if (timer_read32() >= deadline) {
return SPI_STATUS_TIMEOUT; return SPI_STATUS_TIMEOUT;
} }
} while (response & SR_WIP); }
return SPI_STATUS_SUCCESS; return SPI_STATUS_SUCCESS;
} }
@ -105,27 +111,21 @@ void eeprom_driver_erase(void) {
void eeprom_read_block(void *buf, const void *addr, size_t len) { void eeprom_read_block(void *buf, const void *addr, size_t len) {
//------------------------------------------------- //-------------------------------------------------
// Wait for the write-in-progress bit to be cleared // Wait for the write-in-progress bit to be cleared
bool res = spi_eeprom_start();
if (!res) {
dprint("failed to start SPI for WIP check\n");
memset(buf, 0, len);
return;
}
spi_status_t response = spi_eeprom_wait_while_busy(EXTERNAL_EEPROM_SPI_TIMEOUT); spi_status_t response = spi_eeprom_wait_while_busy(EXTERNAL_EEPROM_SPI_TIMEOUT);
spi_stop(); if (response != SPI_STATUS_SUCCESS) {
if (response == SPI_STATUS_TIMEOUT) { spi_stop();
dprint("SPI timeout for WIP check\n");
memset(buf, 0, len); memset(buf, 0, len);
dprint("SPI timeout for WIP check\n");
return; return;
} }
//------------------------------------------------- //-------------------------------------------------
// Perform read // Perform read
res = spi_eeprom_start(); bool res = spi_eeprom_start();
if (!res) { if (!res) {
dprint("failed to start SPI for read\n"); spi_stop();
memset(buf, 0, len); memset(buf, 0, len);
dprint("failed to start SPI for read\n");
return; return;
} }
@ -158,15 +158,9 @@ void eeprom_write_block(const void *buf, void *addr, size_t len) {
//------------------------------------------------- //-------------------------------------------------
// Wait for the write-in-progress bit to be cleared // Wait for the write-in-progress bit to be cleared
res = spi_eeprom_start();
if (!res) {
dprint("failed to start SPI for WIP check\n");
return;
}
spi_status_t response = spi_eeprom_wait_while_busy(EXTERNAL_EEPROM_SPI_TIMEOUT); spi_status_t response = spi_eeprom_wait_while_busy(EXTERNAL_EEPROM_SPI_TIMEOUT);
spi_stop(); if (response != SPI_STATUS_SUCCESS) {
if (response == SPI_STATUS_TIMEOUT) { spi_stop();
dprint("SPI timeout for WIP check\n"); dprint("SPI timeout for WIP check\n");
return; return;
} }
@ -175,6 +169,7 @@ void eeprom_write_block(const void *buf, void *addr, size_t len) {
// Enable writes // Enable writes
res = spi_eeprom_start(); res = spi_eeprom_start();
if (!res) { if (!res) {
spi_stop();
dprint("failed to start SPI for write-enable\n"); dprint("failed to start SPI for write-enable\n");
return; return;
} }
@ -186,6 +181,7 @@ void eeprom_write_block(const void *buf, void *addr, size_t len) {
// Perform the write // Perform the write
res = spi_eeprom_start(); res = spi_eeprom_start();
if (!res) { if (!res) {
spi_stop();
dprint("failed to start SPI for write\n"); dprint("failed to start SPI for write\n");
return; return;
} }

View file

@ -46,6 +46,9 @@ __attribute__((weak)) void spi_init(void) {
palSetPadMode(PAL_PORT(SPI_MOSI_PIN), PAL_PAD(SPI_MOSI_PIN), PAL_MODE_ALTERNATE(SPI_MOSI_PAL_MODE) | PAL_OUTPUT_TYPE_PUSHPULL | PAL_OUTPUT_SPEED_HIGHEST); palSetPadMode(PAL_PORT(SPI_MOSI_PIN), PAL_PAD(SPI_MOSI_PIN), PAL_MODE_ALTERNATE(SPI_MOSI_PAL_MODE) | PAL_OUTPUT_TYPE_PUSHPULL | PAL_OUTPUT_SPEED_HIGHEST);
palSetPadMode(PAL_PORT(SPI_MISO_PIN), PAL_PAD(SPI_MISO_PIN), PAL_MODE_ALTERNATE(SPI_MISO_PAL_MODE) | PAL_OUTPUT_TYPE_PUSHPULL | PAL_OUTPUT_SPEED_HIGHEST); palSetPadMode(PAL_PORT(SPI_MISO_PIN), PAL_PAD(SPI_MISO_PIN), PAL_MODE_ALTERNATE(SPI_MISO_PAL_MODE) | PAL_OUTPUT_TYPE_PUSHPULL | PAL_OUTPUT_SPEED_HIGHEST);
#endif #endif
spiUnselect(&SPI_DRIVER);
spiStop(&SPI_DRIVER);
currentSlavePin = NO_PIN;
} }
} }