Skip to content

Commit 6741cff

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

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

main.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,17 +202,28 @@ 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 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);
205+
// Read data to be sent (if slen > 0)
206+
if (slen > 0) {
207+
readbytes_blocking(tx_buffer, slen);
208+
}
209209

210-
cs_select(SPI_CS);
211-
spi_write_blocking(SPI_IF, tx_buffer, chunk_size);
212-
cs_deselect(SPI_CS);
210+
// Perform SPI operation
211+
cs_select(SPI_CS);
212+
if (slen > 0) {
213+
spi_write_blocking(SPI_IF, tx_buffer, slen);
214+
}
215+
if (rlen > 0 && rlen < MAX_BUFFER_SIZE ) {
216+
spi_read_blocking(SPI_IF, 0, rx_buffer, rlen);
217+
// Send ACK followed by received data
218+
sendbyte_blocking(S_ACK);
219+
if (rlen > 0) {
220+
sendbytes_blocking(rx_buffer, rlen);
221+
}
213222

214-
slen -= chunk_size;
223+
cs_deselect(SPI_CS);
224+
break;
215225
}
226+
cs_deselect(SPI_CS);
216227

217228
// Send ACK after handling slen (before reading)
218229
sendbyte_blocking(S_ACK);

0 commit comments

Comments
 (0)