📄 main.c
字号:
temp32 = fat_file_length;
}
fat_cmd.locate.sector = rs;
i = fat_locate_sector( );
if( i==F_OK )
{
fat_cmd.read.sector = FAT_FILE_BUF_LEN / SEC_SIZE;
i = fat_fread( );
if( i!=F_OK )
{
// show_error_code( i );
printf("Error: fat_fread() \n");
;
}
}
else
{
if( i==END_OF_FILE_ERR )
{
strcpy( fat_cmd.open.buffer, dst_file );
i = fat_fopen( );
if( i==F_OK )
{
fat_cmd.modify.size = file_len;
fat_cmd.modify.date = 0XFFFF;
fat_cmd.modify.time = 0XFFFF;
fat_cmd.modify.attr = 0XFF;
i = fat_fmodify( );
if( i!=F_OK )
printf("Error: fat_fmodify() \n");
// show_error_code( i );
}
else
printf("Error: fat_fopen() \n");
// show_error_code( i );
printf( "Success: copy file\n" );
break;
}
printf("Error: fat_locate_sector( ) \n");
// show_error_code( i );
break;
}
}
else
{
// show_error_code( i );
printf("Error: fat_fopen() \n");
break;
}
strcpy( fat_cmd.open.buffer, dst_file );
i = fat_fopen( );
if( i==F_OK )
{
fat_cmd.locate.sector = rs;
fat_file_length = file_len;
i = fat_locate_sector( );
if( i==F_OK )
{
fat_cmd.write.sector = FAT_FILE_BUF_LEN / SEC_SIZE;
i = fat_fwrite( );
if( i!=F_OK )
{
printf("Error: fat_fwrite() \n");
// show_error_code( i );
break;
}
}
else
{
// show_error_code( i );
printf("Error: fat_locate_sector() \n");
break;
}
}
else
{
// show_error_code( i );
printf("Error: fat_fopen() \n");
break;
}
rs += FAT_FILE_BUF_LEN / SEC_SIZE;
}
}
void test_create_file(char* file_name)
{
Uint8 i;
strcpy( fat_cmd.create.buffer, file_name );
/*以此 F_CREATE_ALWAYS 方式创建文件,如此文件存在则删除*/
i = fat_fcreate( F_CREATE_ALWAYS );
if( i==F_OK )
{
/*不修改文件长度*/
fat_cmd.modify.size = DEF_LEN;
/*修改文件访问日期*/
fat_cmd.modify.date = MDATE( 2008, 1, 1 );
/*修改文件访问时间*/
fat_cmd.modify.time = MTIME( 9, 30, 20 );
/*赋予文件属性*/
fat_cmd.modify.attr = ATTR_ARCHIVE;
i = fat_fmodify( );
if( i!=F_OK )
{
printf("Error: fat_fmodify() \n");
return;
}
}
else
{
printf("Error: fat_fcreate() \n");
return;
}
printf( "Success: create file\n");
}
void test_disk_query()
{
Uint8 i;
i = fat_disk_query( );
if( i==F_OK )
{
/*剩余数据可用扇区*/
printf( "free_sector:%04d\n", fat_cmd.query.free_sector );
/*数据总扇区*/
printf( "total_sector:%04d\n", fat_cmd.query.total_sector );
if( fat_cmd.query.fat_type == F_TYPE_FAT12 )
printf( "FAT12\n" );
else if( fat_cmd.query.fat_type == F_TYPE_FAT16 )
printf( "FAT16\n" );
else
printf( "FAT32\n" );
}
else
printf("Error: fat_disk_query( ) \n");
}
/*
void test_write_file(char* src_file, UINT8 *buff, UINT32 write_len)
{
Uint8 i;
Uint32 sended_len;
strcpy( fat_cmd.open.buffer,src_file );
i=fat_fopen();
if(i == F_OK)
{
fat_fclose( );
strcpy( fat_cmd.del.buffer, src_file);
i =fat_fdelete();
if( i!=F_OK )
{
printf("Error: cannot delete the file \n");
return;
}
strcpy( fat_cmd.create.buffer, src_file );
i = fat_fcreate( F_CREATE_ALWAYS );
if(i != F_OK)
{
printf("Error: cannot create the file \n");
return;
}
}
else
{
printf("Information: file not exist, create file ... \n");
strcpy( fat_cmd.create.buffer, src_file );
i = fat_fcreate( F_CREATE_ALWAYS );
if(i != F_OK)
{
printf("Error: cannot create the file \n");
return;
}
}
strcpy( fat_cmd.open.buffer,src_file );
i=fat_fopen( );
if( i==F_OK )
{
sended_len = 0;
fat_cmd.write.sector = (write_len + SEC_SIZE -1)/SEC_SIZE;
while( write_len > FAT_FILE_BUF_LEN )
{
memcpy(fat_file_buffer, &buff[sended_len], FAT_FILE_BUF_LEN);
i = fat_fwrite( );
write_len = write_len - FAT_FILE_BUF_LEN;
sended_len = sended_len + FAT_FILE_BUF_LEN;
if( i!=F_OK )
{
printf("Error: fat_fwrite( ) \n");
fat_fclose( );
return ;
}
}
memcpy(fat_file_buffer, &buff[sended_len],write_len);
memset(&fat_file_buffer[write_len-1], ' ', FAT_FILE_BUF_LEN - write_len);
i = fat_fwrite( );
if( i!=F_OK )
{
printf("Error: fat_fwrite( ) \n");
}
if( fat_cmd.write.sector==0 )
{
fat_cmd.modify.size = sended_len + write_len;
fat_cmd.modify.date = 0XFFFF;
fat_cmd.modify.time = 0XFFFF;
fat_cmd.modify.attr = 0XFF;
i = fat_fmodify();
if( i!=F_OK )
printf("Error: fat_fmodify() \n");
}
fat_fclose( );
}
else
printf("Error: fileopen() \n");
printf("Success: Write file success \n");
}
*/
/*
Uint32 test_read_file(char* file_name, Uint8 *buff, Uint32 buff_len)
{
Uint8 i;
Uint32 readed_len = 0;
Uint32 temp32;
if( sizeof(file_name) < FAT_CMD_BUF_LEN )
{
strcpy( fat_cmd.open.buffer, file_name );
i = fat_fopen();
if(i==F_OK)
{
readed_len = 0;
temp32 = (fat_file_length + SEC_SIZE - 1 ) / SEC_SIZE;
while( temp32 )
{
if( temp32 > (UINT32)(FAT_FILE_BUF_LEN / SEC_SIZE) )
{
fat_cmd.read.sector = FAT_FILE_BUF_LEN / SEC_SIZE;
temp32 -= FAT_FILE_BUF_LEN / SEC_SIZE;
memcpy(&buff[readed_len],fat_file_buffer,FAT_FILE_BUF_LEN);
readed_len = readed_len + FAT_FILE_BUF_LEN;
}
else
{
fat_cmd.read.sector = (UINT8)temp32;
memcpy(&buff[readed_len],fat_file_buffer,temp32*SEC_SIZE);
temp32 = 0;
}
i = fat_fread();
if( i!=F_OK )
{
printf("Error: fat_fread() \n");
break;
}
}
fat_fclose( );
}
else
{
printf("Error: fat_fopen() \n");
}
}
else
printf( "cmd error!!\n" );
printf( "Success: read file \n" );
return fat_file_length;
}
*/
static int ch375FileOpen(Uint8 dev_id, char *file_name, Uint8 mode)
{
Uint8 i;
SD_U = dev_id;
if(mode == 0) //read
{
strcpy( fat_cmd.open.buffer, file_name );
i = fat_fopen();
if(i!=F_OK)
{
printf("Error: File Open() \n");
return -1;
}
return fat_file_length;
}
else
if(mode == 1) //write
{
strcpy( fat_cmd.open.buffer, file_name );
i = fat_fopen();
if(i == F_OK)
{
fat_fclose( );
strcpy( fat_cmd.del.buffer, file_name);
i =fat_fdelete();
if( i!=F_OK )
{
printf("Error: cannot delete the file \n");
return -1;
}
strcpy( fat_cmd.create.buffer, file_name );
i = fat_fcreate( F_CREATE_ALWAYS );
if(i != F_OK)
{
printf("Error: cannot create the file \n");
return -1;
}
}
else
{
printf("Information: file not exist, create file ... \n");
strcpy( fat_cmd.create.buffer, file_name );
i = fat_fcreate( F_CREATE_ALWAYS );
if(i != F_OK)
{
printf("Error: cannot create the file \n");
return -1;
}
}
return 0;
}
return -1;
}
static int ch375FileClose(Uint8 dev_id, Uint8 mode, Uint32 filesize)
{
Uint8 i;
SD_U = dev_id;
if(mode == 1 )
{
fat_cmd.modify.size = filesize;
fat_cmd.modify.date = 0XFFFF;
fat_cmd.modify.time = 0XFFFF;
fat_cmd.modify.attr = 0XFF;
i = fat_fmodify();
if( i!=F_OK )
{
printf("Error: fat_fmodify() \n");
return -1;
}
}
fat_fclose();
return 0;
}
Uint32 ch375FileWrite( Uint8 *buf, Uint32 len)
{
Uint8 i;
Uint32 written_len;
while(len>FAT_FILE_BUF_LEN)
{
memcpy(&buf[written_len],fat_file_buffer, FAT_FILE_BUF_LEN);
fat_cmd.write.sector = FAT_FILE_BUF_LEN / SEC_SIZE;
i = fat_fwrite();
if( i!=F_OK )
{
printf("Error: fat_fread() \n");
return written_len;
}
len = len - FAT_FILE_BUF_LEN;
written_len = written_len + FAT_FILE_BUF_LEN;
}
memcpy(fat_file_buffer,&buf[written_len],len);
memset(&fat_file_buffer[len-1],' ',FAT_FILE_BUF_LEN - len);
fat_cmd.write.sector = len;
i = fat_fwrite();
if( i!=F_OK )
{
printf("Error: fat_fread() \n");
}
written_len = written_len + len;
return written_len;
}
Uint32 ch375FileRead(Uint8 *buf, Uint32 len)
{
Uint8 i;
Uint32 readed_len;
readed_len = 0;
while(len>FAT_FILE_BUF_LEN)
{
fat_cmd.read.sector = FAT_FILE_BUF_LEN / SEC_SIZE;
i = fat_fread();
if( i!=F_OK )
{
printf("Error: fat_fread() \n");
return readed_len;
}
memcpy(&buf[readed_len],fat_file_buffer, FAT_FILE_BUF_LEN);
len = len - FAT_FILE_BUF_LEN;
readed_len = readed_len + FAT_FILE_BUF_LEN;
}
fat_cmd.read.sector = len;
i = fat_fread();
if( i!=F_OK )
{
printf("Error: fat_fread() \n");
}
memcpy(&buf[readed_len],fat_file_buffer, len);
readed_len = readed_len + len;
return readed_len;
}
void SwiMainFunc() {
unsigned char temp;
int i;
DSTATUS tempstat;
Uint8 temp8;
Uint32 temp32;
Uint32 write_len;
Uint32 file_size;
TIMER_Config MyTimerConfig = { 0x000002C0, /* ctl */
0xFFFFFFFF, /* prd */
0x00000000 /* cnt */
};
for(i=0;i<10000;i++)
{
buff_w[i]=i*2;
buff_r[i]=0;
}
/* The following code */
/* configures the serial port for digital loopback mode. We */
/* then use the CPU to write/read from the port. In loopback */
/* mode, we should read the same value as written, this is */
/* checked for in a loop. */
/* Let's open up serial port 1 */
myMcbsp = MCBSP_open(MCBSP_DEV0, MCBSP_OPEN_RESET);
MCBSP_config(myMcbsp , &myInitialConfig );
/* Now that the port is setup, let's enable it in steps. */
MCBSP_enableSrgr(myMcbsp);
printf("wait for two clock \n");
MCBSP_enableRcv(myMcbsp);
MCBSP_enableXmt(myMcbsp);
htimer1 = TIMER_open(TIMER_DEVANY,0);
TIMER_config(htimer1,&MyTimerConfig);
start = TIMER_getCount(htimer1);
end = TIMER_getCount(htimer1);
overhead = end - start ;
if(MMC_Init()!=CH_OK)
{
printf("Error: MMC is not inited \n");
return;
}
if(fat_init()!=F_OK)
{
printf("Error: fat_init() \n");
return;
}
test_disk_query();
printf("Create file ...\n");
test_create_file("\\TEST2.TXT");
file_size= ch375FileOpen(1, "\\TEST2.TXT", 1);
temp32 = ch375FileWrite( buff_w, 32);
ch375FileClose(1,temp32,file_size);
// test_create_dir("\\新建目录4\\二级子目录1");
// test_enum_file();
/* All done now, close the port. */
MCBSP_close(myMcbsp);
LOG_printf(&LogMain,"<DONE>");
}
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
void main() {
/* Initialize the chip support library, required */
CSL_init();
// SwiMainFunc();
SWI_post(&SwiMain);
}
/*----------------------------------------------------------------------------*/
/******************************************************************************\
* End of main.c
\******************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -