342 lines
8.8 KiB
C
342 lines
8.8 KiB
C
/**
|
|
******************************************************************************
|
|
* File Name : exfuns.c
|
|
* Description : This file provides code for the configuration
|
|
* of the External Functions instances.
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* <h2><center>© Copyright (c) 2019 STMicroelectronics.
|
|
* All rights reserved.</center></h2>
|
|
*
|
|
* This software component is licensed by ST under Ultimate Liberty license
|
|
* SLA0044, the "License"; You may not use this file except in compliance with
|
|
* the License. You may obtain a copy of the License at:
|
|
* www.st.com/SLA0044
|
|
*
|
|
******************************************************************************
|
|
*/
|
|
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include <string.h>
|
|
#include "malloc.h"
|
|
#include "fattester.h"
|
|
#include "usart.h"
|
|
#include "exfuns.h"
|
|
#include "ff.h"
|
|
#include "string.h"
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
//This program is for educational purposes only, without the author's permission, may not be used for any other purpose
|
|
//ALIENTEK STM32F4 Explorer Board
|
|
//FATFS Test code
|
|
//Hot Atomic @ALIENTEK
|
|
//Forum: www.openedv.com
|
|
//Date modified: 2015/9/12
|
|
//Version£ºV1.0
|
|
//All rights reserved pirated reserved.
|
|
//Copyright(C) Guangzhou wing electronic technology limited company 2009-2019
|
|
//All rights reserved
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//For disk registration area
|
|
//Path: a path on disk, such as "0:", "1:"
|
|
//MT:0, do not register (register later); 1, register now
|
|
//Return values: the results
|
|
u8 mf_mount(u8* path,u8 mt)
|
|
{
|
|
return f_mount(fs[2],(const TCHAR*)path,mt);
|
|
}
|
|
//Open the file path
|
|
//Path: path + file name
|
|
//mode: open the mode
|
|
//Return values: the results
|
|
u8 mf_open(u8*path,u8 mode)
|
|
{
|
|
u8 res;
|
|
res=f_open(file,(const TCHAR*)path,mode);//Open the folder
|
|
return res;
|
|
}
|
|
//Close the file
|
|
//Return values: the results
|
|
u8 mf_close(void)
|
|
{
|
|
f_close(file);
|
|
return 0;
|
|
}
|
|
//Read data
|
|
//Len: read the length of the
|
|
//Return values: the results
|
|
u8 mf_read(u16 len)
|
|
{
|
|
u16 i,t;
|
|
u8 res=0;
|
|
u16 tlen=0;
|
|
printf("\r\nRead file data is:\r\n");
|
|
for(i=0;i<len/512;i++)
|
|
{
|
|
res=f_read(file,fatbuf,512,&br);
|
|
if(res)
|
|
{
|
|
printf("Read Error:%d\r\n",res);
|
|
break;
|
|
}else
|
|
{
|
|
tlen+=br;
|
|
for(t=0;t<br;t++)printf("%c",fatbuf[t]);
|
|
}
|
|
}
|
|
if(len%512)
|
|
{
|
|
res=f_read(file,fatbuf,len%512,&br);
|
|
if(res) //Error reading data
|
|
{
|
|
printf("\r\nRead Error:%d\r\n",res);
|
|
}else
|
|
{
|
|
tlen+=br;
|
|
for(t=0;t<br;t++)printf("%c",fatbuf[t]);
|
|
}
|
|
}
|
|
if(tlen)printf("\r\nReaded data len:%d\r\n",tlen);//Read the length of the data
|
|
printf("Read data over\r\n");
|
|
return res;
|
|
}
|
|
//Writing data
|
|
//dat:The data buffer
|
|
//len:Writing to length
|
|
//Return values: the results
|
|
u8 mf_write(u8*dat,u16 len)
|
|
{
|
|
u8 res;
|
|
|
|
printf("\r\nBegin Write file...\r\n");
|
|
printf("Write data len:%d\r\n",len);
|
|
res=f_write(file,dat,len,&bw);
|
|
if(res)
|
|
{
|
|
printf("Write Error:%d\r\n",res);
|
|
}else printf("Writed data len:%d\r\n",bw);
|
|
printf("Write data over.\r\n");
|
|
return res;
|
|
}
|
|
|
|
//Open Directory
|
|
//Path: path
|
|
//Return values: the results
|
|
u8 mf_opendir(u8* path)
|
|
{
|
|
return f_opendir(&dir,(const TCHAR*)path);
|
|
}
|
|
//Close catalog
|
|
//Return values: the results
|
|
u8 mf_closedir(void)
|
|
{
|
|
return f_closedir(&dir);
|
|
}
|
|
//Reading folders
|
|
//Return values: the results
|
|
u8 mf_readdir(void)
|
|
{
|
|
u8 res;
|
|
char *fn;
|
|
#if _USE_LFN
|
|
fileinfo.fsize = _MAX_LFN * 2 + 1;
|
|
#endif
|
|
res=f_readdir(&dir, &fileinfo);//Read a file information
|
|
if(res!=FR_OK||fileinfo.fname[0]==0)
|
|
{
|
|
return res;//Read.
|
|
}
|
|
#if _USE_LFN
|
|
fn=fileinfo.fname;
|
|
#else
|
|
fn=fileinfo.fname;;
|
|
#endif
|
|
printf("\r\n DIR info:\r\n");
|
|
|
|
printf("dir.id:%d\r\n" ,dir.obj.id);
|
|
printf("dir.index:%d\r\n" ,(uint32_t)dir.dptr);
|
|
// printf("dir.sclust:%d\r\n",(u32)dir.);
|
|
printf("dir.clust:%d\r\n" ,(u32)dir.clust);
|
|
printf("dir.sect:%d\r\n" ,(u32)dir.sect);
|
|
|
|
printf("\r\n");
|
|
printf("File Name is:%s\r\n",fn);
|
|
printf("File Size is:%d\r\n",(u32)fileinfo.fsize);
|
|
printf("File data is:%d\r\n",(u32)fileinfo.fdate);
|
|
printf("File time is:%d\r\n",(u32)fileinfo.ftime);
|
|
printf("File Attr is:%d\r\n",(u32)fileinfo.fattrib);
|
|
printf("\r\n");
|
|
return 0;
|
|
}
|
|
|
|
//Traversing file
|
|
//path:path
|
|
//Return values: the results
|
|
u8 mf_scan_files(u8 * path)
|
|
{
|
|
FRESULT res;
|
|
char *fn; /* This function is assuming non-Unicode cfg. */
|
|
#if _USE_LFN
|
|
fileinfo.fsize = _MAX_LFN * 2 + 1;
|
|
#endif
|
|
|
|
res = f_opendir(&dir,(const TCHAR*)path); //Open a directory
|
|
if (res == FR_OK)
|
|
{
|
|
printf("\r\n");
|
|
while(1)
|
|
{
|
|
res = f_readdir(&dir, &fileinfo); //Read a file directory
|
|
if (res != FR_OK || fileinfo.fname[0] == 0) break; //Errors/to the end of the exit
|
|
//if (fileinfo.fname[0] == '.') continue; //Ignoring superior directory
|
|
#if _USE_LFN
|
|
fn = fileinfo.fname;
|
|
#else
|
|
fn = fileinfo.fname;
|
|
#endif /* It is a file. */
|
|
printf("%s/", path);//Print path
|
|
printf("%s\r\n", fn);//Print file name
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
//Displays the remaining capacity
|
|
//drv:Drive letter
|
|
//Return value: remaining capacity (in bytes)
|
|
u32 mf_showfree(u8 *drv)
|
|
{
|
|
FATFS *fs1;
|
|
u8 res;
|
|
u32 fre_clust=0, fre_sect=0, tot_sect=0;
|
|
//Get disk information and the number of free clusters
|
|
res = f_getfree((const TCHAR*)drv,(DWORD*)&fre_clust, &fs1);
|
|
if(res==0)
|
|
{
|
|
tot_sect = (fs1->n_fatent - 2) * fs1->csize;//Total number of sectors
|
|
fre_sect = fre_clust * fs1->csize; //Get number of spare sectors
|
|
#if _MAX_SS!=512
|
|
tot_sect*=fs1->ssize/512;
|
|
fre_sect*=fs1->ssize/512;
|
|
#endif
|
|
if(tot_sect<20480)//Total capacity is less than 10M
|
|
{
|
|
/* Print free space in unit of KB (assuming 512 bytes/sector) */
|
|
printf("\r\nThe total disk capacity:%d KB\r\n"
|
|
"Free space:%d KB\r\n",
|
|
tot_sect>>1,fre_sect>>1);
|
|
}else
|
|
{
|
|
/* Print free space in unit of KB (assuming 512 bytes/sector) */
|
|
printf("\r\nThe total disk capacity:%d MB\r\n"
|
|
"Free space:%d MB\r\n",
|
|
tot_sect>>11,fre_sect>>11);
|
|
}
|
|
}
|
|
return fre_sect;
|
|
}
|
|
//File read/write pointer offset
|
|
//offset:Relative offset of the first address
|
|
//Return value: the results of execution.
|
|
u8 mf_lseek(u32 offset)
|
|
{
|
|
return f_lseek(file,offset);
|
|
}
|
|
//Read the current position of the read/write pointer.
|
|
//Return value:location
|
|
u32 mf_tell(void)
|
|
{
|
|
return f_tell(file);
|
|
}
|
|
//Read the file size
|
|
//Return value: file size
|
|
u32 mf_size(void)
|
|
{
|
|
return f_size(file);
|
|
}
|
|
//Create a directory
|
|
//pname:Directory path + name
|
|
//Return values: the results
|
|
u8 mf_mkdir(u8*pname)
|
|
{
|
|
return f_mkdir((const TCHAR *)pname);
|
|
}
|
|
//Format
|
|
//path:Disk path, such as "0:", "1:"
|
|
//mode:mode
|
|
//au:Cluster size
|
|
//Return values: the results
|
|
//u8 mf_fmkfs(u8* path,u8 mode,u16 au)
|
|
//{
|
|
// return f_mkfs((const TCHAR*)path, mode, au, );//Format the drv: drive letter; mode: mode au: cluster size
|
|
//}
|
|
//Deleting files/directories
|
|
//PName: file/directory path + name
|
|
//Return values: the results
|
|
u8 mf_unlink(u8 *pname)
|
|
{
|
|
return f_unlink((const TCHAR *)pname);
|
|
}
|
|
|
|
//Modify file/directory names (if the directory is different, you can also move the file!)
|
|
//Oldname: previous names
|
|
//Newname new name
|
|
//Return values: the results
|
|
u8 mf_rename(u8 *oldname,u8* newname)
|
|
{
|
|
return f_rename((const TCHAR *)oldname,(const TCHAR *)newname);
|
|
}
|
|
//Gets the drive letter (disk name)
|
|
//Path: a path on disk, such as "0:", "1:"
|
|
void mf_getlabel(u8 *path)
|
|
{
|
|
u8 buf[20];
|
|
u32 sn=0;
|
|
u8 res;
|
|
res=f_getlabel ((const TCHAR *)path,(TCHAR *)buf,(DWORD*)&sn);
|
|
if(res==FR_OK)
|
|
{
|
|
printf("\r\nDisk%s Label:%s\r\n",path,buf);
|
|
printf("Disk%s Serial Number:%X\r\n\r\n",path,sn);
|
|
}else printf("\r\nFailed to get£¬Error code:%X\r\n",res);
|
|
}
|
|
//Set the drive letter (disk name), a maximum of 11 characters!! Support the combination of numbers and uppercase letters and Chinese characters
|
|
//path:Disk number + name£¬For example,"0:ALIENTEK"¡¢"1:OPENEDV"
|
|
void mf_setlabel(u8 *path)
|
|
{
|
|
u8 res;
|
|
res=f_setlabel ((const TCHAR *)path);
|
|
if(res==FR_OK)
|
|
{
|
|
printf("\r\nDisk drive letter set successfully:%s\r\n",path);
|
|
}else printf("\r\nDisk drive letter set£¬Error code:%X\r\n",res);
|
|
}
|
|
|
|
//Read a string from file
|
|
//size:To read the length of the
|
|
void mf_gets(u16 size)
|
|
{
|
|
TCHAR* rbuf;
|
|
rbuf=f_gets((TCHAR*)fatbuf,size,file);
|
|
if(*rbuf==0)return ;//No data to read
|
|
else
|
|
{
|
|
printf("\r\nThe String Readed Is:%s\r\n",rbuf);
|
|
}
|
|
}
|
|
//Need _USE_STRFUNC>=1
|
|
//Write a character to a file
|
|
//c:The character to write
|
|
//Return values: the results
|
|
u8 mf_putc(u8 c)
|
|
{
|
|
return f_putc((TCHAR)c,file);
|
|
}
|
|
//Write a string to a file
|
|
//c:The string to be written
|
|
//Return value: writes the length of the string
|
|
u8 mf_puts(u8*c)
|
|
{
|
|
return f_puts((TCHAR*)c,file);
|
|
}
|