Skip to content

Commit edcb67f

Browse files
committed
SPI module updated, some bugs fixed.
1 parent 04aea86 commit edcb67f

File tree

3 files changed

+48
-957
lines changed

3 files changed

+48
-957
lines changed

MicroPython_BUILD/components/micropython/esp32/machine_hw_spi.c

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,32 @@ STATIC void machine_hw_spi_init_internal(
174174
}
175175

176176
if (bus_state > 0) {
177-
// SPI bus is free
177+
gpio_pad_select_gpio(miso);
178+
gpio_pad_select_gpio(mosi);
179+
gpio_pad_select_gpio(sck);
180+
gpio_set_direction(miso, GPIO_MODE_INPUT);
181+
gpio_set_pull_mode(miso, GPIO_PULLUP_ONLY);
182+
gpio_set_direction(mosi, GPIO_MODE_OUTPUT);
183+
gpio_set_direction(sck, GPIO_MODE_OUTPUT);
184+
// SPI bus is free
178185
MPy_SPIbus[self->host]->sclk_io_num = sck;
179186
MPy_SPIbus[self->host]->mosi_io_num = mosi;
180187
MPy_SPIbus[self->host]->miso_io_num = miso;
181188
MPy_SPIbus[self->host]->quadwp_io_num = -1;
182189
MPy_SPIbus[self->host]->quadhd_io_num = -1;
183190
MPy_SPIbus[self->host]->max_transfer_sz = 4096;
191+
// Initialize the spi bus
192+
ret=spi_bus_initialize(host, MPy_SPIbus[self->host], 1);
193+
if (ret != ESP_OK) {
194+
mp_raise_msg(&mp_type_OSError, "error initializing spi bus");
195+
return;
196+
}
184197
}
185198

186199
if ((cs > -1) && (cs != self->spi.cs)) {
187200
gpio_pad_select_gpio(cs);
201+
gpio_set_direction(cs, GPIO_MODE_OUTPUT);
202+
gpio_set_level(cs, 1);
188203
self->spi.cs = cs;
189204
changed |= 0x0400;
190205
}
@@ -206,9 +221,9 @@ STATIC void machine_hw_spi_init_internal(
206221
self->devcfg.flags = ((self->firstbit == MICROPY_PY_MACHINE_SPI_LSB) ? SPI_DEVICE_TXBIT_LSBFIRST | SPI_DEVICE_RXBIT_LSBFIRST : 0);
207222
if (!self->duplex) self->devcfg.flags |= SPI_DEVICE_HALFDUPLEX;
208223
self->devcfg.pre_cb = NULL;
224+
self->devcfg.queue_size = 1;
209225

210-
// Add device to spi bus
211-
226+
// Add device to spi bus
212227
ret = spi_bus_add_device(self->host, &self->devcfg, &self->spi.handle);
213228

214229
if (ret == ESP_OK) {
@@ -276,7 +291,7 @@ STATIC mp_obj_t machine_hw_spi_init(mp_uint_t n_args, const mp_obj_t *pos_args,
276291
{ MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
277292
{ MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
278293
{ MP_QSTR_cs, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
279-
{ MP_QSTR_duplex, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_bool = -1} },
294+
{ MP_QSTR_duplex, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
280295
{ MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
281296
};
282297

@@ -358,7 +373,7 @@ STATIC mp_obj_t mp_machine_spi_read(size_t n_args, const mp_obj_t *args)
358373

359374
t.rxlength = vstr.len * 8;
360375
t.rx_buffer = vstr.buf;
361-
if (n_args == 3) {
376+
if ((self->duplex) && (n_args == 3)) {
362377
t.length = vstr.len * 8;
363378
t.tx_buffer = vstr.buf;
364379
}
@@ -505,6 +520,30 @@ STATIC mp_obj_t mp_machine_spi_read_from_mem(mp_uint_t n_args, const mp_obj_t *p
505520
}
506521
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mp_machine_spi_read_from_mem_obj, 0, mp_machine_spi_read_from_mem);
507522

523+
//-----------------------------------------------------
524+
STATIC mp_obj_t mp_machine_spi_select(mp_obj_t self_in)
525+
{
526+
machine_hw_spi_obj_t *self = self_in;
527+
528+
esp_err_t ret = spi_device_select(&self->spi, 0);
529+
530+
if (ret == ESP_OK) return mp_const_true;
531+
return mp_const_false;
532+
}
533+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_machine_spi_select_obj, mp_machine_spi_select);
534+
535+
//-------------------------------------------------------
536+
STATIC mp_obj_t mp_machine_spi_deselect(mp_obj_t self_in)
537+
{
538+
machine_hw_spi_obj_t *self = self_in;
539+
540+
esp_err_t ret = spi_device_deselect(&self->spi);
541+
542+
if (ret == ESP_OK) return mp_const_true;
543+
return mp_const_false;
544+
}
545+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_machine_spi_deselect_obj, mp_machine_spi_deselect);
546+
508547

509548
//================================================================
510549
STATIC const mp_rom_map_elem_t machine_spi_locals_dict_table[] = {
@@ -515,6 +554,8 @@ STATIC const mp_rom_map_elem_t machine_spi_locals_dict_table[] = {
515554
{ MP_ROM_QSTR(MP_QSTR_readfrom_mem), (mp_obj_t)&mp_machine_spi_read_from_mem_obj },
516555
{ MP_ROM_QSTR(MP_QSTR_write), (mp_obj_t)&mp_machine_spi_write_obj },
517556
{ MP_ROM_QSTR(MP_QSTR_write_readinto), (mp_obj_t)&mp_machine_spi_write_readinto_obj },
557+
{ MP_ROM_QSTR(MP_QSTR_select), (mp_obj_t)&mp_machine_spi_select_obj },
558+
{ MP_ROM_QSTR(MP_QSTR_deselect), (mp_obj_t)&mp_machine_spi_deselect_obj },
518559

519560
{ MP_ROM_QSTR(MP_QSTR_MSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPI_MSB) },
520561
{ MP_ROM_QSTR(MP_QSTR_LSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPI_LSB) },

0 commit comments

Comments
 (0)