|
使用Platformio平台的libopencm3开发框架来开发STM32G0,下面介绍SD卡模块的使用方法。 ; d& e2 c {. e9 C: B3 n( V
1 新建项目在PIO主页新建项目spi_sdcard,框架选择libopencm3,开发板选择 MonkeyPi_STM32_G070RB; 新建完成后在src目录新建主程序文件main.c; 然后更改项目文件platformio.ini的烧写和调试方式:
6 M7 B* e3 i3 N( v
- 1upload_protocol = cmsis-dap
* _: G/ C% ?/ m- e { - 2debug_tool = cmsis-dap
复制代码 2 ^: U2 I, M, i
" p* G( x7 |$ ? b; `
2 编写程序2.1 加入FATFS库2.2 实现SPI接口的SD读写在src目录下新建spi_sd.h 和 spi_sd.c 文件,现在目录结构如下: 6 t H$ |, a @2 x; q+ _- w
' K$ k* O* d$ b7 Y+ e; D' p0 J# {5 N7 o4 G: _
spi_sd.h 头文件
( }/ B; b/ ]( t: [5 ~; I% H
- 1/**. ^, d9 Z9 ~ G0 D$ L8 W6 ]
- 2 * @file spi_sd.h
9 _2 \/ D/ t. A; Z) L. [ - 3 * @author MakerInChina (makerinchina.cn)
9 Z# t0 _; p$ {2 _0 h - 4 * @brief 7 N! _% S3 ^- `$ G% E
- 5 * @version 0.01
% y7 e, N4 ?- @( d0 K! ~ - 6 * @date 2022-09-188 |; }5 I+ E# W' u
- 7 * $ \5 F8 ~6 E; J' A, n) J) k
- 8 * @copyright Copyright (c) 2022
0 F3 l. E# N2 s9 @9 T( O - 9 * ( a0 T2 r) k0 L' p! X7 x- x
- 10 */+ K6 \# ]# C/ m
- 11
) u' D$ S6 V# N - 12#ifndef _SPI_SD_HEAD_H_
# P n7 \6 l! b( [0 f: `+ k - 13#define _SPI_SD_HEAD_H_
4 }' L+ q/ V; L* | - 14
& l% _: v7 ~3 {3 a - 15#include <stdint.h>) d+ d1 i6 j9 M4 d# L
- 16. v5 `/ g- N8 G7 L; ?( N- z
- 17/**
0 ]& ?' U# [+ v4 l* f$ N! g - 18 * @brief init sd card2 x. N' |: P/ p7 V
- 19 *
. f) v% h) Q4 T+ z7 }8 a - 20 */5 t% b% h6 o- h2 J
- 21uint8_t spi_sd_init();
/ E s/ n8 N: ^ - 22 a7 m+ a# H0 y+ z5 c1 [/ D
- 23/**
) s4 R {- d V1 G5 O- V& Z - 24 * @brief spi read sd3 w# {2 d/ B" e7 O' L7 u
- 25 *
4 d9 ^2 r) V D6 S/ N# g - 26 * @param buff 2 i7 W' t8 O- Y8 z% I3 I' h) U
- 27 * @param sector . N( Z- t% J! e& ^1 {2 s" Z4 O* b
- 28 */1 {0 ?' }2 z* q% V: v4 s3 M
- 29uint8_t spi_sd_read(uint8_t *buff, uint32_t sector);' g; X* o0 X- S( Q/ f8 F9 h; w
- 306 ^+ j- D0 \* g6 E( ]3 W7 [
- 31/*** q. ]% A( K; s Q& ~
- 32 * @brief spi write sd
9 c: x8 @' h7 e3 x* {% r& a - 33 *
# c- n0 q J/ z7 l. j - 34 * @param buff
$ I' a' v, X- i' | - 35 * @param sector & U3 g3 P6 F) v, W4 @, f" ^+ k
- 36 */
9 y5 Y) f" _3 K e3 a8 p7 M - 37uint8_t spi_sd_write(uint8_t *buff, uint32_t sector);
4 T3 R7 ]7 V: t0 K! H - 38) \5 U, F1 S) i, \
- 39#endif //!_SPI_SD_HEAD_H_
复制代码 + a2 `: _1 j4 l: W9 B
7 ?$ a$ L% F& X0 p% R
spi_sd.c 实现
" V D) ~8 ]3 Q$ \
- 1/**/ c; V7 k& \2 W( z7 E. l
- 2 * @file spi_sd.c
+ J$ x" S( q! w* y) j8 i2 ^7 a - 3 * @author MakerInChina (makerinchina.cn)$ P5 x# z! J1 _. c+ i
- 4 * @brief 6 y/ W" E! N2 T/ D8 ?
- 5 * @version 0.01
5 ^2 G' ?/ `& o% v' V) q - 6 * @date 2022-09-18
- D+ ?: ]7 t& V) l; w$ G3 E - 7 *
% \6 O$ w# D6 q' |6 _ - 8 * @copyright Copyright (c) 2022( `3 `7 d0 l' s: ?+ _$ f+ t" X
- 9 *
' A$ k6 l2 ~# f: g9 w( W3 j1 j3 } - 10 */9 K. i) `) a6 s
- 119 D2 n' @5 L0 @. t6 W G B; }4 b+ M
- 12#include "spi_sd.h"# S; H# `- X& E7 j# \, J
- 13
/ g2 @) J- H' k) M @/ g - 14#include <libopencm3/stm32/spi.h>+ }& W- ^9 ]. }0 ], w9 W% C3 F+ L
- 15#include <libopencm3/stm32/gpio.h>
) m3 h$ ^8 u/ Q( @ z, [/ d - 16/ l! K- u5 `/ Y I7 d1 ^
- 17#define SDSPI SPI1! I; ^7 c9 T% I1 I6 b# ?5 W
- 18#define SD_CS GPIO4 |- {% V' N! A
- 19#define SD_PORT GPIOA
6 \0 u3 F- Z% H' |+ ?& b! C - 20
$ D2 Z9 Y0 Y3 g* f4 `+ x/ Z - 21#define spi_cs_deselect() gpio_set(SD_PORT, SD_CS)
3 x& C8 ?% q. J! A( y5 y - 22#define spi_cs_select() gpio_clear(SD_PORT, SD_CS)
6 I1 @2 Q# ]5 K0 C, c - 231 B5 G" L. b1 |1 ?1 P% @
- 248 S5 q6 w( l( Y/ ?% E d
- 25/* Definitions for MMC/SDC command */3 C' M x- X2 [
- 26#define CMD0 (0x40+0) /* GO_IDLE_STATE */
6 ^* E8 o. l* I) Y x) x - 27#define CMD1 (0x40+1) /* SEND_OP_COND (MMC) */8 c, F# y1 K) H! p8 X+ [
- 28#define ACMD41 (0xC0+41) /* SEND_OP_COND (SDC) */, c* f" @# j; f5 o9 k+ A6 x5 v) G
- 29#define CMD8 (0x40+8) /* SEND_IF_COND */6 _, b! @1 i4 Q& S6 I
- 30#define CMD9 (0x40+9) /* SEND_CSD */
! T& p5 v" @- Y- f4 o, v/ }( } - 31#define CMD10 (0x40+10) /* SEND_CID */) T I2 @& z& w! L) c
- 32#define CMD12 (0x40+12) /* STOP_TRANSMISSION */
5 ^+ O) H1 k. ?; c2 V - 33#define ACMD13 (0xC0+13) /* SD_STATUS (SDC) */$ p6 v+ D0 F, m4 A
- 34#define CMD16 (0x40+16) /* SET_BLOCKLEN */
* T4 c" ^8 P, x) q9 p8 d! Y - 35#define CMD17 (0x40+17) /* READ_SINGLE_BLOCK */. M+ q% l6 P( p9 V2 J
- 36#define CMD18 (0x40+18) /* READ_MULTIPLE_BLOCK *// ~3 g7 D6 X! S% C8 Q2 e7 ]4 t& p* Y
- 37#define CMD23 (0x40+23) /* SET_BLOCK_COUNT (MMC) */; t4 |6 d$ S5 @5 f& R2 ]3 M) r
- 38#define ACMD23 (0xC0+23) /* SET_WR_BLK_ERASE_COUNT (SDC) */
$ N6 S0 A: h7 @+ ]: _8 ? - 39#define CMD24 (0x40+24) /* WRITE_BLOCK */* K0 R3 l/ Z. u9 D, n3 T$ o# x
- 40#define CMD25 (0x40+25) /* WRITE_MULTIPLE_BLOCK */
( G; \5 P/ N/ S* F1 j - 41#define CMD55 (0x40+55) /* APP_CMD */# ^" c& _! P' L! I
- 42#define CMD58 (0x40+58) /* READ_OCR */
! a! c2 u* C. F' C, A - 43
2 _, n& t D8 x- r - 44#define CT_MMC 0x01 /* MMC ver 3 */6 \: d/ R5 ^: |# k0 K5 y7 x/ n
- 45#define CT_SD1 0x02 /* SD ver 1 */
+ W% H; |3 W' L7 m7 c) N - 46#define CT_SD2 0x04 /* SD ver 2 */
( k& o& m0 Y/ z- H9 ^* K - 47#define CT_SDC (CT_SD1|CT_SD2) /* SD */
! H; C( d; @% A - 48#define CT_BLOCK 0x08 /* Block addressing */! c3 q; I7 C, i+ L* y
- 49
]. ]8 B1 i' y+ G0 i: r - 50static uint8_t spi_read_write8(uint32_t spi, uint8_t tx);% D: \ j0 s9 g
- 51static uint8_t wait_ready(void);2 v# L$ a# j1 r8 ^
- 52static uint8_t send_cmd (uint8_t cmd,uint32_t arg);8 w3 F2 x, H6 b' q
- 53static void set_spi_slow();
( `2 g- |1 F1 ^0 R0 e - 54static void set_spi_fast();* l) L$ b7 _9 C1 d% ~
- 55
; _9 x0 G4 O' _* Z, K {. E. }" w r - 56/**- A$ k0 P' s# Z9 ^, r7 s9 Y
- 57 * @brief init sd card' d3 J# b ~8 o: }) F& b
- 58 * + ?9 a9 H- A2 f& x9 @) S
- 59 */7 b* g8 s& @# `* @ u8 B
- 60uint8_t spi_sd_init()' l( @! c: B; a2 x2 M2 _
- 61{4 c; h2 k: s; \! ]
- 62 uint8_t n, cmd, ty, ocr[4];
* B' t' [2 L' Q: o' Y8 t- i - 63 uint16_t i;
- S" X. V1 J% r1 t7 Q& b - 64
# B/ L3 F( t/ l4 v - 65
+ o7 I. L3 R( w3 I z! a - 66 //init with low speed
P* \: m; c& ~$ k; I) [: Z) d - 67 // set_spi_slow();. h3 X; {# X. W
- 68% D6 E q" Y7 g* q) M
- 69 spi_cs_select();& l" ^% o u, c4 |2 y
- 70
% {& T! R5 Z( p' ? - 71 for (n = 10; n; n--) spi_read_write8(SDSPI,0xff); /* 80 dummy clocks */
3 @ I2 Z& U- Q1 r6 p+ A1 _ - 72
0 d2 G* N6 [/ T( B; R% R. d - 73 ty = 0;1 j* S9 K8 m7 t' b( f/ ^* @5 ?
- 74+ C3 R6 ~/ y" I! b& c$ T
- 75 /* Enter Idle state */" a3 U; q3 r% k1 a& v$ Z5 G2 O
- 76 ty = send_cmd(CMD0, 0);! w) k: u8 }5 x
- 77
) J9 ~) P, I2 F; D# u4 L* t6 N# q2 d1 u - 78 // printf(" > enter idle:%d\n", ty);3 f" M: C' k( u" F' n) N& m
- 79
' Z) {( Z/ {0 M: F - 80 /* Initialization timeout of 1000 milliseconds */, o5 g. b& w* m4 q. V3 K9 A
- 81 /* SDHC */
' t* T% Y2 U$ c; D - 82
6 K/ l/ q. i3 P' f+ { - 83 if(ty == 1){4 F" U; _7 @# D2 g+ L" ~
- 84% D* y& b5 V/ i# f U2 b
- 85 if (send_cmd(CMD8, 0x1AA) == 1){ /* SDv2? */
, K1 O6 E! P! X) a% d# b - 86 /* Get trailing return value of R7 response */
, ?' P/ O3 j! C* U+ T/ K4 d - 87 for (n = 0; n < 4; n++) ocr[n] = spi_read_write8(SDSPI,0xff);" J2 f9 C& A# J$ X
- 88 /* The card can work at VDD range of 2.7-3.6V */
9 z* v) ^* T, `/ v" ?4 z; M% T - 89 if (ocr[2] == 0x01 && ocr[3] == 0xAA){
6 w+ o$ `, S7 T' o) F - 90 /* Wait for leaving idle state (ACMD41 with HCS bit) */
2 E% k4 A) P. L9 e7 a G - 91 i=0xfff;6 U+ s! V( c2 F
- 92 while (--i && send_cmd(ACMD41, 1UL << 30));3 Z1 s) d' Q4 S. x4 y; k
- 93 if (i && send_cmd(CMD58, 0) == 0){
{3 o' j+ \1 C, I. X( V. S - 94 /* Check CCS bit in the OCR */0 }5 j$ F9 t. m4 h9 X# j4 s
- 95 for (n = 0; n < 4; n++) ocr[n] = spi_read_write8(SDSPI,0xff);
[$ c; Y3 x9 V/ ?& i - 96 ty = (ocr[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2;: X: r6 H2 _ ]
- 974 u- s- l- Z$ T) V5 ?9 O1 ^3 n
- 98 }+ Z8 |. a. q G6 y8 M
- 99 }
8 Z3 }8 g" r/ k! E2 U& r2 I - 100 } else { /* Not SDv2 card */5 l. h) z: N+ L# I6 J3 L. Q7 ?
- 101 // if (send_cmd(ACMD41, 0) <= 1) { /* SDv1 or MMC? */
0 F" e+ [: G; I! D; m - 102 // ty = CT_SD1; cmd = ACMD41; /* SDv1 (ACMD41(0)) */: i, @7 H5 m: S! D2 K% @9 e
- 103 // } else {
5 D, U; ]9 N3 x$ M8 ?3 N - 104 // ty = CT_MMC; cmd = CMD1; /* MMCv3 (CMD1(0)) */+ G% _; }0 J9 r9 U" \
- 105 // }4 y. s0 ^, o; |) f% g* D
- 106
2 u; ?* A: |8 J, w/ f2 E9 a - 107 // while (SPI_Timer_Status() && send_cmd(cmd, 0)) ; /* Wait for end of initialization */! f/ P2 b6 P u/ Y6 s5 j
- 108 // if (!SPI_Timer_Status() || send_cmd(CMD16, 512) != 0) /* Set block length: 512 */& v. Z9 H1 @* G/ d
- 109 // ty = 0;5 Q' e6 D) m- `2 [; v& x
- 110 }5 h' @2 A: e# c* i a: K5 \
- 111
1 a S8 }* }8 l; X5 A - 112 }
' F: n+ p; v4 H; E, O( F - 113 //CardType = ty;, A, S0 I' x8 d I
- 114) i u6 _+ G1 p/ S9 y
- 115; l# Q8 S) B9 R5 N& x4 g- `
- 116 spi_cs_deselect();0 e% d# J. D3 t. ?4 u
- 117 spi_read_write8(SDSPI,0xff);
7 X& Q+ y! X3 c2 n$ `# {/ k - 118 while (SPI_SR(SDSPI) & SPI_SR_BSY);
. E: e5 K- u2 i5 |& d: e1 s( p - 119( J7 P6 N, Z: X: O; S3 L! v }
- 120 set_spi_fast();- q3 R# J9 ?, N7 N& |1 P
- 1217 W. y0 m$ c+ l% K6 ]
- 122 return ty;
" i# S/ G. m& d) V- Q G3 o - 123}
- P+ ]" j0 w5 u" ^; m9 P& k4 H( u5 q+ B - 124- D. x) I/ ?; j; K ^
- 125/**+ o+ ?' k" v7 I% @4 e: G, O2 |
- 126 * @brief spi read sd; o1 p: X3 ~) y0 S8 {
- 127 *
% ` L5 E; @$ D- {: W- c - 128 * @param buff
0 L9 l$ Z4 A$ |* _2 c0 t - 129 * @param sector
; G. w/ v. q9 p/ M+ M - 130 */* w9 z: K" T2 n* P
- 131uint8_t spi_sd_read(uint8_t *buff, uint32_t sector)! ?3 F5 ]( M/ b/ A2 m3 H3 R/ F
- 132{0 T9 f4 p0 M. ^8 O) ]& a
- 133 uint8_t result;# B6 S* P' w& q. Q
- 134 uint16_t cnt=0xffff;
! s0 n1 I! m3 K6 c$ w - 135 spi_cs_select(); & T6 m7 V/ }4 I' O# r7 f
- 136 result=send_cmd(CMD17, sector); //CMD17 даташит стр 50 и 96* P1 v3 l2 ~( l# k( y
- 137 if (result){spi_cs_deselect(); return 5;} //Выйти, если результат не 0x00+ O2 F! z* K* ^5 B9 w
- 138/ i G. [% k% u
- 139 spi_read_write8(SDSPI,0xff);
+ X6 ^: I) K$ N6 N: r! z- D - 140 cnt=0;; _ N$ j/ O0 B, b$ M5 c. D
- 141 do result=spi_read_write8(SDSPI,0xff); while ((result!=0xFE)&&--cnt);
0 |$ \( [, }5 Y6 w - 142 if(!cnt){spi_cs_deselect(); return 5;}' }0 F/ f( Z) F5 E
- 143" M, l4 X5 N9 b0 g5 v4 r1 y0 A4 i
- 144 for (cnt=0;cnt<512;cnt++) *buff++=spi_read_write8(SDSPI,0xff); : o+ Q6 R( k+ z3 |
- 145: E S5 z! \9 G: `
- 146 spi_read_write8(SDSPI,0xff);
3 U& H X- ^7 G- X7 B - 147 spi_read_write8(SDSPI,0xff);
% J5 ^( Z3 j7 _ _, G; [ - 148 spi_cs_deselect();
2 k1 z% y" q k; a% F - 149 spi_read_write8(SDSPI,0xff);
$ N$ ^* Z$ F6 d' N' j: g% p - 150
0 U+ {& U t% j9 i: y1 `* g- { - 151 return 0;
: D0 Y( V. R, ? - 152}. T* u: S U; d# {' W# J
- 153
. f! c1 m; p/ b4 S& _" c; o: _& f' y - 154/**
8 g/ }9 Y- f4 f/ w5 W! [ - 155 * @brief spi write sd) y( ?, c/ |5 P, k( ~+ U! x4 m
- 156 * $ t# S& e- f& W5 s, T1 a# i
- 157 * @param buff
4 a: T- A+ d! D! X; q - 158 * @param sector
' c; _% \$ _" T8 S( f0 z7 o - 159 */
k( @; m; G3 |/ U$ _- R5 Q - 160uint8_t spi_sd_write(uint8_t *buff, uint32_t sector)
- |( b" V/ w! Z2 L, t5 C' q. D - 161{. w$ Z) B- S) L
- 162 uint8_t result;- K! r) R5 r- I, H4 Z1 v7 p- T7 L. g
- 163 uint16_t cnt=0xffff;
) N' p* ]- [) K$ Z" C - 164 spi_cs_select();5 R* C) b% j3 @
- 165 result=send_cmd(CMD24,sector); //CMD24
9 {% A# \. k' \) A/ b# `4 F - 166 if(result){spi_cs_deselect(); return 6;} //- ? Z' j+ G% {3 k5 j' B* T& y
- 167 spi_read_write8(SDSPI,0xff);% l/ ?) }. R+ z$ J1 ~
- 168 spi_read_write8(SDSPI,0xfe);//. `1 {9 t: ?9 d9 h" L; E/ w; W' N
- 169 for (cnt=0;cnt<512;cnt++) spi_read_write8(SDSPI,buff[cnt]); //Данные
, C1 C1 X' e& ~, p C, p - 170 spi_read_write8(SDSPI,0xff);
3 ~' c; o5 \2 C2 g- h3 G2 T( R - 171 spi_read_write8(SDSPI,0xff);
9 f9 r3 H' l% W9 I" m - 172 result=spi_read_write8(SDSPI,0xff);
. m4 L; p( q) [5 Y: L - 173 //result=wait_ready();& K: V9 r- q& f! ?9 \. J5 A
- 174 if((result&0x05)!=0x05){spi_cs_deselect(); return 6;}
c( x0 a- r6 d - 175 //spi_read_write8(SDSPI,0xff);
/ z2 S4 j* z+ f: z7 j @( ? - 176 while (SPI_SR(SDSPI) & SPI_SR_BSY);1 s+ K [+ j3 v6 u& c( H
- 177 //
# _3 Z/ {: N$ A- D - 178 spi_cs_deselect();
& x% \3 I3 |7 r4 U* M1 S - 179 spi_read_write8(SDSPI,0xff);9 p7 z/ \8 O- g7 {0 |7 c
- 180 return 0;
: ~0 A+ o: w. N1 k8 V- ]5 _* ]9 m - 181}
7 x$ C$ A: X! z. A( q - 182
/ x2 {+ s' g5 ] - 183& L0 _4 U" Y5 ^0 \7 a9 T5 x
- 184static void set_spi_slow()7 ]! d) P A: z" B5 \
- 185{/ ?) V2 S5 d5 x8 s) S) ]
- 186 // spi_disable(SDSPI);
' i; A. l& ~7 x8 i# P - 187 spi_set_baudrate_prescaler(SDSPI,SPI_CR1_BAUDRATE_FPCLK_DIV_128);
; e7 ~ V* t" D6 a- o% @' {# C - 188 // spi_enable(SDSPI);" j: \; X q9 t+ y U! n
- 189}
4 ]5 X* L6 K$ J+ a9 W' G+ t$ I9 q - 190
1 `3 ~4 U) R1 P# F J7 [ - 191static void set_spi_fast()
* w: X; u; ]1 Z; B - 192{
0 e) M8 A; i, ?) M ]/ R" d. A. @ - 193 // spi_disable(SDSPI);
% } ~! p8 \6 X/ G0 e6 O* u - 194 spi_set_baudrate_prescaler(SDSPI,SPI_CR1_BAUDRATE_FPCLK_DIV_8);, i4 A. c5 Q$ X3 I- w# Y: K
- 195 // spi_enable(SDSPI);
1 e( i/ Y$ ~# D7 k! U - 196}
' f4 S2 k# W# U& W. d' M! m0 U' x - 1976 ?9 f9 a* X0 h! h3 O8 K: E
- 198static uint8_t spi_read_write8(uint32_t spi, uint8_t tx)5 C/ m) w# L8 N
- 199{# \9 |3 D* Y8 r/ ]; _% l
- 200 spi_send8(spi, tx);
9 V; S: ]( P; C* t8 V# c - 201 return spi_read8(spi);$ J3 Z( ]; A# I! P7 Q" W
- 202}/ v7 v6 e0 D0 e
- 203
% D9 Z. F4 p0 N; O$ j# `% q - 204static uint8_t wait_ready(void)+ Q& v: c( F* I: H+ F* E6 X* l, D' K6 T
- 205{
) X. T0 h2 M' X2 _$ X - 206 uint8_t res;
6 K+ }% q) J. @2 j - 207 uint16_t cnt=0xffff;4 G' H; ?5 f5 ]
- 208 spi_read_write8(SDSPI, 0xff);* z4 Q$ ^: C- x( B; f2 e! q
- 209 do res = spi_read_write8(SDSPI, 0xff); while ((res!=0xFF)&& --cnt );
* Z4 g3 @" Y6 `9 s - 210 return res;
4 F$ v/ T' U% l; x8 G - 211}
! q4 F7 x! _) a& ]3 { - 212
. ?$ M, O; l- d5 M8 G - 213static uint8_t send_cmd (uint8_t cmd,uint32_t arg)
& P2 D5 o/ }. }- L - 214{4 u5 u3 U& Y- E: @4 c0 O
- 215 uint8_t n, res;: C7 y0 C2 d0 |2 U( W* x, Q: p9 Y9 ]
- 216
3 ^0 [" p2 x# e# _+ f( V& r - 217 /* ACMD<n> is the command sequence of CMD55-CMD<n> */
( O& j& g- }; ]( [( [: t - 218 if (cmd & 0x80){" D+ b& S4 j' b- F* @) ^
- 219 cmd &= 0x7F;/ c+ L. d) d3 k7 K* |
- 220 res = send_cmd(CMD55, 0);
- q* W, C) X: G0 m6 E- |" e$ I: ~8 h - 221 if (res > 1) return res;1 I; h5 W4 k* q, U5 m: r
- 222 }
" ^! A$ Y$ e5 p* r. r# h+ I2 ? - 223
0 o& G' @' s9 M - 224 if (wait_ready()!=0xFF) return 0xFF;
( N! I1 b4 L3 D+ K - 225 /* Send command packet */
1 R9 J% h7 }0 f* \% H - 226 spi_read_write8(SDSPI, cmd); /* Start + Command index */
2 J( z) q) T, J" a5 ]3 ^ - 227 spi_read_write8(SDSPI,(uint8_t)(arg >> 24)); /* Argument[31..24] */, _0 n4 D1 y D2 l& H
- 228 spi_read_write8(SDSPI,(uint8_t)(arg >> 16)); /* Argument[23..16] */
( ?% Q5 q6 {5 L x& f - 229 spi_read_write8(SDSPI,(uint8_t)(arg >> 8)); /* Argument[15..8] */1 v ]- P$ W& Q* p8 f# p. r
- 230 spi_read_write8(SDSPI,(uint8_t)arg); /* Argument[7..0] */& B; W1 l# E8 |
- 231 n = 0x01; /* Dummy CRC + Stop */: s9 O% r9 d$ I1 M, y6 \ x3 T
- 232 if (cmd == CMD0) n = 0x95; /* Valid CRC for CMD0(0) */
9 c; k e% n6 ~$ Q# [8 x4 Z3 W; g: o - 233 if (cmd == CMD8) n = 0x87; /* Valid CRC for CMD8(0x1AA) */
# D7 \6 L; L( j; t! V# G - 234 spi_read_write8(SDSPI,n);
3 Q& G& [4 g9 H - 235 /* Receive command response */% v- H) l& D( ]
- 236 if (cmd == CMD12) spi_read_write8(SDSPI,0xff); 2 k" [3 F8 p- m) m3 l' T( N; p+ j4 W
- 237 /* Skip a stuff byte when stop reading */
9 V1 e; j- |7 ~% H, j9 e$ R - 238 /* Wait for a valid response in timeout of 10 attempts */* R, `+ G; p0 ]0 W
- 239 n = 10;
* j. a8 R: v0 z: h5 F' {5 k% ~& {0 b - 240 do res=spi_read_write8(SDSPI,0xff); while ((res & 0x80) && --n);* p; g) H/ O8 t0 D$ H! J2 f
- 241# ], M! h6 ?: j6 p5 ~/ J
- 242 while (SPI_SR(SDSPI) & SPI_SR_BSY); //wait if busy G# M& ^ [% G/ }. C
- 2432 t/ k9 ~) n: ]& d( s
- 244 return res; /* Return with the response value */$ x" V2 P( i: z' m& {- r) A
- 245}
复制代码
( a0 I, y9 t6 w8 m2 G4 J" H, \7 F; n- V8 e% U
注:这里初始化部分只写了SDv2的判断; : r6 ~$ r/ F" g) s1 I
diskio.c 调用 spi_sd的读写接口: " E+ E! [/ a! j6 U+ Q/ d
- 1/*-----------------------------------------------------------------------*/
: |5 `) w+ {( ` I - 2/* Read Sector(s) */. R' i- { h* [! D1 s U0 B8 j
- 3/*-----------------------------------------------------------------------*/
& N& f! j+ t' w5 r/ |3 [ - 41 U# z% e, f8 M* U' u
- 5DRESULT disk_read (
! c2 j; o& G3 f, F7 { - 6 BYTE pdrv, /* Physical drive nmuber to identify the drive */, [7 f8 Y" M: f
- 7 BYTE *buff, /* Data buffer to store read data */$ Q3 r' e# u2 \; w T+ n
- 8 LBA_t sector, /* Start sector in LBA */
. s' n {3 A8 ~/ ? - 9 UINT count /* Number of sectors to read */
9 |6 O4 E- e% T V - 10)1 J4 q) E3 l: `4 o& R
- 11{
5 ?8 h: j+ w: u4 c - 12 while(count){
# C% w" K$ J* M6 ]2 B/ ^* J - 13 if (spi_sd_read(buff,sector)) return RES_ERROR;( z/ R$ a2 w& `4 N* q# j2 m$ H: {
- 14 --count;7 o5 X4 ~# E4 i) t: S
- 15 ++sector;
1 k8 M% ]$ m/ L: A - 16 buff+=512;& P0 N8 H+ [# Z) V# p
- 17 }
* H# P; u# r, y0 h - 18 return RES_OK;
( I8 }% G4 L2 k2 R0 { - 19}
. J1 Z: }0 k5 Y; S - 200 ]& L# [; K4 [& l: [' Y
- 21
5 {7 {8 _+ u1 l8 T- p0 L3 T - 22
# j* |" O- i) l; t: A - 23/*-----------------------------------------------------------------------*/) d f7 e% `' q* O6 g0 ?; R
- 24/* Write Sector(s) */8 z# g) x' h. {& p
- 25/*-----------------------------------------------------------------------*/
5 r. p4 G$ L- \7 b. t+ p. ~ - 26
x+ a* E* z$ i; i" z7 S5 \ - 27#if FF_FS_READONLY == 0
& U1 U. Y( o1 ^ - 28
3 H S! O6 P% d9 f7 d - 29DRESULT disk_write (
: q0 w5 C& @' a - 30 BYTE pdrv, /* Physical drive nmuber to identify the drive */$ ]0 U5 E1 }8 }8 h% u, P
- 31 const BYTE *buff, /* Data to be written */6 j% m g( R9 ^4 Z
- 32 LBA_t sector, /* Start sector in LBA */
$ p1 x3 r: n# W" u* J } - 33 UINT count /* Number of sectors to write */) `9 Q# Q6 X. d- n% {
- 34)) n$ B) _$ X ~0 J9 D
- 35{, s4 ?7 \# k" T6 C' U( b
- 36 while(count){
% M1 |0 _+ C9 m- b3 e4 c4 U - 37 if(spi_sd_write(buff,sector)) return RES_ERROR;
: W8 a( H% G0 J5 ?$ V; x N - 38 --count;
" D0 `0 @+ [8 W) `6 S5 x- { - 39 ++sector;
# W! ~1 E3 D% ?5 i. r$ B - 40 buff+=512;
9 Z& E8 a( U% d: D9 m/ b - 41 }9 ~( g, m( W: ?1 T
- 42 return RES_OK;
- |; B8 k3 o4 J+ K2 X - 43}
复制代码 ( {3 I* d! l* ]3 ~' g, A* Y
6 A" Z& {2 l( K+ H2.3 SPI接口配置 - 1static void spi1_init(void){$ H! s& Z! v( F& t
- 2 //spi1 - display* _/ m7 r* f7 }7 u: u. B+ f3 `
- 3 /* Enable SPI1 Periph and gpio clocks */
" k( V) P, g2 A, c; e+ u - 4 rcc_periph_clock_enable(RCC_SPI1);5 f& ^' n' A# J. K% ?& q( X
- 5 rcc_periph_clock_enable(RCC_GPIOA);5 j% i2 q& m! J: d+ G
- 67 [; s) \# S. ~' O& f) V
- 7 /* Configure GPIOs:
/ e* @* t ]- w. I( r% [ - 8 *
3 t' r) S- a7 h" o o9 | O - 9 * SCK=PA5
$ G( _, `/ x6 {6 K+ M% Z( d- q - 10 * MOSI=PA7 % Y9 ^, P' {. G$ V3 ]
- 11 * MISO=PA6
% l& s; Y4 _- A9 ]; Z - 12 *
7 {" g6 I& U* u- Z - 13 * for SD card# k6 ~+ v# W3 d+ o
- 14 * SDCS PA4
t9 {1 P3 }, c' S! f6 f. B T) ]: V - 15 */
& X+ T0 ^9 t% M) {( K( Q2 u - 16* n% t9 y6 R! }2 z# ~+ R! a7 y
- 17 //MOSI & SCK & MISO( V5 _3 t- t$ J/ ]+ w6 t
- 18 gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE,GPIO5|GPIO7|GPIO6);
7 z" ^/ v8 s8 V! `( _ - 19 gpio_set_af(GPIOA,GPIO_AF0,GPIO5|GPIO7|GPIO6);; d% K3 D+ y& E% X
- 20 gpio_set_output_options(GPIOA, GPIO_OTYPE_PP,GPIO_OSPEED_LOW,GPIO5|GPIO7|GPIO6);
4 S1 o5 J+ ?3 J+ }6 P, u4 N - 21& w& H/ e1 m1 z
- 22 //SDCS & H w! z* j) M) O' H1 G* ?; R
- 23 gpio_mode_setup(GPIOA,GPIO_MODE_OUTPUT,GPIO_PUPD_NONE,GPIO4);
$ @5 i# r: R: N: S D# ? - 244 e" v( e1 V6 l! [4 e
- 25 gpio_set(GPIOA,GPIO4);# Q/ R3 ^4 K# C- G0 n R3 [
- 26
5 ^5 W( [+ l# K1 t. O# ] - 27 /* Reset SPI, SPI_CR1 register cleared, SPI is disabled */9 R0 U- e) Z G# e5 A
- 28 spi_reset(SPI1);
/ }) w* D. y: R2 i, C6 F - 29& h' Z' W& D: U: o5 \
- 30 /* Set up SPI in Master mode with:
& A Z2 G, y- N2 _6 w! N - 31 * Clock baud rate
, p8 s( ^2 |/ Z1 d8 E0 a8 T - 32 * Clock polarity3 P, L: _2 C) R2 ^, L: ]
- 33 * Clock phase
" C6 |3 G4 V4 Z' s2 p - 34 * Frame format MSB- T8 M- }6 P1 _1 ?& _
- 35 */
( x; {( x" u5 \, U1 B - 36 spi_init_master(SPI1, SPI_CR1_BAUDRATE_FPCLK_DIV_128, ' J0 n- B1 J$ l% c9 Z( e' ]1 L, \! m
- 37 SPI_CR1_CPOL_CLK_TO_0_WHEN_IDLE,0 v) o4 H8 y, O" C4 p! Y
- 38 SPI_CR1_CPHA_CLK_TRANSITION_1,
+ ]8 X. z) N9 ?! Q6 A( [ - 39 SPI_CR1_MSBFIRST); o' a4 z3 a& d' B& C% x+ l N
- 40
8 r! \& h, ?) Z, U! f& B1 E - 41 spi_set_data_size(SPI1,SPI_CR2_DS_8BIT);. M. n/ J+ ~5 W. q
- 42 spi_set_full_duplex_mode(SPI1);& Q0 C/ }* B( f9 i; u( ]6 l
- 43! Y' j6 Q$ ~& g$ i1 L0 n
- 44 spi_fifo_reception_threshold_8bit(SPI1);/ @$ f+ w5 Z" n, d5 D6 c/ Z* I
- 45
4 V- p9 d2 h: @) P2 F - 46 SPI_CR2(SPI1) |= SPI_CR2_NSSP; //NSSP, ?? clock continus
4 ?. z) h# q1 O3 a: l - 47 // spi_set_unidirectional_mode(SPI1);
/ F) c7 I! V9 _2 b8 X - 48 // SPI_CR2(SPI1) &= (~SPI_CR2_FRF_TI_MODE); //motorala mode6 W# d/ c+ w; s( \3 t
- 49 // SPI_CR2(SPI1) |= SPI_CR2_FRF_TI_MODE;! s6 G0 [; ~% J) l! y4 X- H
- 50( ~8 K2 e, f+ z1 O Y4 l' b4 {9 Q Q, D
- 51 /*4 A9 f5 I; u8 W0 q
- 52 * Set NSS management to software.6 Y$ f$ p+ a& v; h' Y$ l- V" ?/ J3 L* w
- 53 *
9 Z1 L9 k, p, m( c - 54 * Note:% d" L, k- R Y
- 55 * Setting nss high is very important, even if we are controlling ! z) n4 |+ m) h1 Q
- 56 * the GPIO
2 g" l f: h) f( f - 57 * ourselves this bit needs to be at least set to 1, otherwise the spi
3 M! `: I% j& ?. W; w - 58 * peripheral will not send any data out.
; E" I2 u" ?' u: G; n( [4 G0 w - 59 */5 {0 c- _2 y, X/ H; {. o# i0 u
- 60 spi_enable_software_slave_management(SPI1);4 B, N1 ~) c, R4 t
- 61 spi_set_nss_high(SPI1);
3 X* q, Z2 D$ N! J - 62
, M; P8 B ?9 Z6 L - 63 /* Enable SPI1 periph. */
& N$ g+ r9 J- L" N( S0 _ - 64 spi_enable(SPI1);8 V3 e& q6 W" o8 Y' Q
- 65
# M( J( [( i% I- ?" X- O - 66}
复制代码 6 j: r# b! C+ ]9 l6 m: S" A
4 C! X6 u: P0 g( E; u H: Y2.4 SD卡读写测试 4 L- |; L; v0 T
- 1printf(" init spi.\n");1 x, d! S# i) m7 a+ I+ ~- Z4 t, l
- 2
, O9 |2 M2 k( W$ ?% @6 O - 3//spi7 K' O$ y6 z& o9 G m4 A) K
- 4spi1_init();
! z* s& u; u. ] W) X7 y, U - 57 S2 _" Z1 Q4 c
- 6//sd
0 I2 T1 ^+ j2 l! n! U. |0 a - 7uint8_t sd_type = spi_sd_init();5 S$ j1 F7 W0 k( z* _4 U# p
- 8
' ^& M3 Z+ r; W3 g( E9 w4 H1 h - 9//sdv2: 0x08|0x04 . 0x0c" q& C( Z" t) u$ _& g
- 10// printf(" sd_type: %x\n", sd_type);
( q8 Z9 ]$ W; V0 Z - 118 u( g1 p, P- p( w: o$ y L5 v5 ]
- 12FATFS fs;
1 d' w" o$ \. y# X7 i+ T - 13FRESULT res;
" S' I( n$ x( C - 14res = f_mount(&fs, "", 0);
$ g6 j2 w1 ]* c3 Z9 ~" E9 R' b. q - 15if(res != FR_OK) {7 |0 p* J; f O2 b
- 16 printf("mount fs failed, res = %d\r\n", res);0 F2 v! v _% z$ K5 v2 E& h& \
- 17 return -1;3 ~# x, _6 U- Q
- 18}else{
! }5 h, U9 \9 u! I4 O1 `# V# S - 19 printf(" mount fs OK.\n");
# c' j7 v, p2 ?' F3 ~4 |# w - 20}/ c7 T2 P. I$ |7 n) e
- 21
7 t, l6 K% F( D: j; `3 ]( ` - 22FIL fd;
5 K' C# n$ _; j& j3 f. F9 ~7 ] E* c - 23
$ U. m( k8 g# o. X4 m& ?# f* w - 24res = f_open(&fd, "test.txt", FA_CREATE_ALWAYS|FA_WRITE);/ R {1 H m" V1 P4 k% _
- 25if(res != FR_OK){: f" x# j5 @# I( l$ K! y z% [
- 26 printf("open file failed: %d\n", res);
; R) P0 z8 z4 v4 L' ~ - 27}else{, i9 f9 n4 ?3 ?5 e- f8 `0 R: u
- 28 printf("open file OK.\n");
9 W6 B7 c$ T. ^& T- ~! g9 f - 29}
' m5 N- T5 z, e) o8 B$ Y - 30
# i" `+ B% b& u+ D% k - 31char *buff = "test data to write to fs\n";
1 y8 F* y% s+ q4 }/ `! a. I, m; k5 o+ T! E5 } - 32uint32_t len = 0;
" n- u$ S. h: j, R7 \5 ] - 33res = f_write(&fd,buff, strlen(buff),&len);/ g+ B# R5 ~4 p7 J
- 34if(res != FR_OK){, M- c* u) m" Y
- 35 printf(" write file failed.\n");
. F! c' ]5 q; U) B4 W0 n - 36}else{
* h( [* s: z1 a: }' a5 @; q% e - 37 printf(" write file OK, write size %d .\n", len);
" m% _5 m) |, M - 38}5 h# [4 g: ^1 c
- 39
' c! s. H7 G' _1 ^ - 40res = f_close(&fd);
1 G7 @* _5 {+ i7 H/ P2 G& a( W - 41if(res != FR_OK){
1 D2 d+ I8 l# J! f7 Z - 42 printf(" close fs failed.\n");( p& J5 p; _% b- w0 [5 @2 b
- 43}else{# S3 v1 ]% r/ I0 y* w4 P
- 44 printf(" close fs OK.\n");5 a/ X3 p! J9 o# {
- 45}! U, U3 v; L' g2 M1 R p! j" v
- 46
; u3 C3 x4 }: `" X$ x - 47res = f_unmount("");
6 y4 o: ]; `0 a V; W1 E- ? - 48if(res != FR_OK) { i1 x* t0 {, m I
- 49 printf("Unmount fs failed, res = %d\r\n", res);4 g( T* b9 [0 M' T$ T9 j2 J4 R! c
- 50 return -1;0 S: _- t- m n( _
- 51}else{
7 _1 M; j- F$ H( Q5 c: x& v - 52 printf("Unmound fs OK.\n");
4 r% q+ V' |# v& y' g3 Z- u - 53}
复制代码
8 U# P; ?, @! a: j9 }6 s( {8 D' }. a4 d/ R% F
main中测试SD卡挂载、读写; 6 ?0 K4 K6 C) ?+ e. Z# p& i2 {
3 硬件连接硬件引脚按如下方式连接到SPI1
- E2 Q; W9 s! [. B ! R `' ?/ N9 O6 m
- o, ]8 |; m; |+ t. W
; A7 h* h& {6 F# i2 @, ^: u7 ~$ J4 烧写测试将程序烧写到开发板后,打开串口,可以看到测试成功,卡中写入文件在电脑显示正确:
/ q; q* t$ _8 A6 P) b8 o9 R, _" M, G 7 K2 G" c, i, l5 L
3 j& `/ X: Q7 }9 ?: @% q
9 f P! _* ]& F4 | |