developed_by_feng-arch_add_dma_support_in_machine_spi #1
@ -36,7 +36,8 @@
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
// MicroPython bindings for generic machine.SPI
|
// 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_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);
|
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);
|
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_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_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);
|
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);
|
spi_p->deinit(s);
|
||||||
}
|
}
|
||||||
return mp_const_none;
|
return mp_const_none;
|
||||||
}
|
}
|
||||||
static MP_DEFINE_CONST_FUN_OBJ_1(machine_spi_deinit_obj, machine_spi_deinit);
|
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_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);
|
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);
|
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_t vstr;
|
||||||
vstr_init_len(&vstr, mp_obj_get_int(args[1]));
|
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);
|
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);
|
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_buffer_info_t bufinfo;
|
||||||
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
|
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);
|
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);
|
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_buffer_info_t src;
|
||||||
mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
|
mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
|
||||||
mp_machine_spi_transfer(self, src.len, (const uint8_t *)src.buf, NULL);
|
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);
|
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_buffer_info_t src;
|
||||||
mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
|
mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
|
||||||
mp_buffer_info_t dest;
|
mp_buffer_info_t dest;
|
||||||
mp_get_buffer_raise(rd_buf, &dest, MP_BUFFER_WRITE);
|
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_raise_ValueError(MP_ERROR_TEXT("buffers must be the same length"));
|
||||||
}
|
}
|
||||||
mp_machine_spi_transfer(self, src.len, src.buf, dest.buf);
|
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);
|
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[] = {
|
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_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_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_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_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), 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_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_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_LSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPI_LSB)},
|
||||||
};
|
};
|
||||||
MP_DEFINE_CONST_DICT(mp_machine_spi_locals_dict, machine_spi_locals_dict_table);
|
MP_DEFINE_CONST_DICT(mp_machine_spi_locals_dict, machine_spi_locals_dict_table);
|
||||||
|
|
||||||
@ -119,52 +141,71 @@ MP_DEFINE_CONST_DICT(mp_machine_spi_locals_dict, machine_spi_locals_dict_table);
|
|||||||
|
|
||||||
#if MICROPY_PY_MACHINE_SOFTSPI
|
#if MICROPY_PY_MACHINE_SOFTSPI
|
||||||
|
|
||||||
static uint32_t baudrate_from_delay_half(uint32_t delay_half) {
|
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) {
|
#ifdef MICROPY_HW_SOFTSPI_MIN_DELAY
|
||||||
|
if (delay_half == MICROPY_HW_SOFTSPI_MIN_DELAY)
|
||||||
|
{
|
||||||
return MICROPY_HW_SOFTSPI_MAX_BAUDRATE;
|
return MICROPY_HW_SOFTSPI_MAX_BAUDRATE;
|
||||||
} else
|
}
|
||||||
#endif
|
else
|
||||||
|
#endif
|
||||||
{
|
{
|
||||||
return 500000 / delay_half;
|
return 500000 / delay_half;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint32_t baudrate_to_delay_half(uint32_t baudrate) {
|
static uint32_t baudrate_to_delay_half(uint32_t baudrate)
|
||||||
#ifdef MICROPY_HW_SOFTSPI_MIN_DELAY
|
{
|
||||||
if (baudrate >= MICROPY_HW_SOFTSPI_MAX_BAUDRATE) {
|
#ifdef MICROPY_HW_SOFTSPI_MIN_DELAY
|
||||||
|
if (baudrate >= MICROPY_HW_SOFTSPI_MAX_BAUDRATE)
|
||||||
|
{
|
||||||
return MICROPY_HW_SOFTSPI_MIN_DELAY;
|
return MICROPY_HW_SOFTSPI_MIN_DELAY;
|
||||||
} else
|
}
|
||||||
#endif
|
else
|
||||||
|
#endif
|
||||||
{
|
{
|
||||||
uint32_t delay_half = 500000 / baudrate;
|
uint32_t delay_half = 500000 / baudrate;
|
||||||
// round delay_half up so that: actual_baudrate <= requested_baudrate
|
// round delay_half up so that: actual_baudrate <= requested_baudrate
|
||||||
if (500000 % baudrate != 0) {
|
if (500000 % baudrate != 0)
|
||||||
|
{
|
||||||
delay_half += 1;
|
delay_half += 1;
|
||||||
}
|
}
|
||||||
return delay_half;
|
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_machine_soft_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||||
mp_printf(print, "SoftSPI(baudrate=%u, polarity=%u, phase=%u, firstbit=%u,"
|
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 ")",
|
" sck=" MP_HAL_PIN_FMT ", mosi=" MP_HAL_PIN_FMT ", miso=" MP_HAL_PIN_FMT ")",
|
||||||
baudrate_from_delay_half(self->spi.delay_half), self->spi.polarity, self->spi.phase, self->spi.firstbit,
|
baudrate_from_delay_half(self->spi.delay_half), self->spi.polarity, self->spi.phase, self->spi.firstbit,
|
||||||
mp_hal_pin_name(self->spi.sck), mp_hal_pin_name(self->spi.mosi), mp_hal_pin_name(self->spi.miso));
|
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) {
|
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 };
|
{
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
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_baudrate, MP_ARG_INT, {.u_int = 500000} },
|
{MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 500000}},
|
||||||
{ MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
{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_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_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_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_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_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_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_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);
|
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.delay_half = baudrate_to_delay_half(args[ARG_baudrate].u_int);
|
||||||
self->spi.polarity = args[ARG_polarity].u_int;
|
self->spi.polarity = args[ARG_polarity].u_int;
|
||||||
self->spi.phase = args[ARG_phase].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"));
|
mp_raise_ValueError(MP_ERROR_TEXT("bits must be 8"));
|
||||||
}
|
}
|
||||||
self->spi.firstbit = args[ARG_firstbit].u_int;
|
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 || args[ARG_mosi].u_obj == MP_OBJ_NULL || args[ARG_miso].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"));
|
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);
|
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);
|
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;
|
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[] = {
|
static const mp_arg_t allowed_args[] = {
|
||||||
{ MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
|
{MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1}},
|
||||||
{ MP_QSTR_polarity, 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_phase, 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}},
|
||||||
{ MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
{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_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_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_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);
|
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);
|
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;
|
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;
|
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;
|
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);
|
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);
|
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);
|
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);
|
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_machine_soft_spi_obj_t *self = (mp_machine_soft_spi_obj_t *)self_in;
|
||||||
mp_soft_spi_transfer(&self->spi, len, src, dest);
|
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,
|
make_new, mp_machine_soft_spi_make_new,
|
||||||
print, mp_machine_soft_spi_print,
|
print, mp_machine_soft_spi_print,
|
||||||
protocol, &mp_machine_soft_spi_p,
|
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
|
#endif // MICROPY_PY_MACHINE_SOFTSPI
|
||||||
|
|||||||
@ -89,29 +89,35 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Temporary support for legacy construction of SoftI2C via I2C type.
|
// 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) \
|
#define MP_MACHINE_I2C_CHECK_FOR_LEGACY_SOFTI2C_CONSTRUCTION(n_args, n_kw, all_args) \
|
||||||
do { \
|
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 || all_args[0] == MP_OBJ_NEW_SMALL_INT(-1)) \
|
||||||
if (n_args != 0) { \
|
{ \
|
||||||
--n_args; \
|
mp_print_str(MICROPY_ERROR_PRINTER, "Warning: I2C(-1, ...) is deprecated, use SoftI2C(...) instead\n"); \
|
||||||
++all_args; \
|
if (n_args != 0) \
|
||||||
} \
|
{ \
|
||||||
|
--n_args; \
|
||||||
|
++all_args; \
|
||||||
|
} \
|
||||||
return MP_OBJ_TYPE_GET_SLOT(&mp_machine_soft_i2c_type, make_new)(&mp_machine_soft_i2c_type, n_args, n_kw, all_args); \
|
return MP_OBJ_TYPE_GET_SLOT(&mp_machine_soft_i2c_type, make_new)(&mp_machine_soft_i2c_type, n_args, n_kw, all_args); \
|
||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
// Temporary support for legacy construction of SoftSPI via SPI type.
|
// 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) \
|
#define MP_MACHINE_SPI_CHECK_FOR_LEGACY_SOFTSPI_CONSTRUCTION(n_args, n_kw, all_args) \
|
||||||
do { \
|
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 || all_args[0] == MP_OBJ_NEW_SMALL_INT(-1)) \
|
||||||
if (n_args != 0) { \
|
{ \
|
||||||
--n_args; \
|
mp_print_str(MICROPY_ERROR_PRINTER, "Warning: SPI(-1, ...) is deprecated, use SoftSPI(...) instead\n"); \
|
||||||
++all_args; \
|
if (n_args != 0) \
|
||||||
} \
|
{ \
|
||||||
|
--n_args; \
|
||||||
|
++all_args; \
|
||||||
|
} \
|
||||||
return MP_OBJ_TYPE_GET_SLOT(&mp_machine_soft_spi_type, make_new)(&mp_machine_soft_spi_type, n_args, n_kw, all_args); \
|
return MP_OBJ_TYPE_GET_SLOT(&mp_machine_soft_spi_type, make_new)(&mp_machine_soft_spi_type, n_args, n_kw, all_args); \
|
||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
#if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_SOFTI2C
|
#if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_SOFTI2C
|
||||||
@ -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_uart_obj_t machine_uart_obj_t;
|
||||||
typedef struct _machine_wdt_obj_t machine_wdt_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;
|
mp_obj_base_t base;
|
||||||
unsigned elem_size; // in bytes
|
unsigned elem_size; // in bytes
|
||||||
} machine_mem_obj_t;
|
} machine_mem_obj_t;
|
||||||
|
|
||||||
#if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_SOFTI2C
|
#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;
|
size_t len;
|
||||||
uint8_t *buf;
|
uint8_t *buf;
|
||||||
} mp_machine_i2c_buf_t;
|
} 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
|
// - start/stop/read/write can be NULL, meaning operation is not supported
|
||||||
// - transfer must be non-NULL
|
// - transfer must be non-NULL
|
||||||
// - transfer_single only needs to be set if transfer=mp_machine_i2c_transfer_adaptor
|
// - transfer_single only needs to be set if transfer=mp_machine_i2c_transfer_adaptor
|
||||||
typedef struct _mp_machine_i2c_p_t {
|
typedef struct _mp_machine_i2c_p_t
|
||||||
#if MICROPY_PY_MACHINE_I2C_TRANSFER_WRITE1
|
{
|
||||||
|
#if MICROPY_PY_MACHINE_I2C_TRANSFER_WRITE1
|
||||||
bool transfer_supports_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);
|
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 (*start)(mp_obj_base_t *obj);
|
||||||
int (*stop)(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;
|
} mp_machine_i2c_p_t;
|
||||||
|
|
||||||
// SoftI2C object.
|
// SoftI2C object.
|
||||||
typedef struct _mp_machine_soft_i2c_obj_t {
|
typedef struct _mp_machine_soft_i2c_obj_t
|
||||||
|
{
|
||||||
mp_obj_base_t base;
|
mp_obj_base_t base;
|
||||||
uint32_t us_delay;
|
uint32_t us_delay;
|
||||||
uint32_t us_timeout;
|
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
|
#if MICROPY_PY_MACHINE_SPI || MICROPY_PY_MACHINE_SOFTSPI
|
||||||
|
|
||||||
// SPI protocol.
|
// 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 (*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 (*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 (*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;
|
} mp_machine_spi_p_t;
|
||||||
|
|
||||||
// SoftSPI object.
|
// SoftSPI object.
|
||||||
typedef struct _mp_machine_soft_spi_obj_t {
|
typedef struct _mp_machine_soft_spi_obj_t
|
||||||
|
{
|
||||||
mp_obj_base_t base;
|
mp_obj_base_t base;
|
||||||
mp_soft_spi_obj_t spi;
|
mp_soft_spi_obj_t spi;
|
||||||
} mp_machine_soft_spi_obj_t;
|
} mp_machine_soft_spi_obj_t;
|
||||||
|
|||||||
14
make_and_flash_rp2.sh
Executable file
14
make_and_flash_rp2.sh
Executable 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
|
||||||
@ -32,21 +32,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
|
||||||
@ -56,40 +56,41 @@
|
|||||||
|
|
||||||
#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;
|
||||||
@ -101,42 +102,77 @@ typedef struct _machine_spi_obj_t {
|
|||||||
uint8_t mosi;
|
uint8_t mosi;
|
||||||
uint8_t miso;
|
uint8_t miso;
|
||||||
uint32_t baudrate;
|
uint32_t baudrate;
|
||||||
|
int chan_tx;
|
||||||
|
int chan_rx;
|
||||||
} machine_spi_obj_t;
|
} machine_spi_obj_t;
|
||||||
|
|
||||||
static machine_spi_obj_t machine_spi_obj[] = {
|
static machine_spi_obj_t machine_spi_obj[] = {
|
||||||
{
|
{
|
||||||
{&machine_spi_type}, spi0, 0,
|
{&machine_spi_type},
|
||||||
DEFAULT_SPI_POLARITY, DEFAULT_SPI_PHASE, DEFAULT_SPI_BITS, DEFAULT_SPI_FIRSTBIT,
|
spi0,
|
||||||
MICROPY_HW_SPI0_SCK, MICROPY_HW_SPI0_MOSI, MICROPY_HW_SPI0_MISO,
|
|
||||||
0,
|
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,
|
{&machine_spi_type},
|
||||||
DEFAULT_SPI_POLARITY, DEFAULT_SPI_PHASE, DEFAULT_SPI_BITS, DEFAULT_SPI_FIRSTBIT,
|
spi1,
|
||||||
MICROPY_HW_SPI1_SCK, MICROPY_HW_SPI1_MOSI, MICROPY_HW_SPI1_MISO,
|
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,
|
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);
|
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.
|
||||||
@ -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.
|
// 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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];
|
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)
|
||||||
|
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;
|
||||||
|
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.
|
// 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"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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);
|
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.
|
||||||
@ -213,98 +270,136 @@ 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 (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);
|
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;
|
machine_spi_obj_t *self = (machine_spi_obj_t *)self_in;
|
||||||
// Use DMA for large transfers if channels are available
|
// Use DMA for large transfers if channels are available
|
||||||
const size_t dma_min_size_threshold = 32;
|
int chan_tx = self->chan_tx;
|
||||||
int chan_tx = -1;
|
int chan_rx = self->chan_rx;
|
||||||
int chan_rx = -1;
|
if (chan_rx < 0 && chan_tx < 0)
|
||||||
if (len >= dma_min_size_threshold) {
|
{
|
||||||
// Use two DMA channels to service the two FIFOs
|
mp_raise_msg_varg(&mp_type_RuntimeError, MP_ERROR_TEXT("Error when using DMA, chan rx: %d, chan tx: %d"), chan_rx, chan_tx);
|
||||||
chan_tx = dma_claim_unused_channel(false);
|
return;
|
||||||
chan_rx = dma_claim_unused_channel(false);
|
|
||||||
}
|
}
|
||||||
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
|
// note src is guaranteed to be non-NULL
|
||||||
bool write_only = dest == NULL;
|
bool write_only = dest == NULL;
|
||||||
|
|
||||||
if (use_dma) {
|
uint8_t dev_null;
|
||||||
uint8_t dev_null;
|
dma_channel_config c;
|
||||||
dma_channel_config c = dma_channel_get_default_config(chan_tx);
|
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_transfer_data_size(&c, DMA_SIZE_8);
|
||||||
channel_config_set_dreq(&c, spi_get_index(self->spi_inst) ? DREQ_SPI1_TX : DREQ_SPI0_TX);
|
channel_config_set_dreq(&c, spi_get_index(self->spi_inst) ? DREQ_SPI1_TX : DREQ_SPI0_TX);
|
||||||
dma_channel_configure(chan_tx, &c,
|
dma_channel_configure(chan_tx, &c,
|
||||||
&spi_get_hw(self->spi_inst)->dr,
|
&spi_get_hw(self->spi_inst)->dr,
|
||||||
src,
|
src,
|
||||||
len,
|
len,
|
||||||
false);
|
true);
|
||||||
|
}
|
||||||
|
if (chan_rx >= 0)
|
||||||
|
{
|
||||||
c = dma_channel_get_default_config(chan_rx);
|
c = dma_channel_get_default_config(chan_rx);
|
||||||
channel_config_set_transfer_data_size(&c, DMA_SIZE_8);
|
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);
|
channel_config_set_dreq(&c, spi_get_index(self->spi_inst) ? DREQ_SPI1_RX : DREQ_SPI0_RX);
|
||||||
channel_config_set_read_increment(&c, false);
|
channel_config_set_read_increment(&c, false);
|
||||||
channel_config_set_write_increment(&c, !write_only);
|
channel_config_set_write_increment(&c, !write_only);
|
||||||
dma_channel_configure(chan_rx, &c,
|
dma_channel_configure(chan_rx, &c,
|
||||||
write_only ? &dev_null : dest,
|
write_only ? &dev_null : dest,
|
||||||
&spi_get_hw(self->spi_inst)->dr,
|
&spi_get_hw(self->spi_inst)->dr,
|
||||||
len,
|
len,
|
||||||
false);
|
true);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 we have claimed only one channel successfully, we should release immediately
|
||||||
if (chan_rx >= 0) {
|
// if (chan_rx >= 0) {
|
||||||
dma_channel_unclaim(chan_rx);
|
// dma_channel_unclaim(chan_rx);
|
||||||
}
|
// }
|
||||||
if (chan_tx >= 0) {
|
// if (chan_tx >= 0) {
|
||||||
dma_channel_unclaim(chan_tx);
|
// dma_channel_unclaim(chan_tx);
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (!use_dma) {
|
// if (!use_dma) {
|
||||||
// Use software for small transfers, or if couldn't claim two DMA channels
|
// // Use software for small transfers, or if couldn't claim two DMA channels
|
||||||
if (write_only) {
|
// if (write_only) {
|
||||||
spi_write_blocking(self->spi_inst, src, len);
|
// spi_write_blocking(self->spi_inst, src, len);
|
||||||
} else {
|
// } else {
|
||||||
spi_write_read_blocking(self->spi_inst, src, dest, len);
|
// spi_write_read_blocking(self->spi_inst, src, dest, len);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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;
|
||||||
@ -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 = {
|
static const mp_machine_spi_p_t machine_spi_p = {
|
||||||
.init = machine_spi_init,
|
.init = machine_spi_init,
|
||||||
.transfer = machine_spi_transfer,
|
.transfer = machine_spi_transfer,
|
||||||
|
.wait_done = machine_spi_wait_done,
|
||||||
};
|
};
|
||||||
|
|
||||||
MP_DEFINE_CONST_OBJ_TYPE(
|
MP_DEFINE_CONST_OBJ_TYPE(
|
||||||
@ -327,19 +423,22 @@ 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"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
18780
user_modules/cexample/examplemodule.c
Normal file
18780
user_modules/cexample/examplemodule.c
Normal file
File diff suppressed because it is too large
Load Diff
15
user_modules/cexample/micropython.cmake
Normal file
15
user_modules/cexample/micropython.cmake
Normal 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)
|
||||||
8
user_modules/cexample/micropython.mk
Normal file
8
user_modules/cexample/micropython.mk
Normal 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
2
user_modules/user.cmake
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
include(${CMAKE_CURRENT_LIST_DIR}/cexample/micropython.cmake)
|
||||||
|
include(${CMAKE_CURRENT_LIST_DIR}/lv_binding_micropython/bindings.cmake)
|
||||||
Loading…
x
Reference in New Issue
Block a user