33
33
#include "driver/spi_common_internal.h"
34
34
35
35
#define SPI_MAX_DMA_BITS (SPI_MAX_DMA_LEN * 8)
36
+ #define MAX_SPI_TRANSACTIONS 10
36
37
37
38
static bool spi_never_reset [SOC_SPI_PERIPH_NUM ];
38
39
static spi_device_handle_t spi_handle [SOC_SPI_PERIPH_NUM ];
@@ -59,7 +60,7 @@ static void set_spi_config(busio_spi_obj_t *self,
59
60
.clock_speed_hz = baudrate ,
60
61
.mode = phase | (polarity << 1 ),
61
62
.spics_io_num = -1 , // No CS pin
62
- .queue_size = 1 ,
63
+ .queue_size = MAX_SPI_TRANSACTIONS ,
63
64
.pre_cb = NULL
64
65
};
65
66
esp_err_t result = spi_bus_add_device (self -> host_id , & device_config , & spi_handle [self -> host_id ]);
@@ -213,47 +214,61 @@ bool common_hal_busio_spi_transfer(busio_spi_obj_t *self,
213
214
mp_raise_ValueError (translate ("No MISO Pin" ));
214
215
}
215
216
216
- spi_transaction_t transaction = { 0 } ;
217
+ spi_transaction_t transactions [ MAX_SPI_TRANSACTIONS ] ;
217
218
218
219
// Round to nearest whole set of bits
219
220
int bits_to_send = len * 8 / self -> bits * self -> bits ;
220
221
221
222
if (len <= 4 ) {
223
+ memset (& transactions [0 ], 0 , sizeof (spi_transaction_t ));
222
224
if (data_out != NULL ) {
223
- memcpy (& transaction .tx_data , data_out , len );
225
+ memcpy (& transactions [ 0 ] .tx_data , data_out , len );
224
226
}
225
227
226
- transaction .flags = SPI_TRANS_USE_TXDATA | SPI_TRANS_USE_RXDATA ;
227
- transaction .length = bits_to_send ;
228
- spi_device_transmit (spi_handle [self -> host_id ], & transaction );
228
+ transactions [ 0 ] .flags = SPI_TRANS_USE_TXDATA | SPI_TRANS_USE_RXDATA ;
229
+ transactions [ 0 ] .length = bits_to_send ;
230
+ spi_device_transmit (spi_handle [self -> host_id ], & transactions [ 0 ] );
229
231
230
232
if (data_in != NULL ) {
231
- memcpy (data_in , & transaction .rx_data , len );
233
+ memcpy (data_in , & transactions [ 0 ] .rx_data , len );
232
234
}
233
235
} else {
234
236
int offset = 0 ;
235
237
int bits_remaining = bits_to_send ;
238
+ int cur_trans = 0 ;
236
239
237
240
while (bits_remaining && !mp_hal_is_interrupted ()) {
238
- memset (& transaction , 0 , sizeof (transaction ));
239
241
240
- transaction .length =
241
- bits_remaining > SPI_MAX_DMA_BITS ? SPI_MAX_DMA_BITS : bits_remaining ;
242
+ cur_trans = 0 ;
243
+ while (bits_remaining && (cur_trans != MAX_SPI_TRANSACTIONS )) {
244
+ memset (& transactions [cur_trans ], 0 , sizeof (spi_transaction_t ));
242
245
243
- if (data_out != NULL ) {
244
- transaction .tx_buffer = data_out + offset ;
245
- }
246
- if (data_in != NULL ) {
247
- transaction .rx_buffer = data_in + offset ;
248
- }
246
+ transactions [cur_trans ].length =
247
+ bits_remaining > SPI_MAX_DMA_BITS ? SPI_MAX_DMA_BITS : bits_remaining ;
249
248
250
- spi_device_transmit (spi_handle [self -> host_id ], & transaction );
251
- bits_remaining -= transaction .length ;
249
+ if (data_out != NULL ) {
250
+ transactions [cur_trans ].tx_buffer = data_out + offset ;
251
+ }
252
+ if (data_in != NULL ) {
253
+ transactions [cur_trans ].rx_buffer = data_in + offset ;
254
+ }
252
255
253
- // doesn't need ceil(); loop ends when bits_remaining is 0
254
- offset += transaction .length / 8 ;
256
+ bits_remaining -= transactions [cur_trans ].length ;
257
+
258
+ // doesn't need ceil(); loop ends when bits_remaining is 0
259
+ offset += transactions [cur_trans ].length / 8 ;
260
+ cur_trans ++ ;
261
+ }
255
262
256
- RUN_BACKGROUND_TASKS ;
263
+ for (int i = 0 ; i < cur_trans ; i ++ ) {
264
+ spi_device_queue_trans (spi_handle [self -> host_id ], & transactions [i ], portMAX_DELAY );
265
+ }
266
+
267
+ spi_transaction_t * rtrans ;
268
+ for (int x = 0 ; x < cur_trans ; x ++ ) {
269
+ RUN_BACKGROUND_TASKS ;
270
+ spi_device_get_trans_result (spi_handle [self -> host_id ], & rtrans , portMAX_DELAY );
271
+ }
257
272
}
258
273
}
259
274
return true;
0 commit comments