Skip to content

Commit

Permalink
Fixup SPI. (qmk#17534)
Browse files Browse the repository at this point in the history
  • Loading branch information
tzarc authored Jul 5, 2022
1 parent 0e5d671 commit 29a2bac
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
42 changes: 19 additions & 23 deletions drivers/eeprom/eeprom_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,20 @@ static bool spi_eeprom_start(void) {

static spi_status_t spi_eeprom_wait_while_busy(int timeout) {
uint32_t deadline = timer_read32() + timeout;
spi_status_t response;
do {
spi_status_t response = SR_WIP;
while (response & SR_WIP) {
if (!spi_eeprom_start()) {
return SPI_STATUS_ERROR;
}

spi_write(CMD_RDSR);
response = spi_read();
spi_stop();

if (timer_read32() >= deadline) {
return SPI_STATUS_TIMEOUT;
}
} while (response & SR_WIP);
}
return SPI_STATUS_SUCCESS;
}

Expand Down Expand Up @@ -105,27 +111,21 @@ void eeprom_driver_erase(void) {
void eeprom_read_block(void *buf, const void *addr, size_t len) {
//-------------------------------------------------
// 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_stop();
if (response == SPI_STATUS_TIMEOUT) {
dprint("SPI timeout for WIP check\n");
if (response != SPI_STATUS_SUCCESS) {
spi_stop();
memset(buf, 0, len);
dprint("SPI timeout for WIP check\n");
return;
}

//-------------------------------------------------
// Perform read
res = spi_eeprom_start();
bool res = spi_eeprom_start();
if (!res) {
dprint("failed to start SPI for read\n");
spi_stop();
memset(buf, 0, len);
dprint("failed to start SPI for read\n");
return;
}

Expand Down Expand Up @@ -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
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_stop();
if (response == SPI_STATUS_TIMEOUT) {
if (response != SPI_STATUS_SUCCESS) {
spi_stop();
dprint("SPI timeout for WIP check\n");
return;
}
Expand All @@ -175,6 +169,7 @@ void eeprom_write_block(const void *buf, void *addr, size_t len) {
// Enable writes
res = spi_eeprom_start();
if (!res) {
spi_stop();
dprint("failed to start SPI for write-enable\n");
return;
}
Expand All @@ -186,6 +181,7 @@ void eeprom_write_block(const void *buf, void *addr, size_t len) {
// Perform the write
res = spi_eeprom_start();
if (!res) {
spi_stop();
dprint("failed to start SPI for write\n");
return;
}
Expand Down
3 changes: 3 additions & 0 deletions platforms/chibios/drivers/spi_master.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_MISO_PIN), PAL_PAD(SPI_MISO_PIN), PAL_MODE_ALTERNATE(SPI_MISO_PAL_MODE) | PAL_OUTPUT_TYPE_PUSHPULL | PAL_OUTPUT_SPEED_HIGHEST);
#endif
spiUnselect(&SPI_DRIVER);
spiStop(&SPI_DRIVER);
currentSlavePin = NO_PIN;
}
}

Expand Down

0 comments on commit 29a2bac

Please sign in to comment.