【转载】挂载文件系统SD卡读写

使用软件:stm32cubemx
开发板:野火指南者stm32f103vet6
HAL库;STM32Cube FW_F1 V1.8.0
实现功能:串行flash模拟u盘

先用STM32CubeMX生成代码
需要:
1.RCC
2.SYS
3.SDIO
4.USART1(调试用)
5.FATFS(SD Card)
需要配置中断:无
【转载】挂载文件系统SD卡读写
【转载】挂载文件系统SD卡读写
设置 SDIOCLK clock divide factor (一般设置4-8) 【转载】挂载文件系统SD卡读写

这是对应的标准库sd卡的配置
【转载】挂载文件系统SD卡读写
设置异步通信模式【转载】挂载文件系统SD卡读写
【转载】挂载文件系统SD卡读写
【转载】挂载文件系统SD卡读写
【转载】挂载文件系统SD卡读写
【转载】挂载文件系统SD卡读写
生成代码

1…在usart.c重定向printf,方便给调试

需要加上stdio.h文件

#include "stdio.h"

   
   
  • 1
int fputc(int ch, FILE *f)
{
  HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xffff);
  return ch;
}

/**

  • 函数功能: 重定向c库函数getchar,scanf到DEBUG_USARTx
  • 输入参数: 无
  • 返 回 值: 无
  • 说 明:无
    /
    int fgetc(FILE
    f)
    {
    uint8_t ch = 0;
    HAL_UART_Receive(&huart1,&ch, 1, 0xffff);
    return ch;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

2.在main.c里写用户程序
【转载】挂载文件系统SD卡读写

FATFS fs;													/* FatFs文件系统对象 */
FIL file;													/* 文件对象 */
BYTE ReadBuffer[255]={0};								/*读缓冲区*/
BYTE WriteBuffer[20]="11111";								/*写缓冲区*/

 
 
  • 1
  • 2
  • 3
  • 4

首先要了解几个函数
f_mount 挂载文件系统
f_open 打开文件 查看f_open函数发现fp->fptr = 0;fp->dsect = 0;调用后每次打开文件会将指针清零,所以每次都从头写。
f_lseek 此函数用于文件指针的移动,共有两个参数,第一个参数为文件对象,第二个参数为移动的字节数,注意第二个参数是相对于文件的开始.
f_close 关闭文件
【转载】挂载文件系统SD卡读写
上代码

void SD_Write_Test(void)
{
	int res = 0;
	UINT write_num = 0;
res <span class="token operator">=</span> <span class="token function">f_mount</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>fs<span class="token punctuation">,</span><span class="token string">"0:"</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>            <span class="token comment">//挂载文件系统</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>res <span class="token operator">!=</span> FR_OK<span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"f_mount is error,error code is ;%d\r\n"</span><span class="token punctuation">,</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">else</span>
<span class="token punctuation">{<!-- --></span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"f_mount is successed! test open file\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	res <span class="token operator">=</span> <span class="token function">f_open</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>file<span class="token punctuation">,</span><span class="token string">"test.txt"</span><span class="token punctuation">,</span>FA_READ<span class="token operator">|</span>FA_WRITE<span class="token operator">|</span>FA_OPEN_ALWAYS<span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	<span class="token keyword">int</span> FileSize <span class="token operator">=</span> <span class="token function">f_size</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>res <span class="token operator">!=</span> FR_OK<span class="token punctuation">)</span>
	<span class="token punctuation">{<!-- --></span>
		<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"f_open is error,error code is ;%d\r\n"</span><span class="token punctuation">,</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">else</span>
	<span class="token punctuation">{<!-- --></span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>FileSize<span class="token operator">&gt;</span><span class="token number">3</span><span class="token punctuation">)</span> 							<span class="token comment">//就算文件里没有数据,也算是有一个字节,所以要判断一下,防止文件为空时移动</span>
		<span class="token punctuation">{<!-- --></span>
			res<span class="token operator">=</span><span class="token function">f_lseek</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>file<span class="token punctuation">,</span>FileSize<span class="token punctuation">)</span><span class="token punctuation">;</span> 
						<span class="token keyword">if</span><span class="token punctuation">(</span>res <span class="token operator">!=</span> FR_OK<span class="token punctuation">)</span>
						<span class="token punctuation">{<!-- --></span>
						<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"f_lseek is error,error code is ;%d\r\n"</span><span class="token punctuation">,</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span>
						<span class="token punctuation">}</span>
						
		<span class="token punctuation">}</span>
		res <span class="token operator">=</span> <span class="token function">f_write</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>file<span class="token punctuation">,</span>WriteBuffer<span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>WriteBuffer<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>write_num<span class="token punctuation">)</span><span class="token punctuation">;</span>
		res <span class="token operator">=</span> <span class="token function">f_write</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>file<span class="token punctuation">,</span><span class="token string">"\r\n"</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token string">"\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>write_num<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>res <span class="token operator">!=</span> FR_OK<span class="token punctuation">)</span>
		<span class="token punctuation">{<!-- --></span>
			<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"f_write is error,error code is ;%d\r\n"</span><span class="token punctuation">,</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
		<span class="token keyword">else</span><span class="token punctuation">{<!-- --></span>
			<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"TRUE write word to file size is :%d\r\n"</span><span class="token punctuation">,</span>write_num<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
		


		res <span class="token operator">=</span> <span class="token function">f_close</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>res <span class="token operator">!=</span> FR_OK<span class="token punctuation">)</span>
		<span class="token punctuation">{<!-- --></span>
			<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"f_close is error,error code is ;%d\r\n"</span><span class="token punctuation">,</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
		<span class="token keyword">else</span>
		<span class="token punctuation">{<!-- --></span>
			<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"f_close is successed!\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
void SD_read_Test(void)
{
	int res = 0;
	UINT write_num = 0;
	res = f_mount(&fs,"0:",1);            //挂载文件系统
	res = f_open(&file,"test.txt",FA_READ|FA_WRITE|FA_OPEN_ALWAYS);
res <span class="token operator">=</span> <span class="token function">f_read</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>file<span class="token punctuation">,</span>ReadBuffer<span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>write_num<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>res <span class="token operator">!=</span> FR_OK<span class="token punctuation">)</span>
		<span class="token punctuation">{<!-- --></span>
			<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"f_read is error,error code is ;%d\r\n"</span><span class="token punctuation">,</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
				<span class="token keyword">else</span><span class="token punctuation">{<!-- --></span>
			<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"TRUE read word to file size is :%d\r\n %s\r\n"</span><span class="token punctuation">,</span>write_num<span class="token punctuation">,</span>ReadBuffer<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>

res <span class="token operator">=</span> <span class="token function">f_close</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span>

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

别忘了在main函数之前定义一下。
【转载】挂载文件系统SD卡读写
【转载】挂载文件系统SD卡读写
【转载】挂载文件系统SD卡读写

可以和模拟U盘一起使用,当USB DEVICE与电脑连接时,停止向sd卡写数据。

[stm32Cubemx sd卡模拟U盘]
模拟U盘和FATFS冲突问题

上一篇:vue 快速入门 系列 —— 实例方法(或 property)和静态方法


下一篇:Pytorch自动混合精度(AMP)介绍