feat: add support for eigenmath
Some checks failed
JavaScript code lint and formatting with Biome / eslint (push) Has been cancelled
Check code formatting / code-formatting (push) Has been cancelled
Check code size / build (push) Has been cancelled
Check spelling with codespell / codespell (push) Has been cancelled
Check commit message formatting / build (push) Has been cancelled
JavaScript code lint and formatting with Biome / eslint (pull_request) Has been cancelled
Build docs / build (push) Has been cancelled
Check code formatting / code-formatting (pull_request) Has been cancelled
Check code size / build (pull_request) Has been cancelled
Check examples / embedding (push) Has been cancelled
Check spelling with codespell / codespell (pull_request) Has been cancelled
Check commit message formatting / build (pull_request) Has been cancelled
Package mpremote / build (push) Has been cancelled
Build ports metadata / build (pull_request) Has been cancelled
cc3200 port / build (pull_request) Has been cancelled
.mpy file format and tools / test (push) Has been cancelled
esp32 port / build_idf (esp32_build_cmod_spiram_s2) (pull_request) Has been cancelled
esp32 port / build_idf (esp32_build_s3_c3) (pull_request) Has been cancelled
Build ports metadata / build (push) Has been cancelled
esp8266 port / build (pull_request) Has been cancelled
mimxrt port / build (pull_request) Has been cancelled
cc3200 port / build (push) Has been cancelled
nrf port / build (pull_request) Has been cancelled
powerpc port / build (pull_request) Has been cancelled
qemu port / build_and_test_arm (pull_request) Has been cancelled
qemu port / build_and_test_rv32 (pull_request) Has been cancelled
esp32 port / build_idf (esp32_build_cmod_spiram_s2) (push) Has been cancelled
esp32 port / build_idf (esp32_build_s3_c3) (push) Has been cancelled
renesas-ra port / build_renesas_ra_board (pull_request) Has been cancelled
rp2 port / build (pull_request) Has been cancelled
samd port / build (pull_request) Has been cancelled
esp8266 port / build (push) Has been cancelled
stm32 port / build_stm32 (stm32_misc_build) (pull_request) Has been cancelled
stm32 port / build_stm32 (stm32_nucleo_build) (pull_request) Has been cancelled
stm32 port / build_stm32 (stm32_pyb_build) (pull_request) Has been cancelled
mimxrt port / build (push) Has been cancelled
unix port / minimal (pull_request) Has been cancelled
unix port / reproducible (pull_request) Has been cancelled
unix port / standard (pull_request) Has been cancelled
unix port / standard_v2 (pull_request) Has been cancelled
unix port / coverage (pull_request) Has been cancelled
unix port / coverage_32bit (pull_request) Has been cancelled
unix port / nanbox (pull_request) Has been cancelled
unix port / float (pull_request) Has been cancelled
unix port / stackless_clang (pull_request) Has been cancelled
unix port / float_clang (pull_request) Has been cancelled
unix port / settrace (pull_request) Has been cancelled
unix port / settrace_stackless (pull_request) Has been cancelled
unix port / macos (pull_request) Has been cancelled
unix port / qemu_mips (pull_request) Has been cancelled
unix port / qemu_arm (pull_request) Has been cancelled
unix port / qemu_riscv64 (pull_request) Has been cancelled
nrf port / build (push) Has been cancelled
powerpc port / build (push) Has been cancelled
qemu port / build_and_test_arm (push) Has been cancelled
qemu port / build_and_test_rv32 (push) Has been cancelled
renesas-ra port / build_renesas_ra_board (push) Has been cancelled
rp2 port / build (push) Has been cancelled
samd port / build (push) Has been cancelled
webassembly port / build (pull_request) Has been cancelled
windows port / build-vs (Debug, x64, windows-2022, dev, 2022, [17, 18)) (pull_request) Has been cancelled
windows port / build-vs (Debug, x64, windows-latest, dev, 2017, [15, 16)) (pull_request) Has been cancelled
windows port / build-vs (Debug, x86, windows-2022, dev, 2022, [17, 18)) (pull_request) Has been cancelled
windows port / build-vs (Debug, x86, windows-latest, dev, 2017, [15, 16)) (pull_request) Has been cancelled
windows port / build-vs (Release, x64, windows-2019, dev, 2019, [16, 17)) (pull_request) Has been cancelled
windows port / build-vs (Release, x64, windows-2019, standard, 2019, [16, 17)) (pull_request) Has been cancelled
windows port / build-vs (Release, x64, windows-2022, dev, 2022, [17, 18)) (pull_request) Has been cancelled
windows port / build-vs (Release, x64, windows-2022, standard, 2022, [17, 18)) (pull_request) Has been cancelled
windows port / build-vs (Release, x64, windows-latest, dev, 2017, [15, 16)) (pull_request) Has been cancelled
windows port / build-vs (Release, x64, windows-latest, standard, 2017, [15, 16)) (pull_request) Has been cancelled
windows port / build-vs (Release, x86, windows-2019, dev, 2019, [16, 17)) (pull_request) Has been cancelled
windows port / build-vs (Release, x86, windows-2019, standard, 2019, [16, 17)) (pull_request) Has been cancelled
windows port / build-vs (Release, x86, windows-2022, dev, 2022, [17, 18)) (pull_request) Has been cancelled
windows port / build-vs (Release, x86, windows-2022, standard, 2022, [17, 18)) (pull_request) Has been cancelled
windows port / build-vs (Release, x86, windows-latest, dev, 2017, [15, 16)) (pull_request) Has been cancelled
windows port / build-vs (Release, x86, windows-latest, standard, 2017, [15, 16)) (pull_request) Has been cancelled
windows port / build-mingw (i686, mingw32, dev) (pull_request) Has been cancelled
windows port / build-mingw (i686, mingw32, standard) (pull_request) Has been cancelled
windows port / build-mingw (x86_64, mingw64, dev) (pull_request) Has been cancelled
windows port / build-mingw (x86_64, mingw64, standard) (pull_request) Has been cancelled
windows port / cross-build-on-linux (pull_request) Has been cancelled
stm32 port / build_stm32 (stm32_misc_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_nucleo_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_pyb_build) (push) Has been cancelled
zephyr port / build (pull_request) Has been cancelled
rp2 port / build (PICO) (pull_request) Has been cancelled
Python code lint and formatting with ruff / ruff (pull_request) Has been cancelled
stm32 port / build (STM32F7DISC) (pull_request) Has been cancelled
unix port / build (pull_request) Has been cancelled
unix port / minimal (push) Has been cancelled
unix port / reproducible (push) Has been cancelled
unix port / standard (push) Has been cancelled
unix port / standard_v2 (push) Has been cancelled
unix port / coverage (push) Has been cancelled
unix port / coverage_32bit (push) Has been cancelled
unix port / nanbox (push) Has been cancelled
unix port / float (push) Has been cancelled
unix port / stackless_clang (push) Has been cancelled
unix port / float_clang (push) Has been cancelled
unix port / settrace (push) Has been cancelled
unix port / settrace_stackless (push) Has been cancelled
unix port / macos (push) Has been cancelled
unix port / qemu_mips (push) Has been cancelled
unix port / qemu_arm (push) Has been cancelled
unix port / qemu_riscv64 (push) Has been cancelled
webassembly port / build (push) Has been cancelled
windows port / build-vs (Debug, x64, windows-2022, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Debug, x64, windows-latest, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, x86, windows-2022, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Debug, x86, windows-latest, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-2019, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-2019, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-2022, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-2022, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-latest, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x64, windows-latest, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-2019, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-2019, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-2022, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-2022, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-latest, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x86, windows-latest, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, dev) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, standard) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, dev) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, standard) (push) Has been cancelled
windows port / cross-build-on-linux (push) Has been cancelled
zephyr port / build (push) Has been cancelled
rp2 port / build (PICO) (push) Has been cancelled
Python code lint and formatting with ruff / ruff (push) Has been cancelled
stm32 port / build (STM32F7DISC) (push) Has been cancelled
unix port / build (push) Has been cancelled

This commit is contained in:
feng-arch 2025-10-13 16:04:13 +08:00
parent 2c4811cea4
commit a7373d23f4

View File

@ -34,21 +34,21 @@
#include "hardware/spi.h" #include "hardware/spi.h"
#include "hardware/dma.h" #include "hardware/dma.h"
#define DEFAULT_SPI_BAUDRATE (1000000) #define DEFAULT_SPI_BAUDRATE (1000000)
#define DEFAULT_SPI_POLARITY (0) #define DEFAULT_SPI_POLARITY (0)
#define DEFAULT_SPI_PHASE (0) #define DEFAULT_SPI_PHASE (0)
#define DEFAULT_SPI_BITS (8) #define DEFAULT_SPI_BITS (8)
#define DEFAULT_SPI_FIRSTBIT (SPI_MSB_FIRST) #define DEFAULT_SPI_FIRSTBIT (SPI_MSB_FIRST)
#ifdef MICROPY_HW_SPI_NO_DEFAULT_PINS #ifdef MICROPY_HW_SPI_NO_DEFAULT_PINS
// With no default SPI, need to require the pin args. // With no default SPI, need to require the pin args.
#define MICROPY_HW_SPI0_SCK (0) #define MICROPY_HW_SPI0_SCK (0)
#define MICROPY_HW_SPI0_MOSI (0) #define MICROPY_HW_SPI0_MOSI (0)
#define MICROPY_HW_SPI0_MISO (0) #define MICROPY_HW_SPI0_MISO (0)
#define MICROPY_HW_SPI1_SCK (0) #define MICROPY_HW_SPI1_SCK (0)
#define MICROPY_HW_SPI1_MOSI (0) #define MICROPY_HW_SPI1_MOSI (0)
#define MICROPY_HW_SPI1_MISO (0) #define MICROPY_HW_SPI1_MISO (0)
#define MICROPY_SPI_PINS_ARG_OPTS MP_ARG_REQUIRED #define MICROPY_SPI_PINS_ARG_OPTS MP_ARG_REQUIRED
#else #else
@ -58,41 +58,40 @@
#ifndef MICROPY_HW_SPI0_SCK #ifndef MICROPY_HW_SPI0_SCK
#if PICO_DEFAULT_SPI == 0 #if PICO_DEFAULT_SPI == 0
#define MICROPY_HW_SPI0_SCK (PICO_DEFAULT_SPI_SCK_PIN) #define MICROPY_HW_SPI0_SCK (PICO_DEFAULT_SPI_SCK_PIN)
#define MICROPY_HW_SPI0_MOSI (PICO_DEFAULT_SPI_TX_PIN) #define MICROPY_HW_SPI0_MOSI (PICO_DEFAULT_SPI_TX_PIN)
#define MICROPY_HW_SPI0_MISO (PICO_DEFAULT_SPI_RX_PIN) #define MICROPY_HW_SPI0_MISO (PICO_DEFAULT_SPI_RX_PIN)
#else #else
#define MICROPY_HW_SPI0_SCK (6) #define MICROPY_HW_SPI0_SCK (6)
#define MICROPY_HW_SPI0_MOSI (7) #define MICROPY_HW_SPI0_MOSI (7)
#define MICROPY_HW_SPI0_MISO (4) #define MICROPY_HW_SPI0_MISO (4)
#endif #endif
#endif #endif
#ifndef MICROPY_HW_SPI1_SCK #ifndef MICROPY_HW_SPI1_SCK
#if PICO_DEFAULT_SPI == 1 #if PICO_DEFAULT_SPI == 1
#define MICROPY_HW_SPI1_SCK (PICO_DEFAULT_SPI_SCK_PIN) #define MICROPY_HW_SPI1_SCK (PICO_DEFAULT_SPI_SCK_PIN)
#define MICROPY_HW_SPI1_MOSI (PICO_DEFAULT_SPI_TX_PIN) #define MICROPY_HW_SPI1_MOSI (PICO_DEFAULT_SPI_TX_PIN)
#define MICROPY_HW_SPI1_MISO (PICO_DEFAULT_SPI_RX_PIN) #define MICROPY_HW_SPI1_MISO (PICO_DEFAULT_SPI_RX_PIN)
#else #else
#define MICROPY_HW_SPI1_SCK (10) #define MICROPY_HW_SPI1_SCK (10)
#define MICROPY_HW_SPI1_MOSI (11) #define MICROPY_HW_SPI1_MOSI (11)
#define MICROPY_HW_SPI1_MISO (8) #define MICROPY_HW_SPI1_MISO (8)
#endif #endif
#endif #endif
#endif #endif
// SPI0 can be GP{0..7,16..23}, SPI1 can be GP{8..15,24..29}. // SPI0 can be GP{0..7,16..23}, SPI1 can be GP{8..15,24..29}.
#define IS_VALID_PERIPH(spi, pin) ((((pin) & 8) >> 3) == (spi)) #define IS_VALID_PERIPH(spi, pin) ((((pin) & 8) >> 3) == (spi))
// GP{2,6,10,14,...} // GP{2,6,10,14,...}
#define IS_VALID_SCK(spi, pin) (((pin) & 3) == 2 && IS_VALID_PERIPH(spi, pin)) #define IS_VALID_SCK(spi, pin) (((pin) & 3) == 2 && IS_VALID_PERIPH(spi, pin))
// GP{3,7,11,15,...} // GP{3,7,11,15,...}
#define IS_VALID_MOSI(spi, pin) (((pin) & 3) == 3 && IS_VALID_PERIPH(spi, pin)) #define IS_VALID_MOSI(spi, pin) (((pin) & 3) == 3 && IS_VALID_PERIPH(spi, pin))
// GP{0,4,8,10,...} // GP{0,4,8,10,...}
#define IS_VALID_MISO(spi, pin) (((pin) & 3) == 0 && IS_VALID_PERIPH(spi, pin)) #define IS_VALID_MISO(spi, pin) (((pin) & 3) == 0 && IS_VALID_PERIPH(spi, pin))
typedef struct _machine_spi_obj_t typedef struct _machine_spi_obj_t {
{
mp_obj_base_t base; mp_obj_base_t base;
spi_inst_t *const spi_inst; spi_inst_t *const spi_inst;
uint8_t spi_id; uint8_t spi_id;
@ -112,16 +111,9 @@ typedef struct _machine_spi_obj_t
static machine_spi_obj_t machine_spi_obj[] = { static machine_spi_obj_t machine_spi_obj[] = {
{ {
{&machine_spi_type}, {&machine_spi_type}, spi0, 0,
spi0, DEFAULT_SPI_POLARITY, DEFAULT_SPI_PHASE, DEFAULT_SPI_BITS, DEFAULT_SPI_FIRSTBIT,
0, MICROPY_HW_SPI0_SCK, MICROPY_HW_SPI0_MOSI, MICROPY_HW_SPI0_MISO,
DEFAULT_SPI_POLARITY,
DEFAULT_SPI_PHASE,
DEFAULT_SPI_BITS,
DEFAULT_SPI_FIRSTBIT,
MICROPY_HW_SPI0_SCK,
MICROPY_HW_SPI0_MOSI,
MICROPY_HW_SPI0_MISO,
0, 0,
// DMA channels, -1 means not claimed // DMA channels, -1 means not claimed
-1, -1,
@ -129,16 +121,9 @@ static machine_spi_obj_t machine_spi_obj[] = {
NULL NULL
}, },
{ {
{&machine_spi_type}, {&machine_spi_type}, spi1, 1,
spi1, DEFAULT_SPI_POLARITY, DEFAULT_SPI_PHASE, DEFAULT_SPI_BITS, DEFAULT_SPI_FIRSTBIT,
1, MICROPY_HW_SPI1_SCK, MICROPY_HW_SPI1_MOSI, MICROPY_HW_SPI1_MISO,
DEFAULT_SPI_POLARITY,
DEFAULT_SPI_PHASE,
DEFAULT_SPI_BITS,
DEFAULT_SPI_FIRSTBIT,
MICROPY_HW_SPI1_SCK,
MICROPY_HW_SPI1_MOSI,
MICROPY_HW_SPI1_MISO,
0, 0,
// DMA channels, -1 means not claimed // DMA channels, -1 means not claimed
-1, -1,
@ -147,38 +132,25 @@ static machine_spi_obj_t machine_spi_obj[] = {
}, },
}; };
static void machine_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) static void machine_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
{
machine_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); machine_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_printf(print, "SPI(%u, baudrate=%u, polarity=%u, phase=%u, bits=%u, sck=%u, mosi=%u, miso=%u)", mp_printf(print, "SPI(%u, baudrate=%u, polarity=%u, phase=%u, bits=%u, sck=%u, mosi=%u, miso=%u)",
self->spi_id, self->baudrate, self->polarity, self->phase, self->bits, self->spi_id, self->baudrate, self->polarity, self->phase, self->bits,
self->sck, self->mosi, self->miso); self->sck, self->mosi, self->miso);
} }
mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
{ enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
enum
{
ARG_id,
ARG_baudrate,
ARG_polarity,
ARG_phase,
ARG_bits,
ARG_firstbit,
ARG_sck,
ARG_mosi,
ARG_miso
};
static const mp_arg_t allowed_args[] = { static const mp_arg_t allowed_args[] = {
{MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ}, { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ },
{MP_QSTR_baudrate, MP_ARG_INT, {.u_int = DEFAULT_SPI_BAUDRATE}}, { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = DEFAULT_SPI_BAUDRATE} },
{MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_POLARITY}}, { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_POLARITY} },
{MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_PHASE}}, { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_PHASE} },
{MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_BITS}}, { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_BITS} },
{MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_FIRSTBIT}}, { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_FIRSTBIT} },
{MP_QSTR_sck, MICROPY_SPI_PINS_ARG_OPTS | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE}}, { MP_QSTR_sck, MICROPY_SPI_PINS_ARG_OPTS | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
{MP_QSTR_mosi, MICROPY_SPI_PINS_ARG_OPTS | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE}}, { MP_QSTR_mosi, MICROPY_SPI_PINS_ARG_OPTS | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
{MP_QSTR_miso, MICROPY_SPI_PINS_ARG_OPTS | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE}}, { MP_QSTR_miso, MICROPY_SPI_PINS_ARG_OPTS | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
}; };
// Parse the arguments. // Parse the arguments.
@ -187,8 +159,7 @@ mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
// Get the SPI bus id. // Get the SPI bus id.
int spi_id = mp_obj_get_int(args[ARG_id].u_obj); int spi_id = mp_obj_get_int(args[ARG_id].u_obj);
if (spi_id < 0 || spi_id >= MP_ARRAY_SIZE(machine_spi_obj)) if (spi_id < 0 || spi_id >= MP_ARRAY_SIZE(machine_spi_obj)) {
{
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) doesn't exist"), spi_id); mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) doesn't exist"), spi_id);
} }
@ -196,31 +167,25 @@ mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
machine_spi_obj_t *self = (machine_spi_obj_t *)&machine_spi_obj[spi_id]; machine_spi_obj_t *self = (machine_spi_obj_t *)&machine_spi_obj[spi_id];
// Set SCK/MOSI/MISO pins if configured. // Set SCK/MOSI/MISO pins if configured.
if (args[ARG_sck].u_obj != mp_const_none) if (args[ARG_sck].u_obj != mp_const_none) {
{
int sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj); int sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj);
if (!IS_VALID_SCK(self->spi_id, sck)) if (!IS_VALID_SCK(self->spi_id, sck)) {
{
mp_raise_ValueError(MP_ERROR_TEXT("bad SCK pin")); mp_raise_ValueError(MP_ERROR_TEXT("bad SCK pin"));
} }
self->sck = sck; self->sck = sck;
} }
if (args[ARG_mosi].u_obj != mp_const_none) if (args[ARG_mosi].u_obj != mp_const_none) {
{
int mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj); int mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj);
if (!IS_VALID_MOSI(self->spi_id, mosi)) if (!IS_VALID_MOSI(self->spi_id, mosi)) {
{
mp_raise_ValueError(MP_ERROR_TEXT("bad MOSI pin")); mp_raise_ValueError(MP_ERROR_TEXT("bad MOSI pin"));
} }
self->mosi = mosi; self->mosi = mosi;
if (self->chan_tx < 0) if (self->chan_tx < 0)
self->chan_tx = dma_claim_unused_channel(true); self->chan_tx = dma_claim_unused_channel(true);
} }
if (args[ARG_miso].u_obj != mp_const_none) if (args[ARG_miso].u_obj != mp_const_none) {
{
int miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj); int miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj);
if (!IS_VALID_MISO(self->spi_id, miso)) if (!IS_VALID_MISO(self->spi_id, miso)) {
{
mp_raise_ValueError(MP_ERROR_TEXT("bad MISO pin")); mp_raise_ValueError(MP_ERROR_TEXT("bad MISO pin"));
} }
self->miso = miso; self->miso = miso;
@ -229,15 +194,13 @@ mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
} }
// Initialise the SPI peripheral if any arguments given, or it was not initialised previously. // Initialise the SPI peripheral if any arguments given, or it was not initialised previously.
if (n_args > 1 || n_kw > 0 || self->baudrate == 0) if (n_args > 1 || n_kw > 0 || self->baudrate == 0) {
{
self->baudrate = args[ARG_baudrate].u_int; self->baudrate = args[ARG_baudrate].u_int;
self->polarity = args[ARG_polarity].u_int; self->polarity = args[ARG_polarity].u_int;
self->phase = args[ARG_phase].u_int; self->phase = args[ARG_phase].u_int;
self->bits = args[ARG_bits].u_int; self->bits = args[ARG_bits].u_int;
self->firstbit = args[ARG_firstbit].u_int; self->firstbit = args[ARG_firstbit].u_int;
if (self->firstbit == SPI_LSB_FIRST) if (self->firstbit == SPI_LSB_FIRST) {
{
mp_raise_NotImplementedError(MP_ERROR_TEXT("LSB")); mp_raise_NotImplementedError(MP_ERROR_TEXT("LSB"));
} }
@ -252,22 +215,14 @@ mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
return MP_OBJ_FROM_PTR(self); return MP_OBJ_FROM_PTR(self);
} }
static void machine_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) static void machine_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
{ enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit };
enum
{
ARG_baudrate,
ARG_polarity,
ARG_phase,
ARG_bits,
ARG_firstbit
};
static const mp_arg_t allowed_args[] = { static const mp_arg_t allowed_args[] = {
{MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1}}, { MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
{MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1}}, { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
{MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1}}, { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
{MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1}}, { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
{MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1}}, { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
}; };
// Parse the arguments. // Parse the arguments.
@ -276,46 +231,38 @@ static void machine_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// Reconfigure the baudrate if requested. // Reconfigure the baudrate if requested.
if (args[ARG_baudrate].u_int != -1) if (args[ARG_baudrate].u_int != -1) {
{
self->baudrate = spi_set_baudrate(self->spi_inst, args[ARG_baudrate].u_int); self->baudrate = spi_set_baudrate(self->spi_inst, args[ARG_baudrate].u_int);
} }
// Reconfigure the format if requested. // Reconfigure the format if requested.
bool set_format = false; bool set_format = false;
if (args[ARG_polarity].u_int != -1) if (args[ARG_polarity].u_int != -1) {
{
self->polarity = args[ARG_polarity].u_int; self->polarity = args[ARG_polarity].u_int;
set_format = true; set_format = true;
} }
if (args[ARG_phase].u_int != -1) if (args[ARG_phase].u_int != -1) {
{
self->phase = args[ARG_phase].u_int; self->phase = args[ARG_phase].u_int;
set_format = true; set_format = true;
} }
if (args[ARG_bits].u_int != -1) if (args[ARG_bits].u_int != -1) {
{
self->bits = args[ARG_bits].u_int; self->bits = args[ARG_bits].u_int;
set_format = true; set_format = true;
} }
if (args[ARG_firstbit].u_int != -1) if (args[ARG_firstbit].u_int != -1) {
{
self->firstbit = args[ARG_firstbit].u_int; self->firstbit = args[ARG_firstbit].u_int;
if (self->firstbit == SPI_LSB_FIRST) if (self->firstbit == SPI_LSB_FIRST)
{ {
mp_raise_NotImplementedError(MP_ERROR_TEXT("LSB")); mp_raise_NotImplementedError(MP_ERROR_TEXT("LSB"));
} }
} }
if (self->miso > 0 && self->chan_rx < 0) if (self->miso > 0 && self->chan_rx < 0) {
{
self->chan_rx = dma_claim_unused_channel(true); self->chan_rx = dma_claim_unused_channel(true);
} }
if (self->mosi > 0 && self->chan_tx < 0) if (self->mosi > 0 && self->chan_tx < 0) {
{
self->chan_tx = dma_claim_unused_channel(true); self->chan_tx = dma_claim_unused_channel(true);
} }
if (set_format) if (set_format) {
{
spi_set_format(self->spi_inst, self->bits, self->polarity, self->phase, self->firstbit); spi_set_format(self->spi_inst, self->bits, self->polarity, self->phase, self->firstbit);
} }
} }
@ -429,8 +376,7 @@ static void machine_spi_wait_done(mp_obj_base_t *self_in)
} }
} }
static void machine_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) static void machine_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
{
machine_spi_obj_t *self = (machine_spi_obj_t *)self_in; machine_spi_obj_t *self = (machine_spi_obj_t *)self_in;
bool write_only = dest == NULL; bool write_only = dest == NULL;
const size_t dma_min_size_threshold = 8; const size_t dma_min_size_threshold = 8;
@ -509,8 +455,7 @@ static void machine_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8
// Buffer protocol implementation for SPI. // Buffer protocol implementation for SPI.
// The buffer represents the SPI data FIFO. // The buffer represents the SPI data FIFO.
static mp_int_t machine_spi_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) static mp_int_t machine_spi_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
{
machine_spi_obj_t *self = MP_OBJ_TO_PTR(o_in); machine_spi_obj_t *self = MP_OBJ_TO_PTR(o_in);
bufinfo->len = 4; bufinfo->len = 4;
@ -527,8 +472,6 @@ static const mp_machine_spi_p_t machine_spi_p = {
.set_tx_isr = machine_spi_set_tx_isr, .set_tx_isr = machine_spi_set_tx_isr,
}; };
MP_DEFINE_CONST_OBJ_TYPE( MP_DEFINE_CONST_OBJ_TYPE(
machine_spi_type, machine_spi_type,
MP_QSTR_SPI, MP_QSTR_SPI,
@ -537,22 +480,19 @@ MP_DEFINE_CONST_OBJ_TYPE(
print, machine_spi_print, print, machine_spi_print,
protocol, &machine_spi_p, protocol, &machine_spi_p,
buffer, machine_spi_get_buffer, buffer, machine_spi_get_buffer,
locals_dict, &mp_machine_spi_locals_dict); locals_dict, &mp_machine_spi_locals_dict
);
mp_obj_base_t *mp_hal_get_spi_obj(mp_obj_t o) mp_obj_base_t *mp_hal_get_spi_obj(mp_obj_t o) {
{ if (mp_obj_is_type(o, &machine_spi_type)) {
if (mp_obj_is_type(o, &machine_spi_type))
{
return MP_OBJ_TO_PTR(o); return MP_OBJ_TO_PTR(o);
} }
#if MICROPY_PY_MACHINE_SOFTSPI #if MICROPY_PY_MACHINE_SOFTSPI
else if (mp_obj_is_type(o, &mp_machine_soft_spi_type)) else if (mp_obj_is_type(o, &mp_machine_soft_spi_type)) {
{
return MP_OBJ_TO_PTR(o); return MP_OBJ_TO_PTR(o);
} }
#endif #endif
else else {
{
mp_raise_TypeError(MP_ERROR_TEXT("expecting an SPI object")); mp_raise_TypeError(MP_ERROR_TEXT("expecting an SPI object"));
} }
} }