Atmel website | ARM Community | AVR freaks | Technical Support
Banner
Welcome to AT91SAM Community Forum
http://www.at91.com/samphpbb/

Need to understand SPI programming
http://www.at91.com/samphpbb/viewtopic.php?f=15&t=19053
Page 1 of 1

Author:  terrorgen [ Fri Mar 12, 2010 12:39 am ]
Post subject:  Need to understand SPI programming

I was trying to interface an SPI peripheral using my AT91SAM7S256-EK. However I couldn't understand the example codes because all they have is interfacing with a SD/MMC reader.

I want to ask for assistance on interfacing my device, especially on the initialization part.

I am using Keil uVision as my IDE.

Thanks.

Author:  mjbcswitzerland [ Fri Mar 12, 2010 3:46 pm ]
Post subject:  Re: Need to understand SPI programming

Hi

SPI use on the SAM7 is quite straightforward. Below is a general configuration and then an example transmission and reception:

Code:
POWER_UP(SPI1 | PIOA);                         // enable clocks to SPI1 in PMC and Port A due to CS line
PIO_BSR_A = (SPI_MOSI_1B | SPI_MISO_1B | SPI_CLK_1B);
PIO_PDR_A = (SPI_MOSI_1B | SPI_MISO_1B | SPI_CLK_1B); // enable SPI pins
PIO_PER_A = (SPI_CS0_1B);                      // configure CS0 for SW control
PIO_SODR_A = (SPI_CS0_1B);
PIO_OER_A = (SPI_CS0_1B);

SPI_MR_1 = (SPI_MSTR | MODFDIS | SPI_CS_0);    // set SPI mode
SPI_CSR0_1 = (SPI_CPOL | SPI_8_BITS | (((MASTER_CLOCK + 6000000/2)/6000000) << 8)); // set speed 6MHz
SPI_CR_1 = SPIEN;                              // enable

PIO_CODR_A = SPI_CS0_1B;                       // assert CS line

SPI_TDR_1 = ucCommand;                         // send a byte
while (!(SPI_SR_1 & SPI_RDRF)) {};             // wait until tx byte has been sent and rx byte has been completely received
ucRxData = SPI_RDR_1;                          // read the returned byte

PIO_SODR_A = SPI_CS0_1B;                       // negate CS line


This is from the uTasker project. This may help you to use the SPI and the chip generally. (SPI use in the project includes SD card, SPI FLASH devices and LCD - eg. Nokia LCD on the Olimex boards) Full support at http://www.utasker.com/forum/index.php

Regards

Mark

www.uTasker.com

Page 1 of 1 All times are UTC + 1 hour [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/