LIS3DH sensor with SPI

The official PYBD running MicroPython, and its accessories.
Target audience: Users with a PYBD
Post Reply
MarkTk
Posts: 8
Joined: Tue Mar 24, 2020 8:01 am

LIS3DH sensor with SPI

Post by MarkTk » Tue Mar 31, 2020 5:24 pm

Hi,
I´m really new with micropython and I was trying to work with the LIS3DH sensor throught SPI but i cannt get it to work.
This is the code that I was running:

>
MicroPython v1.11-576-gd667bc642 on 2019-11-13; PYBD-SF6W with STM32F767IIK
Type "help()" for more information.
>>>
>>> from machine import SPI, Pin
>>> import lis3dh
>>>
>>> Pin('EN_3V3').value(1)
>>> spi=SPI(1)
>>> spi.init(baudrate=100000, polarity=0, phase=0)
>>> b=lis3dh.LIS3DH_SPI(spi)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: function missing required positional argument #3
>>>


I was trying to define the CS pin too ,
>>> cs = machine.Pin('X5')
>>> b=lis3dh.LIS3DH_SPI(spi, cs)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/flash/lib/lis3dh.py", line 363, in __init__
ImportError: no module named 'adafruit_bus_device'


I´ve downloaded the driver here:
https://github.com/adafruit/Adafruit_Ci ... hon_LIS3DH


Could you please help me with this?
Thanks so much in advance!!

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

Re: LIS3DH sensor with SPI

Post by jimmo » Tue Mar 31, 2020 10:42 pm

Hi,

This is a CircuitPython library, so it will not work on standard MicroPython. (CircuitPython shares the same core language and VM but has a completely different set of APIs for writing drivers).

It's usually not too difficult to rewrite a CircuitPython driver into MicroPython once you're familiar with both APIs. (replace digitalio with machine.Pin and adafruit_bus_device.spi_device with machine.SPI).

Maybe as a starting point here's another driver (writen for a SPI (or I2C) OLED display) that shows what a typical SPI MicroPython driver might look like. https://github.com/micropython/micropyt ... ssd1306.py

If you want to have a go at it feel free to ask questions here... converting drivers to MicroPython is a topic that's discussed reasonably often.

MarkTk
Posts: 8
Joined: Tue Mar 24, 2020 8:01 am

Re: LIS3DH sensor with SPI

Post by MarkTk » Wed Apr 01, 2020 6:44 am

Hi,
I will try to rewrite the library as you indicated....thanks so much for your help!!!

MarkTk
Posts: 8
Joined: Tue Mar 24, 2020 8:01 am

Re: LIS3DH sensor with SPI

Post by MarkTk » Wed Apr 01, 2020 8:57 am

sorry, I was trying to convert the code but im getting always the error "AttributeError: 'module' object has no attribute 'LIS3DH_SPI'"

>>> from machine import SPI, Pin
>>> Pin('EN_3V3').value(1)
>>> cs = machine.Pin('X5', machine.Pin.OUT)
>>> spi=SPI(1)
>>> spi.init(baudrate=100000, polarity=0, phase=0)
>>> import lis3dh
>>> sensor=lis3dh.LIS3DH_SPI(spi, cs)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'LIS3DH_SPI'
>>>

attached you can find the driver.
I will appreciate any input you could give me. Thanks so much in advance!!
Attachments
lis3dh.rar
(4.06 KiB) Downloaded 193 times

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

Re: LIS3DH sensor with SPI

Post by jimmo » Wed Apr 01, 2020 12:21 pm

Hi,

I don't think you've updated the file on the device correctly -- the file you posted there doesn't compile (due to indent errors), so the import would have failed.

It's likely that it's still loading the old version.

If you're using a pyboard, then either pyboard.py or rshell are the best ways to work with files on the device. http://docs.micropython.org/en/latest/r ... rd.py.html
If you're using the flash drive interface, then copy the files, then make sure to wait for the red led to turn off and then soft-reset from the REPL using Ctrl-D. (But I strongly recommend using pyboard.py or rshell instead).

MarkTk
Posts: 8
Joined: Tue Mar 24, 2020 8:01 am

Re: LIS3DH sensor with SPI

Post by MarkTk » Wed Apr 01, 2020 3:52 pm

Hi,
you were right i was having also problems with that. Thanks so much for your help.

I could import correctly the library (and also the I2C is working without any issue)
but I cannt get it to work with the SPI interface.

>>> import lis3dh
>>> from machine import SPI, Pin
>>> Pin('EN_3V3').value(1)
>>> cs = machine.Pin('X5', machine.Pin.OUT)
>>> spi=SPI(1)
>>> spi.init(baudrate=100000, polarity=0, phase=0)
>>> b=lis3dh.LIS3DH_SPI(spi, cs)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/flash/lib/lis3dh.py", line 314, in __init__
File "/flash/lib/lis3dh.py", line 65, in __init__
File "/flash/lib/lis3dh.py", line 271, in _read_register_byte
File "/flash/lib/lis3dh.py", line 321, in _read_register
AttributeError: 'SPI' object has no attribute '__exit__'
>>>

I think i´m not configuring the SPI correctly in the library. Could you please help me with that?
thanks so much!!!!
Attachments
lis3dh.rar
(4.01 KiB) Downloaded 221 times

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

Re: LIS3DH sensor with SPI

Post by jimmo » Thu Apr 02, 2020 12:53 am

The issue is

Code: Select all

with self._spi as spi:
                        spi.write(self._buffer, start=0, end=1) # pylint: disable=no-member
                        spi.readinto(self._buffer, start=0, end=length) # pylint: disable=no-member
                        return self._buffer
MicroPython's SPI object doesn't support enter/exit (i.e. the "with" statement / context manager). I don't know CircuitPython very well but my guess is that they use this to toggle the CS pin (their SPI object knows about the CS pin). So just remove the "with self._spi" part and use self._spi directly, and set self._cs low at the beginning of the function and high at the end.

MarkTk
Posts: 8
Joined: Tue Mar 24, 2020 8:01 am

Re: LIS3DH sensor with SPI

Post by MarkTk » Fri Apr 03, 2020 9:35 am

Thanks so much for you help.
sorry for bothering you again, but I was trying to implement it as you indicated me but i´m still getting errors.

MicroPython v1.11-576-gd667bc642 on 2019-11-13; PYBD-SF6W with STM32F767IIK
Type "help()" for more information.
>>> import lis3dh
>>> from machine import SPI, Pin
>>> Pin('EN_3V3').value(1)
>>> cs = machine.Pin('X5', machine.Pin.OUT)
>>> spi=SPI(1)
>>> spi.init(baudrate=100000, polarity=0, phase=0)
>>> b=lis3dh.LIS3DH_SPI(spi, cs)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/flash/lib/lis3dh.py", line 314, in __init__
File "/flash/lib/lis3dh.py", line 65, in __init__
File "/flash/lib/lis3dh.py", line 271, in _read_register_byte
TypeError: 'NoneType' object isn't subscriptable

I will apreciate any input, thanks so much for your time!!
Attachments
lis3dh.rar
(4.02 KiB) Downloaded 191 times

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

Re: LIS3DH sensor with SPI

Post by jimmo » Tue Apr 14, 2020 5:17 am

MarkTk wrote:
Fri Apr 03, 2020 9:35 am
File "/flash/lib/lis3dh.py", line 65, in __init__
File "/flash/lib/lis3dh.py", line 271, in _read_register_byte
TypeError: 'NoneType' object isn't subscriptable
__init__ calls read_register_byte, which calls "self._read_register(register, 1)[0]", which is implemented in LIS3DH_SPI

In the case where length == 1, it doesn't return anything (i.e. it returns None), hence the error "'NoneType' object isn't subscriptable" when read_register_byte tries to access the [0]'th element.

Post Reply