Society of Robots - Robot Forum
Software => Software => Topic started by: sprince09 on March 04, 2009, 09:41:10 PM
-
Hey guys,
I've been trying to get my Axon to interface with an SDCard (I need to do some data-logging) over the SPI interface. I read through the spi.c and spi.h files included in the latest source code for the Axon and I have been trying to use those functions to interface with my card. Going off of the pin labels in the Axon datasheet and a google search for the pinout of an SDCard, I'm fairly certain that I have my connections set up properly. Anyway, what I've noticed is that whenever I plug in my SDCard, the board resets continually until I remove, which indicates to me that I have a wiring problem somewhere, but I've rebuilt the circuit four times now, and I haven't been able to find any errors with that.
I guess what I'm really interested in knowing is if there are any special considerations I need to make when using SPI on the axon. I noticed that the hardware programmer uses the same pins (except for SS and reset) as SPI does... is there something I should be doing to account for that?
Relevant code that I am running:
void SPITest(void){
rprintfInit(uart1SendByte);
rprintf("\n\r");
spiInit();
rprintf("SPI Interface Successfully Initialized\r\n");
unsigned char a = 0;
while(1){
spiSendByte(a++);//this function blocks until register is cleared (won't clear if nothing is connected?)
rprintf("byte sent: %c \r",a);
}
}
Also, when I run the code without the SDCard attached, I get the initialization response over my UART "SPI Interface etc..." and then it blocks at spiSendByte(a++). My understanding of that function is that it will block until the data has been shifted out of the register (can't remember what register off the top of my head). I expected that it will block if the pin is unconnected (maybe I'm wrong).
Anyway, I'd appreciate any insight into this :-\
EDIT: Read my next post!
-
Alright, after much frustration and self doubt I've figured out what was wrong. I ended up writing my own initialization function:
spi_init(void){
unsigned char a;
//setup pins
#define MISO PB3
#define MOSI PB2
#define SCK PB1
#define SS PB0
DDRB |= (1<<MOSI)|(1<<SCK)|(1<<SS);
DDRB &= ~(1<<MISO);
PORTB |= (1<<SS);//set SS high
PORTB &= ~(1<<MISO);//turn off internal pullup for MISO
PORTB &= ~(1<<MOSI);//pull MOSI low
PORTB &= ~(1<<SCK);//pull SCK low
//setup registers
SPSR = 0b00000000;//see datasheet
SPCR = 0b01010000;//see datasheet
//clear out any junk in SPI buffer
a = SPSR;
}
And the corresponding function for sending+receiving data:
unsigned char spi_transfer(unsigned char data){
//transmits data over SPI lines and returns the response
SPDR = data;
while(!(SPSR & (1<<SPIF)));//block untill data transmission/reception completes
return SPDR;
}
It turns out that setting up the software side of the interface wasn't my problem... it was the hardware. The axon_schematic.pdf labels the ISP programming headers (the SPI pins are also the programmer pins) as follows:
Left Right
VTG MISO
RST MOSI
GND SCK
However, the actual layout of the programming pins is:
Left Right
MISO VTG
SCK MOSI
RST GND
;) Works perfect for me now. Hope this helps anyone else who is having issues.
-
Can you relabel this thread so that anyone searching for this problem in the future has an easier time finding it due to a more descriptive thread subject
-
Updated
-
Just wanted to say thanks for this thread. Saved me a whole lot of time figuring out the pinout on the datasheet is wrong!