Go Down

Topic: Poblem with USART in MSPI mode (Read 728 times) previous topic - next topic


Hey guys,

I've got a board with a MicroSD card on USART0, and while I've managed to convert some code that used to use the SPI to talk to the SD card to use the USART, in order to make that happen I had to kludge things a little.

Here's the code:

Code: [Select]

/** Send a byte to the card */

//inline void spiSend(uint8_t b) {SPDR = b; while(!(SPSR & (1 << SPIF)));} // Original code.

// Transmit byte via USART0 configured in MSPI mode:

//inline void spiSend(uint8_t b) {UCSR0A = (1 << TXC0); UDR0 = b; while(!(UCSR0A & (1 << TXC0)));} // Last working version
inline void spiSend(uint8_t b) {while(!(UCSR0A & (1 << UDRE0))); UDR0 = b;} // This seems to be the standard method.  Does not wait for byte to finish transmitting to exit.

/** Receive a byte from the card */

// Receive a byte using SPI:
//inline uint8_t spiRec(void) {spiSend(0XFF); return SPDR;} // Original code.

// Recieve byte via USART0 configured in MSPI mode:
inline uint8_t spiRec(void) {spiSend(0XFF); while(!(UCSR0A & (1 << RXC0))); return UDR0;} // Master must transmit a byte to receive one.

The problem I'm having is with the send function.  If I use the "standard method" which checks to see if the buffer is ready for another byte to be stuffed into it, my code doesn't work.  The SD card doesn't initialize.  If however I use the kludge method, where I clear the transmit complete bit, then send the data, then wait for the transmission to finish, which is more similar to what happens in an SPI transfer, then the code works just fine.

But I can't figure out why.  If I transmit a byte, using the standard function, then transmit a few more, it should wait when needed.  And if I attempt to read a byte after transmitting one, again, the code should wait till it's okay to send, and then wait until it gets a byte back.

So it should work.  But it doesn't.

Any ideas?

Go Up