2014-08-02 07:51:18 -04:00
|
|
|
"""
|
|
|
|
Driver for accelerometer on STM32F4 Discover board.
|
|
|
|
|
2014-08-10 17:26:20 -04:00
|
|
|
Sets accelerometer range at +-2g.
|
|
|
|
Returns list containing X,Y,Z axis acceleration values in 'g' units (9.8m/s^2).
|
2014-08-02 07:51:18 -04:00
|
|
|
|
|
|
|
See:
|
|
|
|
STM32Cube_FW_F4_V1.1.0/Drivers/BSP/Components/lis302dl/lis302dl.h
|
|
|
|
STM32Cube_FW_F4_V1.1.0/Drivers/BSP/Components/lis302dl/lis302dl.c
|
|
|
|
STM32Cube_FW_F4_V1.1.0/Drivers/BSP/STM32F4-Discovery/stm32f4_discovery.c
|
|
|
|
STM32Cube_FW_F4_V1.1.0/Drivers/BSP/STM32F4-Discovery/stm32f4_discovery.h
|
|
|
|
STM32Cube_FW_F4_V1.1.0/Drivers/BSP/STM32F4-Discovery/stm32f4_discovery_accelerometer.c
|
|
|
|
STM32Cube_FW_F4_V1.1.0/Drivers/BSP/STM32F4-Discovery/stm32f4_discovery_accelerometer.h
|
|
|
|
STM32Cube_FW_F4_V1.1.0/Projects/STM32F4-Discovery/Demonstrations/Src/main.c
|
|
|
|
"""
|
|
|
|
|
2016-11-02 21:41:11 -04:00
|
|
|
from micropython import const
|
2014-08-02 07:51:18 -04:00
|
|
|
from pyb import Pin
|
|
|
|
from pyb import SPI
|
|
|
|
|
|
|
|
READWRITE_CMD = const(0x80)
|
|
|
|
MULTIPLEBYTE_CMD = const(0x40)
|
2014-09-11 03:32:37 -04:00
|
|
|
WHO_AM_I_ADDR = const(0x0F)
|
|
|
|
OUT_X_ADDR = const(0x29)
|
|
|
|
OUT_Y_ADDR = const(0x2B)
|
|
|
|
OUT_Z_ADDR = const(0x2D)
|
|
|
|
OUT_T_ADDR = const(0x0C)
|
|
|
|
|
2014-08-26 12:23:00 -04:00
|
|
|
LIS302DL_WHO_AM_I_VAL = const(0x3B)
|
2014-08-02 07:51:18 -04:00
|
|
|
LIS302DL_CTRL_REG1_ADDR = const(0x20)
|
2014-08-10 17:26:20 -04:00
|
|
|
# Configuration for 100Hz sampling rate, +-2g range
|
2014-08-26 17:41:27 -04:00
|
|
|
LIS302DL_CONF = const(0b01000111)
|
2014-08-10 17:26:20 -04:00
|
|
|
|
2014-09-11 03:32:37 -04:00
|
|
|
LIS3DSH_WHO_AM_I_VAL = const(0x3F)
|
|
|
|
LIS3DSH_CTRL_REG4_ADDR = const(0x20)
|
|
|
|
LIS3DSH_CTRL_REG5_ADDR = const(0x24)
|
|
|
|
# Configuration for 100Hz sampling rate, +-2g range
|
|
|
|
LIS3DSH_CTRL_REG4_CONF = const(0b01100111)
|
|
|
|
LIS3DSH_CTRL_REG5_CONF = const(0b00000000)
|
2014-08-02 07:51:18 -04:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2014-08-02 07:51:18 -04:00
|
|
|
class STAccel:
|
|
|
|
def __init__(self):
|
|
|
|
self.cs_pin = Pin("PE3", Pin.OUT_PP, Pin.PULL_NONE)
|
|
|
|
self.cs_pin.high()
|
|
|
|
self.spi = SPI(1, SPI.MASTER, baudrate=328125, polarity=0, phase=1, bits=8)
|
2014-09-11 03:32:37 -04:00
|
|
|
|
|
|
|
self.who_am_i = self.read_id()
|
|
|
|
|
|
|
|
if self.who_am_i == LIS302DL_WHO_AM_I_VAL:
|
|
|
|
self.write_bytes(LIS302DL_CTRL_REG1_ADDR, bytearray([LIS302DL_CONF]))
|
|
|
|
self.sensitivity = 18
|
|
|
|
elif self.who_am_i == LIS3DSH_WHO_AM_I_VAL:
|
|
|
|
self.write_bytes(LIS3DSH_CTRL_REG4_ADDR, bytearray([LIS3DSH_CTRL_REG4_CONF]))
|
|
|
|
self.write_bytes(LIS3DSH_CTRL_REG5_ADDR, bytearray([LIS3DSH_CTRL_REG5_CONF]))
|
|
|
|
self.sensitivity = 0.06 * 256
|
|
|
|
else:
|
|
|
|
raise Exception("LIS302DL or LIS3DSH accelerometer not present")
|
|
|
|
|
|
|
|
def convert_raw_to_g(self, x):
|
|
|
|
if x & 0x80:
|
|
|
|
x = x - 256
|
|
|
|
return x * self.sensitivity / 1000
|
2014-08-02 07:51:18 -04:00
|
|
|
|
2014-08-26 17:41:27 -04:00
|
|
|
def read_bytes(self, addr, nbytes):
|
2014-08-02 07:51:18 -04:00
|
|
|
if nbytes > 1:
|
|
|
|
addr |= READWRITE_CMD | MULTIPLEBYTE_CMD
|
|
|
|
else:
|
|
|
|
addr |= READWRITE_CMD
|
|
|
|
self.cs_pin.low()
|
|
|
|
self.spi.send(addr)
|
2014-08-26 17:41:27 -04:00
|
|
|
# buf = self.spi.send_recv(bytearray(nbytes * [0])) # read data, MSB first
|
|
|
|
buf = self.spi.recv(nbytes)
|
2014-08-02 07:51:18 -04:00
|
|
|
self.cs_pin.high()
|
|
|
|
return buf
|
|
|
|
|
2014-08-26 17:41:27 -04:00
|
|
|
def write_bytes(self, addr, buf):
|
2014-08-02 07:51:18 -04:00
|
|
|
if len(buf) > 1:
|
|
|
|
addr |= MULTIPLEBYTE_CMD
|
|
|
|
self.cs_pin.low()
|
|
|
|
self.spi.send(addr)
|
|
|
|
for b in buf:
|
|
|
|
self.spi.send(b)
|
|
|
|
self.cs_pin.high()
|
|
|
|
|
|
|
|
def read_id(self):
|
2014-09-11 03:32:37 -04:00
|
|
|
return self.read_bytes(WHO_AM_I_ADDR, 1)[0]
|
2014-08-02 07:51:18 -04:00
|
|
|
|
2014-08-26 17:41:27 -04:00
|
|
|
def x(self):
|
2014-09-11 03:32:37 -04:00
|
|
|
return self.convert_raw_to_g(self.read_bytes(OUT_X_ADDR, 1)[0])
|
2014-08-26 17:41:27 -04:00
|
|
|
|
|
|
|
def y(self):
|
2014-09-11 03:32:37 -04:00
|
|
|
return self.convert_raw_to_g(self.read_bytes(OUT_Y_ADDR, 1)[0])
|
2014-08-26 17:41:27 -04:00
|
|
|
|
|
|
|
def z(self):
|
2014-09-11 03:32:37 -04:00
|
|
|
return self.convert_raw_to_g(self.read_bytes(OUT_Z_ADDR, 1)[0])
|
2014-08-26 17:41:27 -04:00
|
|
|
|
|
|
|
def xyz(self):
|
2014-09-11 03:32:37 -04:00
|
|
|
return (self.x(), self.y(), self.z())
|