SDIO — SDIO + FatFs 文件系统
固件库: AT32F403A_407_Firmware_Library V2.2.2
芯片: AT32
源文件: AT32/AT32F403A_407_Firmware_Library_V2.2.2/project/at_start_f403a/examples/sdio/sdio_fatfs/src/main.c
功能简介
本示例演示 SD 卡读写操作。通过 SDIO 或 SPI 接口访问 SD 卡,实现大容量数据存储。配合 FatFs 文件系统可以读写文件。
完整代码
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
|
#include <stdbool.h>
#include <string.h>
#include "at32_sdio.h"
#include "at32f403a_407_board.h"
#include "at32f403a_407_clock.h"
#include "ff.h"
int main(void)
{
system_clock_config();
at32_board_init();
nvic_configuration();
uart_print_init(115200);
printf("start test fatfs r0.14b..\r\n");
if(SUCCESS != fatfs_test())
{
while(1)
{
sd_test_error();
}
}
/* all tests pass, led3 and led4 fresh */
while(1)
{
at32_led_toggle(LED3);
at32_led_toggle(LED4);
delay_ms(300);
}
}
|
代码讲解
系统时钟初始化:配置 MCU 的主频和时钟树。不同芯片的时钟配置不同,一般由工具生成。
板级初始化:初始化开发板上的 LED、按键等基础外设。
串口初始化:配置串口波特率(这里是 115200),使能 printf 重定向。
printf 输出:通过串口打印调试信息。需要先调用 uart_print_init 完成重定向。
中断优先级配置:NVIC 设置中断的抢占优先级和子优先级。数字越小优先级越高。
延时:使用系统延时函数控制 LED 翻转间隔。