A library with drivers for different peripherals on the LPC4088 QuickStart Board or related add-on boards.

Dependencies:   FATFileSystem

Dependents:   LPC4088test LPC4088test_ledonly LPC4088test_deleteall LPC4088_RAMtest ... more

Committer:
embeddedartists
Date:
Wed Oct 09 07:51:52 2013 +0000
Revision:
3:9d31a3c5013e
Parent:
0:0fdadbc3d852
Child:
12:15597e45eea0
Updated MCIFileSystem with needed PullUp on card detect pin.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 0:0fdadbc3d852 1 #ifndef MCIFILESYSTEM_H
embeddedartists 0:0fdadbc3d852 2 #define MCIFILESYSTEM_H
embeddedartists 0:0fdadbc3d852 3
embeddedartists 0:0fdadbc3d852 4 #include "mbed.h"
embeddedartists 0:0fdadbc3d852 5 #include "FATFileSystem.h"
embeddedartists 0:0fdadbc3d852 6
embeddedartists 0:0fdadbc3d852 7 /** Access the filesystem on an SD Card using MCI
embeddedartists 0:0fdadbc3d852 8 *
embeddedartists 0:0fdadbc3d852 9 * @code
embeddedartists 0:0fdadbc3d852 10 * #include "mbed.h"
embeddedartists 0:0fdadbc3d852 11 * #include "MCIFileSystem.h"
embeddedartists 0:0fdadbc3d852 12 *
embeddedartists 0:0fdadbc3d852 13 * MCIFileSystem mcifs("mci");
embeddedartists 0:0fdadbc3d852 14 *
embeddedartists 0:0fdadbc3d852 15 * int main() {
embeddedartists 0:0fdadbc3d852 16 * printf("Please insert a SD/MMC card\n");
embeddedartists 0:0fdadbc3d852 17 * while(!mcifs.cardInserted()) {
embeddedartists 0:0fdadbc3d852 18 * wait(0.5);
embeddedartists 0:0fdadbc3d852 19 * }
embeddedartists 0:0fdadbc3d852 20 *
embeddedartists 3:9d31a3c5013e 21 * printf("Found SD/MMC card, writing to /mci/myfile.txt ...\n");
embeddedartists 3:9d31a3c5013e 22 *
embeddedartists 0:0fdadbc3d852 23 * FILE *fp = fopen("/mci/myfile.txt", "w");
embeddedartists 0:0fdadbc3d852 24 * if (fp != NULL) {
embeddedartists 0:0fdadbc3d852 25 * fprintf(fp, "Hello World!\n");
embeddedartists 0:0fdadbc3d852 26 * fclose(fp);
embeddedartists 3:9d31a3c5013e 27 * printf("Wrote to /mci/myfile.txt\n");
embeddedartists 3:9d31a3c5013e 28 * } else {
embeddedartists 3:9d31a3c5013e 29 * printf("Failed to open /mci/myfile.txt\n");
embeddedartists 0:0fdadbc3d852 30 * }
embeddedartists 0:0fdadbc3d852 31 * }
embeddedartists 0:0fdadbc3d852 32 * @endcode
embeddedartists 0:0fdadbc3d852 33 */
embeddedartists 0:0fdadbc3d852 34 class MCIFileSystem : public FATFileSystem {
embeddedartists 0:0fdadbc3d852 35 public:
embeddedartists 0:0fdadbc3d852 36
embeddedartists 0:0fdadbc3d852 37 /** Create the File System for accessing an SD/MMC Card using MCI
embeddedartists 0:0fdadbc3d852 38 *
embeddedartists 0:0fdadbc3d852 39 * @param name The name used to access the virtual filesystem
embeddedartists 0:0fdadbc3d852 40 * @param cd The pin connected to the CardDetect line
embeddedartists 0:0fdadbc3d852 41 */
embeddedartists 0:0fdadbc3d852 42 MCIFileSystem(const char* name, PinName cd = p38);
embeddedartists 0:0fdadbc3d852 43
embeddedartists 0:0fdadbc3d852 44 virtual ~MCIFileSystem();
embeddedartists 0:0fdadbc3d852 45
embeddedartists 0:0fdadbc3d852 46 virtual int disk_initialize();
embeddedartists 0:0fdadbc3d852 47 virtual int disk_status();
embeddedartists 0:0fdadbc3d852 48 virtual int disk_read(uint8_t * buffer, uint64_t block_number);
embeddedartists 0:0fdadbc3d852 49 virtual int disk_write(const uint8_t * buffer, uint64_t block_number);
embeddedartists 0:0fdadbc3d852 50 virtual int disk_sync();
embeddedartists 0:0fdadbc3d852 51 virtual uint64_t disk_sectors();
embeddedartists 0:0fdadbc3d852 52
embeddedartists 0:0fdadbc3d852 53 void mci_MCIIRQHandler();
embeddedartists 0:0fdadbc3d852 54 void mci_DMAIRQHandler();
embeddedartists 0:0fdadbc3d852 55
embeddedartists 0:0fdadbc3d852 56 /** Tests if a SD/MMC card is inserted or not.
embeddedartists 0:0fdadbc3d852 57 *
embeddedartists 0:0fdadbc3d852 58 * @returns
embeddedartists 0:0fdadbc3d852 59 * True if a card has been inserted,
embeddedartists 0:0fdadbc3d852 60 * False if no card is inserted or if the card detect pin is unavailable
embeddedartists 0:0fdadbc3d852 61 */
embeddedartists 0:0fdadbc3d852 62 bool cardInserted() const;
embeddedartists 0:0fdadbc3d852 63
embeddedartists 0:0fdadbc3d852 64 private:
embeddedartists 0:0fdadbc3d852 65
embeddedartists 0:0fdadbc3d852 66 typedef enum {
embeddedartists 0:0fdadbc3d852 67 CardStatusNumBytes = 4,
embeddedartists 0:0fdadbc3d852 68 } Constants;
embeddedartists 0:0fdadbc3d852 69
embeddedartists 0:0fdadbc3d852 70 typedef enum {
embeddedartists 0:0fdadbc3d852 71 PowerOff = 0,
embeddedartists 0:0fdadbc3d852 72 PowerUp = 2,
embeddedartists 0:0fdadbc3d852 73 PowerOn = 3,
embeddedartists 0:0fdadbc3d852 74 } power_ctrl_t;
embeddedartists 0:0fdadbc3d852 75
embeddedartists 0:0fdadbc3d852 76 typedef enum {
embeddedartists 0:0fdadbc3d852 77 SDMMC_IDLE_ST = 0, /*!< Idle state */
embeddedartists 0:0fdadbc3d852 78 SDMMC_READY_ST, /*!< Ready state */
embeddedartists 0:0fdadbc3d852 79 SDMMC_IDENT_ST, /*!< Identification State */
embeddedartists 0:0fdadbc3d852 80 SDMMC_STBY_ST, /*!< standby state */
embeddedartists 0:0fdadbc3d852 81 SDMMC_TRAN_ST, /*!< transfer state */
embeddedartists 0:0fdadbc3d852 82 SDMMC_DATA_ST, /*!< Sending-data State */
embeddedartists 0:0fdadbc3d852 83 SDMMC_RCV_ST, /*!< Receive-data State */
embeddedartists 0:0fdadbc3d852 84 SDMMC_PRG_ST, /*!< Programming State */
embeddedartists 0:0fdadbc3d852 85 SDMMC_DIS_ST /*!< Disconnect State */
embeddedartists 0:0fdadbc3d852 86 } CardState;
embeddedartists 0:0fdadbc3d852 87
embeddedartists 0:0fdadbc3d852 88
embeddedartists 0:0fdadbc3d852 89 typedef struct {
embeddedartists 0:0fdadbc3d852 90 uint8_t CmdIndex;
embeddedartists 0:0fdadbc3d852 91 uint32_t Data[CardStatusNumBytes];
embeddedartists 0:0fdadbc3d852 92 } response_t;
embeddedartists 0:0fdadbc3d852 93
embeddedartists 0:0fdadbc3d852 94 /**
embeddedartists 0:0fdadbc3d852 95 * @brief SDC Clock Control Options
embeddedartists 0:0fdadbc3d852 96 */
embeddedartists 0:0fdadbc3d852 97 typedef enum {
embeddedartists 0:0fdadbc3d852 98 SDC_CLOCK_ENABLE = 8, /*!< Enable SD Card Bus Clock */
embeddedartists 0:0fdadbc3d852 99 SDC_CLOCK_POWER_SAVE = 9, /*!< Disable SD_CLK output when bus is idle */
embeddedartists 0:0fdadbc3d852 100 SDC_CLOCK_DIVIDER_BYPASS = 10, /*!< Enable bypass of clock divide logic */
embeddedartists 0:0fdadbc3d852 101 SDC_CLOCK_WIDEBUS_MODE = 11, /*!< Enable wide bus mode (SD_DAT[3:0] is used instead of SD_DAT[0]) */
embeddedartists 0:0fdadbc3d852 102 } ClockControl;
embeddedartists 0:0fdadbc3d852 103
embeddedartists 0:0fdadbc3d852 104 /**
embeddedartists 0:0fdadbc3d852 105 * @brief SD/MMC Card specific setup data structure
embeddedartists 0:0fdadbc3d852 106 */
embeddedartists 0:0fdadbc3d852 107 typedef struct {
embeddedartists 0:0fdadbc3d852 108 uint32_t response[4]; /*!< Most recent response */
embeddedartists 0:0fdadbc3d852 109 uint32_t cid[4]; /*!< CID of acquired card */
embeddedartists 0:0fdadbc3d852 110 uint32_t csd[4]; /*!< CSD of acquired card */
embeddedartists 0:0fdadbc3d852 111 uint32_t ext_csd[512 / 4]; /*!< Ext CSD */
embeddedartists 0:0fdadbc3d852 112 uint32_t card_type; /*!< Card Type */
embeddedartists 0:0fdadbc3d852 113 uint16_t rca; /*!< Relative address assigned to card */
embeddedartists 0:0fdadbc3d852 114 uint32_t speed; /*!< Speed */
embeddedartists 0:0fdadbc3d852 115 uint32_t block_len; /*!< Card sector size */
embeddedartists 0:0fdadbc3d852 116 uint32_t device_size; /*!< Device Size */
embeddedartists 0:0fdadbc3d852 117 uint32_t blocknr; /*!< Block Number */
embeddedartists 0:0fdadbc3d852 118 uint32_t clk_rate; /*!< Clock rate */
embeddedartists 0:0fdadbc3d852 119 } SDMMC_CARD_T;
embeddedartists 0:0fdadbc3d852 120
embeddedartists 0:0fdadbc3d852 121 typedef enum {
embeddedartists 0:0fdadbc3d852 122 SDC_RET_OK = 0,
embeddedartists 0:0fdadbc3d852 123 SDC_RET_CMD_FAILED = -1,
embeddedartists 0:0fdadbc3d852 124 SDC_RET_BAD_PARAMETERS = -2,
embeddedartists 0:0fdadbc3d852 125 SDC_RET_BUS_NOT_IDLE = -3,
embeddedartists 0:0fdadbc3d852 126 SDC_RET_TIMEOUT = -4,
embeddedartists 0:0fdadbc3d852 127 SDC_RET_ERR_STATE = -5,
embeddedartists 0:0fdadbc3d852 128 SDC_RET_NOT_READY = -6,
embeddedartists 0:0fdadbc3d852 129 SDC_RET_FAILED = -7,
embeddedartists 0:0fdadbc3d852 130 } ReturnCode;
embeddedartists 0:0fdadbc3d852 131
embeddedartists 0:0fdadbc3d852 132 void initMCI();
embeddedartists 0:0fdadbc3d852 133
embeddedartists 0:0fdadbc3d852 134 int32_t mci_Acquire();
embeddedartists 0:0fdadbc3d852 135 uint32_t mci_GetCardStatus() const;
embeddedartists 0:0fdadbc3d852 136 CardState mci_GetCardState() const;
embeddedartists 0:0fdadbc3d852 137 ReturnCode mci_ReadBlocks(void *buffer, int32_t startBlock, int32_t blockNum);
embeddedartists 0:0fdadbc3d852 138 ReturnCode mci_WriteBlocks(void *buffer, int32_t startBlock, int32_t blockNum);
embeddedartists 0:0fdadbc3d852 139 void mci_SetClock(uint32_t freq) const;
embeddedartists 0:0fdadbc3d852 140 void mci_ClockControl(ClockControl ctrlType, bool enable) const;
embeddedartists 0:0fdadbc3d852 141 void mci_PowerControl(power_ctrl_t powerMode, uint32_t flag) const;
embeddedartists 0:0fdadbc3d852 142 ReturnCode mci_ExecuteCmd(uint32_t Command, uint32_t Arg, response_t* pResp) const;
embeddedartists 0:0fdadbc3d852 143 ReturnCode mci_SendIfCond() const;
embeddedartists 0:0fdadbc3d852 144 ReturnCode mci_SendOpCond(uint32_t *pOCR) const;
embeddedartists 0:0fdadbc3d852 145 ReturnCode mci_SendAppOpCond(uint16_t rca, bool hcs, uint32_t *pOcr, bool *pCCS) const;
embeddedartists 0:0fdadbc3d852 146 ReturnCode mci_GetCID(uint32_t *pCID) const;
embeddedartists 0:0fdadbc3d852 147 ReturnCode mci_SetAddr(uint16_t addr) const;
embeddedartists 0:0fdadbc3d852 148 ReturnCode mci_GetAddr(uint16_t *pRCA) const;
embeddedartists 0:0fdadbc3d852 149 ReturnCode mci_GetCSD(uint16_t rca, uint32_t *pCSD) const;
embeddedartists 0:0fdadbc3d852 150 ReturnCode mci_SelectCard(uint16_t addr) const;
embeddedartists 0:0fdadbc3d852 151 ReturnCode mci_GetStatus(uint16_t rca, uint32_t *pStatus) const;
embeddedartists 0:0fdadbc3d852 152 void mci_ProcessCSD();
embeddedartists 0:0fdadbc3d852 153 ReturnCode mci_SetBusWidth(uint16_t rca, uint8_t width) const;
embeddedartists 0:0fdadbc3d852 154 ReturnCode mci_SetTranState(uint16_t rca) const;
embeddedartists 0:0fdadbc3d852 155 ReturnCode mci_SetBlockLength(uint32_t rca, uint32_t block_len) const;
embeddedartists 0:0fdadbc3d852 156 ReturnCode mci_SetCardParams() const;
embeddedartists 0:0fdadbc3d852 157 ReturnCode mci_StopTransmission(uint32_t rca) const;
embeddedartists 0:0fdadbc3d852 158 bool mci_CheckR1Response(uint32_t resp, ReturnCode* pCheckResult) const;
embeddedartists 0:0fdadbc3d852 159 void mci_WriteDelay() const;
embeddedartists 0:0fdadbc3d852 160 ReturnCode mci_SendCmd(uint32_t Command, uint32_t Arg, uint32_t timeout) const;
embeddedartists 0:0fdadbc3d852 161 ReturnCode mci_SendAppCmd(uint16_t rca) const;
embeddedartists 0:0fdadbc3d852 162 void mci_SetDataTransfer(uint16_t BlockNum, bool DirFromCard, uint32_t Timeout) const;
embeddedartists 0:0fdadbc3d852 163 void mci_GetResp(response_t* pResp) const;
embeddedartists 0:0fdadbc3d852 164 uint32_t mci_GetBits(int32_t start, int32_t end, uint32_t *data) const;
embeddedartists 0:0fdadbc3d852 165 void mci_SetCommand(uint32_t Cmd, uint32_t Arg) const;
embeddedartists 0:0fdadbc3d852 166 void mci_ResetCommand() const;
embeddedartists 0:0fdadbc3d852 167 int32_t mci_IRQHandler(uint8_t *txBuf, uint32_t *txCnt, uint8_t *rxBuf, uint32_t *rxCnt);
embeddedartists 0:0fdadbc3d852 168 int32_t mci_FIFOIRQHandler(uint8_t *txBuf, uint32_t *txCnt, uint8_t *rxBuf, uint32_t *rxCnt);
embeddedartists 0:0fdadbc3d852 169 void mci_ReadFIFO(uint32_t *pDst, bool bFirstHalf) const;
embeddedartists 0:0fdadbc3d852 170 void mci_WriteFIFO(uint32_t *pSrc, bool bFirstHalf) const;
embeddedartists 0:0fdadbc3d852 171
embeddedartists 0:0fdadbc3d852 172 void mci_SetupEventWakeup(uint8_t dmaChannel);
embeddedartists 0:0fdadbc3d852 173 uint32_t mci_WaitForEvent() const;
embeddedartists 0:0fdadbc3d852 174
embeddedartists 0:0fdadbc3d852 175 ReturnCode _readBlocks(uint32_t card_type, uint32_t startBlock, uint32_t blockNum) const;
embeddedartists 0:0fdadbc3d852 176 ReturnCode _writeBlocks(uint32_t card_type, uint32_t startBlock, uint32_t blockNum) const;
embeddedartists 0:0fdadbc3d852 177
embeddedartists 0:0fdadbc3d852 178
embeddedartists 0:0fdadbc3d852 179 uint32_t _Stat;
embeddedartists 0:0fdadbc3d852 180 SDMMC_CARD_T _sdCardInfo;
embeddedartists 0:0fdadbc3d852 181
embeddedartists 0:0fdadbc3d852 182 DigitalIn* _cardDetect;
embeddedartists 0:0fdadbc3d852 183
embeddedartists 0:0fdadbc3d852 184 uint8_t _eventDmaChannel; /*!< DMA Channel used for transfer data */
embeddedartists 0:0fdadbc3d852 185 volatile bool _eventReceived;
embeddedartists 0:0fdadbc3d852 186 volatile bool _eventSuccess;
embeddedartists 0:0fdadbc3d852 187 };
embeddedartists 0:0fdadbc3d852 188
embeddedartists 0:0fdadbc3d852 189 #endif
embeddedartists 0:0fdadbc3d852 190