Skip to content

Commit 101d87d

Browse files
committed
stmhal: Working STM32F4DISC accelerometer, via Python script.
Thanks to David Siorpaes.
1 parent 4ef26c1 commit 101d87d

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

stmhal/boards/STM32F4DISC/staccel.py

100644100755
Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
Driver for accelerometer on STM32F4 Discover board.
33
44
Assumes it's a LIS302DL MEMS device.
5-
6-
Not currently working.
5+
Sets accelerometer range at +-2g.
6+
Returns list containing X,Y,Z axis acceleration values in 'g' units (9.8m/s^2).
77
88
See:
99
STM32Cube_FW_F4_V1.1.0/Drivers/BSP/Components/lis302dl/lis302dl.h
@@ -22,12 +22,22 @@
2222
MULTIPLEBYTE_CMD = const(0x40)
2323
LIS302DL_WHO_AM_I_ADDR = const(0x0f)
2424
LIS302DL_CTRL_REG1_ADDR = const(0x20)
25+
LIS302DL_OUT_X = const(0x29)
26+
# Configuration for 100Hz sampling rate, +-2g range
27+
LIS302DL_CONF = 0b01000111
28+
29+
def signed8(x):
30+
if x & 0x80:
31+
return x - 256
32+
else:
33+
return x
2534

2635
class STAccel:
2736
def __init__(self):
2837
self.cs_pin = Pin('PE3', Pin.OUT_PP, Pin.PULL_NONE)
2938
self.cs_pin.high()
3039
self.spi = SPI(1, SPI.MASTER, baudrate=328125, polarity=0, phase=1, bits=8)
40+
self.wr(LIS302DL_CTRL_REG1_ADDR, bytearray([LIS302DL_CONF]))
3141

3242
def rd(self, addr, nbytes):
3343
if nbytes > 1:
@@ -52,5 +62,9 @@ def wr(self, addr, buf):
5262
def read_id(self):
5363
return self.rd(LIS302DL_WHO_AM_I_ADDR, 1)
5464

55-
def init(self, init_param):
56-
self.wr(LIS302DL_CTRL_REG1_ADDR, bytearray([init_param]))
65+
def get_xyz(self):
66+
val = self.rd(LIS302DL_OUT_X, 5)
67+
x = signed8(val[0]) * 18.0 / 1000
68+
y = signed8(val[2]) * 18.0 / 1000
69+
z = signed8(val[4]) * 18.0 / 1000
70+
return [x, y, z]

0 commit comments

Comments
 (0)