// ***********************************************************
// File: mmc.c
// Description: Library to access a MultiMediaCard
// functions: init, read, write ...
// C. Speck / S. Schauer
// Texas Instruments, Inc
// June 2005
//
// Version 1.1
// corrected comments about connection the MMC to the MSP430
// increased timeout in mmcGetXXResponse
//
// ***********************************************************
// MMC Lib
// ***********************************************************
/* ***********************************************************
* THIS PROGRAM IS PROVIDED "AS IS". TI MAKES NO WARRANTIES OR
* REPRESENTATIONS, EITHER EXPRESS, IMPLIED OR STATUTORY,
* INCLUDING ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR
* COMPLETENESS OF RESPONSES, RESULTS AND LACK OF NEGLIGENCE.
* TI DISCLAIMS ANY WARRANTY OF TITLE, QUIET ENJOYMENT, QUIET
* POSSESSION, AND NON-INFRINGEMENT OF ANY THIRD PARTY
* INTELLECTUAL PROPERTY RIGHTS WITH REGARD TO THE PROGRAM OR
* YOUR USE OF THE PROGRAM.
*
* IN NO EVENT SHALL TI BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
* CONSEQUENTIAL OR INDIRECT DAMAGES, HOWEVER CAUSED, ON ANY
* THEORY OF LIABILITY AND WHETHER OR NOT TI HAS BEEN ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGES, ARISING IN ANY WAY OUT
* OF THIS AGREEMENT, THE PROGRAM, OR YOUR USE OF THE PROGRAM.
* EXCLUDED DAMAGES INCLUDE, BUT ARE NOT LIMITED TO, COST OF
* REMOVAL OR REINSTALLATION, COMPUTER TIME, LABOR COSTS, LOSS
* OF GOODWILL, LOSS OF PROFITS, LOSS OF SAVINGS, OR LOSS OF
* USE OR INTERRUPTION OF BUSINESS. IN NO EVENT WILL TI'S
* AGGREGATE LIABILITY UNDER THIS AGREEMENT OR ARISING OUT OF
* YOUR USE OF THE PROGRAM EXCEED FIVE HUNDRED DOLLARS
* (U.S.$500).
*
* Unless otherwise stated, the Program written and copyrighted
* by Texas Instruments is distributed as "freeware". You may,
* only under TI's copyright in the Program, use and modify the
* Program without any charge or restriction. You may
* distribute to third parties, provided that you transfer a
* copy of this license to the third party and the third party
* agrees to these terms by its first use of the Program. You
* must reproduce the copyright notice and any other legend of
* ownership on each copy or partial copy, of the Program.
*
* You acknowledge and agree that the Program contains
* copyrighted material, trade secrets and other TI proprietary
* information and is protected by copyright laws,
* international copyright treaties, and trade secret laws, as
* well as other intellectual property laws. To protect TI's
* rights in the Program, you agree not to decompile, reverse
* engineer, disassemble or otherwise translate any object code
* versions of the Program to a human-readable form. You agree
* that in no event will you alter, remove or destroy any
* copyright notice included in the Program. TI reserves all
* rights not specifically granted under this license. Except
* as specifically provided herein, nothing in this agreement
* shall be construed as conferring by implication, estoppel,
* or otherwise, upon you, any license or other right under any
* TI patents, copyrights or trade secrets.
*
* You may not use the Program in non-TI devices.
* ********************************************************* */
// ********************************************************
//
//
//
// MSP430F149 MMC Card
// ----------------- -----------------
// /|\| XIN|- /|\ | |
// | | | | | |
// --|RST XOUT|- |--|Pin4/Vcc |
// | | | |
// | | | |
// | (P5.5) |<-------|Pin6/CD |
// | P5.4 |------->|Pin1/CS |
// | | | |
// | P5.2/SOMI1 |<-------|Pin2/DIN |
// | P5.1/SIMO1 |------->|Pin7/DOUT |
// | P5.3/UCLK1 |------->|Pin5/CLK |
// | | | |
// | | |--|Pin3/GND |
// |
// =
//
// Pin configuration at MSP430F149:
// --------------------------------
// MSP430F149 MSP Pin MMC MMC Pin
// -------------------------------------------------------------
// P5.4 48 ChipSelect 1
// P5.2 / SOMI 46 DataIn 2
// GND 3 (0 V)
// VDD 4 (3.3 V)
// P5.3 / UCLK1 47 Clock 5
// P5.5 44 CardDetect 6
// P5.1 / SIMO 45 DataOut 7
// -------------------------------------------------------------
//
//
//
//
#ifndef _MMCLIB_C
#define _MMCLIB_C
//
//---------------------------------------------------------------
#include "mmc.h"
#include "msp430f5529.H"
//#define withDMA
// Function Prototypes
char mmcGetResponse(void);
char mmcGetXXResponse(const char resp);
char mmcCheckBusy(void);
void initSPI(void);
uint8 spiSendByte(const uint8 data);
char mmc_GoIdle();
// Varialbes
char mmc_buffer[512] =
{ 0 }; // Buffer for mmc i/o for data and registers
//---------------------------------------------------------------------
// setup usart1 in spi mode
void initSPI(void)
{
UCB0CTL1 |= UCSWRST; // 8-bit SPI Master **SWRST**
UCB0CTL0 = UCCKPH | UCMODE_0 | UCMST | UCSYNC | UCMSB; // SMCLK, 3-pin mode, clock idle low, data valid on rising edge, UCLK delayed
UCB0CTL1 = UCSSEL_2 | UCSWRST; // 8-bit SPI Master **SWRST**
UCB0BR0 = 0x02; // 0x02: UCLK/2 (4 MHz), works also with 3 and 4
UCB0BR1 = 0x00; // -"-
//UCMCTL = 0x00; // no modulation
UCB0CTL1 &= ~UCSWRST; // clear SWRST
//ME2 |= USPIE1; // Enable USART1 SPI mode
while (!(UCB0IFG & UCTXIFG))
; // USART1 TX buffer ready (empty)?
}
// Initialize MMC card
char initMMC(void)
{
//raise SS and MOSI for 80 clock cycles
//SendByte(0xff) 10 times with SS high
//RAISE SS
int i;
// Port 5 Function Dir On/Off
// 5.5-mmcCD Inp 0 - card inserted
// 5.1-Dout Out 0 - off 1 - On -> init in SPI_Init
// 5.2-Din Inp 0 - off 1 - On -> init in SPI_Init
// 5.3-Clk Out - -> init in SPI_Init
// 5.4-mmcCS Out 0 - Active 1 - none Active
//P5SEL |= 0x0E;
//P5SEL &= ~0x11;
//P5OUT |= 0x10;
//P5DIR |= 0x1A;
P3SEL |= BIT0 + BIT1 + BIT2;
P3DIR |= BIT0 + BIT2;
//P3REN |= BIT1;
//P3OUT |= BIT1;
P2SEL &= ~BIT6;
P2OUT |= BIT6;
P2DIR |= BIT6;
initSPI();
//initialization sequence on PowerUp
CS_HIGH();
for (i = 0; i <= 9; i++)
spiSendByte(0xff);
return (mmc_GoIdle());
}
char mmc_GoIdle()
{
char response = 0x01;
CS_LOW();
//Send Command 0 to put MMC in SPI mode
mmcSendCmd(MMC_GO_IDLE_STATE, 0, 0x95);
//Now wait for READY RESPONSE
if (mmcGetResponse() != 0x01)
return MMC_INIT_ERROR;
while (response == 0x01)
{
CS_HIGH();
spiSendByte(0xff);
CS_LOW();
mmcSendCmd(MMC_SEND_OP_COND, 0x00, 0xff);
response = mmcGetResponse();
}
CS_HIGH();
spiSendByte(0xff);
return (MMC_SUCCESS);
}
// mmc Get Responce
char mmcGetResponse(void)
{
//Response comes 1-8bytes after command
//the first bit will be a 0
//followed by an error code
//data