📄 main.c
字号:
/*
*********************************************************************************************************
*
* 模块名称 : 主程序模块。
* 文件名称 : main.c
* 版 本 : V2.0
* 说 明 : 该例程实现读写SD卡文件的功能。移植了FatFS文件系统。
* 修改记录 :
* 版本号 日期 作者 说明
* v1.0 2011-08-27 armfly ST固件库V3.5.0版本。
* v2.0 2011-10-16 armfly 优化工程结构。
*
* Copyright (C), 2010-2011, 安富莱电子 www.armfly.com
*
*********************************************************************************************************
*/
/*
!!!注意:该例程可以在CPU Flash和CPU RAM中调试,暂时不支持在外部RAM调试。
这个例程中的函数用到了较多的局部变量,因此缺省的堆栈不够用。
需要调整大一些。
修改 startup_stm32f10x_hd.s 文件
以前为 :Stack_Size EQU 0x00000400
现在为 :Stack_Size EQU 0x00001000
*/
/*
操作说明:
(1) 请接上串口线,打开windows的串口工具,比如超级终端。
(2) 输入字符1,打印SD根目录下的文件和文件夹列表。
(3) 输入字符2,在SD卡根目录下创建一个文件,文件名为 armfly.txt
并且向该文件写入一个字符串。
(4) 输入字符3,打开SD卡根目录下的armfly.txt文件,读出其内容,打印到串口。
(5) 输入字符4,创建打开SD卡根目录下的armfly.txt文件,读出其内容,打印到串口。
(6) 输入字符5,写文件和读文件速度测试
*/
#include "stm32f10x.h" /* 如果要用ST的固件库,必须包含这个文件 */
#include <stdio.h> /* 因为用到了printf函数,所以必须包含这个文件 */
#include <string.h> /* 因为用到了字符串操作函数,所以必须包含这个文件 */
#include "bsp_usart.h" /* printf函数定向输出到串口,所以必须包含这个文件 */
#include "bsp_led.h" /* LED指示灯驱动模块 */
#include "bsp_button.h" /* 按键驱动模块 */
#include "bsp_timer.h" /* systick定时器模块 */
#include "bsp_sdio_sd.h" /* SD卡驱动模块 */
#include "ff.h" /* FatFS文件系统模块*/
/* 定义例程名和例程发布日期 */
#define EXAMPLE_NAME "SDIO+FatFs文件系统例程"
#define EXAMPLE_DATE "2011-10-16"
#define DEMO_VER "2.0"
/* 用于测试读写速度 */
#define TEST_FILE_LEN 1000 /* (2*1024*1024) 用于测试的文件长度 */
#define BUF_SIZE 50 /* 每次读写SD卡的最大数据长度 */
uint8_t g_TestBuf[BUF_SIZE];
unsigned char str[9]; //全局
unsigned int receivedtxt[30];
/* 仅允许本文件内调用的函数声明 */
static void InitBoard(void);
static void PrintfLogo(void);
static void DispMenu(void);
static void ViewRootDir(void);
static void CreateNewFile(void);
static void ReadFileData(void);
static void CreateDir(void);
static void WriteFileTest(void);
unsigned int format_data(char *s, unsigned int adc) //////将常数合成字符串
{
int pos=0,i=0;
static char temp_str[8]={'\0','\0','\0','\0','\0','\0','\0','\0'};
u32tostr(adc,temp_str); //////整形变字符串
while(temp_str[i]) //do
{
s[pos]=temp_str[i];
i++;pos++;
} //;
i=0;
//pos++;
s[pos]=0x0D;pos++;
s[pos]=0x0A;//pos++;
pos=0;
//s[pos]=0;
return pos;
}
void ascii_int(char *file_buf) //////将常数合成字符串
{
uint32_t i;
uint16_t begin, end;
char Line1[8];
char Line2[8];
char Line3[8];
//1002 回车换行 i==4
//0 回车换行
//2009 回车换行
// 搜索第1行
begin = 0;
for (i = 0; i < 80; i++)
{
if (file_buf[i] == 0x0D)
{
end = i - 1; ////end=3
break;
}
}
// 保存第1行 开始处,结束处
memcpy(Line1, &file_buf[begin], end);
// 搜索第2行
begin = end + 2;
for (i = begin; i < 80; i++)
{
if (file_buf[i] == 0x0D)
{
end = i - 1;
break;
}
}
// 保存第2行
memcpy(Line2, &file_buf[begin], end);
// 搜索第3行
begin = end + 2;
for (i = begin; i < 80; i++)
{
if (file_buf[i] == 0x0D)
{
end = i - 1;
break;
}
}
// 保存第3行
memcpy(Line3, &file_buf[begin], end);
receivedtxt[0]=strtou32(Line1);
receivedtxt[1]=strtou32(Line2);
receivedtxt[2]=strtou32(Line3);
}
/*
*********************************************************************************************************
* 函 数 名: main
* 功能说明: c程序入口
* 形 参:无
* 返 回 值: 错误代码(无需处理)
*********************************************************************************************************
*/
int main(void)
{
char cmd;
uint8_t KeyCode;
/*
由于ST固件库的启动文件已经执行了CPU系统时钟的初始化,所以不必再次重复配置系统时钟。
启动文件配置了CPU主时钟频率、内部Flash访问速度和可选的外部SRAM FSMC初始化。
系统时钟缺省配置为72MHz,如果需要更改,可以修改:
\Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\system_stm32f10x.c
中配置系统时钟的宏。
*/
InitBoard(); /* 为了是main函数看起来更简洁些,我们将硬件初始化的代码封装到这个函数 */
PrintfLogo(); /* 打印例程Logo到串口1 */
SDIO_Interrupts_Config(); /* 配置SDIO中断, 此函数在bsp_sdio_sd.c */
/* 打印命令列表,用户可以通过串口操作指令 */
DispMenu();
while(1)
{
cmd = getchar();
/* 读取按键,大于0表示有键按下 */
KeyCode = bsp_GetKey();
if (KeyCode > 0)
{
/* 有键按下 */
switch (KeyCode)
{
case KEY_DOWN_JOY_LEFT: /* 摇杆LEFT键按下 */
CreateNewFile();
ReadFileData(); /* 读取根目录下armfly.txt的内容 */
break;
case KEY_DOWN_JOY_RIGHT: /* 摇杆RIGHT键按下 */
WriteFileTest(); /* 速度测试 */
break;
default:
break;
}
}
cmd = getchar();
switch (cmd)
{
case '1':
printf("【1 - ViewRootDir】\r\n");
ViewRootDir(); /* 显示SD卡根目录下的文件名 */
break;
case '2':
printf("【2 - CreateNewFile】\r\n");
CreateNewFile(); /* 创建一个新文件,写入一个字符串 */
break;
case '3':
printf("【3 - ReadFileData】\r\n");
ReadFileData(); /* 读取根目录下armfly.txt的内容 */
break;
case '4':
printf("【4 - CreateDir】\r\n");
CreateDir(); /* 创建目录 */
break;
case '5':
printf("【5 - TestSpeed】\r\n");
WriteFileTest(); /* 速度测试 */
break;
default:
DispMenu();
break;
}
}
}
/*
*********************************************************************************************************
* 函 数 名: DispMenu
* 功能说明: 显示操作提示菜单
* 形 参:无
* 返 回 值: 无
*********************************************************************************************************
*/
static void DispMenu(void)
{
printf("\r\n*******************************************\r\n");
printf("请选择操作命令:\r\n");
printf("1 - 显示SD卡根目录下的文件列表\r\n");
printf("2 - 创建一个新文件\r\n");
printf("3 - 读文件数据\r\n");
printf("4 - 创建目录\r\n");
printf("5 - 读写文件速度测试\r\n");
}
/*
*********************************************************************************************************
* 函 数 名: ViewRootDir
* 功能说明: 显示SD卡根目录下的文件名
* 形 参:无
* 返 回 值: 无
*********************************************************************************************************
*/
static void ViewRootDir(void)
{
/* 本函数使用的局部变量占用较多,请修改启动文件,保证堆栈空间够用 */
FRESULT result;
FATFS fs;
DIR DirInf;
FILINFO FileInf;
uint8_t tmpStr[20];
uint32_t cnt = 0;
/* 挂载文件系统 */
result = f_mount(0, &fs); /* Mount a logical drive */
if (result != FR_OK)
{
printf("挂载文件系统失败 (%d)\r\n", result);
}
/* 打开根文件夹 */
result = f_opendir(&DirInf, "/"); /* 如果不带参数,则从当前目录开始 */
if (result != FR_OK)
{
printf("打开根目录失败 (%d)\r\n", result);
return;
}
/* 读取当前文件夹下的文件和目录 */
printf("Name\t\tTyepe\t\tSize\r\n");
for (cnt = 0; ;cnt++)
{
result = f_readdir(&DirInf,&FileInf); /* 读取目录项,索引会自动下移 */
if (result != FR_OK || FileInf.fname[0] == 0)
{
break;
}
if (FileInf.fname[0] == '.')
{
continue;
}
printf("%s", FileInf.fname);
if (strlen(FileInf.fname) < 8) /* 对齐 */
{
printf("\t\t");
}
else
{
printf("\t");
}
if (FileInf.fattrib == AM_DIR)
{
printf("目录\t\t");
}
else
{
printf("文件\t\t");
}
printf("%d\r\n", FileInf.fsize);
sprintf((char *)tmpStr, "%d", FileInf.fsize);
}
/* 卸载文件系统 */
f_mount(0, NULL);
}
/*
*********************************************************************************************************
* 函 数 名: CreateNewFile
* 功能说明: 在SD卡创建一个新文件,文件内容填写“www.armfly.com”
* 形 参:无
* 返 回 值: 无
*********************************************************************************************************
*/
static void CreateNewFile(void)
{
/* 本函数使用的局部变量占用较多,请修改启动文件,保证堆栈空间够用 */
FRESULT result;
FATFS fs;
FIL file;
DIR DirInf;
uint32_t bw;
/* 挂载文件系统 */
result = f_mount(0, &fs); /* Mount a logical drive */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -