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

Dependencies:   mbed SDFileSystem_

main.cpp

Committer:
tnanbu
Date:
2021-08-30
Revision:
11:2d5fcf102778
Parent:
8:7f80139df48d
Child:
13:df67ca499051

File content as of revision 11:2d5fcf102778:

#include "mbed.h"
#include "SDFileSystem.h"
#include <time.h>

/*
   This basic example just shows how to read the ADC internal channels raw values.
   Please look in the corresponding device reference manual for a complete
   description of how to make a temperature sensor, VBat or Vref measurement.
*/

AnalogIn adc_temp(ADC_TEMP);
AnalogIn adc_vref(ADC_VREF);
/* UD-GS2 H/W define
 PIO_SWin    PB_4
 PIO_wkup    PA_4
 PIO_enable  PB_0
 PIO_intout1 PB_2
 PIO_led     PB_5
 PIO_v20v    PC_13
 PIO_v18v    PH_0
 PIO_intout2 PH_1
 PIO_spics   PB_12
 PIO_battryMonEn   PA_5
 PB_7 SDA
 PB_6 SCL

*/
//DigitalOut hx_clk(PB_7);
//DigitalIn hx_dt(PB_6);
//I2C               i2cacc(p_sda, p_scl)

#include "H3LIS331DL.h"

#define TIMEINTERVAL

H3LIS331DL  h3dacc(PB_7,PB_6);

RawSerial         pc(PA_9, PA_10,115200); //console UART
LowPowerTicker    interrupt;
//Ticker    interrupt;
SPI               STSPI(PB_15, PB_14, PB_13); //mosi,miso,clk
DigitalOut        STSPICS(PB_12);

SDFileSystem *sd = new SDFileSystem(PA_12, PA_11, PB_3, PA_15, "sd"); // mosi, miso, sclk, cs, name

DigitalOut        led(PB_5);
int initLIS3DH();
int read3axes(short *tx,short *ty,short *tz);
//int readTemp(short *tmp);
FILE     *fp1,*fp2;//fp1:H3LIS331DL, fp2:LIS3DH
int timecount = 0;
//static uint8_t buffer[512] = {};
//char buffer1[512] = {};
//char buffer2[512] = {};
//char stracc[32] = {};
//bool write1 = false,write2 = false;
//double xyz[3] = {};

/*
void timer(void)
{
    short x=0,y=0,z=0;
    float lis3dh_acc = 0;
    double xyz[3]= {},h3lis331dl_acc=0;
    //char stracc[32] = {};

//H3LIS331DL
    h3dacc.getAcceleration(xyz);
    //pc.printf("x:%f,y:%f,z:%f\r\n",(float)xyz[0],(float)xyz[1],(float)xyz[2]); //!!!If you read here, you cannot get acceleration!!!
    h3lis331dl_acc = sqrt(xyz[0]*xyz[0] + xyz[1]*xyz[1] + xyz[2]*xyz[2]);
    sprintf(stracc1,"%ld,%lf\r\n",timecount*200,h3lis331dl_acc);
    pc.printf("h3lis331dl:%s",stracc1);
    if(strlen(buffer1)+strlen(stracc1) > 512) {
        //pc.printf("buffer over. write file2\r\n");
        write1 = true;
    } else {
        sprintf(buffer1,"%s%s",buffer1,stracc1);
    }

    if(timecount == 10) {
        //pc.printf("calc lis3dh acc\r\n");
        //LIS3DH
        read3axes(&x,&y,&z);
        lis3dh_acc = sqrt( (float)x/1024.0f * (float)x/1024 + (float)y/1024.0f * (float)y/1024 + (float)z/1024.0f * (float)z/1024);
        sprintf(stracc2,"%ld,%lf\r\n",time(NULL),lis3dh_acc);
        pc.printf("lis3dh:%s",stracc2);
        if(strlen(buffer1)+ strlen(stracc1)> 512) {
            write2 = true;
        } else {
            sprintf(buffer2,"%s%s",buffer2,stracc2);
        }
        timecount = 0;
    }

    timecount++;
}
*/

int main()
{
    int lognum1 = 0, lognum2 = 0;
    char filename1[32]= {};
    char filename2[32]= {};
    int ret = 0;
    short x=0,y=0,z=0;
    double xyz[3] = {}, h3lis331dl_acc = 0;
    float lis3dh_acc = 0;
    int count = 0;
    char buffer1[512] = {};
    char buffer2[512] = {};
    char stracc1[32] = {}, stracc2[32] = {};

    initLIS3DH();

    h3dacc.init(H3LIS331DL_ODR_50Hz, H3LIS331DL_NORMAL,H3LIS331DL_FULLSCALE_8);//これで初期化している FULLSCALE_8=400G
    h3dacc.setHPFMode(H3LIS331DL_HPM_NORMAL_MODE_RES);//High Pass Filter ON
    //h3dacc.setHPFCutOFF(H3LIS331DL_HPFCF_1);

    sprintf(filename1,"/sd/lis3dh_%d",lognum1);
    sprintf(filename2,"/sd/h3lis_%d",lognum2);

    fp1 = fopen(filename1,"a");
    if(!fp1) {
        pc.printf("fp1(%s) open failed\r\n",filename1);
    }
    fp2 = fopen(filename2,"a");
    if(!fp2) {
        pc.printf("fp2(%s) open failed\r\n",filename2);
    }

    //interrupt.attach_us(&timer,200000);//200ms

    while(1) {
        //H3LIS331DL
        h3dacc.getAcceleration(xyz);
        //pc.printf("x:%f,y:%f,z:%f\r\n",(float)xyz[0],(float)xyz[1],(float)xyz[2]); //!!!If you read here, you cannot get acceleration!!!
        h3lis331dl_acc = sqrt(xyz[0]*xyz[0] + xyz[1]*xyz[1] + xyz[2]*xyz[2]);
        sprintf(stracc1,"%ld,%lf\r\n",count*20,h3lis331dl_acc);
        //pc.printf("h3lis331dl:%s",stracc1);
        if(strlen(buffer1)+strlen(stracc1) > 512) {
            //pc.printf("buffer over. write file2\r\n");
            ret = fprintf(fp1,"%s",buffer1);
            memset(buffer1,0,sizeof(buffer1));
            sprintf(buffer1,"%s",stracc1);
            //write1 = false;
        
        } else {
            sprintf(buffer1,"%s%s",buffer1,stracc1);
        }
        
        if(count == 10) {
        //LIS3DH
        read3axes(&x,&y,&z);
        lis3dh_acc = sqrt( (float)x/1024.0f * (float)x/1024 + (float)y/1024.0f * (float)y/1024 + (float)z/1024.0f * (float)z/1024);
        sprintf(stracc2,"%ld,%lf\r\n",timecount*20*10,lis3dh_acc);
        //pc.printf("lis3dh:%s",stracc2);
        if(strlen(buffer2)+ strlen(stracc2)> 512) {
            ret = fprintf(fp2,"%s",buffer2);
            //pc.printf("write buffer:%d\r\n",ret);
            memset(buffer2,0,sizeof(buffer2));
            sprintf(buffer2,"%s",stracc2);
        } else {
            sprintf(buffer2,"%s%s",buffer2,stracc2);
        }
        count = 0;
    }
        
        if(time(NULL) > 86400){
            //Create New File
            set_time(NULL);
            timecount = 0;
            fclose(fp1);
            fclose(fp2);
            lognum1++;
            lognum2++;
            sprintf(filename1,"/sd/lis3dh_%d",lognum1);
            sprintf(filename2,"/sd/h3lis331dl_%d",lognum2);
            fp1 = fopen(filename1,"a");
            if(!fp1){
                pc.printf("fp1 create file failed\r\n");
                }
            fp2 = fopen(filename2,"a");
            if(!fp2){
                pc.printf("fp2 create file failed\r\n");
            }
        }
        
        led = !led;
        timecount++;
        count++;
        wait_ms(20);
    }
}
/*********** porting **************/

void spiFormat(int b,int m)
{
    STSPI.format(b,m);  /* 8bit */
}
void spiFrequency(int f)
{
    STSPI.frequency(f); /* 1Mbps */
}
void spiWriteCS(int cs)
{
    STSPICS=cs;
}
int spiWrite(int wd)
{
    return ( STSPI.write(wd));
}

void error(const char* format, ...)
{
    return;
}