base program for tilt measurement
Dependencies: COG4050_ADT7420 ADXL362
Fork of COG4050_adxl355_adxl357-ver2 by
ADXL35x/ADXL355.cpp@4:23b53636b576, 2018-08-08 (annotated)
- Committer:
- vtoffoli
- Date:
- Wed Aug 08 11:57:16 2018 +0000
- Revision:
- 4:23b53636b576
- Parent:
- 3:ee052fdb4331
- Child:
- 6:45d2393ef468
refresh;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vtoffoli | 2:14dc1ec57f3b | 1 | #include <stdint.h> |
vtoffoli | 2:14dc1ec57f3b | 2 | #include "mbed.h" |
vtoffoli | 2:14dc1ec57f3b | 3 | #include "ADXL355.h" |
vtoffoli | 2:14dc1ec57f3b | 4 | |
vtoffoli | 2:14dc1ec57f3b | 5 | |
vtoffoli | 2:14dc1ec57f3b | 6 | //DigitalOut(cs); ///< DigitalOut instance for the chipselect of the ADXL |
vtoffoli | 2:14dc1ec57f3b | 7 | //DigitalOut int1; ///< DigitalOut instance for the chipselect of the ADXL |
vtoffoli | 2:14dc1ec57f3b | 8 | //DigitalOut int2; ///< DigitalOut instance for the chipselect of the ADXL |
vtoffoli | 3:ee052fdb4331 | 9 | |
vtoffoli | 3:ee052fdb4331 | 10 | ADXL355::ADXL355(PinName cs_pin, PinName MOSI, PinName MISO, PinName SCK): adxl355(MOSI, MISO, SCK), cs(cs_pin) |
vtoffoli | 3:ee052fdb4331 | 11 | { |
vtoffoli | 2:14dc1ec57f3b | 12 | cs = 1; |
vtoffoli | 2:14dc1ec57f3b | 13 | adxl355.format(8,_SPI_MODE); |
vtoffoli | 2:14dc1ec57f3b | 14 | adxl355.lock(); |
vtoffoli | 2:14dc1ec57f3b | 15 | } |
vtoffoli | 2:14dc1ec57f3b | 16 | |
vtoffoli | 2:14dc1ec57f3b | 17 | /** SPI bus frequency */ |
vtoffoli | 3:ee052fdb4331 | 18 | void ADXL355::frequency(int hz) |
vtoffoli | 3:ee052fdb4331 | 19 | { |
vtoffoli | 2:14dc1ec57f3b | 20 | adxl355.frequency(hz); |
vtoffoli | 2:14dc1ec57f3b | 21 | } |
vtoffoli | 2:14dc1ec57f3b | 22 | |
vtoffoli | 2:14dc1ec57f3b | 23 | /** Software resets */ |
vtoffoli | 3:ee052fdb4331 | 24 | void ADXL355::reset(void) |
vtoffoli | 3:ee052fdb4331 | 25 | { |
vtoffoli | 2:14dc1ec57f3b | 26 | adxl355.format(8, _SPI_MODE); |
vtoffoli | 2:14dc1ec57f3b | 27 | cs = false; |
vtoffoli | 2:14dc1ec57f3b | 28 | // Writing Code 0x52 (representing the letter, R, in ASCII or unicode) to this register immediately resets the ADXL362. |
vtoffoli | 2:14dc1ec57f3b | 29 | write_reg(RESET, _RESET); |
vtoffoli | 2:14dc1ec57f3b | 30 | cs = true; |
vtoffoli | 2:14dc1ec57f3b | 31 | } |
vtoffoli | 3:ee052fdb4331 | 32 | /** ----------------------------------- */ |
vtoffoli | 3:ee052fdb4331 | 33 | /** Writes the reg register with data */ |
vtoffoli | 3:ee052fdb4331 | 34 | /** ----------------------------------- */ |
vtoffoli | 3:ee052fdb4331 | 35 | void ADXL355::write_reg(ADXL355_register_t reg, uint8_t data) |
vtoffoli | 3:ee052fdb4331 | 36 | { |
vtoffoli | 2:14dc1ec57f3b | 37 | adxl355.format(8, _SPI_MODE); |
vtoffoli | 2:14dc1ec57f3b | 38 | cs = false; |
vtoffoli | 3:ee052fdb4331 | 39 | adxl355.write(static_cast<uint8_t>(reg<<1) | _WRITE_REG_CMD); |
vtoffoli | 2:14dc1ec57f3b | 40 | adxl355.write(data); |
vtoffoli | 2:14dc1ec57f3b | 41 | cs = true; |
vtoffoli | 2:14dc1ec57f3b | 42 | } |
vtoffoli | 3:ee052fdb4331 | 43 | void ADXL355::write_reg_u16(ADXL355_register_t reg, uint16_t data) |
vtoffoli | 3:ee052fdb4331 | 44 | { |
vtoffoli | 3:ee052fdb4331 | 45 | adxl355.format(8, _SPI_MODE); |
vtoffoli | 3:ee052fdb4331 | 46 | cs = false; |
vtoffoli | 3:ee052fdb4331 | 47 | adxl355.write(static_cast<uint8_t>(reg<<1) | _WRITE_REG_CMD); |
vtoffoli | 3:ee052fdb4331 | 48 | adxl355.write(static_cast<uint8_t>(data & 0xff)); |
vtoffoli | 3:ee052fdb4331 | 49 | adxl355.write(static_cast<uint8_t>((data & 0xff00) >> 8)); |
vtoffoli | 3:ee052fdb4331 | 50 | cs = true; |
vtoffoli | 3:ee052fdb4331 | 51 | } |
vtoffoli | 3:ee052fdb4331 | 52 | /** ----------------------------------- */ |
vtoffoli | 2:14dc1ec57f3b | 53 | /** Reads the reg register */ |
vtoffoli | 3:ee052fdb4331 | 54 | /** ----------------------------------- */ |
vtoffoli | 3:ee052fdb4331 | 55 | uint8_t ADXL355::read_reg(ADXL355_register_t reg) |
vtoffoli | 3:ee052fdb4331 | 56 | { |
vtoffoli | 2:14dc1ec57f3b | 57 | uint8_t ret_val; |
vtoffoli | 2:14dc1ec57f3b | 58 | adxl355.format(8, _SPI_MODE); |
vtoffoli | 2:14dc1ec57f3b | 59 | cs = false; |
vtoffoli | 3:ee052fdb4331 | 60 | adxl355.write(static_cast<uint8_t>(reg<<1) | _READ_REG_CMD); |
vtoffoli | 2:14dc1ec57f3b | 61 | ret_val = adxl355.write(_DUMMY_BYTE); |
vtoffoli | 2:14dc1ec57f3b | 62 | cs = true; |
vtoffoli | 2:14dc1ec57f3b | 63 | return ret_val; |
vtoffoli | 2:14dc1ec57f3b | 64 | } |
vtoffoli | 3:ee052fdb4331 | 65 | uint16_t ADXL355::read_reg_u16(ADXL355_register_t reg){ |
vtoffoli | 3:ee052fdb4331 | 66 | uint16_t ret_val = 0; |
vtoffoli | 3:ee052fdb4331 | 67 | adxl355.format(8, _SPI_MODE); |
vtoffoli | 3:ee052fdb4331 | 68 | cs = false; |
vtoffoli | 3:ee052fdb4331 | 69 | adxl355.write(static_cast<uint8_t>(reg<<1) | _READ_REG_CMD); |
vtoffoli | 3:ee052fdb4331 | 70 | ret_val = adxl355.write(_DUMMY_BYTE); |
vtoffoli | 3:ee052fdb4331 | 71 | ret_val = (ret_val<<8) | adxl355.write(_DUMMY_BYTE); |
vtoffoli | 3:ee052fdb4331 | 72 | cs = true; |
vtoffoli | 3:ee052fdb4331 | 73 | return ret_val; |
vtoffoli | 3:ee052fdb4331 | 74 | } |
vtoffoli | 3:ee052fdb4331 | 75 | uint32_t ADXL355::read_reg_u32(ADXL355_register_t reg){ |
vtoffoli | 3:ee052fdb4331 | 76 | uint32_t ret_val = 0; |
vtoffoli | 3:ee052fdb4331 | 77 | adxl355.format(8, _SPI_MODE); |
vtoffoli | 3:ee052fdb4331 | 78 | cs = false; |
vtoffoli | 4:23b53636b576 | 79 | adxl355.write((reg<<1) | _READ_REG_CMD); |
vtoffoli | 3:ee052fdb4331 | 80 | ret_val = adxl355.write(_DUMMY_BYTE); |
vtoffoli | 3:ee052fdb4331 | 81 | ret_val = (ret_val<<8) | adxl355.write(_DUMMY_BYTE); |
vtoffoli | 3:ee052fdb4331 | 82 | ret_val = (ret_val<<8) | adxl355.write(_DUMMY_BYTE); |
vtoffoli | 3:ee052fdb4331 | 83 | cs = true; |
vtoffoli | 3:ee052fdb4331 | 84 | return ret_val; |
vtoffoli | 3:ee052fdb4331 | 85 | } |
vtoffoli | 3:ee052fdb4331 | 86 | /** ----------------------------------- */ |
vtoffoli | 3:ee052fdb4331 | 87 | /** Sets the CTL registers */ |
vtoffoli | 3:ee052fdb4331 | 88 | /** ----------------------------------- */ |
vtoffoli | 3:ee052fdb4331 | 89 | void ADXL355::set_power_ctl_reg(uint8_t data){ |
vtoffoli | 3:ee052fdb4331 | 90 | write_reg(POWER_CTL, data); |
vtoffoli | 3:ee052fdb4331 | 91 | } |
vtoffoli | 3:ee052fdb4331 | 92 | void ADXL355::set_filter_ctl_reg(ADXL355_filter_ctl_t hpf, ADXL355_filter_ctl_t odr){ |
vtoffoli | 3:ee052fdb4331 | 93 | write_reg(FILTER, static_cast<uint8_t>(hpf|odr)); |
vtoffoli | 3:ee052fdb4331 | 94 | } |
vtoffoli | 3:ee052fdb4331 | 95 | void ADXL355::set_clk(ADXL355_sync_ctl_t data) { |
vtoffoli | 3:ee052fdb4331 | 96 | write_reg(SYNC, static_cast<uint8_t>(data)); |
vtoffoli | 3:ee052fdb4331 | 97 | } |
vtoffoli | 3:ee052fdb4331 | 98 | void ADXL355::set_device(ADXL355_range_ctl_t range) { |
vtoffoli | 3:ee052fdb4331 | 99 | write_reg(RANGE, static_cast<uint8_t>(range)); |
vtoffoli | 3:ee052fdb4331 | 100 | } |
vtoffoli | 3:ee052fdb4331 | 101 | /** ----------------------------------- */ |
vtoffoli | 3:ee052fdb4331 | 102 | /** Read the STATUS registers */ |
vtoffoli | 3:ee052fdb4331 | 103 | /** ----------------------------------- */ |
vtoffoli | 3:ee052fdb4331 | 104 | uint8_t ADXL355::read_status(){ |
vtoffoli | 3:ee052fdb4331 | 105 | return read_reg(STATUS); |
vtoffoli | 3:ee052fdb4331 | 106 | } |
vtoffoli | 3:ee052fdb4331 | 107 | /** ----------------------------------- */ |
vtoffoli | 3:ee052fdb4331 | 108 | /** ADXL must be set in measurement */ |
vtoffoli | 3:ee052fdb4331 | 109 | /** mode to read the data registers */ |
vtoffoli | 3:ee052fdb4331 | 110 | /** ----------------------------------- */ |
vtoffoli | 3:ee052fdb4331 | 111 | uint32_t ADXL355::scanx(){ |
vtoffoli | 3:ee052fdb4331 | 112 | return read_reg_u32(XDATA3); |
vtoffoli | 3:ee052fdb4331 | 113 | } |
vtoffoli | 3:ee052fdb4331 | 114 | uint32_t ADXL355::scany(){ |
vtoffoli | 3:ee052fdb4331 | 115 | return read_reg_u32(YDATA3); |
vtoffoli | 3:ee052fdb4331 | 116 | } |
vtoffoli | 3:ee052fdb4331 | 117 | uint32_t ADXL355::scanz(){ |
vtoffoli | 3:ee052fdb4331 | 118 | return read_reg_u32(ZDATA3); |
vtoffoli | 3:ee052fdb4331 | 119 | } |
vtoffoli | 3:ee052fdb4331 | 120 | uint16_t ADXL355::scant(){ |
vtoffoli | 3:ee052fdb4331 | 121 | return read_reg_u16(TEMP2); |
vtoffoli | 3:ee052fdb4331 | 122 | } |
vtoffoli | 3:ee052fdb4331 | 123 | /** ----------------------------------- */ |
vtoffoli | 3:ee052fdb4331 | 124 | /** ----------------------------------- */ |
vtoffoli | 3:ee052fdb4331 | 125 | void ADXL355::set_activity_axis(ADXL355_act_ctl_t axis) {} |
vtoffoli | 3:ee052fdb4331 | 126 | void ADXL355::set_activity_cnt(uint8_t count) {} |
vtoffoli | 3:ee052fdb4331 | 127 | void ADXL355::set_activity_threshold(uint8_t data_h, uint8_t data_l) { |
vtoffoli | 3:ee052fdb4331 | 128 | uint16_t ret_val = static_cast<uint16_t>((data_h<<8)|data_l); |
vtoffoli | 3:ee052fdb4331 | 129 | write_reg_u16(ACT_THRESH_H, ret_val); |
vtoffoli | 3:ee052fdb4331 | 130 | } |
vtoffoli | 3:ee052fdb4331 | 131 | void ADXL355::set_inactivity() {} |
vtoffoli | 3:ee052fdb4331 | 132 | /** ----------------------------------- */ |
vtoffoli | 3:ee052fdb4331 | 133 | /** ----------------------------------- */ |
vtoffoli | 3:ee052fdb4331 | 134 | void ADXL355::set_interrupt1_pin(PinName in, ADXL355_intmap_ctl_t mode) {} |
vtoffoli | 3:ee052fdb4331 | 135 | void ADXL355::set_interrupt2_pin(PinName in, ADXL355_intmap_ctl_t mode) {} |
vtoffoli | 3:ee052fdb4331 | 136 | void ADXL355::enable_interrupt1() {} |
vtoffoli | 3:ee052fdb4331 | 137 | void ADXL355::enable_interrupt2() {} |
vtoffoli | 3:ee052fdb4331 | 138 | void ADXL355::disable_interrupt1() {} |
vtoffoli | 3:ee052fdb4331 | 139 | void ADXL355::disable_interrupt2() {} |
vtoffoli | 3:ee052fdb4331 | 140 | void ADXL355::set_polling_interrupt1_pin(uint8_t data) {} |
vtoffoli | 3:ee052fdb4331 | 141 | void ADXL355::set_polling_interrupt2_pin(uint8_t data) {} |
vtoffoli | 3:ee052fdb4331 | 142 | bool get_int1() {} |
vtoffoli | 3:ee052fdb4331 | 143 | bool get_int2() {} |
vtoffoli | 3:ee052fdb4331 | 144 | /** ----------------------------------- */ |
vtoffoli | 3:ee052fdb4331 | 145 | /** FIFO set up and read operation */ |
vtoffoli | 3:ee052fdb4331 | 146 | /** ----------------------------------- */ |
vtoffoli | 3:ee052fdb4331 | 147 | uint8_t ADXL355::fifo_read_nr_of_entries(){ |
vtoffoli | 3:ee052fdb4331 | 148 | return read_reg(FIFO_ENTRIES); |
vtoffoli | 3:ee052fdb4331 | 149 | } |
vtoffoli | 3:ee052fdb4331 | 150 | void ADXL355::fifo_setup(uint8_t nr_of_entries){ |
vtoffoli | 3:ee052fdb4331 | 151 | if (nr_of_entries > 0x60) { |
vtoffoli | 3:ee052fdb4331 | 152 | nr_of_entries = nr_of_entries; |
vtoffoli | 3:ee052fdb4331 | 153 | } |
vtoffoli | 3:ee052fdb4331 | 154 | write_reg(FIFO_SAMPLES, nr_of_entries); |
vtoffoli | 3:ee052fdb4331 | 155 | } |
vtoffoli | 3:ee052fdb4331 | 156 | uint32_t ADXL355::fifo_read_u32() { |
vtoffoli | 3:ee052fdb4331 | 157 | uint32_t ret_val = 0; |
vtoffoli | 3:ee052fdb4331 | 158 | adxl355.format(8, _SPI_MODE); |
vtoffoli | 3:ee052fdb4331 | 159 | cs = false; |
vtoffoli | 3:ee052fdb4331 | 160 | adxl355.write(_READ_FIFO_CMD); |
vtoffoli | 3:ee052fdb4331 | 161 | ret_val = adxl355.write(_DUMMY_BYTE); |
vtoffoli | 3:ee052fdb4331 | 162 | ret_val = (ret_val<<8) | static_cast<uint8_t>(adxl355.write(_DUMMY_BYTE)); |
vtoffoli | 3:ee052fdb4331 | 163 | ret_val = (ret_val<<4) | static_cast<uint8_t>(adxl355.write(_DUMMY_BYTE)>>4); |
vtoffoli | 3:ee052fdb4331 | 164 | cs = true; |
vtoffoli | 3:ee052fdb4331 | 165 | return ret_val; |
vtoffoli | 3:ee052fdb4331 | 166 | } |
vtoffoli | 3:ee052fdb4331 | 167 | uint64_t ADXL355::fifo_scan() { |
vtoffoli | 3:ee052fdb4331 | 168 | uint64_t ret_val = 0; |
vtoffoli | 3:ee052fdb4331 | 169 | uint32_t x = 0, y = 0, z = 0, dummy; |
vtoffoli | 3:ee052fdb4331 | 170 | adxl355.format(8, _SPI_MODE); |
vtoffoli | 3:ee052fdb4331 | 171 | cs = false; |
vtoffoli | 3:ee052fdb4331 | 172 | adxl355.write(_READ_FIFO_CMD); |
vtoffoli | 3:ee052fdb4331 | 173 | for(uint8_t i = 0; i < 3; i++) { |
vtoffoli | 3:ee052fdb4331 | 174 | dummy = adxl355.write(_DUMMY_BYTE); |
vtoffoli | 3:ee052fdb4331 | 175 | dummy = (dummy<<8) | static_cast<uint8_t>(adxl355.write(_DUMMY_BYTE)); |
vtoffoli | 3:ee052fdb4331 | 176 | dummy = (dummy<<4) | static_cast<uint8_t>(adxl355.write(_DUMMY_BYTE)>>4); |
vtoffoli | 3:ee052fdb4331 | 177 | dummy = dummy & 0xffff; |
vtoffoli | 3:ee052fdb4331 | 178 | switch(i) { |
vtoffoli | 3:ee052fdb4331 | 179 | case 0: // x |
vtoffoli | 3:ee052fdb4331 | 180 | x = dummy; |
vtoffoli | 3:ee052fdb4331 | 181 | break; |
vtoffoli | 3:ee052fdb4331 | 182 | case 1: // y |
vtoffoli | 3:ee052fdb4331 | 183 | y = dummy; |
vtoffoli | 3:ee052fdb4331 | 184 | break; |
vtoffoli | 3:ee052fdb4331 | 185 | case 2: // z |
vtoffoli | 3:ee052fdb4331 | 186 | z = dummy; |
vtoffoli | 3:ee052fdb4331 | 187 | break; |
vtoffoli | 3:ee052fdb4331 | 188 | } |
vtoffoli | 3:ee052fdb4331 | 189 | } |
vtoffoli | 3:ee052fdb4331 | 190 | cs = true; |
vtoffoli | 3:ee052fdb4331 | 191 | // format (24)xx(24)yy(24)zz |
vtoffoli | 3:ee052fdb4331 | 192 | ret_val = static_cast<uint64_t> (x) << 48; |
vtoffoli | 3:ee052fdb4331 | 193 | ret_val |= static_cast<uint64_t>(y) << 24; |
vtoffoli | 3:ee052fdb4331 | 194 | ret_val |= static_cast<uint64_t>(z) ; |
vtoffoli | 3:ee052fdb4331 | 195 | return ret_val; |
vtoffoli | 3:ee052fdb4331 | 196 | } |