Rev 1 | Rev 41 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1 | Rev 24 | ||
---|---|---|---|
Line 52... | Line 52... | ||
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
57 | #include "main.h" |
57 | #include "91x_lib.h" |
Line 58... | Line 58... | ||
58 | 58 | ||
59 | //________________________________________________________________________________________________________________________________________ |
59 | //________________________________________________________________________________________________________________________________________ |
60 | // Module name: fat16.c |
60 | // Module name: ssc.c |
61 | // Compiler used: avr-gcc 3.4.5 |
61 | // Compiler used: avr-gcc 3.4.5 |
62 | // Last Modifikation: 24.07.2007 |
62 | // Last Modifikation: 24.07.2007 |
63 | // Version: 1.02 |
63 | // Version: 1.02 |
64 | // Authors: Stephan Busker |
64 | // Authors: Stephan Busker |
65 | // Description: Source files for access to the synchrnous serial channel. |
65 | // Description: Source files for access to the synchrnous serial channel. |
66 | // Copyright (C) 2007 Stephan Busker |
66 | // Copyright (C) 2007 Stephan Busker |
67 | //........................................................................................................................................ |
67 | //........................................................................................................................................ |
68 | // Functions: extern void SSC_Init(void); |
68 | // ext. Functions: extern void SSC_Init(void); |
69 | // extern u8 SSC_GetChar (void); |
69 | // extern u8 SSC_GetChar (void); |
70 | // extern void SSC_PutChar (u8 Byte); |
70 | // extern void SSC_PutChar (u8 Byte); |
71 | // extern void SSC_Disable(void); |
71 | // extern void SSC_Disable(void); |
- | 72 | // extern void SSC_Enable(void); |
|
72 | // extern void SSC_Enable(void); |
73 | // extern void SSC_ClearRxFifo(); |
73 | //........................................................................................................................................ |
- | |
74 | // ext. functions: extern u8 SDC_GetSector (unsigned long,u8 *); |
- | |
75 | // extern u8 SDC_PutSector (unsigned long,u8 *); |
- | |
76 | //........................................................................................................................................ |
- | |
77 | // |
74 | //........................................................................................................................................ |
78 | // URL: www.Mikro-Control.de |
75 | // URL: www.Mikro-Control.de |
79 | // mailto: stephan.busker@mikro-control.de |
76 | // mailto: stephan.busker@mikro-control.de |
Line -... | Line 77... | ||
- | 77 | //________________________________________________________________________________________________________________________________________ |
|
- | 78 | ||
- | 79 | ||
- | 80 | ||
- | 81 | //________________________________________________________________________________________________________________________________________ |
|
- | 82 | // Function: SSC_Enable(void); |
|
- | 83 | // |
|
- | 84 | // Description: This function enables chipselect of the sdcard (active low) |
|
Line -... | Line 85... | ||
- | 85 | // |
|
- | 86 | // |
|
- | 87 | // Returnvalue: none |
|
- | 88 | //________________________________________________________________________________________________________________________________________ |
|
- | 89 | ||
- | 90 | void SSC_Enable(void) |
|
80 | //________________________________________________________________________________________________________________________________________ |
91 | { |
- | 92 | // enable chipselect of the sd-card (P5.4 -> SD-CS, active low). |
|
- | 93 | GPIO_WriteBit(GPIO5, GPIO_Pin_4 , Bit_RESET); |
|
- | 94 | } |
|
- | 95 | ||
- | 96 | //________________________________________________________________________________________________________________________________________ |
|
- | 97 | // Function: SSC_Disable(void); |
|
- | 98 | // |
|
- | 99 | // Description: This function disables chipselect of the sdcard (active low) |
|
- | 100 | // |
|
- | 101 | // |
|
- | 102 | // Returnvalue: none |
|
- | 103 | //________________________________________________________________________________________________________________________________________ |
|
- | 104 | ||
- | 105 | void SSC_Disable(void) |
|
- | 106 | { |
|
- | 107 | // disable chipselect of the sd-card (P5.4 -> SD-CS, active low). |
|
81 | 108 | GPIO_WriteBit(GPIO5, GPIO_Pin_4 , Bit_SET); |
|
82 | 109 | } |
|
83 | 110 | ||
84 | 111 | ||
85 | //________________________________________________________________________________________________________________________________________ |
112 | //________________________________________________________________________________________________________________________________________ |
86 | // Funtion: SSC_Init(void); |
113 | // Function: SSC_Init(void); |
87 | // |
114 | // |
Line 88... | Line 115... | ||
88 | // Description: This function initialises the synchronus serial channel to the sdcard. |
115 | // Description: This function initialises the synchronus serial channel to the sdcard. |
89 | // |
116 | // |
- | 117 | // |
|
- | 118 | // Returnvalue: none |
|
- | 119 | //________________________________________________________________________________________________________________________________________ |
|
- | 120 | ||
- | 121 | void SSC_Init(void) |
|
- | 122 | { |
|
- | 123 | GPIO_InitTypeDef GPIO_InitStructure; |
|
- | 124 | SSP_InitTypeDef SSP_InitStructure; |
|
- | 125 | WIU_InitTypeDef WIU_InitStructure; |
|
- | 126 | // enable APB clock for SPI1 |
|
- | 127 | SCU_APBPeriphClockConfig(__SSP1 ,ENABLE); |
|
- | 128 | // configure P5.4 -> SD-CS as an output pin |
|
- | 129 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
|
- | 130 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; |
|
- | 131 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ; |
|
- | 132 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable; |
|
- | 133 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1; |
|
- | 134 | GPIO_Init (GPIO5, &GPIO_InitStructure); |
|
- | 135 | // configure P3.4 -> SCK1 and P3.6 -> MOSI1 as an output pin |
|
- | 136 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
|
- | 137 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6; |
|
- | 138 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ; |
|
- | 139 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable; |
|
- | 140 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; |
|
- | 141 | GPIO_Init (GPIO3, &GPIO_InitStructure); |
|
- | 142 | // configure P3.5 <- MISO1 as an input pin |
|
- | 143 | GPIO_InitStructure.GPIO_Direction = GPIO_PinInput; |
|
- | 144 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; |
|
- | 145 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull; |
|
- | 146 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable; |
|
- | 147 | GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; |
|
- | 148 | GPIO_Init (GPIO3, &GPIO_InitStructure); |
|
- | 149 | // configure SPI1 |
|
- | 150 | SSP_DeInit(SSP1); |
|
- | 151 | SSP_StructInit(&SSP_InitStructure); |
|
- | 152 | SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola; |
|
- | 153 | SSP_InitStructure.SSP_Mode = SSP_Mode_Master; |
|
- | 154 | SSP_InitStructure.SSP_CPHA = SSP_CPHA_1Edge; |
|
- | 155 | SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low; |
|
- | 156 | // Set Baud Rate (Prescaler) |
|
- | 157 | // bit rate is BRCLK/SSP_ClockPrescaler/(1+SSP_ClockRate)) |
|
- | 158 | // With MSCLK = 48MHz/2 = BRCLK we get for the SPICLK = 24Mhz / 8 / (1+5) = 500 kHz |
|
- | 159 | SSP_InitStructure.SSP_ClockRate = 14; //5 |
|
- | 160 | SSP_InitStructure.SSP_ClockPrescaler = 8; |
|
- | 161 | SSP_Init(SSP1, &SSP_InitStructure); |
|
- | 162 | SSC_Disable(); |
|
- | 163 | SSP_Cmd(SSP1, ENABLE); |
|
- | 164 | ||
- | 165 | // Configure SD_SWITCH at pin GPIO5.3 as an external irq 11 |
|
- | 166 | ||
- | 167 | // configure the port |
|
- | 168 | SCU_APBPeriphClockConfig(__GPIO5, ENABLE); // Enable the GPIO5 Clock |
|
- | 169 | GPIO_InitStructure.GPIO_Direction = GPIO_PinInput; |
|
- | 170 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; |
|
- | 171 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ; |
|
- | 172 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable; |
|
- | 173 | GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; |
|
- | 174 | GPIO_Init(GPIO5, &GPIO_InitStructure); |
|
- | 175 | ||
- | 176 | // configure the EXT11 interrupt line (P5.3) as an IRQ with the lowest priority |
|
- | 177 | SCU_APBPeriphClockConfig(__WIU, ENABLE); |
|
- | 178 | WIU_Cmd(ENABLE ); |
|
- | 179 | WIU_ClearITPendingBit(WIU_Line11); |
|
- | 180 | WIU_DeInit(); |
|
- | 181 | WIU_InitStructure.WIU_TriggerEdge = WIU_FallingEdge; |
|
- | 182 | WIU_InitStructure.WIU_Line = WIU_Line11; |
|
- | 183 | WIU_Init(&WIU_InitStructure); |
|
- | 184 | // The EXTIT1_IRQHandler() is called every time the SD-Switch is activated (falling edge) |
|
- | 185 | // by inserting an sd-card |
|
- | 186 | SCU_WakeUpLineConfig(11); |
|
- | 187 | VIC_Config(EXTIT1_ITLine, VIC_IRQ, 7); |
|
Line 90... | Line 188... | ||
90 | // |
188 | VIC_ITCmd(EXTIT1_ITLine, ENABLE); |
91 | // Returnvalue: none |
- | |
92 | //________________________________________________________________________________________________________________________________________ |
- | |
93 | 189 | } |
|
Line 94... | Line 190... | ||
94 | void SSC_Init(void) |
190 | |
95 | { |
- | |
96 | - | ||
97 | SerialPutString("SPI1 init..."); |
191 | void SSC_Deinit(void) |
98 | 192 | { |
|
99 | GPIO_InitTypeDef GPIO_InitStructure; |
193 | GPIO_InitTypeDef GPIO_InitStructure; |
100 | SSP_InitTypeDef SSP_InitStructure; |
194 | |
101 | 195 | SSP_Cmd(SSP1, DISABLE); |
|
102 | SCU_APBPeriphClockConfig(__SSP1 ,ENABLE); |
196 | SSP_DeInit(SSP1); |
103 | - | ||
- | 197 | ||
104 | // GPIO_DeInit(GPIO2); |
198 | // configure P5.4 -> SD-CS as an input pin |
105 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
199 | GPIO_InitStructure.GPIO_Direction = GPIO_PinInput; |
106 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6; |
200 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; |
107 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ; |
201 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ; |
108 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable; |
202 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable; |
109 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; |
203 | GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; |
110 | GPIO_Init (GPIO3, &GPIO_InitStructure); |
- | |
- | 204 | GPIO_Init (GPIO5, &GPIO_InitStructure); |
|
111 | 205 | // configure P3.4 -> SCK1 and P3.6 -> MOSI1 as an input pin |
|
112 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
206 | GPIO_InitStructure.GPIO_Direction = GPIO_PinInput; |
113 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; |
207 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6; |
114 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ; |
208 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ; |
115 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable; |
209 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable; |
116 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1; |
210 | GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; |
117 | GPIO_Init (GPIO5, &GPIO_InitStructure); |
211 | GPIO_Init (GPIO3, &GPIO_InitStructure); |
118 | 212 | // configure P3.5 <- MISO1 as an input pin |
|
119 | GPIO_InitStructure.GPIO_Direction = GPIO_PinInput; |
213 | GPIO_InitStructure.GPIO_Direction = GPIO_PinInput; |
- | 214 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; |
|
120 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; |
215 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull; |
121 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull; |
216 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable; |
122 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable; |
217 | GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; |
123 | GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; |
218 | GPIO_Init (GPIO3, &GPIO_InitStructure); |
124 | GPIO_Init (GPIO3, &GPIO_InitStructure); |
219 | |
125 | 220 | // disable APB clock for SPI1 |
|
126 | SSP_DeInit(SSP1); |
221 | SCU_APBPeriphClockConfig(__SSP1 ,DISABLE); |
127 | SSP_StructInit(&SSP_InitStructure); |
222 | |
128 | SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola; |
- | |
129 | SSP_InitStructure.SSP_Mode = SSP_Mode_Master; |
- | |
130 | SSP_InitStructure.SSP_CPHA = SSP_CPHA_1Edge; |
- | |
131 | SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low; |
- | |
132 | // Hier muss die Baudrate noch richtig eingestellt werden (mit Prescaler) |
- | |
133 | SSP_InitStructure.SSP_ClockRate = 5; |
- | |
134 | SSP_InitStructure.SSP_ClockPrescaler = 8; |
- | |
135 | SSP_Init(SSP1, &SSP_InitStructure); |
- | |
136 | SSC_Disable(); |
- | |
137 | SSP_Cmd(SSP1, ENABLE); |
- | |
138 | /* MMC_Direction_REG &=~(1<<SPI_DI); // Set the direction of the ssc-port |
- | |
139 | MMC_Direction_REG |= (1<<SPI_Clock); // _______ _______ |
- | |
140 | MMC_Direction_REG |= (1<<SPI_DO); // CS \________________________/ |
- | |
141 | MMC_Direction_REG |= (1<<MMC_Chip_Select); // |
- | |
142 | MMC_Direction_REG |= (1<<SPI_SS); // ___ ___ ___ |
- | |
143 | // clk __________/ \___/ \___/ \_________ |
- | |
144 | // |
- | |
145 | // ___ ___ |
- | |
146 | // data_________/ \___________/ \___________ |
223 | // configure the port of the SDC-Switch |
Line 147... | Line 224... | ||
147 | 224 | SCU_APBPeriphClockConfig(__GPIO5, ENABLE); // Enable the GPIO5 Clock |
|
148 | // initialise ssc, clock = Idel low |
225 | GPIO_InitStructure.GPIO_Direction = GPIO_PinInput; |
149 | // devide clock by 32 |
226 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; |
150 | SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<CPOL); // Enable SSC in mastermode, invert clockpolarity (idle high) |
227 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ; |
151 | SPSR = SPSR|(1<<SPI2X);*/ |
228 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable; |
152 | 229 | GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; |
|
153 | SerialPutString("ok\n\r"); |
230 | GPIO_Init(GPIO5, &GPIO_InitStructure); |
154 | } |
231 | } |
Line 155... | Line 232... | ||
155 | 232 | ||
156 | 233 | ||
157 | //________________________________________________________________________________________________________________________________________ |
234 | //________________________________________________________________________________________________________________________________________ |
158 | // Funtion: SSC_GetChar(void); |
- | |
159 | // |
- | |
160 | // Description: This function reads one byte from the SSC |
235 | // Function: SSC_GetChar(void); |
161 | // |
- | |
162 | // |
236 | // |
163 | // Returnvalue: the byte received. |
237 | // Description: This function reads one byte from the SSC |
164 | //________________________________________________________________________________________________________________________________________ |
- | |
165 | - | ||
166 | u8 SSC_GetChar (void) |
238 | // |
167 | { |
239 | // |
168 | u8 Byte = 0; |
- | |
169 | // SerialPutString("\n\rGetChar."); |
240 | // Returnvalue: the byte received. |
Line 170... | Line 241... | ||
170 | SSP_SendData(SSP1, 0xFF); |
241 | //________________________________________________________________________________________________________________________________________ |
171 | while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoEmpty) != SET); |
242 | |
172 | Byte = SSP_ReceiveData(SSP1); |
243 | u8 SSC_GetChar (void) |
173 | /* SPDR = 0x00; // read one byte of data from the SSC |
244 | { |
174 | while(!(SPSR & (1<<SPIF))){}; // wait until the data has been read. |
245 | u8 Byte = 0; |
- | 246 | while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoNotFull) != SET); // wait for space in the tx fifo |
|
175 | Byte = SPDR; |
247 | SSP_SendData(SSP1, 0xFF);// send dymmy byte (0xFF) as master to receive a byte from the slave |
176 | */ |
- | |
177 | // sprintf(text," %02X ", Byte); SerialPutString(text);SerialPutString("done."); |
248 | while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoEmpty) != SET); // wait for the byte to be sent |
178 | return (Byte); |
249 | Byte = SSP_ReceiveData(SSP1); // read the byte transmitted from the slave |
179 | 250 | return (Byte); |
|
180 | } |
251 | } |
181 | 252 | ||
182 | //________________________________________________________________________________________________________________________________________ |
253 | //________________________________________________________________________________________________________________________________________ |
183 | void SSC_ClearRxFifo (void) |
254 | void SSC_ClearRxFifo (void) |
184 | { |
255 | { |
185 | // SerialPutString("\n\rClr RxFifo "); |
256 | // wait that the tx fifo is empty |
Line 186... | Line 257... | ||
186 | while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoEmpty) != SET); |
257 | while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoEmpty) != SET); |
187 | while(SSP_GetFlagStatus(SSP1, SSP_FLAG_RxFifoNotEmpty) == SET) SSP_ReceiveData(SSP1); |
258 | // then empty the rx fifo by reading all the bytes that are available |
188 | // SerialPutString("done."); |
259 | while(SSP_GetFlagStatus(SSP1, SSP_FLAG_RxFifoNotEmpty) == SET) SSP_ReceiveData(SSP1); |
189 | } |
260 | } |
- | 261 | //________________________________________________________________________________________________________________________________________ |
|
190 | //________________________________________________________________________________________________________________________________________ |
262 | // Function: SSC_PutChar(u8 Byte); |
191 | // Funtion: SSC_PutChar(u8 Byte); |
- | |
192 | // |
- | |
193 | // Description: This function writes one byte to the SSC |
263 | // |
Line 194... | Line -... | ||
194 | // |
- | |
195 | // |
- | |
196 | // Returnvalue: none |
- | |
197 | //________________________________________________________________________________________________________________________________________ |
- | |
198 | - | ||
199 | void SSC_PutChar (u8 Byte) |
- | |
200 | { |
- | |
201 | //SerialPutString("\n\rPutChar."); sprintf(text," %02X ", Byte); SerialPutString(text); |
- | |
Line 202... | Line -... | ||
202 | while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoNotFull) != SET); |
- | |
203 | SSP_SendData(SSP1, Byte); |
- | |
204 | //SerialPutString("done."); |
- | |
205 | - | ||
206 | } |
- | |
Line 207... | Line -... | ||
207 | - | ||
208 | - | ||
209 | //________________________________________________________________________________________________________________________________________ |
- | |
210 | // Funtion: SSC_Disable(void); |
- | |
211 | // |
- | |
212 | // Description: This function enables chipselect of the sdcard (active low) |
- | |
213 | // |
- | |
214 | // |
- | |
Line 215... | Line -... | ||
215 | // Returnvalue: none |
- | |
216 | //________________________________________________________________________________________________________________________________________ |
- | |
217 | - | ||
218 | void SSC_Disable(void) |
- | |
219 | { |
- |