ADXL355

Committer:
ppcanuck2
Date:
Thu Feb 06 19:25:43 2020 +0000
Revision:
0:df16df7ba53f
Child:
1:c7e291816833
Data dumps to UART but really slowly

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ppcanuck2 0:df16df7ba53f 1 /* Copyright (c) 2019 Analog Devices, Inc. All rights reserved.
ppcanuck2 0:df16df7ba53f 2
ppcanuck2 0:df16df7ba53f 3 Redistribution and use in source and binary forms, with or without modification,
ppcanuck2 0:df16df7ba53f 4 are permitted provided that the following conditions are met:
ppcanuck2 0:df16df7ba53f 5 - Redistributions of source code must retain the above copyright notice,
ppcanuck2 0:df16df7ba53f 6 this list of conditions and the following disclaimer.
ppcanuck2 0:df16df7ba53f 7 - Redistributions in binary form must reproduce the above copyright notice,
ppcanuck2 0:df16df7ba53f 8 this list of conditions and the following disclaimer in the documentation
ppcanuck2 0:df16df7ba53f 9 and/or other materials provided with the distribution.
ppcanuck2 0:df16df7ba53f 10 - Modified versions of the software must be conspicuously marked as such.
ppcanuck2 0:df16df7ba53f 11 - This software is licensed solely and exclusively for use with processors/products
ppcanuck2 0:df16df7ba53f 12 manufactured by or for Analog Devices, Inc.
ppcanuck2 0:df16df7ba53f 13 - This software may not be combined or merged with other code in any manner
ppcanuck2 0:df16df7ba53f 14 that would cause the software to become subject to terms and conditions which
ppcanuck2 0:df16df7ba53f 15 differ from those listed here.
ppcanuck2 0:df16df7ba53f 16 - Neither the name of Analog Devices, Inc. nor the names of its contributors
ppcanuck2 0:df16df7ba53f 17 may be used to endorse or promote products derived from this software without
ppcanuck2 0:df16df7ba53f 18 specific prior written permission.
ppcanuck2 0:df16df7ba53f 19 - The use of this software may or may not infringe the patent rights of one or
ppcanuck2 0:df16df7ba53f 20 more patent holders. This license does not release you from the requirement
ppcanuck2 0:df16df7ba53f 21 that you obtain separate licenses from these patent holders to use this software.
ppcanuck2 0:df16df7ba53f 22
ppcanuck2 0:df16df7ba53f 23 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" AND
ppcanuck2 0:df16df7ba53f 24 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
ppcanuck2 0:df16df7ba53f 25 TITLE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
ppcanuck2 0:df16df7ba53f 26 NO EVENT SHALL ANALOG DEVICES, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
ppcanuck2 0:df16df7ba53f 27 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES
ppcanuck2 0:df16df7ba53f 28 (INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF INTELLECTUAL
ppcanuck2 0:df16df7ba53f 29 PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
ppcanuck2 0:df16df7ba53f 30 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
ppcanuck2 0:df16df7ba53f 31 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
ppcanuck2 0:df16df7ba53f 32 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
ppcanuck2 0:df16df7ba53f 33 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ppcanuck2 0:df16df7ba53f 34
ppcanuck2 0:df16df7ba53f 35 2019-01-10-7CBSD SLA
ppcanuck2 0:df16df7ba53f 36
ppcanuck2 0:df16df7ba53f 37 */
ppcanuck2 0:df16df7ba53f 38
ppcanuck2 0:df16df7ba53f 39 #include "mbed.h"
ppcanuck2 0:df16df7ba53f 40 #include "platform/mbed_thread.h"
ppcanuck2 0:df16df7ba53f 41 #include "ADXL355.h"
ppcanuck2 0:df16df7ba53f 42
ppcanuck2 0:df16df7ba53f 43 /* Global Data */
ppcanuck2 0:df16df7ba53f 44
ppcanuck2 0:df16df7ba53f 45 float LSB_scale_factor=0.0000039f;
ppcanuck2 0:df16df7ba53f 46
ppcanuck2 0:df16df7ba53f 47 int32_t volatile i32SensorX;
ppcanuck2 0:df16df7ba53f 48 int32_t volatile i32SensorY;
ppcanuck2 0:df16df7ba53f 49 int32_t volatile i32SensorZ;
ppcanuck2 0:df16df7ba53f 50 int32_t volatile i32SensorT;
ppcanuck2 0:df16df7ba53f 51
ppcanuck2 0:df16df7ba53f 52 uint8_t volatile ui8SensorX;
ppcanuck2 0:df16df7ba53f 53 uint8_t volatile ui8SensorX2;
ppcanuck2 0:df16df7ba53f 54 uint8_t volatile ui8SensorX3;
ppcanuck2 0:df16df7ba53f 55 uint32_t volatile ui32SensorX;
ppcanuck2 0:df16df7ba53f 56
ppcanuck2 0:df16df7ba53f 57 uint8_t volatile ui8SensorY;
ppcanuck2 0:df16df7ba53f 58 uint8_t volatile ui8SensorY2;
ppcanuck2 0:df16df7ba53f 59 uint8_t volatile ui8SensorY3;
ppcanuck2 0:df16df7ba53f 60 uint32_t volatile ui32SensorY;
ppcanuck2 0:df16df7ba53f 61
ppcanuck2 0:df16df7ba53f 62 uint8_t volatile ui8SensorZ;
ppcanuck2 0:df16df7ba53f 63 uint8_t volatile ui8SensorZ2;
ppcanuck2 0:df16df7ba53f 64 uint8_t volatile ui8SensorZ3;
ppcanuck2 0:df16df7ba53f 65 uint32_t volatile ui32SensorZ;
ppcanuck2 0:df16df7ba53f 66
ppcanuck2 0:df16df7ba53f 67
ppcanuck2 0:df16df7ba53f 68 uint8_t volatile ui8SensorT;
ppcanuck2 0:df16df7ba53f 69 uint8_t volatile ui8SensorT2;
ppcanuck2 0:df16df7ba53f 70 uint32_t volatile ui32SensorT;
ppcanuck2 0:df16df7ba53f 71
ppcanuck2 0:df16df7ba53f 72 uint32_t volatile ui32Result = 0;
ppcanuck2 0:df16df7ba53f 73
ppcanuck2 0:df16df7ba53f 74 // Blinking rate in milliseconds
ppcanuck2 0:df16df7ba53f 75 #define SLEEP_TIME 500
ppcanuck2 0:df16df7ba53f 76 // Setup the SPI peripheral to talk to the ADXL35x
ppcanuck2 0:df16df7ba53f 77 SPI spi(D11, D12, D13);
ppcanuck2 0:df16df7ba53f 78 // initialize chip select
ppcanuck2 0:df16df7ba53f 79 DigitalOut cs(D10);
ppcanuck2 0:df16df7ba53f 80
ppcanuck2 0:df16df7ba53f 81 // Initialise the digital pin LED1 as an output
ppcanuck2 0:df16df7ba53f 82 DigitalOut led1(LED1);
ppcanuck2 0:df16df7ba53f 83 // Initialise the serial object with TX and RX pins
ppcanuck2 0:df16df7ba53f 84 Serial pc(USBTX, USBRX);
ppcanuck2 0:df16df7ba53f 85 int id=0;
ppcanuck2 0:df16df7ba53f 86
ppcanuck2 0:df16df7ba53f 87
ppcanuck2 0:df16df7ba53f 88 // main() runs in its own thread in the OS
ppcanuck2 0:df16df7ba53f 89 int main()
ppcanuck2 0:df16df7ba53f 90 {
ppcanuck2 0:df16df7ba53f 91 pc.printf("Hello World!");
ppcanuck2 0:df16df7ba53f 92
ppcanuck2 0:df16df7ba53f 93 spi.frequency(1000000);
ppcanuck2 0:df16df7ba53f 94
ppcanuck2 0:df16df7ba53f 95 cs=0;
ppcanuck2 0:df16df7ba53f 96 spi.write(DEVID_AD<<1|0x01);
ppcanuck2 0:df16df7ba53f 97 id=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 98 cs=1;
ppcanuck2 0:df16df7ba53f 99 pc.printf("ID register = 0x%X\n", id);
ppcanuck2 0:df16df7ba53f 100 // This should output the ID register as 0xAD
ppcanuck2 0:df16df7ba53f 101
ppcanuck2 0:df16df7ba53f 102 cs=0;
ppcanuck2 0:df16df7ba53f 103 spi.write(DEVID_MST<<1|0x01);
ppcanuck2 0:df16df7ba53f 104 id=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 105 cs=1;
ppcanuck2 0:df16df7ba53f 106 pc.printf("DEVID register = 0x%X\n", id);
ppcanuck2 0:df16df7ba53f 107 // This should output the ID register as 0x1D
ppcanuck2 0:df16df7ba53f 108
ppcanuck2 0:df16df7ba53f 109 cs=0;
ppcanuck2 0:df16df7ba53f 110 spi.write(PARTID<<1|0x01);
ppcanuck2 0:df16df7ba53f 111 id=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 112 cs=1;
ppcanuck2 0:df16df7ba53f 113 pc.printf("PARTID register = 0x%X\n", id);
ppcanuck2 0:df16df7ba53f 114 // This should output the ID register as 0xED
ppcanuck2 0:df16df7ba53f 115
ppcanuck2 0:df16df7ba53f 116 cs=0;
ppcanuck2 0:df16df7ba53f 117 spi.write(REVID<<1|0x01);
ppcanuck2 0:df16df7ba53f 118 id=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 119 cs=1;
ppcanuck2 0:df16df7ba53f 120 pc.printf("REVID register = 0x%X\n", id);
ppcanuck2 0:df16df7ba53f 121 // This should output the ID register as 0x01
ppcanuck2 0:df16df7ba53f 122
ppcanuck2 0:df16df7ba53f 123 // Turn on the sensor
ppcanuck2 0:df16df7ba53f 124 ADXL355_Start_Sensor();
ppcanuck2 0:df16df7ba53f 125
ppcanuck2 0:df16df7ba53f 126 // Now dump the temp and accelerometer data to the port
ppcanuck2 0:df16df7ba53f 127
ppcanuck2 0:df16df7ba53f 128 while (true) {
ppcanuck2 0:df16df7ba53f 129 // Blink LED and wait 500 ms
ppcanuck2 0:df16df7ba53f 130 led1 = !led1;
ppcanuck2 0:df16df7ba53f 131 thread_sleep_for(SLEEP_TIME);
ppcanuck2 0:df16df7ba53f 132
ppcanuck2 0:df16df7ba53f 133 //the acceleration data is 20 bits and needs to come in via three register transactions
ppcanuck2 0:df16df7ba53f 134 cs=0;
ppcanuck2 0:df16df7ba53f 135 spi.write(XDATA3<<1|0x01);
ppcanuck2 0:df16df7ba53f 136 ui8SensorX=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 137 ui8SensorX2=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 138 ui8SensorX3=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 139 cs=1;
ppcanuck2 0:df16df7ba53f 140 // Put it back together
ppcanuck2 0:df16df7ba53f 141
ppcanuck2 0:df16df7ba53f 142 ui32Result = ((ui8SensorX << 16) | (ui8SensorX2 << 8) | ui8SensorX3); /* Set read result*/
ppcanuck2 0:df16df7ba53f 143 //pc.printf("X acceleration data = 0x%X\n", ui32Result);
ppcanuck2 0:df16df7ba53f 144 // This should output the ID register as 0x01
ppcanuck2 0:df16df7ba53f 145 i32SensorX = ADXL355_Acceleration_Data_Conversion(ui32Result);
ppcanuck2 0:df16df7ba53f 146 //pc.printf("X acceleration formatted data = %d\n", i32SensorX);
ppcanuck2 0:df16df7ba53f 147 pc.printf("X = %2.6f g, ", i32SensorX*LSB_scale_factor);
ppcanuck2 0:df16df7ba53f 148
ppcanuck2 0:df16df7ba53f 149 //the acceleration data is 20 bits and needs to come in via three register transactions
ppcanuck2 0:df16df7ba53f 150 cs=0;
ppcanuck2 0:df16df7ba53f 151 spi.write(YDATA3<<1|0x01);
ppcanuck2 0:df16df7ba53f 152 ui8SensorY=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 153 ui8SensorY2=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 154 ui8SensorY3=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 155 cs=1;
ppcanuck2 0:df16df7ba53f 156 // Put it back together
ppcanuck2 0:df16df7ba53f 157
ppcanuck2 0:df16df7ba53f 158 ui32Result = ((ui8SensorY << 16) | (ui8SensorY2 << 8) | ui8SensorY3); /* Set read result*/
ppcanuck2 0:df16df7ba53f 159 //pc.printf("Y acceleration data = 0x%X\n", ui32Result);
ppcanuck2 0:df16df7ba53f 160 // This should output the ID register as 0x01
ppcanuck2 0:df16df7ba53f 161 i32SensorY = ADXL355_Acceleration_Data_Conversion(ui32Result);
ppcanuck2 0:df16df7ba53f 162 //pc.printf("Y acceleration formatted data = %d\n", i32SensorY);
ppcanuck2 0:df16df7ba53f 163 pc.printf("Y = %2.6f g, ", i32SensorY*LSB_scale_factor);
ppcanuck2 0:df16df7ba53f 164
ppcanuck2 0:df16df7ba53f 165
ppcanuck2 0:df16df7ba53f 166 //the acceleration data is 20 bits and needs to come in via three register transactions
ppcanuck2 0:df16df7ba53f 167 cs=0;
ppcanuck2 0:df16df7ba53f 168 spi.write(ZDATA3<<1|0x01);
ppcanuck2 0:df16df7ba53f 169 ui8SensorZ=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 170 ui8SensorZ2=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 171 ui8SensorZ3=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 172 cs=1;
ppcanuck2 0:df16df7ba53f 173 // Put it back together
ppcanuck2 0:df16df7ba53f 174
ppcanuck2 0:df16df7ba53f 175 ui32Result = ((ui8SensorZ << 16) | (ui8SensorZ2 << 8) | ui8SensorZ3); /* Set read result*/
ppcanuck2 0:df16df7ba53f 176 //pc.printf("Z acceleration data = 0x%X\n", ui32Result);
ppcanuck2 0:df16df7ba53f 177 // This should output the ID register as 0x01
ppcanuck2 0:df16df7ba53f 178 i32SensorZ = ADXL355_Acceleration_Data_Conversion(ui32Result);
ppcanuck2 0:df16df7ba53f 179 //pc.printf("Z acceleration formatted data = %d \n", i32SensorZ);
ppcanuck2 0:df16df7ba53f 180 pc.printf("Z = %2.6f g", i32SensorZ*LSB_scale_factor);
ppcanuck2 0:df16df7ba53f 181
ppcanuck2 0:df16df7ba53f 182 pc.printf("\n");
ppcanuck2 0:df16df7ba53f 183 }
ppcanuck2 0:df16df7ba53f 184 }
ppcanuck2 0:df16df7ba53f 185 int32_t ADXL355_Acceleration_Data_Conversion (uint32_t ui32SensorData)
ppcanuck2 0:df16df7ba53f 186 {
ppcanuck2 0:df16df7ba53f 187 int32_t volatile i32Conversion = 0;
ppcanuck2 0:df16df7ba53f 188
ppcanuck2 0:df16df7ba53f 189 ui32SensorData = (ui32SensorData >> 4);
ppcanuck2 0:df16df7ba53f 190 ui32SensorData = (ui32SensorData & 0x000FFFFF);
ppcanuck2 0:df16df7ba53f 191
ppcanuck2 0:df16df7ba53f 192 if((ui32SensorData & 0x00080000) == 0x00080000){
ppcanuck2 0:df16df7ba53f 193
ppcanuck2 0:df16df7ba53f 194 i32Conversion = (ui32SensorData | 0xFFF00000);
ppcanuck2 0:df16df7ba53f 195
ppcanuck2 0:df16df7ba53f 196 }
ppcanuck2 0:df16df7ba53f 197 else{
ppcanuck2 0:df16df7ba53f 198 i32Conversion = ui32SensorData;
ppcanuck2 0:df16df7ba53f 199 }
ppcanuck2 0:df16df7ba53f 200
ppcanuck2 0:df16df7ba53f 201 return i32Conversion;
ppcanuck2 0:df16df7ba53f 202 }
ppcanuck2 0:df16df7ba53f 203
ppcanuck2 0:df16df7ba53f 204 void ADXL355_Start_Sensor(void)
ppcanuck2 0:df16df7ba53f 205 {
ppcanuck2 0:df16df7ba53f 206 cs=0;
ppcanuck2 0:df16df7ba53f 207 spi.write(POWER_CTL<<1|0x01);
ppcanuck2 0:df16df7ba53f 208 id=spi.write(0xFF);
ppcanuck2 0:df16df7ba53f 209 cs=1;
ppcanuck2 0:df16df7ba53f 210 pc.printf("POWER_CTL register = 0x%X\n", id);
ppcanuck2 0:df16df7ba53f 211
ppcanuck2 0:df16df7ba53f 212 id = id & 0xFE; // Clear measurement bit in POWER_CTL register and turn it on
ppcanuck2 0:df16df7ba53f 213
ppcanuck2 0:df16df7ba53f 214 // Write the power control register, which involves add a '1' to the 8th bit
ppcanuck2 0:df16df7ba53f 215 // address for the ADXL355
ppcanuck2 0:df16df7ba53f 216
ppcanuck2 0:df16df7ba53f 217 int volatile tmp=(POWER_CTL<<1);
ppcanuck2 0:df16df7ba53f 218
ppcanuck2 0:df16df7ba53f 219 cs=0;
ppcanuck2 0:df16df7ba53f 220 spi.write(tmp);
ppcanuck2 0:df16df7ba53f 221 spi.write(id);
ppcanuck2 0:df16df7ba53f 222 cs=1;
ppcanuck2 0:df16df7ba53f 223
ppcanuck2 0:df16df7ba53f 224
ppcanuck2 0:df16df7ba53f 225 //SPI_Write(POWER_CTL, ui8temp, 0x00, SPI_WRITE_ONE_REG); /* Write the new value to POWER_CTL register */
ppcanuck2 0:df16df7ba53f 226 }
ppcanuck2 0:df16df7ba53f 227
ppcanuck2 0:df16df7ba53f 228