1. 准备工作硬件准备- 开发板
: r. P9 a# h" e' O首先需要准备一个开发板,这里我准备的是STM32L4的开发板(BearPi):* v" U' E% k, ?6 H' G* ?
' ]! _ W, }& g

- Micro SD卡
! g3 Z% [1 [6 L6 W2 [2 b) w小熊派开发板板载 Micro SD 卡槽,最大支持 32 GB,需要提前自行准备一张 Micro SD卡,如图:' S" |$ d; M& L# J, p8 N
1 r9 t4 {0 ^" V* \5 \

, G& L: f+ s9 w, G软件准备- 需要安装好Keil - MDK及芯片对应的包,以便编译和下载生成的代码;
- 准备一个串口调试助手,这里我使用的是Serial Port Utility;4 Z2 e+ k; W: {$ n9 s
% t( a. n& O% Y% e4 S
- x. f W$ _( U. o0 A 2.生成MDK工程# I4 E4 n6 g) a
选择芯片型号打开STM32CubeMX,打开MCU选择器:
/ y' i2 J Q- {) b0 M7 c 搜索并选中芯片STM32L431RCT6:
8 g- \2 e0 h1 r# C2 }' w 9 n: c3 k S: w4 f# q& S
配置时钟源- 如果选择使用外部高速时钟(HSE),则需要在System Core中配置RCC;
- 如果使用默认内部时钟(HSI),这一步可以略过;
- k B& G; b3 m0 z+ A% x2 n) }3 G9 f4 ~# v
这里我都使用外部时钟:  : w$ g6 Z# c1 C- R3 ~( d
配置串口小熊派开发板板载ST-Link并且虚拟了一个串口,原理图如下: 
这里我将开关拨到AT-MCU模式,使PC的串口与USART1之间连接。 接下来开始配置USART1:  ) `" U7 f }3 U; a' q5 h
配置 SDMMC 接口知识小卡片 —— SDMMC接口
SDMMC接口的全称叫SD/SDIO MMC card host interface,SD/SDIO MMC 卡 主机接口,通俗的来说,就是这个接口支持SD卡,支持SDIO设备,支持MMC卡。 知识小卡片结束啦~
首先查看小熊派开发板的原理图: 
然后根据原理图配置 SDMMC 接口: 
9 b; R6 y% y7 O: B, w5 T配置时钟树STM32L4的最高主频到80M,所以配置PLL,最后使HCLK = 80Mhz即可:  * M; @9 K4 a* U9 A1 e
生成工程设置 - N* m) [: a( l' d
代码生成设置
1 f' s4 n2 B H* ~( E2 v最后设置生成独立的初始化文件: 
- i! |/ \) o6 H( t
生成代码点击GENERATE CODE即可生成MDK-V5工程: 
: P$ K5 l1 w4 [ 3. 在MDK中编写、编译、下载用户代码) R7 H9 \' |5 a4 @- [
重定向printf( )函数) }$ r, }, u$ C* T
读取SD卡信息并打印SD 卡系统(包括主机和 SD 卡)定义了两种操作模式: - 卡识别模式
- 数据传输模式9 B9 I {: I* P# V
& e& Z; l9 {7 w+ p& ]
在系统复位后,主机处于卡识别模式,寻找总线上可用的 SD卡设备;同时,SD 卡也处于卡
# K" y- {* n% a识别模式,直到被主机识别到。 使用STM32CubeMX初始化的工程中会自动生成 SDMMC 初始化函数,向 SD 卡发送命令,当 SD 卡接收到命令后, SD 卡就会进入数据传输模式,而主机在总线上所有卡被识别后也进入数据传输模式。 所以在操作之前,需要先检查 SD 卡是否处于数据传输模式并且处于数据传输状态: 在main函数中首先定义一个变量用于存储 SD 卡状态: - <font color="#000000">int sdcard_status = 0;; }7 z$ M. x6 F2 z% z' E( W' d
- HAL_SD_CardCIDTypeDef sdcard_cid;</font>
复制代码然后在while(1)之前编写如下读取信息代码: - <font color="#000000">/* USER CODE BEGIN 2 */
/ o8 Z6 }0 x" R% K4 T - printf("Micro SD Card Test...\r\n");4 }3 d& E8 j4 U% Z; u# }# C, I
- </font>
复制代码& b8 J. n' F, _
/* 检测SD卡是否正常(处于数据传输模式的传输状态) */+ M/ M( j3 ` r- j
sdcard_status = HAL_SD_GetCardState(&hsd1);$ w1 q, s/ F3 H( e- B7 p
if(sdcard_status == HAL_SD_CARD_TRANSFER)
" s: l8 a+ z! ~0 X7 f( H5 k{$ r b: V3 _/ h8 n$ F- B0 q; c
printf(“SD card init ok!\r\n\r\n”); - <font color="#000000"><span class="token comment">//打印SD卡基本信息</span>6 F$ |! O$ |# l! x
- <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"SD card information!\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
5 P1 g+ |+ W# g1 K5 \4 ^$ m - <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"CardCapacity: %llu\r\n"</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">unsigned</span> <span class="token keyword">long</span> <span class="token keyword">long</span><span class="token punctuation">)</span>hsd1<span class="token punctuation">.</span>SdCard<span class="token punctuation">.</span>BlockSize<span class="token operator">*</span>hsd1<span class="token punctuation">.</span>SdCard<span class="token punctuation">.</span>BlockNbr<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
% m4 `! a) {% k- {* Q+ j3 I - <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"CardBlockSize: %d \r\n"</span><span class="token punctuation">,</span>hsd1<span class="token punctuation">.</span>SdCard<span class="token punctuation">.</span>BlockSize<span class="token punctuation">)</span><span class="token punctuation">;</span>
, g& T* ^( ]- ]% c$ t# A' f - <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"RCA: %d \r\n"</span><span class="token punctuation">,</span>hsd1<span class="token punctuation">.</span>SdCard<span class="token punctuation">.</span>RelCardAdd<span class="token punctuation">)</span><span class="token punctuation">;</span>; `5 ~/ G/ e6 U' s
- <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"CardType: %d \r\n"</span><span class="token punctuation">,</span>hsd1<span class="token punctuation">.</span>SdCard<span class="token punctuation">.</span>CardType<span class="token punctuation">)</span><span class="token punctuation">;</span># [4 e4 b; O! W# t8 e g
- 5 y6 j! i# a: i0 M! A. k
- <span class="token comment">//读取并打印SD卡的CID信息</span>/ p2 e' E6 h' c3 S
- <span class="token function">HAL_SD_GetCardCID</span><span class="token punctuation">(</span><span class="token operator">&</span>hsd1<span class="token punctuation">,</span><span class="token operator">&</span>sdcard_cid<span class="token punctuation">)</span><span class="token punctuation">;</span>3 x1 Z. O7 X$ O4 u+ [+ D
- <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"ManufacturerID: %d \r\n"</span><span class="token punctuation">,</span>sdcard_cid<span class="token punctuation">.</span>ManufacturerID<span class="token punctuation">)</span><span class="token punctuation">;</span></font>
复制代码
0 Z- D6 Y1 e+ r( w2 f
}9 d7 ]0 T3 r# E0 y' s: W( P; K
else
1 f! |/ q- O3 o3 W0 Z, ]{* P% U( N' t" G* G) N. }
printf(“SD card init fail!\r\n” );
: a% O& l* f3 q. Sreturn 0;
, ~" @& \+ V( g6 x+ j( [}
8 E' F0 o! r9 u" X2 e/* USER CODE END 2 */
T* Q- @- F( A6 F2 x4 D# ?2 [
编译下载后串口助手输出结果如下: 
4 | n0 U. I) e& A
擦除SD卡块数据为了验证实验的正确性或,先擦除数据: - /* 擦除SD卡块 */
6 D; m" z; c( U8 i" b! b" ~ - printf("------------------- Block Erase -------------------------------\r\n");
9 B- ?3 x6 \. j, g4 Z4 ` - sdcard_status = HAL_SD_Erase(&hsd1, 0, 512);
. L% T% k$ U* s$ S' @- z h9 s6 y - if (sdcard_status == 0): v1 G3 \ [5 Z0 m- {
- {
% K0 ~: L; Q. {( D, [8 V- K! E - printf("Erase block ok\r\n");
4 ?% o1 B$ P3 ?' J# ~- c - }
4 }2 W, m( E5 x/ V - else; e/ N, V7 g7 ]% a8 O/ p
- {
3 T6 ]( r- y) J' w$ A6 @ - printf("Erase block fail\r\n");
9 }* t9 i; D: b8 U) q - }
/ t* J( Y& R$ b0 y5 A! G3 z
复制代码
2 B. m5 A0 x" u" k+ C3 b读取SD卡块数据- /* Private user code ---------------------------------------------------------*/
% x8 K. n. \9 O L6 g/ `5 t - /* USER CODE BEGIN 0 */ P+ ?3 r/ o5 y5 e
- uint8_t read_buf[512];! K# A6 R' i" p$ s! A& ~/ G c. W
- /* USER CODE END 0 */
7 `+ L/ m: m! s1 e. }; o( x3 g
复制代码
# G* ?5 L9 o3 @# Q: I! y; [然后在之前读取信息的代码之后添加读取数据的代码: - /* 读取未操作之前的数据 */) f' y, L8 ]# ]- r6 \5 i( c% p
- printf("------------------- Read SD card block data Test ------------------\r\n");
8 [ v$ U' E) r, F( a' |3 i - sdcard_status = HAL_SD_ReadBlocks(&hsd1,(uint8_t *)read_buf,0,1,0xffff);
% D4 T- a b _* f+ \" R. w" Q( B4 d: n - if(sdcard_status == 0)
9 Y- F+ }" O2 B0 O3 d# J - { ! `8 h/ h* J- ?4 [# J7 g
- printf("Read block data ok \r\n" );# s$ s# G9 B# k9 a+ l/ [: j# C8 [
- for(i = 0; i < 512; i++)- d+ s9 ]7 w2 k/ P- X9 h
- {
& s2 ~2 t/ y. U& j' b9 { - printf("0x%02x ", read_buf[i]);% p7 b" ~* Q- y% k' K3 u
- if((i+1)%16 == 0)
* f9 v9 J0 p& B, _) }: V6 p - {1 v& p4 J5 s3 B; G7 Y" U% m
- printf("\r\n");5 Y7 S8 X# D5 {9 R; M
- }, ?* O" T2 k* Y
- }
# t& w4 c, \7 X$ _ - }0 K# J1 Q5 A5 f# Z* a4 U
- else( F, N2 n# T. G' `* ~6 ~
- {. L5 t8 q& v& V# x* u
- printf("Read block data fail!\r\n " );
0 L# W' F5 f& h e - }
+ L7 X- j" [- d8 s
复制代码
/ Z" A+ O" h7 p) N向SD卡块写入数据同样的,开辟一个全局缓冲区,用于存放即将要写入SD卡的数据: - uint8_t write_buf[512];
) G6 Q# K3 v1 F4 p
复制代码 ) Z' W4 r5 v2 @1 X7 W
然后在之前读取数据的代码之后添加的代码,将缓冲区的数据赋初值: - /* 填充缓冲区数据 */
& }5 w3 L* @- z8 y - for(i = 0; i < 512; i++)
! |; }' Z8 i$ S& K) q' H - { s+ Y" n8 u% i- T: I
- write_buf[i] = i % 256;# y& e/ \: J& k$ A8 f
- }1 y8 F( N; E- }2 G" |
复制代码
* h* S; m. H ~# p然后继续添加代码,将该缓冲区数据写入SD卡: - /* 向SD卡块写入数据 */ V4 [3 n. b" w7 v: q' H
- printf("------------------- Write SD card block data Test ------------------\r\n");
) A. K, h" G# ]/ ?6 ]8 v# x - sdcard_status = HAL_SD_WriteBlocks(&hsd1,(uint8_t *)write_buf,0,1,0xffff);; E+ e4 O, e! D) B5 j
- if(sdcard_status == 0)
: W" B8 W- g+ S - { - [- w; T* [" V
- printf("Write block data ok \r\n" );
6 [) H1 |; L: |* R - }
C6 i! f+ E* z" P( ~' w# }% Y. i - else
8 Y- L/ C8 r8 v# o - {
3 W7 l) f& S4 F+ j! | - printf("Write block data fail!\r\n " );
: r6 |/ m/ t3 r3 b7 a2 h - }, {7 R; E! a/ z+ E8 D
复制代码
$ z; K; V' O( _; b% a添加完之后,为了检查数据是否正常写入,再将数据读出: - /* 读取操作之后的数据 */' x/ S1 P% O; _! T' e% F& C
- printf("------------------- Read SD card block data after Write ------------------\r\n");
# V4 I8 r5 N: J1 ~* Y1 _ - sdcard_status = HAL_SD_ReadBlocks(&hsd1,(uint8_t *)read_buf,0,1,0xffff);* v# P# n/ c$ z! u; \
- if(sdcard_status == 0)
& q& U- Q/ N4 i; I0 n5 g" N - { 2 e% T( c+ s: u6 d5 |4 v
- printf("Read block data ok \r\n" );" E4 |6 X D3 g. t i
- for(i = 0; i < 512; i++)
0 A! f/ _3 p0 `( B- U - {
- ~* @; ]$ p9 C" I' A - printf("0x%02x ", read_buf[i]);
& k q( Z: {0 ~" k4 _ - if((i+1)%16 == 0)
" _6 M$ t, @; E: a1 M+ \ O5 ^ - {
6 b+ @. F" a! g - printf("\r\n");
# W; {# V: t1 t/ d; ` - }
* H z6 g% D$ H! ? - }
7 r/ Q- C& `' r- ~! b( u' d - }
3 q2 S$ f z4 q7 f4 u7 p' [6 V+ d& s
复制代码 5 [! I- L2 [3 F/ G* x
将程序编译下载,最终的实验结果如下: 
! v/ ]$ t9 L5 H5 q

( z# Q8 ?7 h/ Z+ b& D/ y

- K7 T2 W& m/ ^
 - k' M& s6 ?! ^$ `0 K, A# B
% f& v& s+ K0 K) s8 S
|