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