Skip to content

Commit 692bf40

Browse files
committed
adds quad data support for SPI.
1 parent c17f5ce commit 692bf40

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

micropy_updates/common/mp_spi_common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
mp_obj_t sck;
2222
mp_obj_t mosi;
2323
mp_obj_t miso;
24+
mp_obj_t data2;
25+
mp_obj_t data3;
26+
bool quad;
2427
uint8_t device_count;
2528
machine_hw_spi_device_obj_t **devices;
2629
mp_spi_state_t state;

micropy_updates/esp32/machine_hw_spi.c

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ static void machine_hw_spi_bus_deinit_internal(machine_hw_spi_bus_obj_t *self)
263263
int miso = (int)mp_obj_get_int(self->miso);
264264
int mosi = (int)mp_obj_get_int(self->mosi);
265265
int sck = (int)mp_obj_get_int(self->sck);
266+
int data2 = (int)mp_obj_get_int(self->data2);
267+
int data3 = (int)mp_obj_get_int(self->data3);
266268

267269
if (miso != -1) {
268270
esp_rom_gpio_pad_select_gpio(miso);
@@ -279,6 +281,16 @@ static void machine_hw_spi_bus_deinit_internal(machine_hw_spi_bus_obj_t *self)
279281
esp_rom_gpio_connect_out_signal(sck, SIG_GPIO_OUT_IDX, false, false);
280282
gpio_set_direction(sck, GPIO_MODE_INPUT);
281283
}
284+
if (data2 != -1) {
285+
esp_rom_gpio_pad_select_gpio(data2);
286+
esp_rom_gpio_connect_out_signal(data2, SIG_GPIO_OUT_IDX, false, false);
287+
gpio_set_direction(data2, GPIO_MODE_INPUT);
288+
}
289+
if (data3 != -1) {
290+
esp_rom_gpio_pad_select_gpio(data3);
291+
esp_rom_gpio_connect_out_signal(data3, SIG_GPIO_OUT_IDX, false, false);
292+
gpio_set_direction(data3, GPIO_MODE_INPUT);
293+
}
282294

283295
self->state = MP_SPI_STATE_STOPPED;
284296
}
@@ -327,6 +339,9 @@ static void machine_hw_spi_device_transfer(mp_obj_base_t *self_in, size_t len, c
327339

328340
transaction.flags = SPI_TRANS_USE_TXDATA | SPI_TRANS_USE_RXDATA;
329341
transaction.length = bits_to_send;
342+
if (self->spi_bus->quad) {
343+
transaction.flags |= SPI_TRANS_MODE_QIO;
344+
}
330345
spi_device_transmit(spi_device, &transaction);
331346

332347
if (dest != NULL) {
@@ -361,6 +376,10 @@ static void machine_hw_spi_device_transfer(mp_obj_base_t *self_in, size_t len, c
361376
transaction->flags |= SPI_TRANS_USE_RXDATA;
362377
}
363378

379+
if (self->spi_bus->quad) {
380+
transaction.flags |= SPI_TRANS_MODE_QIO;
381+
}
382+
364383
spi_device_queue_trans(spi_device, transaction, portMAX_DELAY);
365384
bits_remaining -= transaction->length;
366385

@@ -389,12 +408,14 @@ static void machine_hw_spi_device_transfer(mp_obj_base_t *self_in, size_t len, c
389408

390409
mp_obj_t machine_hw_spi_bus_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
391410

392-
enum { ARG_host, ARG_mosi, ARG_miso, ARG_sck };
411+
enum { ARG_host, ARG_mosi, ARG_miso, ARG_sck, ARG_data2, ARG_data3 };
393412
static const mp_arg_t allowed_args[] = {
394413
{ MP_QSTR_host, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
395414
{ MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
396415
{ MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
397-
{ MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT }
416+
{ MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
417+
{ MP_QSTR_data2, MP_ARG_KW_ONLY | MP_ARG_INT, { .u_int = -1 } },
418+
{ MP_QSTR_data3, MP_ARG_KW_ONLY | MP_ARG_INT, { .u_int = -1 } },
398419
};
399420

400421
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
@@ -404,6 +425,8 @@ mp_obj_t machine_hw_spi_bus_make_new(const mp_obj_type_t *type, size_t n_args, s
404425
int mosi = (int)args[ARG_mosi].u_int;
405426
int miso = (int)args[ARG_miso].u_int;
406427
int sck = (int)args[ARG_sck].u_int;
428+
int data2 = (int)args[ARG_data2].u_int;
429+
int data3 = (int)args[ARG_data3].u_int;
407430

408431
machine_hw_spi_bus_obj_t *self;
409432

@@ -427,6 +450,8 @@ mp_obj_t machine_hw_spi_bus_make_new(const mp_obj_type_t *type, size_t n_args, s
427450
if ((int)mp_obj_get_int(self->mosi) != mosi) reconfigure = true;
428451
if ((int)mp_obj_get_int(self->miso) != miso) reconfigure = true;
429452
if ((int)mp_obj_get_int(self->sck) != sck) reconfigure = true;
453+
if ((int)mp_obj_get_int(self->data2) != data2) reconfigure = true;
454+
if ((int)mp_obj_get_int(self->data3) != data3) reconfigure = true;
430455
}
431456

432457
if (reconfigure) {
@@ -438,6 +463,14 @@ mp_obj_t machine_hw_spi_bus_make_new(const mp_obj_type_t *type, size_t n_args, s
438463
self->miso = mp_obj_new_int((mp_int_t)miso);
439464
self->mosi = mp_obj_new_int((mp_int_t)mosi);
440465
self->sck = mp_obj_new_int((mp_int_t)sck);
466+
self->data2 = mp_obj_new_int((mp_int_t)data2);
467+
self->data3 = mp_obj_new_int((mp_int_t)data3);
468+
469+
if (data2 != -1 && data3 != -1) {
470+
self->quad = true;
471+
} else {
472+
self->quad = false;
473+
}
441474
}
442475

443476
return MP_OBJ_FROM_PTR(self);
@@ -448,12 +481,19 @@ void machine_hw_spi_bus_initilize(machine_hw_spi_bus_obj_t *bus)
448481
{
449482
if (bus->state != MP_SPI_STATE_STOPPED) return;
450483

484+
uint32_t flags = 0;
485+
486+
if (self->quad) {
487+
flags |= SPICOMMON_BUSFLAG_QUAD;
488+
}
489+
451490
spi_bus_config_t buscfg = {
452491
.miso_io_num = (int)mp_obj_get_int(bus->miso),
453492
.mosi_io_num = (int)mp_obj_get_int(bus->mosi),
454493
.sclk_io_num = (int)mp_obj_get_int(bus->sck),
455-
.quadwp_io_num = -1,
456-
.quadhd_io_num = -1,
494+
.data2_io_num = (int)mp_obj_get_int(bus->data2),
495+
.data3_io_num = (int)mp_obj_get_int(bus->data3),
496+
.flags = flags,
457497
.max_transfer_sz = SPI_LL_DMA_MAX_BIT_LEN / 8
458498
};
459499

@@ -495,15 +535,15 @@ spi_host_device_t machine_hw_spi_get_host(mp_obj_t in) {
495535

496536
mp_obj_t machine_hw_spi_device_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
497537

498-
enum { ARG_spi_bus, ARG_freq, ARG_cs, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit };
538+
enum { ARG_spi_bus, ARG_freq, ARG_cs, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_quad};
499539
static const mp_arg_t allowed_args[] = {
500540
{ MP_QSTR_spi_bus, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
501541
{ MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
502542
{ MP_QSTR_cs, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
503-
{ MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
504-
{ MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
505-
{ MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
506-
{ MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MICROPY_PY_MACHINE_SPI_MSB} },
543+
{ MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
544+
{ MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
545+
{ MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
546+
{ MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MICROPY_PY_MACHINE_SPI_MSB} }
507547
};
508548

509549
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
@@ -517,7 +557,6 @@ mp_obj_t machine_hw_spi_device_make_new(const mp_obj_type_t *type, size_t n_args
517557
self->spi_bus = MP_OBJ_TO_PTR(args[ARG_spi_bus].u_obj);
518558
self->cs = mp_obj_new_int((mp_int_t)cs);
519559
self->bits = (uint8_t)args[ARG_bits].u_int;
520-
521560
self->deinit = &machine_hw_spi_device_deinit_callback;
522561

523562
spi_device_interface_config_t devcfg = {

0 commit comments

Comments
 (0)