• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

sdcmd.h

Go to the documentation of this file.
00001 /* SD card command definitions file */
00002 /* Copyright (C) 2009 Holger Dietze
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation; either version 2 of the License, or
00007  *   (at your option) version 3.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License along
00015  *   with this program; if not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #ifndef SDCMD_H
00019 #define SDCMD_H
00020 
00021 #ifdef HAVE_STDINT_H
00022 #include <stdint.h>
00023 #endif
00024 
00030 /* card state   host state */
00031 typedef enum {
00032   SD_Inactive=0,/* inactive */
00033   SD_Idle,      /* card identification */
00034   SD_Ready,     /* card identification */
00035   SD_Identify,  /* card identification */
00036   SD_Standby,   /* data transfer */
00037   SD_Transfer,  /* data transfer */
00038   SD_SendData,  /* data transfer */
00039   SD_RecvData,  /* data transfer */
00040   SD_Program,   /* data transfer */
00041   SD_Disconnect,/* data transfer */
00042 } SD_State_t;
00043 
00045 #define CMD(c) (((c) & 0x3f) | 0x40)
00046 
00047 /* commands are transmitted MSB first */
00048 
00050 #define SD_GO_IDLE_STATE CMD(0)
00051 
00053 #define SD_SEND_OP_COND  CMD(1)
00054 
00055 #define SD_SWITCH_FUNC   CMD(6)
00056 
00058 #define SD_SEND_IF_COND  CMD(8)
00059 
00060 #define SD_SEND_CSD      CMD(9)
00061 
00062 #define SD_SEND_CID      CMD(10)
00063 
00064 #define SD_STOP_TRANSMISSION CMD(12)
00065 
00066 #define SD_SEND_STATUS   CMD(13)
00067 
00068 #define SD_SET_BLOCKLEN  CMD(16)
00069 
00070 #define SD_READ_SINGLE_BLOCK CMD(17)
00071 
00072 #define SD_READ_MULTIPLE_BLOCK CMD(18)
00073 
00074 #define SD_WRITE_BLOCK   CMD(24)
00075 
00076 #define SD_WRITE_MULTIPLE_BLOCK CMD(25)
00077 
00078 #define SD_PROGRAM_CSD   CMD(27)
00079 
00080 #define SD_APP_CMD       CMD(55)
00081 
00082 #define SD_READ_OCR      CMD(58)
00083 
00084 #define SD_CRC_ON_OFF    CMD(59)
00085 
00087 #define SD_A_SEND_NUM_WR_BLOCKS CMD(22)
00088 
00090 #define SD_A_SEND_OP_COND CMD(41)
00091 
00093 #define SDTOKEN_START          (0b11111110)
00094 
00095 #define SDTOKEN_START_MULTIPLE (0b11111100)
00096 
00097 #define SDTOKEN_STOP_MULTIPLE  (0b11111101)
00098 
00100 typedef struct sd_csd_data_s {
00101   uint8_t csd_structure; /* 2bit csd structure version|6bit reserved */
00102 #define CSD_STRUCTURE(csd)          ((csd).csd_structure >> 6)
00103   uint8_t taac; /* data read access time-1 */
00104 #define VAL_CSD_ACCESS_TIME_ASYNC_UNIT_RAW(v) ((v) & 0x07)
00105 #define VAL_CSD_ACCESS_TIME_ASYNC_TIME_RAW(v) (((v) >> 3) & 0x0f)
00106 #define VAL_CSD_ACCESS_TIME_ASYNC_UNIT_CONV(u)
00107 #define VAL_CSD_ACCESS_TIME_ASYNC_TIME_CONV(t) (t == 1 ? 1.0 : t == 2 ? 1.2 : \
00108                                                 t == 3 ? 1.3 : t == 4 ? 1.5 : \
00109                                                 t == 5 ? 2.0 : t == 6 ? 2.5 : \
00110                                                 t == 7 ? 3.0 : t == 8 ? 3.5 : \
00111                                                 t == 9 ? 4.0 : t == 10 ? 4.5 : \
00112                                                 t == 11 ? 5.0: t == 12 ? 5.5 : \
00113                                                 t == 13 ? 6.0: t == 14 ? 7.0 : \
00114                                                 t == 15 ? 8.0: 0)
00115 #define VAL_CSD_ACCESS_TIME_ASYNC(v)                                    \
00116   (VAL_CSD_ACCESS_TIME_ASYNC_TIME_CONV(VAL_CSD_ACCESS_TIME_ASYNC_TIME_RAW(v)) \
00117    * VAL_CSD_ACCESS_TIME_ASYNC_UNIT_CONV(VAL_CSD_ACCESS_TIME_UNIT_RAW(v)))
00118 #define CSD_ACCESS_TIME_ASYNC(csd) VAL_CSD_ACCESS_TIME_ASYNC((csd).taac)
00119   uint8_t nsac; /* data read access time-2 */
00120   uint8_t tran_speed; /* max data transfer rate */
00121 #define VAL_CSD_TRAN_UNIT_RAW(v)    ((v) & 7)
00122 #define VAL_CSD_TRAN_TIME_RAW(v)    (((v) >> 3) & 0x0f)
00123 #define VAL_CSD_TRAN_UNIT_CONV(u)   (u == 0 ? 100e3 : u == 1 ? 1e6 :    \
00124                                      u == 2 ? 10e6 : u == 3 ? 100e6 :   \
00125                                      0)
00126 #define VAL_CSD_TRAN_TIME_CONV(t)   (t == 1 ? 1.0 : t == 2 ? 1.2 :      \
00127                                      t == 3 ? 1.3 : t == 4 ? 1.5 :      \
00128                                      t == 5 ? 2.0 : t == 6 ? 2.5 :      \
00129                                      t == 7 ? 3.0 : t == 8 ? 3.5 :      \
00130                                      t == 9 ? 4.0 : t == 10 ? 4.5 :     \
00131                                      t == 11 ? 5.0 : t == 12 ? 5.5 :    \
00132                                      t == 13 ? 6.0 : t == 14 ? 7.0 :    \
00133                                      t == 15 ? 8.0 : 0)
00134 #define VAL_CSD_TRAN_SPEED(v) (VAL_CSD_TRAN_TIME_CONV(VAL_CSD_TRAN_TIME_RAW(v)) \
00135                                * VAL_CSD_TRAN_UNIT_CONV(VAL_CSD_TRAN_UNIT_RAW(v)))
00136 #define CSD_TRAN_SPEED(csd)         VAL_CSD_TRAN_SPEED((csd.tran_speed))
00137   uint8_t ccc_read_bl_len[2]; /* card command classes
00138                                * max read data block len */
00139 #define CSD_CCC(csd)                (((uint16_t)(csd).ccc_read_bl_len[0] << 4) \
00140                                      | ((uint16_t)((csd).ccc_read_bl_len[1]) >> 4))
00141 #define CSD_READ_BL_LEN(csd)        ((csd).ccc_read_bl_len[1] & 0xf)
00142   uint8_t flags1[10];
00143 #define CSD_READ_BL_PARTIAL(csd)    ((csd).flags1[0]>>7)
00144 #define CSD_WRITE_BLK_MISALIGN(csd) (((csd).flags1[0]>>6) & 0x1)
00145 #define CSD_READ_BLK_MISALIGN(csd)  (((csd).flags1[0]>>5) & 0x1)
00146 #define CSD_DSR_IMP(csd)            (((csd).flags1[0]>>4) & 0x1)
00147 #define CSD1_C_SIZE(csd)            (((uint16_t)((csd).flags1[0] & 0x3) << 10) \
00148                                      | ((uint16_t)((csd).flags1[1]) << 2) \
00149                                      | ((uint16_t)((csd).flags1[2]) >> 6))
00150 #define CSD2_C_SIZE(csd)            (((uint32_t)((csd).flags1[1] & 0x3f) << 16)\
00151                                      | ((uint16_t)((csd).flags1[2]) << 8) \
00152                                      | ((uint16_t)((csd).flags1[3]) ))
00153 #define CSD_VDD_R_CURR_MIN(csd)     (((csd).flags1[2]>>3) & 0x7)
00154 #define CSD_VDD_R_CURR_MAX(csd)     (((csd).flags1[2]) & 0x7)
00155 #define CSD_VDD_W_CURR_MIN(csd)     (((csd).flags1[3]>>5) & 0x7)
00156 #define CSD_VDD_W_CURR_MAX(csd)     (((csd).flags1[3]>>2) & 0x7)
00157 #define CSD_C_SIZE_MULT(csd)        ((((csd).flags1[3] & 0x3) << 1) \
00158                                      | ((csd).flags1[4]>>7))
00159 #define CSD_ERASE_BLK_EN(csd)       (((csd).flags1[4]>>6) & 0x1)
00160 #define CSD_SECTOR_SIZE(csd)        (((csd).flags1[4] & 0x3f) \
00161                                      | ((csd).flags1[5]>>7))
00162 #define CSD_WP_GRP_SIZE(csd)        ((csd).flags1[5] & 0x7f)
00163 #define CSD_WP_GRP_ENABLE(csd)      ((csd).flags1[6]>>7)
00164 #define CSD_R2W_FACTOR(csd)         (((csd).flags1[6]>>2) & 0x7)
00165 #define CSD_WRITE_BL_LEN(csd)       (((csd).flags1[6] & 0x3) \
00166                                      | ((csd).flags1[7]>>6))
00167 #define CSD_WRITE_BL_PARTIAL(csd)   (((csd).flags1[7]>>5) & 0x1)
00168 #define CSD_FILE_FORMAT_GRP(csd)    ((csd).flags1[8]>>7)
00169 #define CSD_COPY(csd)               (((csd).flags1[8]>>6) & 0x1)
00170 #define CSD_PERM_WRITE_PROTECT(csd) (((csd).flags1[8]>>5) & 0x1)
00171 #define CSD_TMP_WRITE_PROTECT(csd)  (((csd).flags1[8]>>4) & 0x1)
00172 #define CSD_FILE_FORMAT(csd)        (((csd).flags1[8]>>2) & 0x3)
00173 #define CSD_CRC(csd)                ((csd).flags1[9]>>1)
00174 } sd_csd_data_t;
00175 
00176 /* card capacity in kB */
00177 /* for SD 1.x: C_SIZE*2^(C_SIZE_MULT+2)*2^READ_BL_LEN bytes */
00178 #define CSD1_CARD_CAPACITY_KB(csd)                                      \
00179   ((uint32_t)(CSD1_C_SIZE(csd)+1)<<(CSD_C_SIZE_MULT(csd)+2+CSD_READ_BL_LEN(csd)-10))
00180 /* for SD 2.0: C_SIZE*512 kB */
00181 #define CSD2_CARD_CAPACITY_KB(csd)              \
00182   ((uint32_t)(CSD2_C_SIZE(csd)+1)<<9)
00183 
00184 /* assume 2.x when not 1.x */
00185 #define CSD_CARD_CAPACITY_KB(csd)               \
00186   (CSD_STRUCTURE(csd) == 0 ? CSD1_CARD_CAPACITY_KB(csd) \
00187    : CSD2_CARD_CAPACITY_KB(csd))
00188 
00190 typedef struct sd_cid_data_s {
00191   uint8_t mid;    
00192   uint8_t oid[2]; 
00193   uint8_t pnm[5]; 
00194   uint8_t prv;    
00195   uint32_t psn;   
00196   uint8_t mdt[2]; 
00197   uint8_t crc;    
00198 } sd_cid_data_t;
00199 
00205 typedef uint16_t sd_blen_t;
00206 
00208 #define SECTOR_SHIFT (9)
00209 
00210 #define SECTOR_SIZE (1<<SECTOR_SHIFT)
00211 
00214 #define SDR1_IN_IDLE_STATE        (1<<0)
00215 #define SDR1_ERASE_RESET          (1<<1)
00216 #define SDR1_ILLEGAL_COMMAND      (1<<2)
00217 #define SDR1_COM_CRC_ERROR        (1<<3)
00218 #define SDR1_ERASE_SEQUENCE_ERROR (1<<4)
00219 #define SDR1_ADDRESS_ERROR        (1<<5)
00220 #define SDR1_PARAMETER_ERROR      (1<<6)
00221 
00227 extern uint16_t sd_resp16_crc (uint8_t sd_response[]);
00228 
00229 #if 0
00230 extern void sd_send_cmd (uint8_t cmd, uint32_t arg);
00231 extern void sd_send_acmd (uint8_t acmd, uint32_t arg);
00232 #endif
00233 
00239 extern uint8_t sd_recv_data (uint8_t *data, sd_blen_t blocklen);
00246 extern void sd_send_data (const uint8_t *data, sd_blen_t blocklen, uint8_t token);
00247 
00256 extern uint16_t sd_init_card (uint8_t debug);
00262 extern uint32_t sd_block2addr (uint32_t block);
00263 
00269 extern uint8_t sd_read_block (uint8_t *data, uint32_t address);
00270 
00276 extern uint32_t sd_write_block (const uint8_t *data, uint32_t address);
00277 
00285 typedef struct sd_rd_mult_block_data_s {
00286   uint8_t * buffer;     
00287   uint8_t num_blocks; 
00288 } sd_rd_mult_block_data_t;
00289 
00297 typedef struct sd_wr_mult_block_data_s {
00298   const uint8_t * buffer; 
00299   uint8_t num_blocks;     
00300 } sd_wr_mult_block_data_t;
00301 
00311 extern uint8_t sd_read_multiple_block (uint8_t * const data[],
00312                                        uint8_t cnt, uint32_t address);
00313 
00323 extern uint8_t sd_read_multiple_block2 (sd_rd_mult_block_data_t const data[],
00324                                         uint8_t cnt, uint32_t address);
00325 
00335 extern uint8_t sd_write_multiple_block (const uint8_t * const data[],
00336                                         uint8_t cnt, uint32_t address);
00337 
00347 extern uint8_t sd_write_multiple_block2 (sd_wr_mult_block_data_t const data[],
00348                                          uint8_t cnt, uint32_t address);
00349 
00350 extern void sd_force_deselect (void);
00351 
00352 #endif

Generated by  doxygen 1.7.1