PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
l3g.h
Go to the documentation of this file.
1 
27 #pragma once
28 
29 #include <PropWare/PropWare.h>
31 
32 namespace PropWare {
33 
37 class L3G {
38  public:
42  typedef enum { X, Y, Z, AXES
47  } Axis;
48 
52  typedef enum { DPS_250 = 0x00, DPS_500 = 0x10,DPS_2000 = 0x20
56  } DPSMode;
57 
58  public:
59  static const SPI::Mode SPI_MODE = SPI::Mode::MODE_3;
60  static const SPI::BitMode SPI_BITMODE = SPI::BitMode::MSB_FIRST;
61 
62  enum class Register {
63  WHO_AM_I = 0x0F,
64  CTRL_REG1 = 0x20,
65  CTRL_REG2 = 0x21,
66  CTRL_REG3 = 0x22,
67  CTRL_REG4 = 0x23,
68  CTRL_REG5 = 0x24,
69  REFERENCE = 0x25,
70  OUT_TEMP = 0x26,
71  STATUS_REG = 0x27,
72  OUT_X_L = 0x28,
73  OUT_X_H = 0x29,
74  OUT_Y_L = 0x2A,
75  OUT_Y_H = 0x2B,
76  OUT_Z_L = 0x2C,
77  OUT_Z_H = 0x2D,
78  FIFO_CTRL_REG = 0x2E,
79  FIFO_SRC_REG = 0x2F,
80  INT1_CFG = 0x30,
81  INT1_SRC = 0x31,
82  INT1_THS_XH = 0x32,
83  INT1_THS_XL = 0x33,
84  INT1_THS_YH = 0x34,
85  INT1_THS_YL = 0x35,
86  INT1_THS_ZH = 0x36,
87  INT1_THS_ZL = 0x37,
88  INT1_DURATION = 0x38
89  };
90 
91  public:
100  static double to_dps (const int rawValue, const DPSMode dpsMode) {
101  switch (dpsMode) {
102  case DPSMode::DPS_250:
103  return rawValue * 0.00875;
104  case DPSMode::DPS_500:
105  return rawValue * 0.01750;
106  case DPSMode::DPS_2000:
107  return rawValue * 0.07000;
108  }
109  return 0;
110  }
111 
112  public:
120  L3G (SPI &spi, const Port::Mask cs, const bool alwaysSetMode = false)
121  : m_spi(&spi),
122  m_cs(cs, Pin::Dir::OUT),
123  m_alwaysSetMode(alwaysSetMode) {
124  this->m_cs.set();
125  }
126 
134  void always_set_spi_mode (const bool alwaysSetMode) {
135  this->m_alwaysSetMode = alwaysSetMode;
136  }
137 
146  void read (int16_t *values) const {
147  const auto addr = static_cast<uint8_t>(Register::OUT_X_L) | BIT_7 | BIT_6;
148 
149  this->maybe_set_spi_mode();
150 
151  this->m_cs.clear();
152  this->m_spi->shift_out(8, addr);
153  values[Axis::X] = static_cast<int16_t>(this->m_spi->shift_in(16));
154  values[Axis::Y] = static_cast<int16_t>(this->m_spi->shift_in(16));
155  values[Axis::Z] = static_cast<int16_t>(this->m_spi->shift_in(16));
156  this->m_cs.set();
157 
158  // err is useless at this point and will be used as a temporary
159  // 8-bit variable
160  uint32_t temp;
161  for (unsigned int axis = 0; axis < AXES; ++axis) {
162  temp = (uint32_t) (values[axis] >> 8);
163  values[axis] <<= 8;
164  values[axis] |= temp;
165  }
166  }
167 
175  int16_t read (const Axis axis) const {
176  return this->read16(static_cast<Register>(static_cast<unsigned int>(Register::OUT_X_L) + 2 * axis));
177  }
178 
184  int16_t read_x () const {
185  return this->read16(Register::OUT_X_L);
186  }
187 
193  int16_t read_y () const {
194  return this->read16(Register::OUT_Y_L);
195  }
196 
202  int16_t read_z () const {
203  return this->read16(Register::OUT_Z_L);
204  }
205 
213  void set_dps (const DPSMode dpsMode) const {
214  this->maybe_set_spi_mode();
215 
216  uint8_t registerValue = this->read(Register::CTRL_REG4);
217  registerValue &= ~(BIT_5 | BIT_4);
218  registerValue |= dpsMode;
219  this->write(Register::CTRL_REG4, registerValue);
220  }
221 
230  void write (const Register address, const uint8_t registerValue) const {
231  const uint8_t commandByte = static_cast<uint8_t>(address) & ~BIT_7; // Clear the RW bit (write mode)
232  const uint16_t combinedWord = commandByte << 8 | registerValue;
233 
234  this->maybe_set_spi_mode();
235 
236  this->m_cs.clear();
237  this->m_spi->shift_out(16, combinedWord);
238  this->m_cs.set();
239  }
240 
248  uint8_t read (const Register address) const {
249  // Set RW bit and enable auto-increment
250  const uint8_t commandByte = static_cast<uint8_t>(address) | BIT_7 | BIT_6;
251 
252  this->maybe_set_spi_mode();
253 
254  this->m_cs.clear();
255  this->m_spi->shift_out(8, commandByte);
256  const auto registerValue = static_cast<uint8_t>(this->m_spi->shift_in(8));
257  this->m_cs.set();
258 
259  return registerValue;
260  }
261 
271  void set_bit (const Register address, const Bit bit) const {
272  const auto startingValue = this->read(address);
273  this->write(address, bit | startingValue);
274  }
275 
285  void clear_bit (const Register address, const Bit bit) const {
286  const auto startingValue = this->read(address);
287  this->write(address, ~bit & startingValue);
288  }
289 
290  protected:
298  int16_t read16 (Register address) const {
299  // Set RW bit and enable auto-increment
300  const uint8_t commandByte = static_cast<uint8_t>(address) | BIT_7 | BIT_6;
301 
302  this->maybe_set_spi_mode();
303 
304  this->m_cs.clear();
305  this->m_spi->shift_out(8, commandByte);
306  uint16_t registerValue = static_cast<uint16_t>(this->m_spi->shift_in(16));
307  this->m_cs.set();
308 
309  uint_fast8_t temp = registerValue >> 8;
310  registerValue <<= 8;
311  return static_cast<int16_t>(registerValue | temp);
312  }
313 
319  void maybe_set_spi_mode () const {
320  if (this->m_alwaysSetMode) {
321  this->m_spi->set_mode(SPI_MODE);
322  this->m_spi->set_bit_mode(SPI_BITMODE);
323  }
324  }
325 
326  private:
327  SPI *m_spi;
328  const Pin m_cs;
329  bool m_alwaysSetMode;
330 };
331 
332 }
PropWare::SPI::set_bit_mode
void set_bit_mode(const BitMode bitmode)
Set the bitmode of SPI communication.
Definition: spi.h:181
PropWare::L3G::read_y
int16_t read_y() const
Read data from the Y axis.
Definition: l3g.h:193
PropWare::L3G::set_dps
void set_dps(const DPSMode dpsMode) const
Modify the scale of L3G in units of degrees per second.
Definition: l3g.h:213
PropWare::L3G::read
uint8_t read(const Register address) const
Read one byte from the L3G module.
Definition: l3g.h:248
PropWare::L3G
L3G gyroscope driver using SPI communication for the Parallax Propeller.
Definition: l3g.h:37
PropWare::L3G::AXES
@ AXES
Definition: l3g.h:46
PropWare::L3G::set_bit
void set_bit(const Register address, const Bit bit) const
Set a single bit in a register on the device.
Definition: l3g.h:271
PropWare::L3G::Axis
Axis
Definition: l3g.h:42
PropWare::SPI
SPI serial communications library; Core functionality comes from a dedicated assembly cog.
Definition: spi.h:43
PropWare::SPI::shift_in
uint32_t shift_in(const unsigned int bits) const
Read a value from the MISO line.
Definition: spi.h:242
PropWare::Port::Mask
Mask
Definition: port.h:43
spi.h
PropWare::L3G::to_dps
static double to_dps(const int rawValue, const DPSMode dpsMode)
Convert the raw, integer value from the gyro into units of degrees-per-second.
Definition: l3g.h:100
PropWare::L3G::Z
@ Z
Definition: l3g.h:45
PropWare::L3G::write
void write(const Register address, const uint8_t registerValue) const
Write one byte to the L3G module.
Definition: l3g.h:230
PropWare::L3G::L3G
L3G(SPI &spi, const Port::Mask cs, const bool alwaysSetMode=false)
Definition: l3g.h:120
PropWare::Pin
Utility class to handle general purpose I/O pins.
Definition: pin.h:36
PropWare::L3G::read_z
int16_t read_z() const
Read data from the Z axis.
Definition: l3g.h:202
PropWare::L3G::X
@ X
Definition: l3g.h:43
PropWare.h
PropWare::SPI::shift_out
void shift_out(uint8_t bits, uint32_t value) const
Send a value out to a peripheral device.
Definition: spi.h:224
PropWare::L3G::read_x
int16_t read_x() const
Read data from the X axis.
Definition: l3g.h:184
PropWare::L3G::read
int16_t read(const Axis axis) const
Read a specific axis's data.
Definition: l3g.h:175
PropWare::SPI::Mode::MODE_3
@ MODE_3
PropWare::Port::set
void set() const
Set selected output port high (set all pins to 1)
Definition: port.h:226
PropWare::SPI::set_mode
void set_mode(const Mode mode)
Set the mode of SPI communication.
Definition: spi.h:166
PropWare::L3G::clear_bit
void clear_bit(const Register address, const Bit bit) const
Clear a single bit in a register on the device.
Definition: l3g.h:285
PropWare::L3G::DPS_250
@ DPS_250
Definition: l3g.h:53
PropWare::L3G::Y
@ Y
Definition: l3g.h:44
PropWare::L3G::read
void read(int16_t *values) const
Read data from all three axes.
Definition: l3g.h:146
PropWare::SPI::BitMode
BitMode
Determine if data is communicated with the LSB or MSB sent/received first.
Definition: spi.h:78
SPI
Definition: SPI.h:11
PropWare::L3G::DPSMode
DPSMode
Definition: l3g.h:52
PropWare::L3G::DPS_2000
@ DPS_2000
Definition: l3g.h:55
PropWare
Generic definitions and functions for the Parallax Propeller.
Definition: runnable.h:33
PropWare::Port::clear
void clear() const
Clear selected output port (set it to 0)
Definition: port.h:249
PropWare::L3G::DPS_500
@ DPS_500
Definition: l3g.h:54
PropWare::L3G::always_set_spi_mode
void always_set_spi_mode(const bool alwaysSetMode)
Choose whether to always set the SPI mode and bitmode before reading or writing to the L3G module; Us...
Definition: l3g.h:134
PropWare::SPI::Mode
Mode
Descriptor for SPI signal as defined by Motorola modes.
Definition: spi.h:66