Subversion Repositories NaviCtrl

Rev

Rev 369 | Rev 379 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 369 Rev 378
1
/*#######################################################################################*/
1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + www.MikroKopter.com
5
// + www.MikroKopter.com
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7
// + Software Nutzungsbedingungen (english version: see below)
7
// + Software Nutzungsbedingungen (english version: see below)
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
22
// + des Mitverschuldens offen.
22
// + des Mitverschuldens offen.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
30
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
30
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// + Software LICENSING TERMS
32
// + Software LICENSING TERMS
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
35
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware 
35
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware 
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
37
// + The Software may only be used with the Licensor's products.
37
// + The Software may only be used with the Licensor's products.
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
40
// + agreement shall be the property of the Licensor.
40
// + agreement shall be the property of the Licensor.
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
43
// + The customer shall be responsible for taking reasonable precautions
43
// + The customer shall be responsible for taking reasonable precautions
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
53
// + #### END OF LICENSING TERMS ####
53
// + #### END OF LICENSING TERMS ####
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
#include "91x_lib.h"
56
#include "91x_lib.h"
57
#include "config.h"
57
#include "config.h"
58
#include "main.h"
58
#include "main.h"
59
 
59
 
60
//________________________________________________________________________________________________________________________________________
60
//________________________________________________________________________________________________________________________________________
61
// Module name:                 ssc.c 
61
// Module name:                 ssc.c 
62
// Compiler used:               avr-gcc 3.4.5
62
// Compiler used:               avr-gcc 3.4.5
63
// Last Modifikation:   24.07.2007
63
// Last Modifikation:   24.07.2007
64
// Version:                             1.02
64
// Version:                             1.02
65
// Authors:                             Stephan Busker  
65
// Authors:                             Stephan Busker  
66
// Description:                 Source files for access to the synchrnous serial channel.
66
// Description:                 Source files for access to the synchrnous serial channel.
67
//                                              Copyright (C) 2007 Stephan Busker
67
//                                              Copyright (C) 2007 Stephan Busker
68
//........................................................................................................................................
68
//........................................................................................................................................
69
// ext. Functions:              extern void                     SSC_Init(void);         
69
// ext. Functions:              extern void                     SSC_Init(void);         
70
//                                              extern u8                               SSC_GetChar (void);
70
//                                              extern u8                               SSC_GetChar (void);
71
//                                              extern void                     SSC_PutChar (u8 Byte);
71
//                                              extern void                     SSC_PutChar (u8 Byte);
72
//                                              extern void                     SSC_Disable(void); 
72
//                                              extern void                     SSC_Disable(void); 
73
//                                              extern void                     SSC_Enable(void); 
73
//                                              extern void                     SSC_Enable(void); 
74
//                                              extern void                             SSC_ClearRxFifo();
74
//                                              extern void                             SSC_ClearRxFifo();
75
//........................................................................................................................................
75
//........................................................................................................................................
76
// URL:                                 www.Mikro-Control.de
76
// URL:                                 www.Mikro-Control.de
77
// mailto:                              stephan.busker@mikro-control.de
77
// mailto:                              stephan.busker@mikro-control.de
78
//________________________________________________________________________________________________________________________________________
78
//________________________________________________________________________________________________________________________________________
79
 
79
 
80
 
80
 
81
 
81
 
82
//________________________________________________________________________________________________________________________________________
82
//________________________________________________________________________________________________________________________________________
83
// Function:    SSC_Enable(void);
83
// Function:    SSC_Enable(void);
84
// 
84
// 
85
// Description: This function enables chipselect of the sdcard (active low) 
85
// Description: This function enables chipselect of the sdcard (active low) 
86
//                              
86
//                              
87
//
87
//
88
// Returnvalue: none
88
// Returnvalue: none
89
//________________________________________________________________________________________________________________________________________
89
//________________________________________________________________________________________________________________________________________
90
 
90
 
91
void SSC_Enable(void)
91
void SSC_Enable(void)
92
{
92
{
93
        // enable chipselect of the sd-card (P5.4 -> SD-CS, active low). 
93
        // enable chipselect of the sd-card (P5.4 -> SD-CS, active low). 
94
        GPIO_WriteBit(GPIO5, GPIO_Pin_4 , Bit_RESET);
94
        GPIO_WriteBit(GPIO5, GPIO_Pin_4 , Bit_RESET);
95
}
95
}
96
 
96
 
97
//________________________________________________________________________________________________________________________________________
97
//________________________________________________________________________________________________________________________________________
98
// Function:    SSC_Disable(void);
98
// Function:    SSC_Disable(void);
99
// 
99
// 
100
// Description: This function disables chipselect of the sdcard (active low) 
100
// Description: This function disables chipselect of the sdcard (active low) 
101
//                              
101
//                              
102
//
102
//
103
// Returnvalue: none
103
// Returnvalue: none
104
//________________________________________________________________________________________________________________________________________
104
//________________________________________________________________________________________________________________________________________
105
 
105
 
106
void SSC_Disable(void)
106
void SSC_Disable(void)
107
{
107
{
108
        // disable chipselect of the sd-card (P5.4 -> SD-CS, active low). 
108
        // disable chipselect of the sd-card (P5.4 -> SD-CS, active low). 
109
        GPIO_WriteBit(GPIO5, GPIO_Pin_4 , Bit_SET);
109
        GPIO_WriteBit(GPIO5, GPIO_Pin_4 , Bit_SET);
110
}
110
}
111
 
111
 
112
 
112
 
113
//________________________________________________________________________________________________________________________________________
113
//________________________________________________________________________________________________________________________________________
114
// Function:    SSC_Init(void);
114
// Function:    SSC_Init(void);
115
// 
115
// 
116
// Description: This function initialises the synchronus serial channel to the sdcard. 
116
// Description: This function initialises the synchronus serial channel to the sdcard. 
117
//                              
117
//                              
118
//
118
//
119
// Returnvalue: none
119
// Returnvalue: none
120
//________________________________________________________________________________________________________________________________________
120
//________________________________________________________________________________________________________________________________________
121
 
121
 
122
void SSC_Init(void)
122
void SSC_Init(void)
123
{
123
{
124
        GPIO_InitTypeDef        GPIO_InitStructure;
124
        GPIO_InitTypeDef        GPIO_InitStructure;
125
        SSP_InitTypeDef         SSP_InitStructure;
125
        SSP_InitTypeDef         SSP_InitStructure;
126
        WIU_InitTypeDef         WIU_InitStructure;
126
//      WIU_InitTypeDef         WIU_InitStructure;
127
        // enable APB clock for SPI1
127
        // enable APB clock for SPI1
128
        SCU_APBPeriphClockConfig(__SSP1 ,ENABLE);
128
        SCU_APBPeriphClockConfig(__SSP1 ,ENABLE);
129
        // configure P5.4 -> SD-CS as an output pin
129
        // configure P5.4 -> SD-CS as an output pin
130
        GPIO_StructInit(&GPIO_InitStructure);
130
        GPIO_StructInit(&GPIO_InitStructure);
131
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
131
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
132
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
132
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
133
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
133
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
134
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable;
134
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable;
135
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1;
135
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1;
136
        GPIO_Init (GPIO5, &GPIO_InitStructure);
136
        GPIO_Init (GPIO5, &GPIO_InitStructure);
137
        // configure P3.4 -> SCK1 and P3.6 -> MOSI1 as an output pin
137
        // configure P3.4 -> SCK1 and P3.6 -> MOSI1 as an output pin
138
        GPIO_StructInit(&GPIO_InitStructure);
138
        GPIO_StructInit(&GPIO_InitStructure);
139
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
139
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
140
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6;
140
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6;
141
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
141
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
142
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
142
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
143
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2;
143
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2;
144
        GPIO_Init (GPIO3, &GPIO_InitStructure);
144
        GPIO_Init (GPIO3, &GPIO_InitStructure);
145
        // configure P3.5 <- MISO1 as an input pin
145
        // configure P3.5 <- MISO1 as an input pin
146
        GPIO_StructInit(&GPIO_InitStructure);
146
        GPIO_StructInit(&GPIO_InitStructure);
147
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
147
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
148
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
148
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
149
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull;
149
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull;
150
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
150
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
151
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1;
151
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1;
152
        GPIO_Init (GPIO3, &GPIO_InitStructure);
152
        GPIO_Init (GPIO3, &GPIO_InitStructure);
153
        // configure SPI1
153
        // configure SPI1
154
        SSP_DeInit(SSP1);
154
        SSP_DeInit(SSP1);
155
        SSP_StructInit(&SSP_InitStructure);
155
        SSP_StructInit(&SSP_InitStructure);
156
        SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola;
156
        SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola;
157
        SSP_InitStructure.SSP_Mode = SSP_Mode_Master;
157
        SSP_InitStructure.SSP_Mode = SSP_Mode_Master;
158
        SSP_InitStructure.SSP_CPHA = SSP_CPHA_1Edge;
158
        SSP_InitStructure.SSP_CPHA = SSP_CPHA_1Edge;
159
        SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low;
159
        SSP_InitStructure.SSP_CPOL = SSP_CPOL_Low;
160
        // Set Baud Rate (Prescaler)
160
        // Set Baud Rate (Prescaler)
161
        // bit rate is BRCLK/SSP_ClockPrescaler/(1+SSP_ClockRate))
161
        // bit rate is BRCLK/SSP_ClockPrescaler/(1+SSP_ClockRate))
162
        // With MSCLK = 48MHz/2 = BRCLK we get for the SPICLK = 24Mhz / 8 / (1+5) = 500 kHz
162
        // With MSCLK = 48MHz/2 = BRCLK we get for the SPICLK = 24Mhz / 8 / (1+5) = 500 kHz
163
        SSP_InitStructure.SSP_ClockRate = 5; //5
163
        SSP_InitStructure.SSP_ClockRate = 5; //5
164
        SSP_InitStructure.SSP_ClockPrescaler = 8;
164
        SSP_InitStructure.SSP_ClockPrescaler = 4; //8
165
        SSP_Init(SSP1, &SSP_InitStructure);
165
        SSP_Init(SSP1, &SSP_InitStructure);
166
        SSC_Disable();
166
        SSC_Disable();
167
        SSP_Cmd(SSP1, ENABLE);
167
        SSP_Cmd(SSP1, ENABLE);
168
 
168
 
169
        // Configure SD_SWITCH at pin GPIO5.3 as an external irq 11
169
        // Configure SD_SWITCH at pin GPIO5.3 as an external irq 11
170
 
170
 
171
        // configure the port
171
        // configure the port
172
        SCU_APBPeriphClockConfig(__GPIO5, ENABLE); // Enable the GPIO5 Clock    
172
        SCU_APBPeriphClockConfig(__GPIO5, ENABLE); // Enable the GPIO5 Clock    
173
    GPIO_InitStructure.GPIO_Direction =         GPIO_PinInput;
173
    GPIO_InitStructure.GPIO_Direction =         GPIO_PinInput;
174
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_3;
174
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_3;
175
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull ;
175
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull ;
176
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Disable;
176
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Disable;
177
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1;
177
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1;
178
    GPIO_Init(GPIO5, &GPIO_InitStructure);
178
    GPIO_Init(GPIO5, &GPIO_InitStructure);
179
       
179
       
180
/*
180
/*
181
        // configure the EXT11 interrupt line (P5.3) as an IRQ with the lowest priority
181
        // configure the EXT11 interrupt line (P5.3) as an IRQ with the lowest priority
182
        SCU_APBPeriphClockConfig(__WIU, ENABLE);
182
        SCU_APBPeriphClockConfig(__WIU, ENABLE);
183
        WIU_Cmd(ENABLE);
183
        WIU_Cmd(ENABLE);
184
        WIU_ClearITPendingBit(WIU_Line11);
184
        WIU_ClearITPendingBit(WIU_Line11);
185
        //WIU_DeInit();
185
        //WIU_DeInit();
186
        WIU_InitStructure.WIU_TriggerEdge = WIU_FallingEdge;
186
        WIU_InitStructure.WIU_TriggerEdge = WIU_FallingEdge;
187
        WIU_InitStructure.WIU_Line = WIU_Line11;
187
        WIU_InitStructure.WIU_Line = WIU_Line11;
188
        WIU_Init(&WIU_InitStructure);
188
        WIU_Init(&WIU_InitStructure);
189
        // The  EXTIT1_IRQHandler() is called every time the SD-Switch is activated (falling edge)
189
        // The  EXTIT1_IRQHandler() is called every time the SD-Switch is activated (falling edge)
190
        // by inserting an sd-card
190
        // by inserting an sd-card
191
        SCU_WakeUpLineConfig(11);
191
        SCU_WakeUpLineConfig(11);
192
    VIC_Config(EXTIT1_ITLine, VIC_IRQ, PRIORITY_SDSWITCH);
192
    VIC_Config(EXTIT1_ITLine, VIC_IRQ, PRIORITY_SDSWITCH);
193
    VIC_ITCmd(EXTIT1_ITLine, ENABLE);
193
    VIC_ITCmd(EXTIT1_ITLine, ENABLE);
194
*/
194
*/
195
}
195
}
196
 
196
 
197
void SSC_Deinit(void)
197
void SSC_Deinit(void)
198
{
198
{
199
        GPIO_InitTypeDef        GPIO_InitStructure;
199
        GPIO_InitTypeDef        GPIO_InitStructure;
200
 
200
 
201
        SSP_Cmd(SSP1, DISABLE);
201
        SSP_Cmd(SSP1, DISABLE);
202
        SSP_DeInit(SSP1);
202
        SSP_DeInit(SSP1);
203
 
203
 
204
        // configure P5.4 -> SD-CS as an input pin
204
        // configure P5.4 -> SD-CS as an input pin
205
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
205
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
206
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
206
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
207
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
207
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
208
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable;
208
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable;
209
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1;
209
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1;
210
        GPIO_Init (GPIO5, &GPIO_InitStructure);
210
        GPIO_Init (GPIO5, &GPIO_InitStructure);
211
        // configure P3.4 -> SCK1 and P3.6 -> MOSI1 as an input pin
211
        // configure P3.4 -> SCK1 and P3.6 -> MOSI1 as an input pin
212
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
212
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
213
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6;
213
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6;
214
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
214
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
215
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable;
215
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable;
216
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1;
216
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1;
217
        GPIO_Init (GPIO3, &GPIO_InitStructure);
217
        GPIO_Init (GPIO3, &GPIO_InitStructure);
218
        // configure P3.5 <- MISO1 as an input pin
218
        // configure P3.5 <- MISO1 as an input pin
219
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
219
        GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
220
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
220
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
221
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull;
221
        GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull;
222
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable;
222
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable;
223
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1;
223
        GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1;
224
        GPIO_Init (GPIO3, &GPIO_InitStructure);
224
        GPIO_Init (GPIO3, &GPIO_InitStructure);
225
 
225
 
226
        // disable APB clock for SPI1
226
        // disable APB clock for SPI1
227
        SCU_APBPeriphClockConfig(__SSP1 ,DISABLE);
227
        SCU_APBPeriphClockConfig(__SSP1 ,DISABLE);
228
 
228
 
229
        // configure the port of the SDC-Switch
229
        // configure the port of the SDC-Switch
230
        SCU_APBPeriphClockConfig(__GPIO5, ENABLE); // Enable the GPIO5 Clock    
230
        SCU_APBPeriphClockConfig(__GPIO5, ENABLE); // Enable the GPIO5 Clock    
231
    GPIO_InitStructure.GPIO_Direction =         GPIO_PinInput;
231
    GPIO_InitStructure.GPIO_Direction =         GPIO_PinInput;
232
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_3;
232
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_3;
233
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull ;
233
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull ;
234
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Disable;
234
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Disable;
235
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1;
235
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1;
236
    GPIO_Init(GPIO5, &GPIO_InitStructure);
236
    GPIO_Init(GPIO5, &GPIO_InitStructure);
237
}
237
}
238
 
238
 
239
 
239
 
240
//________________________________________________________________________________________________________________________________________
240
//________________________________________________________________________________________________________________________________________
241
// Function:    SSC_GetChar(void);
241
// Function:    SSC_GetChar(void);
242
// 
242
// 
243
// Description: This function reads one byte from the SSC
243
// Description: This function reads one byte from the SSC
244
//                              
244
//                              
245
//
245
//
246
// Returnvalue: the byte received.
246
// Returnvalue: the byte received.
247
//________________________________________________________________________________________________________________________________________
247
//________________________________________________________________________________________________________________________________________
248
 
248
 
249
u8 SSC_GetChar (void)
249
u8 SSC_GetChar (void)
250
{
250
{
251
        u8 Byte = 0;
251
        u8 Byte = 0;
252
        while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoNotFull) != SET && (SD_WatchDog)); // wait for space in the tx fifo
252
        while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoNotFull) != SET);  // wait for space in the tx fifo
253
        SSP_SendData(SSP1, 0xFF);// send dymmy byte (0xFF) as master to receive a byte from the slave
253
        SSP_SendData(SSP1, 0xFF);// send dymmy byte (0xFF) as master to receive a byte from the slave
254
        while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoEmpty) != SET && (SD_WatchDog)); // wait for the byte to be sent
254
        while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoEmpty) != SET); // wait for the byte to be sent
255
        Byte = SSP_ReceiveData(SSP1); // read the byte transmitted from the slave
255
        Byte = SSP_ReceiveData(SSP1); // read the byte transmitted from the slave
256
        return (Byte);
256
        return (Byte);
257
}
257
}
258
 
258
 
259
//________________________________________________________________________________________________________________________________________
259
//________________________________________________________________________________________________________________________________________
260
void SSC_ClearRxFifo (void)
260
void SSC_ClearRxFifo (void)
261
{
261
{
262
        // wait that the tx fifo is empty
262
        // wait that the tx fifo is empty
263
        while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoEmpty) != SET && (SD_WatchDog));
263
        while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoEmpty) != SET);
264
        // then empty the rx fifo by reading all the bytes that are available
264
        // then empty the rx fifo by reading all the bytes that are available
265
        while(SSP_GetFlagStatus(SSP1, SSP_FLAG_RxFifoNotEmpty) == SET && (SD_WatchDog)) SSP_ReceiveData(SSP1);
265
        while(SSP_GetFlagStatus(SSP1, SSP_FLAG_RxFifoNotEmpty) == SET) SSP_ReceiveData(SSP1);
266
}
266
}
267
//________________________________________________________________________________________________________________________________________
267
//________________________________________________________________________________________________________________________________________
268
// Function:    SSC_PutChar(u8 Byte);
268
// Function:    SSC_PutChar(u8 Byte);
269
// 
269
// 
270
// Description: This function writes one byte to the SSC 
270
// Description: This function writes one byte to the SSC 
271
//                              
271
//                              
272
//
272
//
273
// Returnvalue: none
273
// Returnvalue: none
274
//________________________________________________________________________________________________________________________________________
274
//________________________________________________________________________________________________________________________________________
275
 
275
 
276
void SSC_PutChar (u8 Byte)
276
void SSC_PutChar (u8 Byte)
277
{
277
{
278
        // wait for some space in the tx fifo
278
        // wait for some space in the tx fifo
279
        while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoNotFull) != SET && (SD_WatchDog));
279
        while(SSP_GetFlagStatus(SSP1, SSP_FLAG_TxFifoNotFull) != SET);
280
        // put the byte to send in the tx fifo
280
        // put the byte to send in the tx fifo
281
        SSP_SendData(SSP1, Byte);
281
        SSP_SendData(SSP1, Byte);
282
}
282
}
283
 
283
 
284
 
284
 
285
 
285
 
286
 
286
 
287
 
287
 
288
 
288
 
289
 
289
 
290
 
290
 
291
 
291