Skip to content

Commit 0ff9ccc

Browse files
committed
Try to read and send the requested data in chunks
1 parent 18ba92e commit 0ff9ccc

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

main.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ static void command_loop(void)
190190
sendbyte_blocking(S_ACK);
191191
else
192192
sendbyte_blocking(S_NAK);
193-
break;
193+
break
194194
case S_CMD_O_SPIOP:
195195
{
196196
uint32_t slen, rlen;
@@ -211,29 +211,34 @@ static void command_loop(void)
211211
// Read data to be sent (if slen > 0)
212212
if (slen > 0) {
213213
readbytes_blocking(tx_buffer, slen);
214+
}
215+
216+
// Perform SPI write operation (if slen > 0)
217+
if (slen > 0) {
214218
cs_select(SPI_CS);
215219
spi_write_blocking(SPI_IF, tx_buffer, slen);
216220
cs_deselect(SPI_CS);
217221
}
218222

219-
// Send ACK after handling slen
223+
// Send ACK after handling slen (before reading)
220224
sendbyte_blocking(S_ACK);
221225

222-
// Handle rlen in chunks
223-
while (rlen > 0) {
224-
uint32_t chunk_size = (rlen < MAX_BUFFER_SIZE) ? rlen : MAX_BUFFER_SIZE;
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);
225230

226231
cs_select(SPI_CS);
227232
spi_read_blocking(SPI_IF, 0, rx_buffer, chunk_size);
228233
cs_deselect(SPI_CS);
229234

230235
sendbytes_blocking(rx_buffer, chunk_size);
231-
rlen -= chunk_size;
236+
total_read += chunk_size;
232237
}
233238

234239
break;
235240
}
236-
case S_CMD_S_SPI_FREQ:
241+
case S_CMD_S_SPI_FREQ:
237242
{
238243
uint32_t want_baud;
239244
readbytes_blocking(&want_baud, 4);

0 commit comments

Comments
 (0)