807 lines
18 KiB
C
807 lines
18 KiB
C
/*-------------------------------------------------*/
|
||
/* SD卡源文件 */
|
||
/*-------------------------------------------------*/
|
||
/*-------------------------------------------------*/
|
||
#include "sd.h"
|
||
#include "spi.h"
|
||
//#include "led.h"
|
||
//#include "ff.h"
|
||
//#include "uart5.h"
|
||
#include "stm32f10x_flash.h"
|
||
|
||
#include "stm32f10x_spi.h"
|
||
#include "common.h"
|
||
#include <stdlib.h>
|
||
#include "stdint.h"
|
||
#include "stdarg.h"
|
||
|
||
|
||
u8 SD_Type=0;//SD卡的类型
|
||
|
||
void Delay(__IO uint32_t nCount)
|
||
{
|
||
while(nCount--);
|
||
|
||
}
|
||
////////////////////////////////////移植修改区///////////////////////////////////
|
||
//移植时候的接口
|
||
//data:要写入的数据
|
||
//返回值:读到的数据
|
||
u8 SD_SPI_ReadWriteByte(u8 data)
|
||
{
|
||
return SPI1_ReadWriteByte(data);
|
||
}
|
||
//SD卡初始化的时候,需要低速
|
||
void SD_SPI_SpeedLow(void)
|
||
{
|
||
SPI1_SetSpeed(SPI_BaudRatePrescaler_256);//设置到低速模式
|
||
}
|
||
//SD卡正常工作的时候,可以高速了
|
||
void SD_SPI_SpeedHigh(void)
|
||
{
|
||
SPI1_SetSpeed(SPI_BaudRatePrescaler_8);//设置到高速模式 SPI_BaudRatePrescaler_2
|
||
}
|
||
//SPI硬件层初始化
|
||
void SD_SPI_Init(void)
|
||
{
|
||
GPIO_InitTypeDef GPIO_InitStructure;
|
||
|
||
|
||
|
||
|
||
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE );//PORTB时钟使能
|
||
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE );//PORTB时钟使能
|
||
GPIO_InitStructure.GPIO_Pin = SD_CS_PIN;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;//GPIO_Mode_Out_PP; //PB13/14/15复用推挽输出
|
||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||
GPIO_Init(SD_CS_PORT, &GPIO_InitStructure);//初始化GPIOB
|
||
|
||
SPI1_Init();
|
||
SD_CS_CTL(1);
|
||
}
|
||
///////////////////////////////////////////////////////////////////////////////////
|
||
//取消选择,释放SPI总线
|
||
void SD_DisSelect(void)
|
||
{
|
||
SD_CS_CTL(1);
|
||
SD_SPI_ReadWriteByte(0xff);//提供额外的8个时钟
|
||
}
|
||
//选择sd卡,并且等待卡准备OK
|
||
//返回值:0,成功;1,失败;
|
||
u8 SD_Select(void)
|
||
{
|
||
SD_CS_CTL(0);
|
||
if(SD_WaitReady()==0)return 0;//等待成功
|
||
SD_DisSelect();
|
||
return 1;//等待失败
|
||
}
|
||
//等待卡准备好
|
||
//返回值:0,准备好了;其他,错误代码
|
||
u8 SD_WaitReady(void)
|
||
{
|
||
u32 t=0;
|
||
do
|
||
{
|
||
if(SD_SPI_ReadWriteByte(0XFF)==0XFF)return 0;//OK
|
||
t++;
|
||
}while(t<0XFFFFFFFF);//等待
|
||
return 1;
|
||
}
|
||
//等待SD卡回应
|
||
//Response:要得到的回应值
|
||
//返回值:0,成功得到了该回应值
|
||
// 其他,得到回应值失败
|
||
u8 SD_GetResponse(u8 Response)
|
||
{
|
||
u16 Count=0xFFFF;//等待次数
|
||
while ((SD_SPI_ReadWriteByte(0XFF)!=Response)&&Count)Count--;//等待得到准确的回应
|
||
if (Count==0)return MSD_RESPONSE_FAILURE;//得到回应失败
|
||
else return MSD_RESPONSE_NO_ERROR;//正确回应
|
||
}
|
||
//从sd卡读取一个数据包的内容
|
||
//buf:数据缓存区
|
||
//len:要读取的数据长度.
|
||
//返回值:0,成功;其他,失败;
|
||
u8 SD_RecvData(u8*buf,u16 len)
|
||
{
|
||
if(SD_GetResponse(0xFE))return 1;//等待SD卡发回数据起始令牌0xFE
|
||
while(len--)//开始接收数据
|
||
{
|
||
*buf=SPI1_ReadWriteByte(0xFF);
|
||
buf++;
|
||
}
|
||
//下面是2个伪CRC(dummy CRC)
|
||
SD_SPI_ReadWriteByte(0xFF);
|
||
SD_SPI_ReadWriteByte(0xFF);
|
||
return 0;//读取成功
|
||
}
|
||
//向sd卡写入一个数据包的内容 512字节
|
||
//buf:数据缓存区
|
||
//cmd:指令
|
||
//返回值:0,成功;其他,失败;
|
||
u8 SD_SendBlock(u8*buf,u8 cmd)
|
||
{
|
||
u16 t;
|
||
if(SD_WaitReady())return 1;//等待准备失效
|
||
SD_SPI_ReadWriteByte(cmd);
|
||
if(cmd!=0XFD)//不是结束指令
|
||
{
|
||
for(t=0;t<512;t++)SPI1_ReadWriteByte(buf[t]);//提高速度,减少函数传参时间
|
||
SD_SPI_ReadWriteByte(0xFF);//忽略crc
|
||
SD_SPI_ReadWriteByte(0xFF);
|
||
t=SD_SPI_ReadWriteByte(0xFF);//接收响应
|
||
if((t&0x1F)!=0x05)return 2;//响应错误
|
||
}
|
||
return 0;//写入成功
|
||
}
|
||
|
||
//向SD卡发送一个命令
|
||
//输入: u8 cmd 命令
|
||
// u32 arg 命令参数
|
||
// u8 crc crc校验值
|
||
//返回值:SD卡返回的响应
|
||
u8 SD_SendCmd(u8 cmd, u32 arg, u8 crc)
|
||
{
|
||
u8 r1;
|
||
u8 Retry=0;
|
||
SD_DisSelect();//取消上次片选
|
||
|
||
if(SD_Select())
|
||
{
|
||
return 0XFF;//片选失效
|
||
}
|
||
//发送
|
||
SD_SPI_ReadWriteByte(cmd | 0x40);//分别写入命令
|
||
SD_SPI_ReadWriteByte(arg >> 24);
|
||
SD_SPI_ReadWriteByte(arg >> 16);
|
||
SD_SPI_ReadWriteByte(arg >> 8);
|
||
SD_SPI_ReadWriteByte(arg);
|
||
SD_SPI_ReadWriteByte(crc);
|
||
if(cmd==CMD12)SD_SPI_ReadWriteByte(0xff);//Skip a stuff byte when stop reading
|
||
//等待响应,或超时退出
|
||
Retry=0XFF;
|
||
do
|
||
{
|
||
r1=SD_SPI_ReadWriteByte(0xFF);
|
||
}while((r1&0X80) && Retry--);
|
||
|
||
//返回状态值
|
||
return r1;
|
||
}
|
||
//获取SD卡的CID信息,包括制造商信息
|
||
//输入: u8 *cid_data(存放CID的内存,至少16Byte)
|
||
//返回值:0:NO_ERR
|
||
// 1:错误
|
||
u8 SD_GetCID(u8 *cid_data)
|
||
{
|
||
u8 r1;
|
||
//发CMD10命令,读CID
|
||
r1=SD_SendCmd(CMD10,0,0x01);
|
||
if(r1==0x00)
|
||
{
|
||
r1=SD_RecvData(cid_data,16);//接收16个字节的数据
|
||
}
|
||
SD_DisSelect();//取消片选
|
||
if(r1)return 1;
|
||
else return 0;
|
||
}
|
||
//获取SD卡的CSD信息,包括容量和速度信息
|
||
//输入:u8 *cid_data(存放CID的内存,至少16Byte)
|
||
//返回值:0:NO_ERR
|
||
// 1:错误
|
||
u8 SD_GetCSD(u8 *csd_data)
|
||
{
|
||
u8 r1;
|
||
r1=SD_SendCmd(CMD9,0,0x01);//发CMD9命令,读CSD
|
||
if(r1==0)
|
||
{
|
||
r1=SD_RecvData(csd_data, 16);//接收16个字节的数据
|
||
}
|
||
SD_DisSelect();//取消片选
|
||
if(r1)return 1;
|
||
else return 0;
|
||
}
|
||
//获取SD卡的总扇区数(扇区数)
|
||
//返回值:0: 取容量出错
|
||
// 其他:SD卡的容量(扇区数/512字节)
|
||
//每扇区的字节数必为512,因为如果不是512,则初始化不能通过.
|
||
u32 SD_GetSectorCount(void)
|
||
{
|
||
u8 csd[16];
|
||
u32 Capacity;
|
||
u8 n;
|
||
u16 csize;
|
||
//取CSD信息,如果期间出错,返回0
|
||
if(SD_GetCSD(csd)!=0) return 0;
|
||
//如果为SDHC卡,按照下面方式计算
|
||
if((csd[0]&0xC0)==0x40) //V2.00的卡
|
||
{
|
||
csize = csd[9] + ((u16)csd[8] << 8) + 1;
|
||
Capacity = (u32)csize << 10;//得到扇区数
|
||
}else//V1.XX的卡
|
||
{
|
||
n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2;
|
||
csize = (csd[8] >> 6) + ((u16)csd[7] << 2) + ((u16)(csd[6] & 3) << 10) + 1;
|
||
Capacity= (u32)csize << (n - 9);//得到扇区数
|
||
}
|
||
return Capacity;
|
||
}
|
||
//初始化SD卡
|
||
u8 SD_Init(void)
|
||
{
|
||
u8 r1; // 存放SD卡的返回值
|
||
u16 retry; // 用来进行超时计数
|
||
u8 buf[4];
|
||
u16 i;
|
||
|
||
SD_SPI_Init(); //初始化IO
|
||
SD_SPI_SpeedLow(); //设置到低速模式
|
||
SD_Select();
|
||
|
||
for(i=0;i<0xf00;i++);
|
||
for(i=0;i<10;i++)SD_SPI_ReadWriteByte(0XFF);//发送最少74个脉冲
|
||
retry=200;
|
||
|
||
do
|
||
{
|
||
r1=SD_SendCmd(CMD0,0,0x95);//进入IDLE状态
|
||
}while((r1!=0X01) && retry--);
|
||
|
||
SD_Type=0;//默认无卡
|
||
if(r1==0X01)
|
||
{
|
||
if(SD_SendCmd(CMD8,0x1AA,0x87)==1)//SD V2.0
|
||
{
|
||
for(i=0;i<4;i++)buf[i]=SD_SPI_ReadWriteByte(0XFF); //Get trailing return value of R7 resp
|
||
if(buf[2]==0X01&&buf[3]==0XAA)//卡是否支持2.7~3.6V
|
||
{
|
||
retry=0XFFFE;
|
||
do
|
||
{
|
||
SD_SendCmd(CMD55,0,0X01); //发送CMD55
|
||
r1=SD_SendCmd(CMD41,0x40000000,0X01);//发送CMD41
|
||
}while(r1&&retry--);
|
||
if(retry&&SD_SendCmd(CMD58,0,0X01)==0)//鉴别SD2.0卡版本开始
|
||
{
|
||
for(i=0;i<4;i++)buf[i]=SD_SPI_ReadWriteByte(0XFF);//得到OCR值
|
||
if(buf[0]&0x40)SD_Type=SD_TYPE_V2HC; //检查CCS
|
||
else SD_Type=SD_TYPE_V2;
|
||
}
|
||
}
|
||
}
|
||
else//SD V1.x/ MMC V3
|
||
{
|
||
SD_SendCmd(CMD55,0,0X01); //发送CMD55
|
||
r1=SD_SendCmd(CMD41,0,0X01); //发送CMD41
|
||
if(r1<=1)
|
||
{
|
||
SD_Type=SD_TYPE_V1;
|
||
retry=0XFFFE;
|
||
do //等待退出IDLE模式
|
||
{
|
||
SD_SendCmd(CMD55,0,0X01); //发送CMD55
|
||
r1=SD_SendCmd(CMD41,0,0X01);//发送CMD41
|
||
}while(r1&&retry--);
|
||
}else
|
||
{
|
||
SD_Type=SD_TYPE_MMC;//MMC V3
|
||
retry=0XFFFE;
|
||
do //等待退出IDLE模式
|
||
{
|
||
r1=SD_SendCmd(CMD1,0,0X01);//发送CMD1
|
||
}while(r1&&retry--);
|
||
}
|
||
if(retry==0||SD_SendCmd(CMD16,512,0X01)!=0)SD_Type=SD_TYPE_ERR;//错误的卡
|
||
}
|
||
}
|
||
SD_DisSelect();//取消片选
|
||
SD_SPI_SpeedHigh();//高速
|
||
if(SD_Type)return 0;
|
||
else if(r1)return r1;
|
||
return 0xaa;//其他错误
|
||
}
|
||
//读SD卡
|
||
//buf:数据缓存区
|
||
//sector:扇区
|
||
//cnt:扇区数
|
||
//返回值:0,ok;其他,失败.
|
||
u8 SD_ReadDisk(u8*buf,u32 sector,u8 cnt)
|
||
{
|
||
u8 r1;
|
||
if(SD_Type!=SD_TYPE_V2HC)sector <<= 9;//转换为字节地址
|
||
if(cnt==1)
|
||
{
|
||
r1=SD_SendCmd(CMD17,sector,0X01);//读命令
|
||
if(r1==0)//指令发送成功
|
||
{
|
||
r1=SD_RecvData(buf,512);//接收512个字节
|
||
}
|
||
}else
|
||
{
|
||
r1=SD_SendCmd(CMD18,sector,0X01);//连续读命令
|
||
do
|
||
{
|
||
r1=SD_RecvData(buf,512);//接收512个字节
|
||
buf+=512;
|
||
}while(--cnt && r1==0);
|
||
SD_SendCmd(CMD12,0,0X01); //发送停止命令
|
||
}
|
||
SD_DisSelect();//取消片选
|
||
return r1;//
|
||
}
|
||
//写SD卡
|
||
//buf:数据缓存区
|
||
//sector:起始扇区
|
||
//cnt:扇区数
|
||
//返回值:0,ok;其他,失败.
|
||
u8 SD_WriteDisk(u8*buf,u32 sector,u8 cnt)
|
||
{
|
||
u8 r1;
|
||
if(SD_Type!=SD_TYPE_V2HC)sector *= 512;//转换为字节地址
|
||
if(cnt==1)
|
||
{
|
||
r1=SD_SendCmd(CMD24,sector,0X01);//读命令
|
||
if(r1==0)//指令发送成功
|
||
{
|
||
r1=SD_SendBlock(buf,0xFE);//写512个字节
|
||
}
|
||
}else
|
||
{
|
||
if(SD_Type!=SD_TYPE_MMC)
|
||
{
|
||
SD_SendCmd(CMD55,0,0X01);
|
||
SD_SendCmd(CMD23,cnt,0X01);//发送指令
|
||
}
|
||
r1=SD_SendCmd(CMD25,sector,0X01);//连续读命令
|
||
if(r1==0)
|
||
{
|
||
do
|
||
{
|
||
r1=SD_SendBlock(buf,0xFC);//接收512个字节
|
||
buf+=512;
|
||
}while(--cnt && r1==0);
|
||
r1=SD_SendBlock(0,0xFD);//接收512个字节
|
||
}
|
||
}
|
||
SD_DisSelect();//取消片选
|
||
return r1;//
|
||
}
|
||
|
||
|
||
void OpenSDFile(void)
|
||
{
|
||
//f_mount(&fs[0],"0:",1); //挂载SD卡
|
||
//f_open(&Data_file,"0:/User_Data.txt",FA_OPEN_ALWAYS | FA_READ | FA_WRITE );//创建或打开一个文本文档保存数据
|
||
}
|
||
|
||
|
||
|
||
//升级命令
|
||
|
||
FATFS fs[2]; // Work area (file system object) for logical drive
|
||
|
||
int CheckUpload(void)
|
||
{
|
||
//FATFS fs; // Work area (file system object) for logical drive
|
||
FIL fsrc; // file objects
|
||
FIL testFile;
|
||
FRESULT res; // FatFs function common result code
|
||
UINT br = 0; // File R/W count
|
||
BYTE buffer[32]; // file copy buffer
|
||
char *p;
|
||
res = f_mount(&fs[0],"0:",1); //??SD?
|
||
Delay(5);
|
||
res = f_open(&fsrc, "0:/UploadConfig.txt", FA_OPEN_EXISTING | FA_READ); /* ??????????????? */
|
||
if( res == FR_OK )
|
||
{
|
||
while(1)
|
||
{
|
||
res = f_read(&fsrc, buffer, 8, &br); //?????8????
|
||
if (res || br == 0) break; /* ??????? */
|
||
}
|
||
p=strstr((char *)buffer,"state=1");
|
||
if(p==NULL)
|
||
goto error;
|
||
}
|
||
else
|
||
{
|
||
goto error;
|
||
}
|
||
f_close(&fsrc); // ????
|
||
//f_mount(0, "0:/", 1);
|
||
f_mount(0, NULL,1);
|
||
return 0;
|
||
|
||
error:
|
||
f_close(&fsrc); // ????
|
||
//f_mount(0, "0:/", 1);
|
||
f_mount(0, NULL,1);
|
||
return -1;
|
||
}
|
||
|
||
//-----------------清除flash---------------------
|
||
|
||
#define FLASH_PAGE_SIZE ((uint16_t)0x400) //?????1K??
|
||
#define WRITE_START_ADDR ((uint32_t)0x08008000)//???????
|
||
#define WRITE_END_ADDR ((uint32_t)0x08018000)//????
|
||
static uint32_t EraseCounter = 0x00, Address = 0x00;//????,????
|
||
|
||
static uint32_t NbrOfPage = 0x00;//????????
|
||
static volatile FLASH_Status FLASHStatus = FLASH_COMPLETE;/*FLASH??????*/
|
||
void clearFlash(void)
|
||
{
|
||
/*??FLASH*/
|
||
FLASH_Unlock();
|
||
/*??????FLASH???? */
|
||
NbrOfPage = (WRITE_END_ADDR - WRITE_START_ADDR) / FLASH_PAGE_SIZE;
|
||
/* ????????? */
|
||
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
|
||
/* ??FLASH ?*/
|
||
for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++)
|
||
{
|
||
FLASHStatus = FLASH_ErasePage(WRITE_START_ADDR + (FLASH_PAGE_SIZE * EraseCounter));
|
||
}
|
||
// /* ??FLASH */
|
||
// Address = WRITE_START_ADDR;
|
||
// while((Address < WRITE_END_ADDR) && (FLASHStatus == FLASH_COMPLETE))
|
||
// {
|
||
// FLASHStatus = FLASH_ProgramWord(Address, Data);
|
||
// Address = Address + 4;
|
||
// }
|
||
/* ??FLASH */
|
||
// FLASH_Lock();
|
||
}
|
||
|
||
//------------------------------清除flash---------------------------
|
||
|
||
|
||
void LED_GPIO_Config(void)
|
||
{
|
||
GPIO_InitTypeDef GPIO_InitStructure;
|
||
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA, ENABLE);
|
||
RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO, ENABLE);
|
||
|
||
// GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 ;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
|
||
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
||
|
||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
|
||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
|
||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||
|
||
GPIO_SetBits(GPIOB,GPIO_Pin_4); // turn off all led
|
||
GPIO_SetBits(GPIOA,GPIO_Pin_15); // turn off all led
|
||
}
|
||
|
||
|
||
#include "led.h"
|
||
#include "bsp_cpu_flash.h"
|
||
#define ONEFREAM (2048)
|
||
static uint8_t UploadFileData[ONEFREAM];
|
||
static uint8_t LEDBlinkFlag = 0;
|
||
static uint32_t flashaddr = 0;
|
||
FRESULT res22; // FatFs function common result code
|
||
int programdata(void)
|
||
{
|
||
|
||
FRESULT res; // FatFs function common result code
|
||
static UINT br = 0; // File R/W count
|
||
static uint32_t data_len = 0;
|
||
static uint16_t i=0;
|
||
|
||
static UINT br1 = 0; // File R/W count
|
||
FIL fsrc ,fsrc1;; // file objects
|
||
|
||
|
||
br = 0;
|
||
res = f_mount(&fs[0],"0:",1); //??SD?
|
||
// res = f_mount(&fs[1],"1:",1); //??SD?
|
||
Delay(5);
|
||
res22 = f_open(&fsrc, "0:/App.bin", FA_OPEN_EXISTING | FA_READ); //FA_OPEN_ALWAYS FA_OPEN_EXISTING
|
||
res22 = f_open(&fsrc, "0:/Test.txt", FA_OPEN_EXISTING | FA_READ); //FA_OPEN_ALWAYS FA_OPEN_EXISTING
|
||
res22 = f_open(&fsrc, "0:/Test.bin", FA_OPEN_EXISTING | FA_READ); //FA_OPEN_ALWAYS FA_OPEN_EXISTING
|
||
|
||
if(res)
|
||
{
|
||
f_close(&fsrc); // ????
|
||
f_mount(0, "0:/", 1);
|
||
// DEPRINT("open App.bin fail\r\n");
|
||
return -1;
|
||
}
|
||
Delay(5);
|
||
data_len = f_size(&fsrc);
|
||
|
||
|
||
//f_unlink("1:/test.bin"); //?????
|
||
res = f_open(&fsrc1 , "0:/test.bin" , FA_CREATE_ALWAYS | FA_READ | FA_WRITE ); //FA_CREATE_ALWAYS FA_CREATE_NEW
|
||
if(res)
|
||
{
|
||
f_close(&fsrc1); // ????
|
||
f_mount(0, "1:/", 1);
|
||
// DEPRINT("open test.bin fail\r\n");
|
||
return -1;
|
||
}
|
||
|
||
|
||
clearFlash();
|
||
|
||
|
||
|
||
|
||
|
||
|
||
i = 0;
|
||
while(1)
|
||
{
|
||
LEDBlinkFlag = !LEDBlinkFlag;
|
||
if(LEDBlinkFlag)
|
||
{
|
||
// DC_A_RUN_CT_CTL(0);
|
||
MCU_LED(0);
|
||
}
|
||
else
|
||
{
|
||
// DC_A_RUN_CT_CTL(1);
|
||
MCU_LED(1);
|
||
}
|
||
|
||
|
||
flashaddr = ApplicationAddress + i * ONEFREAM;
|
||
memset(UploadFileData,0,sizeof(UploadFileData));
|
||
|
||
res = f_read(&fsrc, UploadFileData, ONEFREAM, &br);
|
||
if(res)
|
||
break; /* ??????? */
|
||
if(br == 0)
|
||
break;
|
||
if( br < ONEFREAM )
|
||
{
|
||
//Delay_MS(5);
|
||
bsp_WriteCpuFlash(ApplicationAddress + i * ONEFREAM, UploadFileData, br);
|
||
// InsideFlashWrite(ApplicationAddress + i * ONEFREAM, UploadFileData, br);
|
||
// printf("write last times\r\n");
|
||
}
|
||
|
||
//Delay_MS(5);
|
||
res = f_write(&fsrc1, UploadFileData, br, &br1); // ???
|
||
f_sync(&fsrc1); //?????
|
||
if(br == ONEFREAM)
|
||
bsp_WriteCpuFlash(ApplicationAddress + i * ONEFREAM, UploadFileData, br);
|
||
i++;
|
||
|
||
// WatchDog_Feed(); //??
|
||
}
|
||
|
||
f_close(&fsrc1); // ????
|
||
|
||
f_close(&fsrc); // ????
|
||
f_mount(0, "0:/", 1);
|
||
f_mount(0, "1:/", 1);
|
||
return 0;
|
||
}
|
||
|
||
|
||
void removeupload(void)
|
||
{
|
||
//FATFS fs; // Work area (file system object) for logical drive
|
||
//FIL fsrc; // file objects
|
||
FRESULT res; // FatFs function common result code
|
||
|
||
res = f_mount(&fs[0],"0:",1); //??SD?
|
||
f_unlink("0:/UploadConfig.txt"); //?????
|
||
f_mount(0, "0:/", 1);
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
log_handle_t log_handle;
|
||
|
||
|
||
// FRESULT res; // FatFs function common result code
|
||
void User_Write_Debug(char *loginfo, int loglen)
|
||
{
|
||
FATFS fs; // Work aea (file system object) for logical drive
|
||
FIL fsrc; // file objects
|
||
FRESULT res; // FatFs function common result code
|
||
UINT br; // File R/W count
|
||
uint8_t uwHours=0;
|
||
uint8_t uwMinutes = 0;
|
||
uint8_t uwSeconds = 0;
|
||
uint8_t uwYears= 0;
|
||
uint8_t uwMonths= 0;
|
||
uint8_t uwDays=0 ;
|
||
uint32_t totalkB = 0;
|
||
uint32_t freekB = 0;
|
||
char logfile[60] = {0};
|
||
|
||
|
||
static u8 sd_state =0;
|
||
static u8 sd_pre_state =1;
|
||
sd_state = SD_CD_INSERT();
|
||
|
||
|
||
if(Bit_RESET == sd_state) //??SD?
|
||
{
|
||
if(sd_pre_state != sd_state) //???
|
||
{
|
||
sd_pre_state = sd_state;
|
||
if(SD_Init())
|
||
{
|
||
|
||
}
|
||
else
|
||
{
|
||
filesys_init();
|
||
Delay(100);
|
||
fileSys__freeSpace("0:", &totalkB, &freekB);
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
return;
|
||
}
|
||
|
||
// last_day = stRTDB.uwDays - 2;
|
||
// last_month = stRTDB.uwMonths;
|
||
// last_year = stRTDB.uwYears;
|
||
|
||
|
||
res = f_mount(&fs, "0:/", 1);
|
||
if (res != FR_OK) return;
|
||
|
||
//按天生成日志
|
||
//文件大小超过1M,将重新进行截取
|
||
//只保存两天文件
|
||
// stRTDB.uwHours = sysTime.hours;
|
||
// stRTDB.uwMinutes = sysTime.minutes;
|
||
// stRTDB.uwSeconds = sysTime.seconds;
|
||
|
||
|
||
// last_day = stRTDB.uwDays - 2;
|
||
// last_month = stRTDB.uwMonths;
|
||
// last_year = stRTDB.uwYears;
|
||
// if (last_day <= 0) {
|
||
// last_month-- ;
|
||
// if (last_month <= 0) {
|
||
// last_month = 12;
|
||
// last_year--;
|
||
// }
|
||
// }
|
||
// sprintf(logfile, "%s-%d-%02d-%02d-log.txt", (char*)stFactoryInfo.sn, last_year, last_month, last_day);
|
||
// f_unlink(logfile);
|
||
|
||
// char *pstr, *conf_file = "0:/factory_setting.txt";
|
||
// sprintf(logfile, "Demo.txt");
|
||
// sprintf(logfile, "Demo.txt");
|
||
sprintf(logfile, "%d-%02d-%02d-log.txt", uwYears, uwMonths, uwDays);
|
||
res = f_open(&fsrc, logfile, FA_OPEN_EXISTING| FA_WRITE|FA_READ);
|
||
if (res == FR_NO_FILE)
|
||
res = f_open(&fsrc , logfile , FA_CREATE_ALWAYS | FA_WRITE|FA_READ);
|
||
if (res != FR_OK) goto err;
|
||
if (f_size(&fsrc) >= 1024*1024)
|
||
{
|
||
res = f_lseek(&fsrc, 0); //定位到起始
|
||
res = f_truncate(&fsrc); //函数截断文件到当前的文件读/写指针
|
||
}
|
||
else
|
||
{
|
||
res = f_lseek(&fsrc, f_size(&fsrc)); //定位到文件末尾
|
||
}
|
||
if (res != FR_OK) goto err;
|
||
res = f_write(&fsrc, loginfo, loglen, &br); // 写数据
|
||
if (res != FR_OK)
|
||
goto err;
|
||
err:
|
||
|
||
f_close(&fsrc); // 关闭文件
|
||
f_mount(0, "0:/", 1);
|
||
// f_mount(&fs, "0:/", 1);
|
||
}
|
||
|
||
|
||
extern volatile uint32_t SystemTimeHeart;
|
||
void log_printf(char* fmt,...)
|
||
{
|
||
u16 length;
|
||
va_list ap;
|
||
char tmp[20];
|
||
static uint8_t sd_state = 0;
|
||
u8 LOG_DEBUG_TX_BUF[LOG_DEBUG_TX_SIZE];
|
||
u16 loglength;
|
||
uint8_t uwHours =0;
|
||
uint8_t uwMinutes=0;
|
||
uint8_t uwSeconds=0;
|
||
uint8_t uwYears=0;
|
||
uint8_t uwMonths=0;
|
||
uint8_t uwDays =0 ;
|
||
|
||
// if (SD_STATE == 0)
|
||
// {
|
||
// if (sd_state == 0)
|
||
// {
|
||
// sd_state = 1;
|
||
// SD_Init();
|
||
// //fileSys__freeSpace("0:", &disk1_space, &disk2_space);
|
||
// }
|
||
// }
|
||
// else
|
||
// {
|
||
// sd_state = 0;
|
||
// return;
|
||
// }
|
||
|
||
memset(LOG_DEBUG_TX_BUF, 0, sizeof(LOG_DEBUG_TX_BUF));
|
||
|
||
sprintf(tmp, "[%02d:%02d:%02d.%03d]", 0, 0, 0, SystemTimeHeart%1000);
|
||
User_Write_Debug(tmp, strlen(tmp));
|
||
|
||
// va_start(ap,fmt);
|
||
// vsnprintf((char*)LOG_DEBUG_TX_BUF, LOG_DEBUG_TX_SIZE, fmt, ap);
|
||
// va_end(ap);
|
||
|
||
// length=strlen((const char*)LOG_DEBUG_TX_BUF);
|
||
// User_Write_Debug((char*)LOG_DEBUG_TX_BUF, length);
|
||
// User_Write_Debug("\r\n", 2);
|
||
}
|
||
|
||
int check_log_file(void)
|
||
{
|
||
// FATFS fs; // Work aea (file system object) for logical drive
|
||
// FIL fsrc; // file objects
|
||
// FRESULT res; // FatFs function common result code
|
||
//
|
||
// if (FaultStateD.SD_Card_Fault_Flag != 'N')
|
||
// return -1;
|
||
// sprintf(log_handle.file_name, "%s-%d-%02d-%02d-log.txt", CPIndentifier, SysTime.Year, SysTime.Month, SysTime.Day);
|
||
// res = f_mount(&fs, "0:/", 1);
|
||
// if (res != FR_OK) return -1;
|
||
// res = f_open(&fsrc, log_handle.file_name, FA_OPEN_EXISTING | FA_READ);
|
||
// if (res != FR_OK)
|
||
// {
|
||
// f_mount(&fs, "0:/", 0);
|
||
// return -1;
|
||
// }
|
||
// log_handle.file_length = f_size(&fsrc);
|
||
// f_close(&fsrc); // 关闭文件
|
||
// f_mount(&fs, "0:/", 0);
|
||
// return 0;
|
||
}
|
||
|
||
int read_log_file(void)
|
||
{
|
||
|
||
return 0;
|
||
}
|
||
|
||
int log_file_handler(void)
|
||
{
|
||
|
||
return -1;
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|