Skip to content

Commit 2a3c5be

Browse files
committed
Try to read and send the requested data in chunks
1 parent 13e54b0 commit 2a3c5be

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

main.c

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -196,32 +196,47 @@ static void command_loop(void)
196196
uint32_t slen, rlen;
197197
readbytes_blocking(&slen, 3); // Read send length
198198
readbytes_blocking(&rlen, 3); // Read receive length
199-
slen &= 0x00FFFFFF; // Ensure it's treated as 24-bit value
200-
rlen &= 0x00FFFFFF; // Ensure it's treated as 24-bit value
199+
slen &= 0x00FFFFFF; // Mask to use only the lower 24 bits
200+
rlen &= 0x00FFFFFF; // Mask to use only the lower 24 bits
201201

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-
// Handling send operation
205+
// Read data to be sent (if slen > 0)
206206
if (slen > 0) {
207207
readbytes_blocking(tx_buffer, slen);
208-
cs_select(SPI_CS);
208+
}
209+
210+
// Perform SPI operation
211+
cs_select(SPI_CS);
212+
if (slen > 0) {
209213
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+
}
222+
210223
cs_deselect(SPI_CS);
224+
break;
211225
}
212226

213-
// Send ACK after write operation
227+
// Send ACK after handling slen (before reading)
214228
sendbyte_blocking(S_ACK);
215229

216-
// Handling receive operation
217-
if (rlen > 0) {
218-
cs_select(SPI_CS);
219-
spi_read_blocking(SPI_IF, 0, rx_buffer, rlen);
220-
cs_deselect(SPI_CS);
230+
// Handle receive operation in chunks
231+
uint32_t chunk_size = (rlen < MAX_BUFFER_SIZE) ? rlen : MAX_BUFFER_SIZE;
221232

222-
sendbytes_blocking(rx_buffer, rlen);
223-
}
233+
cs_select(SPI_CS);
234+
spi_read_blocking(SPI_IF, 0, rx_buffer, chunk_size);
235+
cs_deselect(SPI_CS);
224236

237+
sendbytes_blocking(rx_buffer, chunk_size);
238+
rlen -= chunk_size;
239+
cs_deselect(SPI_CS);
225240
break;
226241
}
227242
case S_CMD_S_SPI_FREQ:

0 commit comments

Comments
 (0)