1 | /** |
---|
2 | * \file reset_ioc_spi.h |
---|
3 | * \date 30 August 2012 |
---|
4 | * \author Cesar fuguet <cesar.fuguet-tortolero@lip6.fr> |
---|
5 | * |
---|
6 | * This file defines the driver of a SD Card device using an SPI controller |
---|
7 | */ |
---|
8 | |
---|
9 | #ifndef RESET_IOC_SPI_H |
---|
10 | #define RESET_IOC_SPI_H |
---|
11 | |
---|
12 | #include <spi.h> |
---|
13 | |
---|
14 | /** |
---|
15 | * \brief SD Card type definition |
---|
16 | */ |
---|
17 | struct sdcard_dev |
---|
18 | { |
---|
19 | /** |
---|
20 | * SPI controller pointer |
---|
21 | */ |
---|
22 | struct spi_dev * spi; |
---|
23 | |
---|
24 | /** |
---|
25 | * Block length of the SDCARD |
---|
26 | */ |
---|
27 | unsigned int block_length; |
---|
28 | |
---|
29 | /** |
---|
30 | * Access pointer representing the offset in bytes used |
---|
31 | * to read or write in the SDCARD. |
---|
32 | * |
---|
33 | * \note this driver is for cards SDSD, therefore this offset |
---|
34 | * must be multiple of the block length |
---|
35 | */ |
---|
36 | unsigned int access_pointer; |
---|
37 | |
---|
38 | /** |
---|
39 | * Slave ID. This ID represents the number of the slave select signal |
---|
40 | * used in the hardware platform |
---|
41 | */ |
---|
42 | int slave_id; |
---|
43 | |
---|
44 | /* is the card high capacity ? */ |
---|
45 | int sdhc; |
---|
46 | }; |
---|
47 | |
---|
48 | /** |
---|
49 | * \param sdcard : uninitialized pointer. This parameter will contain |
---|
50 | * a pointer to the initialized block device or NULL otherwise |
---|
51 | * \param spi : initialized pointer to the spi controller |
---|
52 | * \param ss : slave select signal number |
---|
53 | * |
---|
54 | * \return 0 when initialization succeeds or an error code value otherwise. |
---|
55 | * The error codes are defined in this header file. |
---|
56 | * |
---|
57 | * \brief Initialize the block device |
---|
58 | */ |
---|
59 | |
---|
60 | int sdcard_dev_open(struct sdcard_dev * sdcard, struct spi_dev * spi, int ss); |
---|
61 | |
---|
62 | /** |
---|
63 | * \param sdcard : Pointer to the initialized block device |
---|
64 | * \param buf : Pointer to a memory segment wherein store |
---|
65 | * \param count : number of bytes to read |
---|
66 | * |
---|
67 | * \return 0 when read succeeds or an error code value otherwise. |
---|
68 | * The error codes are defined in this header file. |
---|
69 | * |
---|
70 | * \brief Read in the block device |
---|
71 | * |
---|
72 | * The read is made in the current block device access pointer. |
---|
73 | * In the read succeeds, the block device access pointer is |
---|
74 | * relocated to the next block. |
---|
75 | */ |
---|
76 | |
---|
77 | int sdcard_dev_read(struct sdcard_dev * sdcard, void * buf, unsigned int count); |
---|
78 | |
---|
79 | /** |
---|
80 | * \param sdcard : Pointer to the initialized block device |
---|
81 | * \param buf : Pointer to a memory segment wherein the |
---|
82 | * \param count : number of blocks to write |
---|
83 | * |
---|
84 | * \return 0 when write succeeds or an error code value otherwise. |
---|
85 | * The error codes are defined in this header file. |
---|
86 | * |
---|
87 | * \brief Write in the block device |
---|
88 | * |
---|
89 | * The write is made in the current block device access pointer. |
---|
90 | * In the write succeeds, the block device access pointer is |
---|
91 | * relocated to the next block. |
---|
92 | */ |
---|
93 | |
---|
94 | unsigned int sdcard_dev_write(struct sdcard_dev * sdcard, void * buf, unsigned int count); |
---|
95 | |
---|
96 | /** |
---|
97 | * \param sdcard : Pointer to the initialized block device |
---|
98 | * \param pos : Position where the block device access |
---|
99 | * pointer must be move |
---|
100 | * |
---|
101 | * \return void |
---|
102 | * |
---|
103 | * \brief Change block device access pointer position |
---|
104 | * |
---|
105 | * The block device access pointer is relocated in terms of blocks |
---|
106 | */ |
---|
107 | |
---|
108 | void sdcard_dev_lseek(struct sdcard_dev * sdcard, unsigned int pos); |
---|
109 | |
---|
110 | /** |
---|
111 | * \param sdcard : Pointer to the initialized block device |
---|
112 | * \param len : Block device length to set |
---|
113 | * |
---|
114 | * \return 0 when succeed or error code value otherwise |
---|
115 | * |
---|
116 | * \brief Set the block length of the device |
---|
117 | */ |
---|
118 | |
---|
119 | int sdcard_dev_set_blocklen(struct sdcard_dev * sdcard, unsigned int len); |
---|
120 | |
---|
121 | /** |
---|
122 | * \return 0 when succeed or error code value otherwise |
---|
123 | * |
---|
124 | * \brief Initialize both the SD Card and the SD Card controller |
---|
125 | */ |
---|
126 | |
---|
127 | int reset_spi_init(); |
---|
128 | |
---|
129 | /** |
---|
130 | * \param lba : First block index on device |
---|
131 | * \param buffer : Destination memory buffer address |
---|
132 | * \param count : Number of bloks to be read |
---|
133 | * |
---|
134 | * \return 0 when succeed or error code value otherwise |
---|
135 | * |
---|
136 | * \brief Transfer count blocks from device to memory |
---|
137 | */ |
---|
138 | |
---|
139 | int reset_spi_read( unsigned int lba, void* buffer, unsigned int count ); |
---|
140 | |
---|
141 | |
---|
142 | |
---|
143 | /** |
---|
144 | * SD Card constants |
---|
145 | */ |
---|
146 | |
---|
147 | /** Number of retries after an unacknowledge command */ |
---|
148 | #define SDCARD_COMMAND_TIMEOUT 100 |
---|
149 | |
---|
150 | /** This command is a simple SD commmand */ |
---|
151 | #define SDCARD_CMD 0 |
---|
152 | |
---|
153 | /** This is an application specific command */ |
---|
154 | #define SDCARD_ACMD 1 |
---|
155 | |
---|
156 | /** The transmition is done in the negative edge of the clock */ |
---|
157 | #define SDCARD_TX_NEGEDGE 0 |
---|
158 | |
---|
159 | /** The transmition is done in the positive edge of the clock */ |
---|
160 | #define SDCARD_TX_POSEDGE 1 |
---|
161 | |
---|
162 | /** The reception is done in the negative edge of the clock */ |
---|
163 | #define SDCARD_RX_NEGEDGE 0 |
---|
164 | |
---|
165 | /** The reception is done in the positive edge of the clock */ |
---|
166 | #define SDCARD_RX_POSEDGE 1 |
---|
167 | |
---|
168 | /** |
---|
169 | * SD Card macros |
---|
170 | */ |
---|
171 | |
---|
172 | /** Check if the response is valid */ |
---|
173 | #define SDCARD_CHECK_R1_VALID(x) (~x & SDCARD_R1_RSP_VALID) ? 1 : 0 |
---|
174 | |
---|
175 | /** |
---|
176 | * Check if there is an error in the response |
---|
177 | * |
---|
178 | * \note this macro must be used after verify that the response is |
---|
179 | * valid |
---|
180 | */ |
---|
181 | #define SDCARD_CHECK_R1_ERROR(x) ( x & 0x7E) ? 1 : 0 |
---|
182 | |
---|
183 | /** |
---|
184 | * SD Card Response 1 (R1) format constants |
---|
185 | */ |
---|
186 | #define SDCARD_R1_IN_IDLE_STATE ( 1 << 0 ) /**< \brief R1 bit 0 */ |
---|
187 | #define SDCARD_R1_ERASE_RESET ( 1 << 1 ) /**< \brief R1 bit 1 */ |
---|
188 | #define SDCARD_R1_ILLEGAL_CMD ( 1 << 2 ) /**< \brief R1 bit 2 */ |
---|
189 | #define SDCARD_R1_COM_CRC_ERR ( 1 << 3 ) /**< \brief R1 bit 3 */ |
---|
190 | #define SDCARD_R1_ERASE_SEQ_ERR ( 1 << 4 ) /**< \brief R1 bit 4 */ |
---|
191 | #define SDCARD_R1_ADDRESS_ERR ( 1 << 5 ) /**< \brief R1 bit 5 */ |
---|
192 | #define SDCARD_R1_PARAMETER_ERR ( 1 << 6 ) /**< \brief R1 bit 6 */ |
---|
193 | #define SDCARD_R1_RSP_VALID ( 1 << 7 ) /**< \brief R1 bit 7 */ |
---|
194 | |
---|
195 | #endif |
---|
196 | |
---|
197 | /* |
---|
198 | * vim: tabstop=4 : shiftwidth=4 : expandtab : softtabstop=4 |
---|
199 | */ |
---|