Rev 271 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 271 | Rev 294 | ||
---|---|---|---|
Line 1... | Line -... | ||
1 | - | ||
2 | #include <avr/io.h> |
1 | #include <avr/io.h> |
3 | #include "fat16.h" |
- | |
4 | #include "ssc.h" |
2 | #include "ssc.h" |
Line 5... | Line -... | ||
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 |
- | |
Line -... | Line 3... | ||
- | 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 |
|
Line 27... | Line 73... | ||
27 | //________________________________________________________________________________________________________________________________________ |
73 | #ifndef SPI2X |
28 | 74 | #define SPI2X SPI2X0 |
|
29 | 75 | #endif |
|
Line 38... | Line 84... | ||
38 | // Returnvalue: none |
84 | // Returnvalue: none |
39 | //________________________________________________________________________________________________________________________________________ |
85 | //________________________________________________________________________________________________________________________________________ |
Line 40... | Line 86... | ||
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 |
- | |
44 | MMC_Direction_REG |= (1<<SPI_Clock); // _______ _______ |
- | |
45 | MMC_Direction_REG |= (1<<SPI_DO); // CS \________________________/ |
89 | // Set MOSI,SCK and CS as output |
46 | MMC_Direction_REG |= (1<<MMC_Chip_Select); // |
- | |
47 | MMC_Direction_REG |= (1<<SPI_SS); // ___ ___ ___ |
- | |
48 | // clk __________/ \___/ \___/ \_________ |
90 | DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_CS); |
49 | // |
91 | // set MISO as input |
50 | SSC_Disable(); // ___ ___ |
- | |
51 | // data_________/ \___________/ \___________ |
92 | DDR_SPI &= ~(1<<DD_MISO); |
52 | - | ||
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) |
93 | |
56 | SPSR = SPSR|(1<<SPI2X); |
- | |
57 | } |
- | |
58 | - | ||
59 | - | ||
60 | 94 | SSC_Disable(); |
|
61 | - | ||
- | 95 | ||
62 | //________________________________________________________________________________________________________________________________________ |
96 | // 20MHz / 32 = 625 kHz |
63 | // Funtion: void SSC_ClearRxFifo(void); |
- | |
64 | // |
97 | #ifdef __SD_INTERFACE_INVERTED |
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) |
- | 99 | #else |
|
66 | // |
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 |
- | |
Line -... | Line 102... | ||
- | 102 | SPSR |= (1<<SPI2X); |
|
- | 103 | ||
- | 104 | // set port pin as input pullup for SD-Card switch |
|
- | 105 | #ifdef USE_FOLLOWME |
|
- | 106 | PORTB |= (1 << PORTB2); |
|
Line 69... | Line 107... | ||
69 | //________________________________________________________________________________________________________________________________________ |
107 | DDRB &= ~(1 << DDB2); |
- | 108 | #endif |
|
- | 109 | ||
70 | 110 | #ifdef USE_SDLOGGER |
|
71 | - | ||
72 | void SSC_ClearRxFifo(void) |
111 | PORTB |= (1 << PORTB3); |
Line -... | Line 112... | ||
- | 112 | DDRB &= ~(1 << DDB3); |
|
- | 113 | #endif |
|
Line -... | Line 114... | ||
- | 114 | } |
|
Line 73... | Line 115... | ||
73 | { |
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); |
Line 81... | Line 123... | ||
81 | // |
123 | // |
82 | // Description: This function reads one byte from the SSC |
124 | // Description: This function reads one byte from the SSC |
83 | // |
125 | // |
Line 84... | Line -... | ||
84 | // |
- | |
85 | // Returnvalue: the byte received. |
- | |
86 | //________________________________________________________________________________________________________________________________________ |
- | |
87 | - | ||
88 | u8 SSC_GetChar (void) |
126 | // |
89 | { |
127 | // Returnvalue: the byte received. |
90 | u8 Byte = 0; |
128 | //________________________________________________________________________________________________________________________________________ |
91 | 129 | ||
92 | SPDR = 0x00; // read one byte of data from the SSC |
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 |
|
Line -... | Line 136... | ||
- | 136 | #else |
|
- | 137 | SPDR = 0xFF; // send dummy byte to initiate the reading |
|
- | 138 | #endif |
|
Line -... | Line 139... | ||
- | 139 | while(!(SPSR & (1<<SPIF))) |
|
93 | while(!(SPSR & (1<<SPIF))){}; // wait until the data has been read. |
140 | { |
Line 94... | Line 141... | ||
94 | Byte = SPDR; |
141 | // wait until the data has been read. |
95 | 142 | } |
|
96 | #ifdef __MMC_INTERFACE_INVERTED |
143 | Byte = SPDR; |
97 | return (~Byte); |
144 | |
98 | #else |
145 | #ifdef __SD_INTERFACE_INVERTED |
99 | return (Byte); // the byte received |
146 | Byte = ~Byte; |
100 | #endif |
147 | #endif |
101 | 148 | ||
Line 102... | Line 149... | ||
102 | 149 | return(Byte); |
|
103 | } |
150 | } |
- | 151 | ||
104 | 152 | ||
105 | 153 | //________________________________________________________________________________________________________________________________________ |
|
106 | //________________________________________________________________________________________________________________________________________ |
154 | // Function: SSC_PutChar(u8 Byte); |
107 | // Funtion: SSC_PutChar(u8 Byte); |
155 | // |
108 | // |
156 | // Description: This function writes one byte to the SSC |
109 | // Description: This function writes one byte to the SSC |
- | |
110 | // |
157 | // |
111 | // |
158 | // |
- | 159 | // Returnvalue: none |
|
112 | // Returnvalue: none |
160 | //________________________________________________________________________________________________________________________________________ |
113 | //________________________________________________________________________________________________________________________________________ |
161 | |
Line 114... | Line 162... | ||
114 | 162 | void SSC_PutChar (uint8_t Byte) |
|
115 | void SSC_PutChar (u8 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 |
Line 122... | Line 170... | ||
122 | SPDR = ~Byte; // send one byte of data to the SSC |
170 | while(!(SPSR & (1<<SPIF))) |
123 | while(!(SPSR & (1<<SPIF))) // wait until data was send. |
171 | { |
124 | { |
172 | // wait until the data has been sent. |
125 | } |
173 | } |
126 | } |
174 | } |
127 | 175 | ||
128 | 176 | ||
129 | //________________________________________________________________________________________________________________________________________ |
177 | //________________________________________________________________________________________________________________________________________ |
Line 130... | Line 178... | ||
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 | ||
Line 138... | Line 186... | ||
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 | } |
Line -... | Line 194... | ||
- | 194 |