stm32使用FatFs系统报HardFault_Handler

这两天尝试使用FatFs系统来读写sd卡时,在尝试写入文件时,执行一半发生硬件中断错误。

void FatFs_WriteTXTFile(TCHAR *filename, char *data)
{
	FIL	file;
	FRESULT res;
	uint8_t file_opened = 0; // 标记文件是否成功打开
	
	printf("\r\n*** Creating TXT file: %s ***\r\n", filename);
	// 先尝试打开文件,如果文件已存在则删除它
	if(f_stat(filename, NULL) == FR_OK) {
		printf("文件已存在,尝试删除旧文件\r\n");
		FRESULT del_res = f_unlink(filename);
		if(del_res != FR_OK) {
			printf("删除文件失败,错误代码: %d\r\n", del_res);
			return;
		}
	}
	
	// 现在尝试创建新文件
	res = f_open(&file, filename, FA_CREATE_ALWAYS | FA_WRITE);
	//打开/创建文件成功
	if(res == FR_OK)
	{
		file_opened = 1; // 标记文件已成功打开
		
		//字符串带有换行符"\n"
//		TCHAR str[]="Line1: Hello, FatFs***\n";  
		//不会写入结束符"\0"
		UINT bw;
		res = f_write(&file, data, strlen(data), &bw);
		if(res == FR_OK) {
			printf("Write file OK: %s, 写入字节数: %d\r\n", filename, bw);
			
			// 确保数据写入磁盘
			res = f_sync(&file);
			if(res != FR_OK) {
				printf("同步文件到磁盘失败,错误代码: %d\r\n", res);
			}
		} else {
			printf("写入文件失败,错误代码: %d\r\n", res);
		}
	}
	else
	{
		printf("创建文件失败,错误代码: %d\r\n", res);
		// 检查文件系统状态
		DWORD free_clusters;
		FATFS *fs;
		res = f_getfree("0:", &free_clusters, &fs);
		if(res == FR_OK) {
			printf("文件系统可用,空闲簇: %lu\r\n", free_clusters);
		} else {
			printf("获取文件系统信息失败,错误代码: %d\r\n", res);
		}
	}
	
	//记得使用完关闭文件 - 只有在文件成功打开的情况下才关闭
	if (file_opened) {
		res = f_close(&file);
		printf("关闭文件结果: %d\r\n", res);
	}
}

后来,将FIL file;定义为全局变量发现问题解决,成功写文件。

FIL	file;

/*创建文本文件*/
void FatFs_WriteTXTFile(TCHAR *filename, char *data)
{
	//FIL	file; 局部变量就有问题
	FRESULT res;
	uint8_t file_opened = 0; // 标记文件是否成功打开
	
	printf("\r\n*** Creating TXT file: %s ***\r\n", filename);
	// 先尝试打开文件,如果文件已存在则删除它
	if(f_stat(filename, NULL) == FR_OK) {
		printf("文件已存在,尝试删除旧文件\r\n");
		FRESULT del_res = f_unlink(filename);
		if(del_res != FR_OK) {
			printf("删除文件失败,错误代码: %d\r\n", del_res);
			return;
		}
	}
	
	// 现在尝试创建新文件
	res = f_open(&file, filename, FA_CREATE_ALWAYS | FA_WRITE);
	//打开/创建文件成功
	if(res == FR_OK)
	{
		file_opened = 1; // 标记文件已成功打开
		UINT bw;
		res = f_write(&file, data, strlen(data), &bw);
		if(res == FR_OK) {
			printf("Write file OK: %s, 写入字节数: %d\r\n", filename, bw);
			
			// 确保数据写入磁盘
			res = f_sync(&file);
			if(res != FR_OK) {
				printf("同步文件到磁盘失败,错误代码: %d\r\n", res);
			}
		} else {
			printf("写入文件失败,错误代码: %d\r\n", res);
		}
	}
	else
	{
		printf("创建文件失败,错误代码: %d\r\n", res);
		// 检查文件系统状态
		DWORD free_clusters;
		FATFS *fs;
		res = f_getfree("0:", &free_clusters, &fs);
		if(res == FR_OK) {
			printf("文件系统可用,空闲簇: %lu\r\n", free_clusters);
		} else {
			printf("获取文件系统信息失败,错误代码: %d\r\n", res);
		}
	}
	
	//记得使用完关闭文件 - 只有在文件成功打开的情况下才关闭
	if (file_opened) {
		res = f_close(&file);
		printf("关闭文件结果: %d\r\n", res);
	}
}

2.发现增加堆栈的大小也可以解决。修改启动文件中堆栈的大小:

Stack_Size		EQU     0x2000

Heap_Size      EQU     0x2000
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值