Rev 271 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 271 | Rev 294 | ||
---|---|---|---|
1 | - | ||
2 | #include <avr/io.h> |
1 | #include <avr/io.h> |
3 | #include "fat16.h" |
- | |
4 | #include "ssc.h" |
2 | #include "ssc.h" |
5 | - | ||
6 | //________________________________________________________________________________________________________________________________________ |
- | |
7 | // Module name: fat16.c |
- | |
8 | // Compiler used: avr-gcc 3.4.5 |
- | |
9 | // Last Modifikation: 24.07.2007 |
- | |
10 | // Version: 1.03 |
- | |
11 | // Authors: Stephan Busker |
- | |
12 | // Description: Source files for access to the synchrnous serial channel. |
- | |
13 | // Copyright (C) 2007 Stephan Busker |
- | |
14 | //........................................................................................................................................ |
- | |
15 | // Functions: extern void SSC_Init(void); |
- | |
16 | // extern u8 SSC_GetChar (void); |
- | |
17 | // extern void SSC_PutChar (u8 Byte); |
- | |
18 | // extern void SSC_Disable(void); |
- | |
19 | // extern void SSC_Enable(void); |
- | |
20 | //........................................................................................................................................ |
- | |
21 | // ext. functions: extern u8 SDC_GetSector (u32,u8*); |
- | |
22 | // extern u8 SDC_PutSector (u32,u8*); |
- | |
23 | //........................................................................................................................................ |
- | |
24 | // |
- | |
25 | // URL: www.Mikro-Control.de |
- | |
26 | // mailto: stephan.busker@mikro-control.de |
- | |
- | 3 | ||
- | 4 | ||
- | 5 | ||
- | 6 | //-------------------------------------- Hardware specific definitions -------------------------------------- |
|
- | 7 | #define PORTR_SPI PINB |
|
- | 8 | #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 |
|
- | 10 | #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 |
|
- | 12 | #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 |
|
- | 14 | ||
- | 15 | ||
- | 16 | #ifdef USE_SDLOGGER |
|
- | 17 | #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. |
|
- | 19 | ||
- | 20 | #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) |
|
- | 22 | #endif |
|
- | 23 | ||
- | 24 | #define DDR_SPI DDRB |
|
- | 25 | #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 |
|
- | 27 | #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 |
|
- | 29 | #define DD_CS DDB4 //Port Pin that is connected to /CS of the MMC/SD-Karte |
|
- | 30 | ||
- | 31 | // for compatibility reasons gcc3.x <-> gcc4.x |
|
- | 32 | #ifndef SPCR |
|
- | 33 | #define SPCR SPCR0 |
|
- | 34 | #endif |
|
- | 35 | #ifndef SPIE |
|
- | 36 | #define SPIE SPIE0 |
|
- | 37 | #endif |
|
- | 38 | #ifndef SPE |
|
- | 39 | #define SPE SPE0 |
|
- | 40 | #endif |
|
- | 41 | #ifndef DORD |
|
- | 42 | #define DORD DORD0 |
|
- | 43 | #endif |
|
- | 44 | #ifndef MSTR |
|
- | 45 | #define MSTR MSTR0 |
|
- | 46 | #endif |
|
- | 47 | #ifndef CPOL |
|
- | 48 | #define CPOL CPOL0 |
|
- | 49 | #endif |
|
- | 50 | #ifndef CPHA |
|
- | 51 | #define CPHA CPHA0 |
|
- | 52 | #endif |
|
- | 53 | #ifndef SPR1 |
|
- | 54 | #define SPR1 SPR01 |
|
- | 55 | #endif |
|
- | 56 | #ifndef SPR0 |
|
- | 57 | #define SPR0 SPR00 |
|
- | 58 | #endif |
|
- | 59 | ||
- | 60 | #ifndef SPDR |
|
- | 61 | #define SPDR SPDR0 |
|
- | 62 | #endif |
|
- | 63 | ||
- | 64 | #ifndef SPSR |
|
- | 65 | #define SPSR SPSR0 |
|
- | 66 | #endif |
|
- | 67 | #ifndef SPIF |
|
- | 68 | #define SPIF SPIF0 |
|
- | 69 | #endif |
|
- | 70 | #ifndef WCOL |
|
- | 71 | #define WCOL WCOL0 |
|
- | 72 | #endif |
|
27 | //________________________________________________________________________________________________________________________________________ |
73 | #ifndef SPI2X |
28 | 74 | #define SPI2X SPI2X0 |
|
29 | 75 | #endif |
|
30 | 76 | ||
31 | 77 | ||
32 | //________________________________________________________________________________________________________________________________________ |
78 | //________________________________________________________________________________________________________________________________________ |
33 | // Funtion: SSC_Init(void); |
79 | // Funtion: SSC_Init(void); |
34 | // |
80 | // |
35 | // Description: This function initialises the synchronus serial channel to the sdcard. |
81 | // Description: This function initialises the synchronus serial channel to the sdcard. |
36 | // |
82 | // |
37 | // |
83 | // |
38 | // Returnvalue: none |
84 | // Returnvalue: none |
39 | //________________________________________________________________________________________________________________________________________ |
85 | //________________________________________________________________________________________________________________________________________ |
40 | 86 | ||
41 | void SSC_Init(void) |
87 | void SSC_Init(void) |
42 | { |
88 | { |
43 | MMC_Direction_REG &=~(1<<SPI_DI); // Set the direction of the ssc-port |
89 | // Set MOSI,SCK and CS as output |
44 | MMC_Direction_REG |= (1<<SPI_Clock); // _______ _______ |
- | |
45 | MMC_Direction_REG |= (1<<SPI_DO); // CS \________________________/ |
- | |
46 | MMC_Direction_REG |= (1<<MMC_Chip_Select); // |
90 | DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_CS); |
47 | MMC_Direction_REG |= (1<<SPI_SS); // ___ ___ ___ |
- | |
48 | // clk __________/ \___/ \___/ \_________ |
- | |
49 | // |
91 | // set MISO as input |
50 | SSC_Disable(); // ___ ___ |
92 | DDR_SPI &= ~(1<<DD_MISO); |
51 | // data_________/ \___________/ \___________ |
- | |
52 | 93 | ||
53 | // initialise ssc, clock = Idel low |
- | |
54 | // devide clock by 32 |
- | |
55 | SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<CPOL); // Enable SSC in mastermode, invert clockpolarity (idle high) |
- | |
56 | SPSR = SPSR|(1<<SPI2X); |
94 | SSC_Disable(); |
57 | } |
- | |
58 | - | ||
59 | - | ||
60 | - | ||
61 | 95 | ||
62 | //________________________________________________________________________________________________________________________________________ |
- | |
- | 96 | // 20MHz / 32 = 625 kHz |
|
63 | // Funtion: void SSC_ClearRxFifo(void); |
97 | #ifdef __SD_INTERFACE_INVERTED |
64 | // |
- | |
65 | // Description: Clears the fifo of the ssc if the controller used has a builtin fifo. |
98 | SPCR = (1<<SPE)|(1<<MSTR)|(0<<DORD)|(1<<CPOL)|(0<<CPHA)|(1<<SPR1)|(1<<SPR0); // Enable SSC in mastermode, inverted clockpolarity (idle high) |
66 | // |
99 | #else |
- | 100 | SPCR = (1<<SPE)|(1<<MSTR)|(0<<DORD)|(0<<CPOL)|(0<<CPHA)|(1<<SPR1)|(1<<SPR0); // Enable SSC in mastermode, noninverted clockpolarity (idle low) |
|
67 | // |
101 | #endif |
68 | // Returnvalue: none |
102 | SPSR |= (1<<SPI2X); |
69 | //________________________________________________________________________________________________________________________________________ |
- | |
- | 103 | ||
- | 104 | // set port pin as input pullup for SD-Card switch |
|
- | 105 | #ifdef USE_FOLLOWME |
|
- | 106 | PORTB |= (1 << PORTB2); |
|
- | 107 | DDRB &= ~(1 << DDB2); |
|
70 | 108 | #endif |
|
- | 109 | ||
- | 110 | #ifdef USE_SDLOGGER |
|
71 | 111 | PORTB |= (1 << PORTB3); |
|
72 | void SSC_ClearRxFifo(void) |
- | |
73 | { |
112 | DDRB &= ~(1 << DDB3); |
- | 113 | #endif |
|
- | 114 | } |
|
- | 115 | ||
74 | // enter your code here to clear the rx-fifo of the ssc. |
116 | void SSC_Deinit(void) |
75 | } |
117 | { |
76 | 118 | ||
77 | 119 | } |
|
78 | 120 | ||
79 | //________________________________________________________________________________________________________________________________________ |
121 | //________________________________________________________________________________________________________________________________________ |
80 | // Funtion: SSC_GetChar(void); |
122 | // Function: SSC_GetChar(void); |
81 | // |
123 | // |
82 | // Description: This function reads one byte from the SSC |
124 | // Description: This function reads one byte from the SSC |
83 | // |
125 | // |
84 | // |
126 | // |
85 | // Returnvalue: the byte received. |
127 | // Returnvalue: the byte received. |
86 | //________________________________________________________________________________________________________________________________________ |
128 | //________________________________________________________________________________________________________________________________________ |
87 | 129 | ||
88 | u8 SSC_GetChar (void) |
130 | uint8_t SSC_GetChar (void) |
89 | { |
131 | { |
90 | u8 Byte = 0; |
- | |
91 | - | ||
92 | SPDR = 0x00; // read one byte of data from the SSC |
- | |
93 | while(!(SPSR & (1<<SPIF))){}; // wait until the data has been read. |
- | |
94 | Byte = SPDR; |
132 | uint8_t Byte = 0; |
95 | 133 | ||
96 | #ifdef __MMC_INTERFACE_INVERTED |
134 | #ifdef __SD_INTERFACE_INVERTED |
97 | return (~Byte); |
135 | SPDR = 0x00; // send dummy byte to initiate the reading |
98 | #else |
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 |
|
99 | return (Byte); // the byte received |
146 | Byte = ~Byte; |
100 | #endif |
147 | #endif |
101 | 148 | ||
102 | 149 | return(Byte); |
|
103 | } |
150 | } |
104 | 151 | ||
105 | 152 | ||
106 | //________________________________________________________________________________________________________________________________________ |
153 | //________________________________________________________________________________________________________________________________________ |
107 | // Funtion: SSC_PutChar(u8 Byte); |
154 | // Function: SSC_PutChar(u8 Byte); |
108 | // |
155 | // |
109 | // Description: This function writes one byte to the SSC |
156 | // Description: This function writes one byte to the SSC |
110 | // |
157 | // |
111 | // |
158 | // |
112 | // Returnvalue: none |
159 | // Returnvalue: none |
113 | //________________________________________________________________________________________________________________________________________ |
160 | //________________________________________________________________________________________________________________________________________ |
114 | 161 | ||
115 | void SSC_PutChar (u8 Byte) |
162 | void SSC_PutChar (uint8_t Byte) |
- | 163 | { |
|
116 | { |
164 | |
117 | #ifdef __MMC_INTERFACE_INVERTED |
165 | #ifdef __SD_INTERFACE_INVERTED |
118 | SPDR = ~Byte; // send one byte of data to the SSC |
166 | SPDR = ~Byte; // send one byte of data to the SSC |
119 | #else |
167 | #else |
120 | SPDR = Byte; // send one byte of data to the SSC |
168 | SPDR = Byte; // send one byte of data to the SSC |
121 | #endif |
169 | #endif |
122 | SPDR = ~Byte; // send one byte of data to the SSC |
- | |
123 | while(!(SPSR & (1<<SPIF))) // wait until data was send. |
170 | while(!(SPSR & (1<<SPIF))) |
124 | { |
171 | { |
- | 172 | // wait until the data has been sent. |
|
125 | } |
173 | } |
126 | } |
174 | } |
127 | 175 | ||
128 | 176 | ||
129 | //________________________________________________________________________________________________________________________________________ |
177 | //________________________________________________________________________________________________________________________________________ |
130 | // Funtion: SSC_Disable(void); |
178 | // Function: SSC_Disable(void); |
131 | // |
179 | // |
132 | // Description: This function enables chipselect of the sdcard (active low) |
180 | // Description: This function enables chipselect of the sdcard (active low) |
133 | // |
181 | // |
134 | // |
182 | // |
135 | // Returnvalue: none |
183 | // Returnvalue: none |
136 | //________________________________________________________________________________________________________________________________________ |
184 | //________________________________________________________________________________________________________________________________________ |
137 | 185 | ||
138 | void SSC_Disable(void) |
186 | void SSC_Disable(void) |
139 | { |
187 | { |
140 | #ifdef __MMC_INTERFACE_INVERTED |
188 | #ifdef __SD_INTERFACE_INVERTED |
141 | MMC_Write &= ~(1<<MMC_Chip_Select); // disable chipselect of the sdcard (active low). |
189 | PORTW_SPI &= ~(1<<PORT_CS); // disable chipselect of the sdcard (active low). |
142 | #else |
190 | #else |
143 | MMC_Write |= (1<<MMC_Chip_Select); // enable chipselect of the sdcard (active low). |
191 | PORTW_SPI |= (1<<PORT_CS); // disable chipselect of the sdcard (active low). |
144 | #endif |
192 | #endif |
145 | } |
193 | } |
146 | 194 | ||
147 | 195 | ||
148 | 196 | ||
149 | 197 | ||
150 | //________________________________________________________________________________________________________________________________________ |
198 | //________________________________________________________________________________________________________________________________________ |
151 | // Funtion: SSC_Enable(void); |
199 | // Function: SSC_Enable(void); |
152 | // |
200 | // |
153 | // Description: This function disables chipselect of the sdcard (active low) |
201 | // Description: This function disables chipselect of the sdcard (active low) |
154 | // |
202 | // |
155 | // |
203 | // |
156 | // Returnvalue: none |
204 | // Returnvalue: none |
157 | //________________________________________________________________________________________________________________________________________ |
205 | //________________________________________________________________________________________________________________________________________ |
158 | 206 | ||
159 | void SSC_Enable(void) |
207 | void SSC_Enable(void) |
160 | { |
208 | { |
161 | #ifdef __MMC_INTERFACE_INVERTED |
209 | #ifdef __SD_INTERFACE_INVERTED |
162 | MMC_Write |= (1<<MMC_Chip_Select); // enable chipselect of the sdcard (active low). |
210 | PORTW_SPI |= (1<<PORT_CS); // enable chipselect of the sdcard (active low). |
163 | #else |
211 | #else |
164 | MMC_Write &= ~(1<<MMC_Chip_Select); // disable chipselect of the sdcard (active low). |
212 | PORTW_SPI &= ~(1<<PORT_CS); // enable chipselect of the sdcard (active low). |
165 | #endif |
213 | #endif |
166 | } |
214 | } |
- | 215 | ||
167 | 216 | ||
168 | 217 |