SDIO — SD/MMC 卡读写
固件库: AT32F403A_407_Firmware_Library V2.2.2
芯片: AT32
源文件: AT32/AT32F403A_407_Firmware_Library_V2.2.2/project/at_start_f403a/examples/sdio/sd_mmc_card/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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
#include <stdbool.h>
#include <string.h>
#include "at32_sdio.h"
#include "at32f403a_407_board.h"
#include "at32f403a_407_clock.h"
#define BLOCK_SIZE (512)
#define BLOCKS_NUMBER (64)
#define MULTI_BUFFER_SIZE (BLOCK_SIZE * BLOCKS_NUMBER)
#define STREAM_BUFFER_SIZE (2048)
int main(void)
{
system_clock_config();
at32_board_init();
nvic_configuration();
uart_print_init(115200);
printf("start test..\r\n");
/* sdio init */
if(SD_OK != sd_init())
{
printf("sdio init fail\r\n");
while(1)
{
sd_test_error();
}
}
printf("sdio init ok\r\n");
show_card_info();
/* sd card single block test */
if(TEST_SUCCESS != sd_single_block_test())
{
printf("sd card single block test fail\r\n");
/* single block test fail, led2 fresh */
while(1)
{
sd_test_error();
}
}
printf("sd card single block test ok\r\n");
/* sd card multiple blocks test */
if(TEST_SUCCESS != sd_multiple_blocks_test())
{
printf("sd card multiple blocks test fail\r\n");
/* multiple blocks test fail, led2 fresh */
while(1)
{
sd_test_error();
}
}
printf("sd card multiple blocks test ok\r\n");
/* mmc card stream data transfer test */
if((sd_card_info.card_type == SDIO_MULTIMEDIA_CARD)||(sd_card_info.card_type == SDIO_HIGH_SPEED_MULTIMEDIA_CARD))
{
if(TEST_SUCCESS != mmc_stream_test())
{
printf("mmc card stream data test fail\r\n");
/* mmc card stream data test fail, led2 fresh */
while(1)
{
sd_test_error();
}
}
printf("mmc card stream data test ok\r\n");
}
/* 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 翻转间隔。