Rev 134 | Rev 139 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 134 | Rev 136 | ||
---|---|---|---|
1 | /*#######################################################################################*/ |
1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
3 | /*#######################################################################################*/ |
3 | /*#######################################################################################*/ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 | // + Copyright (c) 2008 Ingo Busker, Holger Buss |
5 | // + Copyright (c) 2008 Ingo Busker, Holger Buss |
6 | // + Nur für den privaten Gebrauch |
6 | // + Nur für den privaten Gebrauch |
7 | // + FOR NON COMMERCIAL USE ONLY |
7 | // + FOR NON COMMERCIAL USE ONLY |
8 | // + www.MikroKopter.com |
8 | // + www.MikroKopter.com |
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
10 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
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. |
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 |
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
15 | // + Verkauf von Luftbildaufnahmen, usw. |
15 | // + Verkauf von Luftbildaufnahmen, usw. |
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
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 |
18 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
20 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
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" |
21 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
22 | // + eindeutig als Ursprung verlinkt werden |
22 | // + eindeutig als Ursprung verlinkt werden |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
25 | // + Benutzung auf eigene Gefahr |
25 | // + Benutzung auf eigene Gefahr |
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
28 | // + Die PORTIERUNG der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
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 |
29 | // + mit unserer Zustimmung zulässig |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
34 | // + this list of conditions and the following disclaimer. |
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 |
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. |
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 |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
38 | // + for non-commercial use (directly or indirectly) |
38 | // + for non-commercial use (directly or indirectly) |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
40 | // + with our written permission |
40 | // + with our written permission |
41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
42 | // + clearly linked as origin |
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 |
43 | // + * PORTING this software (or part of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed |
44 | // |
44 | // |
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
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 |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
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 |
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 <stdio.h> |
57 | #include <stdio.h> |
58 | #include <stdarg.h> |
58 | #include <stdarg.h> |
59 | #include <string.h> |
59 | #include <string.h> |
60 | 60 | ||
61 | #include "91x_lib.h" |
61 | #include "91x_lib.h" |
- | 62 | #include "config.h" |
|
62 | #include "menu.h" |
63 | #include "menu.h" |
63 | #include "printf_P.h" |
64 | #include "printf_P.h" |
64 | #include "GPS.h" |
65 | #include "GPS.h" |
65 | #include "i2c.h" |
66 | #include "i2c.h" |
66 | #include "uart0.h" |
67 | #include "uart0.h" |
67 | #include "uart1.h" |
68 | #include "uart1.h" |
68 | #include "uart2.h" |
69 | #include "uart2.h" |
69 | #include "timer1.h" |
70 | #include "timer1.h" |
70 | #include "main.h" |
71 | #include "main.h" |
71 | #include "waypoints.h" |
72 | #include "waypoints.h" |
72 | #include "mkprotocol.h" |
73 | #include "mkprotocol.h" |
73 | 74 | ||
74 | #define FALSE 0 |
75 | #define FALSE 0 |
75 | #define TRUE 1 |
76 | #define TRUE 1 |
76 | 77 | ||
77 | 78 | ||
78 | u8 UART1_Request_VersionInfo = FALSE; |
79 | u8 UART1_Request_VersionInfo = FALSE; |
79 | u8 UART1_Request_SendFollowMe = FALSE; |
80 | u8 UART1_Request_SendFollowMe = FALSE; |
80 | u8 UART1_Request_ExternalControl= FALSE; |
81 | u8 UART1_Request_ExternalControl= FALSE; |
81 | u8 UART1_Request_Display = FALSE; |
82 | u8 UART1_Request_Display = FALSE; |
82 | u8 UART1_Request_Display1 = FALSE; |
83 | u8 UART1_Request_Display1 = FALSE; |
83 | u8 UART1_Request_DebugData = FALSE; |
84 | u8 UART1_Request_DebugData = FALSE; |
84 | u8 UART1_Request_DebugLabel = 255; |
85 | u8 UART1_Request_DebugLabel = 255; |
85 | u8 UART1_Request_NaviData = FALSE; |
86 | u8 UART1_Request_NaviData = FALSE; |
86 | u8 UART1_Request_ErrorMessage = FALSE; |
87 | u8 UART1_Request_ErrorMessage = FALSE; |
87 | u8 UART1_Request_NewWaypoint = FALSE; |
88 | u8 UART1_Request_NewWaypoint = FALSE; |
88 | u8 UART1_Request_ReadWaypoint = 255; |
89 | u8 UART1_Request_ReadWaypoint = 255; |
89 | u8 UART1_Request_Data3D = FALSE; |
90 | u8 UART1_Request_Data3D = FALSE; |
90 | u8 UART1_Request_Echo = FALSE; |
91 | u8 UART1_Request_Echo = FALSE; |
91 | u8 UART1_DisplayLine = 0; |
92 | u8 UART1_DisplayLine = 0; |
92 | u8 UART1_ConfirmFrame = 0; |
93 | u8 UART1_ConfirmFrame = 0; |
93 | 94 | ||
94 | UART_TypeDef *DebugUART = UART1; |
95 | UART_TypeDef *DebugUART = UART1; |
95 | - | ||
96 | #define UART1_BAUD_RATE 57600 //Baud Rate for the serial interfaces |
- | |
97 | 96 | ||
98 | // the tx buffer |
97 | // the tx buffer |
99 | #define UART1_TX_BUFFER_LEN 150 |
98 | #define UART1_TX_BUFFER_LEN 150 |
100 | u8 UART1_tbuffer[UART1_TX_BUFFER_LEN]; |
99 | u8 UART1_tbuffer[UART1_TX_BUFFER_LEN]; |
101 | Buffer_t UART1_tx_buffer; |
100 | Buffer_t UART1_tx_buffer; |
102 | 101 | ||
103 | // the rx buffer |
102 | // the rx buffer |
104 | #define UART1_RX_BUFFER_LEN 150 |
103 | #define UART1_RX_BUFFER_LEN 150 |
105 | u8 UART1_rbuffer[UART1_RX_BUFFER_LEN]; |
104 | u8 UART1_rbuffer[UART1_RX_BUFFER_LEN]; |
106 | Buffer_t UART1_rx_buffer; |
105 | Buffer_t UART1_rx_buffer; |
107 | 106 | ||
108 | volatile u8 SerialLinkOkay = 0; |
107 | volatile u8 SerialLinkOkay = 0; |
109 | 108 | ||
110 | u8 text[100]; |
109 | u8 text[100]; |
111 | 110 | ||
112 | const u8 ANALOG_LABEL[32][16] = |
111 | const u8 ANALOG_LABEL[32][16] = |
113 | { |
112 | { |
114 | //1234567890123456 |
113 | //1234567890123456 |
115 | "AngleNick ", //0 |
114 | "AngleNick ", //0 |
116 | "AngleRoll ", |
115 | "AngleRoll ", |
117 | "AccNick ", |
116 | "AccNick ", |
118 | "AccRoll ", |
117 | "AccRoll ", |
119 | " ", |
118 | " ", |
120 | "MK-Flags ", //5 |
119 | "MK-Flags ", //5 |
121 | "NC-Flags ", |
120 | "NC-Flags ", |
122 | "NickServo ", |
121 | "NickServo ", |
123 | "RollServo ", |
122 | "RollServo ", |
124 | "GPS Data ", |
123 | "GPS Data ", |
125 | "CompassHeading ", //10 |
124 | "CompassHeading ", //10 |
126 | "GyroHeading ", |
125 | "GyroHeading ", |
127 | "SPI Error ", |
126 | "SPI Error ", |
128 | "SPI Okay ", |
127 | "SPI Okay ", |
129 | "I2C Error ", |
128 | "I2C Error ", |
130 | "I2C Okay ", //15 |
129 | "I2C Okay ", //15 |
131 | " ",// "FC_Kalman_K ", |
130 | " ",// "FC_Kalman_K ", |
132 | "ACC_Speed_N ", |
131 | "ACC_Speed_N ", |
133 | "ACC_Speed_E ", |
132 | "ACC_Speed_E ", |
134 | " ",// "GPS ACC ", |
133 | " ",// "GPS ACC ", |
135 | " ",// "MAXDrift ", //20 |
134 | " ",// "MAXDrift ", //20 |
136 | "N_Speed ", |
135 | "N_Speed ", |
137 | "E_Speed ", |
136 | "E_Speed ", |
138 | "P-Part ", |
137 | "P-Part ", |
139 | "I-Part ", |
138 | "I-Part ", |
140 | "D-Part ",//25 |
139 | "D-Part ",//25 |
141 | "PID-Part ", |
140 | "PID-Part ", |
142 | "Distance N ", |
141 | "Distance N ", |
143 | "Distance E ", |
142 | "Distance E ", |
144 | "GPS_Nick ", |
143 | "GPS_Nick ", |
145 | "GPS_Roll ", //30 |
144 | "GPS_Roll ", //30 |
146 | "Used_Sats " |
145 | "Used_Sats " |
147 | }; |
146 | }; |
148 | 147 | ||
149 | DebugOut_t DebugOut; |
148 | DebugOut_t DebugOut; |
150 | ExternControl_t ExternControl; |
149 | ExternControl_t ExternControl; |
151 | UART_VersionInfo_t UART_VersionInfo; |
150 | UART_VersionInfo_t UART_VersionInfo; |
152 | NaviData_t NaviData; |
151 | NaviData_t NaviData; |
153 | Waypoint_t FollowMe; |
152 | Waypoint_t FollowMe; |
154 | Data3D_t Data3D; |
153 | Data3D_t Data3D; |
155 | u16 Echo; // 2 bytes recieved will be sent back as echo |
154 | u16 Echo; // 2 bytes recieved will be sent back as echo |
156 | 155 | ||
157 | u32 UART1_DebugData_Timer; |
156 | u32 UART1_DebugData_Timer; |
158 | u32 UART1_DebugData_Interval = 5000; // in ms |
157 | u32 UART1_DebugData_Interval = 5000; // in ms |
159 | u32 UART1_NaviData_Timer; |
158 | u32 UART1_NaviData_Timer; |
160 | u32 UART1_NaviData_Interval = 5000; // in ms |
159 | u32 UART1_NaviData_Interval = 5000; // in ms |
161 | u32 UART1_Data3D_Timer = 0; // in ms |
160 | u32 UART1_Data3D_Timer = 0; // in ms |
162 | u32 UART1_Data3D_Interval = 0; |
161 | u32 UART1_Data3D_Interval = 0; |
163 | 162 | ||
164 | /********************************************************/ |
163 | /********************************************************/ |
165 | /* Initialization the UART1 */ |
164 | /* Initialization the UART1 */ |
166 | /********************************************************/ |
165 | /********************************************************/ |
167 | void UART1_Init (void) |
166 | void UART1_Init (void) |
168 | { |
167 | { |
169 | GPIO_InitTypeDef GPIO_InitStructure; |
168 | GPIO_InitTypeDef GPIO_InitStructure; |
170 | UART_InitTypeDef UART_InitStructure; |
169 | UART_InitTypeDef UART_InitStructure; |
171 | 170 | ||
172 | SCU_APBPeriphClockConfig(__UART1, ENABLE); // Enable the UART1 Clock |
171 | SCU_APBPeriphClockConfig(__UART1, ENABLE); // Enable the UART1 Clock |
173 | SCU_APBPeriphClockConfig(__GPIO3, ENABLE); // Enable the GPIO3 Clock |
172 | SCU_APBPeriphClockConfig(__GPIO3, ENABLE); // Enable the GPIO3 Clock |
174 | 173 | ||
175 | /*Configure UART1_Rx pin GPIO3.2*/ |
174 | /*Configure UART1_Rx pin GPIO3.2*/ |
176 | GPIO_StructInit(&GPIO_InitStructure); |
175 | GPIO_StructInit(&GPIO_InitStructure); |
177 | GPIO_InitStructure.GPIO_Direction = GPIO_PinInput; |
176 | GPIO_InitStructure.GPIO_Direction = GPIO_PinInput; |
178 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; |
177 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; |
179 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull; |
178 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull; |
180 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable; |
179 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable; |
181 | GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; // UART1_RxD |
180 | GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; // UART1_RxD |
182 | GPIO_Init(GPIO3, &GPIO_InitStructure); |
181 | GPIO_Init(GPIO3, &GPIO_InitStructure); |
183 | 182 | ||
184 | /*Configure UART1_Tx pin GPIO3.3*/ |
183 | /*Configure UART1_Tx pin GPIO3.3*/ |
185 | GPIO_StructInit(&GPIO_InitStructure); |
184 | GPIO_StructInit(&GPIO_InitStructure); |
186 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
185 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
187 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; |
186 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; |
188 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull; |
187 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull; |
189 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; // UART1_TX |
188 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; // UART1_TX |
190 | GPIO_Init(GPIO3, &GPIO_InitStructure); |
189 | GPIO_Init(GPIO3, &GPIO_InitStructure); |
191 | 190 | ||
192 | /* UART1 configured as follow: |
191 | /* UART1 configured as follow: |
193 | - Word Length = 8 Bits |
192 | - Word Length = 8 Bits |
194 | - One Stop Bit |
193 | - One Stop Bit |
195 | - No parity |
194 | - No parity |
196 | - BaudRate = 57600 baud |
195 | - BaudRate = 57600 baud |
197 | - Hardware flow control Disabled |
196 | - Hardware flow control Disabled |
198 | - Receive and transmit enabled |
197 | - Receive and transmit enabled |
199 | - Receive and transmit FIFOs are Disabled |
198 | - Receive and transmit FIFOs are Disabled |
200 | */ |
199 | */ |
201 | UART_StructInit(&UART_InitStructure); |
200 | UART_StructInit(&UART_InitStructure); |
202 | UART_InitStructure.UART_WordLength = UART_WordLength_8D; |
201 | UART_InitStructure.UART_WordLength = UART_WordLength_8D; |
203 | UART_InitStructure.UART_StopBits = UART_StopBits_1; |
202 | UART_InitStructure.UART_StopBits = UART_StopBits_1; |
204 | UART_InitStructure.UART_Parity = UART_Parity_No ; |
203 | UART_InitStructure.UART_Parity = UART_Parity_No ; |
205 | UART_InitStructure.UART_BaudRate = UART1_BAUD_RATE; |
204 | UART_InitStructure.UART_BaudRate = UART1_BAUD_RATE; |
206 | UART_InitStructure. UART_HardwareFlowControl = UART_HardwareFlowControl_None; |
205 | UART_InitStructure. UART_HardwareFlowControl = UART_HardwareFlowControl_None; |
207 | UART_InitStructure.UART_Mode = UART_Mode_Tx_Rx; |
206 | UART_InitStructure.UART_Mode = UART_Mode_Tx_Rx; |
208 | UART_InitStructure.UART_FIFO = UART_FIFO_Enable; |
207 | UART_InitStructure.UART_FIFO = UART_FIFO_Enable; |
209 | UART_InitStructure.UART_TxFIFOLevel = UART_FIFOLevel_1_2; |
208 | UART_InitStructure.UART_TxFIFOLevel = UART_FIFOLevel_1_2; |
210 | UART_InitStructure.UART_RxFIFOLevel = UART_FIFOLevel_1_2; |
209 | UART_InitStructure.UART_RxFIFOLevel = UART_FIFOLevel_1_8; |
211 | 210 | ||
212 | UART_DeInit(UART1); // reset uart 1 to default |
211 | UART_DeInit(UART1); // reset uart 1 to default |
213 | UART_Init(UART1, &UART_InitStructure); // initialize uart 1 |
212 | UART_Init(UART1, &UART_InitStructure); // initialize uart 1 |
214 | // enable uart 1 interrupts selective |
213 | // enable uart 1 interrupts selective |
215 | UART_ITConfig(UART1, UART_IT_Receive | UART_IT_ReceiveTimeOut, ENABLE); |
214 | UART_ITConfig(UART1, UART_IT_Receive | UART_IT_ReceiveTimeOut, ENABLE); |
216 | UART_Cmd(UART1, ENABLE); // enable uart 1 |
215 | UART_Cmd(UART1, ENABLE); // enable uart 1 |
217 | // configure the uart 1 interupt line as an IRQ with priority 4 (0 is highest) |
216 | // configure the uart 1 interupt line |
218 | VIC_Config(UART1_ITLine, VIC_IRQ, 4); |
217 | VIC_Config(UART1_ITLine, VIC_IRQ, PRIORITY_UART1); |
219 | // enable the uart 1 IRQ |
218 | // enable the uart 1 IRQ |
220 | VIC_ITCmd(UART1_ITLine, ENABLE); |
219 | VIC_ITCmd(UART1_ITLine, ENABLE); |
221 | // initialize the debug timer |
220 | // initialize the debug timer |
222 | UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval); |
221 | UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval); |
223 | UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval)+500; |
222 | UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval)+500; |
224 | 223 | ||
225 | // initialize txd buffer |
224 | // initialize txd buffer |
226 | Buffer_Init(&UART1_tx_buffer, UART1_tbuffer, UART1_TX_BUFFER_LEN); |
225 | Buffer_Init(&UART1_tx_buffer, UART1_tbuffer, UART1_TX_BUFFER_LEN); |
227 | 226 | ||
228 | // initialize rxd buffer |
227 | // initialize rxd buffer |
229 | Buffer_Init(&UART1_rx_buffer, UART1_rbuffer, UART1_RX_BUFFER_LEN); |
228 | Buffer_Init(&UART1_rx_buffer, UART1_rbuffer, UART1_RX_BUFFER_LEN); |
230 | 229 | ||
231 | // Fill Version Info Structure |
230 | // Fill Version Info Structure |
232 | UART_VersionInfo.SWMajor = VERSION_MAJOR; |
231 | UART_VersionInfo.SWMajor = VERSION_MAJOR; |
233 | UART_VersionInfo.SWMinor = VERSION_MINOR; |
232 | UART_VersionInfo.SWMinor = VERSION_MINOR; |
234 | UART_VersionInfo.SWPatch = VERSION_PATCH; |
233 | UART_VersionInfo.SWPatch = VERSION_PATCH; |
235 | UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR; |
234 | UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR; |
236 | UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR; |
235 | UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR; |
237 | 236 | ||
238 | NaviData.Version = NAVIDATA_VERSION; |
237 | NaviData.Version = NAVIDATA_VERSION; |
239 | 238 | ||
240 | UART1_PutString("\r\nUART1 init...ok"); |
239 | UART1_PutString("\r\nUART1 init...ok"); |
241 | } |
240 | } |
242 | 241 | ||
243 | 242 | ||
244 | /****************************************************************/ |
243 | /****************************************************************/ |
245 | /* USART1 receiver ISR */ |
244 | /* USART1 receiver ISR */ |
246 | /****************************************************************/ |
245 | /****************************************************************/ |
247 | void UART1_IRQHandler(void) |
246 | void UART1_IRQHandler(void) |
248 | { |
247 | { |
249 | static u8 abortState = 0; |
248 | static u8 abortState = 0; |
250 | u8 c; |
249 | u8 c; |
251 | 250 | ||
252 | if((UART_GetITStatus(UART1, UART_IT_Receive) != RESET) || (UART_GetITStatus(UART1, UART_IT_ReceiveTimeOut) != RESET) ) |
251 | if((UART_GetITStatus(UART1, UART_IT_Receive) != RESET) || (UART_GetITStatus(UART1, UART_IT_ReceiveTimeOut) != RESET) ) |
253 | { |
252 | { |
254 | // clear the pending bits |
253 | // clear the pending bits |
255 | UART_ClearITPendingBit(UART1, UART_IT_Receive); |
254 | UART_ClearITPendingBit(UART1, UART_IT_Receive); |
256 | UART_ClearITPendingBit(UART1, UART_IT_ReceiveTimeOut); |
255 | UART_ClearITPendingBit(UART1, UART_IT_ReceiveTimeOut); |
257 | // if debug UART is not UART1 |
256 | // if debug UART is not UART1 |
258 | if (DebugUART != UART1) |
257 | if (DebugUART != UART1) |
259 | { // forward received data to the debug UART tx buffer |
258 | { // forward received data to the debug UART tx buffer |
260 | while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET) |
259 | while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET) |
261 | { |
260 | { |
262 | // move the byte from the rx buffer of UART1 to the tx buffer of DebugUART |
261 | // move the byte from the rx buffer of UART1 to the tx buffer of DebugUART |
263 | c = UART_ReceiveData(UART1); |
262 | c = UART_ReceiveData(UART1); |
264 | 263 | ||
265 | // check for abort condition (ESC ESC 0x55 0xAA 0x00) |
264 | // check for abort condition (ESC ESC 0x55 0xAA 0x00) |
266 | switch (abortState) |
265 | switch (abortState) |
267 | { |
266 | { |
268 | case 0: if (c == 27) abortState++; |
267 | case 0: if (c == 27) abortState++; |
269 | break; |
268 | break; |
270 | case 1: if (c == 27) abortState++; else abortState = 0; |
269 | case 1: if (c == 27) abortState++; else abortState = 0; |
271 | break; |
270 | break; |
272 | case 2: if (c == 0x55) abortState++; else abortState = 0; |
271 | case 2: if (c == 0x55) abortState++; else abortState = 0; |
273 | break; |
272 | break; |
274 | case 3: if (c == 0xAA) abortState++; else abortState = 0; |
273 | case 3: if (c == 0xAA) abortState++; else abortState = 0; |
275 | break; |
274 | break; |
276 | case 4: if (c == 0x00) |
275 | case 4: if (c == 0x00) |
277 | { |
276 | { |
278 | DebugUART = UART1; |
277 | DebugUART = UART1; |
279 | UART0_Connect_to_MKGPS(); |
278 | UART0_Connect_to_MKGPS(); |
280 | } |
279 | } |
281 | abortState = 0; |
280 | abortState = 0; |
282 | break; |
281 | break; |
283 | } |
282 | } |
284 | // if the Debug uart is not UART1, rederect input to the Debug UART |
283 | // if the Debug uart is not UART1, redirect input to the Debug UART |
285 | if (DebugUART != UART1) |
284 | if (DebugUART != UART1) |
286 | { |
285 | { |
287 | // wait for space in the tx buffer of the DebugUART |
286 | // wait for space in the tx buffer of the DebugUART |
288 | while(UART_GetFlagStatus(DebugUART, UART_FLAG_TxFIFOFull) == SET) {}; |
287 | while(UART_GetFlagStatus(DebugUART, UART_FLAG_TxFIFOFull) == SET) {}; |
289 | // move byte to the tx fifi of the debug uart |
288 | // move byte to the tx fifi of the debug uart |
290 | UART_SendData(DebugUART, c); |
289 | UART_SendData(DebugUART, c); |
291 | } |
290 | } |
292 | } |
291 | } |
293 | } |
292 | } |
294 | else // DebugUART == UART1 (normal operation) |
293 | else // DebugUART == UART1 (normal operation) |
295 | { |
294 | { |
296 | while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET) |
295 | while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET) |
297 | { // some byes in the fifo and rxd buffer not locked |
296 | { // some byes in the fifo and rxd buffer not locked |
298 | // get byte from fifo |
297 | // get byte from fifo |
299 | c = UART_ReceiveData(UART1); |
298 | c = UART_ReceiveData(UART1); |
300 | MKProtocol_CollectSerialFrame(&UART1_rx_buffer, c); |
299 | MKProtocol_CollectSerialFrame(&UART1_rx_buffer, c); |
301 | } // some byes in the fifo and rxd buffer not locked |
300 | } // some byes in the fifo and rxd buffer not locked |
302 | } // eof DebugUart = UART1 |
301 | } // eof DebugUart = UART1 |
303 | } |
302 | } |
304 | } |
303 | } |
305 | 304 | ||
306 | /**************************************************************/ |
305 | /**************************************************************/ |
307 | /* Process incomming data from debug uart */ |
306 | /* Process incomming data from debug uart */ |
308 | /**************************************************************/ |
307 | /**************************************************************/ |
309 | void UART1_ProcessRxData(void) |
308 | void UART1_ProcessRxData(void) |
310 | { |
309 | { |
311 | SerialMsg_t SerialMsg; |
310 | SerialMsg_t SerialMsg; |
312 | // if data in the rxd buffer are not locked immediately return |
311 | // if data in the rxd buffer are not locked immediately return |
313 | if((UART1_rx_buffer.Locked == FALSE) || (DebugUART != UART1) ) return; |
312 | if((UART1_rx_buffer.Locked == FALSE) || (DebugUART != UART1) ) return; |
314 | Waypoint_t * pWaypoint = NULL; |
313 | Waypoint_t * pWaypoint = NULL; |
315 | 314 | ||
316 | MKProtocol_DecodeSerialFrame(&UART1_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer |
315 | MKProtocol_DecodeSerialFrame(&UART1_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer |
317 | if(SerialMsg.CmdID != 'z') SerialLinkOkay = 250; // reset SerialTimeout, but not in case of the "ping" |
316 | if(SerialMsg.CmdID != 'z') SerialLinkOkay = 250; // reset SerialTimeout, but not in case of the "ping" |
318 | switch(SerialMsg.Address) // check for Slave Address |
317 | switch(SerialMsg.Address) // check for Slave Address |
319 | { |
318 | { |
320 | case NC_ADDRESS: // own Slave Address |
319 | case NC_ADDRESS: // own Slave Address |
321 | switch(SerialMsg.CmdID) |
320 | switch(SerialMsg.CmdID) |
322 | { |
321 | { |
323 | case 'z': // connection checker |
322 | case 'z': // connection checker |
324 | memcpy(&Echo, SerialMsg.pData, sizeof(Echo)); // copy echo pattern |
323 | memcpy(&Echo, SerialMsg.pData, sizeof(Echo)); // copy echo pattern |
325 | UART1_Request_Echo = TRUE; |
324 | UART1_Request_Echo = TRUE; |
326 | break; |
325 | break; |
327 | 326 | ||
328 | case 'e': // request for the text of the error status |
327 | case 'e': // request for the text of the error status |
329 | UART1_Request_ErrorMessage = TRUE; |
328 | UART1_Request_ErrorMessage = TRUE; |
330 | break; |
329 | break; |
331 | 330 | ||
332 | case 's':// new target position |
331 | case 's':// new target position |
333 | pWaypoint = (Waypoint_t*)SerialMsg.pData; |
332 | pWaypoint = (Waypoint_t*)SerialMsg.pData; |
334 | BeepTime = 300; |
333 | BeepTime = 300; |
335 | if(pWaypoint->Position.Status == NEWDATA) |
334 | if(pWaypoint->Position.Status == NEWDATA) |
336 | { |
335 | { |
337 | WPList_Clear(); // empty WPList |
336 | WPList_Clear(); // empty WPList |
338 | WPList_Append(pWaypoint); |
337 | WPList_Append(pWaypoint); |
339 | GPS_pWaypoint = WPList_Begin(); |
338 | GPS_pWaypoint = WPList_Begin(); |
340 | } |
339 | } |
341 | break; |
340 | break; |
342 | 341 | ||
343 | case 'u': // redirect debug uart |
342 | case 'u': // redirect debug uart |
344 | switch(SerialMsg.pData[0]) |
343 | switch(SerialMsg.pData[0]) |
345 | { |
344 | { |
346 | case UART_FLIGHTCTRL: |
345 | case UART_FLIGHTCTRL: |
347 | UART2_Init(); // initialize UART2 to FC pins |
346 | UART2_Init(); // initialize UART2 to FC pins |
348 | DebugUART = UART2; |
347 | DebugUART = UART2; |
349 | break; |
348 | break; |
350 | case UART_MK3MAG: |
349 | case UART_MK3MAG: |
351 | if(FC.MKFlags & MKFLAG_MOTOR_RUN) break; // not if the motors are running |
350 | if(FC.MKFlags & MKFLAG_MOTOR_RUN) break; // not if the motors are running |
352 | UART0_Connect_to_MK3MAG(); // mux UART0 to MK3MAG pins |
351 | UART0_Connect_to_MK3MAG(); // mux UART0 to MK3MAG pins |
353 | GPSData.Status = INVALID; |
352 | GPSData.Status = INVALID; |
354 | DebugUART = UART0; |
353 | DebugUART = UART0; |
355 | break; |
354 | break; |
356 | case UART_MKGPS: |
355 | case UART_MKGPS: |
357 | if(FC.MKFlags & MKFLAG_MOTOR_RUN) break; // not if the motors are running |
356 | if(FC.MKFlags & MKFLAG_MOTOR_RUN) break; // not if the motors are running |
358 | UART0_Connect_to_MKGPS(); // connect UART0 to MKGPS pins |
357 | UART0_Connect_to_MKGPS(); // connect UART0 to MKGPS pins |
359 | GPSData.Status = INVALID; |
358 | GPSData.Status = INVALID; |
360 | DebugUART = UART0; |
359 | DebugUART = UART0; |
361 | break; |
360 | break; |
362 | } |
361 | } |
363 | break; |
362 | break; |
364 | 363 | ||
365 | case 'w':// Append Waypoint to List |
364 | case 'w':// Append Waypoint to List |
366 | pWaypoint = (Waypoint_t*)SerialMsg.pData; |
365 | pWaypoint = (Waypoint_t*)SerialMsg.pData; |
367 | if(pWaypoint->Position.Status == INVALID) |
366 | if(pWaypoint->Position.Status == INVALID) |
368 | { // clear WP List |
367 | { // clear WP List |
369 | WPList_Clear(); |
368 | WPList_Clear(); |
370 | GPS_pWaypoint = WPList_Begin(); |
369 | GPS_pWaypoint = WPList_Begin(); |
371 | //UART1_PutString("\r\nClear WP List\r\n"); |
370 | //UART1_PutString("\r\nClear WP List\r\n"); |
372 | } |
371 | } |
373 | else if (pWaypoint->Position.Status == NEWDATA) |
372 | else if (pWaypoint->Position.Status == NEWDATA) |
374 | { // app current WP to the list |
373 | { // app current WP to the list |
375 | WPList_Append(pWaypoint); |
374 | WPList_Append(pWaypoint); |
376 | BeepTime = 500; |
375 | BeepTime = 500; |
377 | //UART1_PutString("\r\nAdd WP to List\r\n"); |
376 | //UART1_PutString("\r\nAdd WP to List\r\n"); |
378 | } |
377 | } |
379 | UART1_Request_NewWaypoint = TRUE; |
378 | UART1_Request_NewWaypoint = TRUE; |
380 | break; |
379 | break; |
381 | 380 | ||
382 | case 'x':// Read Waypoint from List |
381 | case 'x':// Read Waypoint from List |
383 | UART1_Request_ReadWaypoint = SerialMsg.pData[0]; |
382 | UART1_Request_ReadWaypoint = SerialMsg.pData[0]; |
384 | break; |
383 | break; |
385 | 384 | ||
386 | default: |
385 | default: |
387 | // unsupported command recieved |
386 | // unsupported command recieved |
388 | break; |
387 | break; |
389 | } // case NC_ADDRESS |
388 | } // case NC_ADDRESS |
390 | // "break;" is missing here to fall thru to the common commands |
389 | // "break;" is missing here to fall thru to the common commands |
391 | 390 | ||
392 | default: // and any other Slave Address |
391 | default: // and any other Slave Address |
393 | 392 | ||
394 | switch(SerialMsg.CmdID) // check CmdID |
393 | switch(SerialMsg.CmdID) // check CmdID |
395 | { |
394 | { |
396 | case 'a':// request for the labels of the analog debug outputs |
395 | case 'a':// request for the labels of the analog debug outputs |
397 | UART1_Request_DebugLabel = SerialMsg.pData[0]; |
396 | UART1_Request_DebugLabel = SerialMsg.pData[0]; |
398 | if(UART1_Request_DebugLabel > 31) UART1_Request_DebugLabel = 31; |
397 | if(UART1_Request_DebugLabel > 31) UART1_Request_DebugLabel = 31; |
399 | break; |
398 | break; |
400 | 399 | ||
401 | case 'b': // submit extern control |
400 | case 'b': // submit extern control |
402 | memcpy(&ExternControl, SerialMsg.pData, sizeof(ExternControl)); |
401 | memcpy(&ExternControl, SerialMsg.pData, sizeof(ExternControl)); |
403 | UART1_ConfirmFrame = ExternControl.Frame; |
402 | UART1_ConfirmFrame = ExternControl.Frame; |
404 | break; |
403 | break; |
405 | 404 | ||
406 | case 'd': // request for debug data; |
405 | case 'd': // request for debug data; |
407 | UART1_DebugData_Interval = (u32) SerialMsg.pData[0] * 10; |
406 | UART1_DebugData_Interval = (u32) SerialMsg.pData[0] * 10; |
408 | if(UART1_DebugData_Interval > 0) UART1_Request_DebugData = TRUE; |
407 | if(UART1_DebugData_Interval > 0) UART1_Request_DebugData = TRUE; |
409 | break; |
408 | break; |
410 | 409 | ||
411 | case 'c': // request for 3D data; |
410 | case 'c': // request for 3D data; |
412 | UART1_Data3D_Interval = (u32) SerialMsg.pData[0] * 10; |
411 | UART1_Data3D_Interval = (u32) SerialMsg.pData[0] * 10; |
413 | if(UART1_Data3D_Interval > 0) UART1_Request_Data3D = TRUE; |
412 | if(UART1_Data3D_Interval > 0) UART1_Request_Data3D = TRUE; |
414 | break; |
413 | break; |
415 | 414 | ||
416 | case 'g':// request for external control data |
415 | case 'g':// request for external control data |
417 | UART1_Request_ExternalControl = TRUE; |
416 | UART1_Request_ExternalControl = TRUE; |
418 | break; |
417 | break; |
419 | 418 | ||
420 | case 'h':// reqest for display line |
419 | case 'h':// reqest for display line |
421 | RemoteKeys |= SerialMsg.pData[0]; |
420 | RemoteKeys |= SerialMsg.pData[0]; |
422 | if(RemoteKeys != 0) UART1_DisplayLine = 0; |
421 | if(RemoteKeys != 0) UART1_DisplayLine = 0; |
423 | UART1_Request_Display = TRUE; |
422 | UART1_Request_Display = TRUE; |
424 | break; |
423 | break; |
425 | 424 | ||
426 | case 'l':// reqest for display columns |
425 | case 'l':// reqest for display columns |
427 | MenuItem = SerialMsg.pData[0]; |
426 | MenuItem = SerialMsg.pData[0]; |
428 | UART1_Request_Display1 = TRUE; |
427 | UART1_Request_Display1 = TRUE; |
429 | break; |
428 | break; |
430 | 429 | ||
431 | case 'o': // request for navigation information |
430 | case 'o': // request for navigation information |
432 | UART1_NaviData_Interval = (u32) SerialMsg.pData[0] * 10; |
431 | UART1_NaviData_Interval = (u32) SerialMsg.pData[0] * 10; |
433 | if(UART1_NaviData_Interval > 0) UART1_Request_NaviData = TRUE; |
432 | if(UART1_NaviData_Interval > 0) UART1_Request_NaviData = TRUE; |
434 | break; |
433 | break; |
435 | 434 | ||
436 | case 'v': // request for version info |
435 | case 'v': // request for version info |
437 | UART1_Request_VersionInfo = TRUE; |
436 | UART1_Request_VersionInfo = TRUE; |
438 | break; |
437 | break; |
439 | default: |
438 | default: |
440 | // unsupported command recieved |
439 | // unsupported command recieved |
441 | break; |
440 | break; |
442 | } |
441 | } |
443 | break; // default: |
442 | break; // default: |
444 | } |
443 | } |
445 | Buffer_Clear(&UART1_rx_buffer); |
444 | Buffer_Clear(&UART1_rx_buffer); |
446 | } |
445 | } |
447 | 446 | ||
448 | 447 | ||
449 | /*****************************************************/ |
448 | /*****************************************************/ |
450 | /* Send a character */ |
449 | /* Send a character */ |
451 | /*****************************************************/ |
450 | /*****************************************************/ |
452 | s16 UART1_Putchar(char c) |
451 | s16 UART1_Putchar(char c) |
453 | { |
452 | { |
454 | if (c == '\n') UART1_Putchar('\r'); |
453 | if (c == '\n') UART1_Putchar('\r'); |
455 | // wait until txd fifo is not full |
454 | // wait until txd fifo is not full |
456 | while (UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != RESET); |
455 | while (UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != RESET); |
457 | // transmit byte |
456 | // transmit byte |
458 | UART_SendData(UART1, c); |
457 | UART_SendData(UART1, c); |
459 | return (0); |
458 | return (0); |
460 | } |
459 | } |
461 | 460 | ||
462 | /*****************************************************/ |
461 | /*****************************************************/ |
463 | /* Send a string to the debug uart */ |
462 | /* Send a string to the debug uart */ |
464 | /*****************************************************/ |
463 | /*****************************************************/ |
465 | void UART1_PutString(u8 *s) |
464 | void UART1_PutString(u8 *s) |
466 | { |
465 | { |
467 | if(s == NULL) return; |
466 | if(s == NULL) return; |
468 | while (*s != '\0' && DebugUART == UART1) |
467 | while (*s != '\0' && DebugUART == UART1) |
469 | { |
468 | { |
470 | UART1_Putchar(*s); |
469 | UART1_Putchar(*s); |
471 | s ++; |
470 | s ++; |
472 | } |
471 | } |
473 | } |
472 | } |
474 | 473 | ||
475 | 474 | ||
476 | /**************************************************************/ |
475 | /**************************************************************/ |
477 | /* Transmit tx buffer via debug uart */ |
476 | /* Transmit tx buffer via debug uart */ |
478 | /**************************************************************/ |
477 | /**************************************************************/ |
479 | void UART1_Transmit(void) |
478 | void UART1_Transmit(void) |
480 | { |
479 | { |
481 | u8 tmp_tx; |
480 | u8 tmp_tx; |
482 | if(DebugUART != UART1) return; |
481 | if(DebugUART != UART1) return; |
483 | // if something has to be send and the txd fifo is not full |
482 | // if something has to be send and the txd fifo is not full |
484 | if(UART1_tx_buffer.Locked == TRUE) |
483 | if(UART1_tx_buffer.Locked == TRUE) |
485 | { |
484 | { |
486 | // while there is some space in the tx fifo |
485 | // while there is some space in the tx fifo |
487 | while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != SET) |
486 | while(UART_GetFlagStatus(UART1, UART_FLAG_TxFIFOFull) != SET) |
488 | { |
487 | { |
489 | tmp_tx = UART1_tx_buffer.pData[UART1_tx_buffer.Position++]; // read next byte from txd buffer |
488 | tmp_tx = UART1_tx_buffer.pData[UART1_tx_buffer.Position++]; // read next byte from txd buffer |
490 | UART_SendData(UART1, tmp_tx); // put character to txd fifo |
489 | UART_SendData(UART1, tmp_tx); // put character to txd fifo |
491 | // if terminating character or end of txd buffer reached |
490 | // if terminating character or end of txd buffer reached |
492 | if((tmp_tx == '\r') || (UART1_tx_buffer.Position == UART1_tx_buffer.DataBytes)) |
491 | if((tmp_tx == '\r') || (UART1_tx_buffer.Position == UART1_tx_buffer.DataBytes)) |
493 | { |
492 | { |
494 | Buffer_Clear(&UART1_tx_buffer); // clear txd buffer |
493 | Buffer_Clear(&UART1_tx_buffer); // clear txd buffer |
495 | break; // end while loop |
494 | break; // end while loop |
496 | } |
495 | } |
497 | } |
496 | } |
498 | } |
497 | } |
499 | } |
498 | } |
500 | 499 | ||
501 | /**************************************************************/ |
500 | /**************************************************************/ |
502 | /* Send the answers to incomming commands at the debug uart */ |
501 | /* Send the answers to incomming commands at the debug uart */ |
503 | /**************************************************************/ |
502 | /**************************************************************/ |
504 | void UART1_TransmitTxData(void) |
503 | void UART1_TransmitTxData(void) |
505 | { |
504 | { |
506 | if(DebugUART != UART1) return; |
505 | if(DebugUART != UART1) return; |
507 | UART1_Transmit(); // output pending bytes in tx buffer |
506 | UART1_Transmit(); // output pending bytes in tx buffer |
508 | if((UART1_tx_buffer.Locked == TRUE)) return; |
507 | if((UART1_tx_buffer.Locked == TRUE)) return; |
509 | 508 | ||
510 | if(UART1_Request_Echo && (UART1_tx_buffer.Locked == FALSE)) |
509 | if(UART1_Request_Echo && (UART1_tx_buffer.Locked == FALSE)) |
511 | { |
510 | { |
512 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'Z', NC_ADDRESS, 1, &Echo, sizeof(Echo)); // answer the echo request |
511 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'Z', NC_ADDRESS, 1, &Echo, sizeof(Echo)); // answer the echo request |
513 | Echo = 0; // reset echo value |
512 | Echo = 0; // reset echo value |
514 | UART1_Request_Echo = FALSE; |
513 | UART1_Request_Echo = FALSE; |
515 | } |
514 | } |
516 | if((UART1_Request_DebugLabel != 0xFF) && (UART1_tx_buffer.Locked == FALSE)) |
515 | if((UART1_Request_DebugLabel != 0xFF) && (UART1_tx_buffer.Locked == FALSE)) |
517 | { |
516 | { |
518 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'A', NC_ADDRESS, 2, &UART1_Request_DebugLabel, sizeof(UART1_Request_DebugLabel), (u8 *) ANALOG_LABEL[UART1_Request_DebugLabel], 16); |
517 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'A', NC_ADDRESS, 2, &UART1_Request_DebugLabel, sizeof(UART1_Request_DebugLabel), (u8 *) ANALOG_LABEL[UART1_Request_DebugLabel], 16); |
519 | UART1_Request_DebugLabel = 0xFF; |
518 | UART1_Request_DebugLabel = 0xFF; |
520 | } |
519 | } |
521 | if(UART1_ConfirmFrame && (UART1_tx_buffer.Locked == FALSE)) |
520 | if(UART1_ConfirmFrame && (UART1_tx_buffer.Locked == FALSE)) |
522 | { |
521 | { |
523 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'B', NC_ADDRESS, 1, &UART1_ConfirmFrame, sizeof(UART1_ConfirmFrame)); |
522 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'B', NC_ADDRESS, 1, &UART1_ConfirmFrame, sizeof(UART1_ConfirmFrame)); |
524 | UART1_ConfirmFrame = 0; |
523 | UART1_ConfirmFrame = 0; |
525 | } |
524 | } |
526 | if( (( (UART1_DebugData_Interval > 0) && CheckDelay(UART1_DebugData_Timer)) || UART1_Request_DebugData) && (UART1_tx_buffer.Locked == FALSE)) |
525 | if( (( (UART1_DebugData_Interval > 0) && CheckDelay(UART1_DebugData_Timer)) || UART1_Request_DebugData) && (UART1_tx_buffer.Locked == FALSE)) |
527 | { |
526 | { |
528 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'D', NC_ADDRESS, 1,(u8 *)&DebugOut, sizeof(DebugOut)); |
527 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'D', NC_ADDRESS, 1,(u8 *)&DebugOut, sizeof(DebugOut)); |
529 | UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval); |
528 | UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval); |
530 | UART1_Request_DebugData = FALSE; |
529 | UART1_Request_DebugData = FALSE; |
531 | } |
530 | } |
532 | 531 | ||
533 | if((( (UART1_Data3D_Interval > 0) && CheckDelay(UART1_Data3D_Timer) ) || UART1_Request_Data3D) && (UART1_tx_buffer.Locked == FALSE)) |
532 | if((( (UART1_Data3D_Interval > 0) && CheckDelay(UART1_Data3D_Timer) ) || UART1_Request_Data3D) && (UART1_tx_buffer.Locked == FALSE)) |
534 | { |
533 | { |
535 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'C', NC_ADDRESS, 1,(u8 *)&Data3D, sizeof(Data3D)); |
534 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'C', NC_ADDRESS, 1,(u8 *)&Data3D, sizeof(Data3D)); |
536 | UART1_Data3D_Timer = SetDelay(UART1_Data3D_Interval); |
535 | UART1_Data3D_Timer = SetDelay(UART1_Data3D_Interval); |
537 | UART1_Request_Data3D = FALSE; |
536 | UART1_Request_Data3D = FALSE; |
538 | } |
537 | } |
539 | 538 | ||
540 | if(UART1_Request_ExternalControl && (UART1_tx_buffer.Locked == FALSE)) |
539 | if(UART1_Request_ExternalControl && (UART1_tx_buffer.Locked == FALSE)) |
541 | { |
540 | { |
542 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'G', NC_ADDRESS, 1, (u8 *)&ExternControl, sizeof(ExternControl)); |
541 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'G', NC_ADDRESS, 1, (u8 *)&ExternControl, sizeof(ExternControl)); |
543 | UART1_Request_ExternalControl = FALSE; |
542 | UART1_Request_ExternalControl = FALSE; |
544 | } |
543 | } |
545 | if(UART1_Request_Display && (UART1_tx_buffer.Locked == FALSE)) |
544 | if(UART1_Request_Display && (UART1_tx_buffer.Locked == FALSE)) |
546 | { |
545 | { |
547 | LCD_PrintMenu(); |
546 | LCD_PrintMenu(); |
548 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'H', NC_ADDRESS, 2, &UART1_DisplayLine, sizeof(UART1_DisplayLine), (u8*)&DisplayBuff[UART1_DisplayLine * 20], 20); |
547 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'H', NC_ADDRESS, 2, &UART1_DisplayLine, sizeof(UART1_DisplayLine), (u8*)&DisplayBuff[UART1_DisplayLine * 20], 20); |
549 | UART1_DisplayLine++; |
548 | UART1_DisplayLine++; |
550 | if(UART1_DisplayLine >= 4) UART1_DisplayLine = 0; |
549 | if(UART1_DisplayLine >= 4) UART1_DisplayLine = 0; |
551 | UART1_Request_Display = FALSE; |
550 | UART1_Request_Display = FALSE; |
552 | } |
551 | } |
553 | if(UART1_Request_Display1 && (UART1_tx_buffer.Locked == FALSE)) |
552 | if(UART1_Request_Display1 && (UART1_tx_buffer.Locked == FALSE)) |
554 | { |
553 | { |
555 | LCD_PrintMenu(); |
554 | LCD_PrintMenu(); |
556 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'L', NC_ADDRESS, 3, (u8*)&MenuItem, sizeof(MenuItem), (u8*)&MaxMenuItem, sizeof(MaxMenuItem),(u8*)DisplayBuff, sizeof(DisplayBuff)); |
555 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'L', NC_ADDRESS, 3, (u8*)&MenuItem, sizeof(MenuItem), (u8*)&MaxMenuItem, sizeof(MaxMenuItem),(u8*)DisplayBuff, sizeof(DisplayBuff)); |
557 | UART1_Request_Display1 = FALSE; |
556 | UART1_Request_Display1 = FALSE; |
558 | } |
557 | } |
559 | if(UART1_Request_VersionInfo && (UART1_tx_buffer.Locked == FALSE)) |
558 | if(UART1_Request_VersionInfo && (UART1_tx_buffer.Locked == FALSE)) |
560 | { |
559 | { |
561 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'V', NC_ADDRESS,1, (u8 *)&UART_VersionInfo, sizeof(UART_VersionInfo)); |
560 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'V', NC_ADDRESS,1, (u8 *)&UART_VersionInfo, sizeof(UART_VersionInfo)); |
562 | UART1_Request_VersionInfo = FALSE; |
561 | UART1_Request_VersionInfo = FALSE; |
563 | } |
562 | } |
564 | if(( (UART1_NaviData_Interval && CheckDelay(UART1_NaviData_Timer) ) || UART1_Request_NaviData) && (UART1_tx_buffer.Locked == FALSE)) |
563 | if(( (UART1_NaviData_Interval && CheckDelay(UART1_NaviData_Timer) ) || UART1_Request_NaviData) && (UART1_tx_buffer.Locked == FALSE)) |
565 | { |
564 | { |
566 | NaviData.Errorcode = ErrorCode; |
565 | NaviData.Errorcode = ErrorCode; |
567 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'O', NC_ADDRESS,1, (u8 *)&NaviData, sizeof(NaviData)); |
566 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'O', NC_ADDRESS,1, (u8 *)&NaviData, sizeof(NaviData)); |
568 | UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval); |
567 | UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval); |
569 | UART1_Request_NaviData = FALSE; |
568 | UART1_Request_NaviData = FALSE; |
570 | } |
569 | } |
571 | if(UART1_Request_ErrorMessage && (UART1_tx_buffer.Locked == FALSE)) |
570 | if(UART1_Request_ErrorMessage && (UART1_tx_buffer.Locked == FALSE)) |
572 | { |
571 | { |
573 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'E', NC_ADDRESS, 1, (u8 *)&ErrorMSG, sizeof(ErrorMSG)); |
572 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'E', NC_ADDRESS, 1, (u8 *)&ErrorMSG, sizeof(ErrorMSG)); |
574 | UART1_Request_ErrorMessage = FALSE; |
573 | UART1_Request_ErrorMessage = FALSE; |
575 | } |
574 | } |
576 | if(UART1_Request_SendFollowMe && (UART1_tx_buffer.Locked == FALSE) && (GPSData.NumOfSats >= 4)) // sending for "Follow me" |
575 | if(UART1_Request_SendFollowMe && (UART1_tx_buffer.Locked == FALSE) && (GPSData.NumOfSats >= 4)) // sending for "Follow me" |
577 | { |
576 | { |
578 | GPS_CopyPosition(&(GPSData.Position),&(FollowMe.Position)); |
577 | GPS_CopyPosition(&(GPSData.Position),&(FollowMe.Position)); |
579 | FollowMe.Position.Status = NEWDATA; |
578 | FollowMe.Position.Status = NEWDATA; |
580 | FollowMe.Heading = -1; |
579 | FollowMe.Heading = -1; |
581 | FollowMe.ToleranceRadius = 1; |
580 | FollowMe.ToleranceRadius = 1; |
582 | FollowMe.HoldTime = 60; |
581 | FollowMe.HoldTime = 60; |
583 | FollowMe.Event_Flag = 0; |
582 | FollowMe.Event_Flag = 0; |
584 | FollowMe.reserve[0] = 0; // reserve |
583 | FollowMe.reserve[0] = 0; // reserve |
585 | FollowMe.reserve[1] = 0; // reserve |
584 | FollowMe.reserve[1] = 0; // reserve |
586 | FollowMe.reserve[2] = 0; // reserve |
585 | FollowMe.reserve[2] = 0; // reserve |
587 | FollowMe.reserve[3] = 0; // reserve |
586 | FollowMe.reserve[3] = 0; // reserve |
588 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 's', NC_ADDRESS, 1, (u8 *)&FollowMe, sizeof(FollowMe)); |
587 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 's', NC_ADDRESS, 1, (u8 *)&FollowMe, sizeof(FollowMe)); |
589 | UART1_Request_SendFollowMe = FALSE; |
588 | UART1_Request_SendFollowMe = FALSE; |
590 | } |
589 | } |
591 | if(UART1_Request_NewWaypoint && (UART1_tx_buffer.Locked == FALSE)) |
590 | if(UART1_Request_NewWaypoint && (UART1_tx_buffer.Locked == FALSE)) |
592 | { |
591 | { |
593 | u8 WPNumber = WPList_GetCount(); |
592 | u8 WPNumber = WPList_GetCount(); |
594 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'W', NC_ADDRESS, 1, &WPNumber, sizeof(WPNumber)); |
593 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'W', NC_ADDRESS, 1, &WPNumber, sizeof(WPNumber)); |
595 | UART1_Request_NewWaypoint = FALSE; |
594 | UART1_Request_NewWaypoint = FALSE; |
596 | } |
595 | } |
597 | if((UART1_Request_ReadWaypoint != 0xFF) && (UART1_tx_buffer.Locked == FALSE)) |
596 | if((UART1_Request_ReadWaypoint != 0xFF) && (UART1_tx_buffer.Locked == FALSE)) |
598 | { |
597 | { |
599 | u8 WPNumber = WPList_GetCount(); |
598 | u8 WPNumber = WPList_GetCount(); |
600 | if (UART1_Request_ReadWaypoint < WPNumber) |
599 | if (UART1_Request_ReadWaypoint < WPNumber) |
601 | { |
600 | { |
602 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'X', NC_ADDRESS, 3, &WPNumber, 1, &UART1_Request_ReadWaypoint, 1, WPList_GetAt(UART1_Request_ReadWaypoint), sizeof(Waypoint_t)); |
601 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'X', NC_ADDRESS, 3, &WPNumber, 1, &UART1_Request_ReadWaypoint, 1, WPList_GetAt(UART1_Request_ReadWaypoint), sizeof(Waypoint_t)); |
603 | } |
602 | } |
604 | else |
603 | else |
605 | { |
604 | { |
606 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer,'X', NC_ADDRESS, 1, &WPNumber, sizeof(WPNumber)); |
605 | MKProtocol_CreateSerialFrame(&UART1_tx_buffer,'X', NC_ADDRESS, 1, &WPNumber, sizeof(WPNumber)); |
607 | } |
606 | } |
608 | UART1_Request_ReadWaypoint = 0xFF; |
607 | UART1_Request_ReadWaypoint = 0xFF; |
609 | } |
608 | } |
610 | UART1_Transmit(); // output pending bytes in tx buffer |
609 | UART1_Transmit(); // output pending bytes in tx buffer |
611 | } |
610 | } |
612 | 611 | ||
613 | 612 |