Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed SDFileSystem_
main.cpp
- Committer:
- tnanbu
- Date:
- 2021-09-10
- Revision:
- 39:75f7b67a5747
- Parent:
- 38:87b7b3de6e88
- Child:
- 40:16c2d6b97a92
File content as of revision 39:75f7b67a5747:
#include "mbed.h"
#include "SDFileSystem.h"
#include <errno.h>
#include <stdio.h>
#include <FileHandle.h>
#include <mbed_poll.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
//キャリブレーション 仮値
//No.1
#define OFFSET_X 220
#define OFFSET_Y 220
#define OFFSET_Z -40
//No.2
//#define OFFSET_X 375
//#define OFFSET_Y 340
//#define OFFSET_Z -200
//#define OFFSET_X 0
//#define OFFSET_Y 0
//#define OFFSET_Z 0
#define DATA_SIZE 512
//#define MBED_BUFF 512
HAL_StatusTypeDef writeEEPROMByte(uint32_t address, uint8_t data);
uint8_t readEEPROMByte(uint32_t address);
//H3LIS331DL h3dacc(PB_7,PB_6);
I2C i2c(PB_7,PB_6); //NUCLEO pin assign
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 myled(PB_5);
DigitalIn button(PB_4);
int btn_flag = 0;
int initLIS3DH();
int read3axes(short *tx,short *ty,short *tz);
int initLIS331();
int read3axes331(short *tx,short *ty,short *tz);
int int_sqrt(unsigned int x);
//int readTemp(short *tmp);
FILE *fp1,*fp2;//fp1:H3LIS331DL, fp2:LIS3DH
unsigned long timecount = 0;
short oldcount = 0;
short newcount = 0;
long max_g=0;
long tmp_max=0;
long tmp_max2=0;
short update=0;
char buffer1[DATA_SIZE] = {};
char buffer2[DATA_SIZE] = {};
char stracc1[32] = {}, stracc2[32] = {};
int lognum1 = 0, lognum2 = 0;
char filename1[16]= {};
char filename2[16]= {};
int ret = 0,init=1;
short end_flag = 0;
/*
void timer(){
}
*/
void sub(){
static short tx=0,ty=0,tz=0;
static short tx2=0,ty2=0,tz2=0;
static long scr = 0;
static long scr2 = 0;
static long ax,ay,az,as;
static long ax2,ay2,az2,as2;
read3axes331(&tx,&ty,&tz);
//キャリブレーションの補正
tx += OFFSET_X;
ty += OFFSET_Y;
tz += OFFSET_Z;
//スカラー値変換
scr = int_sqrt( tx*tx + ty*ty + tz*tz);// 195mg/digit
//now_g = scr;
ax+= tx;
ay+= ty;
az+= tz;
as+= scr;
//newcount++;
//pc.printf("%d,%d,x:%2.2f y:%2.2f,z:%2.2f scaler = %2.2f \r\n",timecount,time(NULL),(float)tx*0.005,(float)ty*0.005,(float)tz*0.005,(float)scr*0.005);
//pc.printf("new x:%d y:%d,z:%d scaler = %d \r\n",tx,ty,tz,scr);
//Total Max G
if ( max_g < scr) {
max_g = scr;
//update=1;
//pc.printf("%max g=%d \r\n",max_g);
//Save EEPROM
for(int i = 0; i < 4; i++){
//pc.printf("writerom 0x%x\r\n",(max_g >> 8*i) & 0xFF);
writeEEPROMByte(i, (max_g >> 8*i) & 0xFF);
}
}
//MAX G per cycle
if(tmp_max < scr){
tmp_max = scr;
}
//平均(/s)
if (newcount == 500) {
float asc = (float)as / 500.0f;
//pc.printf("avarage(new) x:%2.2f,y:%2.2f,z:%2.2f,scaler=%2.2f,max:%2.2f\r\n",
//(float)ax/500.0f*0.005,(float)ay/500.0f*0.005,(float)az/500.0f*0.005,(float)as/500.0f*0.005, (float)tmp_max*0.005);
sprintf(stracc1,"%d,%d,%2.2f,%2.2f\r\n",timecount,time(NULL),asc*0.005,(float)tmp_max*0.005);
if((strlen(buffer1)+strlen(stracc1)) > DATA_SIZE) {
pc.printf("try write fp1:%d,%d\r\n",strlen(buffer1),strlen(stracc1));
//ret = fprintf(fp1,"%s",buffer1);
ret = fwrite(buffer1,sizeof(char),strlen(buffer1),fp1);
if(ret < strlen(buffer1)){
pc.printf("fp1 write failed:%d. errno:%d. Reboot!!!\r\n",ret,errno);
NVIC_SystemReset();
}
memset(buffer1,0,sizeof(buffer1));
sprintf(buffer1,"%s",stracc1);
} else {
sprintf(buffer1,"%s%s",buffer1,stracc1);
}
as=ax=ay=az=0;
newcount = 0;
tmp_max = 0;
}
if( (oldcount % 10) == 0){
//LIS3DH
read3axes(&tx2,&ty2,&tz2);
scr2 = int_sqrt( tx2*tx2 + ty2*ty2 + tz2*tz2);
ax2+= tx2;
ay2+= ty2;
az2+= tz2;
as2+= scr2;
if(tmp_max2 < scr2){
tmp_max2 = scr2;
}
//pc.printf("old %d,%d,x:%2.2f y:%2.2f,z:%2.2f scaler = %2.2f \r\n",timecount,time(NULL),(float)tx2*0.005,(float)ty2*0.005,(float)tz2*0.005,(float)scr2*0.005);
//pc.printf("%d,%d,old x:%d,y:%d,z:%d scaler = %d\r\n",timecount,time(NULL),tx,ty,tz,scr);
//sprintf(stracc2,"%d,%d,%d\r\n",timecount,time(NULL),scr);
//pc.printf("lis3dh:%s",stracc2);
if(oldcount == 5000){
float asc2 = (float)as2 / 500.0f;
//pc.printf("avarage(old) x:%2.2f,y:%2.2f,z:%2.2f,scaler=%2.2f,max:%2.2f\r\n",
//(float)ax2/500.0f*0.005,(float)ay2/500.0f*0.005,(float)az2/500.0f*0.005,(float)as2/500.0f*0.005, (float)tmp_max2*0.005);
sprintf(stracc2,"%d,%d,%2.2f,%2.2f\r\n",timecount,time(NULL),asc2*0.005,(float)tmp_max2*0.005);
if( (strlen(buffer2)+ strlen(stracc2)) > DATA_SIZE) {
pc.printf("try write fp2:%d,%d\r\n",strlen(buffer2),strlen(stracc2));
ret = fwrite(buffer2,sizeof(char),strlen(buffer2),fp2);
if(ret < strlen(buffer2)){
pc.printf("fp2 write failed:%d. errno:%d. Reboot!!!\r\n",ret,errno);
NVIC_SystemReset();
}
memset(buffer2,0,sizeof(buffer2));
sprintf(buffer2,"%s",stracc2);
} else {
sprintf(buffer2,"%s%s",buffer2,stracc2);
}
oldcount = 0;
as2=ax2=ay2=az2=0;
tmp_max2 = 0;
}
}
if((time(NULL) % 300) == 0){
//5 minutes passed. Save File.
if(fp1){
fclose(fp1);
fp1 = NULL;
}
if(fp2){
fclose(fp2);
fp2 = NULL;
}
fp1 = fopen(filename1,"ab");
if(!fp1){
pc.printf("fp1 create file failed:%d\r\n",errno);
NVIC_SystemReset();
}
fp2 = fopen(filename2,"ab");
if(!fp2){
pc.printf("fp2 create file failed:%d\r\n",errno);
NVIC_SystemReset();
}
}
if(time(NULL) > 86400){
//Create New File
set_time(NULL);
timecount = 0;
if(fp1){
fclose(fp1);
//free(fp1);
fp1 = NULL;
}
if(fp2){
fclose(fp2);
//free(fp2);
fp2 = NULL;
}
lognum1++;
lognum2++;
sprintf(filename1,"/sd/new_%d",lognum1);
sprintf(filename2,"/sd/old_%d",lognum2);
fp1 = fopen(filename1,"ab");
if(!fp1){
pc.printf("fp1 create file failed:%d\r\n",errno);
NVIC_SystemReset();
}
fp2 = fopen(filename2,"ab");
if(!fp2){
pc.printf("fp2 create file failed:%d\r\n",errno);
NVIC_SystemReset();
}
pc.printf("Create New File:%d,%d",timecount,time(NULL));
}
//Button Check
if(button.read() == 1){
btn_flag++;
//pc.printf("Button pressed:%d\r\n",btn_flag);
}
else{
btn_flag = 0;
}
if(btn_flag > 200){
//pc.printf("Button Pressed. Close fp\r\n");
if(fp1){
fclose(fp1);
fp1 = NULL;
}
if(fp2){
fclose(fp2);
fp2 = NULL;
}
btn_flag = 0;
myled = 0;
end_flag = 1;
wait(1);
}
}
int main()
{
i2c.frequency(400000);
short retry = 0;
sprintf(filename1,"/sd/new_%d",lognum1);
sprintf(filename2,"/sd/old_%d",lognum2);
while(!fp1 && retry < 5){
fp1 = fopen(filename1,"ab");
if(!fp1) {
pc.printf("fp1(%s) open failed errno:%d\r\n",filename1,errno);
//NVIC_SystemReset();
}
retry++;
wait_ms(100);
}
retry = 0;
while(!fp2 && retry < 5){
fp2 = fopen(filename2,"ab");
if(!fp2) {
pc.printf("fp2(%s) open failed errno:%d\r\n",filename2,errno);
//NVIC_SystemReset();
}
retry++;
wait_ms(100);
}
//Read Max G
for(int i = 0; i < 4; i++){
//pc.printf("readEEPROM:0x%x\r\n",readEEPROMByte(i));
max_g |= readEEPROMByte(i) << 8*i;
}
pc.printf("read max_g from eeprom:%d\r\n",max_g);
while(init) {
init = initLIS331();
initLIS3DH();
pc.printf(" init acc sensor %d\r\n",ret);
wait_ms(100);
}
//interrupt.attach_us(&timer,20000);//20ms
while(!end_flag)
{
timecount++;
oldcount++;
newcount++;
sub();
//wait_ms(20);
//wait_ms(1);
}
pc.printf("Program End.\r\n");
return 0;
}
/*********** 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));
}
/*******************************************************
EEPROM WRITE
********************************************************/
HAL_StatusTypeDef writeEEPROMByte(uint32_t address, uint8_t data)
{
HAL_StatusTypeDef status;
address = address + 0x08080000;
HAL_FLASHEx_DATAEEPROM_Unlock(); //Unprotect the EEPROM to allow writing
status = HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_BYTE, address, data);
HAL_FLASHEx_DATAEEPROM_Lock(); // Reprotect the EEPROM
return status;
}
/*******************************************************
EEPROM READ
********************************************************/
uint8_t readEEPROMByte(uint32_t address) {
volatile uint8_t tmp ;
address = address + 0x08080000;
tmp = *(__IO uint8_t*)address;
return tmp;
}
/***************************
integer sqrt
整数 sqrt √演算を整数で ライブラリより速い
****************************/
int int_sqrt(unsigned int x)
{
register int a = 0, c = 0, y = 0, i = 0, t = x;
while(t >>= 1){
++i;
}
for(i += i & 1; i >= 0; i -= 2){
c = (y << 1 | 1) <= x >> i;
a = a << 1 | c;
y = y << 1 | c;
x -= c * y << i;
y += c;
}
return a;
}