esp32/machine_hw_spi: Use default pins when making SPI if none given.

The default pins can be optionally configured by a board.

Fixes issue #6974.

Signed-off-by: Damien George <damien@micropython.org>
pull/7011/head
Damien George 2021-03-11 13:32:00 +11:00
rodzic a62e791978
commit b24fcd7aec
2 zmienionych plików z 33 dodań i 3 usunięć

Wyświetl plik

@ -302,6 +302,7 @@ has the same methods as software SPI above::
from machine import Pin, SPI
hspi = SPI(1, 10000000)
hspi = SPI(1, 10000000, sck=Pin(14), mosi=Pin(13), miso=Pin(12))
vspi = SPI(2, baudrate=80000000, polarity=0, phase=0, bits=8, firstbit=0, sck=Pin(18), mosi=Pin(23), miso=Pin(19))

Wyświetl plik

@ -36,9 +36,29 @@
#include "driver/spi_master.h"
// Default pins for SPI(1), can be overridden by a board
#ifndef MICROPY_HW_SPI1_SCK
#define MICROPY_HW_SPI1_SCK (14)
#define MICROPY_HW_SPI1_MOSI (13)
#define MICROPY_HW_SPI1_MISO (12)
#endif
// Default pins for SPI(2), can be overridden by a board
#ifndef MICROPY_HW_SPI2_SCK
#define MICROPY_HW_SPI2_SCK (18)
#define MICROPY_HW_SPI2_MOSI (23)
#define MICROPY_HW_SPI2_MISO (19)
#endif
#define MP_HW_SPI_MAX_XFER_BYTES (4092)
#define MP_HW_SPI_MAX_XFER_BITS (MP_HW_SPI_MAX_XFER_BYTES * 8) // Has to be an even multiple of 8
typedef struct _machine_hw_spi_default_pins_t {
int8_t sck;
int8_t mosi;
int8_t miso;
} machine_hw_spi_default_pins_t;
typedef struct _machine_hw_spi_obj_t {
mp_obj_base_t base;
spi_host_device_t host;
@ -58,6 +78,12 @@ typedef struct _machine_hw_spi_obj_t {
} state;
} machine_hw_spi_obj_t;
// Default pin mappings for the hardware SPI instances
STATIC const machine_hw_spi_default_pins_t machine_hw_spi_default_pins[2] = {
{ .sck = MICROPY_HW_SPI1_SCK, .mosi = MICROPY_HW_SPI1_MOSI, .miso = MICROPY_HW_SPI1_MISO },
{ .sck = MICROPY_HW_SPI2_SCK, .mosi = MICROPY_HW_SPI2_MOSI, .miso = MICROPY_HW_SPI2_MISO },
};
// Static objects mapping to HSPI and VSPI hardware peripherals
STATIC machine_hw_spi_obj_t machine_hw_spi_obj[2];
@ -369,10 +395,13 @@ mp_obj_t machine_hw_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
machine_hw_spi_obj_t *self;
const machine_hw_spi_default_pins_t *default_pins;
if (args[ARG_id].u_int == HSPI_HOST) {
self = &machine_hw_spi_obj[0];
default_pins = &machine_hw_spi_default_pins[0];
} else {
self = &machine_hw_spi_obj[1];
default_pins = &machine_hw_spi_default_pins[1];
}
self->base.type = &machine_hw_spi_type;
@ -384,9 +413,9 @@ mp_obj_t machine_hw_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_
args[ARG_phase].u_int,
args[ARG_bits].u_int,
args[ARG_firstbit].u_int,
args[ARG_sck].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id(args[ARG_sck].u_obj),
args[ARG_mosi].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id(args[ARG_mosi].u_obj),
args[ARG_miso].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id(args[ARG_miso].u_obj));
args[ARG_sck].u_obj == MP_OBJ_NULL ? default_pins->sck : machine_pin_get_id(args[ARG_sck].u_obj),
args[ARG_mosi].u_obj == MP_OBJ_NULL ? default_pins->mosi : machine_pin_get_id(args[ARG_mosi].u_obj),
args[ARG_miso].u_obj == MP_OBJ_NULL ? default_pins->miso : machine_pin_get_id(args[ARG_miso].u_obj));
return MP_OBJ_FROM_PTR(self);
}