Atmel website | ARM Community | AVR freaks | Technical Support
Banner
 FAQ •  Search •  Register •  Login 

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: SAM9G35 SPI1 problems
PostPosted: Mon Apr 09, 2012 8:29 pm 
Offline

Joined: Thu Sep 16, 2004 3:10 pm
Posts: 31
I'm trying to setup an app in Linux that uses SPI port 1. I believe the registers are all set properly, but I never get any data in on the port. What am I missing?

Here is the code:


#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <fcntl.h>

int spi1_init(void);

int main(int argc, char *argv[])
{
spi1_init();
spi1_read(); // read data forever
}

#define ADDR_AIC 0xFFFFF000
#define ADDR_SPI1 0xF0004000

#define SPI_CR 0
#define SPI_MR 1
#define SPI_RDR 2
#define SPI_TDR 3
#define SPI_SR 4
#define SPI_IER 5
#define SPI_IDR 6
#define SPI_IMR 7
#define SPI_CSRx 12
#define SPI_WPMR 57
#define SPI_WPSR 58

#define PIOA_PER 256
#define PIOA_PDR 257
#define PIOA_PSR 258
#define PIOA_OER 260
#define PIOA_ODR 261
#define PIOA_PDSR 271
#define PIOA_ABCDSR1 284
#define PIOA_ABCDSR2 285

#define PMC_PCER 772
#define PMC_PCSR 774

int spi1_init(void)
{
unsigned long *addr;
unsigned long data;
int index;
int fd;

fd = open("/dev/mem", O_RDWR|O_SYNC);
if (fd <= 0)
return(-1);

// setup PA8, PA21, PA22, and PA23 to be SPI1
addr = (unsigned long *)mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, ADDR_AIC);

// setup PA8, 21, 22, and 23 for SPI
// PIOA_ABCDSR1 bits should be 1 for these pins
// PIOA_ABCDSR2 bits should be 0 for these pins
*(addr + PIOA_PDR) = 0x00E00100; // disable as PIO and enable as peripheral
*(addr + PIOA_ABCDSR1) |= 0x00E00100;
*(addr + PIOA_ABCDSR2) &= ~0x00E00100;

// make sure SPI1 is enabled in the PMC
*(addr + PMC_PCER) |= 0x00004000;

// use spi1
addr = (unsigned long *)mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, ADDR_SPI1);
*(addr + SPI_CR) = 0x00000080; // software reset
// after reset, the spi port is in slave mode
*(addr + SPI_CR) = 0x00000001; // enable the SPI port

return(0);
}


int spi1_read(void)
{
unsigned long *spi1Addr;
int fd;

fd = open("/dev/mem", O_RDWR|O_SYNC);
if (fd <= 0)
return(-1);

// use spi1
spi1Addr = (unsigned long *)mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, ADDR_SPI1);

while (1) {
if (*(spi1Addr + SPI_SR) & 0x00000100)
printf("%08lX %08lX\n", *(spi1Addr + SPI_SR), *(spi1Addr + SPI_RDR));
}

return(0);
}

_________________
David Peters


Top
 Profile  
 
 Post subject: Re: SAM9G35 SPI1 problems
PostPosted: Mon May 07, 2012 3:51 pm 
Offline

Joined: Thu Sep 16, 2004 3:10 pm
Posts: 31
I found my problem. Although the chip select registers default to the correct values, I still had to write a value to them before the device would start properly. I added the line:

*(addr + SPI_CSR0) = 0x00000000;

_________________
David Peters


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: