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