Subdirectory provided by Embedded Artists

Dependencies:   DM_FATFileSystem DM_HttpServer DM_USBHost EthernetInterface USBDevice mbed-rpc mbed-rtos mbed-src

Dependents:   lpc4088_displaymodule_hello_world_Sept_2018

Fork of DMSupport by Embedded Artists

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MCIFileSystem.h Source File

MCIFileSystem.h

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