Use pyb.SPI & pyb.Pin together

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
mederic
Posts: 2
Joined: Thu Mar 31, 2016 7:21 am

Use pyb.SPI & pyb.Pin together

Post by mederic » Thu Mar 31, 2016 7:36 am

Hi, i wrote a driver for an ADS1118 (16bit ADC for thermocouples measurment)

The ADS1118 as a sdo/drdy pin who share MISO from SPI and a data ready pin

When The SPI transmission was finished and cs pin remain low i need to read the state of MISO pin (maybe interrupt on it)

Is it possible to

- get the pyboard name af the spi miso pin with pyb.SPI module?

-i trie to deinit spi & inti pin for read it but it dosent work in stress test

- read the state of miso pin wiout the need of pyb.pin module

Tanks Médéric

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

Re: Use pyb.SPI & pyb.Pin together

Post by dhylands » Thu Mar 31, 2016 4:51 pm

I'm not aware of any "builtin" way to determine which pin SPI1 MISO is using.

There is a pins_af.py file created which includes all of the pins defined for a board and the alternate functions.

So you could walk through that looking for SPI1_MISO and check to see if that pin was configured in the appropriate alternate function.

Once you've got a pin ('X7' for example which corresponds to SPI1_MISO) you can change the alternate function of the pin between say a GPIO and SPI functionality.

Something like:

Code: Select all

>>> spi = pyb.SPI(1, pyb.SPI.MASTER)
>>> miso = pyb.Pin('X7')
>>> miso
Pin(Pin.cpu.A6, mode=Pin.AF_PP, pull=Pin.PULL_UP, af=Pin.AF5_SPI1)
>>> miso.value()
1
It turns out you can read the value of a pin even when its configured for some other purpose. So you can use miso.value() to read the pin. This behaviour may be chip specific (i.e. some chips do things a bit differently), but the STM series all behave this way.

mederic
Posts: 2
Joined: Thu Mar 31, 2016 7:21 am

Re: Use pyb.SPI & pyb.Pin together

Post by mederic » Thu Apr 07, 2016 11:43 am

Tks for the reply

It work great to read a Pin.value() without initiate it before

Sadly there is now easy way to know witch pin SPI or I2C bus use

I hope this functionnality was added in a future update of µpython

;)

Post Reply