Page 1 of 1

LIS3DH sensor with SPI

Posted: Tue Mar 31, 2020 5:24 pm
by MarkTk
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!!

Re: LIS3DH sensor with SPI

Posted: Tue Mar 31, 2020 10:42 pm
by jimmo
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.

Re: LIS3DH sensor with SPI

Posted: Wed Apr 01, 2020 6:44 am
by MarkTk
Hi,
I will try to rewrite the library as you indicated....thanks so much for your help!!!

Re: LIS3DH sensor with SPI

Posted: Wed Apr 01, 2020 8:57 am
by MarkTk
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!!

Re: LIS3DH sensor with SPI

Posted: Wed Apr 01, 2020 12:21 pm
by jimmo
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).

Re: LIS3DH sensor with SPI

Posted: Wed Apr 01, 2020 3:52 pm
by MarkTk
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!!!!

Re: LIS3DH sensor with SPI

Posted: Thu Apr 02, 2020 12:53 am
by jimmo
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.

Re: LIS3DH sensor with SPI

Posted: Fri Apr 03, 2020 9:35 am
by MarkTk
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!!

Re: LIS3DH sensor with SPI

Posted: Tue Apr 14, 2020 5:17 am
by jimmo
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.