base program for tilt measurement

Dependencies:   COG4050_ADT7420 ADXL362

Fork of COG4050_adxl355_adxl357-ver2 by ADI_CAC

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