-
Notifications
You must be signed in to change notification settings - Fork 1
/
SD_SPI_Registers.c
117 lines (92 loc) · 3.91 KB
/
SD_SPI_Registers.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
//############################################################################
//
// FILE: SD_SPI_Registers.c
//
// TITLE: SD/MMC Register Manipulation 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
extern unsigned int SD_process;
//############################ SD_READ_REGISTER ##############################
void sd_read_register(Uint16 command)
{
CS_LOW; //Pull CS low
data_manipulation = FALSE; //Register manipulation function
//Transmit register command requested
spi_xmit_command(command, STUFF_BITS, DUMMY_CRC);
RESET_RESPONSE; //Reset response
while(response != SUCCESS) //Wait until card responds with SUCCESS response
sd_command_response();
if(command == READ_OCR) //Test if OCR register is requested
sd_ocr_response(); //Call sd_ocr_response to read OCR register
else
//Call sd_cid_csd_response to read CID or CSD registers
sd_cid_csd_response(command);
CS_HIGH; //Pull CS high
//After receiving response clock must be active for 8 clock cycles
EIGHT_CLOCK_CYCLE_DELAY;
SD_process++;
}
//############################ SD_READ_REGISTER ##############################
//############################ SD_OCR_RESPONSE ###############################
void sd_ocr_response()
{
Uint16 i;
//OCR register is five bytes. Transmit 0xFF 5 times to receive the contents
//of OCR register and store them.
for(i=0;i<4;i++)
ocr_contents[i] = spi_xmit_byte(DUMMY_DATA);
//After receiving response clock must be active for 8 clock cycles
EIGHT_CLOCK_CYCLE_DELAY;
}
//############################ SD_OCR_RESPONSE ###############################
//########################## SD_CID_CSD_RESPONSE #############################
void sd_cid_csd_response(Uint16 command)
{
Uint16 i;
RESET_RESPONSE; //Reset Response
while(response != START_BLOCK) //Wait until START BLOCK is received
response = spi_xmit_byte(DUMMY_DATA);
//CID and CSD Registers are 16 bytes. Transmit 0xFF 16 times to receive the contents
//of CID or CSD Register and store them.
for(i=0;i<16;i++)
{
if(command == SEND_CSD)
csd_contents[i] = spi_xmit_byte(DUMMY_DATA);
else
cid_contents[i] = spi_xmit_byte(DUMMY_DATA);
}
//Transmit 0xFF 2 more times to receive CRC. In SPI Mode CRC is disable 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_CID_CSD_RESPONSE #############################
//############################# SD_SEND_STATUS ###############################
void sd_send_status()
{
data_manipulation = FALSE; //Register manipulation function
//Transmit SEND STATUS command
spi_xmit_command(SEND_STATUS, STUFF_BITS, DUMMY_CRC);
RESET_RESPONSE; //Reset response
while(response == DUMMY_DATA) //Wait until response is not 0xFF
response = spi_xmit_byte(DUMMY_DATA);
card_status[0] = response; //Store first byte of Status register
card_status[1] = spi_xmit_byte(DUMMY_DATA); //Store second byte of status register
//If either byte of CARD STATUS is not SUCCESS, error was generated.
if((card_status[0] != SUCCESS) || (card_status[1] != SUCCESS))
sd_error(); //Branch to error function
//After receiving response clock must be active for 8 clock cycles
EIGHT_CLOCK_CYCLE_DELAY;
}
//############################# SD_SEND_STATUS ###############################