|
| 1 | +""" |
| 2 | +Driver for accelerometer on STM32F4 Discover board. |
| 3 | +
|
| 4 | +Assumes it's a LIS302DL MEMS device. |
| 5 | +
|
| 6 | +Not currently working. |
| 7 | +
|
| 8 | +See: |
| 9 | + STM32Cube_FW_F4_V1.1.0/Drivers/BSP/Components/lis302dl/lis302dl.h |
| 10 | + STM32Cube_FW_F4_V1.1.0/Drivers/BSP/Components/lis302dl/lis302dl.c |
| 11 | + STM32Cube_FW_F4_V1.1.0/Drivers/BSP/STM32F4-Discovery/stm32f4_discovery.c |
| 12 | + STM32Cube_FW_F4_V1.1.0/Drivers/BSP/STM32F4-Discovery/stm32f4_discovery.h |
| 13 | + STM32Cube_FW_F4_V1.1.0/Drivers/BSP/STM32F4-Discovery/stm32f4_discovery_accelerometer.c |
| 14 | + STM32Cube_FW_F4_V1.1.0/Drivers/BSP/STM32F4-Discovery/stm32f4_discovery_accelerometer.h |
| 15 | + STM32Cube_FW_F4_V1.1.0/Projects/STM32F4-Discovery/Demonstrations/Src/main.c |
| 16 | +""" |
| 17 | + |
| 18 | +from pyb import Pin |
| 19 | +from pyb import SPI |
| 20 | + |
| 21 | +READWRITE_CMD = const(0x80) |
| 22 | +MULTIPLEBYTE_CMD = const(0x40) |
| 23 | +LIS302DL_WHO_AM_I_ADDR = const(0x0f) |
| 24 | +LIS302DL_CTRL_REG1_ADDR = const(0x20) |
| 25 | + |
| 26 | +class STAccel: |
| 27 | + def __init__(self): |
| 28 | + self.cs_pin = Pin('PE3', Pin.OUT_PP, Pin.PULL_NONE) |
| 29 | + self.cs_pin.high() |
| 30 | + self.spi = SPI(1, SPI.MASTER, baudrate=328125, polarity=0, phase=1, bits=8) |
| 31 | + |
| 32 | + def rd(self, addr, nbytes): |
| 33 | + if nbytes > 1: |
| 34 | + addr |= READWRITE_CMD | MULTIPLEBYTE_CMD |
| 35 | + else: |
| 36 | + addr |= READWRITE_CMD |
| 37 | + self.cs_pin.low() |
| 38 | + self.spi.send(addr) |
| 39 | + buf = self.spi.send_recv(bytearray(nbytes * [0])) # read data, MSB first |
| 40 | + self.cs_pin.high() |
| 41 | + return buf |
| 42 | + |
| 43 | + def wr(self, addr, buf): |
| 44 | + if len(buf) > 1: |
| 45 | + addr |= MULTIPLEBYTE_CMD |
| 46 | + self.cs_pin.low() |
| 47 | + self.spi.send(addr) |
| 48 | + for b in buf: |
| 49 | + self.spi.send(b) |
| 50 | + self.cs_pin.high() |
| 51 | + |
| 52 | + def read_id(self): |
| 53 | + return self.rd(LIS302DL_WHO_AM_I_ADDR, 1) |
| 54 | + |
| 55 | + def init(self, init_param): |
| 56 | + self.wr(LIS302DL_CTRL_REG1_ADDR, bytearray([init_param])) |
0 commit comments