base program for tilt measurement

Dependencies:   COG4050_ADT7420 ADXL362

Fork of COG4050_adxl355_adxl357-ver2 by ADI_CAC

Committer:
vtoffoli
Date:
Tue Aug 14 11:33:30 2018 +0000
Revision:
7:5aaa09c40283
Parent:
6:45d2393ef468
Child:
8:9e6ead2ee8d7
update 14.08.2018;

Who changed what in which revision?

UserRevisionLine numberNew 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 int1; ///< DigitalOut instance for the chipselect of the ADXL
vtoffoli 2:14dc1ec57f3b 7 //DigitalOut int2; ///< DigitalOut instance for the chipselect of the ADXL
vtoffoli 3:ee052fdb4331 8
vtoffoli 7:5aaa09c40283 9 /** ----------------------------------- */
vtoffoli 7:5aaa09c40283 10 /** SPI (MAX 10MHZ) and reset */
vtoffoli 7:5aaa09c40283 11 /** ----------------------------------- */
vtoffoli 3:ee052fdb4331 12 ADXL355::ADXL355(PinName cs_pin, PinName MOSI, PinName MISO, PinName SCK): adxl355(MOSI, MISO, SCK), cs(cs_pin)
vtoffoli 3:ee052fdb4331 13 {
vtoffoli 2:14dc1ec57f3b 14 cs = 1;
vtoffoli 2:14dc1ec57f3b 15 adxl355.format(8,_SPI_MODE);
vtoffoli 2:14dc1ec57f3b 16 adxl355.lock();
vtoffoli 6:45d2393ef468 17 axis355_sens = 3.9e-6;
vtoffoli 6:45d2393ef468 18 axis357_sens = 19.5e-6;
vtoffoli 7:5aaa09c40283 19 calib_data.Sxx = 3.9e-6;
vtoffoli 7:5aaa09c40283 20 calib_data.Syy = 3.9e-6;
vtoffoli 7:5aaa09c40283 21 calib_data.Szz = 3.9e-6;
vtoffoli 2:14dc1ec57f3b 22 }
vtoffoli 3:ee052fdb4331 23 void ADXL355::frequency(int hz)
vtoffoli 3:ee052fdb4331 24 {
vtoffoli 2:14dc1ec57f3b 25 adxl355.frequency(hz);
vtoffoli 2:14dc1ec57f3b 26 }
vtoffoli 3:ee052fdb4331 27 void ADXL355::reset(void)
vtoffoli 3:ee052fdb4331 28 {
vtoffoli 2:14dc1ec57f3b 29 adxl355.format(8, _SPI_MODE);
vtoffoli 2:14dc1ec57f3b 30 cs = false;
vtoffoli 2:14dc1ec57f3b 31 // Writing Code 0x52 (representing the letter, R, in ASCII or unicode) to this register immediately resets the ADXL362.
vtoffoli 2:14dc1ec57f3b 32 write_reg(RESET, _RESET);
vtoffoli 2:14dc1ec57f3b 33 cs = true;
vtoffoli 6:45d2393ef468 34 axis355_sens = 3.9e-6;
vtoffoli 6:45d2393ef468 35 axis357_sens = 19.5e-6;
vtoffoli 2:14dc1ec57f3b 36 }
vtoffoli 3:ee052fdb4331 37 /** ----------------------------------- */
vtoffoli 3:ee052fdb4331 38 /** Writes the reg register with data */
vtoffoli 3:ee052fdb4331 39 /** ----------------------------------- */
vtoffoli 3:ee052fdb4331 40 void ADXL355::write_reg(ADXL355_register_t reg, uint8_t data)
vtoffoli 3:ee052fdb4331 41 {
vtoffoli 2:14dc1ec57f3b 42 adxl355.format(8, _SPI_MODE);
vtoffoli 2:14dc1ec57f3b 43 cs = false;
vtoffoli 3:ee052fdb4331 44 adxl355.write(static_cast<uint8_t>(reg<<1) | _WRITE_REG_CMD);
vtoffoli 2:14dc1ec57f3b 45 adxl355.write(data);
vtoffoli 2:14dc1ec57f3b 46 cs = true;
vtoffoli 2:14dc1ec57f3b 47 }
vtoffoli 3:ee052fdb4331 48 void ADXL355::write_reg_u16(ADXL355_register_t reg, uint16_t data)
vtoffoli 3:ee052fdb4331 49 {
vtoffoli 3:ee052fdb4331 50 adxl355.format(8, _SPI_MODE);
vtoffoli 3:ee052fdb4331 51 cs = false;
vtoffoli 3:ee052fdb4331 52 adxl355.write(static_cast<uint8_t>(reg<<1) | _WRITE_REG_CMD);
vtoffoli 3:ee052fdb4331 53 adxl355.write(static_cast<uint8_t>(data & 0xff));
vtoffoli 3:ee052fdb4331 54 adxl355.write(static_cast<uint8_t>((data & 0xff00) >> 8));
vtoffoli 3:ee052fdb4331 55 cs = true;
vtoffoli 3:ee052fdb4331 56 }
vtoffoli 3:ee052fdb4331 57 /** ----------------------------------- */
vtoffoli 2:14dc1ec57f3b 58 /** Reads the reg register */
vtoffoli 3:ee052fdb4331 59 /** ----------------------------------- */
vtoffoli 3:ee052fdb4331 60 uint8_t ADXL355::read_reg(ADXL355_register_t reg)
vtoffoli 3:ee052fdb4331 61 {
vtoffoli 2:14dc1ec57f3b 62 uint8_t ret_val;
vtoffoli 2:14dc1ec57f3b 63 adxl355.format(8, _SPI_MODE);
vtoffoli 2:14dc1ec57f3b 64 cs = false;
vtoffoli 3:ee052fdb4331 65 adxl355.write(static_cast<uint8_t>(reg<<1) | _READ_REG_CMD);
vtoffoli 2:14dc1ec57f3b 66 ret_val = adxl355.write(_DUMMY_BYTE);
vtoffoli 2:14dc1ec57f3b 67 cs = true;
vtoffoli 2:14dc1ec57f3b 68 return ret_val;
vtoffoli 2:14dc1ec57f3b 69 }
vtoffoli 3:ee052fdb4331 70 uint16_t ADXL355::read_reg_u16(ADXL355_register_t reg){
vtoffoli 3:ee052fdb4331 71 uint16_t ret_val = 0;
vtoffoli 3:ee052fdb4331 72 adxl355.format(8, _SPI_MODE);
vtoffoli 3:ee052fdb4331 73 cs = false;
vtoffoli 3:ee052fdb4331 74 adxl355.write(static_cast<uint8_t>(reg<<1) | _READ_REG_CMD);
vtoffoli 3:ee052fdb4331 75 ret_val = adxl355.write(_DUMMY_BYTE);
vtoffoli 3:ee052fdb4331 76 ret_val = (ret_val<<8) | adxl355.write(_DUMMY_BYTE);
vtoffoli 3:ee052fdb4331 77 cs = true;
vtoffoli 3:ee052fdb4331 78 return ret_val;
vtoffoli 3:ee052fdb4331 79 }
vtoffoli 6:45d2393ef468 80 uint32_t ADXL355::read_reg_u20(ADXL355_register_t reg){
vtoffoli 3:ee052fdb4331 81 uint32_t ret_val = 0;
vtoffoli 3:ee052fdb4331 82 adxl355.format(8, _SPI_MODE);
vtoffoli 3:ee052fdb4331 83 cs = false;
vtoffoli 4:23b53636b576 84 adxl355.write((reg<<1) | _READ_REG_CMD);
vtoffoli 6:45d2393ef468 85 ret_val = 0x0f & adxl355.write(_DUMMY_BYTE);
vtoffoli 3:ee052fdb4331 86 ret_val = (ret_val<<8) | adxl355.write(_DUMMY_BYTE);
vtoffoli 3:ee052fdb4331 87 ret_val = (ret_val<<8) | adxl355.write(_DUMMY_BYTE);
vtoffoli 3:ee052fdb4331 88 cs = true;
vtoffoli 3:ee052fdb4331 89 return ret_val;
vtoffoli 3:ee052fdb4331 90 }
vtoffoli 3:ee052fdb4331 91 /** ----------------------------------- */
vtoffoli 3:ee052fdb4331 92 /** Sets the CTL registers */
vtoffoli 3:ee052fdb4331 93 /** ----------------------------------- */
vtoffoli 3:ee052fdb4331 94 void ADXL355::set_power_ctl_reg(uint8_t data){
vtoffoli 3:ee052fdb4331 95 write_reg(POWER_CTL, data);
vtoffoli 3:ee052fdb4331 96 }
vtoffoli 3:ee052fdb4331 97 void ADXL355::set_filter_ctl_reg(ADXL355_filter_ctl_t hpf, ADXL355_filter_ctl_t odr){
vtoffoli 3:ee052fdb4331 98 write_reg(FILTER, static_cast<uint8_t>(hpf|odr));
vtoffoli 3:ee052fdb4331 99 }
vtoffoli 3:ee052fdb4331 100 void ADXL355::set_clk(ADXL355_sync_ctl_t data) {
vtoffoli 3:ee052fdb4331 101 write_reg(SYNC, static_cast<uint8_t>(data));
vtoffoli 3:ee052fdb4331 102 }
vtoffoli 3:ee052fdb4331 103 void ADXL355::set_device(ADXL355_range_ctl_t range) {
vtoffoli 3:ee052fdb4331 104 write_reg(RANGE, static_cast<uint8_t>(range));
vtoffoli 6:45d2393ef468 105 switch(range){
vtoffoli 6:45d2393ef468 106 case 0x01:
vtoffoli 6:45d2393ef468 107 axis355_sens = 3.9e-6;
vtoffoli 6:45d2393ef468 108 axis357_sens = 19.5e-6;
vtoffoli 6:45d2393ef468 109 break;
vtoffoli 6:45d2393ef468 110 case 0x02:
vtoffoli 6:45d2393ef468 111 axis355_sens = 7.8e-6;
vtoffoli 6:45d2393ef468 112 axis357_sens = 39e-6;
vtoffoli 6:45d2393ef468 113 break;
vtoffoli 6:45d2393ef468 114 case 0x03:
vtoffoli 6:45d2393ef468 115 axis355_sens = 15.6e-6;
vtoffoli 6:45d2393ef468 116 axis357_sens = 78e-6;
vtoffoli 6:45d2393ef468 117 break;
vtoffoli 6:45d2393ef468 118 }
vtoffoli 3:ee052fdb4331 119 }
vtoffoli 3:ee052fdb4331 120 /** ----------------------------------- */
vtoffoli 3:ee052fdb4331 121 /** Read the STATUS registers */
vtoffoli 3:ee052fdb4331 122 /** ----------------------------------- */
vtoffoli 3:ee052fdb4331 123 uint8_t ADXL355::read_status(){
vtoffoli 3:ee052fdb4331 124 return read_reg(STATUS);
vtoffoli 3:ee052fdb4331 125 }
vtoffoli 3:ee052fdb4331 126 /** ----------------------------------- */
vtoffoli 3:ee052fdb4331 127 /** ADXL must be set in measurement */
vtoffoli 3:ee052fdb4331 128 /** mode to read the data registers */
vtoffoli 3:ee052fdb4331 129 /** ----------------------------------- */
vtoffoli 3:ee052fdb4331 130 uint32_t ADXL355::scanx(){
vtoffoli 6:45d2393ef468 131 return read_reg_u20(XDATA3);
vtoffoli 3:ee052fdb4331 132 }
vtoffoli 3:ee052fdb4331 133 uint32_t ADXL355::scany(){
vtoffoli 6:45d2393ef468 134 return read_reg_u20(YDATA3);
vtoffoli 3:ee052fdb4331 135 }
vtoffoli 3:ee052fdb4331 136 uint32_t ADXL355::scanz(){
vtoffoli 6:45d2393ef468 137 return read_reg_u20(ZDATA3);
vtoffoli 3:ee052fdb4331 138 }
vtoffoli 3:ee052fdb4331 139 uint16_t ADXL355::scant(){
vtoffoli 3:ee052fdb4331 140 return read_reg_u16(TEMP2);
vtoffoli 3:ee052fdb4331 141 }
vtoffoli 3:ee052fdb4331 142 /** ----------------------------------- */
vtoffoli 6:45d2393ef468 143 /** Activity SetUp - the measured */
vtoffoli 6:45d2393ef468 144 /** acceleration on any axis is above */
vtoffoli 6:45d2393ef468 145 /** the ACT_THRESH bits for ACT_COUNT */
vtoffoli 6:45d2393ef468 146 /** consecutive measurements. */
vtoffoli 3:ee052fdb4331 147 /** ----------------------------------- */
vtoffoli 6:45d2393ef468 148
vtoffoli 6:45d2393ef468 149 void ADXL355::set_activity_axis(ADXL355_act_ctl_t axis) {
vtoffoli 6:45d2393ef468 150 write_reg(ACT_EN, axis);
vtoffoli 6:45d2393ef468 151 }
vtoffoli 6:45d2393ef468 152 void ADXL355::set_activity_cnt(uint8_t count) {
vtoffoli 6:45d2393ef468 153 write_reg(ACT_COUNT, count);
vtoffoli 6:45d2393ef468 154 }
vtoffoli 3:ee052fdb4331 155 void ADXL355::set_activity_threshold(uint8_t data_h, uint8_t data_l) {
vtoffoli 3:ee052fdb4331 156 uint16_t ret_val = static_cast<uint16_t>((data_h<<8)|data_l);
vtoffoli 3:ee052fdb4331 157 write_reg_u16(ACT_THRESH_H, ret_val);
vtoffoli 3:ee052fdb4331 158 }
vtoffoli 6:45d2393ef468 159 void ADXL355::set_inactivity() {
vtoffoli 6:45d2393ef468 160 write_reg(ACT_EN, 0x00);
vtoffoli 6:45d2393ef468 161 }
vtoffoli 3:ee052fdb4331 162 /** ----------------------------------- */
vtoffoli 3:ee052fdb4331 163 /** ----------------------------------- */
vtoffoli 3:ee052fdb4331 164 void ADXL355::set_interrupt1_pin(PinName in, ADXL355_intmap_ctl_t mode) {}
vtoffoli 3:ee052fdb4331 165 void ADXL355::set_interrupt2_pin(PinName in, ADXL355_intmap_ctl_t mode) {}
vtoffoli 3:ee052fdb4331 166 void ADXL355::enable_interrupt1() {}
vtoffoli 3:ee052fdb4331 167 void ADXL355::enable_interrupt2() {}
vtoffoli 3:ee052fdb4331 168 void ADXL355::disable_interrupt1() {}
vtoffoli 3:ee052fdb4331 169 void ADXL355::disable_interrupt2() {}
vtoffoli 3:ee052fdb4331 170 void ADXL355::set_polling_interrupt1_pin(uint8_t data) {}
vtoffoli 3:ee052fdb4331 171 void ADXL355::set_polling_interrupt2_pin(uint8_t data) {}
vtoffoli 3:ee052fdb4331 172 bool get_int1() {}
vtoffoli 3:ee052fdb4331 173 bool get_int2() {}
vtoffoli 3:ee052fdb4331 174 /** ----------------------------------- */
vtoffoli 3:ee052fdb4331 175 /** FIFO set up and read operation */
vtoffoli 3:ee052fdb4331 176 /** ----------------------------------- */
vtoffoli 3:ee052fdb4331 177 uint8_t ADXL355::fifo_read_nr_of_entries(){
vtoffoli 3:ee052fdb4331 178 return read_reg(FIFO_ENTRIES);
vtoffoli 3:ee052fdb4331 179 }
vtoffoli 3:ee052fdb4331 180 void ADXL355::fifo_setup(uint8_t nr_of_entries){
vtoffoli 3:ee052fdb4331 181 if (nr_of_entries > 0x60) {
vtoffoli 3:ee052fdb4331 182 nr_of_entries = nr_of_entries;
vtoffoli 3:ee052fdb4331 183 }
vtoffoli 3:ee052fdb4331 184 write_reg(FIFO_SAMPLES, nr_of_entries);
vtoffoli 3:ee052fdb4331 185 }
vtoffoli 3:ee052fdb4331 186 uint32_t ADXL355::fifo_read_u32() {
vtoffoli 3:ee052fdb4331 187 uint32_t ret_val = 0;
vtoffoli 3:ee052fdb4331 188 adxl355.format(8, _SPI_MODE);
vtoffoli 3:ee052fdb4331 189 cs = false;
vtoffoli 3:ee052fdb4331 190 adxl355.write(_READ_FIFO_CMD);
vtoffoli 3:ee052fdb4331 191 ret_val = adxl355.write(_DUMMY_BYTE);
vtoffoli 3:ee052fdb4331 192 ret_val = (ret_val<<8) | static_cast<uint8_t>(adxl355.write(_DUMMY_BYTE));
vtoffoli 3:ee052fdb4331 193 ret_val = (ret_val<<4) | static_cast<uint8_t>(adxl355.write(_DUMMY_BYTE)>>4);
vtoffoli 3:ee052fdb4331 194 cs = true;
vtoffoli 3:ee052fdb4331 195 return ret_val;
vtoffoli 3:ee052fdb4331 196 }
vtoffoli 3:ee052fdb4331 197 uint64_t ADXL355::fifo_scan() {
vtoffoli 3:ee052fdb4331 198 uint64_t ret_val = 0;
vtoffoli 3:ee052fdb4331 199 uint32_t x = 0, y = 0, z = 0, dummy;
vtoffoli 3:ee052fdb4331 200 adxl355.format(8, _SPI_MODE);
vtoffoli 3:ee052fdb4331 201 cs = false;
vtoffoli 3:ee052fdb4331 202 adxl355.write(_READ_FIFO_CMD);
vtoffoli 3:ee052fdb4331 203 for(uint8_t i = 0; i < 3; i++) {
vtoffoli 3:ee052fdb4331 204 dummy = adxl355.write(_DUMMY_BYTE);
vtoffoli 3:ee052fdb4331 205 dummy = (dummy<<8) | static_cast<uint8_t>(adxl355.write(_DUMMY_BYTE));
vtoffoli 3:ee052fdb4331 206 dummy = (dummy<<4) | static_cast<uint8_t>(adxl355.write(_DUMMY_BYTE)>>4);
vtoffoli 3:ee052fdb4331 207 dummy = dummy & 0xffff;
vtoffoli 3:ee052fdb4331 208 switch(i) {
vtoffoli 3:ee052fdb4331 209 case 0: // x
vtoffoli 3:ee052fdb4331 210 x = dummy;
vtoffoli 3:ee052fdb4331 211 break;
vtoffoli 3:ee052fdb4331 212 case 1: // y
vtoffoli 3:ee052fdb4331 213 y = dummy;
vtoffoli 3:ee052fdb4331 214 break;
vtoffoli 3:ee052fdb4331 215 case 2: // z
vtoffoli 3:ee052fdb4331 216 z = dummy;
vtoffoli 3:ee052fdb4331 217 break;
vtoffoli 3:ee052fdb4331 218 }
vtoffoli 3:ee052fdb4331 219 }
vtoffoli 3:ee052fdb4331 220 cs = true;
vtoffoli 3:ee052fdb4331 221 // format (24)xx(24)yy(24)zz
vtoffoli 3:ee052fdb4331 222 ret_val = static_cast<uint64_t> (x) << 48;
vtoffoli 3:ee052fdb4331 223 ret_val |= static_cast<uint64_t>(y) << 24;
vtoffoli 3:ee052fdb4331 224 ret_val |= static_cast<uint64_t>(z) ;
vtoffoli 3:ee052fdb4331 225 return ret_val;
vtoffoli 7:5aaa09c40283 226 }
vtoffoli 7:5aaa09c40283 227
vtoffoli 7:5aaa09c40283 228 /** ----------------------------------- */
vtoffoli 7:5aaa09c40283 229 /** CALIBRATION AND CONVERSION */
vtoffoli 7:5aaa09c40283 230 /** ----------------------------------- */
vtoffoli 7:5aaa09c40283 231 float ADXL355::convert(uint32_t data){
vtoffoli 7:5aaa09c40283 232 // If a positive value, return it
vtoffoli 7:5aaa09c40283 233 if ((data & 0x80000) == 0)
vtoffoli 7:5aaa09c40283 234 {
vtoffoli 7:5aaa09c40283 235 return float(data);
vtoffoli 7:5aaa09c40283 236 }
vtoffoli 7:5aaa09c40283 237 //uint32_t rawValue = data<<(32-nbit);
vtoffoli 7:5aaa09c40283 238 // Otherwise perform the 2's complement math on the value
vtoffoli 7:5aaa09c40283 239 return float((~(data - 0x01)) & 0xfffff) * -1;
vtoffoli 3:ee052fdb4331 240 }