Fork me on GitHub

一 概念

   fatfs文件系统在文件读写中不可或却。熟悉和深入理解是一个不可或缺的前提。

这里面需要先明确几个概念:文件open的属性,这个非常重要。可以并列使用。

 

二  源码解析

A  写入数据:

      if((false == g_lora_state) && (Mybuffer.Data_Length( &My_ADS1299_buffer)>= ADS_DATA_SIZE))
      {
          Mybuffer.Get( &My_ADS1299_buffer,  TF_Buffer_ADS1299,  ADS_DATA_SIZE);
          //memset(TF_Buffer_ADS1299,0x11,TF_Buffer_ADS1299_Size);
          //dump8(TF_Buffer_ADS1299,10);

          FRESULT res = f_write( &ADS1299_file,  TF_Buffer_ADS1299 ,   TF_Buffer_ADS1299_Size,   &bw);
          if(res == FR_OK)
          {
            
            ads_data_cnt++;
            //f_sync(&ADS1299_file);

            if(ads_data_cnt%1 == 0)
            {
              mprintf("date 4.10 ads data wirte success cnt:%d \n\r",ads_data_cnt);

            }

          }
          else
          {
            data_failed_cnt++;
            mprintf("ads task write data failed:%d failed cnt:%d  \n\r",res,data_failed_cnt);

          }

      }

B 读取的文件打开

        g_lora_state = true;
        f_close(&ADS1299_file);
        res=f_open(&ADS1299_file, ADS1299_filename,  FA_READ);
        mprintf("file sd enter and open file state is:%d \n\r",res);

C 文件读取

        FRESULT res = f_read( &ADS1299_file,  TF_Buffer_ADS1299 , TF_Buffer_ADS1299_Size,   &bw);
        mprintf("file read state is:%d \n\r",res);
        
        if(bw < 1)
        {
          mprintf("file read over \n\r");
          g_lora_state = false;
          seq_num = 0;

          f_close(&ADS1299_file);
          res=f_open(&ADS1299_file, ADS1299_filename, FA_OPEN_ALWAYS | FA_WRITE);
          mprintf("file save and open file state is:%d \n\r",res);
        }
        else
        {
          dump8(TF_Buffer_ADS1299,20);
        }

 

posted on 2023-04-25 16:26  虚生  阅读(88)  评论(0编辑  收藏  举报