Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1704 | - | 1 | #include <avr/io.h> |
2 | #include "ssc.h" |
||
3 | |||
4 | //-------------------------------------- Hardware specific definitions -------------------------------------- |
||
5 | #define PORTR_SPI PINB |
||
6 | #define PORTW_SPI PORTB //Port to which the sd-card is connected (SPI Port) |
||
7 | #define PORT_MISO PORTB6 //Port Pin that is connected to the DO of the MMC/SD-card |
||
8 | #define PORT_MOSI PORTB5 //Port Pin that is connected to DI of the MMC/SD-card |
||
9 | #define PORT_SCK PORTB7 //Port Pin that is connected the CLK of the MMC/SD-card |
||
10 | #define PORT_SS PORTB4 //Slave Select is not used in SPI Master Mode, but must be defined |
||
11 | #define PORT_CS PORTB4 //Port Pin that is connected to /CS of the MMC/SD-Karte |
||
12 | |||
13 | |||
14 | #ifdef USE_SDLOGGER |
||
15 | #define __SD_INTERFACE_INVERTED // the interface between the controller and the SD-card uses an inverting leveltranslator (transistorinverter) |
||
16 | #endif // and therefore the signals to or from the memorycard have to be inverted. |
||
17 | |||
18 | #ifdef USE_FOLLOWME // uses resitors, therefore its not inverted |
||
19 | //#define __SD_INTERFACE_INVERTED // the interface between the controller and the MMC/SD-card uses an inverting leveltranslator (transistorinverter) |
||
20 | #endif |
||
21 | |||
22 | #define DDR_SPI DDRB |
||
23 | #define DD_MISO DDB6 //Port Pin that is connected to the DO of the MMC/SD-card |
||
24 | #define DD_MOSI DDB5 //Port Pin that is connected to DI of the MMC/SD-card |
||
25 | #define DD_SCK DDB7 //Port Pin that is connected the CLK of the MMC/SD-card |
||
26 | #define DD_SS DDB4 //Slave Select is not used in SPI Master Mode, but must be defined |
||
27 | #define DD_CS DDB4 //Port Pin that is connected to /CS of the MMC/SD-Karte |
||
28 | |||
29 | // for compatibility reasons gcc3.x <-> gcc4.x |
||
30 | #ifndef SPCR |
||
31 | #define SPCR SPCR0 |
||
32 | #endif |
||
33 | #ifndef SPIE |
||
34 | #define SPIE SPIE0 |
||
35 | #endif |
||
36 | #ifndef SPE |
||
37 | #define SPE SPE0 |
||
38 | #endif |
||
39 | #ifndef DORD |
||
40 | #define DORD DORD0 |
||
41 | #endif |
||
42 | #ifndef MSTR |
||
43 | #define MSTR MSTR0 |
||
44 | #endif |
||
45 | #ifndef CPOL |
||
46 | #define CPOL CPOL0 |
||
47 | #endif |
||
48 | #ifndef CPHA |
||
49 | #define CPHA CPHA0 |
||
50 | #endif |
||
51 | #ifndef SPR1 |
||
52 | #define SPR1 SPR01 |
||
53 | #endif |
||
54 | #ifndef SPR0 |
||
55 | #define SPR0 SPR00 |
||
56 | #endif |
||
57 | |||
58 | #ifndef SPDR |
||
59 | #define SPDR SPDR0 |
||
60 | #endif |
||
61 | |||
62 | #ifndef SPSR |
||
63 | #define SPSR SPSR0 |
||
64 | #endif |
||
65 | #ifndef SPIF |
||
66 | #define SPIF SPIF0 |
||
67 | #endif |
||
68 | #ifndef WCOL |
||
69 | #define WCOL WCOL0 |
||
70 | #endif |
||
71 | #ifndef SPI2X |
||
72 | #define SPI2X SPI2X0 |
||
73 | #endif |
||
74 | |||
75 | |||
76 | //________________________________________________________________________________________________________________________________________ |
||
77 | // Function: SSC_Init(void); |
||
78 | // |
||
79 | // Description: This function initialises the synchronus serial channel to the sdcard. |
||
80 | // |
||
81 | // |
||
82 | // Returnvalue: none |
||
83 | //________________________________________________________________________________________________________________________________________ |
||
84 | |||
85 | void SSC_Init(void) |
||
86 | { |
||
87 | // Set MOSI,SCK and CS as output |
||
88 | DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_CS); |
||
89 | // set MISO as input |
||
90 | DDR_SPI &= ~(1<<DD_MISO); |
||
91 | |||
92 | SSC_Disable(); |
||
93 | |||
94 | // 20MHz / 32 = 625 kHz |
||
95 | #ifdef __SD_INTERFACE_INVERTED |
||
96 | SPCR = (1<<SPE)|(1<<MSTR)|(0<<DORD)|(1<<CPOL)|(0<<CPHA)|(1<<SPR1)|(0<<SPR0); // Enable SSC in mastermode, inverted clockpolarity (idle high) |
||
97 | #else |
||
98 | SPCR = (1<<SPE)|(1<<MSTR)|(0<<DORD)|(0<<CPOL)|(0<<CPHA)|(1<<SPR1)|(0<<SPR0); // Enable SSC in mastermode, noninverted clockpolarity (idle low) |
||
99 | #endif |
||
100 | SPSR |= (1<<SPI2X); |
||
101 | |||
102 | // set port pin as input pullup for SD-Card switch |
||
103 | #ifdef USE_FOLLOWME |
||
104 | PORTB |= (1 << PORTB2); |
||
105 | DDRB &= ~(1 << DDB2); |
||
106 | #endif |
||
107 | |||
108 | #ifdef USE_SDLOGGER |
||
109 | PORTB |= (1 << PORTB3); |
||
110 | DDRB &= ~(1 << DDB3); |
||
111 | #endif |
||
112 | } |
||
113 | |||
114 | void SSC_Deinit(void) |
||
115 | { |
||
116 | SSC_Disable(); |
||
117 | SPCR = 0; |
||
118 | SPSR = 0; |
||
119 | } |
||
120 | |||
121 | //________________________________________________________________________________________________________________________________________ |
||
122 | // Function: SSC_GetChar(void); |
||
123 | // |
||
124 | // Description: This function reads one byte from the SSC |
||
125 | // |
||
126 | // |
||
127 | // Returnvalue: the byte received. |
||
128 | //________________________________________________________________________________________________________________________________________ |
||
129 | |||
130 | uint8_t SSC_GetChar (void) |
||
131 | { |
||
132 | uint8_t Byte = 0; |
||
133 | |||
134 | #ifdef __SD_INTERFACE_INVERTED |
||
135 | SPDR = 0x00; // send dummy byte to initiate the reading |
||
136 | #else |
||
137 | SPDR = 0xFF; // send dummy byte to initiate the reading |
||
138 | #endif |
||
139 | while(!(SPSR & (1<<SPIF))) |
||
140 | { |
||
141 | // wait until the data has been read. |
||
142 | } |
||
143 | Byte = SPDR; |
||
144 | |||
145 | #ifdef __SD_INTERFACE_INVERTED |
||
146 | Byte = ~Byte; |
||
147 | #endif |
||
148 | |||
149 | return(Byte); |
||
150 | } |
||
151 | |||
152 | |||
153 | //________________________________________________________________________________________________________________________________________ |
||
154 | // Function: SSC_PutChar(u8 Byte); |
||
155 | // |
||
156 | // Description: This function writes one byte to the SSC |
||
157 | // |
||
158 | // |
||
159 | // Returnvalue: none |
||
160 | //________________________________________________________________________________________________________________________________________ |
||
161 | |||
162 | void SSC_PutChar (uint8_t Byte) |
||
163 | { |
||
164 | |||
165 | #ifdef __SD_INTERFACE_INVERTED |
||
166 | SPDR = ~Byte; // send one byte of data to the SSC |
||
167 | #else |
||
168 | SPDR = Byte; // send one byte of data to the SSC |
||
169 | #endif |
||
170 | while(!(SPSR & (1<<SPIF))) |
||
171 | { |
||
172 | // wait until the data has been sent. |
||
173 | } |
||
174 | } |
||
175 | |||
176 | |||
177 | //________________________________________________________________________________________________________________________________________ |
||
178 | // Function: SSC_Disable(void); |
||
179 | // |
||
180 | // Description: This function enables chipselect of the sdcard (active low) |
||
181 | // |
||
182 | // |
||
183 | // Returnvalue: none |
||
184 | //________________________________________________________________________________________________________________________________________ |
||
185 | |||
186 | void SSC_Disable(void) |
||
187 | { |
||
188 | #ifdef __SD_INTERFACE_INVERTED |
||
189 | PORTW_SPI &= ~(1<<PORT_CS); // disable chipselect of the sdcard (active low). |
||
190 | #else |
||
191 | PORTW_SPI |= (1<<PORT_CS); // disable chipselect of the sdcard (active low). |
||
192 | #endif |
||
193 | } |
||
194 | |||
195 | |||
196 | |||
197 | |||
198 | //________________________________________________________________________________________________________________________________________ |
||
199 | // Function: SSC_Enable(void); |
||
200 | // |
||
201 | // Description: This function disables chipselect of the sdcard (active low) |
||
202 | // |
||
203 | // |
||
204 | // Returnvalue: none |
||
205 | //________________________________________________________________________________________________________________________________________ |
||
206 | |||
207 | void SSC_Enable(void) |
||
208 | { |
||
209 | #ifdef __SD_INTERFACE_INVERTED |
||
210 | PORTW_SPI |= (1<<PORT_CS); // enable chipselect of the sdcard (active low). |
||
211 | #else |
||
212 | PORTW_SPI &= ~(1<<PORT_CS); // enable chipselect of the sdcard (active low). |
||
213 | #endif |
||
214 | } |
||
215 | |||
216 |