Skip to content

Commit 29a2bac

Browse files
authored
Fixup SPI. (qmk#17534)
1 parent 0e5d671 commit 29a2bac

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

drivers/eeprom/eeprom_spi.c

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,20 @@ static bool spi_eeprom_start(void) {
5858

5959
static spi_status_t spi_eeprom_wait_while_busy(int timeout) {
6060
uint32_t deadline = timer_read32() + timeout;
61-
spi_status_t response;
62-
do {
61+
spi_status_t response = SR_WIP;
62+
while (response & SR_WIP) {
63+
if (!spi_eeprom_start()) {
64+
return SPI_STATUS_ERROR;
65+
}
66+
6367
spi_write(CMD_RDSR);
6468
response = spi_read();
69+
spi_stop();
70+
6571
if (timer_read32() >= deadline) {
6672
return SPI_STATUS_TIMEOUT;
6773
}
68-
} while (response & SR_WIP);
74+
}
6975
return SPI_STATUS_SUCCESS;
7076
}
7177

@@ -105,27 +111,21 @@ void eeprom_driver_erase(void) {
105111
void eeprom_read_block(void *buf, const void *addr, size_t len) {
106112
//-------------------------------------------------
107113
// Wait for the write-in-progress bit to be cleared
108-
bool res = spi_eeprom_start();
109-
if (!res) {
110-
dprint("failed to start SPI for WIP check\n");
111-
memset(buf, 0, len);
112-
return;
113-
}
114-
115114
spi_status_t response = spi_eeprom_wait_while_busy(EXTERNAL_EEPROM_SPI_TIMEOUT);
116-
spi_stop();
117-
if (response == SPI_STATUS_TIMEOUT) {
118-
dprint("SPI timeout for WIP check\n");
115+
if (response != SPI_STATUS_SUCCESS) {
116+
spi_stop();
119117
memset(buf, 0, len);
118+
dprint("SPI timeout for WIP check\n");
120119
return;
121120
}
122121

123122
//-------------------------------------------------
124123
// Perform read
125-
res = spi_eeprom_start();
124+
bool res = spi_eeprom_start();
126125
if (!res) {
127-
dprint("failed to start SPI for read\n");
126+
spi_stop();
128127
memset(buf, 0, len);
128+
dprint("failed to start SPI for read\n");
129129
return;
130130
}
131131

@@ -158,15 +158,9 @@ void eeprom_write_block(const void *buf, void *addr, size_t len) {
158158

159159
//-------------------------------------------------
160160
// Wait for the write-in-progress bit to be cleared
161-
res = spi_eeprom_start();
162-
if (!res) {
163-
dprint("failed to start SPI for WIP check\n");
164-
return;
165-
}
166-
167161
spi_status_t response = spi_eeprom_wait_while_busy(EXTERNAL_EEPROM_SPI_TIMEOUT);
168-
spi_stop();
169-
if (response == SPI_STATUS_TIMEOUT) {
162+
if (response != SPI_STATUS_SUCCESS) {
163+
spi_stop();
170164
dprint("SPI timeout for WIP check\n");
171165
return;
172166
}
@@ -175,6 +169,7 @@ void eeprom_write_block(const void *buf, void *addr, size_t len) {
175169
// Enable writes
176170
res = spi_eeprom_start();
177171
if (!res) {
172+
spi_stop();
178173
dprint("failed to start SPI for write-enable\n");
179174
return;
180175
}
@@ -186,6 +181,7 @@ void eeprom_write_block(const void *buf, void *addr, size_t len) {
186181
// Perform the write
187182
res = spi_eeprom_start();
188183
if (!res) {
184+
spi_stop();
189185
dprint("failed to start SPI for write\n");
190186
return;
191187
}

platforms/chibios/drivers/spi_master.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ __attribute__((weak)) void spi_init(void) {
4646
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);
4747
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);
4848
#endif
49+
spiUnselect(&SPI_DRIVER);
50+
spiStop(&SPI_DRIVER);
51+
currentSlavePin = NO_PIN;
4952
}
5053
}
5154

0 commit comments

Comments
 (0)