Lin Team / Mbed OS ADXL355
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Copyright (c) 2019 Analog Devices, Inc.  All rights reserved.
00002 
00003 Redistribution and use in source and binary forms, with or without modification, 
00004 are permitted provided that the following conditions are met:
00005   - Redistributions of source code must retain the above copyright notice, 
00006   this list of conditions and the following disclaimer.
00007   - Redistributions in binary form must reproduce the above copyright notice, 
00008   this list of conditions and the following disclaimer in the documentation 
00009   and/or other materials provided with the distribution.  
00010   - Modified versions of the software must be conspicuously marked as such.
00011   - This software is licensed solely and exclusively for use with processors/products 
00012   manufactured by or for Analog Devices, Inc.
00013   - This software may not be combined or merged with other code in any manner 
00014   that would cause the software to become subject to terms and conditions which 
00015   differ from those listed here.
00016   - Neither the name of Analog Devices, Inc. nor the names of its contributors 
00017   may be used to endorse or promote products derived from this software without 
00018   specific prior written permission.
00019   - The use of this software may or may not infringe the patent rights of one or 
00020   more patent holders.  This license does not release you from the requirement 
00021   that you obtain separate licenses from these patent holders to use this software.
00022 
00023 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" AND 
00024 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, 
00025 TITLE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 
00026 NO EVENT SHALL ANALOG DEVICES, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
00027 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES 
00028 (INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF INTELLECTUAL 
00029 PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 
00030 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
00031 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
00032 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
00033 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 
00035 2019-01-10-7CBSD SLA
00036 
00037  */
00038 
00039 #include "mbed.h"
00040 #include "platform/mbed_thread.h"
00041 #include "ADXL355.h"
00042 
00043 /* Global Data */
00044 
00045 float LSB_scale_factor=0.0000039f; 
00046 
00047 int32_t volatile i32SensorX;
00048 int32_t volatile i32SensorY;
00049 int32_t volatile i32SensorZ;
00050 int32_t volatile i32SensorT;
00051 
00052 uint8_t volatile ui8SensorX;
00053 uint8_t volatile ui8SensorX2;
00054 uint8_t volatile ui8SensorX3;
00055 uint32_t volatile ui32SensorX;
00056 
00057 uint8_t volatile ui8SensorY;
00058 uint8_t volatile ui8SensorY2;
00059 uint8_t volatile ui8SensorY3;
00060 uint32_t volatile ui32SensorY;
00061 
00062 uint8_t volatile ui8SensorZ;
00063 uint8_t volatile ui8SensorZ2;
00064 uint8_t volatile ui8SensorZ3;
00065 uint32_t volatile ui32SensorZ;
00066 
00067 
00068 uint8_t volatile ui8SensorT;
00069 uint8_t volatile ui8SensorT2;
00070 uint32_t volatile ui32SensorT;
00071 
00072 uint32_t volatile ui32Result = 0;
00073 
00074 // Blinking rate in milliseconds
00075 #define SLEEP_TIME                  500
00076 // Setup the SPI peripheral to talk to the ADXL35x
00077 //SPI spi(D11, D12, D13);
00078 // initialize chip select
00079 //DigitalOut cs(D10);
00080 
00081 SPI spi(PB_15, PB_14, PB_13);
00082 // initialize chip select
00083 DigitalOut cs(PB_1);
00084 
00085 
00086 
00087 
00088 
00089 // Initialise the digital pin LED1 as an output
00090 DigitalOut led1(LED1);
00091 // Initialise the serial object with TX and RX pins
00092 Serial pc(USBTX, USBRX);
00093 Timer t; 
00094 int id=0;
00095 int i=1;
00096 
00097 // main() runs in its own thread in the OS
00098 int main()
00099 {
00100     pc.printf("Hello World!");
00101     pc.baud (115200) ;
00102     
00103     spi.frequency(1000000);
00104     
00105     cs=0;
00106     spi.write(DEVID_AD<<1|0x01);
00107     id=spi.write(0xFF);
00108     cs=1;
00109     pc.printf("ID register = 0x%X\n", id);
00110     // This should output the ID register as 0xAD
00111     
00112     cs=0;
00113     spi.write(DEVID_MST<<1|0x01);
00114     id=spi.write(0xFF);
00115     cs=1;
00116     pc.printf("DEVID register = 0x%X\n", id);
00117     // This should output the ID register as 0x1D
00118     
00119     cs=0;
00120     spi.write(PARTID<<1|0x01);
00121     id=spi.write(0xFF);
00122     cs=1;
00123     pc.printf("PARTID register = 0x%X\n", id);
00124     // This should output the ID register as 0xED
00125     
00126     cs=0;
00127     spi.write(REVID<<1|0x01);
00128     id=spi.write(0xFF);
00129     cs=1;
00130     pc.printf("REVID register = 0x%X\n", id);
00131     // This should output the ID register as 0x01
00132 
00133     // Turn on the sensor
00134     ADXL355_Start_Sensor();
00135     
00136     // Now dump the temp and accelerometer data to the port
00137     
00138     while (true) {
00139          t.start(); 
00140         
00141         // Blink LED and wait 500 ms
00142         //led1 = !led1;
00143         //thread_sleep_for(SLEEP_TIME);
00144         
00145         //the acceleration data is 20 bits and needs to come in via three register transactions
00146         cs=0;
00147         spi.write(XDATA3<<1|0x01);
00148         ui8SensorX=spi.write(0xFF); 
00149         ui8SensorX2=spi.write(0xFF);
00150         ui8SensorX3=spi.write(0xFF);        
00151         cs=1;
00152         // Put it back together
00153         
00154         ui32Result = ((ui8SensorX << 16) | (ui8SensorX2 << 8) | ui8SensorX3); /* Set read result*/
00155         //pc.printf("X acceleration data = 0x%X\n", ui32Result);
00156         // This should output the ID register as 0x01        
00157         i32SensorX = ADXL355_Acceleration_Data_Conversion(ui32Result); 
00158         //pc.printf("X acceleration formatted data = %d\n", i32SensorX);
00159      //  pc.printf("%2.6f   \n\r", i32SensorX*LSB_scale_factor);
00160         
00161         //the acceleration data is 20 bits and needs to come in via three register transactions
00162         cs=0;
00163         spi.write(YDATA3<<1|0x01);
00164         ui8SensorY=spi.write(0xFF); 
00165         ui8SensorY2=spi.write(0xFF);
00166         ui8SensorY3=spi.write(0xFF);        
00167         cs=1;
00168         // Put it back together
00169         
00170         ui32Result = ((ui8SensorY << 16) | (ui8SensorY2 << 8) | ui8SensorY3); /* Set read result*/
00171         //pc.printf("Y acceleration data = 0x%X\n", ui32Result);
00172         // This should output the ID register as 0x01        
00173         i32SensorY = ADXL355_Acceleration_Data_Conversion(ui32Result); 
00174         //pc.printf("Y acceleration formatted data = %d\n", i32SensorY);
00175      //  pc.printf("%2.6f   \n\r", i32SensorY*LSB_scale_factor);
00176         
00177         
00178         
00179         
00180         //the acceleration data is 20 bits and needs to come in via three register transactions
00181         cs=0;
00182         spi.write(ZDATA3<<1|0x01);
00183         ui8SensorZ=spi.write(0xFF); 
00184         ui8SensorZ2=spi.write(0xFF);
00185         ui8SensorZ3=spi.write(0xFF);        
00186         cs=1;
00187         // Put it back together
00188         
00189         
00190        
00191         
00192         
00193         ui32Result = ((ui8SensorZ << 16) | (ui8SensorZ2 << 8) | ui8SensorZ3); /* Set read result*/
00194         //pc.printf("Z acceleration data = 0x%X\n", ui32Result);
00195         // This should output the ID register as 0x01        
00196         i32SensorZ = ADXL355_Acceleration_Data_Conversion(ui32Result);        
00197         //pc.printf("Z acceleration formatted data = %d \n", i32SensorZ);
00198     //   pc.printf("%2.6f  \n\r", i32SensorZ*LSB_scale_factor);
00199         
00200         
00201         
00202         
00203             
00204         t.stop(); 
00205         //pc.printf(" %d us\n\r", t.read_us());  wait1.5ms
00206 
00207         wait(0.01); //max0.0016
00208         
00209          
00210         
00211     }
00212 }
00213 int32_t ADXL355_Acceleration_Data_Conversion (uint32_t ui32SensorData)
00214 {
00215    int32_t volatile i32Conversion = 0;
00216 
00217    ui32SensorData = (ui32SensorData  >> 4);
00218    ui32SensorData = (ui32SensorData & 0x000FFFFF);
00219 
00220    if((ui32SensorData & 0x00080000)  == 0x00080000){
00221 
00222          i32Conversion = (ui32SensorData | 0xFFF00000);
00223 
00224    }
00225    else{
00226          i32Conversion = ui32SensorData;
00227    }
00228 
00229    return i32Conversion;
00230 }
00231 
00232 void ADXL355_Start_Sensor(void)
00233 {
00234    cs=0;
00235    spi.write(POWER_CTL<<1|0x01);
00236    id=spi.write(0xFF);
00237    cs=1;
00238    pc.printf("POWER_CTL register = 0x%X\n", id);
00239     
00240    id = id & 0xFE; // Clear measurement bit in POWER_CTL register and turn it on
00241    
00242    // Write the power control register, which involves add a '1' to the 8th bit 
00243    // address for the ADXL355
00244    
00245    int volatile tmp=(POWER_CTL<<1);
00246    
00247    cs=0;
00248    spi.write(tmp);
00249    spi.write(id);
00250    cs=1;
00251       
00252    
00253    //SPI_Write(POWER_CTL, ui8temp, 0x00, SPI_WRITE_ONE_REG);                    /* Write the new value to POWER_CTL register */
00254 }
00255 
00256