Files
SM-1000M/IAPV1.1/Driver/FATFS/data_handle.c
2026-04-23 10:50:18 +08:00

156 lines
3.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "bsp.h"
data_common data_common1;
FIL fp; /* Pointer to the blank file object */
FATFS fs;
UINT btw; /* Number of bytes to write */
UINT bws;
u32 send_pos=0;
DIR DirInf;
FILINFO FileInf;
/*
功能TF卡数据写入
参数fname
dat
len
返回值:无
*/
void FATFS_WriteFile(char *fname,char *dat,u16 len) //写数据到SD卡
{
UINT br;
char name[20];
sprintf(name,"%s%s",fname,".txt");
f_mount(0,&fs);
f_open (&fp,fname,FA_READ|FA_WRITE|FA_OPEN_ALWAYS);
f_lseek(&fp,f_size(&fp)); //移动到文件的末尾--追加数据
f_write(&fp,dat,len,&br);
f_close(&fp);
f_mount(0,NULL);
}
/*
功能TF卡数据写入 时间、电压、经纬度、海拔、传感器数据
参数fname
dat
len
返回值:无
*/
FRESULT Data_Write_TF(void)//,char *dat,u16 len
{
FRESULT res;
u8 i=0;
char name[10]="\0";
f_mount(0,&fs);
res=f_open(&fp,name,FA_READ | FA_WRITE | FA_OPEN_ALWAYS );//0:/
if(res!=FR_OK) //打开失败
{
i=0;
while((res!=FR_OK)&(i<100))
{
i++;
f_close(&fp);
f_mount(0,NULL);
f_mount(0,&fs);
res=f_open(&fp,name,FA_READ | FA_WRITE | FA_OPEN_ALWAYS );//0:/
}
if(i>98)return FR_NO_PATH ;
}
f_lseek (&fp,f_size(&fp));
f_write(&fp,data_common1.Pack_data,strlen(data_common1.Pack_data),&bws);
f_write(&fp,"\r\n",2,&bws);
f_close(&fp);
return FR_OK;
}
/*************文件删除*******************************/
FRESULT Fil_delete(char *names)
{
FRESULT result;
char namebuff[10]="\0";
sprintf(namebuff,"%s",names);
f_mount(0,&fs);
result = f_unlink(namebuff);
f_close(&fp);
f_mount(0,NULL);
return result;
}
//读取文件的大小byte
u32 txt_size_read(char *fname)
{
// u8 i=0;
FRESULT res;
u32 txt_size;
char name[20];
sprintf(name,"%s%s",fname,".txt");
res=f_mount(0,&fs);
res=f_open(&fp,"APP.bin",FA_READ);//0:/
if(res!=FR_OK) //打开失败
{
return FR_NO_FILE;
}
txt_size=f_size(&fp);
res=f_close(&fp);
f_mount(0,NULL);
return txt_size;
}
/*
功能TF卡数据读取
参数start_pos
len
返回值暂时测试用串口1打印 后续需要返回读取的数据
*/
void FATFS_Read_TF(char *fname,u32 start_pos,u32 len)
{
//u16 time1=0;
FRESULT res;
u32 tf_sendpos=0;
u8 times;
u32 i,lens_quotient,len_remainder;//商,余数
uint8_t Tf_Read_Buff[max_transmission+1];
lens_quotient=len/max_transmission;
len_remainder=len%max_transmission;
memset(Tf_Read_Buff,0,sizeof(Tf_Read_Buff));
res=f_mount(0,&fs);
res=f_open(&fp,fname,FA_READ);
times=0;
while((res!=FR_OK)&(times<100))
{
res=f_close(&fp);
res=f_mount(0,&fs);
res=f_open(&fp,fname,FA_READ);
times++;
if(times==100)return;
}
f_lseek(&fp, start_pos+tf_sendpos);
for(i=0;i<lens_quotient;i++)//发送大数据max_transmission字节
{
res=f_read(&fp,Tf_Read_Buff,max_transmission,&bws);
FLASH_WriteWord(APP_ADDR+(i*max_transmission),Tf_Read_Buff,max_transmission);
}
memset(Tf_Read_Buff,0,sizeof(Tf_Read_Buff));
f_read(&fp,Tf_Read_Buff,len_remainder,&bws);//发送余数字节
FLASH_WriteWord(APP_ADDR+(i*max_transmission),Tf_Read_Buff,len_remainder);
res=f_close(&fp);
f_mount(0,NULL);
}
u8 App_check(void)
{
uint8_t buff[10];
f_mount(0,&fs);
f_open(&fp,"AAP.bin",FA_READ);
f_read(&fp,buff,10,&bws);
if(((*(vu32*)(buff+4))&0xFF000000)==0x08000000)//判断是否为0X08XXXXXX.
{
return 0;
}
return 1;
}