Soft SPI suitable for all boards?

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Post Reply
ExXec
Posts: 83
Joined: Sat Oct 20, 2018 4:02 pm

Soft SPI suitable for all boards?

Post by ExXec » Sun Apr 28, 2019 3:04 pm

Hey,

should I let the hard_spi type delegate the constructor to the soft spi module if I'm not on a ST board?

Code: Select all

const mp_obj_type_t machine_hard_spi_type = {
    { &mp_type_type },
    .name = MP_QSTR_SPI,
    .print = machine_hard_spi_print,
    .make_new = mp_machine_spi_make_new, // delegate to master constructor
    .protocol = &machine_hard_spi_p,
    .locals_dict = (mp_obj_t)&machine_spi_locals_dict,
};
and can I just use my own locals dict here? ^ I need more constants

sorry for asking, this looks much more integrated that eg the UART module

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Soft SPI suitable for all boards?

Post by jimmo » Mon Apr 29, 2019 12:19 pm

hi ExXec,

Sorry I'm not entirely sure I understand the question you're asking, but it looks like the tm4c123 has hardware SPI so I'll assume you want to match the STM32 functionality where you get hardware SPI if you pass an id to the constructor, or software SPI if you pass miso/mosi/clk instead.

In which case, you want to do exactly what the STM32 port does -- set

Code: Select all

.make_new = mp_machine_spi_make_new
and in mpconfigport.h

Code: Select all

#define MICROPY_PY_MACHINE_SPI_MAKE_NEW machine_hard_spi_make_new
and in your modmachine.c

Code: Select all

{ MP_ROM_QSTR(MP_QSTR_SPI),                 MP_ROM_PTR(&machine_hard_spi_type) },
If all you want is software SPI, ignoring the hardware peripheral, then don't set MICROPY_PY_MACHINE_SPI_MAKE_NEW and use machine_soft_spi_type in your modmachine.c (see ESP32).

You're going to have trouble with the locals dict, because there's no externs for the existing members (and duplicating the table would be fragile anyway). Not sure the best option there... I wonder if you could create a subclass which adds your additional locals (and then register that subclass in modmachine.c instead).

ExXec
Posts: 83
Joined: Sat Oct 20, 2018 4:02 pm

Re: Soft SPI suitable for all boards?

Post by ExXec » Wed May 01, 2019 3:55 pm

Hey, yeah that is basically what I meant,

I'm going to try duplicating the dict and adding my own constants anyways, maybe it will work.
Would you care to elaborate why it would be fragile?

Thanks,
-ExXec

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Soft SPI suitable for all boards?

Post by jimmo » Thu May 02, 2019 1:14 am

Your dict will need to be kept in sync with the "main" one, and it's fragile in the sense that changes to the main one won't be reflected automatically in yours leading to confusing behaviour. This is the sort of thing that would be a consideration if you tried to upstream the port.

Post Reply