本程序所用的程序型号为:STM32F103VE,振:8MHz
文件
开发:容量为16MB的W25Q128工具开发:Keil MDK 5
实现了用C语言
里面的fopen,fread,fwrite,fclose等标准库函数打开W25Q128中的文件。当然还是FatFS库通过重写Keil的_sys_open、_sys_close、_sys_write、_sys_read等,实现fopen与FatFS的f_open的绑定
。_ttywrch函数是abort内部调用的接口字符函数输出函数。
程序使用的是标准ç库,没有使用microlib中。项目属性中没有勾选使用microlib中复选框。
中的#pragma import(__ use_no_semihosting)这句话的作用是禁用半主机模式。默认情况下,的fopen函数是通过调用ARM调试器的指令读取调试器上的文件,如果程序运行在非调试模式下正式生成硬错误错误,这就是优化的半模式模式。只要程序编写_主机_ *系列的功能,开始关闭半主机模式,实现我们想要的功能。加上这句话,辅助我们检测并注意哪些功能需要更新,避免严重错误错误产生。
程序的下载地址:https://pan.baidu.com/s/16-jnl_03HhA_TYaxbNj3aw
主程序:
#include
#include
#include
#include
#include
#include
static FATFS fatfs;
static int file_init(void)
{
char buffer[FF_MIN_SS];
FRESULT fr;
fr = f_mount(&fatfs, "0:/", 1); // 挂载磁盘
if (fr == FR_OK)
{
printf("f_mount OK!n");
return 1;
}
if (fr == FR_NO_FILESYSTEM)
{
// 若磁盘没有格式化, 则格式化磁盘
fr = f_mkfs("0:/", FM_FAT, 0, buffer, sizeof(buffer));
if (fr == FR_OK)
{
printf("Disk is formatted!n");
return 1;
}
else
{
printf("Disk cannot be formatted! fr=%dn", fr);
return 0;
}
}
else
{
printf("f_mount error! fr=%dn", fr);
return 0;
}
}
static void errno_test(void)
{
double a = 0.0;
a = sin(3.4 / a);
printf("a=%lf, sin=0x%p, errno=%dn", a, sin, errno);
perror("error");
errno = 0;
perror("error");
}
static void file_test(void)
{
FILE *fp;
int num;
fp = fopen("hello7.txt", "r+");
if (fp == NULL) // 若文件不存在, 则创建一个
{
fp = fopen("hello7.txt", "w+");
if (fp == NULL)
{
printf("Cannot open File 1!n");
return;
}
printf("File 1 is created!n");
fputs("=0", fp);
rewind(fp);
}
else
printf("File 1 is open!n");
fscanf(fp, "=%d", &num);
printf("num=%dn", num);
rewind(fp);
fprintf(fp, "=%d", num + 1);
printf("ftell=%ldn", ftell(fp));
fclose(fp);
}
static void file_test2(void)
{
char buffer[50];
char *s = "This is a string! Hello World!";
FILE *fp;
int n;
fp = fopen("foo.txt", "r");
if (fp == NULL)
{
fp = fopen("foo.txt", "w");
if (fp == NULL)
{
printf("Cannot open File 2!n");
return;
}
n = fwrite(s, 1, strlen(s), fp);
printf("%d bytes written!n", n);
fclose(fp);
return;
}
n = fread(buffer, 1, sizeof(buffer) - 1, fp);
if (n > 0)
{
buffer[n] = '