Line No. | Rev | Author | Line |
---|---|---|---|
1 | 6 | kaklik | /*! \file spi.c \brief SPI interface driver. */ |
2 | //***************************************************************************** |
||
3 | // |
||
4 | // File Name : 'spi.c' |
||
5 | // Title : SPI interface driver |
||
6 | // Author : Pascal Stang - Copyright (C) 2000-2002 |
||
7 | // Created : 11/22/2000 |
||
8 | // Revised : 06/06/2002 |
||
9 | // Version : 0.6 |
||
10 | // Target MCU : Atmel AVR series |
||
11 | // Editor Tabs : 4 |
||
12 | // |
||
13 | // NOTE: This code is currently below version 1.0, and therefore is considered |
||
14 | // to be lacking in some functionality or documentation, or may not be fully |
||
15 | // tested. Nonetheless, you can expect most functions to work. |
||
16 | // |
||
17 | // This code is distributed under the GNU Public License |
||
18 | // which can be found at http://www.gnu.org/licenses/gpl.txt |
||
19 | // |
||
20 | //***************************************************************************** |
||
21 | |||
22 | #include <avr/io.h> |
||
23 | #include <avr/interrupt.h> |
||
24 | |||
25 | #include "spi.h" |
||
26 | |||
27 | // Define the SPI_USEINT key if you want SPI bus operation to be |
||
28 | // interrupt-driven. The primary reason for not using SPI in |
||
29 | // interrupt-driven mode is if the SPI send/transfer commands |
||
30 | // will be used from within some other interrupt service routine |
||
31 | // or if interrupts might be globally turned off due to of other |
||
32 | // aspects of your program |
||
33 | // |
||
34 | // Comment-out or uncomment this line as necessary |
||
35 | //#define SPI_USEINT |
||
36 | |||
37 | // global variables |
||
38 | volatile u08 spiTransferComplete; |
||
39 | |||
40 | // SPI interrupt service handler |
||
41 | #ifdef SPI_USEINT |
||
42 | SIGNAL(SIG_SPI) |
||
43 | { |
||
44 | spiTransferComplete = TRUE; |
||
45 | } |
||
46 | #endif |
||
47 | |||
48 | // access routines |
||
49 | void spiInit() |
||
50 | { |
||
51 | #ifdef __AVR_ATmega128__ |
||
52 | // setup SPI I/O pins |
||
53 | sbi(PORTB, 1); // set SCK hi |
||
54 | sbi(DDRB, 1); // set SCK as output |
||
55 | cbi(DDRB, 3); // set MISO as input |
||
56 | sbi(DDRB, 2); // set MOSI as output |
||
57 | sbi(DDRB, 0); // SS must be output for Master mode to work |
||
58 | #elif __AVR_ATmega8__ |
||
59 | // setup SPI I/O pins |
||
60 | sbi(PORTB, 5); // set SCK hi |
||
61 | sbi(DDRB, 5); // set SCK as output |
||
62 | cbi(DDRB, 4); // set MISO as input |
||
63 | sbi(DDRB, 3); // set MOSI as output |
||
64 | sbi(DDRB, 2); // SS must be output for Master mode to work |
||
65 | #else |
||
66 | // setup SPI I/O pins |
||
67 | sbi(PORTB, 7); // set SCK hi |
||
68 | sbi(DDRB, 7); // set SCK as output |
||
69 | cbi(DDRB, 6); // set MISO as input |
||
70 | sbi(DDRB, 5); // set MOSI as output |
||
71 | sbi(DDRB, 4); // SS must be output for Master mode to work |
||
72 | #endif |
||
73 | |||
74 | // setup SPI interface : |
||
75 | // master mode |
||
76 | sbi(SPCR, MSTR); |
||
77 | // clock = f/4 |
||
78 | // cbi(SPCR, SPR0); |
||
79 | // cbi(SPCR, SPR1); |
||
80 | // clock = f/16 |
||
81 | cbi(SPCR, SPR0); |
||
82 | sbi(SPCR, SPR1); |
||
83 | // select clock phase positive-going in middle of data |
||
84 | cbi(SPCR, CPOL); |
||
85 | // Data order MSB first |
||
86 | cbi(SPCR,DORD); |
||
87 | // enable SPI |
||
88 | sbi(SPCR, SPE); |
||
89 | |||
90 | |||
91 | // some other possible configs |
||
92 | //outp((1<<MSTR)|(1<<SPE)|(1<<SPR0), SPCR ); |
||
93 | //outp((1<<CPHA)|(1<<CPOL)|(1<<MSTR)|(1<<SPE)|(1<<SPR0)|(1<<SPR1), SPCR ); |
||
94 | //outp((1<<CPHA)|(1<<MSTR)|(1<<SPE)|(1<<SPR0), SPCR ); |
||
95 | |||
96 | // clear status |
||
97 | inb(SPSR); |
||
98 | spiTransferComplete = TRUE; |
||
99 | |||
100 | // enable SPI interrupt |
||
101 | #ifdef SPI_USEINT |
||
102 | sbi(SPCR, SPIE); |
||
103 | #endif |
||
104 | } |
||
105 | /* |
||
106 | void spiSetBitrate(u08 spr) |
||
107 | { |
||
108 | outb(SPCR, (inb(SPCR) & ((1<<SPR0)|(1<<SPR1))) | (spr&((1<<SPR0)|(1<<SPR1))))); |
||
109 | } |
||
110 | */ |
||
111 | void spiSendByte(u08 data) |
||
112 | { |
||
113 | // send a byte over SPI and ignore reply |
||
114 | #ifdef SPI_USEINT |
||
115 | while(!spiTransferComplete); |
||
116 | spiTransferComplete = FALSE; |
||
117 | #else |
||
118 | while(!(inb(SPSR) & (1<<SPIF))); |
||
119 | #endif |
||
120 | |||
121 | outb(SPDR, data); |
||
122 | } |
||
123 | |||
124 | u08 spiTransferByte(u08 data) |
||
125 | { |
||
126 | #ifdef SPI_USEINT |
||
127 | // send the given data |
||
128 | spiTransferComplete = FALSE; |
||
129 | outb(SPDR, data); |
||
130 | // wait for transfer to complete |
||
131 | while(!spiTransferComplete); |
||
132 | #else |
||
133 | // send the given data |
||
134 | outb(SPDR, data); |
||
135 | // wait for transfer to complete |
||
136 | while(!(inb(SPSR) & (1<<SPIF))); |
||
137 | #endif |
||
138 | // return the received data |
||
139 | return inb(SPDR); |
||
140 | } |
||
141 | |||
142 | u16 spiTransferWord(u16 data) |
||
143 | { |
||
144 | u16 rxData = 0; |
||
145 | |||
146 | // send MS byte of given data |
||
147 | rxData = (spiTransferByte((data>>8) & 0x00FF))<<8; |
||
148 | // send LS byte of given data |
||
149 | rxData |= (spiTransferByte(data & 0x00FF)); |
||
150 | |||
151 | // return the received data |
||
152 | return rxData; |
||
153 | } |
Powered by WebSVN v2.8.3