MAV'RIC
sd_spi.h
1 /*******************************************************************************
2  * Copyright (c) 2009-2014, MAV'RIC Development Team
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 are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors
16  * may be used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  ******************************************************************************/
31 
32 /*******************************************************************************
33  * \file sd_spi.h
34  *
35  * \author MAV'RIC Team
36  * \author Gregoire Heitz
37  *
38  * \brief This file is the sd card driver using SPI
39  *
40  * \detail It has been implemented, using the SD/MMC using SPI Example for
41  * EVK1100 Documentation from ATMEL
42  * (http://asf.atmel.com/docs/2.9.0/avr32.components.memory.sdmmc.spi.example.evk1100/html/index.html)
43  * you can call sd_spi_test(&central_data->sd_spi); from main.cpp before
44  * creating task to try the functions
45  *
46  ******************************************************************************/
47 
48 
49 #ifndef SD_SPI_H_
50 #define SD_SPI_H_
51 
52 #include "compiler.h"
53 #include "user_board.h"
54 
55 //Linked with SPI usage
56 #define SPI_LOW_SPEED 400000
57 #define SPI_HIGH_SPEED 6000000
58 #define SD_MMC_SPI_BITS 8
59 
60 //linked with sd card
61 #define MMC_SECTOR_SIZE 512
62 
63 // Card identification
64 #define MMC_CARD 0
65 #define SD_CARD 1
66 #define SD_CARD_2 2
67 #define SD_CARD_2_SDHC 3
68 
69 /* status bits for card types */
70 #define SD_SPEC_1 0
71 #define SD_SPEC_2 1
72 #define SD_SPEC_SDHC 2
73 
74 
75 // Lock operations
76 #define OP_UNLOCK 0x00
77 #define OP_LOCK 0x04
78 #define OP_RESET_PWD 0x02
79 #define OP_SET_PWD 0x01
80 #define OP_FORCED_ERASE 0x08
81 
82 // MMC commands (taken from MMC reference)
83 #define MMC_GO_IDLE_STATE 0
84 #define MMC_SEND_OP_COND 1
85 #define MMC_CMD2 2
86 #define MMC_SEND_IF_COND 8
87 #define MMC_SEND_CSD 9
88 #define MMC_SEND_CID 10
89 #define MMC_SEND_STATUS 13
90 #define MMC_SET_BLOCKLEN 16
91 #define MMC_READ_SINGLE_BLOCK 17
92 #define MMC_WRITE_BLOCK 24
93 #define MMC_PROGRAM_CSD 27
94 #define MMC_SET_WRITE_PROT 28
95 #define MMC_CLR_WRITE_PROT 29
96 #define MMC_SEND_WRITE_PROT 30
97 #define SD_TAG_WR_ERASE_GROUP_START 32
98 #define SD_TAG_WR_ERASE_GROUP_END 33
99 #define MMC_TAG_SECTOR_START 32
100 #define MMC_TAG_SECTOR_END 33
101 #define MMC_UNTAG_SECTOR 34
102 #define MMC_TAG_ERASE_GROUP_START 35
103 #define MMC_TAG_ERASE_GROUP_END 36
104 #define MMC_UNTAG_ERASE_GROUP 37
105 #define MMC_ERASE 38
106 #define SD_SEND_OP_COND_ACMD 41
107 #define MMC_LOCK_UNLOCK 42
108 #define SD_APP_CMD55 55
109 #define SD_READ_OCR 58
110 #define MMC_CRC_ON_OFF 59
111 // R1 Response bit-defines
112 #define MMC_R1_BUSY 0x80
113 #define MMC_R1_PARAMETER 0x40
114 #define MMC_R1_ADDRESS 0x20
115 #define MMC_R1_ERASE_SEQ 0x10
116 #define MMC_R1_COM_CRC 0x08
117 #define MMC_R1_ILLEGAL_COM 0x04
118 #define MMC_R1_ERASE_RESET 0x02
119 #define MMC_R1_IDLE_STATE 0x01
120 // Data Start tokens
121 #define MMC_STARTBLOCK_READ 0xFE
122 #define MMC_STARTBLOCK_WRITE 0xFE
123 #define MMC_STARTBLOCK_MWRITE 0xFC
124 // Data Stop tokens
125 #define MMC_STOPTRAN_WRITE 0xFD
126 // Data Error Token values
127 #define MMC_DE_MASK 0x1F
128 #define MMC_DE_ERROR 0x01
129 #define MMC_DE_CC_ERROR 0x02
130 #define MMC_DE_ECC_FAIL 0x04
131 #define MMC_DE_OUT_OF_RANGE 0x04
132 #define MMC_DE_CARD_LOCKED 0x04
133 // Data Response Token values
134 #define MMC_DR_MASK 0x1F
135 #define MMC_DR_ACCEPT 0x05
136 #define MMC_DR_REJECT_CRC 0x0B
137 #define MMC_DR_REJECT_WRITE_ERROR 0x0D
138 
139 #define SDHC_CARD 1
140 #define SD_CARD_T 0
141 
142 #define SD_FAILURE -1
143 #define SD_MMC 0
144 
148 typedef struct
149 {
150  bool init_done;
151  uint8_t csd[16];
152  uint8_t card_type;
153  uint64_t capacity;
154  uint16_t capacity_mult;
155  uint32_t clock;
157  uint16_t erase_group_size;
158 }sd_spi_t;
159 
160 //Functions definitions
161 
167 bool sd_spi_mem_check(void);
168 
169 
173 void sd_spi_get_capacity(void);
174 
175 
181 bool sd_spi_reset_card(void);
182 
183 
189 void sd_spi_get_sector_count(void* res);
190 
196 void sd_spi_get_sector_size(void* res);
197 
198 
204 void sd_spi_get_block_size(void* res);
205 
211 bool is_sd_spi_write_pwd_locked(void);
212 
218 bool sd_spi_init(void);
219 
220 
226 bool sd_spi_status(void);
227 
228 
234 bool sd_spi_wait_not_busy(void);
235 
236 
244 bool sd_spi_write_sector_from_ram(const void *ram);
245 
255 bool sd_spi_read_sector_to_ram_pcda(void* ram, uint32_t beginning_sector, uint8_t count);
256 
264 bool sd_mmc_spi_read_sector_to_ram(void *ram);
265 
275 bool sd_spi_read_given_sector_to_ram(void* ram, uint32_t beginning_sector, uint8_t count);
276 
284 bool sd_mmc_spi_read_open_PDCA (uint32_t pos);
285 
295 bool sd_spi_write_given_sector_from_ram(const void *ram, uint32_t beginning_sector, uint8_t count);
296 
300 void sd_spi_test(void);
301 
302 #endif // SD_SPI_H_
uint8_t card_type
stores SD_CARD or MMC_CARD type card
Definition: sd_spi.h:152
Define the SD SPI structure.
Definition: sd_spi.h:148
uint32_t last_block_address
Define the last block address read or written.
Definition: sd_spi.h:156
bool init_done
Define the init status.
Definition: sd_spi.h:150
uint16_t capacity_mult
Define the multi capacity.
Definition: sd_spi.h:154
uint32_t clock
Define the clock to use.
Definition: sd_spi.h:155
uint64_t capacity
stores the capacity in bytes
Definition: sd_spi.h:153
uint16_t erase_group_size
Define the size of the erase group command.
Definition: sd_spi.h:157