I am trying to interface a HCS08QE32 to the uDRIVE-uSD-G1 (an embedded DOS uSD card module), which has a command set for reading/writing etc to it.
I am trying to write a file to it, this requires the micro to:
wait some time for device to settle,
send byte to lock onto baud rate,
micro should receive "ack" byte,
send bytes to set up file,
receive ack,
send file data bytes.
receive ack.
...problem is, my program sends the auto-baud command (tested with serial terminal, Docklight), then just sends random bytes while waiting for ack. I have tried removing the wait-for-ack code, replacing with delays, and just sending the commands, which seems to send all required bytes, followed by random bytes, but does not create file.
I can write a file by sending the commands through Docklight (and getting the expected acks), so uSD module is working fine.
I think the problem must lie in either my wait-for-ack code, or possibly the initial delay time (which I've tried changing a bit). Should I disable the micro Tx pin while waiting for ack, rather than let it send random data? Any Ideas?
#include <hidef.h> /* for EnableInterrupts macro */#include "derivative.h" /* include peripheral declarations *///Delay functionvoid delay( long val){ for ( ; val; val--);}void main(void) { SCI2BD = 26; //set baud rate 96000 with busclock=4MHz //delay while device settles (should be ~500ms) delay(5000); SCI2C2 = 0b00000100; //enable R SCI2C2 = 0b00001000; //enable T while (!SCI2S1_TDRE); // wait for the transmit buffer to be empty SCI2D = 0x55; //auto baud //wait for ack do { while(!SCI2S1_RDRF); //wait for received byte } while (SCI2D != 0x06); //check for ack // delay(100); //Set up file //write while (!SCI2S1_TDRE); // wait for the transmit buffer to be empty SCI2D = 0x40; while (!SCI2S1_TDRE); // wait for the transmit buffer to be empty SCI2D = 0x74; //no handshaking while (!SCI2S1_TDRE); // wait for the transmit buffer to be empty SCI2D = 0x00; //file name while (!SCI2S1_TDRE); // wait for the transmit buffer to be empty SCI2D = 0x46; //"F" while (!SCI2S1_TDRE); // wait for the transmit buffer to be empty SCI2D = 0x6E; //"N" while (!SCI2S1_TDRE); // wait for the transmit buffer to be empty SCI2D = 0x74; //"T" while (!SCI2S1_TDRE); // wait for the transmit buffer to be empty SCI2D = 0x31; //"1" //null while (!SCI2S1_TDRE); // wait for the transmit buffer to be empty SCI2D = 0x00; //file size while (!SCI2S1_TDRE); // wait for the transmit buffer to be empty SCI2D = 0x00; while (!SCI2S1_TDRE); // wait for the transmit buffer to be empty SCI2D = 0x00; while (!SCI2S1_TDRE); // wait for the transmit buffer to be empty SCI2D = 0x00; while (!SCI2S1_TDRE); // wait for the transmit buffer to be empty SCI2D = 0x05; //5 bytes delay(100); //wait for ack do { while(!SCI2S1_RDRF); //wait for received byte } while (SCI2D != 0x06); //check for ack // //send data while (!SCI2S1_TDRE); // wait for the transmit buffer to be empty SCI2D = 0x68; //"h" while (!SCI2S1_TDRE); // wait for the transmit buffer to be empty SCI2D = 0x65; //"e" while (!SCI2S1_TDRE); // wait for the transmit buffer to be empty SCI2D = 0x6C; //"l" while (!SCI2S1_TDRE); // wait for the transmit buffer to be empty SCI2D = 0x6C; //"l" while (!SCI2S1_TDRE); // wait for the transmit buffer to be empty SCI2D = 0x6F; //"o" delay(100); //wait for ack do { while(!SCI2S1_RDRF); //wait for received byte } while (SCI2D != 0x06); //check for ack // while(1);} Iain