Skip to content

Commit 235084e

Browse files
committed
Try to read and send the requested data in chunks
1 parent ce4fe99 commit 235084e

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

main.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -202,31 +202,27 @@ static void command_loop(void)
202202
uint8_t tx_buffer[MAX_BUFFER_SIZE]; // Buffer for transmit data
203203
uint8_t rx_buffer[MAX_BUFFER_SIZE]; // Buffer for receive data
204204

205-
// Ensure buffer sizes are adequate
206-
if (slen > MAX_BUFFER_SIZE || rlen > MAX_BUFFER_SIZE) {
207-
sendbyte_blocking(S_NAK);
208-
break;
209-
}
210-
211205
// Read data to be sent (if slen > 0)
212206
if (slen > 0) {
213207
readbytes_blocking(tx_buffer, slen);
214208
}
215209

216-
// Perform SPI operation
217-
cs_select(SPI_CS);
210+
sendbyte_blocking(S_ACK);
211+
218212
if (slen > 0) {
219213
spi_write_blocking(SPI_IF, tx_buffer, slen);
220214
}
221-
if (rlen > 0) {
222-
spi_read_blocking(SPI_IF, 0, rx_buffer, rlen);
223-
}
224-
cs_deselect(SPI_CS);
225215

226-
// Send ACK followed by received data
227-
sendbyte_blocking(S_ACK);
228-
if (rlen > 0) {
229-
sendbytes_blocking(rx_buffer, rlen);
216+
// Perform SPI operation in chunks
217+
while(rlen > MAX_BUFFER_SIZE) {
218+
cs_select(SPI_CS);
219+
spi_read_blocking(SPI_IF, 0, rx_buffer, MAX_BUFFER_SIZE);
220+
cs_deselect(SPI_CS);
221+
222+
// Send ACK followed by received data
223+
sendbytes_blocking(rx_buffer, MAX_BUFFER_SIZE);
224+
225+
rlen -= MAX_BUFFER_SIZE;
230226
}
231227

232228
break;

0 commit comments

Comments
 (0)