Author Topic: HOW TO: SPI on Axon  (Read 5754 times)

0 Members and 1 Guest are viewing this topic.

Offline sprince09Topic starter

  • Jr. Member
  • **
  • Posts: 16
  • Helpful? 0
HOW TO: SPI on Axon
« 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){
  rprintf("SPI Interface Successfully Initialized\r\n");

  unsigned char a = 0;
    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!
« Last Edit: March 13, 2009, 12:53:40 PM by sprince09 »

Offline sprince09Topic starter

  • Jr. Member
  • **
  • Posts: 16
  • Helpful? 0
Re: SPI woes (Axon)
« Reply #1 on: March 13, 2009, 12:47:35 PM »
Alright, after much frustration and self doubt I've figured out what was wrong. I ended up writing my own initialization function:

  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
 SCK      MOSI
 RST      GND

 ;) Works perfect for me now. Hope this helps anyone else who is having issues.

Offline pomprocker

  • Supreme Robot
  • *****
  • Posts: 1,431
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
    • Nerdcore - Programming, Electronics, Mechanics
Re: SPI woes (Axon)
« Reply #2 on: March 13, 2009, 12:52:10 PM »
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

Offline sprince09Topic starter

  • Jr. Member
  • **
  • Posts: 16
  • Helpful? 0
Re: HOW TO: SPI on Axon
« Reply #3 on: March 13, 2009, 12:53:54 PM »

Offline Hawl

  • Beginner
  • *
  • Posts: 1
  • Helpful? 0
Re: HOW TO: SPI on Axon
« Reply #4 on: January 13, 2011, 10:01:09 PM »
Just wanted to say thanks for this thread. Saved me a whole lot of time figuring out the pinout on the datasheet is wrong!


Get Your Ad Here