807 lines
18 KiB
C
807 lines
18 KiB
C
|
/*-------------------------------------------------*/
|
|||
|
/* SD<53><44>Դ<EFBFBD>ļ<EFBFBD> */
|
|||
|
/*-------------------------------------------------*/
|
|||
|
/*-------------------------------------------------*/
|
|||
|
#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<53><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
void Delay(__IO uint32_t nCount)
|
|||
|
{
|
|||
|
while(nCount--);
|
|||
|
|
|||
|
}
|
|||
|
////////////////////////////////////<2F><>ֲ<EFBFBD><EFBFBD><DEB8><EFBFBD>///////////////////////////////////
|
|||
|
//<2F><>ֲʱ<D6B2><CAB1><EFBFBD>Ľӿ<C4BD>
|
|||
|
//data:Ҫд<D2AA><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//<2F><><EFBFBD><EFBFBD>ֵ:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
u8 SD_SPI_ReadWriteByte(u8 data)
|
|||
|
{
|
|||
|
return SPI1_ReadWriteByte(data);
|
|||
|
}
|
|||
|
//SD<53><44><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>,<2C><>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
|
|||
|
void SD_SPI_SpeedLow(void)
|
|||
|
{
|
|||
|
SPI1_SetSpeed(SPI_BaudRatePrescaler_256);//<2F><><EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
|||
|
}
|
|||
|
//SD<53><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>,<2C><><EFBFBD>Ը<EFBFBD><D4B8><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
void SD_SPI_SpeedHigh(void)
|
|||
|
{
|
|||
|
SPI1_SetSpeed(SPI_BaudRatePrescaler_8);//<2F><><EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD>ģʽ SPI_BaudRatePrescaler_2
|
|||
|
}
|
|||
|
//SPIӲ<49><D3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
|
|||
|
void SD_SPI_Init(void)
|
|||
|
{
|
|||
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE );//PORTBʱ<42><CAB1>ʹ<EFBFBD><CAB9>
|
|||
|
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE );//PORTBʱ<42><CAB1>ʹ<EFBFBD><CAB9>
|
|||
|
GPIO_InitStructure.GPIO_Pin = SD_CS_PIN;
|
|||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;//GPIO_Mode_Out_PP; //PB13/14/15<31><35><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|||
|
GPIO_Init(SD_CS_PORT, &GPIO_InitStructure);//<2F><>ʼ<EFBFBD><CABC>GPIOB
|
|||
|
|
|||
|
SPI1_Init();
|
|||
|
SD_CS_CTL(1);
|
|||
|
}
|
|||
|
///////////////////////////////////////////////////////////////////////////////////
|
|||
|
//ȡ<><C8A1>ѡ<EFBFBD><D1A1>,<2C>ͷ<EFBFBD>SPI<50><49><EFBFBD><EFBFBD>
|
|||
|
void SD_DisSelect(void)
|
|||
|
{
|
|||
|
SD_CS_CTL(1);
|
|||
|
SD_SPI_ReadWriteByte(0xff);//<2F>ṩ<EFBFBD><E1B9A9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>8<EFBFBD><38>ʱ<EFBFBD><CAB1>
|
|||
|
}
|
|||
|
//ѡ<><D1A1>sd<73><64>,<2C><><EFBFBD>ҵȴ<D2B5><C8B4><EFBFBD><EFBFBD><D7BC>OK
|
|||
|
//<2F><><EFBFBD><EFBFBD>ֵ:0,<2C>ɹ<EFBFBD>;1,ʧ<><CAA7>;
|
|||
|
u8 SD_Select(void)
|
|||
|
{
|
|||
|
SD_CS_CTL(0);
|
|||
|
if(SD_WaitReady()==0)return 0;//<2F>ȴ<EFBFBD><C8B4>ɹ<EFBFBD>
|
|||
|
SD_DisSelect();
|
|||
|
return 1;//<2F>ȴ<EFBFBD>ʧ<EFBFBD><CAA7>
|
|||
|
}
|
|||
|
//<2F>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><D7BC><EFBFBD><EFBFBD>
|
|||
|
//<2F><><EFBFBD><EFBFBD>ֵ:0,<><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;<3B><><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
u8 SD_WaitReady(void)
|
|||
|
{
|
|||
|
u32 t=0;
|
|||
|
do
|
|||
|
{
|
|||
|
if(SD_SPI_ReadWriteByte(0XFF)==0XFF)return 0;//OK
|
|||
|
t++;
|
|||
|
}while(t<0XFFFFFFFF);//<2F>ȴ<EFBFBD>
|
|||
|
return 1;
|
|||
|
}
|
|||
|
//<2F>ȴ<EFBFBD>SD<53><44><EFBFBD><EFBFBD>Ӧ
|
|||
|
//Response:Ҫ<>õ<EFBFBD><C3B5>Ļ<EFBFBD>Ӧֵ
|
|||
|
//<2F><><EFBFBD><EFBFBD>ֵ:0,<2C>ɹ<EFBFBD><C9B9>õ<EFBFBD><C3B5>˸û<CBB8>Ӧֵ
|
|||
|
// <20><><EFBFBD><EFBFBD>,<2C>õ<EFBFBD><C3B5><EFBFBD>Ӧֵʧ<D6B5><CAA7>
|
|||
|
u8 SD_GetResponse(u8 Response)
|
|||
|
{
|
|||
|
u16 Count=0xFFFF;//<2F>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
while ((SD_SPI_ReadWriteByte(0XFF)!=Response)&&Count)Count--;//<2F>ȴ<EFBFBD><C8B4>õ<EFBFBD>ȷ<D7BC>Ļ<EFBFBD>Ӧ
|
|||
|
if (Count==0)return MSD_RESPONSE_FAILURE;//<2F>õ<EFBFBD><C3B5><EFBFBD>Ӧʧ<D3A6><CAA7>
|
|||
|
else return MSD_RESPONSE_NO_ERROR;//<2F><>ȷ<EFBFBD><C8B7>Ӧ
|
|||
|
}
|
|||
|
//<2F><>sd<73><64><EFBFBD><EFBFBD>ȡһ<C8A1><D2BB><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD><DDB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//buf:<3A><><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//len:Ҫ<><D2AA>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>.
|
|||
|
//<2F><><EFBFBD><EFBFBD>ֵ:0,<2C>ɹ<EFBFBD>;<3B><><EFBFBD><EFBFBD>,ʧ<><CAA7>;
|
|||
|
u8 SD_RecvData(u8*buf,u16 len)
|
|||
|
{
|
|||
|
if(SD_GetResponse(0xFE))return 1;//<2F>ȴ<EFBFBD>SD<53><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>0xFE
|
|||
|
while(len--)//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
*buf=SPI1_ReadWriteByte(0xFF);
|
|||
|
buf++;
|
|||
|
}
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD><32>αCRC<52><43>dummy CRC<52><43>
|
|||
|
SD_SPI_ReadWriteByte(0xFF);
|
|||
|
SD_SPI_ReadWriteByte(0xFF);
|
|||
|
return 0;//<2F><>ȡ<EFBFBD>ɹ<EFBFBD>
|
|||
|
}
|
|||
|
//<2F><>sd<73><64>д<EFBFBD><D0B4>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD><DDB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 512<31>ֽ<EFBFBD>
|
|||
|
//buf:<3A><><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//cmd:ָ<><D6B8>
|
|||
|
//<2F><><EFBFBD><EFBFBD>ֵ:0,<2C>ɹ<EFBFBD>;<3B><><EFBFBD><EFBFBD>,ʧ<><CAA7>;
|
|||
|
u8 SD_SendBlock(u8*buf,u8 cmd)
|
|||
|
{
|
|||
|
u16 t;
|
|||
|
if(SD_WaitReady())return 1;//<2F>ȴ<EFBFBD><EFBFBD><D7BC>ʧЧ
|
|||
|
SD_SPI_ReadWriteByte(cmd);
|
|||
|
if(cmd!=0XFD)//<2F><><EFBFBD>ǽ<EFBFBD><C7BD><EFBFBD>ָ<EFBFBD><D6B8>
|
|||
|
{
|
|||
|
for(t=0;t<512;t++)SPI1_ReadWriteByte(buf[t]);//<2F><><EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD>,<2C><><EFBFBD>ٺ<EFBFBD><D9BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
SD_SPI_ReadWriteByte(0xFF);//<2F><><EFBFBD><EFBFBD>crc
|
|||
|
SD_SPI_ReadWriteByte(0xFF);
|
|||
|
t=SD_SPI_ReadWriteByte(0xFF);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ
|
|||
|
if((t&0x1F)!=0x05)return 2;//<2F><>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD>
|
|||
|
}
|
|||
|
return 0;//д<><D0B4><EFBFBD>ɹ<EFBFBD>
|
|||
|
}
|
|||
|
|
|||
|
//<2F><>SD<53><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//<2F><><EFBFBD><EFBFBD>: u8 cmd <20><><EFBFBD><EFBFBD>
|
|||
|
// u32 arg <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
// u8 crc crcУ<63><D0A3>ֵ
|
|||
|
//<2F><><EFBFBD><EFBFBD>ֵ:SD<53><44><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD>Ӧ
|
|||
|
u8 SD_SendCmd(u8 cmd, u32 arg, u8 crc)
|
|||
|
{
|
|||
|
u8 r1;
|
|||
|
u8 Retry=0;
|
|||
|
SD_DisSelect();//ȡ<><C8A1><EFBFBD>ϴ<EFBFBD>Ƭѡ
|
|||
|
|
|||
|
if(SD_Select())
|
|||
|
{
|
|||
|
return 0XFF;//ƬѡʧЧ
|
|||
|
}
|
|||
|
//<2F><><EFBFBD><EFBFBD>
|
|||
|
SD_SPI_ReadWriteByte(cmd | 0x40);//<2F>ֱ<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
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
|
|||
|
//<2F>ȴ<EFBFBD><C8B4><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>˳<EFBFBD>
|
|||
|
Retry=0XFF;
|
|||
|
do
|
|||
|
{
|
|||
|
r1=SD_SPI_ReadWriteByte(0xFF);
|
|||
|
}while((r1&0X80) && Retry--);
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD>״ֵ̬
|
|||
|
return r1;
|
|||
|
}
|
|||
|
//<2F><>ȡSD<53><44><EFBFBD><EFBFBD>CID<49><44>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
|||
|
//<2F><><EFBFBD><EFBFBD>: u8 *cid_data(<28><><EFBFBD><EFBFBD>CID<49><44><EFBFBD>ڴ棬<DAB4><E6A3AC><EFBFBD><EFBFBD>16Byte<74><65>
|
|||
|
//<2F><><EFBFBD><EFBFBD>ֵ:0<><30>NO_ERR
|
|||
|
// 1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
u8 SD_GetCID(u8 *cid_data)
|
|||
|
{
|
|||
|
u8 r1;
|
|||
|
//<2F><>CMD10<31><30><EFBFBD><EFBFBD><EEA3AC>CID
|
|||
|
r1=SD_SendCmd(CMD10,0,0x01);
|
|||
|
if(r1==0x00)
|
|||
|
{
|
|||
|
r1=SD_RecvData(cid_data,16);//<2F><><EFBFBD><EFBFBD>16<31><36><EFBFBD>ֽڵ<D6BD><DAB5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
}
|
|||
|
SD_DisSelect();//ȡ<><C8A1>Ƭѡ
|
|||
|
if(r1)return 1;
|
|||
|
else return 0;
|
|||
|
}
|
|||
|
//<2F><>ȡSD<53><44><EFBFBD><EFBFBD>CSD<53><44>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD><D9B6><EFBFBD>Ϣ
|
|||
|
//<2F><><EFBFBD><EFBFBD>:u8 *cid_data(<28><><EFBFBD><EFBFBD>CID<49><44><EFBFBD>ڴ棬<DAB4><E6A3AC><EFBFBD><EFBFBD>16Byte<74><65>
|
|||
|
//<2F><><EFBFBD><EFBFBD>ֵ:0<><30>NO_ERR
|
|||
|
// 1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
u8 SD_GetCSD(u8 *csd_data)
|
|||
|
{
|
|||
|
u8 r1;
|
|||
|
r1=SD_SendCmd(CMD9,0,0x01);//<2F><>CMD9<44><39><EFBFBD><EFBFBD><EEA3AC>CSD
|
|||
|
if(r1==0)
|
|||
|
{
|
|||
|
r1=SD_RecvData(csd_data, 16);//<2F><><EFBFBD><EFBFBD>16<31><36><EFBFBD>ֽڵ<D6BD><DAB5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
}
|
|||
|
SD_DisSelect();//ȡ<><C8A1>Ƭѡ
|
|||
|
if(r1)return 1;
|
|||
|
else return 0;
|
|||
|
}
|
|||
|
//<2F><>ȡSD<53><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//<2F><><EFBFBD><EFBFBD>ֵ:0<><30> ȡ<><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
// <20><><EFBFBD><EFBFBD>:SD<53><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>/512<31>ֽ<EFBFBD>)
|
|||
|
//ÿ<><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>Ϊ512<31><32><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>512<31><32><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>.
|
|||
|
u32 SD_GetSectorCount(void)
|
|||
|
{
|
|||
|
u8 csd[16];
|
|||
|
u32 Capacity;
|
|||
|
u8 n;
|
|||
|
u16 csize;
|
|||
|
//ȡCSD<53><44>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڼ<EFBFBD><DABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0
|
|||
|
if(SD_GetCSD(csd)!=0) return 0;
|
|||
|
//<2F><><EFBFBD><EFBFBD>ΪSDHC<48><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>淽ʽ<E6B7BD><CABD><EFBFBD><EFBFBD>
|
|||
|
if((csd[0]&0xC0)==0x40) //V2.00<EFBFBD>Ŀ<EFBFBD>
|
|||
|
{
|
|||
|
csize = csd[9] + ((u16)csd[8] << 8) + 1;
|
|||
|
Capacity = (u32)csize << 10;//<2F>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
}else//V1.XX<58>Ŀ<EFBFBD>
|
|||
|
{
|
|||
|
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);//<2F>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
}
|
|||
|
return Capacity;
|
|||
|
}
|
|||
|
//<2F><>ʼ<EFBFBD><CABC>SD<53><44>
|
|||
|
u8 SD_Init(void)
|
|||
|
{
|
|||
|
u8 r1; // <20><><EFBFBD><EFBFBD>SD<53><44><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD>ֵ
|
|||
|
u16 retry; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
|
|||
|
u8 buf[4];
|
|||
|
u16 i;
|
|||
|
|
|||
|
SD_SPI_Init(); //<2F><>ʼ<EFBFBD><CABC>IO
|
|||
|
SD_SPI_SpeedLow(); //<2F><><EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
|||
|
SD_Select();
|
|||
|
|
|||
|
for(i=0;i<0xf00;i++);
|
|||
|
for(i=0;i<10;i++)SD_SPI_ReadWriteByte(0XFF);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>74<37><34><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
retry=200;
|
|||
|
|
|||
|
do
|
|||
|
{
|
|||
|
r1=SD_SendCmd(CMD0,0,0x95);//<2F><><EFBFBD><EFBFBD>IDLE״̬
|
|||
|
}while((r1!=0X01) && retry--);
|
|||
|
|
|||
|
SD_Type=0;//Ĭ<><C4AC><EFBFBD><EFBFBD>
|
|||
|
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)//<2F><><EFBFBD>Ƿ<EFBFBD>֧<EFBFBD><D6A7>2.7~3.6V
|
|||
|
{
|
|||
|
retry=0XFFFE;
|
|||
|
do
|
|||
|
{
|
|||
|
SD_SendCmd(CMD55,0,0X01); //<2F><><EFBFBD><EFBFBD>CMD55
|
|||
|
r1=SD_SendCmd(CMD41,0x40000000,0X01);//<2F><><EFBFBD><EFBFBD>CMD41
|
|||
|
}while(r1&&retry--);
|
|||
|
if(retry&&SD_SendCmd(CMD58,0,0X01)==0)//<2F><><EFBFBD><EFBFBD>SD2.0<EFBFBD><EFBFBD><EFBFBD>汾<EFBFBD><EFBFBD>ʼ
|
|||
|
{
|
|||
|
for(i=0;i<4;i++)buf[i]=SD_SPI_ReadWriteByte(0XFF);//<2F>õ<EFBFBD>OCRֵ
|
|||
|
if(buf[0]&0x40)SD_Type=SD_TYPE_V2HC; //<2F><><EFBFBD><EFBFBD>CCS
|
|||
|
else SD_Type=SD_TYPE_V2;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
else//SD V1.x/ MMC V3
|
|||
|
{
|
|||
|
SD_SendCmd(CMD55,0,0X01); //<2F><><EFBFBD><EFBFBD>CMD55
|
|||
|
r1=SD_SendCmd(CMD41,0,0X01); //<2F><><EFBFBD><EFBFBD>CMD41
|
|||
|
if(r1<=1)
|
|||
|
{
|
|||
|
SD_Type=SD_TYPE_V1;
|
|||
|
retry=0XFFFE;
|
|||
|
do //<2F>ȴ<EFBFBD><C8B4>˳<EFBFBD>IDLEģʽ
|
|||
|
{
|
|||
|
SD_SendCmd(CMD55,0,0X01); //<2F><><EFBFBD><EFBFBD>CMD55
|
|||
|
r1=SD_SendCmd(CMD41,0,0X01);//<2F><><EFBFBD><EFBFBD>CMD41
|
|||
|
}while(r1&&retry--);
|
|||
|
}else
|
|||
|
{
|
|||
|
SD_Type=SD_TYPE_MMC;//MMC V3
|
|||
|
retry=0XFFFE;
|
|||
|
do //<2F>ȴ<EFBFBD><C8B4>˳<EFBFBD>IDLEģʽ
|
|||
|
{
|
|||
|
r1=SD_SendCmd(CMD1,0,0X01);//<2F><><EFBFBD><EFBFBD>CMD1
|
|||
|
}while(r1&&retry--);
|
|||
|
}
|
|||
|
if(retry==0||SD_SendCmd(CMD16,512,0X01)!=0)SD_Type=SD_TYPE_ERR;//<2F><><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD>
|
|||
|
}
|
|||
|
}
|
|||
|
SD_DisSelect();//ȡ<><C8A1>Ƭѡ
|
|||
|
SD_SPI_SpeedHigh();//<2F><><EFBFBD><EFBFBD>
|
|||
|
if(SD_Type)return 0;
|
|||
|
else if(r1)return r1;
|
|||
|
return 0xaa;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
}
|
|||
|
//<2F><>SD<53><44>
|
|||
|
//buf:<3A><><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//sector:<3A><><EFBFBD><EFBFBD>
|
|||
|
//cnt:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//<2F><><EFBFBD><EFBFBD>ֵ:0,ok;<3B><><EFBFBD><EFBFBD>,ʧ<><CAA7>.
|
|||
|
u8 SD_ReadDisk(u8*buf,u32 sector,u8 cnt)
|
|||
|
{
|
|||
|
u8 r1;
|
|||
|
if(SD_Type!=SD_TYPE_V2HC)sector <<= 9;//ת<><D7AA>Ϊ<EFBFBD>ֽڵ<D6BD>ַ
|
|||
|
if(cnt==1)
|
|||
|
{
|
|||
|
r1=SD_SendCmd(CMD17,sector,0X01);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if(r1==0)//ָ<><EFBFBD>ͳɹ<CDB3>
|
|||
|
{
|
|||
|
r1=SD_RecvData(buf,512);//<2F><><EFBFBD><EFBFBD>512<31><32><EFBFBD>ֽ<EFBFBD>
|
|||
|
}
|
|||
|
}else
|
|||
|
{
|
|||
|
r1=SD_SendCmd(CMD18,sector,0X01);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
do
|
|||
|
{
|
|||
|
r1=SD_RecvData(buf,512);//<2F><><EFBFBD><EFBFBD>512<31><32><EFBFBD>ֽ<EFBFBD>
|
|||
|
buf+=512;
|
|||
|
}while(--cnt && r1==0);
|
|||
|
SD_SendCmd(CMD12,0,0X01); //<2F><><EFBFBD><EFBFBD>ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD>
|
|||
|
}
|
|||
|
SD_DisSelect();//ȡ<><C8A1>Ƭѡ
|
|||
|
return r1;//
|
|||
|
}
|
|||
|
//дSD<53><44>
|
|||
|
//buf:<3A><><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//sector:<3A><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
|
|||
|
//cnt:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//<2F><><EFBFBD><EFBFBD>ֵ:0,ok;<3B><><EFBFBD><EFBFBD>,ʧ<><CAA7>.
|
|||
|
u8 SD_WriteDisk(u8*buf,u32 sector,u8 cnt)
|
|||
|
{
|
|||
|
u8 r1;
|
|||
|
if(SD_Type!=SD_TYPE_V2HC)sector *= 512;//ת<><D7AA>Ϊ<EFBFBD>ֽڵ<D6BD>ַ
|
|||
|
if(cnt==1)
|
|||
|
{
|
|||
|
r1=SD_SendCmd(CMD24,sector,0X01);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if(r1==0)//ָ<><EFBFBD>ͳɹ<CDB3>
|
|||
|
{
|
|||
|
r1=SD_SendBlock(buf,0xFE);//д512<31><32><EFBFBD>ֽ<EFBFBD>
|
|||
|
}
|
|||
|
}else
|
|||
|
{
|
|||
|
if(SD_Type!=SD_TYPE_MMC)
|
|||
|
{
|
|||
|
SD_SendCmd(CMD55,0,0X01);
|
|||
|
SD_SendCmd(CMD23,cnt,0X01);//<2F><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>
|
|||
|
}
|
|||
|
r1=SD_SendCmd(CMD25,sector,0X01);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if(r1==0)
|
|||
|
{
|
|||
|
do
|
|||
|
{
|
|||
|
r1=SD_SendBlock(buf,0xFC);//<2F><><EFBFBD><EFBFBD>512<31><32><EFBFBD>ֽ<EFBFBD>
|
|||
|
buf+=512;
|
|||
|
}while(--cnt && r1==0);
|
|||
|
r1=SD_SendBlock(0,0xFD);//<2F><><EFBFBD><EFBFBD>512<31><32><EFBFBD>ֽ<EFBFBD>
|
|||
|
}
|
|||
|
}
|
|||
|
SD_DisSelect();//ȡ<><C8A1>Ƭѡ
|
|||
|
return r1;//
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
void OpenSDFile(void)
|
|||
|
{
|
|||
|
//f_mount(&fs[0],"0:",1); //<2F><><EFBFBD><EFBFBD>SD<53><44>
|
|||
|
//f_open(&Data_file,"0:/User_Data.txt",FA_OPEN_ALWAYS | FA_READ | FA_WRITE );//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ı<EFBFBD><C4B1>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
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;
|
|||
|
}
|
|||
|
|
|||
|
//-----------------<2D><><EFBFBD><EFBFBD>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();
|
|||
|
}
|
|||
|
|
|||
|
//------------------------------<2D><><EFBFBD><EFBFBD>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;
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־
|
|||
|
//<2F>ļ<EFBFBD><C4BC><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>1M<31><4D><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><C2BD>н<EFBFBD>ȡ
|
|||
|
//ֻ<><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
|
|||
|
// 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); //<2F><>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>ʼ
|
|||
|
res = f_truncate(&fsrc); //<2F><><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><D8B6>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>/дָ<D0B4><D6B8>
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
res = f_lseek(&fsrc, f_size(&fsrc)); //<2F><>λ<EFBFBD><CEBB><EFBFBD>ļ<EFBFBD>ĩβ
|
|||
|
}
|
|||
|
if (res != FR_OK) goto err;
|
|||
|
res = f_write(&fsrc, loginfo, loglen, &br); // д<><D0B4><EFBFBD><EFBFBD>
|
|||
|
if (res != FR_OK)
|
|||
|
goto err;
|
|||
|
err:
|
|||
|
|
|||
|
f_close(&fsrc); // <20>ر<EFBFBD><D8B1>ļ<EFBFBD>
|
|||
|
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); // <20>ر<EFBFBD><D8B1>ļ<EFBFBD>
|
|||
|
// f_mount(&fs, "0:/", 0);
|
|||
|
// return 0;
|
|||
|
}
|
|||
|
|
|||
|
int read_log_file(void)
|
|||
|
{
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
int log_file_handler(void)
|
|||
|
{
|
|||
|
|
|||
|
return -1;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|