Skip to main content
Source Link
Hooplator15
  • 327
  • 1
  • 8
  • 23

How can I set up SPI using Atmel Studio and C code for ARM processor?

I am trying to program the SAM3x8E chip on the Arduino Due through Atmel Studio. I would like to be able to control a digital resistor over the SPI interface with the SAM3 chip. I am running Atmel Studio 6.2. I have NO PROBLEM getting things to work on the Arduino IDE, but I really want to figure out how to program this the "hard way" in Atmel. I can successfully program the chip in Atmel Studio, by using a batch file which loads bosssa.exe.

Here is my WORKING Arduino IDE code:

Code:

// inslude the SPI library:
#include <SPI.h>


// set pin 10 as the slave select for the digital pot:
const int slaveSelectPin = 13;

void setup() {
  // initialize SPI:
  SPI.begin();
  SPI.setDataMode(SPI_MODE0);
 // SPI.setClockDivider(slaveSelectPin, 8);
  SPI.setBitOrder(LSBFIRST);
  pinMode (slaveSelectPin, OUTPUT);
}

void loop() {

  // Raise the volume from off to loud

  // Increase brightness of LED
  for (int i = 63; i>=0; i--) {
    digitalPotWrite(i, i);
   
    if(i>20)
    delay(10);
    else
    delay(100);
  }
 
      digitalPotWrite(0, 0);
       // delay(500);
 
 
  // Decrease brightness of LED
  for (int i = 0; i <= 63; i++) {
    digitalPotWrite(i, i);     
   
    if(i>20)
    delay(10);
    else
    delay(100);
  }
}

void digitalPotWrite(int left, int right) {
  // take the SS pin low to select the chip:
  digitalWrite(slaveSelectPin, HIGH);
  //  send in the address and value via SPI:
  SPI.transfer(left);
  SPI.transfer(right);
  // take the SS pin high to de-select the chip:
  digitalWrite(slaveSelectPin, LOW);
}

Now (sadly), this is my Atmel code:

Code:

#include <asf.h>
#include "spi_master.h"


int main (void)
{
   /* Initialize the SAM system. */
   sysclk_init();
   board_init();

}

As you can see, I don't have the slightest clue how to go about setting up the SPI interface in Atmel studio. I've looked at example code such as the built in "SPI_EXAMPLE1" but I do NOT understand it. Can anyone help me to understand how you initialize the SPI? How do I know what pins to use in the initialization code?