ADXL355

Files at this revision

API Documentation at this revision

Comitter:
ppcanuck2
Date:
Thu Feb 06 19:25:43 2020 +0000
Child:
1:c7e291816833
Commit message:
Data dumps to UART but really slowly

Changed in this revision

ADXL35x/ADXL355.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADXL35x/ADXL355.h	Thu Feb 06 19:25:43 2020 +0000
@@ -0,0 +1,130 @@
+/*!
+ *****************************************************************************
+ * @file:    ADXL355.h
+ * @brief:   ADXL355 accelerometer IC
+ * @version: $Revision$
+ * @date:    $Date$
+ *-----------------------------------------------------------------------------
+ *
+Copyright (c) 2016-2017 Analog Devices, Inc.
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification,
+are permitted provided that the following conditions are met:
+  - Redistributions of source code must retain the above copyright notice,
+    this list of conditions and the following disclaimer.
+  - Redistributions in binary form must reproduce the above copyright notice,
+    this list of conditions and the following disclaimer in the documentation
+    and/or other materials provided with the distribution.
+  - Modified versions of the software must be conspicuously marked as such.
+  - This software is licensed solely and exclusively for use with processors
+    manufactured by or for Analog Devices, Inc.
+  - This software may not be combined or merged with other code in any manner
+    that would cause the software to become subject to terms and conditions
+    which differ from those listed here.
+  - Neither the name of Analog Devices, Inc. nor the names of its
+    contributors may be used to endorse or promote products derived
+    from this software without specific prior written permission.
+  - The use of this software may or may not infringe the patent rights of one
+    or more patent holders.  This license does not release you from the
+    requirement that you obtain separate licenses from these patent holders
+    to use this software.
+
+THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" AND ANY
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
+TITLE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
+NO EVENT SHALL ANALOG DEVICES, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF INTELLECTUAL
+PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ *****************************************************************************/
+
+#ifndef ADXL355_H_
+#define ADXL355_H_
+
+/********************************* Definitions ********************************/
+
+/* ADXL355 registers addresses */
+#define DEVID_AD                 0x00
+#define DEVID_MST                0x01
+#define PARTID                   0x02
+#define REVID                    0x03
+#define STATUS                   0x04
+#define FIFO_ENTRIES             0x05
+#define TEMP2                    0x06
+#define TEMP1                    0x07
+#define XDATA3                   0x08
+#define XDATA2                   0x09
+#define XDATA1                   0x0A
+#define YDATA3                   0x0B
+#define YDATA2                   0x0C
+#define YDATA1                   0x0D
+#define ZDATA3                   0x0E
+#define ZDATA2                   0x0F
+#define ZDATA1                   0x10
+#define FIFO_DATA                0x11
+#define OFFSET_X_H               0x1E
+#define OFFSET_X_L               0x1F
+#define OFFSET_Y_H               0x20
+#define OFFSET_Y_L               0x21
+#define OFFSET_Z_H               0x22
+#define OFFSET_Z_L               0x23
+#define ACT_EN                   0x24
+#define ACT_THRESH_H             0x25
+#define ACT_THRESH_L             0x26
+#define ACT_COUNT                0x27
+#define FILTER                   0x28
+#define FIFO_SAMPLES             0x29
+#define INT_MAP                  0x2A
+#define SYNC                     0x2B
+#define RANGE                    0x2C
+#define POWER_CTL                0x2D
+#define SELF_TEST                0x2E
+#define RESET                    0x2F
+
+/**************************** Configuration parameters **********************/
+
+/* Temperature parameters */
+#define ADXL355_TEMP_BIAS       (float)1852.0      /* Accelerometer temperature bias(in ADC codes) at 25 Deg C */
+#define ADXL355_TEMP_SLOPE      (float)-9.05       /* Accelerometer temperature change from datasheet (LSB/degC) */
+
+/* Accelerometer parameters */
+#define ADXL_RANGE     2     /* ADXL362 sensitivity: 2, 4, 8 [g] */
+
+#define ACT_VALUE          50     /* Activity threshold value */
+
+#define INACT_VALUE        50     /* Inactivity threshold value */
+
+#define ACT_TIMER          100    /* Activity timer value in ms */
+
+#define INACT_TIMER        10     /* Inactivity timer value in seconds */
+
+/****************************** Global Data ***********************************/
+
+extern volatile int32_t i32SensorX;
+extern volatile int32_t i32SensorY;
+extern volatile int32_t i32SensorZ;
+
+extern volatile uint32_t ui32SensorX;
+extern volatile uint32_t ui32SensorY;
+extern volatile uint32_t ui32SensorZ;
+extern volatile uint32_t ui32SensorT;
+
+extern volatile uint32_t ui32timer_counter;
+
+
+/*************************** Functions prototypes *****************************/
+
+void ADXL355_Init(void);
+void ADXL355_Start_Sensor(void);
+void ADXL355_Stop_Sensor(void);
+void ADXL355_Data_Scan(void);
+int32_t ADXL355_Acceleration_Data_Conversion (uint32_t ui32SensorData);
+
+#endif /* ADXL355_H_ */
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Feb 06 19:25:43 2020 +0000
@@ -0,0 +1,228 @@
+/* Copyright (c) 2019 Analog Devices, Inc.  All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, 
+are permitted provided that the following conditions are met:
+  - Redistributions of source code must retain the above copyright notice, 
+  this list of conditions and the following disclaimer.
+  - Redistributions in binary form must reproduce the above copyright notice, 
+  this list of conditions and the following disclaimer in the documentation 
+  and/or other materials provided with the distribution.  
+  - Modified versions of the software must be conspicuously marked as such.
+  - This software is licensed solely and exclusively for use with processors/products 
+  manufactured by or for Analog Devices, Inc.
+  - This software may not be combined or merged with other code in any manner 
+  that would cause the software to become subject to terms and conditions which 
+  differ from those listed here.
+  - Neither the name of Analog Devices, Inc. nor the names of its contributors 
+  may be used to endorse or promote products derived from this software without 
+  specific prior written permission.
+  - The use of this software may or may not infringe the patent rights of one or 
+  more patent holders.  This license does not release you from the requirement 
+  that you obtain separate licenses from these patent holders to use this software.
+
+THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" AND 
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, 
+TITLE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 
+NO EVENT SHALL ANALOG DEVICES, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
+INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES 
+(INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF INTELLECTUAL 
+PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
+NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
+EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+2019-01-10-7CBSD SLA
+
+ */
+
+#include "mbed.h"
+#include "platform/mbed_thread.h"
+#include "ADXL355.h"
+
+/* Global Data */
+
+float LSB_scale_factor=0.0000039f; 
+
+int32_t volatile i32SensorX;
+int32_t volatile i32SensorY;
+int32_t volatile i32SensorZ;
+int32_t volatile i32SensorT;
+
+uint8_t volatile ui8SensorX;
+uint8_t volatile ui8SensorX2;
+uint8_t volatile ui8SensorX3;
+uint32_t volatile ui32SensorX;
+
+uint8_t volatile ui8SensorY;
+uint8_t volatile ui8SensorY2;
+uint8_t volatile ui8SensorY3;
+uint32_t volatile ui32SensorY;
+
+uint8_t volatile ui8SensorZ;
+uint8_t volatile ui8SensorZ2;
+uint8_t volatile ui8SensorZ3;
+uint32_t volatile ui32SensorZ;
+
+
+uint8_t volatile ui8SensorT;
+uint8_t volatile ui8SensorT2;
+uint32_t volatile ui32SensorT;
+
+uint32_t volatile ui32Result = 0;
+
+// Blinking rate in milliseconds
+#define SLEEP_TIME                  500
+// Setup the SPI peripheral to talk to the ADXL35x
+SPI spi(D11, D12, D13);
+// initialize chip select
+DigitalOut cs(D10);
+
+// Initialise the digital pin LED1 as an output
+DigitalOut led1(LED1);
+// Initialise the serial object with TX and RX pins
+Serial pc(USBTX, USBRX);
+int id=0;
+
+
+// main() runs in its own thread in the OS
+int main()
+{
+    pc.printf("Hello World!");
+    
+    spi.frequency(1000000);
+    
+    cs=0;
+    spi.write(DEVID_AD<<1|0x01);
+    id=spi.write(0xFF);
+    cs=1;
+    pc.printf("ID register = 0x%X\n", id);
+    // This should output the ID register as 0xAD
+    
+    cs=0;
+    spi.write(DEVID_MST<<1|0x01);
+    id=spi.write(0xFF);
+    cs=1;
+    pc.printf("DEVID register = 0x%X\n", id);
+    // This should output the ID register as 0x1D
+    
+    cs=0;
+    spi.write(PARTID<<1|0x01);
+    id=spi.write(0xFF);
+    cs=1;
+    pc.printf("PARTID register = 0x%X\n", id);
+    // This should output the ID register as 0xED
+    
+    cs=0;
+    spi.write(REVID<<1|0x01);
+    id=spi.write(0xFF);
+    cs=1;
+    pc.printf("REVID register = 0x%X\n", id);
+    // This should output the ID register as 0x01
+
+    // Turn on the sensor
+    ADXL355_Start_Sensor();
+    
+    // Now dump the temp and accelerometer data to the port
+    
+    while (true) {
+        // Blink LED and wait 500 ms
+        led1 = !led1;
+        thread_sleep_for(SLEEP_TIME);
+        
+        //the acceleration data is 20 bits and needs to come in via three register transactions
+        cs=0;
+        spi.write(XDATA3<<1|0x01);
+        ui8SensorX=spi.write(0xFF); 
+        ui8SensorX2=spi.write(0xFF);
+        ui8SensorX3=spi.write(0xFF);        
+        cs=1;
+        // Put it back together
+        
+        ui32Result = ((ui8SensorX << 16) | (ui8SensorX2 << 8) | ui8SensorX3); /* Set read result*/
+        //pc.printf("X acceleration data = 0x%X\n", ui32Result);
+        // This should output the ID register as 0x01        
+        i32SensorX = ADXL355_Acceleration_Data_Conversion(ui32Result); 
+        //pc.printf("X acceleration formatted data = %d\n", i32SensorX);
+        pc.printf("X = %2.6f g, ", i32SensorX*LSB_scale_factor);
+        
+        //the acceleration data is 20 bits and needs to come in via three register transactions
+        cs=0;
+        spi.write(YDATA3<<1|0x01);
+        ui8SensorY=spi.write(0xFF); 
+        ui8SensorY2=spi.write(0xFF);
+        ui8SensorY3=spi.write(0xFF);        
+        cs=1;
+        // Put it back together
+        
+        ui32Result = ((ui8SensorY << 16) | (ui8SensorY2 << 8) | ui8SensorY3); /* Set read result*/
+        //pc.printf("Y acceleration data = 0x%X\n", ui32Result);
+        // This should output the ID register as 0x01        
+        i32SensorY = ADXL355_Acceleration_Data_Conversion(ui32Result); 
+        //pc.printf("Y acceleration formatted data = %d\n", i32SensorY);
+        pc.printf("Y = %2.6f g, ", i32SensorY*LSB_scale_factor);
+        
+        
+        //the acceleration data is 20 bits and needs to come in via three register transactions
+        cs=0;
+        spi.write(ZDATA3<<1|0x01);
+        ui8SensorZ=spi.write(0xFF); 
+        ui8SensorZ2=spi.write(0xFF);
+        ui8SensorZ3=spi.write(0xFF);        
+        cs=1;
+        // Put it back together
+        
+        ui32Result = ((ui8SensorZ << 16) | (ui8SensorZ2 << 8) | ui8SensorZ3); /* Set read result*/
+        //pc.printf("Z acceleration data = 0x%X\n", ui32Result);
+        // This should output the ID register as 0x01        
+        i32SensorZ = ADXL355_Acceleration_Data_Conversion(ui32Result);        
+        //pc.printf("Z acceleration formatted data = %d \n", i32SensorZ);
+        pc.printf("Z = %2.6f g", i32SensorZ*LSB_scale_factor);
+        
+        pc.printf("\n");
+    }
+}
+int32_t ADXL355_Acceleration_Data_Conversion (uint32_t ui32SensorData)
+{
+   int32_t volatile i32Conversion = 0;
+
+   ui32SensorData = (ui32SensorData  >> 4);
+   ui32SensorData = (ui32SensorData & 0x000FFFFF);
+
+   if((ui32SensorData & 0x00080000)  == 0x00080000){
+
+         i32Conversion = (ui32SensorData | 0xFFF00000);
+
+   }
+   else{
+         i32Conversion = ui32SensorData;
+   }
+
+   return i32Conversion;
+}
+
+void ADXL355_Start_Sensor(void)
+{
+   cs=0;
+   spi.write(POWER_CTL<<1|0x01);
+   id=spi.write(0xFF);
+   cs=1;
+   pc.printf("POWER_CTL register = 0x%X\n", id);
+    
+   id = id & 0xFE; // Clear measurement bit in POWER_CTL register and turn it on
+   
+   // Write the power control register, which involves add a '1' to the 8th bit 
+   // address for the ADXL355
+   
+   int volatile tmp=(POWER_CTL<<1);
+   
+   cs=0;
+   spi.write(tmp);
+   spi.write(id);
+   cs=1;
+      
+   
+   //SPI_Write(POWER_CTL, ui8temp, 0x00, SPI_WRITE_ONE_REG);                    /* Write the new value to POWER_CTL register */
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Thu Feb 06 19:25:43 2020 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#3a57ec7401a77b8b98f6356a1498cb154229483f