Update
ADXL362.cpp@0:860fafcf34d6, 2019-01-11 (annotated)
- Committer:
- APS_Lab
- Date:
- Fri Jan 11 07:05:02 2019 +0000
- Revision:
- 0:860fafcf34d6
First;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
APS_Lab | 0:860fafcf34d6 | 1 | #include "ADXL362.h" |
APS_Lab | 0:860fafcf34d6 | 2 | |
APS_Lab | 0:860fafcf34d6 | 3 | #define WAIT_US(value) (0.000001 * ((float)value)) |
APS_Lab | 0:860fafcf34d6 | 4 | |
APS_Lab | 0:860fafcf34d6 | 5 | #define REGADDR_WRITE (0x80) |
APS_Lab | 0:860fafcf34d6 | 6 | #define REGADDR_WR_L (0x00) |
APS_Lab | 0:860fafcf34d6 | 7 | #define REGADDR_WR_H (0x01) |
APS_Lab | 0:860fafcf34d6 | 8 | |
APS_Lab | 0:860fafcf34d6 | 9 | #define GET_VAL_L(value) ((value >> 0) & 0xFF) |
APS_Lab | 0:860fafcf34d6 | 10 | #define GET_VAL_H(value) ((value >> 8) & 0xFF) |
APS_Lab | 0:860fafcf34d6 | 11 | |
APS_Lab | 0:860fafcf34d6 | 12 | // define for EV-COG-AD3029LZ |
APS_Lab | 0:860fafcf34d6 | 13 | #define SPI_CS SPI1_CS3 |
APS_Lab | 0:860fafcf34d6 | 14 | |
APS_Lab | 0:860fafcf34d6 | 15 | /** ========================================== |
APS_Lab | 0:860fafcf34d6 | 16 | * Public ( initializing ) |
APS_Lab | 0:860fafcf34d6 | 17 | * ========================================== */ |
APS_Lab | 0:860fafcf34d6 | 18 | ADXL362::ADXL362(Serial *setUart, SPI* spi1) { |
APS_Lab | 0:860fafcf34d6 | 19 | int check; |
APS_Lab | 0:860fafcf34d6 | 20 | |
APS_Lab | 0:860fafcf34d6 | 21 | uart = setUart; |
APS_Lab | 0:860fafcf34d6 | 22 | _spi = spi1; |
APS_Lab | 0:860fafcf34d6 | 23 | _cs = new DigitalOut(SPI_CS); |
APS_Lab | 0:860fafcf34d6 | 24 | |
APS_Lab | 0:860fafcf34d6 | 25 | _spi->format(8,3); |
APS_Lab | 0:860fafcf34d6 | 26 | _spi->frequency(1000000); |
APS_Lab | 0:860fafcf34d6 | 27 | |
APS_Lab | 0:860fafcf34d6 | 28 | chipSelOff(); |
APS_Lab | 0:860fafcf34d6 | 29 | |
APS_Lab | 0:860fafcf34d6 | 30 | /* start */ |
APS_Lab | 0:860fafcf34d6 | 31 | check = regRD(DEVID_AD); |
APS_Lab | 0:860fafcf34d6 | 32 | if (check != DEVID_AD_ADXL362) { |
APS_Lab | 0:860fafcf34d6 | 33 | return; |
APS_Lab | 0:860fafcf34d6 | 34 | } |
APS_Lab | 0:860fafcf34d6 | 35 | check = regRD(DEVID_MST); |
APS_Lab | 0:860fafcf34d6 | 36 | if (check != DEVID_MST_ADXL362) { |
APS_Lab | 0:860fafcf34d6 | 37 | return; |
APS_Lab | 0:860fafcf34d6 | 38 | } |
APS_Lab | 0:860fafcf34d6 | 39 | check = regRD(PARTID); |
APS_Lab | 0:860fafcf34d6 | 40 | if (check != PARTID_ADXL362) { |
APS_Lab | 0:860fafcf34d6 | 41 | return; |
APS_Lab | 0:860fafcf34d6 | 42 | } |
APS_Lab | 0:860fafcf34d6 | 43 | |
APS_Lab | 0:860fafcf34d6 | 44 | /* init MIN/MAX store */ |
APS_Lab | 0:860fafcf34d6 | 45 | initMinMax(&minStore, &maxStore); |
APS_Lab | 0:860fafcf34d6 | 46 | /* set convert parameter */ |
APS_Lab | 0:860fafcf34d6 | 47 | SoftReset(); |
APS_Lab | 0:860fafcf34d6 | 48 | |
APS_Lab | 0:860fafcf34d6 | 49 | scaleAccel = PARAM_ADXL362_SCALE_ACCEL; |
APS_Lab | 0:860fafcf34d6 | 50 | scaleThermal = PARAM_ADXL362_SCALE_THERMAL; |
APS_Lab | 0:860fafcf34d6 | 51 | offsetThermal = PARAM_ADXL362_THERMAL_OFFSET; |
APS_Lab | 0:860fafcf34d6 | 52 | |
APS_Lab | 0:860fafcf34d6 | 53 | //SetMesureParam(POWER_CTL_PARAM_LOWNOISE_ULTRA); |
APS_Lab | 0:860fafcf34d6 | 54 | //StartMesure(); |
APS_Lab | 0:860fafcf34d6 | 55 | } |
APS_Lab | 0:860fafcf34d6 | 56 | |
APS_Lab | 0:860fafcf34d6 | 57 | ADXL362::~ADXL362() { |
APS_Lab | 0:860fafcf34d6 | 58 | delete this->_cs; |
APS_Lab | 0:860fafcf34d6 | 59 | } |
APS_Lab | 0:860fafcf34d6 | 60 | |
APS_Lab | 0:860fafcf34d6 | 61 | |
APS_Lab | 0:860fafcf34d6 | 62 | /** ========================================== |
APS_Lab | 0:860fafcf34d6 | 63 | * Private ( Control Pins ) |
APS_Lab | 0:860fafcf34d6 | 64 | * ========================================== */ |
APS_Lab | 0:860fafcf34d6 | 65 | /* Assert CHIP_SEL = enable */ |
APS_Lab | 0:860fafcf34d6 | 66 | void ADXL362::chipSelOn() { |
APS_Lab | 0:860fafcf34d6 | 67 | chipSelDelay(); |
APS_Lab | 0:860fafcf34d6 | 68 | *_cs = 0; |
APS_Lab | 0:860fafcf34d6 | 69 | } |
APS_Lab | 0:860fafcf34d6 | 70 | |
APS_Lab | 0:860fafcf34d6 | 71 | /* Assert CHIP_SEL = disable */ |
APS_Lab | 0:860fafcf34d6 | 72 | void ADXL362::chipSelOff() { |
APS_Lab | 0:860fafcf34d6 | 73 | *_cs = 1; |
APS_Lab | 0:860fafcf34d6 | 74 | } |
APS_Lab | 0:860fafcf34d6 | 75 | |
APS_Lab | 0:860fafcf34d6 | 76 | /* delay for CHIP_SEL */ |
APS_Lab | 0:860fafcf34d6 | 77 | void ADXL362::chipSelDelay() { |
APS_Lab | 0:860fafcf34d6 | 78 | wait(WAIT_US(0.2)); |
APS_Lab | 0:860fafcf34d6 | 79 | } |
APS_Lab | 0:860fafcf34d6 | 80 | /** ========================================== |
APS_Lab | 0:860fafcf34d6 | 81 | * Public ( ADXL Configuration ) |
APS_Lab | 0:860fafcf34d6 | 82 | * ========================================== */ |
APS_Lab | 0:860fafcf34d6 | 83 | void ADXL362::set_gravity(int g) |
APS_Lab | 0:860fafcf34d6 | 84 | { |
APS_Lab | 0:860fafcf34d6 | 85 | int value; |
APS_Lab | 0:860fafcf34d6 | 86 | unsigned char g_reg; |
APS_Lab | 0:860fafcf34d6 | 87 | |
APS_Lab | 0:860fafcf34d6 | 88 | switch(g) |
APS_Lab | 0:860fafcf34d6 | 89 | { |
APS_Lab | 0:860fafcf34d6 | 90 | case GRAVITY_2G: |
APS_Lab | 0:860fafcf34d6 | 91 | gravity = GRAVITY_2G; |
APS_Lab | 0:860fafcf34d6 | 92 | g_reg = 0x00; |
APS_Lab | 0:860fafcf34d6 | 93 | break; |
APS_Lab | 0:860fafcf34d6 | 94 | case GRAVITY_4G: |
APS_Lab | 0:860fafcf34d6 | 95 | gravity = GRAVITY_4G; |
APS_Lab | 0:860fafcf34d6 | 96 | g_reg = 0x40; |
APS_Lab | 0:860fafcf34d6 | 97 | break; |
APS_Lab | 0:860fafcf34d6 | 98 | case GRAVITY_8G: |
APS_Lab | 0:860fafcf34d6 | 99 | gravity = GRAVITY_8G; |
APS_Lab | 0:860fafcf34d6 | 100 | g_reg = 0x80; |
APS_Lab | 0:860fafcf34d6 | 101 | break; |
APS_Lab | 0:860fafcf34d6 | 102 | default: |
APS_Lab | 0:860fafcf34d6 | 103 | gravity = GRAVITY_2G; |
APS_Lab | 0:860fafcf34d6 | 104 | g_reg = 0x00; |
APS_Lab | 0:860fafcf34d6 | 105 | break; |
APS_Lab | 0:860fafcf34d6 | 106 | } |
APS_Lab | 0:860fafcf34d6 | 107 | value = regRD(FILTER_CTL); |
APS_Lab | 0:860fafcf34d6 | 108 | value &= 0x3f; |
APS_Lab | 0:860fafcf34d6 | 109 | value |= g_reg; |
APS_Lab | 0:860fafcf34d6 | 110 | regWR(FILTER_CTL, value); |
APS_Lab | 0:860fafcf34d6 | 111 | set_scalefactor(); |
APS_Lab | 0:860fafcf34d6 | 112 | } |
APS_Lab | 0:860fafcf34d6 | 113 | |
APS_Lab | 0:860fafcf34d6 | 114 | void ADXL362::set_ODR(int o) |
APS_Lab | 0:860fafcf34d6 | 115 | { |
APS_Lab | 0:860fafcf34d6 | 116 | int value; |
APS_Lab | 0:860fafcf34d6 | 117 | unsigned char o_reg; |
APS_Lab | 0:860fafcf34d6 | 118 | |
APS_Lab | 0:860fafcf34d6 | 119 | switch(o) |
APS_Lab | 0:860fafcf34d6 | 120 | { |
APS_Lab | 0:860fafcf34d6 | 121 | case ODR_12: |
APS_Lab | 0:860fafcf34d6 | 122 | odr = ODR_12; |
APS_Lab | 0:860fafcf34d6 | 123 | o_reg = 0x00; |
APS_Lab | 0:860fafcf34d6 | 124 | break; |
APS_Lab | 0:860fafcf34d6 | 125 | case ODR_25: |
APS_Lab | 0:860fafcf34d6 | 126 | odr = ODR_25; |
APS_Lab | 0:860fafcf34d6 | 127 | o_reg = 0x01; |
APS_Lab | 0:860fafcf34d6 | 128 | break; |
APS_Lab | 0:860fafcf34d6 | 129 | case ODR_50: |
APS_Lab | 0:860fafcf34d6 | 130 | odr = ODR_50; |
APS_Lab | 0:860fafcf34d6 | 131 | o_reg = 0x02; |
APS_Lab | 0:860fafcf34d6 | 132 | break; |
APS_Lab | 0:860fafcf34d6 | 133 | case ODR_100: |
APS_Lab | 0:860fafcf34d6 | 134 | odr = ODR_100; |
APS_Lab | 0:860fafcf34d6 | 135 | o_reg = 0x03; |
APS_Lab | 0:860fafcf34d6 | 136 | break; |
APS_Lab | 0:860fafcf34d6 | 137 | case ODR_200: |
APS_Lab | 0:860fafcf34d6 | 138 | odr = ODR_200; |
APS_Lab | 0:860fafcf34d6 | 139 | o_reg = 0x04; |
APS_Lab | 0:860fafcf34d6 | 140 | break; |
APS_Lab | 0:860fafcf34d6 | 141 | case ODR_400: |
APS_Lab | 0:860fafcf34d6 | 142 | odr = ODR_400; |
APS_Lab | 0:860fafcf34d6 | 143 | o_reg = 0x07; |
APS_Lab | 0:860fafcf34d6 | 144 | break; |
APS_Lab | 0:860fafcf34d6 | 145 | default: |
APS_Lab | 0:860fafcf34d6 | 146 | odr = ODR_100; |
APS_Lab | 0:860fafcf34d6 | 147 | o_reg = 0x03; |
APS_Lab | 0:860fafcf34d6 | 148 | break; |
APS_Lab | 0:860fafcf34d6 | 149 | } |
APS_Lab | 0:860fafcf34d6 | 150 | value = regRD(FILTER_CTL); |
APS_Lab | 0:860fafcf34d6 | 151 | value &= 0xf8; |
APS_Lab | 0:860fafcf34d6 | 152 | value |= o_reg; |
APS_Lab | 0:860fafcf34d6 | 153 | regWR(FILTER_CTL, value); |
APS_Lab | 0:860fafcf34d6 | 154 | } |
APS_Lab | 0:860fafcf34d6 | 155 | |
APS_Lab | 0:860fafcf34d6 | 156 | void ADXL362::set_powermode(int m) |
APS_Lab | 0:860fafcf34d6 | 157 | { |
APS_Lab | 0:860fafcf34d6 | 158 | ADXL362::SetMesureParam(m); |
APS_Lab | 0:860fafcf34d6 | 159 | } |
APS_Lab | 0:860fafcf34d6 | 160 | |
APS_Lab | 0:860fafcf34d6 | 161 | void ADXL362::set_wakeupmode(void) |
APS_Lab | 0:860fafcf34d6 | 162 | { |
APS_Lab | 0:860fafcf34d6 | 163 | regWR(THRESH_ACT_L, 0x50); |
APS_Lab | 0:860fafcf34d6 | 164 | regWR(THRESH_ACT_H, 0x00); |
APS_Lab | 0:860fafcf34d6 | 165 | regWR(TIME_ACT, 0x00); |
APS_Lab | 0:860fafcf34d6 | 166 | regWR(THRESH_INACT_L, 0xff); |
APS_Lab | 0:860fafcf34d6 | 167 | regWR(THRESH_INACT_H, 0x07); |
APS_Lab | 0:860fafcf34d6 | 168 | regWR(TIME_INACT_L, 0x06); |
APS_Lab | 0:860fafcf34d6 | 169 | regWR(TIME_INACT_H, 0x00); |
APS_Lab | 0:860fafcf34d6 | 170 | regWR(ACT_INACT_CTL, 0x1F); |
APS_Lab | 0:860fafcf34d6 | 171 | regWR(INTMAP1, 0xC0); |
APS_Lab | 0:860fafcf34d6 | 172 | regWR(POWER_CTL, 0x0E); |
APS_Lab | 0:860fafcf34d6 | 173 | } |
APS_Lab | 0:860fafcf34d6 | 174 | |
APS_Lab | 0:860fafcf34d6 | 175 | void ADXL362::set_scalefactor(void) |
APS_Lab | 0:860fafcf34d6 | 176 | { |
APS_Lab | 0:860fafcf34d6 | 177 | float base, sf; |
APS_Lab | 0:860fafcf34d6 | 178 | |
APS_Lab | 0:860fafcf34d6 | 179 | base = (gravity / 2.0f); |
APS_Lab | 0:860fafcf34d6 | 180 | sf = base*(0.001f)*9.80665f; |
APS_Lab | 0:860fafcf34d6 | 181 | scaleAccel = sf; |
APS_Lab | 0:860fafcf34d6 | 182 | } |
APS_Lab | 0:860fafcf34d6 | 183 | |
APS_Lab | 0:860fafcf34d6 | 184 | void ADXL362::start(void) |
APS_Lab | 0:860fafcf34d6 | 185 | { |
APS_Lab | 0:860fafcf34d6 | 186 | int value; |
APS_Lab | 0:860fafcf34d6 | 187 | value = regRD(POWER_CTL); |
APS_Lab | 0:860fafcf34d6 | 188 | value &= 0xfc; |
APS_Lab | 0:860fafcf34d6 | 189 | value |= POWER_CTL_MESURE; |
APS_Lab | 0:860fafcf34d6 | 190 | regWR(POWER_CTL, value); |
APS_Lab | 0:860fafcf34d6 | 191 | wait_ms(5); |
APS_Lab | 0:860fafcf34d6 | 192 | GetStatus(); |
APS_Lab | 0:860fafcf34d6 | 193 | } |
APS_Lab | 0:860fafcf34d6 | 194 | |
APS_Lab | 0:860fafcf34d6 | 195 | void ADXL362::stop(void) |
APS_Lab | 0:860fafcf34d6 | 196 | { |
APS_Lab | 0:860fafcf34d6 | 197 | int value; |
APS_Lab | 0:860fafcf34d6 | 198 | value = regRD(POWER_CTL); |
APS_Lab | 0:860fafcf34d6 | 199 | value &= 0xfc; |
APS_Lab | 0:860fafcf34d6 | 200 | value |= POWER_CTL_STOP; |
APS_Lab | 0:860fafcf34d6 | 201 | regWR(POWER_CTL, value); |
APS_Lab | 0:860fafcf34d6 | 202 | wait_ms(5); |
APS_Lab | 0:860fafcf34d6 | 203 | GetStatus(); |
APS_Lab | 0:860fafcf34d6 | 204 | } |
APS_Lab | 0:860fafcf34d6 | 205 | |
APS_Lab | 0:860fafcf34d6 | 206 | /** ========================================== |
APS_Lab | 0:860fafcf34d6 | 207 | * Public ( Send Command to Device ) |
APS_Lab | 0:860fafcf34d6 | 208 | * ========================================== */ |
APS_Lab | 0:860fafcf34d6 | 209 | /* Write 16bit-Aligned Register */ |
APS_Lab | 0:860fafcf34d6 | 210 | void ADXL362::SoftReset() { |
APS_Lab | 0:860fafcf34d6 | 211 | regWR(SOFT_RESET, SOFT_RESET_ADXL362); |
APS_Lab | 0:860fafcf34d6 | 212 | wait(0.5); |
APS_Lab | 0:860fafcf34d6 | 213 | } |
APS_Lab | 0:860fafcf34d6 | 214 | |
APS_Lab | 0:860fafcf34d6 | 215 | void ADXL362::SetMesureParam(int param) { |
APS_Lab | 0:860fafcf34d6 | 216 | int value; |
APS_Lab | 0:860fafcf34d6 | 217 | value = regRD(POWER_CTL); |
APS_Lab | 0:860fafcf34d6 | 218 | param &= ~(POWER_CTL_MODEMASK); |
APS_Lab | 0:860fafcf34d6 | 219 | value &= POWER_CTL_MODEMASK; |
APS_Lab | 0:860fafcf34d6 | 220 | value |= param; |
APS_Lab | 0:860fafcf34d6 | 221 | regWR(POWER_CTL, value); |
APS_Lab | 0:860fafcf34d6 | 222 | } |
APS_Lab | 0:860fafcf34d6 | 223 | |
APS_Lab | 0:860fafcf34d6 | 224 | void ADXL362::StartMesure() { |
APS_Lab | 0:860fafcf34d6 | 225 | int value; |
APS_Lab | 0:860fafcf34d6 | 226 | GetStatus(); |
APS_Lab | 0:860fafcf34d6 | 227 | value = regRD(POWER_CTL); |
APS_Lab | 0:860fafcf34d6 | 228 | value &= ~(POWER_CTL_MODEMASK); |
APS_Lab | 0:860fafcf34d6 | 229 | value |= POWER_CTL_MESURE; |
APS_Lab | 0:860fafcf34d6 | 230 | regWR(POWER_CTL, POWER_CTL_MESURE); |
APS_Lab | 0:860fafcf34d6 | 231 | value = regRD(POWER_CTL); |
APS_Lab | 0:860fafcf34d6 | 232 | wait_ms(5); |
APS_Lab | 0:860fafcf34d6 | 233 | GetStatus(); |
APS_Lab | 0:860fafcf34d6 | 234 | } |
APS_Lab | 0:860fafcf34d6 | 235 | |
APS_Lab | 0:860fafcf34d6 | 236 | int ADXL362::GetStatus() { |
APS_Lab | 0:860fafcf34d6 | 237 | int value; |
APS_Lab | 0:860fafcf34d6 | 238 | value = regRD(STATUS); |
APS_Lab | 0:860fafcf34d6 | 239 | return value; |
APS_Lab | 0:860fafcf34d6 | 240 | } |
APS_Lab | 0:860fafcf34d6 | 241 | |
APS_Lab | 0:860fafcf34d6 | 242 | /** ========================================== |
APS_Lab | 0:860fafcf34d6 | 243 | * Public ( Sensing ) |
APS_Lab | 0:860fafcf34d6 | 244 | * ========================================== */ |
APS_Lab | 0:860fafcf34d6 | 245 | void ADXL362::SensorRead(AccelTemp *pAT) { |
APS_Lab | 0:860fafcf34d6 | 246 | int burstBuf[8]; |
APS_Lab | 0:860fafcf34d6 | 247 | /* Xx2 + Yx2 + Zx2 + Tempx2 = 8*/ |
APS_Lab | 0:860fafcf34d6 | 248 | regBurstRD(XDATA_L, 8, burstBuf); |
APS_Lab | 0:860fafcf34d6 | 249 | convertSensorData(pAT, burstBuf); |
APS_Lab | 0:860fafcf34d6 | 250 | #if 0 |
APS_Lab | 0:860fafcf34d6 | 251 | uart->printf("ADXL362[ax] = 0x%02x\n", pAT->ax); |
APS_Lab | 0:860fafcf34d6 | 252 | uart->printf("ADXL362[ay] = 0x%02x\n", pAT->ay); |
APS_Lab | 0:860fafcf34d6 | 253 | uart->printf("ADXL362[az] = 0x%02x\n", pAT->az); |
APS_Lab | 0:860fafcf34d6 | 254 | uart->printf("ADXL362[tm] = 0x%02x\n", pAT->tm) |
APS_Lab | 0:860fafcf34d6 | 255 | #endif |
APS_Lab | 0:860fafcf34d6 | 256 | updateMinMax(&minStore, &maxStore, pAT); |
APS_Lab | 0:860fafcf34d6 | 257 | } |
APS_Lab | 0:860fafcf34d6 | 258 | |
APS_Lab | 0:860fafcf34d6 | 259 | /** ========================================== |
APS_Lab | 0:860fafcf34d6 | 260 | * Public ( Sub Infomation ) |
APS_Lab | 0:860fafcf34d6 | 261 | * ========================================== */ |
APS_Lab | 0:860fafcf34d6 | 262 | /* Get Internal Store (for Min Info) */ |
APS_Lab | 0:860fafcf34d6 | 263 | AccelTemp* ADXL362::GetMinInfo(void) { |
APS_Lab | 0:860fafcf34d6 | 264 | return &minStore; |
APS_Lab | 0:860fafcf34d6 | 265 | } |
APS_Lab | 0:860fafcf34d6 | 266 | |
APS_Lab | 0:860fafcf34d6 | 267 | /* Get Internal Store (for Max Info) */ |
APS_Lab | 0:860fafcf34d6 | 268 | AccelTemp* ADXL362::GetMaxInfo(void) { |
APS_Lab | 0:860fafcf34d6 | 269 | return &maxStore; |
APS_Lab | 0:860fafcf34d6 | 270 | } |
APS_Lab | 0:860fafcf34d6 | 271 | |
APS_Lab | 0:860fafcf34d6 | 272 | /* Convert CtrlValue to Real for Accelerometer */ |
APS_Lab | 0:860fafcf34d6 | 273 | float ADXL362::ConvAccel(int ctrlval) { |
APS_Lab | 0:860fafcf34d6 | 274 | return scaleAccel * (float)ctrlval; |
APS_Lab | 0:860fafcf34d6 | 275 | } |
APS_Lab | 0:860fafcf34d6 | 276 | |
APS_Lab | 0:860fafcf34d6 | 277 | /* Convert CtrlValue to Real for Thermal Sensor */ |
APS_Lab | 0:860fafcf34d6 | 278 | float ADXL362::ConvThermal(int ctrlval) { |
APS_Lab | 0:860fafcf34d6 | 279 | return (scaleThermal * (float)ctrlval) + offsetThermal; |
APS_Lab | 0:860fafcf34d6 | 280 | } |
APS_Lab | 0:860fafcf34d6 | 281 | |
APS_Lab | 0:860fafcf34d6 | 282 | /** ========================================== |
APS_Lab | 0:860fafcf34d6 | 283 | * Private ( convert sensing value ) |
APS_Lab | 0:860fafcf34d6 | 284 | * ========================================== */ |
APS_Lab | 0:860fafcf34d6 | 285 | void ADXL362::convertSensorData(AccelTemp *at, int *buf) { |
APS_Lab | 0:860fafcf34d6 | 286 | at->ax = ext12bitToInt(buf[0], buf[1]); |
APS_Lab | 0:860fafcf34d6 | 287 | at->ay = ext12bitToInt(buf[2], buf[3]); |
APS_Lab | 0:860fafcf34d6 | 288 | at->az = ext12bitToInt(buf[4], buf[5]); |
APS_Lab | 0:860fafcf34d6 | 289 | at->tm = ext12bitToInt(buf[6], buf[7]); |
APS_Lab | 0:860fafcf34d6 | 290 | } |
APS_Lab | 0:860fafcf34d6 | 291 | |
APS_Lab | 0:860fafcf34d6 | 292 | int ADXL362::ext12bitToInt(int l, int h) |
APS_Lab | 0:860fafcf34d6 | 293 | { |
APS_Lab | 0:860fafcf34d6 | 294 | h <<= 8; |
APS_Lab | 0:860fafcf34d6 | 295 | h &= 0x0f00; |
APS_Lab | 0:860fafcf34d6 | 296 | h |= l & 0xff; |
APS_Lab | 0:860fafcf34d6 | 297 | if ((h & 0x800) != 0) { |
APS_Lab | 0:860fafcf34d6 | 298 | h |= 0xfffff000; |
APS_Lab | 0:860fafcf34d6 | 299 | } |
APS_Lab | 0:860fafcf34d6 | 300 | return h; |
APS_Lab | 0:860fafcf34d6 | 301 | } |
APS_Lab | 0:860fafcf34d6 | 302 | |
APS_Lab | 0:860fafcf34d6 | 303 | /** ========================================== |
APS_Lab | 0:860fafcf34d6 | 304 | * Private ( SPI Communication ) |
APS_Lab | 0:860fafcf34d6 | 305 | * ========================================== */ |
APS_Lab | 0:860fafcf34d6 | 306 | #define ADXL362_SPI_CMD_WR 0x0A |
APS_Lab | 0:860fafcf34d6 | 307 | #define ADXL362_SPI_CMD_RD 0x0B |
APS_Lab | 0:860fafcf34d6 | 308 | #define ADXL362_SPI_CMD_RD_FIFO 0x0D |
APS_Lab | 0:860fafcf34d6 | 309 | |
APS_Lab | 0:860fafcf34d6 | 310 | /* Read Single Register */ |
APS_Lab | 0:860fafcf34d6 | 311 | int ADXL362::regRD(int regAddr) { |
APS_Lab | 0:860fafcf34d6 | 312 | int recvData; |
APS_Lab | 0:860fafcf34d6 | 313 | regBurstRD(regAddr, 1, &recvData); |
APS_Lab | 0:860fafcf34d6 | 314 | return recvData; |
APS_Lab | 0:860fafcf34d6 | 315 | } |
APS_Lab | 0:860fafcf34d6 | 316 | |
APS_Lab | 0:860fafcf34d6 | 317 | /* Read Multi Register */ |
APS_Lab | 0:860fafcf34d6 | 318 | void ADXL362::regBurstRD(int regAddr, int numBurst, int *recvBuf) { |
APS_Lab | 0:860fafcf34d6 | 319 | int cnt; |
APS_Lab | 0:860fafcf34d6 | 320 | |
APS_Lab | 0:860fafcf34d6 | 321 | /* SPI Burst Read Loop ** |
APS_Lab | 0:860fafcf34d6 | 322 | * Write A -> Write B : Read A -> Write C : Read B -> ... */ |
APS_Lab | 0:860fafcf34d6 | 323 | _spi->lock(); |
APS_Lab | 0:860fafcf34d6 | 324 | chipSelOn(); |
APS_Lab | 0:860fafcf34d6 | 325 | /* WriteADDR[n] and ReadData[n-1] */ |
APS_Lab | 0:860fafcf34d6 | 326 | _spi->write(ADXL362_SPI_CMD_RD); |
APS_Lab | 0:860fafcf34d6 | 327 | _spi->write(regAddr); |
APS_Lab | 0:860fafcf34d6 | 328 | for (cnt = 0; cnt < numBurst; cnt++) { |
APS_Lab | 0:860fafcf34d6 | 329 | /* WriteADDR[n] and ReadData[n-1] */ |
APS_Lab | 0:860fafcf34d6 | 330 | recvBuf[cnt] = _spi->write(0x00); |
APS_Lab | 0:860fafcf34d6 | 331 | } |
APS_Lab | 0:860fafcf34d6 | 332 | chipSelOff(); |
APS_Lab | 0:860fafcf34d6 | 333 | _spi->unlock(); |
APS_Lab | 0:860fafcf34d6 | 334 | return; |
APS_Lab | 0:860fafcf34d6 | 335 | } |
APS_Lab | 0:860fafcf34d6 | 336 | |
APS_Lab | 0:860fafcf34d6 | 337 | /* Write 16bit-Aligned Register */ |
APS_Lab | 0:860fafcf34d6 | 338 | void ADXL362::regWR(int regAddr, int value) { |
APS_Lab | 0:860fafcf34d6 | 339 | _spi->lock(); |
APS_Lab | 0:860fafcf34d6 | 340 | chipSelOn(); |
APS_Lab | 0:860fafcf34d6 | 341 | _spi->write(ADXL362_SPI_CMD_WR); |
APS_Lab | 0:860fafcf34d6 | 342 | _spi->write(regAddr); |
APS_Lab | 0:860fafcf34d6 | 343 | _spi->write(value); |
APS_Lab | 0:860fafcf34d6 | 344 | chipSelOff(); |
APS_Lab | 0:860fafcf34d6 | 345 | _spi->unlock(); |
APS_Lab | 0:860fafcf34d6 | 346 | return; |
APS_Lab | 0:860fafcf34d6 | 347 | } |
APS_Lab | 0:860fafcf34d6 | 348 | |
APS_Lab | 0:860fafcf34d6 | 349 | /** ========================================== |
APS_Lab | 0:860fafcf34d6 | 350 | * Private ( Control internal Stores ) |
APS_Lab | 0:860fafcf34d6 | 351 | * ========================================== */ |
APS_Lab | 0:860fafcf34d6 | 352 | /* clear internal Min/Max infomation */ |
APS_Lab | 0:860fafcf34d6 | 353 | void ADXL362::initMinMax |
APS_Lab | 0:860fafcf34d6 | 354 | (AccelTemp *minData, AccelTemp *maxData) { |
APS_Lab | 0:860fafcf34d6 | 355 | minData->ax = INT_MAX; |
APS_Lab | 0:860fafcf34d6 | 356 | minData->ay = INT_MAX; |
APS_Lab | 0:860fafcf34d6 | 357 | minData->az = INT_MAX; |
APS_Lab | 0:860fafcf34d6 | 358 | minData->tm = INT_MAX; |
APS_Lab | 0:860fafcf34d6 | 359 | |
APS_Lab | 0:860fafcf34d6 | 360 | maxData->ax = INT_MIN; |
APS_Lab | 0:860fafcf34d6 | 361 | maxData->ay = INT_MIN; |
APS_Lab | 0:860fafcf34d6 | 362 | maxData->az = INT_MIN; |
APS_Lab | 0:860fafcf34d6 | 363 | maxData->tm = INT_MIN; |
APS_Lab | 0:860fafcf34d6 | 364 | } |
APS_Lab | 0:860fafcf34d6 | 365 | |
APS_Lab | 0:860fafcf34d6 | 366 | /* update internal Min/Max infomation */ |
APS_Lab | 0:860fafcf34d6 | 367 | #define TEST_MIN_AND_SET(now, test) (now = (now >= test)? test : now) |
APS_Lab | 0:860fafcf34d6 | 368 | #define TEST_MAX_AND_SET(now, test) (now = (now <= test)? test : now) |
APS_Lab | 0:860fafcf34d6 | 369 | void ADXL362::updateMinMax |
APS_Lab | 0:860fafcf34d6 | 370 | (AccelTemp *minData, AccelTemp *maxData, AccelTemp *getData) { |
APS_Lab | 0:860fafcf34d6 | 371 | TEST_MIN_AND_SET(minData->ax, getData->ax); |
APS_Lab | 0:860fafcf34d6 | 372 | TEST_MIN_AND_SET(minData->ay, getData->ay); |
APS_Lab | 0:860fafcf34d6 | 373 | TEST_MIN_AND_SET(minData->az, getData->az); |
APS_Lab | 0:860fafcf34d6 | 374 | TEST_MIN_AND_SET(minData->tm, getData->tm); |
APS_Lab | 0:860fafcf34d6 | 375 | |
APS_Lab | 0:860fafcf34d6 | 376 | TEST_MAX_AND_SET(maxData->ax, getData->ax); |
APS_Lab | 0:860fafcf34d6 | 377 | TEST_MAX_AND_SET(maxData->ay, getData->ay); |
APS_Lab | 0:860fafcf34d6 | 378 | TEST_MAX_AND_SET(maxData->az, getData->az); |
APS_Lab | 0:860fafcf34d6 | 379 | TEST_MAX_AND_SET(maxData->tm, getData->tm); |
APS_Lab | 0:860fafcf34d6 | 380 | } |