MAV'RIC
/home/travis/build/lis-epfl/MAVRIC_Library/hal/avr32/sd_spi.h
00001 /*******************************************************************************
00002  * Copyright (c) 2009-2016, MAV'RIC Development Team
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright notice,
00009  * this list of conditions and the following disclaimer.
00010  *
00011  * 2. Redistributions in binary form must reproduce the above copyright notice,
00012  * this list of conditions and the following disclaimer in the documentation
00013  * and/or other materials provided with the distribution.
00014  *
00015  * 3. Neither the name of the copyright holder nor the names of its contributors
00016  * may be used to endorse or promote products derived from this software without
00017  * specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029  * POSSIBILITY OF SUCH DAMAGE.
00030  ******************************************************************************/
00031 
00032 /*******************************************************************************
00033  * \file sd_spi.h
00034  *
00035  * \author MAV'RIC Team
00036  * \author Gregoire Heitz
00037  *
00038  * \brief This file is the sd card driver using SPI
00039  *
00040  * \detail It has been implemented, using the SD/MMC using SPI Example for
00041  * EVK1100 Documentation from ATMEL
00042  * (http://asf.atmel.com/docs/2.9.0/avr32.components.memory.sdmmc.spi.example.evk1100/html/index.html)
00043  * you can call sd_spi_test(&central_data->sd_spi); from main.cpp before
00044  * creating task to try the functions
00045  *
00046  ******************************************************************************/
00047 
00048 
00049 #ifndef SD_SPI_H_
00050 #define SD_SPI_H_
00051 
00052 
00053 #ifdef __cplusplus
00054 extern "C" {
00055 #endif
00056 
00057 #include <stdint.h>
00058 #include <stdbool.h>
00059 
00060 //Linked with SPI usage
00061 #define SPI_LOW_SPEED                   400000      ///< SPI low speed configuration
00062 #define SPI_HIGH_SPEED                  6000000     ///< SPI high speed configuration
00063 #define SD_MMC_SPI_BITS                 8           ///< Number of bits in spi communication
00064 
00065 //linked with sd card
00066 #define MMC_SECTOR_SIZE                   512       ///< default sector size is 512 bytes
00067 
00068 // Card identification
00069 #define MMC_CARD                          0         ///< Define a MMC card
00070 #define SD_CARD                           1         ///< Define a standar SD card
00071 #define SD_CARD_2                         2         ///< Define a SD card type 2
00072 #define SD_CARD_2_SDHC                    3         ///< Define a fast SD card type
00073 
00074 /* status bits for card types */
00075 #define SD_SPEC_1 0                                 ///< Define 0 status bit
00076 #define SD_SPEC_2 1                                 ///< Define 1 status bit
00077 #define SD_SPEC_SDHC 2                              ///< Define 2 status bit
00078 
00079 
00080 // Lock operations
00081 #define OP_UNLOCK                         0x00      ///< Define the unlock operation
00082 #define OP_LOCK                           0x04      ///< Define the lock operation
00083 #define OP_RESET_PWD                      0x02      ///< Define the reset operation
00084 #define OP_SET_PWD                        0x01      ///< Define the set power operation
00085 #define OP_FORCED_ERASE                   0x08      ///< Define the force erase operation
00086 
00087 // MMC commands (taken from MMC reference)
00088 #define MMC_GO_IDLE_STATE                 0         ///< initialize card to SPI-type access
00089 #define MMC_SEND_OP_COND                  1         ///< set card operational mode
00090 #define MMC_CMD2                          2         ///< illegal in SPI mode !
00091 #define MMC_SEND_IF_COND                  8
00092 #define MMC_SEND_CSD                      9         ///< get card's CSD
00093 #define MMC_SEND_CID                      10        ///< get card's CID
00094 #define MMC_SEND_STATUS                   13
00095 #define MMC_SET_BLOCKLEN                  16        ///< Set number of bytes to transfer per block
00096 #define MMC_READ_SINGLE_BLOCK             17        ///< read a block
00097 #define MMC_WRITE_BLOCK                   24        ///< write a block
00098 #define MMC_PROGRAM_CSD                   27
00099 #define MMC_SET_WRITE_PROT                28
00100 #define MMC_CLR_WRITE_PROT                29
00101 #define MMC_SEND_WRITE_PROT               30
00102 #define SD_TAG_WR_ERASE_GROUP_START       32
00103 #define SD_TAG_WR_ERASE_GROUP_END         33
00104 #define MMC_TAG_SECTOR_START              32
00105 #define MMC_TAG_SECTOR_END                33
00106 #define MMC_UNTAG_SECTOR                  34
00107 #define MMC_TAG_ERASE_GROUP_START         35        ///< Sets beginning of erase group (mass erase)
00108 #define MMC_TAG_ERASE_GROUP_END           36        ///< Sets end of erase group (mass erase)
00109 #define MMC_UNTAG_ERASE_GROUP             37        ///< Untag (unset) erase group (mass erase)
00110 #define MMC_ERASE                         38        ///< Perform block/mass erase
00111 #define SD_SEND_OP_COND_ACMD              41        ///< Same as MMC_SEND_OP_COND but specific to SD (must be preceeded by CMD55)
00112 #define MMC_LOCK_UNLOCK                   42        ///< To start a lock/unlock/pwd operation
00113 #define SD_APP_CMD55                      55        ///< Use before any specific command (type ACMD)
00114 #define SD_READ_OCR                       58
00115 #define MMC_CRC_ON_OFF                    59        ///< Turns CRC check on/off
00116 // R1 Response bit-defines
00117 #define MMC_R1_BUSY                       0x80      ///< R1 response: bit indicates card is busy
00118 #define MMC_R1_PARAMETER                  0x40
00119 #define MMC_R1_ADDRESS                    0x20
00120 #define MMC_R1_ERASE_SEQ                  0x10
00121 #define MMC_R1_COM_CRC                    0x08
00122 #define MMC_R1_ILLEGAL_COM                0x04
00123 #define MMC_R1_ERASE_RESET                0x02
00124 #define MMC_R1_IDLE_STATE                 0x01
00125 // Data Start tokens
00126 #define MMC_STARTBLOCK_READ               0xFE      ///< when received from card, indicates that a block of data will follow
00127 #define MMC_STARTBLOCK_WRITE              0xFE      ///< when sent to card, indicates that a block of data will follow
00128 #define MMC_STARTBLOCK_MWRITE             0xFC
00129 // Data Stop tokens
00130 #define MMC_STOPTRAN_WRITE                0xFD
00131 // Data Error Token values
00132 #define MMC_DE_MASK                       0x1F
00133 #define MMC_DE_ERROR                      0x01
00134 #define MMC_DE_CC_ERROR                   0x02
00135 #define MMC_DE_ECC_FAIL                   0x04
00136 #define MMC_DE_OUT_OF_RANGE               0x04
00137 #define MMC_DE_CARD_LOCKED                0x04
00138 // Data Response Token values
00139 #define MMC_DR_MASK                       0x1F
00140 #define MMC_DR_ACCEPT                     0x05
00141 #define MMC_DR_REJECT_CRC                 0x0B
00142 #define MMC_DR_REJECT_WRITE_ERROR         0x0D
00143 
00144 #define SDHC_CARD                         1
00145 #define SD_CARD_T                         0
00146 
00147 #define SD_FAILURE                       -1
00148 #define SD_MMC                            0
00149 
00153 typedef struct
00154 {
00155     bool                init_done;                  
00156     uint8_t             csd[16];                    
00157     uint8_t             card_type;                  
00158     uint64_t            capacity;                   
00159     uint16_t            capacity_mult;              
00160     uint32_t            clock;                      
00161     uint32_t            last_block_address;         
00162     uint16_t            erase_group_size;           
00163 } sd_spi_t;
00164 
00165 //Functions definitions
00166 
00172 bool sd_spi_mem_check(void);
00173 
00174 
00178 void sd_spi_get_capacity(void);
00179 
00180 
00186 bool sd_spi_reset_card(void);
00187 
00188 
00194 void sd_spi_get_sector_count(void* res);
00195 
00201 void sd_spi_get_sector_size(void* res);
00202 
00203 
00209 void sd_spi_get_block_size(void* res);
00210 
00216 bool is_sd_spi_write_pwd_locked(void);
00217 
00223 bool sd_spi_init(void);
00224 
00225 
00231 bool sd_spi_status(void);
00232 
00233 
00239 bool sd_spi_wait_not_busy(void);
00240 
00241 
00249 bool sd_spi_write_sector_from_ram(const void* ram);
00250 
00260 bool sd_spi_read_sector_to_ram_pcda(void* ram, uint32_t beginning_sector, uint8_t count);
00261 
00269 bool sd_mmc_spi_read_sector_to_ram(void* ram);
00270 
00280 bool sd_spi_read_given_sector_to_ram(void* ram, uint32_t beginning_sector, uint8_t count);
00281 
00289 bool sd_mmc_spi_read_open_PDCA(uint32_t pos);
00290 
00300 bool sd_spi_write_given_sector_from_ram(const void* ram, uint32_t beginning_sector, uint8_t count);
00301 
00305 void sd_spi_test(void);
00306 
00307 
00308 #ifdef __cplusplus
00309 }
00310 #endif
00311 
00312 #endif // SD_SPI_H_
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines