Society of Robots
     | Robot Forum | Robot Tutorials | Robot FAQ |
September 02, 2010, 09:45:41 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: March 18th - Buy an Axon or Axon II, build a great robot, and support SoR.

Robot Forum
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: HOW TO: SPI on Axon  (Read 1318 times)
0 Members and 1 Guest are viewing this topic.
sprince09Topic starter
Jr. Member
**

Helpful? 0
Offline Offline

Posts: 16


View Profile
« 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  Undecided

EDIT: Read my next post!
« Last Edit: March 13, 2009, 01:53:40 PM by sprince09 » Logged
sprince09Topic starter
Jr. Member
**

Helpful? 0
Offline Offline

Posts: 16


View Profile
« Reply #1 on: March 13, 2009, 01: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:

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


 Wink Works perfect for me now. Hope this helps anyone else who is having issues.
Logged
pomprocker
Supreme Robot
*****

Helpful? 15
Offline Offline

Posts: 1,430


Sorry miss, I was giving myself an oil-job.


View Profile WWW
« Reply #2 on: March 13, 2009, 01: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
Logged

sprince09Topic starter
Jr. Member
**

Helpful? 0
Offline Offline

Posts: 16


View Profile
« Reply #3 on: March 13, 2009, 01:53:54 PM »

Updated
Logged
Pages: [1]
  Print  
 
Jump to:  


Related Topics
Subject Started by Replies Views Last post
PWM on Axon
Software
mbateman 10 4060 Last post March 23, 2009, 06:07:14 PM
by Hanoush
Axon ----> SSC-32
Software
Techman09 2 1208 Last post November 06, 2008, 01:22:49 AM
by Admin
Cant get any output with printf() on the axon with default program axon.c
Software
axonuser 1 770 Last post May 22, 2009, 11:42:24 PM
by billhowl
FBOOT for Axon/Axon II: version for Linux and OS X
Software
Admin 0 336 Last post January 09, 2010, 11:52:06 PM
by Admin
Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!


Advertise on this Forum