-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSD_SPI_Initialization.c
209 lines (161 loc) · 7.22 KB
/
SD_SPI_Initialization.c
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//############################################################################
//
// FILE: SD_SPI_Initialization.c
//
// TITLE: SD/MMC Initialization Functions
//
//############################################################################
// Author: Hector Ta
// Release Date: Dec 2021
//############################################################################
/* ***********************************************************
* You may not use the Program in non-TI devices.
* ********************************************************* */
#include "DSP2833x_Device.h" // DSP2833x Headerfile Include File
#include "DSP2833x_Examples.h" // DSP2833x Examples Include File
#include "SD.h" //SD Include File
//Global Variables
Uint16 response, ocr_contents[5], csd_contents[16], cid_contents[16];
Uint16 card_status[2], data_manipulation = TRUE, high_capacity = FALSE, crc_enabled = FALSE;
//######################### CHECK_CARD_INSERTION #############################
void sd_card_insertion()
{
Uint16 i;
//After Card Detection, SD protocol states that card needs 74 clock
//cycles with the DATA IN line high for chip to stabilize. CS does not
//need to be active for this action.
sdstatus.bit.bit10_HighCapacity= 0;
sdstatus.bit.bit0_CardDetected = 0;
CS_HIGH; //Pull CS high
for(i=0;i<10;i++){ //Transmit 0xFF for 80 clock cycles
spi_xmit_byte(DUMMY_DATA);
}
SD_process ++;
}
//######################### CHECK_CARD_INSERTION #############################
//########################## SD_INITIALIZATION ###############################
void sd_initialization()
{
CS_LOW; //Pull CS low
data_manipulation = FALSE; //Register manipulation function
//Transmit GO IDLE STATE command to card with CS low to select SPI mode
//and put the card in IDLE mode.
spi_xmit_command(GO_IDLE_STATE, STUFF_BITS, INITIAL_CRC);
RESET_RESPONSE; //Reset response
while(response != IN_IDLE_STATE) //Wait until card responds with IDLE response
sd_command_response();
sdstatus.bit.bit0_CardDetected = 1;
// GpioDataRegs.GPASET.bit.GPIO20 = TRUE; //Disable SDIO Mode LED, Optional
// GpioDataRegs.GPACLEAR.bit.GPIO5 = TRUE; //Emit SPI Mode LED, Optional
RESET_RESPONSE; //Reset response
//After receiving response clock must be active for 8 clock cycles
EIGHT_CLOCK_CYCLE_DELAY;
crc_enabled = TRUE; //CRC is always enabled for SEND_IF_COND command
spi_xmit_command(SEND_IF_COND, INTERFACE_COND, DUMMY_CRC); //Transmit SEND_IF_COND command
crc_enabled = FALSE; //CRC is disabled for SPI mode
//Wait until card responds with IDLE response
while((response != IN_IDLE_STATE) && (response != ILLEGAL_COMMAND_IDLE_STATE))
response = spi_xmit_byte(DUMMY_DATA);
//If SEND_IF_COND returned illegal command call sd_version1_initialization for
//standard capacity card initialization. Otherwise call sd_version2_initialization
//for high capacity card initialization
if(response == ILLEGAL_COMMAND_IDLE_STATE)
sd_version1_initialization();
else if(response == IN_IDLE_STATE)
sd_version2_initialization();
CS_HIGH; //Pull CS high
//After receiving response clock must be active for 8 clock cycles
EIGHT_CLOCK_CYCLE_DELAY;
SD_process++;
SpiaRegs.SPIBRR = 0x0003; //Adjust Clock to 10.0 MHz (10.0 Mbps)
}
//########################## SD_INITIALIZATION ###############################
//###################### SD_VERSION1_INITIALIZATION ##########################
void sd_version1_initialization()
{
RESET_RESPONSE; //Reset response
//After receiving response clock must be active for 8 clock cycles
EIGHT_CLOCK_CYCLE_DELAY;
spi_xmit_command(READ_OCR, STUFF_BITS, DUMMY_CRC); //Transmit READ_OCR command
//Wait until card responds with IN_IDLE_STATE response
while(response != IN_IDLE_STATE)
sd_command_response();
sd_ocr_response(); //Call OCR response function
//If host voltage is not compatible with card voltage, do not communicate
//further with card
if(ocr_contents[1] != SUPPORTED_VOLTAGE)
sd_error();
RESET_RESPONSE; //Reset response
//After receiving response clock must be active for 8 clock cycles
EIGHT_CLOCK_CYCLE_DELAY;
while(response != SUCCESS)
{
RESET_RESPONSE; //Reset response
//After receiving response clock must be active for 8 clock cycles
EIGHT_CLOCK_CYCLE_DELAY;
//Before transmitting application specific commands, the APP_CMD
//must be transmitted
spi_xmit_command(APP_CMD, STUFF_BITS, DUMMY_CRC);
//Wait until card responds with IN_IDLE_STATE response
while(response != IN_IDLE_STATE)
sd_command_response();
RESET_RESPONSE; //Reset response
//After receiving response clock must be active for 8 clock cycles
EIGHT_CLOCK_CYCLE_DELAY;
//Transmit SEND OP COND command
spi_xmit_command(SD_SEND_OP_COND, STUFF_BITS, DUMMY_CRC);
sd_command_response(); //Receive response
}
RESET_RESPONSE; //Reset response
//After receiving response clock must be active for 8 clock cycles
EIGHT_CLOCK_CYCLE_DELAY;
}
//###################### SD_VERSION1_INITIALIZATION ##########################
//###################### SD_VERSION2_INITIALIZATION ##########################
void sd_version2_initialization()
{
Uint16 i, send_if_cond_response[4], ccs_check;
//Receive SEND_IF_COND response
for(i=0;i<3;i++)
send_if_cond_response[i] = spi_xmit_byte(DUMMY_DATA);
//If voltage accepted or check pattern does not match, do not communicate further
if((send_if_cond_response[2] != SUPPLY_VOLTAGE) || (send_if_cond_response[3] != CHECK_PATTERN))
sd_error();
RESET_RESPONSE; //Reset response
//After receiving response clock must be active for 8 clock cycles
EIGHT_CLOCK_CYCLE_DELAY;
while((response != SUCCESS)&&(SD_process))
{
RESET_RESPONSE; //Reset response
//After receiving response clock must be active for 8 clock cycles
EIGHT_CLOCK_CYCLE_DELAY;
//Before transmitting application specific commands, the APP_CMD
//must be transmitted
spi_xmit_command(APP_CMD, STUFF_BITS, DUMMY_CRC);
while(response != IN_IDLE_STATE)//Wait until card responds with IN_IDLE_STATE response
sd_command_response();
RESET_RESPONSE; //Reset response
//After receiving response clock must be active for 8 clock cycles
EIGHT_CLOCK_CYCLE_DELAY;
//Transmit SEND_OP_COND command
spi_xmit_command(SD_SEND_OP_COND, VER2_OP_COND, DUMMY_CRC);
sd_command_response(); //Receive response
}
RESET_RESPONSE; //Reset response
//After receiving response clock must be active for 8 clock cycles
EIGHT_CLOCK_CYCLE_DELAY;
spi_xmit_command(READ_OCR, STUFF_BITS, DUMMY_CRC); //Transmit READ_OCR command
//Wait until card responds with SUCCESS response
while((response != SUCCESS)&&(SD_process))
sd_command_response();
sd_ocr_response(); //Call OCR response function
ccs_check = ocr_contents[0] & HIGH_CAPACITY; //Mask ocr_contents to test for High Capacity
if(ccs_check == HIGH_CAPACITY){ //Check if card is High Capacity
high_capacity = TRUE;
sdstatus.bit.bit10_HighCapacity= 1;
}
RESET_RESPONSE; //Reset response
//After receiving response clock must be active for 8 clock cycles
EIGHT_CLOCK_CYCLE_DELAY;
}
//###################### SD_VERSION2_INITIALIZATION ##########################