Rev 19 | Rev 22 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 19 | Rev 21 | ||
---|---|---|---|
1 | /*####################################################################################### |
1 | /*####################################################################################### |
2 | MK3Mag 3D-Magnet sensor |
2 | MK3Mag 3D-Magnet sensor |
3 | !!! THIS IS NOT FREE SOFTWARE !!! |
3 | !!! THIS IS NOT FREE SOFTWARE !!! |
4 | #######################################################################################*/ |
4 | #######################################################################################*/ |
5 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
6 | // + Copyright (c) 05.2008 Holger Buss |
6 | // + Copyright (c) 05.2008 Holger Buss |
7 | // + Thanks to Ilja Fähnrich (P_Latzhalter) |
7 | // + Thanks to Ilja Fähnrich (P_Latzhalter) |
8 | // + Nur für den privaten Gebrauch |
8 | // + Nur für den privaten Gebrauch |
9 | // + www.MikroKopter.com |
9 | // + www.MikroKopter.com |
10 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
10 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
11 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
11 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
12 | // + mit unserer Zustimmung zulässig |
12 | // + mit unserer Zustimmung zulässig |
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
14 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
14 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
15 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
15 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
16 | // + AUSNAHME: Ein bei www.mikrokopter.de erworbener vorbestückter MK3Mag darf als Baugruppe auch in kommerziellen Systemen verbaut werden |
16 | // + AUSNAHME: Ein bei www.mikrokopter.de erworbener vorbestückter MK3Mag darf als Baugruppe auch in kommerziellen Systemen verbaut werden |
17 | // + Im Zweifelsfall bitte anfragen bei: info@mikrokopter.de |
17 | // + Im Zweifelsfall bitte anfragen bei: info@mikrokopter.de |
18 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
18 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
19 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
19 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
20 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
20 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
21 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
21 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
22 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
22 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
23 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
23 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
24 | // + eindeutig als Ursprung verlinkt werden |
24 | // + eindeutig als Ursprung verlinkt werden |
25 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
25 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
26 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
26 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
27 | // + Benutzung auf eigene Gefahr |
27 | // + Benutzung auf eigene Gefahr |
28 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
28 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
29 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
29 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
30 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
30 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
32 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
32 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
33 | // + this list of conditions and the following disclaimer. |
33 | // + this list of conditions and the following disclaimer. |
34 | // + * PORTING this software (or parts of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed |
34 | // + * PORTING this software (or parts of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed |
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 permittet |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
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 | // + Exception: A preassembled MK3Mag, purchased from www.mikrokopter.de may be used as a part of commercial systems |
41 | // + Exception: A preassembled MK3Mag, purchased from www.mikrokopter.de may be used as a part of commercial systems |
42 | // + In case of doubt please contact: info@MikroKopter.de |
42 | // + In case of doubt please contact: info@MikroKopter.de |
43 | // + * If sources or documentations are redistributet on other webpages, our webpage (http://www.MikroKopter.de) must be |
43 | // + * If sources or documentations are redistributet on other webpages, our webpage (http://www.MikroKopter.de) must be |
44 | // + clearly linked as origin |
44 | // + clearly linked as origin |
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 <avr/io.h> |
57 | #include <avr/io.h> |
58 | #include <avr/interrupt.h> |
58 | #include <avr/interrupt.h> |
59 | #include <avr/wdt.h> |
59 | #include <avr/wdt.h> |
60 | #include "main.h" |
60 | #include "main.h" |
61 | #include "uart.h" |
61 | #include "uart.h" |
62 | #include "timer0.h" |
62 | #include "timer0.h" |
63 | #include "twislave.h" |
63 | #include "twislave.h" |
64 | 64 | ||
65 | 65 | ||
66 | #define FALSE 0 |
66 | #define FALSE 0 |
67 | #define TRUE 1 |
67 | #define TRUE 1 |
68 | 68 | ||
69 | #define TXD_BUFFER_LEN 100 |
69 | #define TXD_BUFFER_LEN 150 |
70 | #define RXD_BUFFER_LEN 100 |
70 | #define RXD_BUFFER_LEN 150 |
71 | 71 | ||
72 | volatile uint8_t txd_buffer[TXD_BUFFER_LEN]; |
72 | volatile uint8_t txd_buffer[TXD_BUFFER_LEN]; |
73 | volatile uint8_t rxd_buffer_locked = FALSE; |
73 | volatile uint8_t rxd_buffer_locked = FALSE; |
74 | volatile uint8_t rxd_buffer[RXD_BUFFER_LEN]; |
74 | volatile uint8_t rxd_buffer[RXD_BUFFER_LEN]; |
75 | volatile uint8_t txd_complete = TRUE; |
75 | volatile uint8_t txd_complete = TRUE; |
76 | volatile uint8_t ReceivedBytes = 0; |
76 | volatile uint8_t ReceivedBytes = 0; |
- | 77 | ||
- | 78 | ||
77 | 79 | ||
78 | uint8_t RequestVerInfo = FALSE; |
80 | uint8_t RequestVerInfo = FALSE; |
79 | uint8_t RequestDebugData = FALSE; |
81 | uint8_t RequestDebugData = FALSE; |
80 | uint8_t RequestDebugLabel = FALSE; |
82 | uint8_t RequestDebugLabel = FALSE; |
81 | uint8_t RequestCompassHeading = FALSE; |
83 | uint8_t RequestCompassHeading = FALSE; |
82 | 84 | ||
83 | uint8_t MySlaveAddr = 0; |
85 | uint8_t MySlaveAddr = 0; |
84 | 86 | ||
85 | //unsigned char,DebugGetAnforderung = 0, |
87 | //unsigned char,DebugGetAnforderung = 0, |
86 | 88 | ||
87 | DebugOut_t DebugOut; |
89 | DebugOut_t DebugOut; |
88 | ExternData_t ExternData; |
90 | ExternData_t ExternData; |
89 | ExternControl_t ExternControl; |
91 | ExternControl_t ExternControl; |
90 | VersionInfo_t VersionInfo; |
92 | VersionInfo_t VersionInfo; |
91 | 93 | ||
92 | uint16_t Debug_Timer; |
94 | uint16_t Debug_Timer; |
93 | 95 | ||
94 | const uint8_t ANALOG_LABEL[32][16] = |
96 | const uint8_t ANALOG_LABEL[32][16] = |
95 | { |
97 | { |
96 | //1234567890123456 |
98 | //1234567890123456 |
97 | "Magnet X ", //0 |
99 | "Magnet X ", //0 |
98 | "Magnet Y ", |
100 | "Magnet Y ", |
99 | "Magnet Z ", |
101 | "Magnet Z ", |
100 | "Raw X ", |
102 | "Raw X ", |
101 | "Raw Y ", |
103 | "Raw Y ", |
102 | "Raw Z ", //5 |
104 | "Raw Z ", //5 |
103 | "Attitude Nick ", |
105 | "Attitude Nick ", |
104 | "Attitude Roll ", |
106 | "Attitude Roll ", |
105 | "XOffset ", |
107 | "XOffset ", |
106 | "XRange ", |
108 | "XRange ", |
107 | "YOffset ", //10 |
109 | "YOffset ", //10 |
108 | "YRange ", |
110 | "YRange ", |
109 | "ZOffset ", |
111 | "ZOffset ", |
110 | "ZRange ", |
112 | "ZRange ", |
111 | "Calstate ", |
113 | "Calstate ", |
112 | "Heading Request ", //15 |
114 | "Heading ", //15 |
113 | "User0 ", |
115 | "User0 ", |
114 | "User1 ", |
116 | "User1 ", |
115 | "Analog18 ", |
117 | "Analog18 ", |
116 | "Analog19 ", |
118 | "Analog19 ", |
117 | "Analog20 ", //20 |
119 | "Analog20 ", //20 |
118 | "Analog21 ", |
120 | "Analog21 ", |
119 | "Analog22 ", |
121 | "Analog22 ", |
120 | "Analog23 ", |
122 | "Analog23 ", |
121 | "Analog24 ", |
123 | "Analog24 ", |
122 | "Analog25 ", //25 |
124 | "Analog25 ", //25 |
123 | "Analog26 ", |
125 | "Analog26 ", |
124 | "Analog27 ", |
126 | "Analog27 ", |
125 | "Analog28 ", |
127 | "Analog28 ", |
126 | "Analog29 ", |
128 | "Analog29 ", |
127 | "Analog30 ", //30 |
129 | "Analog30 ", //30 |
128 | "Analog31 " |
130 | "Analog31 " |
129 | }; |
131 | }; |
130 | 132 | ||
131 | 133 | ||
132 | /****************************************************************/ |
134 | /****************************************************************/ |
133 | /* Initialization of the USART0 */ |
135 | /* Initialization of the USART0 */ |
134 | /****************************************************************/ |
136 | /****************************************************************/ |
135 | void USART0_Init (void) |
137 | void USART0_Init (void) |
136 | { |
138 | { |
137 | uint8_t sreg = SREG; |
139 | uint8_t sreg = SREG; |
138 | 140 | ||
139 | uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * BAUD_RATE) - 1); |
141 | uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * BAUD_RATE) - 1); |
140 | 142 | ||
141 | // disable all interrupts before configuration |
143 | // disable all interrupts before configuration |
142 | cli(); |
144 | cli(); |
143 | 145 | ||
144 | // disable RX-Interrupt |
146 | // disable RX-Interrupt |
145 | UCSR0B &= ~(1 << RXCIE0); |
147 | UCSR0B &= ~(1 << RXCIE0); |
146 | // disable TX-Interrupt |
148 | // disable TX-Interrupt |
147 | UCSR0B &= ~(1 << TXCIE0); |
149 | UCSR0B &= ~(1 << TXCIE0); |
148 | 150 | ||
149 | // set direction of RXD0 and TXD0 pins |
151 | // set direction of RXD0 and TXD0 pins |
150 | 152 | ||
151 | // set RXD0 (PD0) as an input pin |
153 | // set RXD0 (PD0) as an input pin |
152 | PORTD |= (1 << PORTD0); |
154 | PORTD |= (1 << PORTD0); |
153 | DDRD &= ~(1 << DDD0); |
155 | DDRD &= ~(1 << DDD0); |
154 | // set TXD0 (PD1) as an output pin |
156 | // set TXD0 (PD1) as an output pin |
155 | PORTD |= (1 << PORTD1); |
157 | PORTD |= (1 << PORTD1); |
156 | DDRD |= (1 << DDD1); |
158 | DDRD |= (1 << DDD1); |
157 | 159 | ||
158 | // USART0 Baud Rate Register |
160 | // USART0 Baud Rate Register |
159 | // set clock divider |
161 | // set clock divider |
160 | UBRR0H = (uint8_t)(ubrr >> 8); |
162 | UBRR0H = (uint8_t)(ubrr >> 8); |
161 | UBRR0L = (uint8_t)ubrr; |
163 | UBRR0L = (uint8_t)ubrr; |
162 | 164 | ||
163 | // USART0 Control and Status Register A, B, C |
165 | // USART0 Control and Status Register A, B, C |
164 | 166 | ||
165 | // enable double speed operation in |
167 | // enable double speed operation |
166 | UCSR0A |= (1 << U2X0); |
168 | UCSR0A |= (1 << U2X0); |
167 | // enable receiver and transmitter in |
169 | // enable receiver and transmitter |
168 | UCSR0B |= (1 << TXEN0) | (1 << RXEN0); |
170 | UCSR0B |= (1 << TXEN0) | (1 << RXEN0); |
169 | // set asynchronous mode |
171 | // set asynchronous mode |
170 | UCSR0C &= ~(1 << UMSEL01); |
172 | UCSR0C &= ~(1 << UMSEL01); |
171 | UCSR0C &= ~(1 << UMSEL00); |
173 | UCSR0C &= ~(1 << UMSEL00); |
172 | // no parity |
174 | // no parity |
173 | UCSR0C &= ~(1 << UPM01); |
175 | UCSR0C &= ~(1 << UPM01); |
174 | UCSR0C &= ~(1 << UPM00); |
176 | UCSR0C &= ~(1 << UPM00); |
175 | // 1 stop bit |
177 | // 1 stop bit |
176 | UCSR0C &= ~(1 << USBS0); |
178 | UCSR0C &= ~(1 << USBS0); |
177 | // 8-bit |
179 | // 8-bit |
178 | UCSR0B &= ~(1 << UCSZ02); |
180 | UCSR0B &= ~(1 << UCSZ02); |
179 | UCSR0C |= (1 << UCSZ01); |
181 | UCSR0C |= (1 << UCSZ01); |
180 | UCSR0C |= (1 << UCSZ00); |
182 | UCSR0C |= (1 << UCSZ00); |
181 | 183 | ||
182 | // flush receive buffer |
184 | // flush receive buffer |
183 | while ( UCSR0A & (1<<RXC0) ) UDR0; |
185 | while ( UCSR0A & (1<<RXC0) ) UDR0; |
184 | 186 | ||
185 | // enable interrupts at the end |
187 | // enable interrupts at the end |
186 | // enable RX-Interrupt |
188 | // enable RX-Interrupt |
187 | UCSR0B |= (1 << RXCIE0); |
189 | UCSR0B |= (1 << RXCIE0); |
188 | // enable TX-Interrupt |
190 | // enable TX-Interrupt |
189 | UCSR0B |= (1 << TXCIE0); |
191 | UCSR0B |= (1 << TXCIE0); |
190 | 192 | ||
191 | rxd_buffer_locked = FALSE; |
193 | rxd_buffer_locked = FALSE; |
192 | txd_complete = TRUE; |
194 | txd_complete = TRUE; |
193 | 195 | ||
194 | Debug_Timer = SetDelay(200); |
196 | Debug_Timer = SetDelay(200); |
195 | 197 | ||
196 | 198 | ||
197 | // restore global interrupt flags |
199 | // restore global interrupt flags |
198 | SREG = sreg; |
200 | SREG = sreg; |
199 | 201 | ||
200 | VersionInfo.Major = VERSION_MAJOR; |
202 | VersionInfo.Major = VERSION_MAJOR; |
201 | VersionInfo.Minor = VERSION_MINOR; |
203 | VersionInfo.Minor = VERSION_MINOR; |
202 | VersionInfo.PCCompatible = VERSION_COMPATIBLE; |
204 | VersionInfo.PCCompatible = VERSION_COMPATIBLE; |
203 | 205 | ||
204 | // Version beim Start ausgeben (nicht schön, aber geht... ) |
206 | // Version beim Start ausgeben (nicht schön, aber geht... ) |
205 | uart_putchar ('\n');uart_putchar ('C');uart_putchar ('P');uart_putchar (':'); |
207 | uart_putchar ('\n');uart_putchar ('C');uart_putchar ('P');uart_putchar (':'); |
206 | uart_putchar ('V');uart_putchar (0x30 + VERSION_MAJOR);uart_putchar ('.');uart_putchar (0x30 + VERSION_MINOR/10); uart_putchar (0x30 + VERSION_MINOR%10); |
208 | uart_putchar ('V');uart_putchar (0x30 + VERSION_MAJOR);uart_putchar ('.');uart_putchar (0x30 + VERSION_MINOR/10); uart_putchar (0x30 + VERSION_MINOR%10); |
207 | uart_putchar ('\n'); |
209 | uart_putchar ('\n'); |
208 | } |
210 | } |
209 | 211 | ||
210 | // --------------------------------------------------------------------------------- |
212 | // --------------------------------------------------------------------------------- |
211 | void USART0_EnableTXD(void) |
213 | void USART0_EnableTXD(void) |
212 | { |
214 | { |
- | 215 | //if(!(UCSR0B & (1 << TXEN0))) return; |
|
213 | DDRD |= (1<<DDD1); // set TXD pin as output |
216 | DDRD |= (1<<DDD1); // set TXD pin as output |
214 | UCSR0B |= (1 << TXEN0); // enable TX in USART |
217 | UCSR0B |= (1 << TXEN0); // enable TX in USART |
215 | } |
218 | } |
216 | 219 | ||
217 | // --------------------------------------------------------------------------------- |
220 | // --------------------------------------------------------------------------------- |
218 | void USART0_DisableTXD(void) |
221 | void USART0_DisableTXD(void) |
219 | { |
222 | { |
- | 223 | //if((UCSR0B & (1 << TXEN0))) return; |
|
- | 224 | while(!txd_complete){ }; |
|
- | 225 | ||
220 | UCSR0B &= ~(1 << TXEN0); // disable TXD in USART |
226 | UCSR0B &= ~(1 << TXEN0); // disable TXD in USART |
221 | DDRD &= ~(1<<DDD1); // set TXD pin as input |
227 | DDRD &= ~(1<<DDD1); // set TXD pin as input |
222 | } |
228 | } |
223 | 229 | ||
224 | /****************************************************************/ |
230 | /****************************************************************/ |
225 | /* USART0 transmitter ISR */ |
231 | /* USART0 transmitter ISR */ |
226 | /****************************************************************/ |
232 | /****************************************************************/ |
227 | ISR(USART_TX_vect) |
233 | ISR(USART_TX_vect) |
228 | { |
234 | { |
229 | static uint16_t ptr_txd_buffer = 0; |
235 | static uint16_t ptr_txd_buffer = 0; |
230 | uint8_t tmp_tx; |
236 | uint8_t tmp_tx; |
231 | if(!txd_complete) // transmission not completed |
237 | if(!txd_complete) // transmission not completed |
232 | { |
238 | { |
233 | ptr_txd_buffer++; // die [0] wurde schon gesendet |
239 | ptr_txd_buffer++; // die [0] wurde schon gesendet |
234 | tmp_tx = txd_buffer[ptr_txd_buffer]; |
240 | tmp_tx = txd_buffer[ptr_txd_buffer]; |
235 | // if terminating character or end of txd buffer was reached |
241 | // if terminating character or end of txd buffer was reached |
236 | if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN)) |
242 | if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN)) |
237 | { |
243 | { |
238 | ptr_txd_buffer = 0; // reset txd pointer |
244 | ptr_txd_buffer = 0; // reset txd pointer |
239 | txd_complete = 1; // stop transmission |
245 | txd_complete = TRUE; // stop transmission |
240 | } |
246 | } |
241 | UDR0 = tmp_tx; // send current byte will trigger this ISR again |
247 | UDR0 = tmp_tx; // send current byte will trigger this ISR again |
242 | } |
248 | } |
243 | // transmission completed |
249 | // transmission completed |
244 | else ptr_txd_buffer = 0; |
250 | else ptr_txd_buffer = 0; |
245 | } |
251 | } |
246 | 252 | ||
247 | /****************************************************************/ |
253 | /****************************************************************/ |
248 | /* USART0 receiver ISR */ |
254 | /* USART0 receiver ISR */ |
249 | /****************************************************************/ |
255 | /****************************************************************/ |
250 | ISR(USART_RX_vect) |
256 | ISR(USART_RX_vect) |
251 | { |
257 | { |
252 | static uint16_t crc; |
258 | static uint16_t crc; |
253 | static uint8_t ptr_rxd_buffer = 0; |
- | |
254 | uint8_t crc1, crc2; |
259 | uint8_t crc1, crc2; |
255 | uint8_t c; |
260 | uint8_t c; |
- | 261 | static uint8_t ptr_rxd_buffer = 0; |
|
256 | 262 | ||
257 | c = UDR0; // catch the received byte |
263 | c = UDR0; // catch the received byte |
258 | 264 | ||
259 | if(rxd_buffer_locked) return; // if rxd buffer is locked immediately return |
265 | if(rxd_buffer_locked) return; // if rxd buffer is locked immediately return |
260 | 266 | ||
261 | // the rxd buffer is unlocked |
267 | // the rxd buffer is unlocked |
262 | if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received |
268 | if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received |
263 | { |
269 | { |
264 | rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer |
270 | rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer |
265 | crc = c; // init crc |
271 | crc = c; // init crc |
266 | } |
272 | } |
267 | #if 0 |
273 | #if 0 |
268 | else if (ptr_rxd_buffer == 1) // handle address |
274 | else if (ptr_rxd_buffer == 1) // handle address |
269 | { |
275 | { |
270 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
276 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
271 | crc += c; // update crc |
277 | crc += c; // update crc |
272 | } |
278 | } |
273 | #endif |
279 | #endif |
274 | else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // collect incomming bytes |
280 | else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // collect incomming bytes |
275 | { |
281 | { |
276 | if(c != '\r') // no termination character |
282 | if(c != '\r') // no termination character |
277 | { |
283 | { |
278 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
284 | rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer |
279 | crc += c; // update crc |
285 | crc += c; // update crc |
280 | } |
286 | } |
281 | else // termination character was received |
287 | else // termination character was received |
282 | { |
288 | { |
283 | // the last 2 bytes are no subject for checksum calculation |
289 | // the last 2 bytes are no subject for checksum calculation |
284 | // they are the checksum itself |
290 | // they are the checksum itself |
285 | crc -= rxd_buffer[ptr_rxd_buffer-2]; |
291 | crc -= rxd_buffer[ptr_rxd_buffer-2]; |
286 | crc -= rxd_buffer[ptr_rxd_buffer-1]; |
292 | crc -= rxd_buffer[ptr_rxd_buffer-1]; |
287 | // calculate checksum from transmitted data |
293 | // calculate checksum from transmitted data |
288 | crc %= 4096; |
294 | crc %= 4096; |
289 | crc1 = '=' + crc / 64; |
295 | crc1 = '=' + crc / 64; |
290 | crc2 = '=' + crc % 64; |
296 | crc2 = '=' + crc % 64; |
291 | // compare checksum to transmitted checksum bytes |
297 | // compare checksum to transmitted checksum bytes |
292 | if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1])) |
298 | if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1])) |
293 | { // checksum valid |
299 | { // checksum valid |
294 | rxd_buffer_locked = TRUE; // lock the rxd buffer |
300 | rxd_buffer_locked = TRUE; // lock the rxd buffer |
295 | ReceivedBytes = ptr_rxd_buffer; // store number of received bytes |
301 | ReceivedBytes = ptr_rxd_buffer; // store number of received bytes |
296 | rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character |
302 | rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character |
297 | // if 2nd byte is an 'R' enable watchdog that will result in an reset |
303 | // if 2nd byte is an 'R' enable watchdog that will result in an reset |
298 | if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando |
304 | if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando |
299 | } |
305 | } |
300 | else |
306 | else |
301 | { // checksum invalid |
307 | { // checksum invalid |
302 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
308 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
303 | } |
309 | } |
304 | ptr_rxd_buffer = 0; // reset rxd buffer pointer |
310 | ptr_rxd_buffer = 0; // reset rxd buffer pointer |
305 | } |
311 | } |
306 | } |
312 | } |
307 | else // rxd buffer overrun |
313 | else // rxd buffer overrun |
308 | { |
314 | { |
309 | ptr_rxd_buffer = 0; // reset rxd buffer |
315 | ptr_rxd_buffer = 0; // reset rxd buffer |
310 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
316 | rxd_buffer_locked = FALSE; // unlock rxd buffer |
311 | } |
317 | } |
312 | } |
318 | } |
313 | 319 | ||
314 | 320 | ||
315 | // -------------------------------------------------------------------------- |
321 | // -------------------------------------------------------------------------- |
316 | void AddCRC(uint16_t datalen) |
322 | void AddCRC(uint16_t datalen) |
317 | { |
323 | { |
318 | uint16_t tmpCRC = 0, i; |
324 | uint16_t tmpCRC = 0, i; |
319 | for(i = 0; i < datalen; i++) |
325 | for(i = 0; i < datalen; i++) |
320 | { |
326 | { |
321 | tmpCRC += txd_buffer[i]; |
327 | tmpCRC += txd_buffer[i]; |
322 | } |
328 | } |
323 | tmpCRC %= 4096; |
329 | tmpCRC %= 4096; |
324 | txd_buffer[i++] = '=' + tmpCRC / 64; |
330 | txd_buffer[i++] = '=' + tmpCRC / 64; |
325 | txd_buffer[i++] = '=' + tmpCRC % 64; |
331 | txd_buffer[i++] = '=' + tmpCRC % 64; |
326 | txd_buffer[i++] = '\r'; |
332 | txd_buffer[i++] = '\r'; |
327 | txd_complete = FALSE; |
333 | txd_complete = FALSE; |
328 | UDR0 = txd_buffer[0]; // initiates the transmission (continued in the TXD ISR) |
334 | UDR0 = txd_buffer[0]; // initiates the transmission (continued in the TXD ISR) |
329 | } |
335 | } |
330 | 336 | ||
331 | 337 | ||
332 | 338 | ||
333 | // -------------------------------------------------------------------------- |
339 | // -------------------------------------------------------------------------- |
334 | void SendOutData(uint8_t cmd,uint8_t module, uint8_t *snd, uint8_t len) |
340 | void SendOutData(uint8_t cmd,uint8_t module, uint8_t *snd, uint8_t len) |
335 | { |
341 | { |
336 | uint16_t pt = 0; |
342 | uint16_t pt = 0; |
337 | uint8_t a,b,c; |
343 | uint8_t a,b,c; |
338 | uint8_t ptr = 0; |
344 | uint8_t ptr = 0; |
339 | 345 | ||
340 | txd_buffer[pt++] = '#'; // Start character |
346 | txd_buffer[pt++] = '#'; // Start character |
341 | txd_buffer[pt++] = module; // Address (a=0; b=1,...) |
347 | txd_buffer[pt++] = module; // Address (a=0; b=1,...) |
342 | txd_buffer[pt++] = cmd; // Command |
348 | txd_buffer[pt++] = cmd; // Command |
343 | 349 | ||
344 | while(len) |
350 | while(len) |
345 | { |
351 | { |
346 | if(len) { a = snd[ptr++]; len--;} else a = 0; |
352 | if(len) { a = snd[ptr++]; len--;} else a = 0; |
347 | if(len) { b = snd[ptr++]; len--;} else b = 0; |
353 | if(len) { b = snd[ptr++]; len--;} else b = 0; |
348 | if(len) { c = snd[ptr++]; len--;} else c = 0; |
354 | if(len) { c = snd[ptr++]; len--;} else c = 0; |
349 | txd_buffer[pt++] = '=' + (a >> 2); |
355 | txd_buffer[pt++] = '=' + (a >> 2); |
350 | txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
356 | txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
351 | txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
357 | txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
352 | txd_buffer[pt++] = '=' + ( c & 0x3f); |
358 | txd_buffer[pt++] = '=' + ( c & 0x3f); |
353 | } |
359 | } |
354 | AddCRC(pt); // add checksum after data block and initates the transmission |
360 | AddCRC(pt); // add checksum after data block and initates the transmission |
355 | } |
361 | } |
356 | 362 | ||
357 | 363 | ||
358 | // -------------------------------------------------------------------------- |
364 | // -------------------------------------------------------------------------- |
359 | void Decode64(uint8_t *ptrOut, uint8_t len, uint8_t ptrIn, uint8_t max) |
365 | void Decode64(uint8_t *ptrOut, uint8_t len, uint8_t ptrIn, uint8_t max) |
360 | { |
366 | { |
361 | uint8_t a,b,c,d; |
367 | uint8_t a,b,c,d; |
362 | uint8_t ptr = 0; |
368 | uint8_t ptr = 0; |
363 | uint8_t x,y,z; |
369 | uint8_t x,y,z; |
364 | while(len) |
370 | while(len) |
365 | { |
371 | { |
366 | a = rxd_buffer[ptrIn++] - '='; |
372 | a = rxd_buffer[ptrIn++] - '='; |
367 | b = rxd_buffer[ptrIn++] - '='; |
373 | b = rxd_buffer[ptrIn++] - '='; |
368 | c = rxd_buffer[ptrIn++] - '='; |
374 | c = rxd_buffer[ptrIn++] - '='; |
369 | d = rxd_buffer[ptrIn++] - '='; |
375 | d = rxd_buffer[ptrIn++] - '='; |
370 | if(ptrIn > max - 2) break; |
376 | if(ptrIn > max - 2) break; |
371 | 377 | ||
372 | x = (a << 2) | (b >> 4); |
378 | x = (a << 2) | (b >> 4); |
373 | y = ((b & 0x0f) << 4) | (c >> 2); |
379 | y = ((b & 0x0f) << 4) | (c >> 2); |
374 | z = ((c & 0x03) << 6) | d; |
380 | z = ((c & 0x03) << 6) | d; |
375 | 381 | ||
376 | if(len--) ptrOut[ptr++] = x; else break; |
382 | if(len--) ptrOut[ptr++] = x; else break; |
377 | if(len--) ptrOut[ptr++] = y; else break; |
383 | if(len--) ptrOut[ptr++] = y; else break; |
378 | if(len--) ptrOut[ptr++] = z; else break; |
384 | if(len--) ptrOut[ptr++] = z; else break; |
379 | } |
385 | } |
380 | } |
386 | } |
381 | 387 | ||
382 | 388 | ||
383 | // -------------------------------------------------------------------------- |
389 | // -------------------------------------------------------------------------- |
384 | int16_t uart_putchar (int8_t c) |
390 | int uart_putchar (int8_t c) |
385 | { |
391 | { |
386 | // if tx is not enabled return immediatly |
392 | // if tx is not enabled return immediatly |
387 | if(!(UCSR0B & (1 << TXEN0))) return (0); |
393 | if(!(UCSR0B & (1 << TXEN0))) return (0); |
388 | if (c == '\n') uart_putchar('\r'); |
394 | if (c == '\n') uart_putchar('\r'); |
389 | // wait until previous character was send |
395 | // wait until previous character was send |
390 | loop_until_bit_is_set(UCSR0A, UDRE0); |
396 | loop_until_bit_is_set(UCSR0A, UDRE0); |
391 | // send character |
397 | // send character |
392 | UDR0 = c; |
398 | UDR0 = c; |
393 | return (0); |
399 | return (0); |
394 | } |
400 | } |
395 | 401 | ||
396 | 402 | ||
397 | // -------------------------------------------------------------------------- |
403 | // -------------------------------------------------------------------------- |
398 | void USART0_ProcessRxData(void) |
404 | void USART0_ProcessRxData(void) |
399 | { |
405 | { |
400 | // if data in the rxd buffer are not locked immediately return |
406 | // if data in the rxd buffer are not locked immediately return |
401 | if(!rxd_buffer_locked) return; |
407 | if(!rxd_buffer_locked) return; |
402 | 408 | ||
403 | uint8_t tmp_char_arr2[2]; // local char buffer |
409 | uint8_t tmp_char_arr2[2]; // local char buffer |
404 | 410 | ||
405 | switch(rxd_buffer[2]) |
411 | switch(rxd_buffer[2]) |
406 | { |
412 | { |
407 | case 'w':// Attitude |
413 | case 'w':// Attitude |
408 | Decode64((uint8_t *) &ExternData, sizeof(ExternData), 3, ReceivedBytes); |
414 | Decode64((uint8_t *) &ExternData, sizeof(ExternData), 3, ReceivedBytes); |
409 | DebugOut.Analog[15]++; |
- | |
410 | RequestCompassHeading = TRUE; |
415 | RequestCompassHeading = TRUE; |
411 | break; |
416 | break; |
412 | 417 | ||
413 | case 'b': // extern control |
418 | case 'b': // extern control |
414 | case 'c': // extern control with debug request |
419 | case 'c': // extern control with debug request |
415 | Decode64((uint8_t *) &ExternControl,sizeof(ExternControl), 3,ReceivedBytes); |
420 | Decode64((uint8_t *) &ExternControl,sizeof(ExternControl), 3,ReceivedBytes); |
- | 421 | #define KEY1 0x01 |
|
- | 422 | #define KEY2 0x02 |
|
- | 423 | #define KEY3 0x04 |
|
- | 424 | #define KEY4 0x08 |
|
- | 425 | #define KEY5 0x10 |
|
- | 426 | // use right arrow at display for switching the calstate |
|
- | 427 | if(ExternControl.RemoteButtons & KEY2) |
|
- | 428 | { |
|
- | 429 | ExternData.CalState++; |
|
- | 430 | if(ExternData.CalState == 6) ExternData.CalState = 0; |
|
- | 431 | } |
|
416 | ExternData.Attitude[0] = ExternControl.Par1; |
432 | ExternData.Attitude[0] = ExternControl.Par1; |
417 | ExternData.Attitude[1] = ExternControl.Par2; |
433 | ExternData.Attitude[1] = ExternControl.Par2; |
- | 434 | PC_Connected = 255; |
|
418 | break; |
435 | break; |
419 | 436 | ||
420 | case 'h':// x-1 display columns |
437 | case 'h':// x-1 display columns |
421 | PC_Connected = 255; |
438 | PC_Connected = 255; |
422 | break; |
439 | break; |
423 | 440 | ||
424 | case 'v': // get version and board release |
441 | case 'v': // get version and board release |
425 | RequestVerInfo = TRUE; |
442 | RequestVerInfo = TRUE; |
426 | PC_Connected = 255; |
443 | PC_Connected = 255; |
427 | break; |
444 | break; |
428 | 445 | ||
429 | case 'a':// Labels of the Analog Debug outputs |
446 | case 'a':// Labels of the Analog Debug outputs |
430 | Decode64((uint8_t *) &tmp_char_arr2[0], sizeof(tmp_char_arr2), 3, ReceivedBytes); |
447 | Decode64((uint8_t *) &tmp_char_arr2[0], sizeof(tmp_char_arr2), 3, ReceivedBytes); |
431 | RequestDebugLabel = tmp_char_arr2[0]; |
448 | RequestDebugLabel = tmp_char_arr2[0]; |
432 | PC_Connected = 255; |
449 | PC_Connected = 255; |
433 | break; |
450 | break; |
434 | 451 | ||
435 | case 'g':// get debug data |
452 | case 'g':// get debug data |
436 | RequestDebugData = TRUE; |
453 | RequestDebugData = TRUE; |
437 | PC_Connected = 255; |
454 | PC_Connected = 255; |
438 | break; |
455 | break; |
439 | } |
456 | } |
440 | // unlock the rxd buffer after processing |
457 | // unlock the rxd buffer after processing |
441 | rxd_buffer_locked = FALSE; |
458 | rxd_buffer_locked = FALSE; |
442 | } |
459 | } |
443 | 460 | ||
444 | 461 | ||
445 | 462 | ||
446 | //--------------------------------------------------------------------------------------------- |
463 | //--------------------------------------------------------------------------------------------- |
447 | void USART0_TransmitTxData(void) |
464 | void USART0_TransmitTxData(void) |
448 | { |
465 | { |
449 | if(!(UCSR0B & (1 << TXEN0))) return; |
466 | if(!(UCSR0B & (1 << TXEN0))) return; |
450 | 467 | ||
451 | if(!txd_complete) return; |
468 | if(!txd_complete) return; |
452 | 469 | ||
453 | if(CheckDelay(Debug_Timer) && txd_complete) |
470 | if(CheckDelay(Debug_Timer) && txd_complete) |
454 | { |
471 | { |
455 | SetDebugValues(); |
472 | SetDebugValues(); |
456 | SendOutData('D',MySlaveAddr,(uint8_t *) &DebugOut,sizeof(DebugOut)); |
473 | SendOutData('D',MySlaveAddr,(uint8_t *) &DebugOut,sizeof(DebugOut)); |
457 | Debug_Timer = SetDelay(250); |
474 | Debug_Timer = SetDelay(250); |
458 | } |
475 | } |
459 | 476 | ||
460 | if(RequestDebugLabel != 255) |
477 | if(RequestDebugLabel != 255) |
461 | { |
478 | { |
462 | SendOutData('A',RequestDebugLabel + '0',(uint8_t *) ANALOG_LABEL[RequestDebugLabel],16); |
479 | SendOutData('A',RequestDebugLabel + '0',(uint8_t *) ANALOG_LABEL[RequestDebugLabel],16); |
463 | RequestDebugLabel = 255; |
480 | RequestDebugLabel = 255; |
464 | } |
481 | } |
465 | 482 | ||
466 | if(RequestVerInfo && txd_complete) |
483 | if(RequestVerInfo && txd_complete) |
467 | { |
484 | { |
468 | SendOutData('V',MySlaveAddr,(uint8_t *) &VersionInfo, sizeof(VersionInfo)); |
485 | SendOutData('V',MySlaveAddr,(uint8_t *) &VersionInfo, sizeof(VersionInfo)); |
469 | RequestVerInfo = FALSE; |
486 | RequestVerInfo = FALSE; |
470 | } |
487 | } |
471 | 488 | ||
472 | if(RequestDebugData && txd_complete) |
489 | if(RequestDebugData && txd_complete) |
473 | { |
490 | { |
474 | SetDebugValues(); |
491 | SetDebugValues(); |
475 | SendOutData('G',MySlaveAddr,(uint8_t *) &ExternControl,sizeof(ExternControl)); |
492 | SendOutData('G',MySlaveAddr,(uint8_t *) &ExternControl,sizeof(ExternControl)); |
476 | RequestDebugData = FALSE; |
493 | RequestDebugData = FALSE; |
477 | } |
494 | } |
478 | 495 | ||
479 | if(RequestCompassHeading && txd_complete) |
496 | if(RequestCompassHeading && txd_complete) |
480 | { |
497 | { |
481 | SendOutData('K',MySlaveAddr,(uint8_t *) &I2C_Heading, sizeof(I2C_Heading)); |
498 | SendOutData('K',MySlaveAddr,(uint8_t *) &I2C_Heading, sizeof(I2C_Heading)); |
482 | RequestCompassHeading = FALSE; |
499 | RequestCompassHeading = FALSE; |
483 | } |
500 | } |
484 | } |
501 | } |
485 | 502 |