Debian Codegraph @

ports/stm32/boards/STM32F4DISC/staccel.py

raw
1 """
2 Driver for accelerometer on STM32F4 Discover board.
3
4 Sets accelerometer range at +-2g.
5 Returns list containing X,Y,Z axis acceleration values in 'g' units (9.8m/s^2).
6
7 See:
8 STM32Cube_FW_F4_V1.1.0/Drivers/BSP/Components/lis302dl/lis302dl.h
9 STM32Cube_FW_F4_V1.1.0/Drivers/BSP/Components/lis302dl/lis302dl.c
10 STM32Cube_FW_F4_V1.1.0/Drivers/BSP/STM32F4-Discovery/stm32f4_discovery.c
11 STM32Cube_FW_F4_V1.1.0/Drivers/BSP/STM32F4-Discovery/stm32f4_discovery.h
12 STM32Cube_FW_F4_V1.1.0/Drivers/BSP/STM32F4-Discovery/stm32f4_discovery_accelerometer.c
13 STM32Cube_FW_F4_V1.1.0/Drivers/BSP/STM32F4-Discovery/stm32f4_discovery_accelerometer.h
14 STM32Cube_FW_F4_V1.1.0/Projects/STM32F4-Discovery/Demonstrations/Src/main.c
15 """
16
17 from micropython import const
18 from pyb import Pin
19 from pyb import SPI
20
21 READWRITE_CMD = const(0x80)
22 MULTIPLEBYTE_CMD = const(0x40)
23 WHO_AM_I_ADDR = const(0x0F)
24 OUT_X_ADDR = const(0x29)
25 OUT_Y_ADDR = const(0x2B)
26 OUT_Z_ADDR = const(0x2D)
27 OUT_T_ADDR = const(0x0C)
28
29 LIS302DL_WHO_AM_I_VAL = const(0x3B)
30 LIS302DL_CTRL_REG1_ADDR = const(0x20)
31 # Configuration for 100Hz sampling rate, +-2g range
32 LIS302DL_CONF = const(0b01000111)
33
34 LIS3DSH_WHO_AM_I_VAL = const(0x3F)
35 LIS3DSH_CTRL_REG4_ADDR = const(0x20)
36 LIS3DSH_CTRL_REG5_ADDR = const(0x24)
37 # Configuration for 100Hz sampling rate, +-2g range
38 LIS3DSH_CTRL_REG4_CONF = const(0b01100111)
39 LIS3DSH_CTRL_REG5_CONF = const(0b00000000)
40
41
42 class STAccel:
43 def __init__(self):
44 self.cs_pin = Pin("PE3", Pin.OUT_PP, Pin.PULL_NONE)
45 self.cs_pin.high()
46 self.spi = SPI(1, SPI.MASTER, baudrate=328125, polarity=0, phase=1, bits=8)
47
48 self.who_am_i = self.read_id()
49
50 if self.who_am_i == LIS302DL_WHO_AM_I_VAL:
51 self.write_bytes(LIS302DL_CTRL_REG1_ADDR, bytearray([LIS302DL_CONF]))
52 self.sensitivity = 18
53 elif self.who_am_i == LIS3DSH_WHO_AM_I_VAL:
54 self.write_bytes(LIS3DSH_CTRL_REG4_ADDR, bytearray([LIS3DSH_CTRL_REG4_CONF]))
55 self.write_bytes(LIS3DSH_CTRL_REG5_ADDR, bytearray([LIS3DSH_CTRL_REG5_CONF]))
56 self.sensitivity = 0.06 * 256
57 else:
58 raise Exception("LIS302DL or LIS3DSH accelerometer not present")
59
60 def convert_raw_to_g(self, x):
61 if x & 0x80:
62 x = x - 256
63 return x * self.sensitivity / 1000
64
65 def read_bytes(self, addr, nbytes):
66 if nbytes > 1:
67 addr |= READWRITE_CMD | MULTIPLEBYTE_CMD
68 else:
69 addr |= READWRITE_CMD
70 self.cs_pin.low()
71 self.spi.send(addr)
72 # buf = self.spi.send_recv(bytearray(nbytes * [0])) # read data, MSB first
73 buf = self.spi.recv(nbytes)
74 self.cs_pin.high()
75 return buf
76
77 def write_bytes(self, addr, buf):
78 if len(buf) > 1:
79 addr |= MULTIPLEBYTE_CMD
80 self.cs_pin.low()
81 self.spi.send(addr)
82 for b in buf:
83 self.spi.send(b)
84 self.cs_pin.high()
85
86 def read_id(self):
87 return self.read_bytes(WHO_AM_I_ADDR, 1)[0]
88
89 def x(self):
90 return self.convert_raw_to_g(self.read_bytes(OUT_X_ADDR, 1)[0])
91
92 def y(self):
93 return self.convert_raw_to_g(self.read_bytes(OUT_Y_ADDR, 1)[0])
94
95 def z(self):
96 return self.convert_raw_to_g(self.read_bytes(OUT_Z_ADDR, 1)[0])
97
98 def xyz(self):
99 return (self.x(), self.y(), self.z())