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