[feat]: rp2040芯片, 在machine_spi中添加DMA支持,注意:SPI.write函数会直接返回,这意味着你不能在调用SPI.write后立即变更CS引脚的状态

This commit is contained in:
feng-arch 2025-09-19 10:15:16 +08:00
parent 233ef84925
commit 54b2eb0def
8 changed files with 19199 additions and 210 deletions

View File

@ -36,7 +36,8 @@
/******************************************************************************/
// MicroPython bindings for generic machine.SPI
static mp_obj_t machine_spi_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
static mp_obj_t machine_spi_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args)
{
mp_obj_base_t *s = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t *)MP_OBJ_TYPE_GET_SLOT(s->type, protocol);
spi_p->init(s, n_args - 1, args + 1, kw_args);
@ -44,23 +45,39 @@ static mp_obj_t machine_spi_init(size_t n_args, const mp_obj_t *args, mp_map_t *
}
static MP_DEFINE_CONST_FUN_OBJ_KW(machine_spi_init_obj, 1, machine_spi_init);
static mp_obj_t machine_spi_deinit(mp_obj_t self) {
static mp_obj_t machine_spi_deinit(mp_obj_t self)
{
mp_obj_base_t *s = (mp_obj_base_t *)MP_OBJ_TO_PTR(self);
mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t *)MP_OBJ_TYPE_GET_SLOT(s->type, protocol);
if (spi_p->deinit != NULL) {
if (spi_p->deinit != NULL)
{
spi_p->deinit(s);
}
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(machine_spi_deinit_obj, machine_spi_deinit);
static void mp_machine_spi_transfer(mp_obj_t self, size_t len, const void *src, void *dest) {
static void mp_machine_spi_transfer(mp_obj_t self, size_t len, const void *src, void *dest)
{
mp_obj_base_t *s = (mp_obj_base_t *)MP_OBJ_TO_PTR(self);
mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t *)MP_OBJ_TYPE_GET_SLOT(s->type, protocol);
spi_p->transfer(s, len, src, dest);
}
static mp_obj_t mp_machine_spi_read(size_t n_args, const mp_obj_t *args) {
static mp_obj_t mp_machine_spi_wait_done(size_t n_args, const mp_obj_t *args)
{
mp_obj_base_t *s = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t *)MP_OBJ_TYPE_GET_SLOT(s->type, protocol);
if (spi_p->wait_done != NULL)
{
spi_p->wait_done(s);
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_wait_done_obj, 1, 2, mp_machine_spi_wait_done);
static mp_obj_t mp_machine_spi_read(size_t n_args, const mp_obj_t *args)
{
vstr_t vstr;
vstr_init_len(&vstr, mp_obj_get_int(args[1]));
memset(vstr.buf, n_args == 3 ? mp_obj_get_int(args[2]) : 0, vstr.len);
@ -69,7 +86,8 @@ static mp_obj_t mp_machine_spi_read(size_t n_args, const mp_obj_t *args) {
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_read_obj, 2, 3, mp_machine_spi_read);
static mp_obj_t mp_machine_spi_readinto(size_t n_args, const mp_obj_t *args) {
static mp_obj_t mp_machine_spi_readinto(size_t n_args, const mp_obj_t *args)
{
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
memset(bufinfo.buf, n_args == 3 ? mp_obj_get_int(args[2]) : 0, bufinfo.len);
@ -78,7 +96,8 @@ static mp_obj_t mp_machine_spi_readinto(size_t n_args, const mp_obj_t *args) {
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_readinto_obj, 2, 3, mp_machine_spi_readinto);
static mp_obj_t mp_machine_spi_write(mp_obj_t self, mp_obj_t wr_buf) {
static mp_obj_t mp_machine_spi_write(mp_obj_t self, mp_obj_t wr_buf)
{
mp_buffer_info_t src;
mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
mp_machine_spi_transfer(self, src.len, (const uint8_t *)src.buf, NULL);
@ -86,12 +105,14 @@ static mp_obj_t mp_machine_spi_write(mp_obj_t self, mp_obj_t wr_buf) {
}
MP_DEFINE_CONST_FUN_OBJ_2(mp_machine_spi_write_obj, mp_machine_spi_write);
static mp_obj_t mp_machine_spi_write_readinto(mp_obj_t self, mp_obj_t wr_buf, mp_obj_t rd_buf) {
static mp_obj_t mp_machine_spi_write_readinto(mp_obj_t self, mp_obj_t wr_buf, mp_obj_t rd_buf)
{
mp_buffer_info_t src;
mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
mp_buffer_info_t dest;
mp_get_buffer_raise(rd_buf, &dest, MP_BUFFER_WRITE);
if (src.len != dest.len) {
if (src.len != dest.len)
{
mp_raise_ValueError(MP_ERROR_TEXT("buffers must be the same length"));
}
mp_machine_spi_transfer(self, src.len, src.buf, dest.buf);
@ -100,15 +121,16 @@ static mp_obj_t mp_machine_spi_write_readinto(mp_obj_t self, mp_obj_t wr_buf, mp
MP_DEFINE_CONST_FUN_OBJ_3(mp_machine_spi_write_readinto_obj, mp_machine_spi_write_readinto);
static const mp_rom_map_elem_t machine_spi_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_spi_init_obj) },
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_spi_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_machine_spi_read_obj) },
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_machine_spi_readinto_obj) },
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_machine_spi_write_obj) },
{ MP_ROM_QSTR(MP_QSTR_write_readinto), MP_ROM_PTR(&mp_machine_spi_write_readinto_obj) },
{MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_spi_init_obj)},
{MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_spi_deinit_obj)},
{MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_machine_spi_read_obj)},
{MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_machine_spi_readinto_obj)},
{MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_machine_spi_write_obj)},
{MP_ROM_QSTR(MP_QSTR_write_readinto), MP_ROM_PTR(&mp_machine_spi_write_readinto_obj)},
{MP_ROM_QSTR(MP_QSTR_wait_done), MP_ROM_PTR(&mp_machine_spi_wait_done_obj)},
{ MP_ROM_QSTR(MP_QSTR_MSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPI_MSB) },
{ MP_ROM_QSTR(MP_QSTR_LSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPI_LSB) },
{MP_ROM_QSTR(MP_QSTR_MSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPI_MSB)},
{MP_ROM_QSTR(MP_QSTR_LSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPI_LSB)},
};
MP_DEFINE_CONST_DICT(mp_machine_spi_locals_dict, machine_spi_locals_dict_table);
@ -119,34 +141,42 @@ MP_DEFINE_CONST_DICT(mp_machine_spi_locals_dict, machine_spi_locals_dict_table);
#if MICROPY_PY_MACHINE_SOFTSPI
static uint32_t baudrate_from_delay_half(uint32_t delay_half) {
#ifdef MICROPY_HW_SOFTSPI_MIN_DELAY
if (delay_half == MICROPY_HW_SOFTSPI_MIN_DELAY) {
static uint32_t baudrate_from_delay_half(uint32_t delay_half)
{
#ifdef MICROPY_HW_SOFTSPI_MIN_DELAY
if (delay_half == MICROPY_HW_SOFTSPI_MIN_DELAY)
{
return MICROPY_HW_SOFTSPI_MAX_BAUDRATE;
} else
#endif
}
else
#endif
{
return 500000 / delay_half;
}
}
static uint32_t baudrate_to_delay_half(uint32_t baudrate) {
#ifdef MICROPY_HW_SOFTSPI_MIN_DELAY
if (baudrate >= MICROPY_HW_SOFTSPI_MAX_BAUDRATE) {
static uint32_t baudrate_to_delay_half(uint32_t baudrate)
{
#ifdef MICROPY_HW_SOFTSPI_MIN_DELAY
if (baudrate >= MICROPY_HW_SOFTSPI_MAX_BAUDRATE)
{
return MICROPY_HW_SOFTSPI_MIN_DELAY;
} else
#endif
}
else
#endif
{
uint32_t delay_half = 500000 / baudrate;
// round delay_half up so that: actual_baudrate <= requested_baudrate
if (500000 % baudrate != 0) {
if (500000 % baudrate != 0)
{
delay_half += 1;
}
return delay_half;
}
}
static void mp_machine_soft_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
static void mp_machine_soft_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind)
{
mp_machine_soft_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_printf(print, "SoftSPI(baudrate=%u, polarity=%u, phase=%u, firstbit=%u,"
" sck=" MP_HAL_PIN_FMT ", mosi=" MP_HAL_PIN_FMT ", miso=" MP_HAL_PIN_FMT ")",
@ -154,17 +184,28 @@ static void mp_machine_soft_spi_print(const mp_print_t *print, mp_obj_t self_in,
mp_hal_pin_name(self->spi.sck), mp_hal_pin_name(self->spi.mosi), mp_hal_pin_name(self->spi.miso));
}
static mp_obj_t mp_machine_soft_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_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
static mp_obj_t mp_machine_soft_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_baudrate,
ARG_polarity,
ARG_phase,
ARG_bits,
ARG_firstbit,
ARG_sck,
ARG_mosi,
ARG_miso
};
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 500000} },
{ MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
{ MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MICROPY_PY_MACHINE_SPI_MSB} },
{ MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 500000}},
{MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0}},
{MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0}},
{MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8}},
{MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MICROPY_PY_MACHINE_SPI_MSB}},
{MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
{MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
{MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@ -176,13 +217,13 @@ static mp_obj_t mp_machine_soft_spi_make_new(const mp_obj_type_t *type, size_t n
self->spi.delay_half = baudrate_to_delay_half(args[ARG_baudrate].u_int);
self->spi.polarity = args[ARG_polarity].u_int;
self->spi.phase = args[ARG_phase].u_int;
if (args[ARG_bits].u_int != 8) {
if (args[ARG_bits].u_int != 8)
{
mp_raise_ValueError(MP_ERROR_TEXT("bits must be 8"));
}
self->spi.firstbit = args[ARG_firstbit].u_int;
if (args[ARG_sck].u_obj == MP_OBJ_NULL
|| args[ARG_mosi].u_obj == MP_OBJ_NULL
|| args[ARG_miso].u_obj == MP_OBJ_NULL) {
if (args[ARG_sck].u_obj == MP_OBJ_NULL || args[ARG_mosi].u_obj == MP_OBJ_NULL || args[ARG_miso].u_obj == MP_OBJ_NULL)
{
mp_raise_ValueError(MP_ERROR_TEXT("must specify all of sck/mosi/miso"));
}
self->spi.sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj);
@ -195,41 +236,58 @@ static mp_obj_t mp_machine_soft_spi_make_new(const mp_obj_type_t *type, size_t n
return MP_OBJ_FROM_PTR(self);
}
static void mp_machine_soft_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 mp_machine_soft_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args)
{
mp_machine_soft_spi_obj_t *self = (mp_machine_soft_spi_obj_t *)self_in;
enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
enum
{
ARG_baudrate,
ARG_polarity,
ARG_phase,
ARG_firstbit,
ARG_sck,
ARG_mosi,
ARG_miso
};
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_polarity, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_phase, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1}},
{MP_QSTR_polarity, MP_ARG_INT, {.u_int = -1}},
{MP_QSTR_phase, MP_ARG_INT, {.u_int = -1}},
{MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1}},
{MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
{MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
{MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
if (args[ARG_baudrate].u_int != -1) {
if (args[ARG_baudrate].u_int != -1)
{
self->spi.delay_half = baudrate_to_delay_half(args[ARG_baudrate].u_int);
}
if (args[ARG_polarity].u_int != -1) {
if (args[ARG_polarity].u_int != -1)
{
self->spi.polarity = args[ARG_polarity].u_int;
}
if (args[ARG_phase].u_int != -1) {
if (args[ARG_phase].u_int != -1)
{
self->spi.phase = args[ARG_phase].u_int;
}
if (args[ARG_firstbit].u_int != -1) {
if (args[ARG_firstbit].u_int != -1)
{
self->spi.firstbit = args[ARG_firstbit].u_int;
}
if (args[ARG_sck].u_obj != MP_OBJ_NULL) {
if (args[ARG_sck].u_obj != MP_OBJ_NULL)
{
self->spi.sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj);
}
if (args[ARG_mosi].u_obj != MP_OBJ_NULL) {
if (args[ARG_mosi].u_obj != MP_OBJ_NULL)
{
self->spi.mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj);
}
if (args[ARG_miso].u_obj != MP_OBJ_NULL) {
if (args[ARG_miso].u_obj != MP_OBJ_NULL)
{
self->spi.miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj);
}
@ -237,7 +295,8 @@ static void mp_machine_soft_spi_init(mp_obj_base_t *self_in, size_t n_args, cons
mp_soft_spi_ioctl(&self->spi, MP_SPI_IOCTL_INIT);
}
static void mp_machine_soft_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
static void mp_machine_soft_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest)
{
mp_machine_soft_spi_obj_t *self = (mp_machine_soft_spi_obj_t *)self_in;
mp_soft_spi_transfer(&self->spi, len, src, dest);
}
@ -255,7 +314,6 @@ MP_DEFINE_CONST_OBJ_TYPE(
make_new, mp_machine_soft_spi_make_new,
print, mp_machine_soft_spi_print,
protocol, &mp_machine_soft_spi_p,
locals_dict, &mp_machine_spi_locals_dict
);
locals_dict, &mp_machine_spi_locals_dict);
#endif // MICROPY_PY_MACHINE_SOFTSPI

View File

@ -90,10 +90,13 @@
// Temporary support for legacy construction of SoftI2C via I2C type.
#define MP_MACHINE_I2C_CHECK_FOR_LEGACY_SOFTI2C_CONSTRUCTION(n_args, n_kw, all_args) \
do { \
if (n_args == 0 || all_args[0] == MP_OBJ_NEW_SMALL_INT(-1)) { \
do \
{ \
if (n_args == 0 || all_args[0] == MP_OBJ_NEW_SMALL_INT(-1)) \
{ \
mp_print_str(MICROPY_ERROR_PRINTER, "Warning: I2C(-1, ...) is deprecated, use SoftI2C(...) instead\n"); \
if (n_args != 0) { \
if (n_args != 0) \
{ \
--n_args; \
++all_args; \
} \
@ -103,10 +106,13 @@
// Temporary support for legacy construction of SoftSPI via SPI type.
#define MP_MACHINE_SPI_CHECK_FOR_LEGACY_SOFTSPI_CONSTRUCTION(n_args, n_kw, all_args) \
do { \
if (n_args == 0 || all_args[0] == MP_OBJ_NEW_SMALL_INT(-1)) { \
do \
{ \
if (n_args == 0 || all_args[0] == MP_OBJ_NEW_SMALL_INT(-1)) \
{ \
mp_print_str(MICROPY_ERROR_PRINTER, "Warning: SPI(-1, ...) is deprecated, use SoftSPI(...) instead\n"); \
if (n_args != 0) { \
if (n_args != 0) \
{ \
--n_args; \
++all_args; \
} \
@ -134,14 +140,16 @@ typedef struct _machine_pwm_obj_t machine_pwm_obj_t;
typedef struct _machine_uart_obj_t machine_uart_obj_t;
typedef struct _machine_wdt_obj_t machine_wdt_obj_t;
typedef struct _machine_mem_obj_t {
typedef struct _machine_mem_obj_t
{
mp_obj_base_t base;
unsigned elem_size; // in bytes
} machine_mem_obj_t;
#if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_SOFTI2C
typedef struct _mp_machine_i2c_buf_t {
typedef struct _mp_machine_i2c_buf_t
{
size_t len;
uint8_t *buf;
} mp_machine_i2c_buf_t;
@ -151,10 +159,11 @@ typedef struct _mp_machine_i2c_buf_t {
// - start/stop/read/write can be NULL, meaning operation is not supported
// - transfer must be non-NULL
// - transfer_single only needs to be set if transfer=mp_machine_i2c_transfer_adaptor
typedef struct _mp_machine_i2c_p_t {
#if MICROPY_PY_MACHINE_I2C_TRANSFER_WRITE1
typedef struct _mp_machine_i2c_p_t
{
#if MICROPY_PY_MACHINE_I2C_TRANSFER_WRITE1
bool transfer_supports_write1;
#endif
#endif
void (*init)(mp_obj_base_t *obj, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
int (*start)(mp_obj_base_t *obj);
int (*stop)(mp_obj_base_t *obj);
@ -165,7 +174,8 @@ typedef struct _mp_machine_i2c_p_t {
} mp_machine_i2c_p_t;
// SoftI2C object.
typedef struct _mp_machine_soft_i2c_obj_t {
typedef struct _mp_machine_soft_i2c_obj_t
{
mp_obj_base_t base;
uint32_t us_delay;
uint32_t us_timeout;
@ -178,14 +188,17 @@ typedef struct _mp_machine_soft_i2c_obj_t {
#if MICROPY_PY_MACHINE_SPI || MICROPY_PY_MACHINE_SOFTSPI
// SPI protocol.
typedef struct _mp_machine_spi_p_t {
typedef struct _mp_machine_spi_p_t
{
void (*init)(mp_obj_base_t *obj, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
void (*deinit)(mp_obj_base_t *obj); // can be NULL
void (*transfer)(mp_obj_base_t *obj, size_t len, const uint8_t *src, uint8_t *dest);
void (*wait_done)(mp_obj_base_t *obj); // can be NULL
} mp_machine_spi_p_t;
// SoftSPI object.
typedef struct _mp_machine_soft_spi_obj_t {
typedef struct _mp_machine_soft_spi_obj_t
{
mp_obj_base_t base;
mp_soft_spi_obj_t spi;
} mp_machine_soft_spi_obj_t;

14
make_and_flash_rp2.sh Executable file
View File

@ -0,0 +1,14 @@
make -j -C ports/rp2 BOARD=RPI_PICO USER_C_MODULES=../../user_modules/lv_binding_micropython/bindings.cmake
exit status=$?
# 检查编译是否成功
if [ $? -eq 0 ]; then
echo "编译成功,开始烧录固件..."
sudo mount /dev/sde1 /mnt/usb
sudo cp ./ports/rp2/build-RPI_PICO/firmware.uf2 /mnt/usb/
sudo umount /mnt/usb
echo "Firmware flashed to RPI_PICO"
echo "You can now safely eject the RPI_PICO from your computer."
else
echo "编译失败,已取消烧录操作"
exit 1
fi

View File

@ -89,7 +89,8 @@
// GP{0,4,8,10,...}
#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;
spi_inst_t *const spi_inst;
uint8_t spi_id;
@ -101,42 +102,77 @@ typedef struct _machine_spi_obj_t {
uint8_t mosi;
uint8_t miso;
uint32_t baudrate;
int chan_tx;
int chan_rx;
} machine_spi_obj_t;
static machine_spi_obj_t machine_spi_obj[] = {
{
{&machine_spi_type}, spi0, 0,
DEFAULT_SPI_POLARITY, DEFAULT_SPI_PHASE, DEFAULT_SPI_BITS, DEFAULT_SPI_FIRSTBIT,
MICROPY_HW_SPI0_SCK, MICROPY_HW_SPI0_MOSI, MICROPY_HW_SPI0_MISO,
{&machine_spi_type},
spi0,
0,
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,
// DMA channels, -1 means not claimed
-1,
-1,
},
{
{&machine_spi_type}, spi1, 1,
DEFAULT_SPI_POLARITY, DEFAULT_SPI_PHASE, DEFAULT_SPI_BITS, DEFAULT_SPI_FIRSTBIT,
MICROPY_HW_SPI1_SCK, MICROPY_HW_SPI1_MOSI, MICROPY_HW_SPI1_MISO,
{&machine_spi_type},
spi1,
1,
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,
// DMA channels, -1 means not claimed
-1,
-1,
},
};
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);
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->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) {
enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_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)
{
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[] = {
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ 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_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_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_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_id, MP_ARG_REQUIRED | MP_ARG_OBJ},
{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_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_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_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}},
};
// Parse the arguments.
@ -145,7 +181,8 @@ 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.
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);
}
@ -153,36 +190,48 @@ 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];
// 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);
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"));
}
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);
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"));
}
self->mosi = mosi;
if (self->chan_tx < 0)
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);
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"));
}
self->miso = miso;
if (self->chan_rx < 0)
self->chan_rx = dma_claim_unused_channel(true);
}
// 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->polarity = args[ARG_polarity].u_int;
self->phase = args[ARG_phase].u_int;
self->bits = args[ARG_bits].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"));
}
@ -197,14 +246,22 @@ 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);
}
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 };
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
};
static const mp_arg_t allowed_args[] = {
{ 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_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_firstbit, 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_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_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1}},
};
// Parse the arguments.
@ -213,61 +270,99 @@ 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);
// 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);
}
// Reconfigure the format if requested.
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;
set_format = true;
}
if (args[ARG_phase].u_int != -1) {
if (args[ARG_phase].u_int != -1)
{
self->phase = args[ARG_phase].u_int;
set_format = true;
}
if (args[ARG_bits].u_int != -1) {
if (args[ARG_bits].u_int != -1)
{
self->bits = args[ARG_bits].u_int;
set_format = true;
}
if (args[ARG_firstbit].u_int != -1) {
if (args[ARG_firstbit].u_int != -1)
{
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"));
}
}
if (set_format) {
if (self->miso > 0 && self->chan_rx < 0)
{
self->chan_rx = dma_claim_unused_channel(true);
}
if (self->mosi > 0 && self->chan_tx < 0)
{
self->chan_tx = dma_claim_unused_channel(true);
}
if (set_format)
{
spi_set_format(self->spi_inst, self->bits, self->polarity, self->phase, self->firstbit);
}
}
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_wait_done(mp_obj_base_t *self_in)
{
machine_spi_obj_t *self = (machine_spi_obj_t *)self_in;
int chan_tx = self->chan_tx;
int chan_rx = self->chan_rx;
if (chan_rx >= 0)
dma_channel_wait_for_finish_blocking(chan_rx);
if (chan_tx >= 0)
dma_channel_wait_for_finish_blocking(chan_tx);
spi_hw_t *spi_hw = spi_get_hw(self->spi_inst);
while (!((spi_hw->sr & (1 << 0)) && !(spi_hw->sr & (1 << 4)))) {
// wait for TX FIFO to be empty and SPI not busy
}
}
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;
// Use DMA for large transfers if channels are available
const size_t dma_min_size_threshold = 32;
int chan_tx = -1;
int chan_rx = -1;
if (len >= dma_min_size_threshold) {
// Use two DMA channels to service the two FIFOs
chan_tx = dma_claim_unused_channel(false);
chan_rx = dma_claim_unused_channel(false);
int chan_tx = self->chan_tx;
int chan_rx = self->chan_rx;
if (chan_rx < 0 && chan_tx < 0)
{
mp_raise_msg_varg(&mp_type_RuntimeError, MP_ERROR_TEXT("Error when using DMA, chan rx: %d, chan tx: %d"), chan_rx, chan_tx);
return;
}
bool use_dma = chan_rx >= 0 && chan_tx >= 0;
if (chan_rx >= 0)
dma_channel_wait_for_finish_blocking(chan_rx);
if (chan_tx >= 0)
dma_channel_wait_for_finish_blocking(chan_tx);
// bool use_dma = chan_rx >= 0 && chan_tx >= 0;
// note src is guaranteed to be non-NULL
bool write_only = dest == NULL;
if (use_dma) {
uint8_t dev_null;
dma_channel_config c = dma_channel_get_default_config(chan_tx);
dma_channel_config c;
if (chan_tx >= 0)
{
c = dma_channel_get_default_config(chan_tx);
channel_config_set_transfer_data_size(&c, DMA_SIZE_8);
channel_config_set_dreq(&c, spi_get_index(self->spi_inst) ? DREQ_SPI1_TX : DREQ_SPI0_TX);
dma_channel_configure(chan_tx, &c,
&spi_get_hw(self->spi_inst)->dr,
src,
len,
false);
true);
}
if (chan_rx >= 0)
{
c = dma_channel_get_default_config(chan_rx);
channel_config_set_transfer_data_size(&c, DMA_SIZE_8);
channel_config_set_dreq(&c, spi_get_index(self->spi_inst) ? DREQ_SPI1_RX : DREQ_SPI0_RX);
@ -277,34 +372,34 @@ static void machine_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8
write_only ? &dev_null : dest,
&spi_get_hw(self->spi_inst)->dr,
len,
false);
dma_start_channel_mask((1u << chan_rx) | (1u << chan_tx));
dma_channel_wait_for_finish_blocking(chan_rx);
dma_channel_wait_for_finish_blocking(chan_tx);
true);
}
// dma_channel_wait_for_finish_blocking(chan_rx);
// dma_channel_wait_for_finish_blocking(chan_tx);
// If we have claimed only one channel successfully, we should release immediately
if (chan_rx >= 0) {
dma_channel_unclaim(chan_rx);
}
if (chan_tx >= 0) {
dma_channel_unclaim(chan_tx);
}
// if (chan_rx >= 0) {
// dma_channel_unclaim(chan_rx);
// }
// if (chan_tx >= 0) {
// dma_channel_unclaim(chan_tx);
// }
if (!use_dma) {
// Use software for small transfers, or if couldn't claim two DMA channels
if (write_only) {
spi_write_blocking(self->spi_inst, src, len);
} else {
spi_write_read_blocking(self->spi_inst, src, dest, len);
}
}
// if (!use_dma) {
// // Use software for small transfers, or if couldn't claim two DMA channels
// if (write_only) {
// spi_write_blocking(self->spi_inst, src, len);
// } else {
// spi_write_read_blocking(self->spi_inst, src, dest, len);
// }
// }
}
// Buffer protocol implementation for SPI.
// 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);
bufinfo->len = 4;
@ -317,6 +412,7 @@ static mp_int_t machine_spi_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo,
static const mp_machine_spi_p_t machine_spi_p = {
.init = machine_spi_init,
.transfer = machine_spi_transfer,
.wait_done = machine_spi_wait_done,
};
MP_DEFINE_CONST_OBJ_TYPE(
@ -327,19 +423,22 @@ MP_DEFINE_CONST_OBJ_TYPE(
print, machine_spi_print,
protocol, &machine_spi_p,
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) {
if (mp_obj_is_type(o, &machine_spi_type)) {
mp_obj_base_t *mp_hal_get_spi_obj(mp_obj_t o)
{
if (mp_obj_is_type(o, &machine_spi_type))
{
return MP_OBJ_TO_PTR(o);
}
#if MICROPY_PY_MACHINE_SOFTSPI
else if (mp_obj_is_type(o, &mp_machine_soft_spi_type)) {
#if MICROPY_PY_MACHINE_SOFTSPI
else if (mp_obj_is_type(o, &mp_machine_soft_spi_type))
{
return MP_OBJ_TO_PTR(o);
}
#endif
else {
#endif
else
{
mp_raise_TypeError(MP_ERROR_TEXT("expecting an SPI object"));
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,15 @@
# Create an INTERFACE library for our C module.
add_library(usermod_cexample INTERFACE)
# Add our source files to the lib
target_sources(usermod_cexample INTERFACE
${CMAKE_CURRENT_LIST_DIR}/examplemodule.c
)
# Add the current directory as an include directory.
target_include_directories(usermod_cexample INTERFACE
${CMAKE_CURRENT_LIST_DIR}
)
# Link our INTERFACE library to the usermod target.
target_link_libraries(usermod INTERFACE usermod_cexample)

View File

@ -0,0 +1,8 @@
CEXAMPLE_MOD_DIR := $(USERMOD_DIR)
# Add all C files to SRC_USERMOD.
SRC_USERMOD += $(CEXAMPLE_MOD_DIR)/examplemodule.c
# We can add our module folder to include paths if needed
# This is not actually needed in this example.
CFLAGS_USERMOD += -I$(CEXAMPLE_MOD_DIR)

2
user_modules/user.cmake Normal file
View File

@ -0,0 +1,2 @@
include(${CMAKE_CURRENT_LIST_DIR}/cexample/micropython.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/lv_binding_micropython/bindings.cmake)