📄 mmc_drv.c
字号:
/*C**************************************************************************
* NAME: mmc_drv.c
*----------------------------------------------------------------------------
* Copyright (c) 2003 Atmel.
*----------------------------------------------------------------------------
* RELEASE: snd1c-refd-nf-4_0_3
* REVISION: 1.5
*----------------------------------------------------------------------------
* PURPOSE:
* This file contains the MMC driver routines
*
* NOTES:
* Driver Configuration:
* - None
* Global Variables:
* - None
*****************************************************************************/
/*_____ I N C L U D E S ____________________________________________________*/
#include "config.h" /* system configuration */
#include "mmc_drv.h" /* mmc driver definition */
/*_____ M A C R O S ________________________________________________________*/
/*_____ D E F I N I T I O N ________________________________________________*/
static Byte mmc_state;
static bit mmc_ready; /* MMC in prog state */
/*_____ D E C L A R A T I O N ______________________________________________*/
void mmc_set_prio (Byte);
void mmc_send_cmd (Byte, Uint32, Byte);
bit mmc_check_response (void);
/*F**************************************************************************
* NAME: mmc_set_prio
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Set the MMC controller priority interrupt
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
* ram/xram:
* cycle:
* stack:
* code:
*****************************************************************************/
void mmc_set_prio (Byte priority)
{
if ((priority == 1) || (priority == 3)) /* set LSB priority bit */
{
IPL1 |= MSK_ESPI;
}
if ((priority == 2) || (priority == 3)) /* set MSB priority bit */
{
IPH1 |= MSK_ESPI;
}
}
/*F**************************************************************************
* NAME: mmc_send_cmd
*----------------------------------------------------------------------------
* PARAMS:
* index: command index
* argument: argument (32 bits) of the command to send
* response: expected response to the command to send
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Send a command on the bus
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* The fifo lock flag is not tested it is under firmware responsability to
* take care of inter-command delays
*----------------------------------------------------------------------------
* REQUIREMENTS:
* ram/xram:
* cycle:
* stack:
* code:
*****************************************************************************/
void mmc_send_cmd (Byte index, Uint32 argument, Byte response)
{
MMCMD = index;
MMCMD = ((Byte*)&argument)[0];
MMCMD = ((Byte*)&argument)[1];
MMCMD = ((Byte*)&argument)[2];
MMCMD = ((Byte*)&argument)[3];
switch (response)
{
case MMC_RESP_R1:
case MMC_RESP_R4:
case MMC_RESP_R5:
{
MMCON0 |= MSK_RFMT; /* set 48 bits response */
MMCON0 &= ~MSK_CRCDIS; /* set response with CRC7 */
MMCON1 |= (MSK_CMDEN|MSK_RESPEN);
MMCON1 &= ~(MSK_CMDEN|MSK_RESPEN); /* send command with response */
break;
}
case MMC_RESP_R2:
{
MMCON0 &= ~(MSK_RFMT | MSK_CRCDIS); /* set 136 bits response with CRC7 */
MMCON1 |= (MSK_CMDEN|MSK_RESPEN);
MMCON1 &= ~(MSK_CMDEN|MSK_RESPEN); /* send command with response */
break;
}
case MMC_RESP_R3:
{
MMCON0 |= (MSK_RFMT | MSK_CRCDIS); /* set 48 bits response without CRC7 */
MMCON1 |= (MSK_CMDEN|MSK_RESPEN);
MMCON1 &= ~(MSK_CMDEN|MSK_RESPEN); /* send command with response */
break;
}
case MMC_NO_RESP:
default:
{
MMCON1 |= MSK_CMDEN;
MMCON1 &= ~MSK_CMDEN; /* send command without response */
break;
}
}
}
/*F**************************************************************************
* NAME: mmc_send_scmd
*----------------------------------------------------------------------------
* PARAMS:
* index: command index
* response: expected response to the command to send
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Send a short command on the bus
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* The fifo lock flag is not tested it is under firmware responsability to
* take care of inter-command delays
*----------------------------------------------------------------------------
* REQUIREMENTS:
* ram/xram:
* cycle:
* stack:
* code:
*****************************************************************************/
void mmc_send_scmd (Byte index, Byte response)
{
MMCMD = index;
MMCMD = (Byte)MMC_NO_ARG;
MMCMD = (Byte)MMC_NO_ARG;
MMCMD = (Byte)MMC_NO_ARG;
MMCMD = (Byte)MMC_NO_ARG;
switch (response)
{
case MMC_RESP_R1:
case MMC_RESP_R4:
case MMC_RESP_R5:
case SD_RESP_R6:
{
MMCON0 |= MSK_RFMT; /* set 48 bits response */
MMCON0 &= ~MSK_CRCDIS; /* set response with CRC7 */
MMCON1 |= (MSK_CMDEN|MSK_RESPEN);
MMCON1 &= ~(MSK_CMDEN|MSK_RESPEN); /* send command with response */
break;
}
case MMC_RESP_R2:
{
MMCON0 &= ~(MSK_RFMT | MSK_CRCDIS); /* set 136 bits response with CRC7 */
MMCON1 |= (MSK_CMDEN|MSK_RESPEN);
MMCON1 &= ~(MSK_CMDEN|MSK_RESPEN); /* send command with response */
break;
}
case MMC_RESP_R3:
{
MMCON0 |= (MSK_RFMT | MSK_CRCDIS); /* set 48 bits response without CRC7 */
MMCON1 |= (MSK_CMDEN|MSK_RESPEN);
MMCON1 &= ~(MSK_CMDEN|MSK_RESPEN); /* send command with response */
break;
}
case MMC_NO_RESP:
default:
{
MMCON1 |= MSK_CMDEN;
MMCON1 &= ~MSK_CMDEN; /* send command without response */
break;
}
}
}
/*F**************************************************************************
* NAME: mmc_check_response
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
* MMC_ERR_RESP: no response or bad format received
* MMC_RESP_OK: response received
*----------------------------------------------------------------------------
* PURPOSE:
* Check command response
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
* ram/xram:
* cycle:
* stack:
* code:
*****************************************************************************/
bit mmc_check_response (void)
{
if (Mmc_response_received())
{ /* response received */
if ((MMCON0 & MSK_CRCDIS) != 0)
{ /* CRC7 not computed */
if ((MMSTA & MSK_RESPFS) != 0)
{
return (MMC_RESP_OK);
}
else
{
return (MMC_ERR_RESP); /* format error */
}
}
else
{ /* CRC7 computed */
if ((MMSTA & (MSK_RESPFS | MSK_CRC7S)) == (MSK_RESPFS | MSK_CRC7S))
{
return (MMC_RESP_OK);
}
else
{
return (MMC_ERR_RESP); /* format or CRC7 error */
}
}
}
else
{ /* no response received */
return (MMC_ERR_RESP);
}
}
/*F**************************************************************************
* NAME: mmc_read_response
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
* 4-byte argument of the response
*----------------------------------------------------------------------------
* PURPOSE:
* Read command argument response
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
* ram/xram:
* cycle:
* stack:
* code:
*****************************************************************************/
Uint32 mmc_read_response (void)
{
Uint32 argument;
((Byte*)&argument)[0] = MMCMD; /* dummy index read */
((Byte*)&argument)[0] = MMCMD;
((Byte*)&argument)[1] = MMCMD;
((Byte*)&argument)[2] = MMCMD;
((Byte*)&argument)[3] = MMCMD;
return argument;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -