⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 430sd.c

📁 5个430系列微控制器源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
// 这是一个MSP430与MMC/SD卡接口设计,提供纯源代码。真正DIY设计!
/*********************************************************
 * ----------------------------------------------------------------------------
 */
#include  <msp430x14x.h>
#include  <string.h>
#include "mmc.h"
#include "led.h"

extern char card_state;

extern char mmc_buffer[512];

char card_state = 0;                              // card state: 0: not found, 1 found (init successfull)
unsigned long loop;

void main (void)
{
  volatile unsigned char dco_cnt = 255;

  BCSCTL1 &= ~XT2OFF;                             // XT2on
  do                                              // wait for MCLK from quartz
  {

    IFG1 &= ~OFIFG;                               // Clear OSCFault flag
    for (dco_cnt = 0xff; dco_cnt > 0; dco_cnt--); // Time for flag to set
  }
  while ((IFG1 & OFIFG) != 0);                    // OSCFault flag still set?

  WDTCTL = WDTPW + WDTHOLD;                       // Stop WDT

  BCSCTL1 = 0x07;                                 // LFXT1: XT2on: LF quartz for MCLK

  BCSCTL2 = SELM1 + SELS;                         // LFXT2: use HF quartz (XT2)

  DCOCTL = 0xe0;

  P3SEL = 0x00;
  P3DIR = 0xcf;
  P3OUT = 0x0f;

  // Port 4 Function           Dir     On/Off
  //         4.0-Led red       Out       0 - off   1 - On
  //         4.1-Led green     Out       0 - off   1 - On
  //         4.5-CardDetected   ?        0 - ?     1 - ?
  //         4.6-WriteProtected ?        0 - ?     1 - ?
  // D 7 6 5 4 3 2 1 0
  P4SEL = 0x00;                                   //   0 0 0 0 0 0 0 0
  P4DIR = 0x03;                                   //   0 0 0 0 0 0 1 1
  P4OUT = 0x00;

  // Port 5 Function           Dir       On/Off
  //         5.1-Dout          Out       0 - off    1 - On
  //         5.2-Din           Inp       0 - off    1 - On
  //         5.3-Clk           Out       -
  //         5.4-mmcCS         Out       0 - Active 1 - none Active

  P5SEL = 0x00;
  P5DIR = 0xff;
  P5OUT = 0xfe;

  P6SEL = 0x00;
  P6OUT = 0x00;
  P6DIR = 0xff;

 ADC12IE = 0;
  ADC12IFG = 0;

  initLED();
  for (;;)
  {
    // switch on red led to indicate -> insert card

    // #ifdef DeBuG0
    RED_LED_ON();
    GREEN_LED_OFF();
    // Card insert?
    while(P4IN&0x20);
    //switch off both led's
    GREEN_LED_OFF();
    RED_LED_OFF();
    //init mmc card
    if (initMMC() == MMC_SUCCESS)                 // card found
    {
      card_state |= 1;
      GREEN_LED_ON();
      // Read Out Card Type and print it or trace memory
      memset(&mmc_buffer,0,512);
      mmcReadRegister (10, 16);
      mmc_buffer[7]=0;
      // PLease mofify based on your Compiler sim io function
      // debug_printf("Multi Media Card Name: %s",&mmc_buffer[3]);

      // Fill first Block (0) with 'A'
      memset(&mmc_buffer,'A',512);                //set breakpoint and trace mmc_buffer contents
      mmcWriteBlock(0x00);
      // Fill second Block (1)-AbsAddr 512 with 'B'
      memset(&mmc_buffer,'B',512);
      mmcWriteBlock(512);

      // Read first Block back to buffer
      memset(&mmc_buffer,0x00,512);
      mmcReadBlock(0x00,512);
      memset(&mmc_buffer,0x00,512);               //set breakpoint and trace mmc_buffer contents
      mmcReadBlock(512,512);
      memset(&mmc_buffer,0x00,512);               //set breakpoint and trace mmc_buffer contents
    }

    else
    {
      //Error card not detected or rejected during writing
      // switch red led on
      card_state = 0;                             // no card
      RED_LED_ON();
    }

    // #endif

  }
  //  return;
}

mmc.h
/*
  mmc.h: Dekcarations for Communikation with the MMC (see mmc.c) in unprotected spi mode.

Pin configuration at MSP430F149:
--------------------------------
  MC                    MC Pin          MMC                      MMC Pin
  P5.4                  48              ChipSelect               1
  P5.1 / SlaveInMasterOut 45            DataIn                   2
  .                                     GND                      3 (0 V)
  .                                     VDD                      4 (3.3 V)
  P5.3 UCLK1 / SlaveCLocK 47            Clock                    5
  .                                     GND                      6 (0 V)
  P5.2 / SlaveOutMasterIn 46            DataOut                  7
  P5.4                                  CardDetect with pullup
---------------------------------------------------------------------

 Revisions
 Date           Author                                  Revision
 11. May 2003           Rolf Freitag                            0.02
(2004: corrected MC pin numbers (switched only 45, 46))
*/
/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 44):
 * Rolf Freitag (webmaster at true-random.com) wrote this file.
 * As long as you retain this notice you can do whatever
  * the LGPL (Lesser GNU public License) allows with this stuff.
 * If you think this stuff is worth it, you can send me money via
 * paypal or if we met some day you can buy me a beer in return.
 * ----------------------------------------------------------------------------
 */
#ifndef _MMCLIB_H
#define _MMCLIB_H

#ifndef TXEPT                                     // transmitter-empty flag
#define TEXPT 0x01
#endif

// macro defines
#define HIGH(a) ((a>>8)&0xFF)                     // high byte from word
#define LOW(a) (a&0xFF)                           // low byte from word

#define CS_LOW()  P5OUT &= ~0x10                  // Card Select
#define CS_HIGH() P5OUT |= 0x10                   // Card Deselect
#define SPI_RXC (IFG2 & URXIFG1)
#define SPI_TXC (IFG2 & UTXIFG1)

#define SPI_RX_COMPLETE (IFG2 & URXIFG1)
#define SPI_TX_READY (IFG2 & UTXIFG1)

#define DUMMY 0xff

// Tokens (nessisary because at nop/idle (and CS active) only 0xff is on the data/command line)
#define MMC_START_DATA_BLOCK_TOKEN    0xfe        // Data token start byte, Start Single Block Read
#define MMC_START_DATA_MULTIPLE_BLOCK_READ  0xfe  // Data token start byte, Start Multiple Block Read
#define MMC_START_DATA_BLOCK_WRITE    0xfe        // Data token start byte, Start Single Block Write
#define MMC_START_DATA_MULTIPLE_BLOCK_WRITE 0xfc  // Data token start byte, Start Multiple Block Write
#define MMC_STOP_DATA_MULTIPLE_BLOCK_WRITE  0xfd  // Data toke stop byte, Stop Multiple Block Write

// an affirmative R1 response (no errors)
#define MMC_R1_RESPONSE       0x00

// this variable will be used to track the current block length
// this allows the block length to be set only when needed
// unsigned long _BlockLength = 0;

// error/success codes
#define MMC_SUCCESS           0x00


#define MMC_BLOCK_SET_ERROR   0x01
#define MMC_RESPONSE_ERROR    0x02
#define MMC_DATA_TOKEN_ERROR  0x03
#define MMC_INIT_ERROR        0x04
#define MMC_CRC_ERROR         0x10
#define MMC_WRITE_ERROR       0x11
#define MMC_OTHER_ERROR       0x12
#define MMC_TIMEOUT_ERROR     0xFF

// commands: first bit 0 (start bit), second 1 (transmission bit); CMD-number + 0ffsett 0x40
#define MMC_GO_IDLE_STATE   0x40                  //CMD0
#define MMC_SEND_OP_COND  0x41                    //CMD1
#define MMC_READ_CSD    0x49                      //CMD9
#define MMC_SEND_CID    0x4a                      //CMD10
#define MMC_STOP_TRANSMISSION   0x4c              //CMD12
#define MMC_SEND_STATUS   0x4d                    //CMD13
#define MMC_SET_BLOCKLEN  0x50                    //CMD16 Set block length for next read/write
#define MMC_READ_SINGLE_BLOCK   0x51              //CMD17 Read block from memory
#define MMC_READ_MULTIPLE_BLOCK 0x52              //CMD18
#define MMC_CMD_WRITEBLOCK  0x54                  //CMD20 Write block to memory
#define MMC_WRITE_BLOCK   0x58                    //CMD25
#define MMC_WRITE_MULTIPLE_BLOCK 0x59             //CMD??
#define MMC_WRITE_CSD     0x5b                    //CMD27 PROGRAM_CSD
#define MMC_SET_WRITE_PROT  0x5c                  //CMD28
#define MMC_CLR_WRITE_PROT  0x5d                  //CMD29
#define MMC_SEND_WRITE_PROT   0x5e                //CMD30
#define MMC_TAG_SECTOR_START  0x60                //CMD32
#define MMC_TAG_SECTOR_END  0x61                  //CMD33
#define MMC_UNTAG_SECTOR  0x62                    //CMD34
#define MMC_TAG_EREASE_GROUP_START 0x63           //CMD35
#define MMC_TAG_EREASE_GROUP_END 0x64             //CMD36
#define MMC_UNTAG_EREASE_GROUP  0x65              //CMD37
#define MMC_EREASE    0x66                        //CMD38
#define MMC_READ_OCR    0x67                      //CMD39
#define MMC_CRC_ON_OFF    0x68                    //CMD40

//TI added sub function for top two spi_xxx

// mmc init
char initMMC (void);
// send command to MMC
void mmcSendCmd (const char cmd, unsigned long data, const char crc);
// set MMC block length of count=2^n Byte
char mmcSetBlockLength (const unsigned long);
// read a size Byte big block beginning at the address.
char mmcReadBlock(const unsigned long address, const unsigned long count);
// write a 512 Byte big block beginning at the (aligned) adress
char mmcWriteBlock (const unsigned long address);
// Register arg1 der Laenge arg2 auslesen (into the buffer)
char mmcReadRegister(const char, const unsigned char);
#endif                                            /* _MMCLIB_H */

mmc.c
// mmc.c : MultiMediaCard functions: init, read, write ...
// Works also with SC cars. Modes: SPI mode.
//
// Rolf Freitag 5/2003
//
/* See also:
http://www.sandisk.com/pdf/oem/ProdManualSDCardv1.9.pdf Sandisk SD card product manual
http://www.sandisk.com/pdf/oem/ProdManualminiSDv1.1.pdf MiniSD card
http://www.sandisk.com/download/Product%20Manuals/Product%20ManualSDCardv1.7.pdf Older SD card
http://www.compile-it.com/support/crndatasheets/MMC%20ADAPTER.pdf
*/

/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 44):
 * Rolf Freitag (webmaster at true-random.com) wrote this file.
 * As long as you retain this notice you can do whatever
 * the LGPL (Lesser GNU public License) allows with this stuff.
 * If you think this stuff is worth it, you can send me money via
 * paypal or if we met some day you can buy me a beer in return.
 * ----------------------------------------------------------------------------
 */

// MMC Lib
#ifndef _MMCLIB_C
#define _MMCLIB_C
//---------------------------------------------------------------------
#include "mmc.h"
#include "led.h"

#include  "MSP430x14x.H"



#include  "math.h"
#include  "string.h"

char mmcGetResponse(void);
char mmcGetXXResponse(const char resp);
char mmcCheckBusy(void);

void initSPI (void);
unsigned char spiSendByte(const unsigned char data);

char mmc_buffer[512] =                            // Buffer for mmc i/o for data and registers
{
  0
};

extern char card_state;                           // 0 for no card found, 1 for card found (init successfull)

//---------------------------------------------------------------------

// setup usart1 in spi mode
void initSPI (void)
{
  ME2 |= USPIE1;                                  // Enable USART1 SPI mode
  UTCTL1 = CKPH | SSEL1 | SSEL0 | STC;            // SMCLK, 3-pin mode, clock idle low, data valid on rising edge, UCLK delayed
  UBR01 = 0x02;                                   // 0x02: UCLK/2 (4 MHz), works also with 3 and 4
  UBR11 = 0x00;                                   // -"-
  UMCTL1 = 0x00;                                  // no modulation
  UCTL1 = CHAR | SYNC | MM;                       // 8-bit SPI Master **SWRST**
  P5SEL |= 0x0E;                                  // P5.1-3 SPI option select
  P5DIR |= 0x01;                                  // P5.0 output direction
  P5OUT = 0xff;
  while (!(IFG2 & UTXIFG1));                      // USART1 TX buffer ready (empty)?
  // debug_printf("init......SPI");
}


// Initialisieren
char initMMC (void)
{

  //raise SS and MOSI for 80 clock cycles
  //SendByte(0xff) 10 times with SS high
  //RAISE SS
  int i;
  char response=0x01;

  // debug_printf("Start iniMMC......");
  initSPI();
  //initialization sequence on PowerUp
  CS_HIGH();
  for(i=0;i<=9;i++)
    spiSendByte(0xff);
  CS_LOW();
  //Send Command 0 to put MMC in SPI mode
  mmcSendCmd(0x00,0,0x95);
  //Now wait for READY RESPONSE
  if(mmcGetResponse()!=0x01);
  //       debug_printf("no responce");

  while(response==0x01)
  {
    //  debug_printf("Sending Command 1");
    CS_HIGH();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -