PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
adxl345.h
Go to the documentation of this file.
1 
26 #pragma once
27 
29 
30 namespace PropWare {
31 
32 class ADXL345 {
33  public:
37  enum class Register {
43  DEVICE_ID,
49  TAP_THRESHOLD = 0x1D,
85  TAP_WINDOW,
127  TAP_AXES,
134  TAP_SOURCE,
170  DATA_FORMAT,
176  X_AXIS_0,
182  X_AXIS_1,
188  Y_AXIS_0,
194  Y_AXIS_1,
200  Z_AXIS_0,
206  Z_AXIS_1,
212  FIFO_CONTROL,
219  };
220 
224  typedef enum {
225  X,
226  Y,
227  Z,
228  AXES
229  } Axis;
230 
248  } DataRate;
249 
253  typedef enum { HZ_8, HZ_4, HZ_2, HZ_1
258  } WakeupFrequency;
259 
265  typedef enum { _2G, _4G, _8G, _16G
270  } Range;
271 
275  typedef enum {
294  } FIFOMode;
295 
300  struct {
302  unsigned int inactiveZEnable: 1;
304  unsigned int inactiveYEnable: 1;
306  unsigned int inactiveXEnable: 1;
308  unsigned int inactiveAcDc: 1;
310  unsigned int activeZEnable: 1;
312  unsigned int activeYEnable: 1;
314  unsigned int activeXEnable: 1;
316  unsigned int activeAcDc: 1;
317  } fields;
318  uint8_t raw;
319  };
320 
324  union TapAxes {
325  struct {
327  unsigned int tapZEnable: 1;
329  unsigned int tapYEnable: 1;
331  unsigned int tapXEnable: 1;
336  unsigned int suppress: 1;
337  } fields;
338  uint8_t raw;
339  };
340 
344  union TapSource {
345  struct {
347  unsigned int zTapped: 1;
349  unsigned int yTapped: 1;
351  unsigned int xTapped: 1;
353  unsigned int asleep: 1;
355  unsigned int zActivity: 1;
357  unsigned int yActivity: 1;
359  unsigned int xActivity: 1;
360  } fields;
361  uint8_t raw;
362  };
363 
368  struct {
372  DataRate dataRate: 3;
377  unsigned int lowPowerMode: 1;
378  } fields;
379  uint8_t raw;
380  };
381 
385  union PowerControl {
386  struct {
390  WakeupFrequency wakeup: 2;
401  unsigned int sleep: 1;
406  unsigned int measure: 1;
419  unsigned int autoSleep: 1;
424  unsigned int link: 1;
425  } fields;
426  uint8_t raw;
427  };
428 
433  struct { unsigned int overrun: 1; unsigned int watermark: 1; unsigned int freeFall: 1; unsigned int inactivity: 1; unsigned int activity: 1; unsigned int doubleTap: 1; unsigned int singleTap: 1; unsigned int dataReady: 1;
442  } fields;
443  uint8_t raw;
444  };
445 
450  struct { unsigned int
454  overrun: 1; unsigned int
458  watermark: 1; unsigned int
462  freeFall: 1; unsigned int
466  inactivity: 1; unsigned int
470  activity: 1; unsigned int
474  doubleTap: 1; unsigned int
478  singleTap: 1; unsigned int
482  dataReady: 1;
483  } fields;
484  uint8_t raw;
485  };
486 
491  struct { unsigned int overrun: 1; unsigned int watermark: 1; unsigned int freeFall: 1; unsigned int inactivity: 1; unsigned int activity: 1; unsigned int doubleTap: 1; unsigned int singleTap: 1; unsigned int dataReady: 1;
500  } fields;
501  uint8_t raw;
502  };
503 
507  union DataFormat {
508  struct {
512  Range range: 2;
517  unsigned int justify: 1;
522  unsigned int fullResolution: 1;
526  unsigned int invertedInterrupts: 1;
530  unsigned int spiMode;
535  unsigned int selfTest;
536  } fields;
537  uint8_t raw;
538  };
539 
543  union FIFOControl {
544  struct {
551  unsigned int samples: 5;
556  unsigned int trigger: 1;
560  FIFOMode fifoMode: 2;
561  } fields;
562  uint8_t raw;
563  };
564 
568  union FIFOStatus {
569  struct { unsigned int entries: 6; unsigned int unused: 1; unsigned int fifoTrigger: 1;
573  } fields;
574  uint8_t raw;
575  };
576 
577  public:
578  static const SPI::Mode SPI_MODE = SPI::Mode::MODE_3;
579 
583  static const uint8_t DEVICE_ID = 0xE5;
584 
590  static constexpr double FULL_RESOLUTION_SCALE = 0.004;
591 
603  static double scale (const int value, const Range range) {
604  return value * (1 << range) * FULL_RESOLUTION_SCALE;
605  }
606 
607  public:
615  ADXL345 (SPI &bus, const Pin::Mask csMask, const bool alwaysSetSPIMode = false)
616  : m_bus(&bus),
617  m_cs(csMask, Pin::Dir::OUT),
618  m_alwaysSetMode(alwaysSetSPIMode) {
619  if (!alwaysSetSPIMode)
620  this->m_bus->set_mode(SPI_MODE);
621  this->m_cs.set();
622  }
623 
630  ADXL345 (const Pin::Mask csMask, const bool alwaysSetSPIMode = true)
631  : m_bus(&SPI::get_instance()),
632  m_cs(csMask, Pin::Dir::OUT),
633  m_alwaysSetMode(alwaysSetSPIMode) {
634  if (!alwaysSetSPIMode)
635  this->m_bus->set_mode(SPI::Mode::MODE_1);
636  this->m_cs.set();
637  }
638 
645  void always_set_spi_mode (const bool alwaysSetMode) {
646  this->m_alwaysSetMode = alwaysSetMode;
647  }
648 
660  void start (const bool autoSleep = false, const bool link = false, const bool sleep = false,
661  const bool measure = true, const WakeupFrequency wakeupFrequency = WakeupFrequency::HZ_8) const {
663  wakeup: wakeupFrequency,
664  sleep: static_cast<unsigned int>(sleep),
665  measure: static_cast<unsigned int>(measure),
666  autoSleep: static_cast<unsigned int>(autoSleep),
667  link: static_cast<unsigned int>(link)
668  }}.raw);
669  }
670 
678  void read (int16_t *values) const {
679  this->read(Register::X_AXIS_0, AXES, values);
680  }
681 
689  int16_t read_axis (const Axis axis) const {
690  int16_t temp[3];
691  this->read(temp);
692  return temp[static_cast<size_t>(axis)];
693  }
694 
700  int16_t read_x () const {
701  return this->read_axis(Axis::X);
702  }
703 
709  int16_t read_y () const {
710  return this->read_axis(Axis::Y);
711  }
712 
718  int16_t read_z () const {
719  return this->read_axis(Axis::Z);
720  }
721 
728  void write (const Register address, const uint8_t value) const {
729  if (this->m_alwaysSetMode)
730  this->m_bus->set_mode(SPI_MODE);
731 
732  this->m_cs.clear();
733  this->m_bus->shift_out(8, static_cast<uint32_t>(address));
734  this->m_bus->shift_out(8, value);
735  this->m_cs.set();
736  }
737 
745  uint8_t read (const Register address) const {
746  if (this->m_alwaysSetMode)
747  this->m_bus->set_mode(SPI_MODE);
748 
749  this->m_cs.clear();
750  this->m_bus->shift_out(8, BIT_7 | static_cast<uint32_t>(address));
751  const uint8_t result = static_cast<const uint8_t>(this->m_bus->shift_in(8));
752  this->m_cs.set();
753  return result;
754  }
755 
763  void read (const Register startingAddress, const size_t words, int16_t *result) const {
764  if (this->m_alwaysSetMode)
765  this->m_bus->set_mode(SPI_MODE);
766 
767  this->m_cs.clear();
768  this->m_bus->shift_out(8, BIT_7 | BIT_6 | static_cast<uint32_t>(startingAddress));
769  for (size_t i = 0; i < words; ++i) {
770  const uint8_t lowByte = static_cast<const uint8_t>(this->m_bus->shift_in(8));
771  const uint8_t highByte = static_cast<const uint8_t>(this->m_bus->shift_in(8));
772  result[i] = highByte << 8 | lowByte;
773  }
774  this->m_cs.set();
775  }
776 
786  void set_bit (const Register address, const Bit bit) const {
787  const auto startingValue = this->read(address);
788  this->write(address, bit | startingValue);
789  }
790 
800  void clear_bit (const Register address, const Bit bit) const {
801  const auto startingValue = this->read(address);
802  this->write(address, ~bit & startingValue);
803  }
804 
805  private:
806  SPI *m_bus;
807  const Pin m_cs;
808  bool m_alwaysSetMode;
809 };
810 
811 }
PropWare::ADXL345::write
void write(const Register address, const uint8_t value) const
Perform a manual write to the device.
Definition: adxl345.h:728
PropWare::ADXL345::Register::TAP_WINDOW
@ TAP_WINDOW
Tap window.
PropWare::ADXL345::Register::X_AXIS_1
@ X_AXIS_1
X-Axis Data 1.
PropWare::ADXL345::Register::X_AXIS_0
@ X_AXIS_0
X-Axis Data 0.
PropWare::ADXL345::_100_HZ
@ _100_HZ
Definition: adxl345.h:242
PropWare::ADXL345::HZ_4
@ HZ_4
Definition: adxl345.h:255
PropWare::ADXL345::_400_HZ
@ _400_HZ
Definition: adxl345.h:244
PropWare::ADXL345::_8G
@ _8G
Definition: adxl345.h:268
PropWare::ADXL345::Register::INTERRUPT_SOURCES
@ INTERRUPT_SOURCES
Source of interrupts.
PropWare::ADXL345::read
uint8_t read(const Register address) const
Perform a single manual read from the device.
Definition: adxl345.h:745
PropWare::ADXL345::FIFOControl
Bit-mapping for Register::FIFO_CONTROL.
Definition: adxl345.h:543
PropWare::ADXL345::_200_HZ
@ _200_HZ
Definition: adxl345.h:243
PropWare::ADXL345::STREAM
@ STREAM
Definition: adxl345.h:288
PropWare::ADXL345::Register::INACTIVITY_THRESHOLD
@ INACTIVITY_THRESHOLD
Inactivity threshold.
PropWare::ADXL345::PowerControl
Bit-mapping for Register::POWER_SAVINGS.
Definition: adxl345.h:385
PropWare::ADXL345::read_x
int16_t read_x() const
Read data for all three axes and return only the x-axis.
Definition: adxl345.h:700
PropWare::ADXL345::Register::TAP_AXES
@ TAP_AXES
Axis control for tap/double tap.
PropWare::ADXL345::Register::INTERRUPT_MAPPING
@ INTERRUPT_MAPPING
Interrupt mapping control.
PropWare::ADXL345::start
void start(const bool autoSleep=false, const bool link=false, const bool sleep=false, const bool measure=true, const WakeupFrequency wakeupFrequency=WakeupFrequency::HZ_8) const
Convenience function to start the device in a sane fashion for basic reading.
Definition: adxl345.h:660
PropWare::ADXL345::Register::Y_AXIS_OFFSET
@ Y_AXIS_OFFSET
Y-axis offset.
PropWare::ADXL345::Axis
Axis
Convenience enumeration for selecting axes via the ADXL345::read_axis() method.
Definition: adxl345.h:224
PropWare::ADXL345::FIFO
@ FIFO
Definition: adxl345.h:284
PropWare::ADXL345::read
void read(const Register startingAddress, const size_t words, int16_t *result) const
Perform a manual, multi-byte read from the device.
Definition: adxl345.h:763
PropWare::ADXL345::Register::Y_AXIS_1
@ Y_AXIS_1
Y-Axis Data 1.
PropWare::SPI
SPI serial communications library; Core functionality comes from a dedicated assembly cog.
Definition: spi.h:43
PropWare::ADXL345::_12_5_HZ
@ _12_5_HZ
Definition: adxl345.h:239
PropWare::ADXL345::Register::TAP_LATENCY
@ TAP_LATENCY
Tap latency.
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::ADXL345::Register::ACT_INACT_CTL
@ ACT_INACT_CTL
Axis enable control for activity and inactivity detection.
PropWare::ADXL345::ActivityDetection
Bit-mapping for Register::ACT_INACT_CTL.
Definition: adxl345.h:299
PropWare::Port::Mask
Mask
Definition: port.h:43
PropWare::ADXL345::FULL_RESOLUTION_SCALE
static constexpr double FULL_RESOLUTION_SCALE
The highest resolution that the device is capable of. Resolution will become less precise (larger num...
Definition: adxl345.h:590
PropWare::ADXL345::FIFOStatus
Bit-mapping for Register::FIFO_STATUS.
Definition: adxl345.h:568
PropWare::ADXL345::read
void read(int16_t *values) const
Retrieve the values from all 3 axes with a fast, 6-byte successive read.
Definition: adxl345.h:678
PropWare::ADXL345::HZ_2
@ HZ_2
Definition: adxl345.h:256
PropWare::ADXL345::Register::X_AXIS_OFFSET
@ X_AXIS_OFFSET
X-axis offset.
PropWare::ADXL345::HZ_8
@ HZ_8
Definition: adxl345.h:254
spi.h
PropWare::ADXL345::_1600_HZ
@ _1600_HZ
Definition: adxl345.h:246
PropWare::ADXL345::Register::RATE_AND_POWER_MODE
@ RATE_AND_POWER_MODE
Data rate and power mode control.
PropWare::ADXL345::Register::TAP_THRESHOLD
@ TAP_THRESHOLD
Tap threshold.
PropWare::ADXL345::DEVICE_ID
static const uint8_t DEVICE_ID
The ADXL345 is hardwired for a device ID of 0xE5
Definition: adxl345.h:583
PropWare::ADXL345::Register::POWER_CONTROL
@ POWER_CONTROL
Control of the power-saving features.
PropWare::ADXL345::InterruptEnable
Bit-mapping for Register::INTERRUPT_ENABLE.
Definition: adxl345.h:432
PropWare::ADXL345::always_set_spi_mode
void always_set_spi_mode(const bool alwaysSetMode)
Choose whether to always set the SPI mode before writing to the device; Useful when multiple devices ...
Definition: adxl345.h:645
PropWare::ADXL345::read_axis
int16_t read_axis(const Axis axis) const
Read data for all three axes and return only the requested axis.
Definition: adxl345.h:689
PropWare::ADXL345::Register::INTERRUPT_ENABLE
@ INTERRUPT_ENABLE
Interrupt enable control.
PropWare::ADXL345::Register::DATA_FORMAT
@ DATA_FORMAT
Data format control.
PropWare::Pin
Utility class to handle general purpose I/O pins.
Definition: pin.h:36
PropWare::ADXL345::_800_HZ
@ _800_HZ
Definition: adxl345.h:245
PropWare::ADXL345::TapAxes
Bit-mapping for Register::TAP_AXES.
Definition: adxl345.h:324
PropWare::ADXL345::_50_HZ
@ _50_HZ
Definition: adxl345.h:241
justify
void justify(char *string_ptr, uint32_t *justptr)
Definition: Graphics.c:526
PropWare::ADXL345::FIFOMode
FIFOMode
Data codes for the various modes of operation that the FIFO can assume.
Definition: adxl345.h:275
PropWare::ADXL345::_25_HZ
@ _25_HZ
Definition: adxl345.h:240
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::SPI::Mode::MODE_3
@ MODE_3
PropWare::ADXL345::Register::Z_AXIS_0
@ Z_AXIS_0
Z-Axis Data 0.
PropWare::Port::set
void set() const
Set selected output port high (set all pins to 1)
Definition: port.h:226
PropWare::ADXL345::clear_bit
void clear_bit(const Register address, const Bit bit) const
Clear a single bit in a register on the device.
Definition: adxl345.h:800
PropWare::ADXL345::read_y
int16_t read_y() const
Read data for all three axes and return only the y-axis.
Definition: adxl345.h:709
PropWare::ADXL345::ADXL345
ADXL345(const Pin::Mask csMask, const bool alwaysSetSPIMode=true)
Create an object which communicates over the shared SPI bus.
Definition: adxl345.h:630
PropWare::SPI::set_mode
void set_mode(const Mode mode)
Set the mode of SPI communication.
Definition: spi.h:166
PropWare::ADXL345::ADXL345
ADXL345(SPI &bus, const Pin::Mask csMask, const bool alwaysSetSPIMode=false)
Create an object which communicates over the given SPI bus.
Definition: adxl345.h:615
PropWare::ADXL345::InterruptSources
Bit-mapping for Register::INTERRUPT_SOURCES.
Definition: adxl345.h:490
PropWare::ADXL345::DataRate
DataRate
Data rate codes for the Register::RATE_AND_POWER_MODE register.
Definition: adxl345.h:237
PropWare::ADXL345::HZ_1
@ HZ_1
Definition: adxl345.h:257
PropWare::ADXL345::Register::TAP_DURATION
@ TAP_DURATION
Tap duration.
PropWare::ADXL345
Definition: adxl345.h:32
PropWare::ADXL345::TRIGGER
@ TRIGGER
Definition: adxl345.h:293
PropWare::ADXL345::RateAndPowerMode
Bit-mapping for Register::RATE_AND_POWER_MODE.
Definition: adxl345.h:367
PropWare::ADXL345::TapSource
Bit-mapping for Register::TAP_SOURCE.
Definition: adxl345.h:344
PropWare::ADXL345::Register::INACTIVITY_TIME
@ INACTIVITY_TIME
Inactivity time.
PropWare::ADXL345::DataFormat
Bit-mapping for Register::DATA_FORMAT.
Definition: adxl345.h:507
PropWare::ADXL345::_6_25_HZ
@ _6_25_HZ
Definition: adxl345.h:238
PropWare::ADXL345::BYPASS
@ BYPASS
Definition: adxl345.h:279
PropWare::ADXL345::read_z
int16_t read_z() const
Read data for all three axes and return only the z-axis.
Definition: adxl345.h:718
PropWare::ADXL345::Register::Z_AXIS_OFFSET
@ Z_AXIS_OFFSET
Z-axis offset.
PropWare::ADXL345::Register
Register
Register map for all registers on the device.
Definition: adxl345.h:37
PropWare::ADXL345::set_bit
void set_bit(const Register address, const Bit bit) const
Set a single bit in a register on the device.
Definition: adxl345.h:786
PropWare::ADXL345::_3200_HZ
@ _3200_HZ
Definition: adxl345.h:247
PropWare::ADXL345::Register::DEVICE_ID
@ DEVICE_ID
Device ID.
PropWare::ADXL345::Register::Z_AXIS_1
@ Z_AXIS_1
Z-Axis Data 1.
PropWare::ADXL345::Register::FREE_FALL_TIME
@ FREE_FALL_TIME
Free-fall time Name: TIME_FF, Access: read/write, Reset value: 0
PropWare::ADXL345::Register::Y_AXIS_0
@ Y_AXIS_0
Y-Axis Data 0.
PropWare::ADXL345::_2G
@ _2G
Definition: adxl345.h:266
PropWare::SPI::Mode::MODE_1
@ MODE_1
PropWare::ADXL345::_4G
@ _4G
Definition: adxl345.h:267
PropWare::ADXL345::_16G
@ _16G
Definition: adxl345.h:269
PropWare::ADXL345::Register::TAP_SOURCE
@ TAP_SOURCE
Source of tap/double tap.
PropWare::ADXL345::Register::FIFO_CONTROL
@ FIFO_CONTROL
FIFO control.
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::ADXL345::Register::FIFO_STATUS
@ FIFO_STATUS
FIFO status.
PropWare::ADXL345::Range
Range
Selection of data ranges.
Definition: adxl345.h:265
PropWare::ADXL345::InterruptMapping
Bit-mapping for Register::INTERRUPT_MAPPING.
Definition: adxl345.h:449
PropWare::ADXL345::Register::FREE_FALL_THRESHOLD
@ FREE_FALL_THRESHOLD
Free-fall threshold.
PropWare::ADXL345::WakeupFrequency
WakeupFrequency
Wakeup frequency codes for the Register::POWER_SAVINGS register.
Definition: adxl345.h:253
PropWare::ADXL345::scale
static double scale(const int value, const Range range)
Scale the non-justified, raw accelerometer value to a human-readable number.
Definition: adxl345.h:603
PropWare::SPI::Mode
Mode
Descriptor for SPI signal as defined by Motorola modes.
Definition: spi.h:66
PropWare::ADXL345::Register::ACTIVITY_THRESHOLD
@ ACTIVITY_THRESHOLD
Activity threshold.