Cubemx_STM32H743 SD_Card纳入文件管理系统
1、简介
和前面文章原理其实一样的。 1.29 Cubemx_STM32F429\H743 nandflash、spifalsh纳入文件管理系统 现在一并将SD卡、NandFlash、SPIFlash一并作为文件管理。 下面直接介绍SDcard,其他参考前面文章。
2、CubeMX配置
3 、bsp
#include <stdio.h>
#include <string.h>
#include "ff.h"
#include "fatfs.h"
#include "bsp_led.h"
#define FILE_NAME_SD "0:sudaroot.txt"
extern uint8_t mk_buffer[4096];
extern unsigned int count;
extern unsigned char work[4096];
extern unsigned char read_buf[50] ;
extern unsigned char write_buf[50];
void test_mkfs_sdcard(void)
{
FRESULT retSD;
retSD = f_mount(&SDFatFS, SDPath, 1);
retSD = f_mkfs((TCHAR const*)SDPath, FM_FAT32, 0, mk_buffer, sizeof(mk_buffer));
__NOP();
}
void test_SD_FATFS(void)
{
FRESULT retSD;
printf("\r\n ****** FatFs Example ******\r\n\r\n");
retSD = f_mount(&SDFatFS, SDPath, 1);
if(retSD != FR_OK)
{
if(retSD == FR_NO_FILESYSTEM)
{
printf("f_mount 没有文件系统,开始格式化spi-flash\r\n");
retSD = f_mkfs((TCHAR const*)SDPath, FM_FAT32, 0, mk_buffer, sizeof(mk_buffer));
if(retSD != FR_OK)
{
printf("f_mkfs 格式化失败,err = %d\r\n", retSD);
while(1);
}
else
{
printf("格式化成功,开始重新挂载spi-flash\r\n");
retSD = f_mount(&SDFatFS,SDPath, 1);
if(retSD != FR_OK)
{
printf("f_mount 发生错误,err = %d\r\n", retSD);
}
else printf("spi-flash文件系统挂载成功\r\n");
}
}
else
{
printf("f_mount 发生其他错误,err = %d\r\n", retSD);
while(1);
}
}
else printf("sd_card文件系统挂载成功\r\n");
printf("\r\n****** 即将进行文件写入测试... ******\r\n");
retSD = f_open(&SDFile,FILE_NAME_SD, FA_OPEN_ALWAYS | FA_WRITE);
if(retSD == FR_OK)
{
printf("打开/创建sudaroot.txt文件成功,向文件写入数据。\r\n");
retSD = f_write(&SDFile, write_buf, strlen((const char *)write_buf), &count);
if(retSD != FR_OK)
{
printf("f_write 发生错误,err = %d\r\n", retSD);
printf("关闭打开的sudaroot.txt文件\r\n");
count = 0;
f_close(&SDFile);
}
else
{
printf("文件写入成功,写入字节数据:%d\n", count);
printf("向文件写入的数据为:\r\n%s\r\n", write_buf);
printf("关闭打开的sudaroot.txt文件\r\n");
count = 0;
f_close(&SDFile);
}
}
else printf("打开/创建sudaroot.txt文件失败,err = %d\r\n", retSD);
printf("****** 即将进行文件读取测试... ******\r\n");
retSD = f_open(&SDFile, FILE_NAME_SD, FA_OPEN_EXISTING | FA_READ);
if(retSD == FR_OK)
{
printf("打开sudaroot.txt文件成功\r\n");
retSD = f_read(&SDFile, read_buf, sizeof(read_buf), &count);
if(retSD != FR_OK)
{
printf("f_read 发生错误,err = %d\r\n", retSD);
printf("关闭打开的sudaroot.txt文件\r\n");
f_close(&SDFile);
}
else
{
printf("文件读取成功,读取字节数据:%d\n", count);
printf("向文件读取的数据为:\r\n%s\r\n", read_buf);
printf("关闭打开的sudaroot.txt文件\r\n");
f_close(&SDFile);
}
}
else printf("打开sudaroot.txt文件失败,err = %d\r\n", retSD);
}
|