base program for tilt measurement

Dependencies:   COG4050_ADT7420 ADXL362

Fork of COG4050_adxl355_adxl357-ver2 by ADI_CAC

Committer:
vtoffoli
Date:
Tue Aug 21 13:25:37 2018 +0000
Revision:
8:9e6ead2ee8d7
Parent:
7:5aaa09c40283
Child:
10:f5ba762b58b4
COG+ADXL355

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