Skip to content

Commit 1dcb973

Browse files
committed
Try to read and send the requested data in chunks
1 parent c20e434 commit 1dcb973

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

main.c

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -202,38 +202,31 @@ 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-
// Handle error for oversized slen
206-
if (slen > MAX_BUFFER_SIZE) {
207-
sendbyte_blocking(S_NAK);
208-
break;
209-
}
205+
// Handle send operation in chunks
206+
while (slen > 0) {
207+
uint32_t chunk_size = (slen < MAX_BUFFER_SIZE) ? slen : MAX_BUFFER_SIZE;
208+
readbytes_blocking(tx_buffer, chunk_size);
210209

211-
// Read data to be sent (if slen > 0)
212-
if (slen > 0) {
213-
readbytes_blocking(tx_buffer, slen);
214-
}
215-
216-
// Perform SPI write operation (if slen > 0)
217-
if (slen > 0) {
218210
cs_select(SPI_CS);
219-
spi_write_blocking(SPI_IF, tx_buffer, slen);
211+
spi_write_blocking(SPI_IF, tx_buffer, chunk_size);
220212
cs_deselect(SPI_CS);
213+
214+
slen -= chunk_size;
221215
}
222216

223217
// Send ACK after handling slen (before reading)
224218
sendbyte_blocking(S_ACK);
225219

226-
// Perform SPI read operation in chunks (if rlen > 0)
227-
uint32_t total_read = 0;
228-
while (total_read < rlen) {
229-
uint32_t chunk_size = (rlen - total_read > MAX_BUFFER_SIZE) ? MAX_BUFFER_SIZE : (rlen - total_read);
220+
// Handle receive operation in chunks
221+
while (rlen > 0) {
222+
uint32_t chunk_size = (rlen < MAX_BUFFER_SIZE) ? rlen : MAX_BUFFER_SIZE;
230223

231224
cs_select(SPI_CS);
232225
spi_read_blocking(SPI_IF, 0, rx_buffer, chunk_size);
233226
cs_deselect(SPI_CS);
234227

235228
sendbytes_blocking(rx_buffer, chunk_size);
236-
total_read += chunk_size;
229+
rlen -= chunk_size;
237230
}
238231

239232
break;

0 commit comments

Comments
 (0)