Subversion Repositories NaviCtrl

Rev

Rev 1 | Rev 41 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ingob 1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
6
// + Nur für den privaten Gebrauch
7
// + FOR NON COMMERCIAL USE ONLY
8
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), 
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. 
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt 
13
// + bzgl. der Nutzungsbedingungen aufzunehmen. 
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
15
// + Verkauf von Luftbildaufnahmen, usw.
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, 
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
22
// + eindeutig als Ursprung verlinkt werden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
25
// + Benutzung auf eigene Gefahr
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
28
// + Die PORTIERUNG der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur 
29
// + mit unserer Zustimmung zulässig
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice, 
34
// + this list of conditions and the following disclaimer.
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
36
// +     from this software without specific prior written permission.
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted 
38
// +     for non-commercial use (directly or indirectly)
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted 
40
// +     with our written permission
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be 
42
// +     clearly linked as origin 
43
// +   * PORTING this software (or part of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed
44
//
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
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
55
// +  POSSIBILITY OF SUCH DAMAGE. 
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24 StephanB 57
#include "91x_lib.h"
1 ingob 58
 
59
//________________________________________________________________________________________________________________________________________
24 StephanB 60
// Module name:                 ssc.c 
1 ingob 61
// Compiler used:               avr-gcc 3.4.5
62
// Last Modifikation:   24.07.2007
63
// Version:                             1.02
64
// Authors:                             Stephan Busker  
65
// Description:                 Source files for access to the synchrnous serial channel.
66
//                                              Copyright (C) 2007 Stephan Busker
67
//........................................................................................................................................
24 StephanB 68
// ext. Functions:              extern void                     SSC_Init(void);         
69
//                                              extern u8                               SSC_GetChar (void);
1 ingob 70
//                                              extern void                     SSC_PutChar (u8 Byte);
71
//                                              extern void                     SSC_Disable(void); 
72
//                                              extern void                     SSC_Enable(void); 
24 StephanB 73
//                                              extern void                             SSC_ClearRxFifo();
1 ingob 74
//........................................................................................................................................
75
// URL:                                 www.Mikro-Control.de
76
// mailto:                              stephan.busker@mikro-control.de
77
//________________________________________________________________________________________________________________________________________
78
 
79
 
80
 
24 StephanB 81
//________________________________________________________________________________________________________________________________________
82
// Function:    SSC_Enable(void);
83
// 
84
// Description: This function enables chipselect of the sdcard (active low) 
85
//                              
86
//
87
// Returnvalue: none
88
//________________________________________________________________________________________________________________________________________
1 ingob 89
 
24 StephanB 90
void SSC_Enable(void)
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
 
1 ingob 96
//________________________________________________________________________________________________________________________________________
24 StephanB 97
// Function:    SSC_Disable(void);
1 ingob 98
// 
24 StephanB 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). 
108
        GPIO_WriteBit(GPIO5, GPIO_Pin_4 , Bit_SET);
109
}
110
 
111
 
112
//________________________________________________________________________________________________________________________________________
113
// Function:    SSC_Init(void);
114
// 
1 ingob 115
// Description: This function initialises the synchronus serial channel to the sdcard. 
116
//                              
117
//
118
// Returnvalue: none
119
//________________________________________________________________________________________________________________________________________
120
 
121
void SSC_Init(void)
122
{
24 StephanB 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);
1 ingob 164
 
24 StephanB 165
        // Configure SD_SWITCH at pin GPIO5.3 as an external irq 11
1 ingob 166
 
24 StephanB 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);
188
    VIC_ITCmd(EXTIT1_ITLine, ENABLE);
189
}
1 ingob 190
 
24 StephanB 191
void SSC_Deinit(void)
192
{
193
        GPIO_InitTypeDef        GPIO_InitStructure;
1 ingob 194
 
24 StephanB 195
        SSP_Cmd(SSP1, DISABLE);
196
        SSP_DeInit(SSP1);
1 ingob 197
 
24 StephanB 198
        // configure P5.4 -> SD-CS as an input pin
199
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
200
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
201
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
202
        GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable;
203
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1;
204
        GPIO_Init (GPIO5, &GPIO_InitStructure);
205
        // configure P3.4 -> SCK1 and P3.6 -> MOSI1 as an input pin
206
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
207
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6;
208
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
209
        GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable;
210
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1;
211
        GPIO_Init (GPIO3, &GPIO_InitStructure);
212
        // configure P3.5 <- MISO1 as an input pin
213
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
214
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
215
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull;
216
        GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable;
217
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1;
218
        GPIO_Init (GPIO3, &GPIO_InitStructure);
219
 
220
        // disable APB clock for SPI1
221
        SCU_APBPeriphClockConfig(__SSP1 ,DISABLE);
222
 
223
        // configure the port of the SDC-Switch
224
        SCU_APBPeriphClockConfig(__GPIO5, ENABLE); // Enable the GPIO5 Clock    
225
    GPIO_InitStructure.GPIO_Direction =         GPIO_PinInput;
226
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_3;
227
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull ;
228
        GPIO_InitStructure.GPIO_IPConnected =   GPIO_IPConnected_Disable;
229
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1;
230
    GPIO_Init(GPIO5, &GPIO_InitStructure);
1 ingob 231
}
232
 
233
 
234
//________________________________________________________________________________________________________________________________________
24 StephanB 235
// Function:    SSC_GetChar(void);
1 ingob 236
// 
237
// Description: This function reads one byte from the SSC
238
//                              
239
//
240
// Returnvalue: the byte received.
241
//________________________________________________________________________________________________________________________________________
242
 
243
u8 SSC_GetChar (void)
244
{
245
        u8 Byte = 0;
24 StephanB 246
        while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoNotFull) != SET);  // wait for space in the tx fifo
247
        SSP_SendData(SSP1, 0xFF);// send dymmy byte (0xFF) as master to receive a byte from the slave
248
        while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoEmpty) != SET); // wait for the byte to be sent
249
        Byte = SSP_ReceiveData(SSP1); // read the byte transmitted from the slave
1 ingob 250
        return (Byte);
251
}
252
 
253
//________________________________________________________________________________________________________________________________________
254
void SSC_ClearRxFifo (void)
255
{
24 StephanB 256
        // wait that the tx fifo is empty
1 ingob 257
        while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoEmpty) != SET);
24 StephanB 258
        // then empty the rx fifo by reading all the bytes that are available
1 ingob 259
        while(SSP_GetFlagStatus(SSP1, SSP_FLAG_RxFifoNotEmpty) == SET) SSP_ReceiveData(SSP1);
260
}
261
//________________________________________________________________________________________________________________________________________
24 StephanB 262
// Function:    SSC_PutChar(u8 Byte);
1 ingob 263
// 
264
// Description: This function writes one byte to the SSC 
265
//                              
266
//
267
// Returnvalue: none
268
//________________________________________________________________________________________________________________________________________
269
 
270
void SSC_PutChar (u8 Byte)
271
{
24 StephanB 272
        // wait for some space in the tx fifo
1 ingob 273
        while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoNotFull) != SET);
24 StephanB 274
        // put the byte to send in the tx fifo
1 ingob 275
        SSP_SendData(SSP1, Byte);
276
}
277
 
278
 
279
 
280
 
281
 
282
 
283
 
284