Nut/OS  5.0.5
API Reference
lpc177x_8x_mci.c File Reference
#include <cfg/arch.h>
#include <sys/timer.h>
#include <dev/gpio.h>
#include <dev/irqreg.h>
#include <arch/cm3/nxp/lpc177x_8x_clk.h>
#include <arch/cm3/nxp/lpc177x_8x_mci.h>
Include dependency graph for lpc177x_8x_mci.c:

Defines

#define _SHIFT(x)   (1 << x)
#define _XSHIFT(x, y)   (x << y)
#define SHIFT_(x)   (1 >> x)
#define XSHIFT_(x, y)   (x >> y)
#define _SBF(f, v)   (v<<f)
#define MCI_CARDSTATUS_READYFORDATA_P0S   8
#define MCI_CARDSTATUS_CURRENTSTATE_POS   9
#define MCI_CARDSTATUS_CURRENTSTATE_BMASK   0x0F
#define CARDSTATEOF(x)   (XSHIFT_(x, MCI_CARDSTATUS_CURRENTSTATE_POS) & MCI_CARDSTATUS_CURRENTSTATE_BMASK)
#define MCI_CMD8_VOLTAGESUPPLIED_POS   8
#define MCI_CMD8_VOLTAGESUPPLIED_BMASK   0xFF
#define MCI_CMD8_CHECKPATTERN_POS   0
#define MCI_CMD8_CHECKPATTERN_BMASK   0xFF
#define MCI_ACMD41_HCS_POS   30
#define MCI_PWRCTRL_BMASK   0xC3
#define MCI_PWRCTRL_OPENDRAIN_POS   6
#define MCI_PWRCTRL_OPENDRAIN_NUMBIT   1
#define MCI_PWRCTRL_OPENDRAIN_BMASK   0x01
#define MCI_CID_MANUFACTURER_ID_WPOS   24
#define MCI_CID_MANUFACTURER_ID_WBMASK   0xFF
#define MCI_CID_OEMAPPLICATION_ID_WPOS   8
#define MCI_CID_OEMAPPLICATION_ID_WBMASK   0xFFFF
#define MCI_CID_PRODUCTNAME_ID_H_WPOS   0
#define MCI_CID_PRODUCTNAME_ID_H_WBMASK   0xFF
#define MCI_CID_PRODUCTNAME_ID_L_WPOS   0
#define MCI_CID_PRODUCTNAME_ID_L_WBMASK   0xFFFFFFFF
#define MCI_CID_PRODUCTREVISION_ID_WPOS   24
#define MCI_CID_PRODUCTREVISION_ID_WBMASK   0xFF
#define MCI_CID_PRODUCTSERIALNUM_ID_H_WPOS   0
#define MCI_CID_PRODUCTSERIALNUM_ID_H_WBMASK   0x00FFFFFF
#define MCI_CID_PRODUCTSERIALNUM_ID_L_WPOS   24
#define MCI_CID_PRODUCTSERIALNUM_ID_L_WBMASK   0xFF
#define MCI_CID_PRODUCTSERIALNUM_ID_WBMASK   0xFFFFFFFF
#define MCI_CID_RESERVED_ID_WPOS   20
#define MCI_CID_RESERVED_ID_WBMASK   0x1F
#define MCI_CID_MANUFACTURINGDATE_ID_WPOS   8
#define MCI_CID_MANUFACTURINGDATE_ID_WBMASK   0x0FFF
#define MCI_CID_CHECKSUM_ID_WPOS   1
#define MCI_CID_CHECKSUM_ID_WBMASK   0x7F
#define MCI_CID_UNUSED_ID_WPOS   0
#define MCI_CID_UNUSED_ID_WBMASK   0x01

Functions

uint32_t Lpc177x_8x_MciSettingDma (uint8_t *memBuf, uint32_t ChannelNum, uint32_t DMAMode)
void Lpc177x_8x_MciTXEnable (void)
 Enable Transmit data interrupt.
void Lpc177x_8x_MciRXEnable (void)
 Enable Receive data interrupt.
void Lpc177x_8x_MciTXDisable (void)
 Disable Transmit data interrupt.
void Lpc177x_8x_MciRXDisable (void)
 Disable Receive data interrupt.
void Lpc177x_8x_MciCmdProcess (void)
 Called by MCI interrupt handler to simplify the command process.
void Lpc177x_8x_MciDataErrorProcess (void)
 Called by MCI interrupt handler to manage error on the bus.
void Lpc177x_8x_MciDATA_END_InterruptService (void)
 Called by MCI interrupt handler. This is the last interrupt manipulates the process of the data-block write and read to/with card.
void Lpc177x_8x_MciFIFOInterruptService (void)
 Called by MCI interrupt handler if requiring to using FIFO for data transferring. It copy data to/from FIFO register from/to a data buffer.
int32_t Lpc177x_8x_MciCheckStatus (void)
 Check status when working with data-block transfer.
void Lpc177x_8x_MciSet_MCIClock (uint32_t ClockRate)
 Set MCI clock rate, during initialization phase < 400K during data phase < 20Mhz.
int32_t Lpc177x_8x_MciSetBusWidth (uint32_t width)
 Set the Width to 1-bit Bus or 4-bit Bus.
int32_t Lpc177x_8x_MciInit (uint8_t powerActiveLevel)
 Do initialization the MCI block as set its clock, registers, setup NVIC for interrupts, configure the pins used for MCI function, do initialize the card in slot...
void Lpc177x_8x_MciSetOutputMode (uint32_t mode)
 Set output in open drain mode or pushpull mode.
void Lpc177x_8x_MciSendCmd (uint32_t CmdIndex, uint32_t Argument, uint32_t ExpectResp, uint32_t AllowTimeout)
 The routine is used to send a CMD to the card.
int32_t Lpc177x_8x_MciGetCmdResp (uint32_t ExpectCmdData, uint32_t ExpectResp, uint32_t *CmdResp)
 The routine is to get the reponse from card after commands. This function is always used in pair of Lpc177x_8x_MciSendCmd() func.
int32_t Lpc177x_8x_MciCmdResp (uint32_t CmdIndex, uint32_t Argument, uint32_t ExpectResp, uint32_t *CmdResp, uint32_t AllowTimeout)
 The routine is to send command to cards then get back the reponses (if required).
int32_t Lpc177x_8x_MciCardReset (void)
 To reset the card, the CMD0 is sent and then the card is put in idle state. This is the very first command to be sent to initialize either MMC or SD card.
int32_t Lpc177x_8x_MciCmd_SendOpCond (void)
 Send CMD1 (SEND_OP_COND) to card.
int32_t Lpc177x_8x_MciCmd_SendIfCond (void)
 Send CMD8 (SEND_IF_COND) for interface condition to card.
int32_t Lpc177x_8x_MciCmd_SendACMD (void)
 Send CMD55 (APP_CMD) to indicate to the card that the next command is an application specific command rather than a standard command. Before an ACMD, call this routine first.
int32_t Lpc177x_8x_MciAcmd_SendOpCond (uint8_t hcsVal)
 Send ACMD41 (SEND_APP_OP_COND) to Host Capacity Support (HCS) information and asks the accessed card to send its operating condition (OCR).
int32_t Lpc177x_8x_MciCardInit (void)
 Do initialization for the card in the slot.
en_Mci_CardType Lpc177x_8x_MciGetCardType (void)
 Get the type of card that is currently used in the slot.
int32_t Lpc177x_8x_MciGetCID (st_Mci_CardId *cidValue)
 Get the all the Identifier (CID) of the card by sending the CMD2 (ALL_SEND_CID) command. Then parse 4-byte data obtained from the card by the CID meaning.
int32_t Lpc177x_8x_MciSetCardAddress (void)
 Set the address for the card in the slot by sending CMD3 (SET_RELATIVE_ADDR) command. To get the address of the card currently in used, needs to call Lpc177x_8x_MciGetCardAddress()
uint32_t Lpc177x_8x_MciGetCardAddress (void)
 Get the address for the card in the slot.
int32_t Lpc177x_8x_MciGetCSD (uint32_t *csdVal)
 Get the Card-Specific Data of in-slot card by sending CMD9 (SEND_CSD) command.
int32_t Lpc177x_8x_MciCmd_SelectCard (void)
 Select the card by the specified address. This is done by sending out the CMD7 with the address argument to needed card.
int32_t Lpc177x_8x_MciGetCardStatus (int32_t *cardStatus)
 Get the status of the card. The return is from the card. By sending CMD13 (SEND_STATUS), the status of the card will be responded from card addressed.
int32_t Lpc177x_8x_MciSetBlockLen (uint32_t blockLength)
 Set the length for the blocks in the next action on data manipulation (as read, write, erase). This function is to send CMD16 (SET_BLOCK_LEN) to cards.
int32_t Lpc177x_8x_MciAcmd_SendBusWidth (uint32_t buswidth)
 Set bus-width (1 bit or 4 bit) to work with the card by command CMD6 (SET_ACMD_BUS_WIDTH).
uint32_t Lpc177x_8x_MciGetDataXferEndState (void)
 Get the state of data transfer to see if it is ended or not.
uint32_t Lpc177x_8x_MciGetXferErrState (void)
 Get the error state of the lastest data transfer.
int32_t Lpc177x_8x_MciCmd_StopTransmission (void)
 Stop the current transmission on the bus by sending command CMD12 (STOP_TRANSMISSION). In this case, the card may be in a unknown state. So that it need a warm reset for normal operation.
int32_t Lpc177x_8x_MciCmd_WriteBlock (uint32_t blockNum, uint32_t numOfBlock)
 Write blocks to card by sending command CMD24 (WRITE_BLOCK) or command CMD25 (WRITE_MULTIPLE_BLOCK) followed by the blocks of data to be written.
int32_t Lpc177x_8x_MciCmd_ReadBlock (uint32_t blockNum, uint32_t numOfBlock)
 Read blocks to card by sending CMD17 (READ_SINGLE_BLOCK) or CMD18 (READ_MULTIPLE_BLOCK) commands followed by the blocks of data to be read.
int32_t Lpc177x_8x_MciWriteBlock (uint8_t *memblock, uint32_t blockNum, uint32_t numOfBlock)
 Write data at a specific address to starting block with number of blocks will be written from first block.
int32_t Lpc177x_8x_MciReadBlock (uint8_t *destBlock, uint32_t blockNum, uint32_t numOfBlock)
 Read data at a specific address to starting block with number of blocks will be read from first block.
void Lpc177x_8x_MciPowerOff (void)
 Turn off the MCI power by disabling the Power Register for MCI.

Variables

volatile uint32_t Mci_Data_Xfer_End = 0
volatile uint32_t Mci_Data_Xfer_ERR = 0
volatile uint8_t fifo_plane = 0
volatile uint32_t CardRCA
volatile uint8_t CCS
volatile uint8_t MCI_AddressingMode
volatile en_Mci_CardType MCI_CardType
uint32_t dmaWrCh_TermianalCnt
uint32_t dmaWrCh_ErrorCnt
uint32_t dmaRdCh_TermianalCnt
uint32_t dmaRdCh_ErrorCnt
volatile uint8_tdataSrcBlock = (uint8_t *) MCI_DMA_SRC_ADDR
volatile uint8_tdataDestBlock = (uint8_t *) MCI_DMA_DST_ADDR
volatile uint32_t txBlockCnt = 0
volatile uint32_t rxBlockCnt = 0

Define Documentation

#define _XSHIFT (   x,
 
)    (x << y)
#define SHIFT_ (   x)    (1 >> x)
#define XSHIFT_ (   x,
 
)    (x >> y)
#define _SBF (   f,
 
)    (v<<f)
#define MCI_CARDSTATUS_CURRENTSTATE_POS   9
#define MCI_CARDSTATUS_CURRENTSTATE_BMASK   0x0F
#define CARDSTATEOF (   x)    (XSHIFT_(x, MCI_CARDSTATUS_CURRENTSTATE_POS) & MCI_CARDSTATUS_CURRENTSTATE_BMASK)
#define MCI_CMD8_VOLTAGESUPPLIED_POS   8
#define MCI_CMD8_VOLTAGESUPPLIED_BMASK   0xFF
#define MCI_CMD8_CHECKPATTERN_POS   0
#define MCI_CMD8_CHECKPATTERN_BMASK   0xFF
#define MCI_ACMD41_HCS_POS   30
#define MCI_PWRCTRL_BMASK   0xC3
#define MCI_PWRCTRL_OPENDRAIN_POS   6
#define MCI_PWRCTRL_OPENDRAIN_NUMBIT   1
#define MCI_PWRCTRL_OPENDRAIN_BMASK   0x01
#define MCI_CID_MANUFACTURER_ID_WPOS   24

Referenced by Lpc177x_8x_MciGetCID().

#define MCI_CID_MANUFACTURER_ID_WBMASK   0xFF

Referenced by Lpc177x_8x_MciGetCID().

#define MCI_CID_OEMAPPLICATION_ID_WPOS   8

Referenced by Lpc177x_8x_MciGetCID().

#define MCI_CID_OEMAPPLICATION_ID_WBMASK   0xFFFF

Referenced by Lpc177x_8x_MciGetCID().

#define MCI_CID_PRODUCTNAME_ID_H_WPOS   0

Referenced by Lpc177x_8x_MciGetCID().

#define MCI_CID_PRODUCTNAME_ID_H_WBMASK   0xFF

Referenced by Lpc177x_8x_MciGetCID().

#define MCI_CID_PRODUCTNAME_ID_L_WPOS   0

Referenced by Lpc177x_8x_MciGetCID().

#define MCI_CID_PRODUCTNAME_ID_L_WBMASK   0xFFFFFFFF

Referenced by Lpc177x_8x_MciGetCID().

#define MCI_CID_PRODUCTREVISION_ID_WPOS   24

Referenced by Lpc177x_8x_MciGetCID().

#define MCI_CID_PRODUCTREVISION_ID_WBMASK   0xFF

Referenced by Lpc177x_8x_MciGetCID().

#define MCI_CID_PRODUCTSERIALNUM_ID_H_WPOS   0

Referenced by Lpc177x_8x_MciGetCID().

#define MCI_CID_PRODUCTSERIALNUM_ID_H_WBMASK   0x00FFFFFF

Referenced by Lpc177x_8x_MciGetCID().

#define MCI_CID_PRODUCTSERIALNUM_ID_L_WPOS   24

Referenced by Lpc177x_8x_MciGetCID().

#define MCI_CID_PRODUCTSERIALNUM_ID_L_WBMASK   0xFF

Referenced by Lpc177x_8x_MciGetCID().

#define MCI_CID_PRODUCTSERIALNUM_ID_WBMASK   0xFFFFFFFF
#define MCI_CID_RESERVED_ID_WPOS   20

Referenced by Lpc177x_8x_MciGetCID().

#define MCI_CID_RESERVED_ID_WBMASK   0x1F

Referenced by Lpc177x_8x_MciGetCID().

#define MCI_CID_MANUFACTURINGDATE_ID_WPOS   8

Referenced by Lpc177x_8x_MciGetCID().

#define MCI_CID_MANUFACTURINGDATE_ID_WBMASK   0x0FFF

Referenced by Lpc177x_8x_MciGetCID().

#define MCI_CID_CHECKSUM_ID_WPOS   1

Referenced by Lpc177x_8x_MciGetCID().

#define MCI_CID_CHECKSUM_ID_WBMASK   0x7F

Referenced by Lpc177x_8x_MciGetCID().

#define MCI_CID_UNUSED_ID_WPOS   0

Referenced by Lpc177x_8x_MciGetCID().

#define MCI_CID_UNUSED_ID_WBMASK   0x01

Referenced by Lpc177x_8x_MciGetCID().


Function Documentation

uint32_t Lpc177x_8x_MciSettingDma ( uint8_t memBuf,
uint32_t  ChannelNum,
uint32_t  DMAMode 
)
void Lpc177x_8x_MciTXEnable ( void  )

Enable Transmit data interrupt.

Parameters:
None
Returns:
None

References DATA_END_INT_MASK, ERR_TX_INT_MASK, FIFO_TX_INT_MASK, and LPC_MCI.

Referenced by Lpc177x_8x_MciWriteBlock().

void Lpc177x_8x_MciRXEnable ( void  )

Enable Receive data interrupt.

Parameters:
None
Returns:
None

References DATA_END_INT_MASK, ERR_RX_INT_MASK, FIFO_RX_INT_MASK, and LPC_MCI.

Referenced by Lpc177x_8x_MciReadBlock().

void Lpc177x_8x_MciTXDisable ( void  )

Disable Transmit data interrupt.

Parameters:
None
Returns:
None

References DATA_END_INT_MASK, ERR_TX_INT_MASK, FIFO_TX_INT_MASK, and LPC_MCI.

Referenced by Lpc177x_8x_MciDATA_END_InterruptService().

void Lpc177x_8x_MciRXDisable ( void  )

Disable Receive data interrupt.

Parameters:
None
Returns:
None

References DATA_END_INT_MASK, ERR_RX_INT_MASK, FIFO_RX_INT_MASK, and LPC_MCI.

Referenced by Lpc177x_8x_MciDATA_END_InterruptService().

void Lpc177x_8x_MciCmdProcess ( void  )

Called by MCI interrupt handler to simplify the command process.

Parameters:
None
Returns:
None
Note:
In card initialization, the commnad interrupts are disabled

References LPC_MCI, MCI_CMD_ACTIVE, MCI_CMD_CRC_FAIL, MCI_CMD_RESP_END, MCI_CMD_SENT, and MCI_CMD_TIMEOUT.

void Lpc177x_8x_MciDataErrorProcess ( void  )

Called by MCI interrupt handler to manage error on the bus.

Parameters:
None
Returns:
None

References LPC_MCI, MCI_DATA_CRC_FAIL, MCI_DATA_TIMEOUT, Mci_Data_Xfer_End, Mci_Data_Xfer_ERR, MCI_RX_OVERRUN, MCI_START_BIT_ERR, and MCI_TX_UNDERRUN.

void Lpc177x_8x_MciDATA_END_InterruptService ( void  )

Called by MCI interrupt handler. This is the last interrupt manipulates the process of the data-block write and read to/with card.

This service is also used with/without DMA support. It simply clears the flag messages the in-process data-block transfer has been done/ended

Parameters:
None
Returns:
None

References Lpc177x_8x_MciRXDisable(), Lpc177x_8x_MciTXDisable(), LPC_MCI, MCI_DATA_BLK_END, MCI_DATA_END, Mci_Data_Xfer_End, Mci_Data_Xfer_ERR, MCI_RX_ACTIVE, and MCI_TX_ACTIVE.

Here is the call graph for this function:

void Lpc177x_8x_MciFIFOInterruptService ( void  )

Called by MCI interrupt handler if requiring to using FIFO for data transferring. It copy data to/from FIFO register from/to a data buffer.

Parameters:
None
Returns:
None
Note:
This function is done without DMA transfer support

References BLOCK_LENGTH, dataDestBlock, dataSrcBlock, FIFO_RX_INT_MASK, FIFO_TX_INT_MASK, LPC_MCI, MCI_RX_HALF_FULL, MCI_TX_HALF_EMPTY, NULL, rxBlockCnt, and txBlockCnt.

int32_t Lpc177x_8x_MciCheckStatus ( void  )

Check status when working with data-block transfer.

Right after the block read and write, this routine is important that, even the FIFO is empty, complete block has been sent, but, data is still being written to the card. This routine is to ensure that the data has been written based on the state of the card, not by the length being set.

Parameters:
None
Returns:
MCI_FUNC_OK if all success

References _SHIFT, CARDSTATEOF, Lpc177x_8x_MciGetCardStatus(), MCI_CARDSTATE_TRAN, MCI_CARDSTATUS_READYFORDATA_P0S, MCI_FUNC_ERR_STATE, MCI_FUNC_FAILED, MCI_FUNC_NOT_READY, and MCI_FUNC_OK.

Referenced by Lpc177x_8x_MciReadBlock(), and Lpc177x_8x_MciWriteBlock().

Here is the call graph for this function:

void Lpc177x_8x_MciSetOutputMode ( uint32_t  mode)

Set output in open drain mode or pushpull mode.

Parameters:
[in]modethe mode going to set
Returns:
None

References LPC_MCI, MCI_OUTPUT_MODE_OPENDRAIN, MCI_PWRCTRL_BMASK, and MCI_PWRCTRL_OPENDRAIN_POS.

Referenced by Lpc177x_8x_MciAcmd_SendOpCond(), and Lpc177x_8x_MciCardInit().

int32_t Lpc177x_8x_MciCmd_SendOpCond ( void  )

Send CMD1 (SEND_OP_COND) to card.

Parameters:
None
Returns:
MCI_FUNC_OK if all success

References ALLOW_CMD_TIMER, CMD1_SEND_OP_COND, EXPECT_SHORT_RESP, Lpc177x_8x_MciCmdResp(), MCI_CMD_TIMEOUT, MCI_FUNC_BUS_NOT_IDLE, MCI_FUNC_FAILED, MCI_FUNC_OK, MCI_FUNC_TIMEOUT, and OCR_INDEX.

Referenced by Lpc177x_8x_MciCardInit().

Here is the call graph for this function:

int32_t Lpc177x_8x_MciCmd_SendACMD ( void  )

Send CMD55 (APP_CMD) to indicate to the card that the next command is an application specific command rather than a standard command. Before an ACMD, call this routine first.

Parameters:
None
Returns:
MCI_FUNC_OK if all success

References ALLOW_CMD_TIMER, CARD_STATUS_ACMD_ENABLE, CardRCA, CMD55_APP_CMD, EXPECT_SHORT_RESP, Lpc177x_8x_MciCmdResp(), MCI_CardType, MCI_FUNC_FAILED, MCI_FUNC_NOT_READY, MCI_FUNC_OK, MCI_SDHC_SDXC_CARD, MCI_SDSC_V1_CARD, and MCI_SDSC_V2_CARD.

Referenced by Lpc177x_8x_MciAcmd_SendBusWidth(), and Lpc177x_8x_MciAcmd_SendOpCond().

Here is the call graph for this function:

void Lpc177x_8x_MciPowerOff ( void  )

Turn off the MCI power by disabling the Power Register for MCI.

Parameters:
None
Returns:
None

References LPC_MCI.


Variable Documentation

volatile uint8_t* dataSrcBlock = (uint8_t *) MCI_DMA_SRC_ADDR
volatile uint8_t* dataDestBlock = (uint8_t *) MCI_DMA_DST_ADDR