I-O DATA DEV2 / Mbed 2 deprecated ud-gs4-R_400G_SD_Log_No1

Dependencies:   mbed SDFileSystem_

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers h3lis331spi.cpp Source File

h3lis331spi.cpp

00001 #include  "mbed.h"
00002 #include <stdio.h>
00003 
00004 #define H3LIS331DL_CTRL_REG1            0x20
00005 #define H3LIS331DL_CTRL_REG2            0x21
00006 #define H3LIS331DL_CTRL_REG3            0x22
00007 #define H3LIS331DL_CTRL_REG4            0x23
00008 #define H3LIS331DL_CTRL_REG5            0x24 
00009 //STATUS_REG_AXIES 
00010 #define H3LIS331DL_STATUS_REG           0x27   //
00011 
00012 ///i2c or SPI 
00013 //#define USESPI 
00014 
00015 //SPI STSPI(SPI_MOSI, SPI_MISO, SPI_SCK);
00016 /*******************************************
00017 int initLIS331();
00018 int read3axes(short *tx,short *ty,short *tz);
00019 int readTemp(short *tmp);
00020 ******************************************/
00021 
00022 
00023 //mbed Class
00024 //SPI STSPI(SPI_MOSI, SPI_MISO, SPI_SCK);
00025 //SPI STSPI(D11, D12, D13);
00026 //\\DigitalOut  STSPICS(D10);
00027 
00028 #ifdef USESPI
00029 ///// SPI -
00030 extern SPI STSPI;
00031 extern DigitalOut  STSPICS;
00032 /*********** porting **************/   
00033 void spiFormat(int b,int m) {
00034    STSPI.format(b,m);  /* 8bit */
00035 }
00036 void spiFrequency(int f){
00037     STSPI.frequency(f ); /* 1Mbps */
00038 }
00039 void spiWriteCS(int cs) {
00040     STSPICS=cs;
00041 }
00042 int spiWrite(int wd) {
00043     return ( STSPI.write(wd)); 
00044 }   
00045 ////////
00046 #else 
00047 //i2c /////
00048 extern I2C i2c;
00049 //int deviceAddress = 0x18;
00050 int deviceAddress = 0x19;
00051 ///////////
00052 #endif
00053 extern RawSerial          pc;
00054 static int i2cReadRegs(unsigned char ad,unsigned char *p,int bytes);
00055 static unsigned char spireadReg(unsigned char ad);
00056 static int spireadRegs(unsigned char ad,unsigned char *p,int bytes);
00057 static unsigned char spiwriteReg(unsigned char ad,unsigned char val);
00058 #define LIS331  0x32
00059 /*****************************************
00060   init h3LIS331DL ST micro 3-axes accelerometer
00061    SPI interface 
00062 *****************************************/
00063 int initLIS331()
00064 {
00065 //    STSPI = new SPI(SPI_MOSI, SPI_MISO, SPI_SCK, D6);
00066 #ifdef USESPI   
00067     spiFormat(8,3);  /* 8bit */
00068     spiFrequency(8000000); /* 8Mbps */
00069     spiWriteCS(1);
00070 #else
00071 #endif    
00072     wait_us(10);
00073     //pc.printf("Who am I %02x\r\n", spireadReg(0x0f));
00074     //spiwriteReg(H3LIS331DL_CTRL_REG1,0x27);  // power normal xyz enable out 50Hz  0010 0111 
00075     spiwriteReg(H3LIS331DL_CTRL_REG1,0x3F);  // power normal xyz enable out 1000Hz  0011 1111
00076     //spiwriteReg(H3LIS331DL_CTRL_REG1,0x37);  // power normal xyz enable out 400Hz  0011 0111
00077     spiwriteReg(H3LIS331DL_CTRL_REG2,0x00);  // normal, no filter 
00078     spiwriteReg(H3LIS331DL_CTRL_REG3,0x00);  // no interrut
00079     spiwriteReg(H3LIS331DL_CTRL_REG4,0x30);  // 0011 0000 / BDU:0 BLE:0  FS1:1 FS0:1 400g 
00080     //spiwriteReg(H3LIS331DL_CTRL_REG4,0x70);  // 0011 1000 / BDU:0 BLE:1  FS1:1 FS0:1 400g 
00081     spiwriteReg(H3LIS331DL_CTRL_REG5,0x00);  // max 400G
00082   //  spiwriteReg(0x23,0x98);  
00083     //spireadReg(0x25);
00084     return (spireadReg(0x0f) - LIS331);
00085 }
00086 
00087   
00088 /*************************************************
00089   read 3-axes from LIS3DH
00090 *************************************************/
00091 int read3axes331(short *tx,short *ty,short *tz)
00092 {
00093     int timeout=5;
00094     unsigned char regs[6];
00095     while((spireadReg(0x27)&7)!=7 && timeout--){
00096         //pc.printf("read3axes331 not ready.\r\n");
00097         wait_us(10);
00098     }
00099     /*
00100     if((spireadReg(0x27)& 7) == 7){
00101         pc.printf("read OK!\r\n");
00102     }
00103     */
00104     //for (int i=0;i<6;i++) regs[i]= spireadReg(0x28 + i );// 14bit signed   1G = 413 9.8m/s**2
00105 //    STSPI.frequency(); //1MHz
00106 #if 1
00107     regs[0]=spireadReg(0x28);
00108     regs[1]=spireadReg(0x29);
00109     regs[2]=spireadReg(0x2A);
00110     regs[3]=spireadReg(0x2B);
00111     regs[4]=spireadReg(0x2C);
00112     regs[5]=spireadReg(0x2D);
00113 #else
00114     spireadRegs(0x28 ,regs, 6);// x y z, read all
00115 #endif
00116 
00117     //*tx = (short)(regs[0] | (regs[1]<<8)) >> 3;
00118     *tx = (short)(regs[0] | (regs[1]<<8));
00119     //*ty = (short)(regs[2] | (regs[3]<<8)) >> 3;
00120     *ty = (short)(regs[2] | (regs[3]<<8));
00121     //*tz = (short)(regs[4] | (regs[5]<<8)) >> 3;
00122     *tz = (short)(regs[4] | (regs[5]<<8));
00123     return(0);
00124 }
00125 
00126 
00127 
00128 /*************************************************
00129   Read a regster from SPI
00130 *************************************************/
00131 static unsigned char spireadReg(unsigned char ad){
00132     unsigned char r;
00133 #ifdef USESPI    
00134     spiWriteCS(0);
00135     wait_us(1);
00136     spiWrite(ad | 0x80);  
00137     r = spiWrite(0);
00138     wait_us(1);
00139     spiWriteCS(1);
00140 #else
00141     char Reg=ad;
00142     char Data[2];
00143 
00144     i2c.write((deviceAddress << 1) & 0xFE, (char *)&Reg, 1);
00145     i2c.read((deviceAddress << 1) | 0x01, (char *)Data, 2);
00146     i2c.read((deviceAddress << 1) | 0x01, (char *)Data, 1);
00147     r = Data[1];   
00148     //r = Data[0];  
00149     //pc.printf("read regs %02x %02x\r\n", Data[0],Data[1]);
00150 #endif    
00151     return(r);
00152 }
00153 
00154 /*************************************************
00155 Read multi regsters from i2c
00156 *************************************************/
00157 static int i2cReadRegs(unsigned char ad,unsigned char *p,int bytes){
00158 char Reg=ad;
00159 i2c.start();
00160 i2c.write((deviceAddress << 1) & 0xFE, (char *)&Reg, 1);
00161 i2c.read((deviceAddress << 1) | 0x01, (char *)p, bytes);
00162 i2c.stop();
00163 return(0);
00164 }
00165 
00166 /*************************************************
00167   Read multi regsters from SPI
00168 *************************************************/
00169 static int spireadRegs(unsigned char ad,unsigned char *p,int bytes){
00170     
00171 #ifdef USESPI
00172 #if 1
00173    while(bytes--) {
00174       *p = spireadReg( ad );
00175       p++;
00176       ad++;
00177    }    
00178 #else   
00179    spiWriteCS(0);
00180     wait_us(1);
00181     spiWrite(ad | 0x80 );  
00182    while(bytes--) {
00183       *p++ = spiWrite(0);
00184    }
00185    wait_us(1);
00186    spiWriteCS(1);
00187 #endif 
00188 #else
00189 char Reg=ad;
00190 i2c.start();
00191 i2c.write((deviceAddress << 1) & 0xFE, (char *)&Reg, 1);
00192 i2c.read((deviceAddress << 1) | 0x01, (char *)p, bytes);
00193 i2c.stop();
00194 #endif
00195     return(0);
00196 }
00197 
00198 /*************************************************
00199   Write a regster via SPI
00200 *************************************************/
00201 static unsigned char spiwriteReg(unsigned char ad,unsigned char val){
00202     unsigned char r;
00203 #ifdef USESPI
00204     spiWriteCS(0);
00205     wait_us(1);
00206     spiWrite(ad);  
00207     r = spiWrite(val);
00208     wait_us(1);
00209     spiWriteCS(1);
00210 #else 
00211 //i2c
00212     uint8_t buf[2];
00213     
00214     buf[0] = ad;
00215     buf[1] = val;
00216     i2c.write((deviceAddress << 1) & 0xFE, (char *)buf, 2);
00217 #endif    
00218     return(r);
00219 }
00220 
00221