How to set up SPI with the SAM3X8E?

Discussion around product based on ARM Cortex M3 core.

Moderators: nferre, ncollot

amodedude1
Posts: 6
Joined: Fri Apr 24, 2015 8:08 am

How to set up SPI with the SAM3X8E?

Fri Apr 24, 2015 9:55 am

I am an extreme beginner with micro-controllers. None the less, I would like to be able to control a digital resistor over the SPI interface with the SAM3 chip. I am using, of all things, an Arduino Due for this purpose and I am running Atmel Studio 6. 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 code:

Code: Select all

// 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: Select all

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

int main (void)
{
	/* Initialize the SAM system. */
	sysclk_init();
	board_init();
	
	// Enable SPI
	spi_master_init(Spi *p_spi);
	spi_master_setup_device(Spi *p_spi, ,SPI_MODE_0, 9600, 0);
	spi_enable(Spi *p_spi);
}
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?

I finally found this site:

http://asf.atmel.com/docs/3.17.0/sam3x/ ... start.html

Which helps a little, BUT, what goes in the place of "SPI_EXAMPLE" and "&SPI_DEVICE_EXAMPLE" ??? :?
amodedude1
Posts: 6
Joined: Fri Apr 24, 2015 8:08 am

Re: How to set up SPI with the SAM3X8E?

Mon Apr 27, 2015 3:15 am

Hi, I realize this is a duplicate, I did not know how to delete the other one. This one is in the correct place.


(SAM3X8E fourm) I am still having this problem with the SPI. I have the following code:

Code: Select all

// Function Initializes the serial peripheral interface
static void spi_master_initialize(void)
{
	/* Configure an SPI peripheral. */
	spi_enable_clock(SPI0);
	spi_disable(SPI0);
	spi_reset(SPI0);
	spi_set_master_mode(SPI0);
	spi_disable_mode_fault_detect(SPI0);
	spi_set_clock_polarity(SPI0, SPI_CHIP_SEL, SPI_CLK_POLARITY);
	spi_set_clock_phase(SPI0, SPI_CHIP_SEL, SPI_CLK_PHASE);
        spi_set_bits_per_transfer(SPI0, SPI_CHIP_SEL,SPI_CSR_BITS_8_BIT);
	spi_enable(SPI0);
	
}
That Initializes the SPI then this should send the data:

Code: Select all

// Interrupt handler for volume up button
void volume_up_button_press_handler(uint32_t a, uint32_t b)
{
	pio_set_output(PIOD, PIO_PD7, HIGH, DISABLE, ENABLE);
	// Initialize SPI
	spi_master_initialize();
	
	uint8_t data = 0;
	
	// Send volume up command over SPI
	pio_set_output(PIOD, PIO_PD7, HIGH, DISABLE, ENABLE);
    spi_write(SPI0,data,SPI_CHIP_PCS,0);
    spi_write(SPI0,data,SPI_CHIP_PCS,0);
	pio_set_output(PIOD, PIO_PD7, LOW, DISABLE, ENABLE);
}
.. but it doesn't. Please, isn't there anyone here who can help!?
amodedude1
Posts: 6
Joined: Fri Apr 24, 2015 8:08 am

Re: How to set up SPI with the SAM3X8E?

Mon Apr 27, 2015 8:14 am

I realize that some of my calls may have been unnecessary, so I reduced things down to this:

Code: Select all

	uint8_t data = 0b00000000;

	struct spi_device digi_pot_spi = { .id = 0 };
	spi_master_init(SPI0);
	spi_master_setup_device(SPI0, &digi_pot_spi, SPI_MODE_0, 4000000, 0);	
	spi_enable(SPI0);
	spi_write_packet(SPI0, &data, sizeof(data));	
..but still NOTHING. I am trying to write to an LSB first device that needs Chip Select to be 1 while data is sent from the sam3 buffer. Specifically it is the DS1801 from Maxim (http://datasheets.maximintegrated.com/en/ds/DS1801.pdf). Anyone have ideas? :oops: Since it is CS HIGH to read data, does that mean I should use SPI_MODE_3 instead??
amodedude1
Posts: 6
Joined: Fri Apr 24, 2015 8:08 am

Re: How to set up SPI with the SAM3X8E?

Wed Apr 29, 2015 10:44 am

All very good points. At the moment I am working on a project that is incorporating the SAM3x into a custom embedded device and so this is why I was originally thinking that I should learn to program this thing in C.

After a great deal of research I THINK it might be possible to still use the SAM3x8e on an embedded device while still making it Arduino IDE compatible so long as I am careful to utilize identical pin configuration as the Arduino. I believe I can do away with all of the Atmel 16u chip business as that is only used, as I understand it, to erase and reset the MCU upon a flash from the Arduino IDE. If I wire things the way the "Native Programing port" is, than I may be able to get connectivity. Just in the same way as the Digistup Digix Arduino Due clone does.




I know I am probably in over my head, but it is a fun project and I want to give this a try! The worst that can happen is that it wont work! :D

(On an off topic note: I enjoyed your back country skiing reference.. :lol: )

Return to “SAM3 Cortex-M3 MCU”

Who is online

Users browsing this forum: No registered users and 4 guests