1 #include <AT91SAM7S64.h>
7 unsigned char spi_busy = 0;
9 #define SPI_SPEED 1000000
13 #define SPI_TRANSFER (AT91C_PA12_MISO | AT91C_PA13_MOSI | AT91C_PA14_SPCK)
15 #define SPI_CS (AT91C_PA11_NPCS0 | AT91C_PA31_NPCS1)
20 static uint8_t initialised = 0;
22 *AT91C_SPI_CR = AT91C_SPI_SPIDIS | AT91C_SPI_SWRST;
23 *AT91C_PMC_PCER = (1 << AT91C_ID_SPI);
24 *AT91C_PIOA_ASR = SPI_TRANSFER | SPI_CS;
25 *AT91C_PIOA_PDR = SPI_TRANSFER | SPI_CS;
26 *AT91C_PIOA_PPUER = AT91C_PA12_MISO | SPI_CS;
27 *AT91C_SPI_MR = (AT91C_SPI_MSTR | AT91C_SPI_PS_FIXED
28 | AT91C_SPI_MODFDIS | AT91C_SPI_PCS);
32 AT91C_SPI_CSR[0] = (MCK/SPI_SPEED)<<8;
34 *AT91C_SPI_CR = AT91C_SPI_SPIEN;
40 spi_init_chip_select(
unsigned int chip,
unsigned int speed,
42 unsigned int dlybs,
unsigned int phase,
43 unsigned int polarity)
48 ((dlybct<<24) | (dlybs<<16) | (((MCK+speed/2)/speed)<<8)
49 | (phase?AT91C_SPI_NCPHA:0) | (polarity?AT91C_SPI_CPOL:0)
50 | AT91C_SPI_BITS_8 | AT91C_SPI_CSAAT);
54 #define DBG_SEND dbg_blocking_putchar('>');
55 #define DBG_RECV dbg_blocking_putchar('<');
62 spi_transfer(
unsigned int chip,
const struct spi_block *block,
unsigned int blocks)
65 while(!(*AT91C_SPI_SR & AT91C_SPI_TXEMPTY));
72 *AT91C_SPI_MR = ((*AT91C_SPI_MR & ~AT91C_SPI_PCS)
73 | ((~(1<<chip) & 0x0f) << 16));
76 struct spi_block current = *block++;
78 if (current.receive) {
80 while(current.len-- > 0) {
81 while(!(*AT91C_SPI_SR & AT91C_SPI_TDRE));
82 *AT91C_SPI_TDR = *current.send++;
84 while(!(*AT91C_SPI_SR & AT91C_SPI_RDRF));
85 *current.receive++ = *AT91C_SPI_RDR;
90 while(current.len-- > 0) {
91 while(!(*AT91C_SPI_SR & AT91C_SPI_TDRE));
92 *AT91C_SPI_TDR = *current.send++;
94 while(!(*AT91C_SPI_SR & AT91C_SPI_RDRF));
100 if (current.receive) {
102 while(current.len-- > 0) {
103 while(!(*AT91C_SPI_SR & AT91C_SPI_TDRE));
106 while(!(*AT91C_SPI_SR & AT91C_SPI_RDRF));
107 *current.receive++ = *AT91C_SPI_RDR;
112 while(current.len-- > 0) {
113 while(!(*AT91C_SPI_SR & AT91C_SPI_TDRE));
116 while(!(*AT91C_SPI_SR & AT91C_SPI_RDRF));
117 (void)*AT91C_SPI_RDR;
123 *AT91C_SPI_CR = AT91C_SPI_LASTXFER;