@@ -75,10 +75,14 @@ static bool bus_uses_iomux_pins(spi_host_device_t host, const spi_bus_config_t*
75
75
76
76
// End copied code.
77
77
78
- static bool spi_bus_free (spi_host_device_t host_id ) {
78
+ static bool _spi_bus_free (spi_host_device_t host_id ) {
79
79
return spi_bus_get_attr (host_id ) == NULL ;
80
80
}
81
81
82
+ static void spi_interrupt_handler (void * arg ) {
83
+
84
+ }
85
+
82
86
void common_hal_busio_spi_construct (busio_spi_obj_t * self ,
83
87
const mcu_pin_obj_t * clock , const mcu_pin_obj_t * mosi ,
84
88
const mcu_pin_obj_t * miso ) {
@@ -90,21 +94,21 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
90
94
bus_config .quadhd_io_num = -1 ;
91
95
bus_config .max_transfer_sz = 0 ; // Uses the default
92
96
bus_config .flags = SPICOMMON_BUSFLAG_MASTER | SPICOMMON_BUSFLAG_SCLK |
93
- mosi != NULL ? SPICOMMON_BUSFLAG_MOSI : 0 |
94
- miso != NULL ? SPICOMMON_BUSFLAG_MISO : 0 ;
97
+ ( mosi != NULL ? SPICOMMON_BUSFLAG_MOSI : 0 ) |
98
+ ( miso != NULL ? SPICOMMON_BUSFLAG_MISO : 0 ) ;
95
99
bus_config .intr_flags = 0 ;
96
100
97
101
// RAM and Flash is often on SPI1 and is unsupported by the IDF so use it as
98
102
// a flag value.
99
103
spi_host_device_t host_id = SPI1_HOST ;
100
104
self -> connected_through_gpio = true;
101
105
// Try and save SPI2 for pins that are on the IOMUX
102
- if (bus_uses_iomux_pins (SPI2_HOST , & bus_config ) && spi_bus_free (SPI2_HOST )) {
106
+ if (bus_uses_iomux_pins (SPI2_HOST , & bus_config ) && _spi_bus_free (SPI2_HOST )) {
103
107
host_id = SPI2_HOST ;
104
108
self -> connected_through_gpio = false;
105
- } else if (spi_bus_free (SPI3_HOST )) {
109
+ } else if (_spi_bus_free (SPI3_HOST )) {
106
110
host_id = SPI3_HOST ;
107
- } else if (spi_bus_free (SPI2_HOST )) {
111
+ } else if (_spi_bus_free (SPI2_HOST )) {
108
112
host_id = SPI2_HOST ;
109
113
}
110
114
if (host_id == SPI1_HOST ) {
@@ -114,21 +118,21 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
114
118
esp_err_t result = spi_bus_initialize (host_id , & bus_config , 0 /* dma channel */ );
115
119
if (result == ESP_ERR_NO_MEM ) {
116
120
mp_raise_msg (& mp_type_MemoryError , translate ("ESP-IDF memory allocation failed" ));
117
- } else if (result = ESP_INVALID_ARG ) {
121
+ } else if (result == ESP_ERR_INVALID_ARG ) {
118
122
mp_raise_ValueError (translate ("Invalid pins" ));
119
123
}
120
124
spi_bus_lock_dev_config_t config = { .flags = 0 };
121
125
result = spi_bus_lock_register_dev (spi_bus_get_attr (host_id )-> lock ,
122
- spi_bus_lock_dev_config_t * config ,
126
+ & config ,
123
127
& self -> lock );
124
128
if (result == ESP_ERR_NO_MEM ) {
125
129
mp_raise_msg (& mp_type_MemoryError , translate ("ESP-IDF memory allocation failed" ));
126
130
}
127
131
128
132
129
- err = esp_intr_alloc (spicommon_irqsource_for_host (host_id ),
130
- bus_config .intr_flags | ESP_INTR_FLAG_INTRDISABLED ,
131
- spi_interrupt_handler , self , & self -> intr );
133
+ result = esp_intr_alloc (spicommon_irqsource_for_host (host_id ),
134
+ bus_config .intr_flags | ESP_INTR_FLAG_INTRDISABLED ,
135
+ spi_interrupt_handler , self , & self -> interrupt );
132
136
if (result == ESP_ERR_NO_MEM ) {
133
137
mp_raise_msg (& mp_type_MemoryError , translate ("ESP-IDF memory allocation failed" ));
134
138
}
@@ -198,18 +202,21 @@ bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
198
202
bits == self -> bits ) {
199
203
return true;
200
204
}
201
- self -> hal_context -> mode = polarity << 1 | phase ;
205
+ self -> hal_context . mode = polarity << 1 | phase ;
202
206
self -> polarity = polarity ;
203
207
self -> phase = phase ;
204
208
self -> bits = bits ;
205
209
self -> target_frequency = baudrate ;
206
- esp_err_t result = spi_hal_get_clock_conf (self -> hal_context ,
210
+ esp_err_t result = spi_hal_get_clock_conf (& self -> hal_context ,
207
211
self -> target_frequency ,
208
212
128 /* duty_cycle */ ,
209
213
self -> connected_through_gpio ,
210
214
0 /* input_delay_ns */ ,
211
215
& self -> real_frequency ,
212
216
& self -> timing_conf );
217
+ if (result != ESP_OK ) {
218
+ return false;
219
+ }
213
220
214
221
spi_hal_setup_device (& self -> hal_context );
215
222
return true;
@@ -222,9 +229,9 @@ bool common_hal_busio_spi_try_lock(busio_spi_obj_t *self) {
222
229
return false;
223
230
}
224
231
// Wait to grab the lock from another task.
225
- esp_err_t ret = spi_bus_lock_acquire_start (self -> lock , portMAX_DELAY );
226
- self -> has_lock = true ;
227
- return true ;
232
+ esp_err_t result = spi_bus_lock_acquire_start (self -> lock , portMAX_DELAY );
233
+ self -> has_lock = result == ESP_OK ;
234
+ return self -> has_lock ;
228
235
}
229
236
230
237
bool common_hal_busio_spi_has_lock (busio_spi_obj_t * self ) {
@@ -238,62 +245,37 @@ void common_hal_busio_spi_unlock(busio_spi_obj_t *self) {
238
245
239
246
bool common_hal_busio_spi_write (busio_spi_obj_t * self ,
240
247
const uint8_t * data , size_t len ) {
241
- if (len == 0 ) {
242
- return true;
243
- }
244
- // int32_t status;
245
- if (len >= 16 ) {
246
- // status = sercom_dma_write(self->spi_desc.dev.prvt, data, len);
247
- } else {
248
- // struct io_descriptor *spi_io;
249
- // spi_m_sync_get_io_descriptor(&self->spi_desc, &spi_io);
250
- // status = spi_io->write(spi_io, data, len);
251
- }
252
- return false; // Status is number of chars read or an error code < 0.
248
+ return common_hal_busio_spi_transfer (self , data , NULL , len );
253
249
}
254
250
255
251
bool common_hal_busio_spi_read (busio_spi_obj_t * self ,
256
252
uint8_t * data , size_t len , uint8_t write_value ) {
257
- if (len == 0 ) {
258
- return true;
259
- }
260
- // int32_t status;
261
- if (len >= 16 ) {
262
- // status = sercom_dma_read(self->spi_desc.dev.prvt, data, len, write_value);
263
- } else {
264
- // self->spi_desc.dev.dummy_byte = write_value;
265
-
266
- // struct io_descriptor *spi_io;
267
- // spi_m_sync_get_io_descriptor(&self->spi_desc, &spi_io);
268
-
269
- // status = spi_io->read(spi_io, data, len);
270
- }
271
- return false; // Status is number of chars read or an error code < 0.
253
+ return common_hal_busio_spi_transfer (self , NULL , data , len );
272
254
}
273
255
274
- bool common_hal_busio_spi_transfer (busio_spi_obj_t * self , uint8_t * data_out , uint8_t * data_in , size_t len ) {
256
+ bool common_hal_busio_spi_transfer (busio_spi_obj_t * self , const uint8_t * data_out , uint8_t * data_in , size_t len ) {
275
257
if (len == 0 ) {
276
258
return true;
277
259
}
278
260
279
261
spi_hal_context_t * hal = & self -> hal_context ;
280
- hal -> tx_bitlen = len * 8 ;
281
- hal -> rx_bitlen = len * 8 ;
282
- hal -> send_buffer = data_out ;
262
+ hal -> tx_bitlen = len * self -> bits ;
263
+ hal -> rx_bitlen = len * self -> bits ;
264
+ hal -> send_buffer = ( uint8_t * ) data_out ;
283
265
hal -> rcv_buffer = data_in ;
284
266
285
267
spi_hal_setup_trans (hal );
286
268
spi_hal_prepare_data (hal );
287
269
spi_hal_user_start (hal );
288
- if (len >= 16 && false) {
270
+ if (len >= SOC_SPI_MAXIMUM_BUFFER_SIZE && false) {
289
271
// Set up the interrupt and wait on the lock.
290
272
} else {
291
273
while (!spi_hal_usr_is_done (hal )) {
292
- RUN_BACKGROUND_TASKS () ;
274
+ RUN_BACKGROUND_TASKS ;
293
275
}
294
276
}
295
277
296
- return false ;
278
+ return true ;
297
279
}
298
280
299
281
uint32_t common_hal_busio_spi_get_frequency (busio_spi_obj_t * self ) {
0 commit comments