base program for tilt measurement
Dependencies: COG4050_ADT7420 ADXL362
Fork of COG4050_adxl355_adxl357-ver2 by
ADXL35x/ADXL355.h@3:ee052fdb4331, 2018-08-08 (annotated)
- Committer:
- vtoffoli
- Date:
- Wed Aug 08 08:56:00 2018 +0000
- Revision:
- 3:ee052fdb4331
- Parent:
- 2:14dc1ec57f3b
- Child:
- 4:23b53636b576
cog4050 for tilt measurement; adxl35x, adxrs290
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vtoffoli | 2:14dc1ec57f3b | 1 | |
vtoffoli | 2:14dc1ec57f3b | 2 | #ifndef ADXL355_H_ |
vtoffoli | 2:14dc1ec57f3b | 3 | #define ADXL355_H_ |
vtoffoli | 2:14dc1ec57f3b | 4 | |
vtoffoli | 2:14dc1ec57f3b | 5 | class ADXL355 |
vtoffoli | 2:14dc1ec57f3b | 6 | { |
vtoffoli | 2:14dc1ec57f3b | 7 | public: |
vtoffoli | 2:14dc1ec57f3b | 8 | |
vtoffoli | 2:14dc1ec57f3b | 9 | // -------------------------- // |
vtoffoli | 2:14dc1ec57f3b | 10 | // REGISTERS // |
vtoffoli | 2:14dc1ec57f3b | 11 | // -------------------------- // |
vtoffoli | 2:14dc1ec57f3b | 12 | typedef enum { |
vtoffoli | 2:14dc1ec57f3b | 13 | DEVID_AD = 0x00, |
vtoffoli | 2:14dc1ec57f3b | 14 | DEVID_MST = 0x01, |
vtoffoli | 2:14dc1ec57f3b | 15 | PARTID = 0x02, |
vtoffoli | 2:14dc1ec57f3b | 16 | REVID = 0x03, |
vtoffoli | 2:14dc1ec57f3b | 17 | STATUS = 0x04, |
vtoffoli | 2:14dc1ec57f3b | 18 | FIFO_ENTRIES = 0x05, |
vtoffoli | 2:14dc1ec57f3b | 19 | TEMP2 = 0x06, |
vtoffoli | 2:14dc1ec57f3b | 20 | TEMP1 = 0x07, |
vtoffoli | 2:14dc1ec57f3b | 21 | XDATA3 = 0x08, |
vtoffoli | 2:14dc1ec57f3b | 22 | XDATA2 = 0x09, |
vtoffoli | 2:14dc1ec57f3b | 23 | XDATA1 = 0x0A, |
vtoffoli | 2:14dc1ec57f3b | 24 | YDATA3 = 0x0B, |
vtoffoli | 2:14dc1ec57f3b | 25 | YDATA2 = 0x0C, |
vtoffoli | 2:14dc1ec57f3b | 26 | YDATA1 = 0x0D, |
vtoffoli | 2:14dc1ec57f3b | 27 | ZDATA3 = 0x0E, |
vtoffoli | 2:14dc1ec57f3b | 28 | ZDATA2 = 0x0F, |
vtoffoli | 2:14dc1ec57f3b | 29 | ZDATA1 = 0x10, |
vtoffoli | 2:14dc1ec57f3b | 30 | FIFO_DATA = 0x11, |
vtoffoli | 2:14dc1ec57f3b | 31 | OFFSET_X_H = 0x1E, |
vtoffoli | 2:14dc1ec57f3b | 32 | OFFSET_X_L = 0x1F, |
vtoffoli | 2:14dc1ec57f3b | 33 | OFFSET_Y_H = 0x20, |
vtoffoli | 2:14dc1ec57f3b | 34 | OFFSET_Y_L = 0x21, |
vtoffoli | 2:14dc1ec57f3b | 35 | OFFSET_Z_H = 0x22, |
vtoffoli | 2:14dc1ec57f3b | 36 | OFFSET_Z_L = 0x23, |
vtoffoli | 2:14dc1ec57f3b | 37 | ACT_EN = 0x24, |
vtoffoli | 2:14dc1ec57f3b | 38 | ACT_THRESH_H = 0x25, |
vtoffoli | 2:14dc1ec57f3b | 39 | ACT_THRESH_L = 0x26, |
vtoffoli | 2:14dc1ec57f3b | 40 | ACT_COUNT = 0x27, |
vtoffoli | 2:14dc1ec57f3b | 41 | FILTER = 0x28, |
vtoffoli | 2:14dc1ec57f3b | 42 | FIFO_SAMPLES = 0x29, |
vtoffoli | 2:14dc1ec57f3b | 43 | INT_MAP = 0x2A, |
vtoffoli | 2:14dc1ec57f3b | 44 | SYNC = 0x2B, |
vtoffoli | 2:14dc1ec57f3b | 45 | RANGE = 0x2C, |
vtoffoli | 2:14dc1ec57f3b | 46 | POWER_CTL = 0x2D, |
vtoffoli | 2:14dc1ec57f3b | 47 | SELF_TEST = 0x2E, |
vtoffoli | 2:14dc1ec57f3b | 48 | RESET = 0x2F |
vtoffoli | 2:14dc1ec57f3b | 49 | } ADXL355_register_t; |
vtoffoli | 2:14dc1ec57f3b | 50 | |
vtoffoli | 2:14dc1ec57f3b | 51 | // -------------------------- // |
vtoffoli | 2:14dc1ec57f3b | 52 | // REGISTERS - DEFAULT VALUES // |
vtoffoli | 2:14dc1ec57f3b | 53 | // -------------------------- // |
vtoffoli | 2:14dc1ec57f3b | 54 | // Modes - POWER_CTL |
vtoffoli | 2:14dc1ec57f3b | 55 | typedef enum { |
vtoffoli | 2:14dc1ec57f3b | 56 | DRDY_OFF = 0x04, |
vtoffoli | 2:14dc1ec57f3b | 57 | TEMP_OFF = 0x02, |
vtoffoli | 2:14dc1ec57f3b | 58 | STANDBY = 0x00, |
vtoffoli | 2:14dc1ec57f3b | 59 | MEASUREMENT = 0x01 |
vtoffoli | 2:14dc1ec57f3b | 60 | } ADXL355_modes_t; |
vtoffoli | 2:14dc1ec57f3b | 61 | // Activate Threshold - ACT_EN |
vtoffoli | 2:14dc1ec57f3b | 62 | typedef enum { |
vtoffoli | 2:14dc1ec57f3b | 63 | ACT_Z = 0x04, |
vtoffoli | 2:14dc1ec57f3b | 64 | ACT_Y = 0x02, |
vtoffoli | 2:14dc1ec57f3b | 65 | ACT_X = 0x01 |
vtoffoli | 2:14dc1ec57f3b | 66 | } ADXL355_act_ctl_t; |
vtoffoli | 2:14dc1ec57f3b | 67 | // High-Pass and Low-Pass Filter - FILTER |
vtoffoli | 2:14dc1ec57f3b | 68 | typedef enum { |
vtoffoli | 2:14dc1ec57f3b | 69 | HPFOFF = 0x00, |
vtoffoli | 2:14dc1ec57f3b | 70 | HPF247 = 0x10, |
vtoffoli | 2:14dc1ec57f3b | 71 | HPF62 = 0x20, |
vtoffoli | 2:14dc1ec57f3b | 72 | HPF15 = 0x30, |
vtoffoli | 2:14dc1ec57f3b | 73 | HPF3 = 0x40, |
vtoffoli | 2:14dc1ec57f3b | 74 | HPF09 = 0x50, |
vtoffoli | 2:14dc1ec57f3b | 75 | HPF02 = 0x60, |
vtoffoli | 2:14dc1ec57f3b | 76 | ODR4000HZ = 0x00, |
vtoffoli | 2:14dc1ec57f3b | 77 | ODR2000HZ = 0x01, |
vtoffoli | 2:14dc1ec57f3b | 78 | ODR1000HZ = 0x02, |
vtoffoli | 2:14dc1ec57f3b | 79 | ODR500HZ = 0x03, |
vtoffoli | 2:14dc1ec57f3b | 80 | ODR250HZ = 0x04, |
vtoffoli | 2:14dc1ec57f3b | 81 | ODR125Hz = 0x05, |
vtoffoli | 2:14dc1ec57f3b | 82 | ODR62HZ = 0x06, |
vtoffoli | 2:14dc1ec57f3b | 83 | ODR31Hz = 0x07, |
vtoffoli | 2:14dc1ec57f3b | 84 | ODR15Hz = 0x08, |
vtoffoli | 2:14dc1ec57f3b | 85 | ODR7Hz = 0x09, |
vtoffoli | 2:14dc1ec57f3b | 86 | ODR3HZ = 0x0A |
vtoffoli | 2:14dc1ec57f3b | 87 | } ADXL355_filter_ctl_t; |
vtoffoli | 2:14dc1ec57f3b | 88 | // External timing register - INT_MAP |
vtoffoli | 2:14dc1ec57f3b | 89 | typedef enum { |
vtoffoli | 2:14dc1ec57f3b | 90 | OVR_EN = 0x04, |
vtoffoli | 2:14dc1ec57f3b | 91 | FULL_EN = 0x02, |
vtoffoli | 2:14dc1ec57f3b | 92 | RDY_EN = 0x01 |
vtoffoli | 2:14dc1ec57f3b | 93 | } ADXL355_intmap_ctl_t; |
vtoffoli | 2:14dc1ec57f3b | 94 | // External timing register - SYNC |
vtoffoli | 2:14dc1ec57f3b | 95 | typedef enum { |
vtoffoli | 2:14dc1ec57f3b | 96 | EXT_CLK = 0x04, |
vtoffoli | 2:14dc1ec57f3b | 97 | INT_SYNC = 0x00, |
vtoffoli | 2:14dc1ec57f3b | 98 | EXT_SYNC_NO_INT = 0x01, |
vtoffoli | 2:14dc1ec57f3b | 99 | EXT_SYNC_INT = 0x02 |
vtoffoli | 2:14dc1ec57f3b | 100 | } ADXL355_sync_ctl_t; |
vtoffoli | 2:14dc1ec57f3b | 101 | // polarity and range - RANGE |
vtoffoli | 2:14dc1ec57f3b | 102 | typedef enum { |
vtoffoli | 2:14dc1ec57f3b | 103 | RANGE2G = 0x01, |
vtoffoli | 2:14dc1ec57f3b | 104 | RANGE4G = 0x02, |
vtoffoli | 2:14dc1ec57f3b | 105 | RANGE8G = 0x03, |
vtoffoli | 2:14dc1ec57f3b | 106 | RANGE10 = 0x00, |
vtoffoli | 2:14dc1ec57f3b | 107 | RANGE20 = 0x02, |
vtoffoli | 2:14dc1ec57f3b | 108 | RANGE40 = 0x03 |
vtoffoli | 2:14dc1ec57f3b | 109 | } ADXL355_range_ctl_t; |
vtoffoli | 2:14dc1ec57f3b | 110 | // self test interrupt - INT |
vtoffoli | 2:14dc1ec57f3b | 111 | typedef enum { |
vtoffoli | 2:14dc1ec57f3b | 112 | ST2 = 0x02, |
vtoffoli | 2:14dc1ec57f3b | 113 | ST1 = 0x01 |
vtoffoli | 2:14dc1ec57f3b | 114 | } ADXL355_int_ctl_t; |
vtoffoli | 2:14dc1ec57f3b | 115 | |
vtoffoli | 2:14dc1ec57f3b | 116 | // -------------------------- // |
vtoffoli | 2:14dc1ec57f3b | 117 | // FUNCTIONS // |
vtoffoli | 2:14dc1ec57f3b | 118 | // -------------------------- // |
vtoffoli | 2:14dc1ec57f3b | 119 | // SPI configuration & constructor |
vtoffoli | 2:14dc1ec57f3b | 120 | ADXL355(PinName cs_pin , PinName MOSI , PinName MISO , PinName SCK ); |
vtoffoli | 2:14dc1ec57f3b | 121 | void frequency(int hz); |
vtoffoli | 2:14dc1ec57f3b | 122 | // Low level SPI bus comm methods |
vtoffoli | 2:14dc1ec57f3b | 123 | void reset(void); |
vtoffoli | 2:14dc1ec57f3b | 124 | void write_reg(ADXL355_register_t reg, uint8_t data); |
vtoffoli | 2:14dc1ec57f3b | 125 | void write_reg_u16(ADXL355_register_t reg, uint16_t data); |
vtoffoli | 2:14dc1ec57f3b | 126 | uint8_t read_reg(ADXL355_register_t reg); |
vtoffoli | 2:14dc1ec57f3b | 127 | uint16_t read_reg_u16(ADXL355_register_t reg); |
vtoffoli | 2:14dc1ec57f3b | 128 | uint32_t read_reg_u32(ADXL355_register_t reg); |
vtoffoli | 2:14dc1ec57f3b | 129 | // ADXL general register R/W methods |
vtoffoli | 2:14dc1ec57f3b | 130 | void set_power_ctl_reg(uint8_t data); |
vtoffoli | 2:14dc1ec57f3b | 131 | void set_filter_ctl_reg(ADXL355_filter_ctl_t hpf, ADXL355_filter_ctl_t odr); |
vtoffoli | 2:14dc1ec57f3b | 132 | void set_clk(ADXL355_sync_ctl_t data); |
vtoffoli | 2:14dc1ec57f3b | 133 | void set_device(ADXL355_range_ctl_t range); |
vtoffoli | 2:14dc1ec57f3b | 134 | uint8_t read_status(); |
vtoffoli | 2:14dc1ec57f3b | 135 | // ADXL X/Y/Z/T scanning methods |
vtoffoli | 2:14dc1ec57f3b | 136 | uint32_t scanx(); |
vtoffoli | 2:14dc1ec57f3b | 137 | uint32_t scany(); |
vtoffoli | 2:14dc1ec57f3b | 138 | uint32_t scanz(); |
vtoffoli | 2:14dc1ec57f3b | 139 | uint16_t scant(); |
vtoffoli | 2:14dc1ec57f3b | 140 | // ADXL activity methods |
vtoffoli | 2:14dc1ec57f3b | 141 | void set_activity_axis(ADXL355_act_ctl_t axis); |
vtoffoli | 2:14dc1ec57f3b | 142 | void set_activity_cnt(uint8_t count); |
vtoffoli | 3:ee052fdb4331 | 143 | void set_activity_threshold(uint8_t data_h, uint8_t data_l); |
vtoffoli | 2:14dc1ec57f3b | 144 | void set_inactivity(); |
vtoffoli | 2:14dc1ec57f3b | 145 | // ADXL interrupt methods |
vtoffoli | 2:14dc1ec57f3b | 146 | void set_interrupt1_pin(PinName in, ADXL355_intmap_ctl_t mode); |
vtoffoli | 2:14dc1ec57f3b | 147 | void set_interrupt2_pin(PinName in, ADXL355_intmap_ctl_t mode); |
vtoffoli | 2:14dc1ec57f3b | 148 | void enable_interrupt1(); |
vtoffoli | 2:14dc1ec57f3b | 149 | void enable_interrupt2(); |
vtoffoli | 2:14dc1ec57f3b | 150 | void disable_interrupt1(); |
vtoffoli | 2:14dc1ec57f3b | 151 | void disable_interrupt2(); |
vtoffoli | 2:14dc1ec57f3b | 152 | void set_polling_interrupt1_pin(uint8_t data); |
vtoffoli | 2:14dc1ec57f3b | 153 | void set_polling_interrupt2_pin(uint8_t data); |
vtoffoli | 2:14dc1ec57f3b | 154 | bool get_int1(); |
vtoffoli | 2:14dc1ec57f3b | 155 | bool get_int2(); |
vtoffoli | 2:14dc1ec57f3b | 156 | // ADXL FIFO methods |
vtoffoli | 3:ee052fdb4331 | 157 | uint8_t fifo_read_nr_of_entries(); |
vtoffoli | 2:14dc1ec57f3b | 158 | void fifo_setup(uint8_t nr_of_entries); |
vtoffoli | 2:14dc1ec57f3b | 159 | uint32_t fifo_read_u32(); |
vtoffoli | 2:14dc1ec57f3b | 160 | uint64_t fifo_scan(); |
vtoffoli | 3:ee052fdb4331 | 161 | // ADXL tilt methods and calibration |
vtoffoli | 2:14dc1ec57f3b | 162 | // TBD |
vtoffoli | 2:14dc1ec57f3b | 163 | private: |
vtoffoli | 2:14dc1ec57f3b | 164 | // SPI adxl355; ///< SPI instance of the ADXL |
vtoffoli | 2:14dc1ec57f3b | 165 | SPI adxl355; DigitalOut cs; |
vtoffoli | 2:14dc1ec57f3b | 166 | const static uint8_t _DEVICE_AD = 0xAD; // contect of DEVID_AD (only-read) register |
vtoffoli | 2:14dc1ec57f3b | 167 | const static uint8_t _RESET = 0x52; // reset code |
vtoffoli | 2:14dc1ec57f3b | 168 | const static uint8_t _DUMMY_BYTE = 0xAA; // 10101010 |
vtoffoli | 2:14dc1ec57f3b | 169 | const static uint8_t _WRITE_REG_CMD = 0x00; // write register |
vtoffoli | 2:14dc1ec57f3b | 170 | const static uint8_t _READ_REG_CMD = 0x01; // read register |
vtoffoli | 2:14dc1ec57f3b | 171 | const static uint8_t _READ_FIFO_CMD = 0x23; // read FIFO |
vtoffoli | 2:14dc1ec57f3b | 172 | const static uint8_t _SPI_MODE = 0; // timing scheme |
vtoffoli | 2:14dc1ec57f3b | 173 | }; |
vtoffoli | 2:14dc1ec57f3b | 174 | |
vtoffoli | 2:14dc1ec57f3b | 175 | #endif |