Timo Karppinen / Mbed OS Pmod_ACL2_ADXL362_L432KC_OS6_tk1

Dependencies:   ADXL362

Committer:
timo_k2
Date:
Wed Dec 30 15:19:15 2020 +0000
Revision:
6:a0b604602460
Parent:
5:4d6ef028eeae
Child:
7:652e2c5ad650
updated for OS6, printing notes on register defaults, calculating derivative.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
timo_k2 6:a0b604602460 1 /*
timo_k2 6:a0b604602460 2 * Copyright (c) 2006-2020 Arm Limited and affiliates.
timo_k2 6:a0b604602460 3 * SPDX-License-Identifier: Apache-2.0
timo_k2 6:a0b604602460 4 *******************************************************************************
timo_k2 6:a0b604602460 5 * Pmod_ACL2_ADXL362_L432KC_OS6
timo_k2 6:a0b604602460 6 *
timo_k2 6:a0b604602460 7 * Accelerations in 3D with Analog Devices ADXL362. The PmodACL2 was used but
timo_k2 6:a0b604602460 8 * should work with almost any sensor module with the ADXL362.
timo_k2 6:a0b604602460 9 * The ADXL362 connects with SPI. The library ADXL362 published by Analog Devises
timo_k2 6:a0b604602460 10 * includes the necessary methods for setting up the sensor and reading the
timo_k2 6:a0b604602460 11 * acceleration results.
timo_k2 6:a0b604602460 12 *
timo_k2 6:a0b604602460 13 * Hardware
timo_k2 6:a0b604602460 14 * ST NUCLEO L432KC or almost any other MbedOS microcontroller,
timo_k2 6:a0b604602460 15 * Digilent PmodACL2 sensor module with the ADXL362 acceleration sensor
timo_k2 6:a0b604602460 16 * A LED with 220 Ohm series resistor for indicating movement
timo_k2 6:a0b604602460 17 *
timo_k2 6:a0b604602460 18 * Connect:
timo_k2 6:a0b604602460 19 * L432KC D13 - ACL2 4 SCLK hardware defined for the SPI
timo_k2 6:a0b604602460 20 * L432KC D12 - ACL2 3 MISO hardware defined for the SPI
timo_k2 6:a0b604602460 21 * L432KC D11 - ACL2 2 MOSI
timo_k2 6:a0b604602460 22 * L432KC D5 - ACL2 1 CS or any other free
timo_k2 6:a0b604602460 23 * GND - ACL2 5 GND
timo_k2 6:a0b604602460 24 * Vcc - ACL2 6 Vcc
timo_k2 6:a0b604602460 25 * The ACL2 pins 7 and 8 will be connected if hardware interrupts will be used.
timo_k2 6:a0b604602460 26 * L432KC D1 - LED - 220 Ohm - GND
timo_k2 6:a0b604602460 27 *
timo_k2 6:a0b604602460 28 * Reference:
timo_k2 6:a0b604602460 29 * PmodACL2 https://reference.digilentinc.com/reference/pmod/pmodacl2/start
timo_k2 6:a0b604602460 30 * ADXL362 https://www.analog.com/en/products/adxl362.html
timo_k2 6:a0b604602460 31 * Datasheet for register reference
timo_k2 6:a0b604602460 32 * https://www.analog.com/media/en/technical-documentation/data-sheets/ADXL362.pdf
timo_k2 6:a0b604602460 33 *
timo_k2 6:a0b604602460 34 * Timo Karppinen 30.12.2020 Apache-2.0
timo_k2 6:a0b604602460 35 ******************************************************************************/
timo_k2 6:a0b604602460 36
jackclar 0:1ca806626aba 37 #include "mbed.h"
jackclar 0:1ca806626aba 38 #include "ADXL362.h"
timo_k2 5:4d6ef028eeae 39
jackclar 0:1ca806626aba 40 // ADXL362::ADXL362(PinName CS, PinName MOSI, PinName MISO, PinName SCK) :
timo_k2 5:4d6ef028eeae 41 ADXL362 ADXL362(D5,D11,D12,D13);
timo_k2 5:4d6ef028eeae 42
timo_k2 5:4d6ef028eeae 43 DigitalOut moveLed(D1);
jackclar 0:1ca806626aba 44
jackclar 2:3299365b3e3c 45 int ADXL362_reg_print(int start, int length);
timo_k2 5:4d6ef028eeae 46 void ADXL362_movement_detect();
jackclar 0:1ca806626aba 47
jackclar 0:1ca806626aba 48 int main()
jackclar 0:1ca806626aba 49 {
jackclar 2:3299365b3e3c 50 ADXL362.reset();
timo_k2 5:4d6ef028eeae 51 // we need to wait at least 500ms after ADXL362 reset
timo_k2 5:4d6ef028eeae 52 ThisThread::sleep_for(600ms);
jackclar 2:3299365b3e3c 53 ADXL362.set_mode(ADXL362::MEASUREMENT);
jackclar 4:a6069cbc4c71 54 ADXL362_reg_print(0, 0);
timo_k2 5:4d6ef028eeae 55 ADXL362_movement_detect();
jackclar 2:3299365b3e3c 56 }
jackclar 2:3299365b3e3c 57
timo_k2 5:4d6ef028eeae 58 void ADXL362_movement_detect()
jackclar 2:3299365b3e3c 59 {
timo_k2 5:4d6ef028eeae 60 int8_t x1,y1,z1,x2,y2,z2,x,y,z,dx,dy,dz;
timo_k2 5:4d6ef028eeae 61 int i = 0;
jackclar 2:3299365b3e3c 62 while(1)
jackclar 2:3299365b3e3c 63 {
jackclar 2:3299365b3e3c 64
jackclar 2:3299365b3e3c 65 while(1)
jackclar 2:3299365b3e3c 66 {
jackclar 2:3299365b3e3c 67 x1=ADXL362.scanx_u8();
jackclar 2:3299365b3e3c 68 y1=ADXL362.scany_u8();
jackclar 2:3299365b3e3c 69 z1=ADXL362.scanz_u8();
timo_k2 5:4d6ef028eeae 70 ThisThread::sleep_for(10ms);
jackclar 2:3299365b3e3c 71 x2=ADXL362.scanx_u8();
jackclar 2:3299365b3e3c 72 y2=ADXL362.scany_u8();
jackclar 2:3299365b3e3c 73 z2=ADXL362.scanz_u8();
jackclar 2:3299365b3e3c 74
timo_k2 5:4d6ef028eeae 75 x=(x1 + x2)/2;
timo_k2 5:4d6ef028eeae 76 y=(y1 + y2)/2;
timo_k2 5:4d6ef028eeae 77 z=(z1 + z2)/2;
jackclar 2:3299365b3e3c 78
timo_k2 5:4d6ef028eeae 79 dx=abs(x1 - x2);
timo_k2 5:4d6ef028eeae 80 dy=abs(y1 - y2);
timo_k2 5:4d6ef028eeae 81 dz=abs(z1 - z2);
timo_k2 5:4d6ef028eeae 82
timo_k2 5:4d6ef028eeae 83 if (dx>10 || dy>10 || dz>10)
jackclar 2:3299365b3e3c 84 break;
jackclar 2:3299365b3e3c 85
timo_k2 5:4d6ef028eeae 86 printf("x = %3d y = %3d z = %3d dx = %3d dy = %3d dz = %3d\r\n",x,y,z,dx,dy,dz);
timo_k2 5:4d6ef028eeae 87 ThisThread::sleep_for(100ms);
jackclar 2:3299365b3e3c 88 }
jackclar 2:3299365b3e3c 89
timo_k2 5:4d6ef028eeae 90 moveLed = 1;
timo_k2 5:4d6ef028eeae 91 //wait(2);
timo_k2 5:4d6ef028eeae 92 ThisThread::sleep_for(2s);
timo_k2 5:4d6ef028eeae 93 moveLed = 0;
jackclar 2:3299365b3e3c 94 i++;
timo_k2 5:4d6ef028eeae 95 printf("%d\r\n", i);
jackclar 2:3299365b3e3c 96
jackclar 2:3299365b3e3c 97 }
jackclar 0:1ca806626aba 98 }
jackclar 0:1ca806626aba 99
jackclar 2:3299365b3e3c 100 int ADXL362_reg_print(int start, int length)
timo_k2 5:4d6ef028eeae 101 /*
timo_k2 5:4d6ef028eeae 102 * The register bit allocations are explained in the datasheet
timo_k2 5:4d6ef028eeae 103 * https://www.analog.com/media/en/technical-documentation/data-sheets/ADXL362.pdf
timo_k2 5:4d6ef028eeae 104 * starting on page 23.
timo_k2 5:4d6ef028eeae 105 */
jackclar 0:1ca806626aba 106 {
jackclar 2:3299365b3e3c 107 uint8_t i;
timo_k2 5:4d6ef028eeae 108 char name[32];
timo_k2 5:4d6ef028eeae 109 char note[64];
timo_k2 5:4d6ef028eeae 110
jackclar 2:3299365b3e3c 111 ADXL362::ADXL362_register_t reg;
jackclar 4:a6069cbc4c71 112 if(start >= 0x00 && start <= 0x2E && length >= 0x00 && (ADXL362.read_reg(ADXL362.DEVID_AD) == 0xAD))
jackclar 0:1ca806626aba 113 {
jackclar 2:3299365b3e3c 114 if(length == 0)
jackclar 2:3299365b3e3c 115 {
jackclar 2:3299365b3e3c 116 start = 0;
jackclar 2:3299365b3e3c 117 length = 47;
jackclar 2:3299365b3e3c 118 }
jackclar 2:3299365b3e3c 119
jackclar 2:3299365b3e3c 120 for(i = start; i < start + length; i++)
jackclar 0:1ca806626aba 121 {
jackclar 2:3299365b3e3c 122 switch(i)
jackclar 2:3299365b3e3c 123 {
jackclar 2:3299365b3e3c 124 case 0x00:
timo_k2 5:4d6ef028eeae 125 snprintf(name, 32, "DEVID_AD" );
timo_k2 5:4d6ef028eeae 126 snprintf(note, 64, "default 0xAD = I am the ADXL362");
jackclar 2:3299365b3e3c 127 reg = ADXL362.DEVID_AD;
jackclar 2:3299365b3e3c 128 break;
jackclar 2:3299365b3e3c 129 case 0x01:
timo_k2 5:4d6ef028eeae 130 snprintf(name, 32, "DEVID_MST" );
timo_k2 5:4d6ef028eeae 131 snprintf(note, 64, "-");
jackclar 2:3299365b3e3c 132 reg = ADXL362.DEVID_MST;
jackclar 2:3299365b3e3c 133 break;
jackclar 2:3299365b3e3c 134 case 0x02:
timo_k2 5:4d6ef028eeae 135 snprintf(name, 32, "PARTID" );
timo_k2 5:4d6ef028eeae 136 snprintf(note, 64, "-");
jackclar 2:3299365b3e3c 137 reg = ADXL362.PARTID;
jackclar 2:3299365b3e3c 138 break;
jackclar 2:3299365b3e3c 139 case 0x03:
timo_k2 5:4d6ef028eeae 140 snprintf(name, 32, "REVID" );
timo_k2 5:4d6ef028eeae 141 snprintf(note, 64, "-");
jackclar 2:3299365b3e3c 142 reg = ADXL362.REVID;
jackclar 2:3299365b3e3c 143 break;
jackclar 2:3299365b3e3c 144 case 0x08:
timo_k2 5:4d6ef028eeae 145 snprintf(name, 32, "XDATA" );
timo_k2 5:4d6ef028eeae 146 snprintf(note, 63, "binary 8bit, two's complement");
jackclar 2:3299365b3e3c 147 reg = ADXL362.XDATA;
jackclar 2:3299365b3e3c 148 break;
jackclar 2:3299365b3e3c 149 case 0x09:
timo_k2 5:4d6ef028eeae 150 snprintf(name, 32, "YDATA" );
timo_k2 5:4d6ef028eeae 151 snprintf(note, 64, "binary 8bit, two's complement");
jackclar 2:3299365b3e3c 152 reg = ADXL362.YDATA;
jackclar 2:3299365b3e3c 153 break;
jackclar 2:3299365b3e3c 154 case 0x0A:
timo_k2 5:4d6ef028eeae 155 snprintf(name, 32, "ZDATA" );
timo_k2 5:4d6ef028eeae 156 snprintf(note, 64, "binary 8bit, two's complement");
jackclar 2:3299365b3e3c 157 reg = ADXL362.ZDATA;
jackclar 2:3299365b3e3c 158 break;
jackclar 2:3299365b3e3c 159 case 0x0B:
timo_k2 5:4d6ef028eeae 160 snprintf(name, 32, "STATUS" );
timo_k2 5:4d6ef028eeae 161 snprintf(note, 64, "typically 0x41, 4=awake, 1=data ready");
jackclar 2:3299365b3e3c 162 reg = ADXL362.STATUS;
jackclar 2:3299365b3e3c 163 break;
jackclar 2:3299365b3e3c 164 case 0x0C:
timo_k2 5:4d6ef028eeae 165 snprintf(name, 32, "FIFO_ENTRIES_L" );
timo_k2 5:4d6ef028eeae 166 snprintf(note, 64, "-");
jackclar 2:3299365b3e3c 167 reg = ADXL362.FIFO_ENTRIES_L;
jackclar 2:3299365b3e3c 168 break;
jackclar 2:3299365b3e3c 169 case 0x0D:
timo_k2 5:4d6ef028eeae 170 snprintf(name, 32, "FIFO_ENTRIES_H" );
timo_k2 5:4d6ef028eeae 171 snprintf(note, 64, "-");
jackclar 2:3299365b3e3c 172 reg = ADXL362.FIFO_ENTRIES_H;
jackclar 2:3299365b3e3c 173 break;
jackclar 2:3299365b3e3c 174 case 0x0E:
timo_k2 5:4d6ef028eeae 175 snprintf(name, 32, "XDATA_L" );
timo_k2 5:4d6ef028eeae 176 snprintf(note, 64, "binary 12bit, two's complement");
jackclar 2:3299365b3e3c 177 reg = ADXL362.XDATA_L;
jackclar 2:3299365b3e3c 178 break;
jackclar 2:3299365b3e3c 179 case 0x0F:
timo_k2 5:4d6ef028eeae 180 snprintf(name, 32, "XDATA_H" );
timo_k2 5:4d6ef028eeae 181 snprintf(note, 64, "-");
jackclar 2:3299365b3e3c 182 reg = ADXL362.XDATA_H;
jackclar 2:3299365b3e3c 183 break;
jackclar 2:3299365b3e3c 184 case 0x10:
timo_k2 5:4d6ef028eeae 185 snprintf(name, 32, "YDATA_L" );
timo_k2 5:4d6ef028eeae 186 snprintf(note, 64, "binary 12bit, two's complement");
jackclar 2:3299365b3e3c 187 reg = ADXL362.YDATA_L;
jackclar 2:3299365b3e3c 188 break;
jackclar 2:3299365b3e3c 189 case 0x11:
timo_k2 5:4d6ef028eeae 190 snprintf(name, 32, "YDATA_H" );
timo_k2 5:4d6ef028eeae 191 snprintf(note, 64, "-");
jackclar 2:3299365b3e3c 192 reg = ADXL362.YDATA_H;
jackclar 2:3299365b3e3c 193 break;
jackclar 2:3299365b3e3c 194 case 0x12:
timo_k2 5:4d6ef028eeae 195 snprintf(name, 32, "ZDATA_L" );
timo_k2 5:4d6ef028eeae 196 snprintf(note, 64, "binary 12bit, two's complement");
jackclar 2:3299365b3e3c 197 reg = ADXL362.ZDATA_L;
jackclar 2:3299365b3e3c 198 break;
jackclar 2:3299365b3e3c 199 case 0x13:
timo_k2 5:4d6ef028eeae 200 snprintf(name, 32, "ZDATA_H" );
timo_k2 5:4d6ef028eeae 201 snprintf(note, 64, "-");
jackclar 2:3299365b3e3c 202 reg = ADXL362.ZDATA_H;
jackclar 2:3299365b3e3c 203 break;
jackclar 2:3299365b3e3c 204 case 0x14:
timo_k2 5:4d6ef028eeae 205 snprintf(name, 32, "TEMP_L" );
timo_k2 5:4d6ef028eeae 206 snprintf(note, 64, "-");
jackclar 2:3299365b3e3c 207 reg = ADXL362.TEMP_L;
jackclar 2:3299365b3e3c 208 break;
jackclar 2:3299365b3e3c 209 case 0x15:
timo_k2 5:4d6ef028eeae 210 snprintf(name, 32, "TEMP_H" );
timo_k2 5:4d6ef028eeae 211 snprintf(note, 64, "-");
jackclar 2:3299365b3e3c 212 reg = ADXL362.TEMP_H;
jackclar 2:3299365b3e3c 213 break;
jackclar 2:3299365b3e3c 214 case 0x1F:
timo_k2 5:4d6ef028eeae 215 snprintf(name, 32, "SOFT_RESET" );
timo_k2 5:4d6ef028eeae 216 snprintf(note, 64, "-");
jackclar 2:3299365b3e3c 217 reg = ADXL362.SOFT_RESET;
jackclar 2:3299365b3e3c 218 break;
jackclar 2:3299365b3e3c 219 case 0x20:
timo_k2 5:4d6ef028eeae 220 snprintf(name, 32, "THRESH_ACT_L" );
timo_k2 5:4d6ef028eeae 221 snprintf(note, 64, "Activity threshold value, binary 16bit");
jackclar 2:3299365b3e3c 222 reg = ADXL362.THRESH_ACT_L;
jackclar 2:3299365b3e3c 223 break;
jackclar 2:3299365b3e3c 224 case 0x21:
timo_k2 5:4d6ef028eeae 225 snprintf(name, 32, "THRESH_ACT_H" );
timo_k2 5:4d6ef028eeae 226 snprintf(note, 64, "-");
jackclar 2:3299365b3e3c 227 reg = ADXL362.THRESH_ACT_H;
jackclar 2:3299365b3e3c 228 break;
jackclar 2:3299365b3e3c 229 case 0x22:
timo_k2 5:4d6ef028eeae 230 snprintf(name, 32, "TIME_ACT" );
timo_k2 5:4d6ef028eeae 231 snprintf(note, 64, "-");
jackclar 2:3299365b3e3c 232 reg = ADXL362.TIME_ACT;
jackclar 2:3299365b3e3c 233 break;
jackclar 2:3299365b3e3c 234 case 0x23:
timo_k2 5:4d6ef028eeae 235 snprintf(name, 32, "THRESH_INACT_L" );
timo_k2 5:4d6ef028eeae 236 snprintf(note, 64, "Inactivity threshold value, binary 16bit");
jackclar 2:3299365b3e3c 237 reg = ADXL362.THRESH_INACT_L;
jackclar 2:3299365b3e3c 238 break;
jackclar 2:3299365b3e3c 239 case 0x24:
timo_k2 5:4d6ef028eeae 240 snprintf(name, 32, "THRESH_INACT_H" );
timo_k2 5:4d6ef028eeae 241 snprintf(note, 64, "-");
jackclar 2:3299365b3e3c 242 reg = ADXL362.THRESH_INACT_H;
jackclar 2:3299365b3e3c 243 break;
jackclar 2:3299365b3e3c 244 case 0x25:
timo_k2 5:4d6ef028eeae 245 snprintf(name, 32, "TIME_INACT_L" );
timo_k2 5:4d6ef028eeae 246 snprintf(note, 64, "-");
jackclar 2:3299365b3e3c 247 reg = ADXL362.TIME_INACT_L;
jackclar 2:3299365b3e3c 248 break;
jackclar 2:3299365b3e3c 249 case 0x26:
timo_k2 5:4d6ef028eeae 250 snprintf(name, 32, "TIME_INACT_H" );
timo_k2 5:4d6ef028eeae 251 snprintf(note, 64, "-");
jackclar 2:3299365b3e3c 252 reg = ADXL362.TIME_INACT_H;
jackclar 2:3299365b3e3c 253 break;
jackclar 2:3299365b3e3c 254 case 0x27:
timo_k2 5:4d6ef028eeae 255 snprintf(name, 32, "ACT_INACT_CTL" );
timo_k2 5:4d6ef028eeae 256 snprintf(note, 64, "default 0x00 = disable, 0x01 = enable");
jackclar 2:3299365b3e3c 257 reg = ADXL362.ACT_INACT_CTL;
jackclar 2:3299365b3e3c 258 break;
jackclar 2:3299365b3e3c 259 case 0x28:
timo_k2 5:4d6ef028eeae 260 snprintf(name, 32, "FIFO_CONTROL" );
timo_k2 5:4d6ef028eeae 261 snprintf(note, 64, "-");
jackclar 2:3299365b3e3c 262 reg = ADXL362.FIFO_CONTROL;
jackclar 2:3299365b3e3c 263 break;
jackclar 2:3299365b3e3c 264 case 0x29:
timo_k2 5:4d6ef028eeae 265 snprintf(name, 32, "FIFO_SAMPLES" );
timo_k2 5:4d6ef028eeae 266 snprintf(note, 64, "-");
jackclar 2:3299365b3e3c 267 reg = ADXL362.FIFO_SAMPLES;
jackclar 2:3299365b3e3c 268 break;
jackclar 2:3299365b3e3c 269 case 0x2A:
timo_k2 5:4d6ef028eeae 270 snprintf(name, 32, "INTMAP1" );
timo_k2 5:4d6ef028eeae 271 snprintf(note, 64, "-");
jackclar 2:3299365b3e3c 272 reg = ADXL362.INTMAP1;
jackclar 2:3299365b3e3c 273 break;
jackclar 2:3299365b3e3c 274 case 0x2B:
timo_k2 5:4d6ef028eeae 275 snprintf(name, 32, "INTMAP2" );
timo_k2 5:4d6ef028eeae 276 snprintf(note, 64, "-");
jackclar 2:3299365b3e3c 277 reg = ADXL362.INTMAP2;
jackclar 2:3299365b3e3c 278 break;
jackclar 2:3299365b3e3c 279 case 0x2C:
timo_k2 5:4d6ef028eeae 280 snprintf(name, 32, "FILTER_CTL" );
timo_k2 5:4d6ef028eeae 281 snprintf(note, 64, "default 0x13, 1=half samplin freq, 3=freq 100 sampl/sec");
jackclar 2:3299365b3e3c 282 reg = ADXL362.FILTER_CTL;
jackclar 2:3299365b3e3c 283 break;
jackclar 2:3299365b3e3c 284 case 0x2D:
timo_k2 5:4d6ef028eeae 285 snprintf(name, 32, "POWER_CTL" );
timo_k2 5:4d6ef028eeae 286 snprintf(note, 64, "default 0x02 = measure 3D");
jackclar 2:3299365b3e3c 287 reg = ADXL362.POWER_CTL;
jackclar 2:3299365b3e3c 288 break;
jackclar 2:3299365b3e3c 289 case 0x2E:
timo_k2 5:4d6ef028eeae 290 snprintf(name, 32, "SELF_TEST" );
timo_k2 5:4d6ef028eeae 291 snprintf(note, 64, "-");
jackclar 2:3299365b3e3c 292 reg = ADXL362.SELF_TEST;
jackclar 2:3299365b3e3c 293 break;
jackclar 2:3299365b3e3c 294 }
timo_k2 5:4d6ef028eeae 295 // Printing register content as hexadecimal and the notes
timo_k2 5:4d6ef028eeae 296 printf("register %d %s %x %s\n", i, name, ADXL362.read_reg(reg), note);
jackclar 0:1ca806626aba 297 }
jackclar 0:1ca806626aba 298 }
jackclar 0:1ca806626aba 299 else
jackclar 0:1ca806626aba 300 {
timo_k2 5:4d6ef028eeae 301 printf("Error");
jackclar 2:3299365b3e3c 302 return(-1);
jackclar 2:3299365b3e3c 303 }
jackclar 2:3299365b3e3c 304 return(0);
timo_k2 5:4d6ef028eeae 305 }