machine.SPI constructor

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
User avatar
kamikaze
Posts: 154
Joined: Tue Aug 16, 2016 10:10 am
Location: Latvia
Contact:

machine.SPI constructor

Post by kamikaze » Mon Oct 17, 2016 12:18 am

http://docs.micropython.org/en/latest/p ... e.SPI.html

how is it supposed to work? while there is a constructor info in docs for pyb.SPI - there is nothing for machine.SPI
also how is it possible to specify which SPI bus/interface to use on PyBoard? (in pyb.SPI there is a "bus" argument for that)

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: machine.SPI constructor

Post by Damien » Mon Oct 17, 2016 5:24 am

Those docs are, unfortunately, out of date. They reflect the wipy's implementation and need to be updated for esp8266 and new pyboard behaviour. There is no longer a "mode" argument to the constructor / init function.

To construct a hardware instance use: machine.SPI('X').

To construct a software instance use: machine.SPI(sck=pin_sck, mosi=pin_mosi, miso=pin_miso).

User avatar
kamikaze
Posts: 154
Joined: Tue Aug 16, 2016 10:10 am
Location: Latvia
Contact:

Re: machine.SPI constructor

Post by kamikaze » Mon Oct 17, 2016 1:52 pm

Damien wrote:To construct a hardware instance use: machine.SPI('X').
what does 'X' mean? where is it possible to see the code? can't find it. there are so many different modmachine.c and spi.c files...

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: machine.SPI constructor

Post by dhylands » Mon Oct 17, 2016 5:07 pm

On the pyboard there is an X half and a Y half. On the X half, all of the pins start with the letter X, and on the Y half, all of the pins start with the letter Y.

To determine exactly which pins are used for SPI for X and which for Y, I normally look at the boards mpconfigboard.h file. For the original pyboard, this is here:
https://github.com/micropython/micropyt ... .h#L58-L68

User avatar
kamikaze
Posts: 154
Joined: Tue Aug 16, 2016 10:10 am
Location: Latvia
Contact:

Re: machine.SPI constructor

Post by kamikaze » Mon Oct 17, 2016 5:13 pm

it could be obvious for pyb.SPI, but when it is machine.SPI - this is not anymore. But it looks like it is not 'X' anymore but 0, 1 etc.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: machine.SPI constructor

Post by dhylands » Mon Oct 17, 2016 6:22 pm

The stmhal SPI constructor takes either an int, (1 refers to SPI1, 2 refers to SPI2). It can also take a string argument, in which case it gets mapped according to the MICROPY_HW_SPIx_NAME macros from the mpconfigboard.h file.

See: the spi_find function in spi.c:
https://github.com/micropython/micropyt ... #L166-L196

User avatar
kamikaze
Posts: 154
Joined: Tue Aug 16, 2016 10:10 am
Location: Latvia
Contact:

Re: machine.SPI constructor

Post by kamikaze » Mon Oct 17, 2016 6:46 pm

ok, thanks :)

Post Reply