STAccel script in STM32F4 DISC
STAccel script in STM32F4 DISC
Hello Guys,
I am new to Python and come from embedded software. I compiled firmware for discovery board, flashed it using STLink, and was able to get some LED's blinking with a script.
I am excited how quick it is to use compared to traditional firmware development.
Now, I am trying to run staccel.py . I copied it into PYBFLASH.
I did import staccel in repl, but when I try to create a object using 'a = staccel.STAccel()', it gives me error saying, module object has no attribute STAccel. I am confused as the script definitely has STAccel. What am I missing here?
I am new to Python and come from embedded software. I compiled firmware for discovery board, flashed it using STLink, and was able to get some LED's blinking with a script.
I am excited how quick it is to use compared to traditional firmware development.
Now, I am trying to run staccel.py . I copied it into PYBFLASH.
I did import staccel in repl, but when I try to create a object using 'a = staccel.STAccel()', it gives me error saying, module object has no attribute STAccel. I am confused as the script definitely has STAccel. What am I missing here?
-
- Posts: 4
- Joined: Sat Nov 08, 2014 1:44 am
Re: STAccel script in STM32F4 DISC
I was just looking at the source and noticed that the STAccel class has a syntax error that may be causing the error. There are no () in the class definition of STAccel.
Re: STAccel script in STM32F4 DISC
It's not a syntax error and the import and creation of the object should work:
Although for me it gives another error: Exception: LIS302DL or LIS3DSH accelerometer not present...
Code: Select all
>>> import staccel
>>> ac = staccel.STAccel()
-
- Posts: 4
- Joined: Sat Nov 08, 2014 1:44 am
Re: STAccel script in STM32F4 DISC
You're right Damien, sorry about that.
It seemed like all the materials I read showed the parentheses, and I just assumed they were required.
I have the discovery board with MicroPython and the demo that uses the accelerometer to turn on LEDs based on the direction of tilt worked fine for me. It was at least a couple months ago that I tried it though.
Edit: It was November actually

I have the discovery board with MicroPython and the demo that uses the accelerometer to turn on LEDs based on the direction of tilt worked fine for me. It was at least a couple months ago that I tried it though.
Edit: It was November actually
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: STAccel script in STM32F4 DISC
I just tested this myself, git the same error and tracked it down.Damien wrote:Although for me it gives another error: Exception: LIS302DL or LIS3DSH accelerometer not present...
It seems that the call to STAccel.read_id() in STAccel.__init__() always returns 255 instead of the correct value 63. In fact, the first read on the SPI bus after creating the pyb.SPI instance seems to always return 255.
So I just fixed this by reading the ID again:
Code: Select all
class STAccel:
def __init__(self, cs='PE3', spi=1, debug=False):
[...]
self.read_id()
# First SPI read always returns 255 --> discard and read ID again
self.who_am_i = self.read_id()
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: STAccel script in STM32F4 DISC
And here's an adapted version of the USB-HID accelerometer-mouse tutorial:
Note that STAccel.xyz() returns a tuple of floats between 0..1, so the values must be magnified and converted to ints to use them for the relative mouse movement coordinates.
Press and release USER button to turn on sending mouse events, press again to turn it off.
Code: Select all
# -*- coding: utf-8 -*-
"""Move mouse pointer via USB-HID according to accelerometer measurements."""
import pyb
from staccel import STAccel
FREQ = const(50)
MAG = const(30)
def set_sw_state():
global sw_state
sw_state = not sw_state
led.toggle()
sw_state = False
led = pyb.LED(1)
switch = pyb.Switch()
switch.callback(set_sw_state)
accel = STAccel()
hid = pyb.USB_HID()
while True:
if sw_state:
x, y, z = accel.xyz()
hid.send((0, int(x * MAG), int(-y * MAG), 0))
pyb.delay(int(1000 / FREQ))
Press and release USER button to turn on sending mouse events, press again to turn it off.
Re: STAccel script in STM32F4 DISC
Thank you guys.
I found some trash-100 files in PYBFLASH. Also I noticed, my board was trying to run led blinking script I had previously wrote and deleted. Not sure of correct process, I just reflashed the board with new firmware, tried STAccel script again with the suggested fix and it worked.
I found some trash-100 files in PYBFLASH. Also I noticed, my board was trying to run led blinking script I had previously wrote and deleted. Not sure of correct process, I just reflashed the board with new firmware, tried STAccel script again with the suggested fix and it worked.
Re: STAccel script in STM32F4 DISC
The trash-100 files were probably left behind by the Host OS when it connected to the flash drive.gk26 wrote:Thank you guys.
I found some trash-100 files in PYBFLASH. Also I noticed, my board was trying to run led blinking script I had previously wrote and deleted. Not sure of correct process, I just reflashed the board with new firmware, tried STAccel script again with the suggested fix and it worked.
Reflashing the pyboard with new firmware generally doesn't touch the contents of the flash drive.
Hitting RESET or power cycling at just the wrong time can leave the flash drive in an invalid state which will cause it to be reinitialized when the next time the firmware starts up. This is because rewriting things requires a read-erase-write cycle. If the board gets reset after the erase starts and before the write completes, then the internal drive can get left in an invalid state (it depends on exactly which block this happens to).
If you delete a file using the host, the host will typically buffer that delete. If you reset the pyboard before that buffered data is actually written out, then the deleted file may reappear.
Re: STAccel script in STM32F4 DISC
Thanks, I was trying to figure that out myself to no avail.I just tested this myself, git the same error and tracked it down.
It seems that the call to STAccel.read_id() in STAccel.__init__() always returns 255 instead of the correct value 63. In fact, the first read on the SPI bus after creating the pyb.SPI instance seems to always return 255.
So I just fixed this by reading the ID again:
Code: Select all
class STAccel:
def __init__(self, cs='PE3', spi=1, debug=False):
[...]
self.read_id()
# First SPI read always returns 255 --> discard and read ID again
self.who_am_i = self.read_id()
and then everything works as it should.
Re: STAccel script in STM32F4 DISC
hmmm, i just started testing with DISCOVERY, got LEDs to flash, but Accel isn't working, i edited boards/STM32F4DISC/staccel.py... what am i missing?
Micro Python v1.4.1-12-g282ca09-dirty on 2015-04-11; F4DISC with STM32F407
>>> import staccel
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: no module named 'staccel'
also tried
>>> import pyb
>>> accel = pyb.Accel()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Accel'
Micro Python v1.4.1-12-g282ca09-dirty on 2015-04-11; F4DISC with STM32F407
>>> import staccel
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: no module named 'staccel'
also tried
>>> import pyb
>>> accel = pyb.Accel()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Accel'