Hi,
I am at wits end trying to figure out what I am doing wrong interfacing am MSP430F5438 with a 1 GB SD Card.
I've checked that the clock is at 400 kHz, the CS line is at 0 Volts when asserted.
For UCB1CTL0 I've tried all combinations, but I'm mostly sure that UCCKPL is the correct configuration from this post:
I've also read the foust app note and the SanDisk Secure Digital Card Product Manual Version 1.9. Still can't figure it out. It seems so simple to send 5 bytes over SPI and get the R1 responce, but the SD card remains silent. I've also replace my SD card with a new one.
The following code doesn't show the boot up code, but my peripherals are configured correctly. I can see the clock and the MOSI signals on my scope, and they look correct to me. I'm not sure what I did, but one time the SD card even started sending over data, but it didn't seem to be valid R1 responses, bu tnow I can't repeat that.
Code is below, any troubleshooting advice would be greatly appreciated.
WDTCTL = WDTPW + WDTHOLD; // disable watch dog for tesintg
UCB1CTL1 |= UCSWRST; // **Put state machine in reset**
UCB1CTL1 |= UCSSEL__SMCLK; // select SMCLK
UCB1CTL0 =UCSYNC| UCMST| UCMSB| UCCKPL;// ;
// SET TO 400 KHZ
UCB1BR0 = 0x28;
UCB1BR1 = 0x0;
UCB1CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
char tmp;
while (1)
{
spi_cs_assert();
//__delay_cycles(250000);
spi_send_byte((0x40));
spi_send_byte((0x00));
spi_send_byte((0x00));
spi_send_byte((0x00));
spi_send_byte((0x00));
spi_send_byte((0x95));
do
{
tmp = spi_rcv_byte();
i++;
}
while (((tmp & 0x80) != 0) && i < 100);
spi_cs_deassert();
__delay_cycles(250000);
}
/* Send a single byte over the SPI port */
void spi_send_byte(unsigned char input)
{
//IFG1 &= ~URXIFG0;
/* Send the byte */
unsigned char tmp;
/* Send the byte */
while ((UCB1STAT & UCBUSY)); // SPI ready?
while (!(UCB1IFG&UCTXIFG)); // USCI_B1 TX buffer ready?
UCB1TXBUF = input;
/* Wait for the byte to be sent */
//UCTXIFG is set when UCxxTXBUF empty.
while ((UCB1IFG & UCTXIFG) == 0) { }
}
/* Receive a byte. Output an 0xFF (the bus idles high) to receive the byte */
unsigned char spi_rcv_byte()
{
unsigned char tmp;
/* Send the byte */
tmp = UCB1RXBUF; // clear buffer
while ((UCB1STAT & UCBUSY)); // SPI ready?
spi_send_byte(0xFF);
/* Wait for the byte to be received */
while ((UCB1IFG & UCRXIFG) == 0) { }
while ((UCB1STAT & UCBUSY)); // SPI ready?
tmp = UCB1RXBUF;
return (tmp);
}