|
| 1 | +""" |
| 2 | +Driver for accelerometer on STM32F429 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/l3gd20/l3gd20.h |
| 9 | + STM32Cube_FW_F4_V1.1.0/Drivers/BSP/Components/l3gd20/l3gd20.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 pyb import Pin |
| 18 | +from pyb import SPI |
| 19 | +import struct |
| 20 | + |
| 21 | +# |
8000
| 22 | +# General SPI |
| 23 | +# |
| 24 | +L3GD20_READWRITE_CMD = const(0x80) |
| 25 | +L3GD20_MULTIPLEBYTE_CMD = const(0x40) |
| 26 | +# |
| 27 | +# Constants for the SPI device. |
| 28 | +# |
| 29 | +L3GD20_WHO_AM_I_ADDR = const(0x0F) # device identification register |
| 30 | +L3GD20_CTRL_REG1_ADDR = const(0x20) # Control register 1 |
| 31 | +L3GD20_CTRL_REG2_ADDR = const(0x21) # Control register 2 |
| 32 | +L3GD20_CTRL_REG3_ADDR = const(0x22) # Control register 3 |
| 33 | +L3GD20_CTRL_REG4_ADDR = const(0x23) # Control register 4 |
| 34 | +L3GD20_CTRL_REG5_ADDR = const(0x24) # Control register 5 |
| 35 | +L3GD20_REFERENCE_REG_ADDR = const(0x25) # Reference register |
| 36 | +L3GD20_OUT_TEMP_ADDR = const(0x26) # Out temp register |
| 37 | +L3GD20_STATUS_REG_ADDR = const(0x27) # Status register |
| 38 | +L3GD20_OUT_X_L_ADDR = const(0x28) # Output Register X |
| 39 | +L3GD20_OUT_X_H_ADDR = const(0x29) # Output Register X |
| 40 | +L3GD20_OUT_Y_L_ADDR = const(0x2A) # Output Register Y |
| 41 | +L3GD20_OUT_Y_H_ADDR = const(0x2B) # Output Register Y |
| 42 | +L3GD20_OUT_Z_L_ADDR = const(0x2C) # Output Register Z |
| 43 | +L3GD20_OUT_Z_H_ADDR = const(0x2D) # Output Register Z |
| 44 | +L3GD20_FIFO_CTRL_REG_ADDR = const(0x2E) # Fifo control Register |
| 45 | +L3GD20_FIFO_SRC_REG_ADDR = const(0x2F) # Fifo src Register |
| 46 | +L3GD20_INT1_CFG_ADDR = const(0x30) # Interrupt 1 configuration Register |
| 47 | +L3GD20_INT1_SRC_ADDR = const(0x31) # Interrupt 1 source Register |
| 48 | +L3GD20_INT1_TSH_XH_ADDR = const(0x32) # Interrupt 1 Threshold X register |
| 49 | +L3GD20_INT1_TSH_XL_ADDR = const(0x33) # Interrupt 1 Threshold X register |
| 50 | +L3GD20_INT1_TSH_YH_ADDR = const(0x34) # Interrupt 1 Threshold Y register |
| 51 | +L3GD20_INT1_TSH_YL_ADDR = const(0x35) # Interrupt 1 Threshold Y register |
| 52 | +L3GD20_INT1_TSH_ZH_ADDR = const(0x36) # Interrupt 1 Threshold Z register |
| 53 | +L3GD20_INT1_TSH_ZL_ADDR = const(0x37) # Interrupt 1 Threshold Z register |
| 54 | +L3GD20_INT1_DURATION_ADDR = const(0x38) # Interrupt 1 DURATION register |
| 55 | +L3GD20_I_AM_L3GD20 = const(0xD4) |
| 56 | +L3GD20_I_AM_L3GD20_TR = const(0xD5) |
| 57 | +L3GD20_MODE_POWERDOWN = const(0x00) |
| 58 | +L3GD20_MODE_ACTIVE = const(0x08) |
| 59 | +L3GD20_OUTPUT_DATARATE_1 = const(0x00) |
| 60 | +L3GD20_OUTPUT_DATARATE_2 = const(0x40) |
| 61 | +L3GD20_OUTPUT_DATARATE_3 = const(0x80) |
| 62 | +L3GD20_OUTPUT_DATARATE_4 = const(0xC0) |
| 63 | +L3GD20_X_ENABLE = const(0x02) |
| 64 | +L3GD20_Y_ENABLE = const(0x01) |
| 65 | +L3GD20_Z_ENABLE = const(0x04) |
| 66 | +L3GD20_AXES_ENABLE = const(0x07) |
| 67 | +L3GD20_AXES_DISABLE = const(0x00) |
| 68 | +L3GD20_BANDWIDTH_1 = const(0x00) |
| 69 | +L3GD20_BANDWIDTH_2 = const(0x10) |
| 70 | +L3GD20_BANDWIDTH_3 = const(0x20) |
| 71 | +L3GD20_BANDWIDTH_4 = const(0x30) |
| 72 | +L3GD20_FULLSCALE_250 = const(0x00) |
| 73 | +L3GD20_FULLSCALE_500 = const(0x10) |
| 74 | +L3GD20_FULLSCALE_2000 = const(0x20) |
| 75 | +L3GD20_FULLSCALE_SELECTION = const(0x30) |
| 76 | +L3GD20_SENSITIVITY_250DPS = 8.75 #!< gyroscope sensitivity with 250 dps full scale [DPS/LSB] |
| 77 | +L3GD20_SENSITIVITY_500DPS = 17.50 #!< gyroscope sensitivity with 500 dps full scale [DPS/LSB] |
| 78 | +L3GD20_SENSITIVITY_2000DPS = 70.00 #!< gyroscope sensitivity with 2000 dps full scale [DPS/LSB] |
| 79 | +L3GD20_BlockDataUpdate_Continous = const(0x00) |
| 80 | +L3GD20_BlockDataUpdate_Single = const(0x80) |
| 81 | +L3GD20_BLE_LSB = const(0x00) |
| 82 | +L3GD20_BLE_MSB = const(0x40) |
| 83 | +L3GD20_HIGHPASSFILTER_DISABLE = const(0x00) |
| 84 | +L3GD20_HIGHPASSFILTER_ENABLE = const(0x10) |
| 85 | +L3GD20_INT1 = const(0x00) |
| 86 | +L3GD20_INT2 = const(0x01) |
| 87 | +L3GD20_INT1INTERRUPT_DISABLE = const(0x00) |
| 88 | +L3GD20_INT1INTERRUPT_ENABLE = const(0x80) |
| 89 | +L3GD20_INT2INTERRUPT_DISABLE = const(0x00) |
| 90 | +L3GD20_INT2INTERRUPT_ENABLE = const(0x08) |
| 91 | +L3GD20_INT1INTERRUPT_LOW_EDGE = const(0x20) |
| 92 | +L3GD20_INT1INTERRUPT_HIGH_EDGE = const(0x00) |
| 93 | +L3GD20_BOOT_NORMALMODE = const(0x00) |
| 94 | +L3GD20_BOOT_REBOOTMEMORY = const(0x80) |
| 95 | +L3GD20_HPM_NORMAL_MODE_RES = const(0x00) |
| 96 | +L3GD20_HPM_REF_SIGNAL = const(0x10) |
| 97 | +L3GD20_HPM_NORMAL_MODE = const(0x20) |
| 98 | +L3GD20_HPM_AUTORESET_INT = const(0x30) |
| 99 | +L3GD20_HPFCF_0 = const(0x00) |
| 100 | +L3GD20_HPFCF_1 = const(0x01) |
| 101 | +L3GD20_HPFCF_2 = const(0x02) |
| 102 | +L3GD20_HPFCF_3 = const(0x03) |
| 103 | +L3GD20_HPFCF_4 = const(0x04) |
| 104 | +L3GD20_HPFCF_5 = const(0x05) |
| 105 | +L3GD20_HPFCF_6 = const(0x06) |
| 106 | +L3GD20_HPFCF_7 = const(0x07) |
| 107 | +L3GD20_HPFCF_8 = const(0x08) |
| 108 | +L3GD20_HPFCF_9 = const(0x09) |
| 109 | +# |
| 110 | +# Default configuration: |
| 111 | +# Output data rate 190 Hz |
| 112 | +# Bandwidth/Cut off 50 Hz |
| 113 | +L3GD20_CTRL_REG1_VAL = const(0b01101111) |
| 114 | +# Normal Mode |
| 115 | +# Highpass cut-off 0.018Hz |
| 116 | +L3GD20_CTRL_REG2_VAL = const(0b00001001) |
| 117 | +# No interrupts |
| 118 | +L3GD20_CTRL_REG3_VAL = const(0b00000000) |
| 119 | +# Continous block update |
| 120 | +# Little endinan data |
| 121 | +# Full scale is 250 dps (degrees per second) |
| 122 | +# Use 4 wire SPI |
| 123 | +L3GD20_CTRL_REG4_VAL = const(0b00000000) |
| 124 | +# Normal Mode |
| 125 | +# FIFO is enabled |
| 126 | +# No Highpass |
| 127 | +# Int1/int2:00 |
| 128 | +# |
| 129 | +L3GD20_CTRL_REG5_VAL = const(0b01000000) |
| 130 | + |
| 131 | + |
| 132 | +class L3GD20: |
| 133 | + # |
| 134 | + # Debug |
| 135 | + # |
| 136 | + DEFAULT_CONF = [ |
| 137 | + (L3GD20_CTRL_REG1_ADDR, L3GD20_CTRL_REG1_VAL), |
| 138 | + (L3GD20_CTRL_REG2_ADDR, L3GD20_CTRL_REG2_VAL), |
| 139 | + (L3GD20_CTRL_REG3_ADDR, L3GD20_CTRL_REG3_VAL), |
| 140 | + (L3GD20_CTRL_REG4_ADDR, L3GD20_CTRL_REG4_VAL), |
| 141 | + (L3GD20_CTRL_REG5_ADDR, L3GD20_CTRL_REG5_VAL)] |
| 142 | + DEBUG = False |
| 143 | + def __init__(self, spiNr=5): |
| 144 | + self._conf={} |
| 145 | + self.cs_pin = Pin('PC1', Pin.OUT_PP, Pin.PULL_NONE) |
| 146 | + self.cs_pin.high() |
| 147 | + self.spi = SPI(spiNr, SPI.MASTER, baudrate=328125, polarity=1, phase=1, bits=8) |
| 148 | + self.who_am_i = self.read_id() |
| 149 | + if self.who_am_i == L3GD20_I_AM_L3GD20: |
| 150 | + for addr, val in L3GD20.DEFAULT_CONF: |
| 151 | + self.write_bytes(addr, bytearray([val,])) |
| 152 | + self._conf[addr] = val |
| 153 | + else: |
| 154 | + raise Exception('L3GD20 gyro not present') |
| 155 | + self.updateDpsFS() |
| 156 | + |
| 157 | + def updateDpsFS(self): |
| 158 | + conv = { |
| 159 | + L3GD20_FULLSCALE_250 : 250.0, |
| 160 | + L3GD20_FULLSCALE_500 : 500.0, |
| 161 | + L3GD20_FULLSCALE_2000 : 2000.0, |
| 162 | + L3GD20_FULLSCALE_SELECTION: 2000.0} |
| 163 | + entry = self._conf[L3GD20_CTRL_REG4_ADDR] & L3GD20_FULLSCALE_SELECTION |
| 164 | + self._dpsFS= conv[entry] |
| 165 | + |
| 166 | + def convert_raw_to_dps(self, x): |
| 167 | + x = x[1]*256+x[0] |
| 168 | + if x & 0x8000: |
| 169 | + x = x - 65536 |
| 170 | + return x * self._dpsFS / 1000.0 |
| 171 | + |
| 172 | + def read_bytes(self, addr, nbytes): |
| 173 | + addr |= L3GD20_READWRITE_CMD |
| 174 | + if nbytes > 1: |
| 175 | + addr |= L3GD20_MULTIPLEBYTE_CMD |
| 176 | + self.cs_pin.low() |
| 177 | + self.spi.send(addr) |
| 178 | + if self.DEBUG: |
| 179 | + print("SPI read addr: ", addr) |
| 180 | + buf = self.spi.recv(nbytes) |
| 181 | + self.cs_pin.high() |
| 182 | + if self.DEBUG: |
| 183 | + print("SPI read data", buf) |
| 184 | + return buf |
| 185 | + |
| 186 | + def write_bytes(self, addr, buf): |
| 187 | + if len(buf) > 1: |
| 188 | + addr |= L3GD20_MULTIPLEBYTE_CMD |
| 189 | + self.cs_pin.low() |
| 190 | + self.spi.send(addr) |
| 191 | + if self.DEBUG: |
| 192 | + print("SPI write addr: ", addr) |
| 193 | + for b in buf: |
| 194 | + self.spi.send(b) |
| 195 | + if self.DEBUG: |
| 196 | + print("SPI write data: ", b) |
| 197 | + self.cs_pin.high() |
| 198 | + |
| 199 | + def read_id(self): |
| 200 | + return self.read_bytes(L3GD20_WHO_AM_I_ADDR, 1)[0] |
| 201 | + |
| 202 | + def x(self): |
| 203 | + return self.convert_raw_to_dps(self.read_bytes(L3GD20_OUT_X_L_ADDR, 2)) |
| 204 | + |
| 205 | + def y(self): |
| 206 | + return self.convert_raw_to_dps(self.read_bytes(L3GD20_OUT_Y_L_ADDR, 2)) |
| 207 | + |
| 208 | + def z(self): |
| 209 | + return self.convert_raw_to_dps(self.read_bytes(L3GD20_OUT_Z_L_ADDR, 2)) |
| 210 | + |
| 211 | + def xyz(self): |
| 212 | + return (self.x(), self.y(), self.z()) |
| 213 | + |
| 214 | + def temp(self): |
| 215 | + val = self.read_bytes(L3GD20_OUT_TEMP_ADDR, 1)[0] |
| 216 | + if val & 0x80: |
| 217 | + val -= 256 |
| 218 | + return val |
0 commit comments