xrocusOS_ADXL355 version

Dependencies:   mbed SDFileSystem

Committer:
Inscape_ao
Date:
Mon May 27 08:17:29 2019 +0000
Revision:
15:35f01ee28b44
Parent:
14:89665bcef3f0
update FFlush

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Inscape_ao 14:89665bcef3f0 1 #include "mbed.h"
Inscape_ao 14:89665bcef3f0 2 #include "Adxl372Drv.h"
Inscape_ao 14:89665bcef3f0 3
Inscape_ao 14:89665bcef3f0 4 /* --- Definitions --- */
Inscape_ao 14:89665bcef3f0 5 #define MAX_SEND_SIZE (128)
Inscape_ao 14:89665bcef3f0 6
Inscape_ao 14:89665bcef3f0 7 /* AXIS */
Inscape_ao 14:89665bcef3f0 8 #define AXIS_X (0)
Inscape_ao 14:89665bcef3f0 9 #define AXIS_Y (1)
Inscape_ao 14:89665bcef3f0 10 #define AXIS_Z (2)
Inscape_ao 14:89665bcef3f0 11 #define NUM_OF_AXIS (3)
Inscape_ao 14:89665bcef3f0 12
Inscape_ao 14:89665bcef3f0 13 /* REGISTERS */
Inscape_ao 14:89665bcef3f0 14 #define ADXL372_REG_DEVID_AD (0x00)
Inscape_ao 14:89665bcef3f0 15 #define ADXL372_REG_DEVID_MST (0x01)
Inscape_ao 14:89665bcef3f0 16 #define ADXL372_REG_PARTID (0x02)
Inscape_ao 14:89665bcef3f0 17 #define ADXL372_REG_REVID (0x03)
Inscape_ao 14:89665bcef3f0 18
Inscape_ao 14:89665bcef3f0 19 #define ADXL372_REG_XDATA_H (0x08)
Inscape_ao 14:89665bcef3f0 20 #define ADXL372_REG_XDATA_L (0x09)
Inscape_ao 14:89665bcef3f0 21 #define ADXL372_REG_YDATA_H (0x0A)
Inscape_ao 14:89665bcef3f0 22 #define ADXL372_REG_YDATA_L (0x0B)
Inscape_ao 14:89665bcef3f0 23 #define ADXL372_REG_ZDATA_H (0x0C)
Inscape_ao 14:89665bcef3f0 24 #define ADXL372_REG_ZDATA_L (0x0D)
Inscape_ao 14:89665bcef3f0 25
Inscape_ao 14:89665bcef3f0 26
Inscape_ao 14:89665bcef3f0 27 #define ADXL372_REG_FIFO_CTL (0x3A)
Inscape_ao 14:89665bcef3f0 28 #define ADXL372_REG_MESURE (0x3E)
Inscape_ao 14:89665bcef3f0 29 #define ADXL372_REG_POWER_CTL (0x3F)
Inscape_ao 14:89665bcef3f0 30
Inscape_ao 14:89665bcef3f0 31 /* --- Static Variables --- */
Inscape_ao 14:89665bcef3f0 32 static int dummyreg = 0;
Inscape_ao 14:89665bcef3f0 33 static int devSpiId = 0;
Inscape_ao 14:89665bcef3f0 34 static int senseUid = 0;
Inscape_ao 14:89665bcef3f0 35 static float a[3] = {0.0};
Inscape_ao 14:89665bcef3f0 36
Inscape_ao 14:89665bcef3f0 37 static Ticker mesureTick;
Inscape_ao 14:89665bcef3f0 38
Inscape_ao 14:89665bcef3f0 39 /* --- Prototypes --- */
Inscape_ao 14:89665bcef3f0 40 static void tickHanlderForSensorRead(void);
Inscape_ao 14:89665bcef3f0 41
Inscape_ao 14:89665bcef3f0 42 /* DeviceDriverBody*/
Inscape_ao 14:89665bcef3f0 43 static void initConnectionToAdxl372(void);
Inscape_ao 14:89665bcef3f0 44 static void updateDeviceInfoAdxl372(void);
Inscape_ao 14:89665bcef3f0 45 static void startMesumentAdxl372(void);
Inscape_ao 14:89665bcef3f0 46 static void stopMesumentAdxl372(void);
Inscape_ao 14:89665bcef3f0 47 static int readAccelAdxl372(int selAxis);
Inscape_ao 14:89665bcef3f0 48 static void spiWriteAdxl372(int reg, int val);
Inscape_ao 14:89665bcef3f0 49 static int spiReadAdxl372(int reg);
Inscape_ao 14:89665bcef3f0 50 static int spiReadDualAdxl372(int reg);
Inscape_ao 14:89665bcef3f0 51
Inscape_ao 14:89665bcef3f0 52 extern Serial pc;
Inscape_ao 14:89665bcef3f0 53
Inscape_ao 14:89665bcef3f0 54 static SPI myspi(SPI_MOSI, SPI_MISO, SPI_SCK); /* D13, D12, D11 */
Inscape_ao 14:89665bcef3f0 55 static DigitalOut mycs(SPI_CS); /* D10 */
Inscape_ao 14:89665bcef3f0 56 static DigitalIn int1(D7); /* D7/D6 */
Inscape_ao 14:89665bcef3f0 57 static DigitalIn int2(D5); /* D5/D4 */
Inscape_ao 14:89665bcef3f0 58
Inscape_ao 14:89665bcef3f0 59 #define SPI_RD (1)
Inscape_ao 14:89665bcef3f0 60 #define SPI_WR (0)
Inscape_ao 14:89665bcef3f0 61 #define SPI_EMPTY_SEND (0x00)
Inscape_ao 14:89665bcef3f0 62
Inscape_ao 14:89665bcef3f0 63
Inscape_ao 14:89665bcef3f0 64 /***************************************************************/
Inscape_ao 14:89665bcef3f0 65 /* Device Driver Interface */
Inscape_ao 14:89665bcef3f0 66 /***************************************************************/
Inscape_ao 14:89665bcef3f0 67 /** This handler is called
Inscape_ao 14:89665bcef3f0 68 * in SRS command */
Inscape_ao 15:35f01ee28b44 69 static bool adxl372_simple_reset(void)
Inscape_ao 14:89665bcef3f0 70 {
Inscape_ao 14:89665bcef3f0 71 /* TODO */
Inscape_ao 15:35f01ee28b44 72 dbgprintf("debug:adxl372_simple_reset\n");
Inscape_ao 14:89665bcef3f0 73 mesureTick.detach();
Inscape_ao 14:89665bcef3f0 74 return true;
Inscape_ao 14:89665bcef3f0 75 }
Inscape_ao 14:89665bcef3f0 76
Inscape_ao 14:89665bcef3f0 77 /** This handler is called
Inscape_ao 14:89665bcef3f0 78 * in the first time of mesuments */
Inscape_ao 15:35f01ee28b44 79 static int adxl372_simple_init(void)
Inscape_ao 14:89665bcef3f0 80 {
Inscape_ao 14:89665bcef3f0 81 /* TODO */
Inscape_ao 15:35f01ee28b44 82 dbgprintf("debug:adxl372_simple_init\n");
Inscape_ao 14:89665bcef3f0 83 mesureTick.detach();
Inscape_ao 14:89665bcef3f0 84
Inscape_ao 14:89665bcef3f0 85 initConnectionToAdxl372();
Inscape_ao 14:89665bcef3f0 86
Inscape_ao 14:89665bcef3f0 87 return 0;
Inscape_ao 14:89665bcef3f0 88 }
Inscape_ao 14:89665bcef3f0 89
Inscape_ao 14:89665bcef3f0 90 /** This handler is called
Inscape_ao 14:89665bcef3f0 91 * in the repling mesurement data to Host (ASAP) */
Inscape_ao 15:35f01ee28b44 92 static bool adxl372_simple_exec(int deviceID, Serial *pOut, FILE *pSave)
Inscape_ao 14:89665bcef3f0 93 {
Inscape_ao 14:89665bcef3f0 94 char sendBuf[MAX_SEND_SIZE] = {0};
Inscape_ao 14:89665bcef3f0 95 int sendCharLen;
Inscape_ao 14:89665bcef3f0 96
Inscape_ao 15:35f01ee28b44 97 dbgprintf("debug:adxl372_simple_exec\n");
Inscape_ao 14:89665bcef3f0 98
Inscape_ao 14:89665bcef3f0 99 updateDeviceInfoAdxl372();
Inscape_ao 14:89665bcef3f0 100
Inscape_ao 14:89665bcef3f0 101 a[AXIS_X] = ((double)readAccelAdxl372(AXIS_X)) * 9.8 * 0.1; /* only use 8bit/MSB in 12bit */
Inscape_ao 14:89665bcef3f0 102 a[AXIS_Y] = ((double)readAccelAdxl372(AXIS_Y)) * 9.8 * 0.1; /* only use 8bit/MSB in 12bit */
Inscape_ao 14:89665bcef3f0 103 a[AXIS_Z] = ((double)readAccelAdxl372(AXIS_Z)) * 9.8 * 0.1; /* only use 8bit/MSB in 12bit */
Inscape_ao 14:89665bcef3f0 104
Inscape_ao 14:89665bcef3f0 105 /* TODO */
Inscape_ao 14:89665bcef3f0 106 uprintf(":%d XDS ", deviceID);
Inscape_ao 14:89665bcef3f0 107 uprintf("0029 REG=%04x,AXIS=%d,ID=%08x\n",
Inscape_ao 14:89665bcef3f0 108 dummyreg, NUM_OF_AXIS, devSpiId);
Inscape_ao 14:89665bcef3f0 109 uprintf(":%d XFD ", deviceID);
Inscape_ao 14:89665bcef3f0 110 sprintf(sendBuf, "%4.4f %4.4f %4.4f", a[AXIS_X], a[AXIS_Y], a[AXIS_Z]);
Inscape_ao 14:89665bcef3f0 111 sendCharLen = strlen(sendBuf);
Inscape_ao 14:89665bcef3f0 112 uprintf("%04d %s\n", sendCharLen, sendBuf);
Inscape_ao 14:89665bcef3f0 113
Inscape_ao 14:89665bcef3f0 114 if (pSave != NULL) {
Inscape_ao 15:35f01ee28b44 115 dbgprintf("debug:adxl372_simple_exec [Write SD card]\n");
Inscape_ao 14:89665bcef3f0 116 /* TODO Sd Card save */
Inscape_ao 14:89665bcef3f0 117 fprintf(pSave, "Device:%08d,%d,%f,%f,%f\n",
Inscape_ao 14:89665bcef3f0 118 devSpiId, senseUid, a[AXIS_X], a[AXIS_Y], a[AXIS_Z]);
Inscape_ao 14:89665bcef3f0 119 }
Inscape_ao 14:89665bcef3f0 120
Inscape_ao 14:89665bcef3f0 121 return true;
Inscape_ao 14:89665bcef3f0 122 }
Inscape_ao 14:89665bcef3f0 123
Inscape_ao 14:89665bcef3f0 124 /** This handler is called
Inscape_ao 14:89665bcef3f0 125 * in CRS command */
Inscape_ao 15:35f01ee28b44 126 static bool adxl372_simple_ready2run(void)
Inscape_ao 14:89665bcef3f0 127 {
Inscape_ao 14:89665bcef3f0 128 /* TODO */
Inscape_ao 15:35f01ee28b44 129 dbgprintf("debug:adxl372_simple_ready2run\n");
Inscape_ao 14:89665bcef3f0 130 updateDeviceInfoAdxl372();
Inscape_ao 14:89665bcef3f0 131 startMesumentAdxl372();
Inscape_ao 14:89665bcef3f0 132 mesureTick.attach(tickHanlderForSensorRead, 0.5);
Inscape_ao 14:89665bcef3f0 133 return true;
Inscape_ao 14:89665bcef3f0 134 }
Inscape_ao 14:89665bcef3f0 135
Inscape_ao 14:89665bcef3f0 136 /** This handler is called
Inscape_ao 14:89665bcef3f0 137 * in End of Mesument */
Inscape_ao 15:35f01ee28b44 138 static bool adxl372_simple_run2ready(void)
Inscape_ao 14:89665bcef3f0 139 {
Inscape_ao 14:89665bcef3f0 140 /* TODO */
Inscape_ao 15:35f01ee28b44 141 dbgprintf("debug:adxl372_simple_ready2run\n");
Inscape_ao 14:89665bcef3f0 142 stopMesumentAdxl372();
Inscape_ao 14:89665bcef3f0 143 mesureTick.detach();
Inscape_ao 14:89665bcef3f0 144 return true;
Inscape_ao 14:89665bcef3f0 145 }
Inscape_ao 14:89665bcef3f0 146
Inscape_ao 14:89665bcef3f0 147 /** This handler is called
Inscape_ao 14:89665bcef3f0 148 * in CFW after CID */
Inscape_ao 15:35f01ee28b44 149 static bool adxl372_simple_get_config(int id_pos, int*get)
Inscape_ao 14:89665bcef3f0 150 {
Inscape_ao 14:89665bcef3f0 151 /* TODO */
Inscape_ao 15:35f01ee28b44 152 dbgprintf("debug:adxl372_simple_get_config(ID=%x)\n", id_pos);
Inscape_ao 14:89665bcef3f0 153 *get = dummyreg;
Inscape_ao 14:89665bcef3f0 154 return true;
Inscape_ao 14:89665bcef3f0 155 }
Inscape_ao 14:89665bcef3f0 156
Inscape_ao 14:89665bcef3f0 157 /** This handler is called
Inscape_ao 14:89665bcef3f0 158 * in CFW after CID */
Inscape_ao 15:35f01ee28b44 159 static bool adxl372_simple_set_config(int id_pos, int set)
Inscape_ao 14:89665bcef3f0 160 {
Inscape_ao 14:89665bcef3f0 161 /* TODO */
Inscape_ao 15:35f01ee28b44 162 dbgprintf("debug:adxl372_simple_set_config(ID=%x)\n", id_pos);
Inscape_ao 14:89665bcef3f0 163 dummyreg = set;
Inscape_ao 14:89665bcef3f0 164 return true;
Inscape_ao 14:89665bcef3f0 165 }
Inscape_ao 14:89665bcef3f0 166
Inscape_ao 14:89665bcef3f0 167 /****************************************************/
Inscape_ao 14:89665bcef3f0 168 /* for Mesurement */
Inscape_ao 14:89665bcef3f0 169 /****************************************************/
Inscape_ao 14:89665bcef3f0 170 static void tickHanlderForSensorRead(void)
Inscape_ao 14:89665bcef3f0 171 {
Inscape_ao 14:89665bcef3f0 172 /* TODO */
Inscape_ao 14:89665bcef3f0 173 senseUid++;
Inscape_ao 14:89665bcef3f0 174 return;
Inscape_ao 14:89665bcef3f0 175 }
Inscape_ao 14:89665bcef3f0 176
Inscape_ao 14:89665bcef3f0 177 /***************************************************************/
Inscape_ao 14:89665bcef3f0 178 /* Device Driver Body */
Inscape_ao 14:89665bcef3f0 179 /***************************************************************/
Inscape_ao 14:89665bcef3f0 180 static void initConnectionToAdxl372(void)
Inscape_ao 14:89665bcef3f0 181 {
Inscape_ao 14:89665bcef3f0 182 /** Init SPI for ADXL372
Inscape_ao 14:89665bcef3f0 183 * CPHA = CPOL = 0, 1MHz(max.10MHz))
Inscape_ao 14:89665bcef3f0 184 * CS = inactive
Inscape_ao 14:89665bcef3f0 185 */
Inscape_ao 14:89665bcef3f0 186 myspi.format(8,0);
Inscape_ao 14:89665bcef3f0 187 myspi.frequency(1000000);
Inscape_ao 14:89665bcef3f0 188 mycs = 1;
Inscape_ao 14:89665bcef3f0 189 }
Inscape_ao 14:89665bcef3f0 190
Inscape_ao 14:89665bcef3f0 191 static void startMesumentAdxl372(void)
Inscape_ao 14:89665bcef3f0 192 {
Inscape_ao 14:89665bcef3f0 193 spiWriteAdxl372(ADXL372_REG_FIFO_CTL, 0); /* all data will be bipassed FIFO. */
Inscape_ao 14:89665bcef3f0 194 spiWriteAdxl372(ADXL372_REG_POWER_CTL, 1); /* WAKEUP mode */
Inscape_ao 14:89665bcef3f0 195 }
Inscape_ao 14:89665bcef3f0 196
Inscape_ao 14:89665bcef3f0 197 static void stopMesumentAdxl372(void)
Inscape_ao 14:89665bcef3f0 198 {
Inscape_ao 14:89665bcef3f0 199 spiWriteAdxl372(ADXL372_REG_FIFO_CTL, 0); /* all data will be bipassed FIFO. */
Inscape_ao 14:89665bcef3f0 200 spiWriteAdxl372(ADXL372_REG_POWER_CTL, 0); /* STANDBY mode */
Inscape_ao 14:89665bcef3f0 201 }
Inscape_ao 14:89665bcef3f0 202
Inscape_ao 14:89665bcef3f0 203 static int readAccelAdxl372(int selAxis)
Inscape_ao 14:89665bcef3f0 204 {
Inscape_ao 14:89665bcef3f0 205 int ret;
Inscape_ao 14:89665bcef3f0 206 switch(selAxis) {
Inscape_ao 14:89665bcef3f0 207 case AXIS_X:
Inscape_ao 14:89665bcef3f0 208 ret = spiReadDualAdxl372(ADXL372_REG_XDATA_H);
Inscape_ao 14:89665bcef3f0 209 break;
Inscape_ao 14:89665bcef3f0 210 case AXIS_Y:
Inscape_ao 14:89665bcef3f0 211 ret = spiReadDualAdxl372(ADXL372_REG_YDATA_H);
Inscape_ao 14:89665bcef3f0 212 break;
Inscape_ao 14:89665bcef3f0 213 case AXIS_Z:
Inscape_ao 14:89665bcef3f0 214 ret = spiReadDualAdxl372(ADXL372_REG_ZDATA_H);
Inscape_ao 14:89665bcef3f0 215 break;
Inscape_ao 14:89665bcef3f0 216 default:
Inscape_ao 14:89665bcef3f0 217 ret = 0xFFFFFFFF;
Inscape_ao 14:89665bcef3f0 218 }
Inscape_ao 14:89665bcef3f0 219 ret = ret >> 4;
Inscape_ao 14:89665bcef3f0 220 if ((ret & 0x00000800) != 0) {
Inscape_ao 14:89665bcef3f0 221 ret |= 0xFFFFF800;
Inscape_ao 14:89665bcef3f0 222 }
Inscape_ao 14:89665bcef3f0 223 dbgprintf("debug:readAccelAdxl372(AXIS=%d)=0x%08x\n", selAxis, ret);
Inscape_ao 14:89665bcef3f0 224 return ret;
Inscape_ao 14:89665bcef3f0 225 }
Inscape_ao 14:89665bcef3f0 226
Inscape_ao 14:89665bcef3f0 227 static void updateDeviceInfoAdxl372(void)
Inscape_ao 14:89665bcef3f0 228 {
Inscape_ao 14:89665bcef3f0 229
Inscape_ao 14:89665bcef3f0 230 int devid_ad;
Inscape_ao 14:89665bcef3f0 231 int devid_mst;
Inscape_ao 14:89665bcef3f0 232 int devid_part;
Inscape_ao 14:89665bcef3f0 233 int devid_rev;
Inscape_ao 14:89665bcef3f0 234
Inscape_ao 14:89665bcef3f0 235 devid_ad = spiReadAdxl372(ADXL372_REG_DEVID_AD);
Inscape_ao 14:89665bcef3f0 236 devid_mst = spiReadAdxl372(ADXL372_REG_DEVID_MST);
Inscape_ao 14:89665bcef3f0 237 devid_part = spiReadAdxl372(ADXL372_REG_PARTID);
Inscape_ao 14:89665bcef3f0 238 devid_rev = spiReadAdxl372(ADXL372_REG_REVID);
Inscape_ao 14:89665bcef3f0 239 devSpiId = (devid_ad << 0)
Inscape_ao 14:89665bcef3f0 240 | (devid_mst << 8)
Inscape_ao 14:89665bcef3f0 241 | (devid_part << 16)
Inscape_ao 14:89665bcef3f0 242 | (devid_rev << 24);
Inscape_ao 14:89665bcef3f0 243 }
Inscape_ao 14:89665bcef3f0 244
Inscape_ao 14:89665bcef3f0 245 static int spiReadAdxl372(int reg)
Inscape_ao 14:89665bcef3f0 246 {
Inscape_ao 14:89665bcef3f0 247 int val;
Inscape_ao 14:89665bcef3f0 248 mycs = 0;
Inscape_ao 14:89665bcef3f0 249 myspi.write(reg << 1 | SPI_RD);
Inscape_ao 14:89665bcef3f0 250 val = myspi.write(SPI_EMPTY_SEND);
Inscape_ao 14:89665bcef3f0 251 mycs = 1;
Inscape_ao 14:89665bcef3f0 252 dbgprintf("debug:spiReadAdxl372(reg=0x%08x)=0x%08x\n", reg, val);
Inscape_ao 14:89665bcef3f0 253 return val;
Inscape_ao 14:89665bcef3f0 254 }
Inscape_ao 14:89665bcef3f0 255
Inscape_ao 14:89665bcef3f0 256 static int spiReadDualAdxl372(int reg)
Inscape_ao 14:89665bcef3f0 257 {
Inscape_ao 14:89665bcef3f0 258 int valh, vall, val;
Inscape_ao 14:89665bcef3f0 259 mycs = 0;
Inscape_ao 14:89665bcef3f0 260 myspi.write(reg << 1 | SPI_RD);
Inscape_ao 14:89665bcef3f0 261 valh = myspi.write(SPI_EMPTY_SEND);
Inscape_ao 14:89665bcef3f0 262 vall = myspi.write(SPI_EMPTY_SEND);
Inscape_ao 14:89665bcef3f0 263 mycs = 1;
Inscape_ao 14:89665bcef3f0 264 val = valh << 8 | vall;
Inscape_ao 14:89665bcef3f0 265 dbgprintf("debug:spiReadDualAdxl372(reg=0x%08x)=0x%08x\n", reg, val);
Inscape_ao 14:89665bcef3f0 266 return val;
Inscape_ao 14:89665bcef3f0 267 }
Inscape_ao 14:89665bcef3f0 268
Inscape_ao 14:89665bcef3f0 269 static void spiWriteAdxl372(int reg, int val)
Inscape_ao 14:89665bcef3f0 270 {
Inscape_ao 14:89665bcef3f0 271 mycs = 0;
Inscape_ao 14:89665bcef3f0 272 myspi.write(reg << 1 | SPI_WR);
Inscape_ao 14:89665bcef3f0 273 myspi.write(val);
Inscape_ao 14:89665bcef3f0 274 mycs = 1;
Inscape_ao 14:89665bcef3f0 275 dbgprintf("debug:spiWriteAdxl372(reg=0x%08x)=0x%08x\n", reg, val);
Inscape_ao 14:89665bcef3f0 276 }
Inscape_ao 14:89665bcef3f0 277
Inscape_ao 14:89665bcef3f0 278 /****************************************************/
Inscape_ao 14:89665bcef3f0 279 /* for Registration of Device Driver */
Inscape_ao 14:89665bcef3f0 280 /****************************************************/
Inscape_ao 15:35f01ee28b44 281 DeviceDriver Adxl372Simple = {
Inscape_ao 15:35f01ee28b44 282 .init = adxl372_simple_init,
Inscape_ao 15:35f01ee28b44 283 .set_config = adxl372_simple_set_config,
Inscape_ao 15:35f01ee28b44 284 .get_config = adxl372_simple_get_config,
Inscape_ao 15:35f01ee28b44 285 .reset = adxl372_simple_reset,
Inscape_ao 15:35f01ee28b44 286 .exec = adxl372_simple_exec,
Inscape_ao 15:35f01ee28b44 287 .ready2run = adxl372_simple_ready2run,
Inscape_ao 15:35f01ee28b44 288 .run2ready = adxl372_simple_run2ready,
Inscape_ao 14:89665bcef3f0 289 };