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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MCIFileSystem.h Source File

MCIFileSystem.h

00001 /*
00002  *  Copyright 2013 Embedded Artists AB
00003  *
00004  *  Licensed under the Apache License, Version 2.0 (the "License");
00005  *  you may not use this file except in compliance with the License.
00006  *  You may obtain a copy of the License at
00007  *
00008  *    http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  *  Unless required by applicable law or agreed to in writing, software
00011  *  distributed under the License is distributed on an "AS IS" BASIS,
00012  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *  See the License for the specific language governing permissions and
00014  *  limitations under the License.
00015  */
00016 
00017 #ifndef MCIFILESYSTEM_H
00018 #define MCIFILESYSTEM_H
00019 
00020 #include "mbed.h"
00021 #include "FATFileSystem.h"
00022 
00023 /** Access the filesystem on an SD Card using MCI
00024  *
00025  * @code
00026  * #include "mbed.h"
00027  * #include "MCIFileSystem.h"
00028  *
00029  * MCIFileSystem mcifs("mci");
00030  *
00031  * int main() {
00032  *     printf("Please insert a SD/MMC card\n");
00033  *     while(!mcifs.cardInserted()) {
00034  *         wait(0.5);
00035  *     }
00036  *     
00037  *     printf("Found SD/MMC card, writing to /mci/myfile.txt ...\n");
00038  *     
00039  *     FILE *fp = fopen("/mci/myfile.txt", "w");
00040  *     if (fp != NULL) {
00041  *         fprintf(fp, "Hello World!\n");
00042  *         fclose(fp);
00043  *         printf("Wrote to /mci/myfile.txt\n");
00044  *     } else {
00045  *         printf("Failed to open /mci/myfile.txt\n");
00046  *     }
00047  * }
00048  * @endcode
00049  */
00050 class MCIFileSystem : public FATFileSystem {
00051 public:
00052 
00053     /** Create the File System for accessing an SD/MMC Card using MCI
00054      *
00055      * @param name The name used to access the virtual filesystem
00056      * @param cd   The pin connected to the CardDetect line
00057      */
00058     MCIFileSystem(const char* name, PinName cd = p38);
00059 
00060     virtual ~MCIFileSystem();
00061 
00062     virtual int disk_initialize();
00063     virtual int disk_status();
00064     virtual int disk_read(uint8_t * buffer, uint64_t block_number);
00065     virtual int disk_write(const uint8_t * buffer, uint64_t block_number);
00066     virtual int disk_sync();
00067     virtual uint64_t disk_sectors();
00068 
00069     void mci_MCIIRQHandler();
00070     void mci_DMAIRQHandler();
00071 
00072     /** Tests if a SD/MMC card is inserted or not.
00073      *
00074      * @returns
00075      *   True if a card has been inserted,
00076      *   False if no card is inserted or if the card detect pin is unavailable
00077      */
00078     bool cardInserted() const;
00079 
00080 private:
00081 
00082     typedef enum {
00083       CardStatusNumBytes = 4,
00084     } Constants;
00085 
00086     typedef enum {
00087       PowerOff = 0,
00088       PowerUp = 2,
00089       PowerOn = 3,
00090     } power_ctrl_t;
00091 
00092     typedef enum {
00093       SDMMC_IDLE_ST = 0, /*!< Idle state */
00094       SDMMC_READY_ST,    /*!< Ready state */
00095       SDMMC_IDENT_ST,    /*!< Identification State */
00096       SDMMC_STBY_ST,     /*!< standby state */
00097       SDMMC_TRAN_ST,     /*!< transfer state */
00098       SDMMC_DATA_ST,     /*!< Sending-data State */
00099       SDMMC_RCV_ST,      /*!< Receive-data State */
00100       SDMMC_PRG_ST,      /*!< Programming State */
00101       SDMMC_DIS_ST       /*!< Disconnect State */
00102     } CardState;
00103 
00104 
00105     typedef struct {
00106       uint8_t CmdIndex;
00107       uint32_t Data[CardStatusNumBytes];
00108     } response_t;
00109 
00110     /**
00111      * @brief SDC Clock Control Options
00112      */
00113     typedef enum {
00114       SDC_CLOCK_ENABLE         =  8, /*!< Enable SD Card Bus Clock */
00115       SDC_CLOCK_POWER_SAVE     =  9, /*!< Disable SD_CLK output when bus is idle */
00116       SDC_CLOCK_DIVIDER_BYPASS = 10, /*!< Enable bypass of clock divide logic */
00117       SDC_CLOCK_WIDEBUS_MODE   = 11, /*!< Enable wide bus mode (SD_DAT[3:0] is used instead of SD_DAT[0]) */
00118     } ClockControl;
00119 
00120     /**
00121      * @brief SD/MMC Card specific setup data structure
00122      */
00123     typedef struct {
00124       uint32_t response[4];        /*!< Most recent response */
00125       uint32_t cid[4];             /*!< CID of acquired card  */
00126       uint32_t csd[4];             /*!< CSD of acquired card */
00127       uint32_t ext_csd[512 / 4];   /*!< Ext CSD */
00128       uint32_t card_type;          /*!< Card Type */
00129       uint16_t rca;                /*!< Relative address assigned to card */
00130       uint32_t speed;              /*!< Speed */
00131       uint32_t block_len;          /*!< Card sector size */
00132       uint32_t device_size;        /*!< Device Size */
00133       uint32_t blocknr;            /*!< Block Number */
00134       uint32_t clk_rate;           /*!< Clock rate */
00135     } SDMMC_CARD_T;
00136 
00137     typedef enum {
00138       SDC_RET_OK             =  0,
00139       SDC_RET_CMD_FAILED     = -1,
00140       SDC_RET_BAD_PARAMETERS = -2,
00141       SDC_RET_BUS_NOT_IDLE   = -3,
00142       SDC_RET_TIMEOUT        = -4,
00143       SDC_RET_ERR_STATE      = -5,
00144       SDC_RET_NOT_READY      = -6,
00145       SDC_RET_FAILED         = -7,
00146     } ReturnCode;
00147 
00148     void initMCI();
00149 
00150     int32_t     mci_Acquire();
00151     uint32_t    mci_GetCardStatus() const;
00152     CardState   mci_GetCardState() const;
00153     ReturnCode  mci_ReadBlocks(void *buffer, int32_t startBlock, int32_t blockNum);
00154     ReturnCode  mci_WriteBlocks(void *buffer, int32_t startBlock, int32_t blockNum);
00155     void        mci_SetClock(uint32_t freq) const;
00156     void        mci_ClockControl(ClockControl ctrlType, bool enable) const;
00157     void        mci_PowerControl(power_ctrl_t powerMode, uint32_t flag) const;
00158     ReturnCode  mci_ExecuteCmd(uint32_t Command, uint32_t Arg, response_t* pResp) const;
00159     ReturnCode  mci_SendIfCond() const;
00160     ReturnCode  mci_SendOpCond(uint32_t *pOCR) const;
00161     ReturnCode  mci_SendAppOpCond(uint16_t rca, bool hcs, uint32_t *pOcr, bool *pCCS) const;
00162     ReturnCode  mci_GetCID(uint32_t *pCID) const;
00163     ReturnCode  mci_SetAddr(uint16_t addr) const;
00164     ReturnCode  mci_GetAddr(uint16_t *pRCA) const;
00165     ReturnCode  mci_GetCSD(uint16_t rca, uint32_t *pCSD) const;
00166     ReturnCode  mci_SelectCard(uint16_t addr) const;
00167     ReturnCode  mci_GetStatus(uint16_t rca, uint32_t *pStatus) const;
00168     void        mci_ProcessCSD();
00169     ReturnCode  mci_SetBusWidth(uint16_t rca, uint8_t width) const;
00170     ReturnCode  mci_SetTranState(uint16_t rca) const;
00171     ReturnCode  mci_SetBlockLength(uint32_t rca, uint32_t block_len) const;
00172     ReturnCode  mci_SetCardParams() const;
00173     ReturnCode  mci_StopTransmission(uint32_t rca) const;
00174     bool        mci_CheckR1Response(uint32_t resp, ReturnCode* pCheckResult) const;
00175     void        mci_WriteDelay() const;
00176     ReturnCode  mci_SendCmd(uint32_t Command, uint32_t Arg, uint32_t timeout) const;
00177     ReturnCode  mci_SendAppCmd(uint16_t rca) const;
00178     void        mci_SetDataTransfer(uint16_t BlockNum, bool DirFromCard, uint32_t Timeout) const;
00179     void        mci_GetResp(response_t* pResp) const;
00180     uint32_t    mci_GetBits(int32_t start, int32_t end, uint32_t *data) const;
00181     void        mci_SetCommand(uint32_t Cmd, uint32_t Arg) const;
00182     void        mci_ResetCommand() const;
00183     int32_t     mci_IRQHandler(uint8_t *txBuf, uint32_t *txCnt, uint8_t *rxBuf, uint32_t *rxCnt);
00184     int32_t     mci_FIFOIRQHandler(uint8_t *txBuf, uint32_t *txCnt, uint8_t *rxBuf, uint32_t *rxCnt);
00185     void        mci_ReadFIFO(uint32_t *pDst, bool bFirstHalf) const;
00186     void        mci_WriteFIFO(uint32_t *pSrc, bool bFirstHalf) const;
00187                 
00188     void        mci_SetupEventWakeup(uint8_t dmaChannel);
00189     uint32_t    mci_WaitForEvent() const;
00190 
00191     ReturnCode _readBlocks(uint32_t card_type, uint32_t startBlock, uint32_t blockNum) const;
00192     ReturnCode _writeBlocks(uint32_t card_type, uint32_t startBlock, uint32_t blockNum) const;
00193 
00194 
00195     uint32_t _Stat;
00196     SDMMC_CARD_T _sdCardInfo;
00197 
00198     DigitalIn* _cardDetect;
00199 
00200     uint8_t _eventDmaChannel;    /*!< DMA Channel used for transfer data */
00201     volatile bool _eventReceived;
00202     volatile bool _eventSuccess;
00203 };
00204 
00205 #endif
00206