This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Hi,
I have been trying to interface Micro SD card over SPI lines. I'm only concentrating first on the initialization command (CMD0 - 0x40). I'm not getting the response as 0x01(SD card in Idle state).
Here is the code for it.
Can anybody analyse it and provide some feedback of what wrong am I doing?
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "msp430x54x.h"
#define DUMMY_CHAR 0xFF
char Response = 0x77;
void SpiInitialize(void);
int InitializeSDCard(void);
void AssertCS(void);
void DeAssertCS(void);
void spi_send_byte(unsigned char input);
void DelayFunc(int value);
void SendCommand (const int cmd, unsigned long data, const int crc);
unsigned char spiSendByte(const unsigned char data);
void main()
{
WDTCTL = WDTPW+WDTHOLD; // Stop watchdog timer
P4DIR |= 0x01; //Chip select
SpiInitialize();
__bis_SR_register(GIE); // Enable Interrupts
while(1)
{
InitializeSDCard();
}
}
void AssertCS(void)
{
P4OUT &= ~0x01; //Active Low Chip Select
}
void DeAssertCS(void)
{
P4OUT |= 0x01; //Active High Chip Select
}
void SpiInitialize(void)
{
P10SEL |= 0x30; // P10.5,4,0 option select
UCA3CTL1 |= UCSWRST; // **Put state machine in reset**
UCA3CTL0 |= UCMST+UCSYNC+UCMSB+UCCKPL; // 3-pin, 8-bit SPI master
UCA3CTL1 |= UCSSEL_3; // SMCLK
UCA3BR0 = 0x03;
UCA3BR1 = 0; // Baud rate is 350Khz
UCA3MCTL = 0; // No modulation
UCA3CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA3IE |= UCRXIE; // Enable USCI_A0 RX interrupt
}
int InitializeSDCard(void)
{
int i=0,k=0;
unsigned char Dummy = 0xFF;
AssertCS(); // Pull down Chip Select
for(i=0;i<10;i++)
{
spi_send_byte(Dummy); // 80 Clock Pulses
}
DeAssertCS(); // Pull High Chip select
for(i=0;i<2;i++)
{
spi_send_byte(Dummy); // 16 Clock Pulses
}
SendCommand(0x40,0,0x95);
for(k=0;k<10;k++)
{
spi_send_byte(Dummy) ; // Send Dummy Character to keep MOSI High
}
return 0;
}
void spi_send_byte(unsigned char input)
{
while (!(UCA3IFG&UCTXIFG));
UCA3TXBUF = input;
while (!(UCA3IFG&UCTXIFG));
}
void SendCommand (const int cmd, unsigned long data, const int crc)
{
AssertCS(); // Pull down Chip Select
spi_send_byte(0x40);
spi_send_byte(0x00);
spi_send_byte(0x00);
spi_send_byte(0x00);
spi_send_byte(0x00);
spi_send_byte(0x95);
}
#pragma vector=USCI_A3_VECTOR
__interrupt void USCI_A3_ISR(void)
{
switch(__even_in_range(UCA3IV,4))
{
case 0: break; // Vector 0 - no interrupt
case 2: // Vector 2 - RXIFG
Response = UCA3RXBUF; // Response Buffer
break;
case 4: break; // Vector 4 - TXIFG
default: break;
}
}
**Attention** This is a public forum