📄 mmc.c
字号:
/*
* File: mmc.c
* Purpose: SD/MMC handler functions
* Author: ChaN, Peter Ivanov
* Modified by:
* Created: 2007-05-19 11:29:32
* Last modify: 2007-10-03 21:11:58 ivanovp {Time-stamp}
* Copyright: (C) ChaN, Peter Ivanov, 2007
* Licence: GPL
*/
/**
* \file mmc.c
* \brief SD/MMC handler functions. Disk IO implementation for DOS filesystem.
* \author ChaN, Peter Ivanov
*/
/*-----------------------------------------------------------------------*/
/* MMC/SD (in SPI mode) control module (C)ChaN, 2007 */
/*-----------------------------------------------------------------------*/
/* Only rcvr_spi(), xmit_spi(), disk_timerproc() and some macros are */
/* platform dependent. */
/*-----------------------------------------------------------------------*/
#include <msp430xG461x.h>
#include <string.h>
#include "diskio.h"
#include "mmc.h"
#include "lcd.h" // debug!
/* MMC/SD command (in SPI) */
#define CMD0 (0x40+0) /* GO_IDLE_STATE */
#define CMD1 (0x40+1) /* SEND_OP_COND */
#define CMD8 (0x40+8) /* SEND_IF_COND */
#define CMD9 (0x40+9) /* SEND_CSD */
#define CMD10 (0x40+10) /* SEND_CID */
#define CMD12 (0x40+12) /* STOP_TRANSMISSION */
#define CMD16 (0x40+16) /* SET_BLOCKLEN */
#define CMD17 (0x40+17) /* READ_SINGLE_BLOCK */
#define CMD18 (0x40+18) /* READ_MULTIPLE_BLOCK */
#define CMD23 (0x40+23) /* SET_BLOCK_COUNT */
#define CMD24 (0x40+24) /* WRITE_BLOCK */
#define CMD25 (0x40+25) /* WRITE_MULTIPLE_BLOCK */
#define CMD41 (0x40+41) /* SEND_OP_COND (ACMD) */
#define CMD55 (0x40+55) /* APP_CMD */
#define CMD58 (0x40+58) /* READ_OCR */
/* Control signals (Platform dependent) */
#define SELECT() P3OUT &= ~BIT0 // Card Select
#define DESELECT() P3OUT |= BIT0 // Card Deselect
//#define SOCKPORT IO.PDR5.BYTE /* Socket control port */
#define SOCKWP BIT5 /* Write protect switch (PB5) */
#define SOCKINS BIT1 /* Card detect switch (PB4) */
//#define CARDPWR 0x80 /* Card power (PE7) */
/*--------------------------------------------------------------------------
Module Private Functions
---------------------------------------------------------------------------*/
static volatile
DSTATUS Stat = STA_NOINIT; /* Disk status */
static volatile
BYTE Timer1, Timer2; /* 100Hz decrement timer */
static
BYTE CardType; /* b0:MMC, b1:SDC, b2:Block addressing */
// Set up UASRT11 in SPI mode
void MMC_init (void)
{
// Initialize SPI
// master mode, data valid on rising edge, msb first
UCB0CTL0 = UCCKPH + UCMST + UCMSB + UCSYNC;
// clock -> SMCLK
UCB0CTL1 = UCSSEL1;
// // 0x02: SMCLK/2 (4 MHz)
// UCB0BR0 = 0x02;
// UCB0BR0 = 0x00;
// 0x02: SMCLK/2 (1 MHz)
UCB0BR0 = 0x02;
UCB0BR1 = 0x00;
// second functionality of pin
P3SEL |= BIT1 | BIT2 | BIT3; // P3.1-3 SPI option select
P3DIR |= BIT0; // P3.0 output direction
// end of SPI initialization
// CP (Card present) - P1.0
P1SEL &= ~BIT0; // gpio
P1DIR &= ~BIT0; // input
// WP (Write protection) - P7.5
P7SEL &= ~BIT5; // gpio
P7DIR &= ~BIT5; // input
}
inline char MMC_cardPresent ()
{
return !(P1IN & BIT0);
}
inline char MMC_cardWriteProtected ()
{
return (P7IN & BIT5);
}
/*-----------------------------------------------------------------------*/
/* Transmit a byte to MMC via SPI (Platform dependent) */
/*-----------------------------------------------------------------------*/
static
void xmit_spi (BYTE data)
{
uint8_t dummy;
while ((IFG2 & UCB0TXIFG) == 0); // wait while not ready / for RX
UCB0TXBUF = data; // write
while ((IFG2 & UCB0RXIFG) == 0); // wait for RX buffer (full)
dummy = UCB0RXBUF;
//return (UCB0RXBUF);
}
/*-----------------------------------------------------------------------*/
/* Receive a byte from MMC via SPI (Platform dependent) */
/*-----------------------------------------------------------------------*/
static
BYTE rcvr_spi (void)
{
while ((IFG2 & UCB0TXIFG) == 0); // wait while not ready / for RX
UCB0TXBUF = 0xFF; // write a dummy byte
while ((IFG2 & UCB0RXIFG) == 0); // wait for RX buffer (full)
return (UCB0RXBUF);
}
/*-----------------------------------------------------------------------*/
/* Wait for card ready */
/*-----------------------------------------------------------------------*/
static
BYTE wait_ready (void)
{
BYTE res;
Timer2 = 50; /* Wait for ready in timeout of 500ms */
rcvr_spi();
do
res = rcvr_spi();
while ((res != 0xFF) && Timer2);
return res;
}
/*-----------------------------------------------------------------------*/
/* Power Control (Platform dependent) */
/*-----------------------------------------------------------------------*/
/* When the target system does not support socket power control, there */
/* is nothing to do in these functions and chk_power always returns 1. */
#if 0
static
void power_on (void)
{
IO.PUCR5.BYTE = 0x30; /* Enable pull-up for socket contacts */
IO.PDR5.BYTE = 0x00; /* Power ON */
IO.PCR5 = 0x80;
for (Timer1 = 2; Timer1; ); /* Wait for 20ms */
IO.PDR5.BYTE = 0x0E; /* Enable Driver */
IO.PCR5 = 0x8B;
for (Timer1 = 1; Timer1; ); /* Wait for 10ms */
}
static
void power_off (void)
{
SELECT(); /* Wait for card ready */
wait_ready();
DESELECT();
rcvr_spi();
IO.PDR5.BYTE = 0x00; /* Disable driver */
IO.PCR5 = 0x80;
IO.PDR5.BYTE = 0x80; /* Power OFF */
Stat |= STA_NOINIT; /* Set STA_NOINIT */
}
#endif
/*-----------------------------------------------------------------------*/
/* Receive a data packet from MMC */
/*-----------------------------------------------------------------------*/
static
BOOL rcvr_datablock (
BYTE *buff, /* Data buffer to store received data */
UINT btr /* Byte count (must be even number) */
)
{
BYTE token;
Timer1 = 10;
do { /* Wait for data packet in timeout of 100ms */
token = rcvr_spi();
} while ((token == 0xFF) && Timer1);
if(token != 0xFE) return FALSE; /* If not valid data token, retutn with error */
do { /* Receive the data block into buffer */
*buff++ = rcvr_spi();
*buff++ = rcvr_spi();
} while (btr -= 2);
rcvr_spi(); /* Discard CRC */
rcvr_spi();
return TRUE; /* Return with success */
}
/*-----------------------------------------------------------------------*/
/* Send a data packet to MMC */
/*-----------------------------------------------------------------------*/
#if _READONLY == 0
static
BOOL xmit_datablock (
const BYTE *buff, /* 512 byte data block to be transmitted */
BYTE token /* Data/Stop token */
)
{
BYTE resp, wc;
if (wait_ready() != 0xFF) return FALSE;
xmit_spi(token); /* Xmit data token */
if (token != 0xFD) { /* Is data token */
wc = 0;
do { /* Xmit the 512 byte data block to MMC */
xmit_spi(*buff++);
xmit_spi(*buff++);
} while (--wc);
xmit_spi(0xFF); /* CRC (Dummy) */
xmit_spi(0xFF);
resp = rcvr_spi(); /* Reveive data response */
if ((resp & 0x1F) != 0x05) /* If not accepted, return with error */
return FALSE;
}
return TRUE;
}
#endif /* _READONLY */
/*-----------------------------------------------------------------------*/
/* Send a command packet to MMC */
/*-----------------------------------------------------------------------*/
static
BYTE send_cmd (
BYTE cmd, /* Command byte */
DWORD arg /* Argument */
)
{
BYTE n, res;
if (wait_ready() != 0xFF) return 0xFF;
/* Send command packet */
xmit_spi(cmd); /* Command */
xmit_spi((BYTE)(arg >> 24)); /* Argument[31..24] */
xmit_spi((BYTE)(arg >> 16)); /* Argument[23..16] */
xmit_spi((BYTE)(arg >> 8)); /* Argument[15..8] */
xmit_spi((BYTE)arg); /* Argument[7..0] */
n = 0;
if (cmd == CMD0) n = 0x95; /* CRC for CMD0(0) */
if (cmd == CMD8) n = 0x87; /* CRC for CMD8(0x1AA) */
xmit_spi(n);
/* Receive command response */
if (cmd == CMD12) rcvr_spi(); /* Skip a stuff byte when stop reading */
n = 10; /* Wait for a valid response in timeout of 10 attempts */
do
res = rcvr_spi();
while ((res & 0x80) && --n);
return res; /* Return with the response value */
}
/*--------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -