-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSD_SPI_Read.c
125 lines (97 loc) · 4.03 KB
/
SD_SPI_Read.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
//############################################################################
//
// FILE: SD_SPI_Read.c
//
// TITLE: SD/MMC Read Data 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
//############################# SD_READ_BLOCK ################################
void sd_read_block(Uint16 sector, Uint16 *pBuffer)
{
CS_LOW; //Pull CS low
data_manipulation = TRUE; //Data manipulation function
//Transmit READ SINGLE BLOCK command
spi_xmit_command(READ_SINGLE_BLOCK, sector, DUMMY_CRC);
RESET_RESPONSE; //Reset Response
while(response != SUCCESS) //Wait until card responds with SUCCESS response
sd_command_response();
//After receiving response clock must be active for 8 clock cycles
EIGHT_CLOCK_CYCLE_DELAY;
//Call sd_data_response to read specified sector
sd_data_response(pBuffer, SINGLE_SECTOR);
CS_HIGH; //Pull CS high
//After receiving response clock must be active for 8 clock cycles
EIGHT_CLOCK_CYCLE_DELAY;
}
//############################# SD_READ_BLOCK ################################
//######################## SD_READ_MULTIPLE_BLOCK ############################
void sd_read_multiple_block(Uint16 sector, Uint16 *pBuffer, Uint16 total_sectors)
{
Uint16 i;
CS_LOW; //Pull CS low
data_manipulation = TRUE; //Data manipulation function
//Transmit READ MULTIPLE BLOCK command
spi_xmit_command(READ_MULTIPLE_BLOCK, sector, DUMMY_CRC);
RESET_RESPONSE; //Reset response
while(response != SUCCESS) //Wait until card responds with SUCCESS response
sd_command_response();
//After receiving response clock must be active for 8 clock cycles
EIGHT_CLOCK_CYCLE_DELAY;
//Call sd_data_response to read specified sectors
sd_data_response(pBuffer, total_sectors);
//Transmit STOP TRANSMISSION command
spi_xmit_command(STOP_TRANSMISSION, STUFF_BITS, DUMMY_CRC);
RESET_RESPONSE; //Reset response
//Wait 64 clock cycles or until card responds with SUCCESS response to move on
for(i=0;i<8;i++)
{
response = spi_xmit_byte(DUMMY_DATA);
if (response == SUCCESS)
break;
}
CS_HIGH; //Pull CS high
//After receiving response clock must be active for 8 clock cycles
EIGHT_CLOCK_CYCLE_DELAY;
}
//######################## SD_READ_MULTIPLE_BLOCK ############################
//########################### SD_DATA_RESPONSE ###############################
void sd_data_response(Uint16 *pBuffer, Uint16 total_sectors)
{
Uint16 i, j;
data_manipulation = TRUE; //Data manipulation function
//Continue until all total sectors requested are transmitted
for(i=0;i<total_sectors;i++)
{
RESET_RESPONSE; //Reset response
//This loop continues receiving a response from the card until a START_BLOCK
//or and error response is transmitted. If response is START_BLOCK then
//data will be transmitted. If response is an error the data will not be
//transmitted.
while(response != START_BLOCK)
{
response = spi_xmit_byte(DUMMY_DATA);
if((response == ERROR) || (response == CC_ERROR) || \
(response == CARD_ECC_FAILED) || (response == OUT_OF_RANGE))
sd_error();
}
for(j=0;j<512;j++) //Read block of data
//Store data to address referred to by pBuffer
*pBuffer++ = spi_xmit_byte(DUMMY_DATA);
//Transmit 0xFF 2 more times to receive CRC. In SPI Mode CRC is disabled by default
//so application does not need to store it.
spi_xmit_byte(DUMMY_DATA);
spi_xmit_byte(DUMMY_DATA);
}
//After receiving response clock must be active for 8 clock cycles
EIGHT_CLOCK_CYCLE_DELAY;
}
//########################### SD_DATA_RESPONSE ###############################