Contiki-Inga 3.x
sdcard.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, TU Braunschweig.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /**
31  * \file
32  * SD Card interface definitions
33  * \author
34  * Ulf Kulau <kulau@ibr.cs.tu-bs.de>
35  * Christoph Peltz <peltz@ibr.cs.tu-bs.de>
36  * Enrico Joerns <joerns@ibr.cs.tu-bs.de>
37  */
38 
39 /**
40  * \addtogroup inga_device_driver
41  * @{
42  */
43 
44 /**
45  * \defgroup sdcard_interface MicroSD Card Interface
46  *
47  * This driver provides the following main features:
48  *
49  * - single block read
50  * - single block write
51  * - multi block write
52  *
53  * Note that multiple bock write is faster than single block write
54  * but only writes sequential block numbers
55  *
56  * \note CRC functionality is not fully implemented thus it sould not be used yet.
57  *
58  * \author
59  * Ulf Kulau <kulau@ibr.cs.tu-bs.de>
60  * Christoph Peltz <peltz@ibr.cs.tu-bs.de>
61  * Enrico Joerns <joerns@ibr.cs.tu-bs.de>
62  * @{
63  */
64 
65 #ifndef INGA_SDCARD_H
66 #define INGA_SDCARD_H
67 
68 #include "mspi.h"
69 #include <stdio.h>
70 
71 /*!
72  * SPI device order. The chip select number where the
73  * sdcard-Card is connected to the BCD-decimal decoder
74  */
75 #define MICRO_SD_CS 5
76 
77 /**
78  * \name Interface return codes
79  * \{ */
80 /** Successfully completed operation */
81 #define SDCARD_SUCCESS 0
82 /** Indicates the host cannot handle this card,
83  * maybe due to rejected voltage range */
84 #define SDCARD_REJECTED 2
85 /** Timeout while trying to send command */
86 #define SDCARD_CMD_TIMEOUT 4
87 /** Card sent an error response */
88 #define SDCARD_CMD_ERROR 3
89 /** Card did not send a data start byte to indicate beginning of a data block */
90 #define SDCARD_DATA_TIMEOUT 6
91 /** Card returned error when trying to read or write data.
92  * Obtained from data response (write) or data error response (read) token */
93 #define SDCARD_DATA_ERROR 7
94 /** Busy waiting timed out (card held down data line too long) */
95 #define SDCARD_BUSY_TIMEOUT 8
96 /** Failed reading CSD register */
97 #define SDCARD_CSD_ERROR 10
98 /** \} */
99 
100 #define SDCARD_WRITE_COMMAND_ERROR 1
101 #define SDCARD_WRITE_DATA_ERROR 2
102 
103 #define SDCARD_ERASE_START_ERROR 1
104 #define SDCARD_ERASE_END_ERR 2
105 
106 /**
107  * \brief Initializes the SD Card
108  *
109  * \retval SDCARD_SUCCESS SD-Card was initialized without an error
110  * \retval SDCARD_CMD_ERROR
111  * \retval SDCARD_CMD_TIMEOUT
112  * \retval SDCARD_REJECTED
113  * \retval SDCARD_CSD_ERROR
114  */
115 uint8_t sdcard_init(void);
116 
117 /**
118  * \brief This function will read the CSD (16 Bytes) of the SD-Card.
119  *
120  * \param *buffer Pointer to a block buffer, MUST hold at least 16 Bytes.
121  *
122  * \retval SDCARD_SUCCESS SD-Card CSD read was successful
123  * \retval SDCARD_CMD_ERROR CMD9 failure.
124  * \retval SDCARD_DATA_TIMEOUT
125  * \retval SDCARD_DATA_ERROR
126  */
127 uint8_t sdcard_read_csd(uint8_t *buffer);
128 
129 /**
130  * Returns the size of one block in bytes.
131  *
132  * Mainly used to calculated size of the SD-Card together with
133  * sdcard_get_card_block_count().
134  *
135  * \note It's fixed at 512 Bytes.
136  *
137  * \return Number of Bytes in one Block.
138  */
139 uint16_t sdcard_get_block_size();
140 
141 /**
142  * \brief This function indicates if a card is a SDSC or SDHC/SDXC card.
143  *
144  * \note sdcard_init() must be called beforehand and be successful before this
145  * functions return value has any meaning.
146  *
147  * \return Not 0 if the card is SDSC and 0 if SDHC/SDXC
148  */
149 uint8_t sdcard_is_SDSC();
150 
151 /**
152  * Turns crc capabilities of the card on or off.
153  *
154  * \note Does not work properly at this time. (FIXME)
155  * \param enable 0 if CRC should be disabled, 1 if it should be enabled
156  * \return 0 on success, !=0 otherwise
157  */
158 uint8_t sdcard_set_CRC(uint8_t enable);
159 
160 /**
161  * Returns card size
162  * @return card size in bytes
163  */
164 uint64_t sdcard_get_card_size();
165 
166 /**
167  * @return number of blocks
168  */
169 uint32_t sdcard_get_block_num();
170 
171 /**
172  * @note Not implemented
173  */
174 uint8_t sdcard_erase_blocks(uint32_t startaddr, uint32_t endaddr);
175 
176 /**
177  * \brief This function will read one block (512, 1024, 2048 or 4096Byte) of the SD-Card.
178  *
179  * \param addr Block address
180  * \param *buffer Pointer to a block buffer (needs to be as long as sdcard_get_block_size()).
181  *
182  * \retval SDCARD_SUCCESS SD-Card block read was successful
183  * \retval SDCARD_CMD_ERROR CMD17 failure
184  * \retval SDCARD_BUSY_TIMEOUT
185  * \retval SDCARD_DATA_TIMEOUT
186  * \retval SDCARD_DATA_ERROR
187  */
188 uint8_t sdcard_read_block(uint32_t addr, uint8_t *buffer);
189 
190 /**
191  * \brief This function will write one block (512, 1024, 2048 or 4096Byte) of the SD-Card.
192  *
193  * \param addr Block address
194  * \param *buffer Pointer to a block buffer (needs to be as long as sdcard_get_block_size()).
195  *
196  * \retval SDCARD_SUCCESS SD-Card block write was successful
197  * \retval SDCARD_CMD_ERROR CMD24 failure
198  * \retval SDCARD_BUSY_TIMEOUT
199  * \retval SDCARD_DATA_ERROR
200  */
201 uint8_t sdcard_write_block(uint32_t addr, uint8_t *buffer);
202 
203 /**
204  * \brief Prepares to write multiple blocks sequentially.
205  *
206  * \param addr Address of first block
207  * \param num_blocks Number of blocks that should be written (0 means not known yet).
208  * Givin a number here could speed up writing due to possible sector pre-erase
209  * \retval SDCARD_SUCCESS Starting mutli lock write was successful
210  * \retval SDCARD_CMD_ERROR CMD25 failure
211  * \retval SDCARD_BUSY_TIMEOUT
212  */
213 uint8_t sdcard_write_multi_block_start(uint32_t addr, uint32_t num_blocks);
214 
215 /**
216  * \brief Writes single of multiple sequental blocks.
217  *
218  * \param buffer
219  * \retval SDCARD_SUCCESS Successfully wrote block
220  * \retval SDCARD_BUSY_TIMEOUT
221  * \retval SDCARD_DATA_ERROR
222  */
223 uint8_t sdcard_write_multi_block_next(uint8_t *buffer);
224 
225 /**
226  * \brief Stops multiple block write.
227  *
228  * \retval SDCARD_SUCCESS successfull
229  * \retval SDCARD_BUSY_TIMEOUT
230  */
232 
233 
234 /**
235  * \brief This function sends a command via SPI to the SD-Card. An SPI
236  * command consists off 6 bytes
237  *
238  * \param cmd Command number to send
239  * \param *arg pointer to 32bit argument (addresses etc.)
240  * \param *resp Pointer to the response array. Only needed for responses other than R1. May be NULL if response is R1. Otherwise resp must be long enough for the response (only R3 and R7 are supported yet) and the first byte of the response array must indicate the response that is expected. For Example the first byte should be 0x07 if response type R7 is expected.
241  *
242  * \return R1 response byte or 0xFF in case of read/write timeout
243  */
244 uint8_t sdcard_write_cmd(uint8_t cmd, uint32_t *arg, uint8_t *resp);
245 
246 /**
247  * \brief This function calculates the CRC16 for a 512 Byte data block.
248  *
249  * \param *data The array containing the data to be send. Must be 512 Bytes long.
250  * \return The CRC16 code for the given data.
251  */
252 uint16_t sdcard_data_crc(uint8_t *data);
253 
254 
255 /** @} */ // sdcard_interface
256 /** @} */ // inga_device_driver
257 
258 #endif /* INGA_SDCARD_H */