Subversion Repositories MK3Mag

Rev

Rev 21 | Rev 23 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 21 Rev 22
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  150
69
#define TXD_BUFFER_LEN  150
70
#define RXD_BUFFER_LEN  150
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
 
77
 
78
 
78
 
79
 
79
 
80
uint8_t RequestVerInfo                  = FALSE;
80
uint8_t RequestVerInfo                  = FALSE;
81
uint8_t RequestDebugData                = FALSE;
81
uint8_t RequestDebugData                = FALSE;
82
uint8_t RequestDebugLabel               = FALSE;
82
uint8_t RequestDebugLabel               = FALSE;
83
uint8_t RequestCompassHeading   = FALSE;
83
uint8_t RequestCompassHeading   = FALSE;
84
 
84
 
85
uint8_t MySlaveAddr = 0;
85
uint8_t MySlaveAddr = 0;
86
 
86
 
87
//unsigned char,DebugGetAnforderung = 0,
87
//unsigned char,DebugGetAnforderung = 0,
88
 
88
 
89
DebugOut_t              DebugOut;
89
DebugOut_t              DebugOut;
90
ExternData_t    ExternData;
90
ExternData_t    ExternData;
91
ExternControl_t ExternControl;
91
ExternControl_t ExternControl;
92
VersionInfo_t   VersionInfo;
92
VersionInfo_t   VersionInfo;
93
 
93
 
94
uint16_t  Debug_Timer;
94
uint16_t  Debug_Timer;
95
 
95
 
96
const uint8_t ANALOG_LABEL[32][16] =
96
const uint8_t ANALOG_LABEL[32][16] =
97
{
97
{
98
   //1234567890123456
98
   //1234567890123456
99
    "Magnet X        ", //0
99
    "Magnet X        ", //0
100
    "Magnet Y        ",
100
    "Magnet Y        ",
101
    "Magnet Z        ",
101
    "Magnet Z        ",
102
    "Raw X           ",
102
    "Raw X           ",
103
    "Raw Y           ",
103
    "Raw Y           ",
104
    "Raw Z           ", //5
104
    "Raw Z           ", //5
105
    "Attitude Nick   ",
105
    "Attitude Nick   ",
106
    "Attitude Roll   ",
106
    "Attitude Roll   ",
107
    "XOffset         ",
107
    "XOffset         ",
108
    "XRange          ",
108
    "XRange          ",
109
    "YOffset         ", //10
109
    "YOffset         ", //10
110
    "YRange          ",
110
    "YRange          ",
111
    "ZOffset         ",
111
    "ZOffset         ",
112
    "ZRange          ",
112
    "ZRange          ",
113
    "Calstate        ",
113
    "Calstate        ",
114
    "Heading         ", //15
114
    "Heading         ", //15
115
    "User0           ",
115
    "User0           ",
116
    "User1           ",
116
    "User1           ",
117
    "Analog18        ",
117
    "Analog18        ",
118
    "Analog19        ",
118
    "Analog19        ",
119
    "Analog20        ", //20
119
    "Analog20        ", //20
120
    "Analog21        ",
120
    "Analog21        ",
121
    "Analog22        ",
121
    "Analog22        ",
122
    "Analog23        ",
122
    "Analog23        ",
123
    "Analog24        ",
123
    "Analog24        ",
124
    "Analog25        ", //25
124
    "Analog25        ", //25
125
    "Analog26        ",
125
    "Analog26        ",
126
    "Analog27        ",
126
    "Analog27        ",
127
    "Analog28        ",
127
    "Analog28        ",
128
    "Analog29        ",
128
    "Analog29        ",
129
    "Analog30        ", //30
129
    "Analog30        ", //30
130
    "Analog31        "
130
    "Analog31        "
131
};
131
};
132
 
132
 
133
 
133
 
134
/****************************************************************/
134
/****************************************************************/
135
/*              Initialization of the USART0                    */
135
/*              Initialization of the USART0                    */
136
/****************************************************************/
136
/****************************************************************/
137
void USART0_Init (void)
137
void USART0_Init (void)
138
{
138
{
139
        uint8_t sreg = SREG;
-
 
140
 
-
 
141
        uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * BAUD_RATE) - 1);
139
        uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * BAUD_RATE) - 1);
142
 
140
 
143
        // disable all interrupts before configuration
141
        // disable all interrupts before configuration
144
        cli();
142
        cli();
145
 
143
 
146
        // disable RX-Interrupt
144
        // disable RX-Interrupt
147
        UCSR0B &= ~(1 << RXCIE0);
145
        UCSR0B &= ~(1 << RXCIE0);
148
        // disable TX-Interrupt
146
        // disable TX-Interrupt
149
        UCSR0B &= ~(1 << TXCIE0);
147
        UCSR0B &= ~(1 << TXCIE0);
150
 
148
 
151
        // set direction of RXD0 and TXD0 pins
149
        // set direction of RXD0 and TXD0 pins
152
 
150
 
153
        // set RXD0 (PD0) as an input pin
-
 
154
        PORTD |= (1 << PORTD0);
151
        // set RXD0 (PD0) as an input pin tristate
-
 
152
        DDRD  &= ~(1 << DDD0);
155
        DDRD  &= ~(1 << DDD0);
153
        PORTD &= ~(1 << PORTD0);
156
        // set TXD0 (PD1) as an output pin
-
 
157
        PORTD |= (1 << PORTD1);
154
        // set TXD0 (PD1) as an output pin
-
 
155
        DDRD  |= (1 << DDD1);
-
 
156
        PORTD &= ~(1 << PORTD1);
158
        DDRD  |= (1 << DDD1);
157
 
159
 
158
 
160
        // USART0 Baud Rate Register
159
        // USART0 Baud Rate Register
161
        // set clock divider
160
        // set clock divider
162
        UBRR0H = (uint8_t)(ubrr >> 8);
161
        UBRR0H = (uint8_t)(ubrr >> 8);
163
        UBRR0L = (uint8_t)ubrr;
162
        UBRR0L = (uint8_t)ubrr;
164
 
163
 
165
        // USART0 Control and Status Register A, B, C
164
        // USART0 Control and Status Register A, B, C
166
 
165
 
167
        // enable double speed operation
166
        // enable double speed operation
168
        UCSR0A |= (1 << U2X0);
167
        UCSR0A |= (1 << U2X0);
169
        // enable receiver and transmitter
-
 
170
        UCSR0B |= (1 << TXEN0) | (1 << RXEN0);
-
 
-
 
168
 
171
        // set asynchronous mode
169
        // set asynchronous mode
172
        UCSR0C &= ~(1 << UMSEL01);
170
        UCSR0C &= ~(1 << UMSEL01);
173
        UCSR0C &= ~(1 << UMSEL00);
171
        UCSR0C &= ~(1 << UMSEL00);
174
        // no parity
172
        // no parity
175
        UCSR0C &= ~(1 << UPM01);
173
        UCSR0C &= ~(1 << UPM01);
176
        UCSR0C &= ~(1 << UPM00);
174
        UCSR0C &= ~(1 << UPM00);
177
        // 1 stop bit
175
        // 1 stop bit
178
        UCSR0C &= ~(1 << USBS0);
176
        UCSR0C &= ~(1 << USBS0);
179
        // 8-bit
177
        // 8-bit
180
        UCSR0B &= ~(1 << UCSZ02);
178
        UCSR0B &= ~(1 << UCSZ02);
181
        UCSR0C |=  (1 << UCSZ01);
179
        UCSR0C |=  (1 << UCSZ01);
182
        UCSR0C |=  (1 << UCSZ00);
180
        UCSR0C |=  (1 << UCSZ00);
-
 
181
 
-
 
182
        // enable receiver and transmitter
-
 
183
        UCSR0B |= (1 << RXEN0);
-
 
184
        UCSR0B |= (1 << TXEN0);
183
 
185
 
184
        // flush receive buffer
186
        // flush receive buffer
185
        while ( UCSR0A & (1<<RXC0) ) UDR0;
187
        while ( UCSR0A & (1<<RXC0) ) UDR0;
186
 
-
 
187
        // enable interrupts at the end
188
 
188
        // enable RX-Interrupt
189
        // enable RX-Interrupt
189
        UCSR0B |= (1 << RXCIE0);
190
        UCSR0B |= (1 << RXCIE0);
190
        // enable TX-Interrupt
191
        // enable TX-Interrupt
191
        UCSR0B |= (1 << TXCIE0);
192
        UCSR0B |= (1 << TXCIE0);
192
 
193
 
193
        rxd_buffer_locked = FALSE;
194
        rxd_buffer_locked = FALSE;
194
        txd_complete = TRUE;
195
        txd_complete = TRUE;
195
 
-
 
196
        Debug_Timer = SetDelay(200);
-
 
197
 
-
 
198
 
-
 
199
        // restore global interrupt flags
-
 
200
    SREG = sreg;
196
 
201
 
197
 
202
        VersionInfo.Major = VERSION_MAJOR;
198
        VersionInfo.Major = VERSION_MAJOR;
203
        VersionInfo.Minor = VERSION_MINOR;
199
        VersionInfo.Minor = VERSION_MINOR;
204
    VersionInfo.PCCompatible = VERSION_COMPATIBLE;
200
    VersionInfo.PCCompatible = VERSION_COMPATIBLE;
-
 
201
 
205
 
202
 
-
 
203
    // Version beim Start ausgeben (nicht schön, aber geht... )
-
 
204
        uart_putchar ('\n');
-
 
205
        uart_putchar ('C');
-
 
206
        uart_putchar ('P');
-
 
207
        uart_putchar (':');
-
 
208
        uart_putchar ('V');
206
        // Version beim Start ausgeben (nicht schön, aber geht... )
209
        uart_putchar (0x30 + VERSION_MAJOR);
207
        uart_putchar ('\n');uart_putchar ('C');uart_putchar ('P');uart_putchar (':');
210
        uart_putchar ('.');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);
211
        uart_putchar (0x30 + VERSION_MINOR%10);
209
    uart_putchar ('\n');
212
    uart_putchar ('\n');
210
}
213
}
211
 
214
 
212
// ---------------------------------------------------------------------------------
215
// ---------------------------------------------------------------------------------
213
void USART0_EnableTXD(void)
216
void USART0_EnableTXD(void)
214
{
217
{
215
        //if(!(UCSR0B & (1 << TXEN0))) return;
-
 
216
        DDRD |= (1<<DDD1);              // set TXD pin as output
218
        DDRD |= (1<<DDD1);                      // set TXD pin as output
-
 
219
        PORTD &= ~(1 << PORTD1);
217
        UCSR0B |= (1 << TXEN0);         // enable TX in USART
220
        UCSR0B |= (1 << TXEN0);         // enable TX in USART
-
 
221
        UCSR0B |= (1 << TXCIE0);        // disable TX-Interrupt
218
}
222
}
219
 
223
 
220
// ---------------------------------------------------------------------------------
224
// ---------------------------------------------------------------------------------
221
void USART0_DisableTXD(void)
225
void USART0_DisableTXD(void)
222
{
226
{
223
        //if((UCSR0B & (1 << TXEN0))) return;
-
 
224
        while(!txd_complete){ };
227
        while(!txd_complete){ };
-
 
228
 
225
 
229
        UCSR0B &= ~(1 << TXCIE0);   // disable TX-Interrupt
226
        UCSR0B &= ~(1 << TXEN0);        // disable TXD in USART
230
        UCSR0B &= ~(1 << TXEN0);        // disable TXD in USART
-
 
231
        DDRD &= ~(1<<DDD1);             // set TXD pin as input
227
        DDRD &= ~(1<<DDD1);             // set TXD pin as input
232
        PORTD &= ~(1 << PORTD1);
228
}
233
}
229
 
234
 
230
/****************************************************************/
235
/****************************************************************/
231
/*               USART0 transmitter ISR                         */
236
/*               USART0 transmitter ISR                         */
232
/****************************************************************/
237
/****************************************************************/
233
ISR(USART_TX_vect)
238
ISR(USART_TX_vect)
234
{
239
{
235
        static uint16_t ptr_txd_buffer = 0;
240
        static uint16_t ptr_txd_buffer = 0;
236
        uint8_t tmp_tx;
241
        uint8_t tmp_tx;
237
        if(!txd_complete) // transmission not completed
242
        if(!txd_complete) // transmission not completed
238
        {
243
        {
239
                ptr_txd_buffer++;                    // die [0] wurde schon gesendet
244
                ptr_txd_buffer++;                    // die [0] wurde schon gesendet
240
                tmp_tx = txd_buffer[ptr_txd_buffer];
245
                tmp_tx = txd_buffer[ptr_txd_buffer];
241
                // if terminating character or end of txd buffer was reached
246
                // if terminating character or end of txd buffer was reached
242
                if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN))
247
                if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN))
243
                {
248
                {
244
                        ptr_txd_buffer = 0; // reset txd pointer
249
                        ptr_txd_buffer = 0; // reset txd pointer
245
                        txd_complete = TRUE; // stop transmission
250
                        txd_complete = TRUE; // stop transmission
246
                }
251
                }
247
                UDR0 = tmp_tx; // send current byte will trigger this ISR again
252
                UDR0 = tmp_tx; // send current byte will trigger this ISR again
248
        }
253
        }
249
        // transmission completed
254
        // transmission completed
250
        else ptr_txd_buffer = 0;
255
        else ptr_txd_buffer = 0;
251
}
256
}
252
 
257
 
253
/****************************************************************/
258
/****************************************************************/
254
/*               USART0 receiver ISR                            */
259
/*               USART0 receiver ISR                            */
255
/****************************************************************/
260
/****************************************************************/
256
ISR(USART_RX_vect)
261
ISR(USART_RX_vect)
257
{
262
{
258
        static uint16_t crc;
263
        static uint16_t crc;
259
        uint8_t crc1, crc2;
264
        uint8_t crc1, crc2;
260
        uint8_t c;
265
        uint8_t c;
261
        static uint8_t ptr_rxd_buffer = 0;
266
        static uint8_t ptr_rxd_buffer = 0;
262
 
267
 
263
        c = UDR0;  // catch the received byte
268
        c = UDR0;  // catch the received byte
264
 
269
 
265
        if(rxd_buffer_locked) return; // if rxd buffer is locked immediately return
270
        if(rxd_buffer_locked) return; // if rxd buffer is locked immediately return
266
 
271
 
267
        // the rxd buffer is unlocked
272
        // the rxd buffer is unlocked
268
        if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received
273
        if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received
269
        {
274
        {
270
                rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer
275
                rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer
271
                crc = c; // init crc
276
                crc = c; // init crc
272
        }
277
        }
273
        #if 0
278
        #if 0
274
        else if (ptr_rxd_buffer == 1) // handle address
279
        else if (ptr_rxd_buffer == 1) // handle address
275
        {
280
        {
276
                rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
281
                rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
277
                crc += c; // update crc
282
                crc += c; // update crc
278
        }
283
        }
279
        #endif
284
        #endif
280
        else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // collect incomming bytes
285
        else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // collect incomming bytes
281
        {
286
        {
282
                if(c != '\r') // no termination character
287
                if(c != '\r') // no termination character
283
                {
288
                {
284
                        rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
289
                        rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
285
                        crc += c; // update crc
290
                        crc += c; // update crc
286
                }
291
                }
287
                else // termination character was received
292
                else // termination character was received
288
                {
293
                {
289
                        // the last 2 bytes are no subject for checksum calculation
294
                        // the last 2 bytes are no subject for checksum calculation
290
                        // they are the checksum itself
295
                        // they are the checksum itself
291
                        crc -= rxd_buffer[ptr_rxd_buffer-2];
296
                        crc -= rxd_buffer[ptr_rxd_buffer-2];
292
                        crc -= rxd_buffer[ptr_rxd_buffer-1];
297
                        crc -= rxd_buffer[ptr_rxd_buffer-1];
293
                        // calculate checksum from transmitted data
298
                        // calculate checksum from transmitted data
294
                        crc %= 4096;
299
                        crc %= 4096;
295
                        crc1 = '=' + crc / 64;
300
                        crc1 = '=' + crc / 64;
296
                        crc2 = '=' + crc % 64;
301
                        crc2 = '=' + crc % 64;
297
                        // compare checksum to transmitted checksum bytes
302
                        // compare checksum to transmitted checksum bytes
298
                        if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1]))
303
                        if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1]))
299
                        {   // checksum valid
304
                        {   // checksum valid
300
                                rxd_buffer_locked = TRUE;          // lock the rxd buffer
305
                                rxd_buffer_locked = TRUE;          // lock the rxd buffer
301
                                ReceivedBytes = ptr_rxd_buffer;    // store number of received bytes
306
                                ReceivedBytes = ptr_rxd_buffer;    // store number of received bytes
302
                                rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character
307
                                rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character
303
                                // if 2nd byte is an 'R' enable watchdog that will result in an reset
308
                                // if 2nd byte is an 'R' enable watchdog that will result in an reset
304
                                if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando
309
                                if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando
305
                        }
310
                        }
306
                        else
311
                        else
307
                        {       // checksum invalid
312
                        {       // checksum invalid
308
                                rxd_buffer_locked = FALSE; // unlock rxd buffer
313
                                rxd_buffer_locked = FALSE; // unlock rxd buffer
309
                        }
314
                        }
310
                        ptr_rxd_buffer = 0; // reset rxd buffer pointer
315
                        ptr_rxd_buffer = 0; // reset rxd buffer pointer
311
                }
316
                }
312
        }
317
        }
313
        else // rxd buffer overrun
318
        else // rxd buffer overrun
314
        {
319
        {
315
                ptr_rxd_buffer = 0; // reset rxd buffer
320
                ptr_rxd_buffer = 0; // reset rxd buffer
316
                rxd_buffer_locked = FALSE; // unlock rxd buffer
321
                rxd_buffer_locked = FALSE; // unlock rxd buffer
317
        }
322
        }
318
}
323
}
319
 
324
 
320
 
325
 
321
// --------------------------------------------------------------------------
326
// --------------------------------------------------------------------------
322
void AddCRC(uint16_t datalen)
327
void AddCRC(uint16_t datalen)
323
{
328
{
324
        uint16_t tmpCRC = 0, i;
329
        uint16_t tmpCRC = 0, i;
325
        for(i = 0; i < datalen; i++)
330
        for(i = 0; i < datalen; i++)
326
        {
331
        {
327
                tmpCRC += txd_buffer[i];
332
                tmpCRC += txd_buffer[i];
328
        }
333
        }
329
        tmpCRC %= 4096;
334
        tmpCRC %= 4096;
330
        txd_buffer[i++] = '=' + tmpCRC / 64;
335
        txd_buffer[i++] = '=' + tmpCRC / 64;
331
        txd_buffer[i++] = '=' + tmpCRC % 64;
336
        txd_buffer[i++] = '=' + tmpCRC % 64;
332
        txd_buffer[i++] = '\r';
337
        txd_buffer[i++] = '\r';
333
        txd_complete = FALSE;
338
        txd_complete = FALSE;
334
        UDR0 = txd_buffer[0]; // initiates the transmission (continued in the TXD ISR)
339
        UDR0 = txd_buffer[0]; // initiates the transmission (continued in the TXD ISR)
335
}
340
}
336
 
341
 
337
 
342
 
338
 
343
 
339
// --------------------------------------------------------------------------
344
// --------------------------------------------------------------------------
340
void SendOutData(uint8_t cmd,uint8_t module, uint8_t *snd, uint8_t len)
345
void SendOutData(uint8_t cmd,uint8_t module, uint8_t *snd, uint8_t len)
341
{
346
{
342
        uint16_t pt = 0;
347
        uint16_t pt = 0;
343
        uint8_t a,b,c;
348
        uint8_t a,b,c;
344
        uint8_t ptr = 0;
349
        uint8_t ptr = 0;
345
 
350
 
346
        txd_buffer[pt++] = '#';         // Start character
351
        txd_buffer[pt++] = '#';         // Start character
347
        txd_buffer[pt++] = module;      // Address (a=0; b=1,...)
352
        txd_buffer[pt++] = module;      // Address (a=0; b=1,...)
348
        txd_buffer[pt++] = cmd;         // Command
353
        txd_buffer[pt++] = cmd;         // Command
349
 
354
 
350
        while(len)
355
        while(len)
351
        {
356
        {
352
                if(len) { a = snd[ptr++]; len--;} else a = 0;
357
                if(len) { a = snd[ptr++]; len--;} else a = 0;
353
                if(len) { b = snd[ptr++]; len--;} else b = 0;
358
                if(len) { b = snd[ptr++]; len--;} else b = 0;
354
                if(len) { c = snd[ptr++]; len--;} else c = 0;
359
                if(len) { c = snd[ptr++]; len--;} else c = 0;
355
                txd_buffer[pt++] = '=' + (a >> 2);
360
                txd_buffer[pt++] = '=' + (a >> 2);
356
                txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
361
                txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
357
                txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
362
                txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
358
                txd_buffer[pt++] = '=' + ( c & 0x3f);
363
                txd_buffer[pt++] = '=' + ( c & 0x3f);
359
        }
364
        }
360
        AddCRC(pt); // add checksum after data block and initates the transmission
365
        AddCRC(pt); // add checksum after data block and initates the transmission
361
}
366
}
362
 
367
 
363
 
368
 
364
// --------------------------------------------------------------------------
369
// --------------------------------------------------------------------------
365
void Decode64(uint8_t *ptrOut, uint8_t len, uint8_t ptrIn, uint8_t max)
370
void Decode64(uint8_t *ptrOut, uint8_t len, uint8_t ptrIn, uint8_t max)
366
{
371
{
367
        uint8_t a,b,c,d;
372
        uint8_t a,b,c,d;
368
        uint8_t ptr = 0;
373
        uint8_t ptr = 0;
369
        uint8_t x,y,z;
374
        uint8_t x,y,z;
370
        while(len)
375
        while(len)
371
        {
376
        {
372
                a = rxd_buffer[ptrIn++] - '=';
377
                a = rxd_buffer[ptrIn++] - '=';
373
                b = rxd_buffer[ptrIn++] - '=';
378
                b = rxd_buffer[ptrIn++] - '=';
374
                c = rxd_buffer[ptrIn++] - '=';
379
                c = rxd_buffer[ptrIn++] - '=';
375
                d = rxd_buffer[ptrIn++] - '=';
380
                d = rxd_buffer[ptrIn++] - '=';
376
                if(ptrIn > max - 2) break;
381
                if(ptrIn > max - 2) break;
377
 
382
 
378
                x = (a << 2) | (b >> 4);
383
                x = (a << 2) | (b >> 4);
379
                y = ((b & 0x0f) << 4) | (c >> 2);
384
                y = ((b & 0x0f) << 4) | (c >> 2);
380
                z = ((c & 0x03) << 6) | d;
385
                z = ((c & 0x03) << 6) | d;
381
 
386
 
382
                if(len--) ptrOut[ptr++] = x; else break;
387
                if(len--) ptrOut[ptr++] = x; else break;
383
                if(len--) ptrOut[ptr++] = y; else break;
388
                if(len--) ptrOut[ptr++] = y; else break;
384
                if(len--) ptrOut[ptr++] = z; else break;
389
                if(len--) ptrOut[ptr++] = z; else break;
385
        }
390
        }
386
}
391
}
387
 
392
 
388
 
393
 
389
// --------------------------------------------------------------------------
394
// --------------------------------------------------------------------------
390
int uart_putchar (int8_t c)
395
int uart_putchar (int8_t c)
391
{
396
{
392
        // if tx is not enabled return immediatly
397
        // if tx is not enabled return immediatly
393
        if(!(UCSR0B & (1 << TXEN0))) return (0);
398
        if(!(UCSR0B & (1 << TXEN0))) return (0);
394
        if (c == '\n') uart_putchar('\r');
399
        if (c == '\n') uart_putchar('\r');
395
        // wait until previous character was send
400
        // wait until previous character was send
396
        loop_until_bit_is_set(UCSR0A, UDRE0);
401
        loop_until_bit_is_set(UCSR0A, UDRE0);
397
        // send character
402
        // send character
398
        UDR0 = c;
403
        UDR0 = c;
399
        return (0);
404
        return (0);
400
}
405
}
401
 
406
 
402
 
407
 
403
// --------------------------------------------------------------------------
408
// --------------------------------------------------------------------------
404
void USART0_ProcessRxData(void)
409
void USART0_ProcessRxData(void)
405
{
410
{
406
        // if data in the rxd buffer are not locked immediately return
411
        // if data in the rxd buffer are not locked immediately return
407
        if(!rxd_buffer_locked) return;
412
        if(!rxd_buffer_locked) return;
408
 
413
 
409
        uint8_t tmp_char_arr2[2]; // local char buffer
414
        uint8_t tmp_char_arr2[2]; // local char buffer
410
 
415
 
411
        switch(rxd_buffer[2])
416
        switch(rxd_buffer[2])
412
        {
417
        {
413
           case 'w':// Attitude
418
           case 'w':// Attitude
414
                        Decode64((uint8_t *) &ExternData, sizeof(ExternData), 3, ReceivedBytes);
419
                        Decode64((uint8_t *) &ExternData, sizeof(ExternData), 3, ReceivedBytes);
415
                        RequestCompassHeading = TRUE;
420
                        RequestCompassHeading = TRUE;
416
                        break;
421
                        break;
417
 
422
 
418
                case 'b': // extern control
423
                case 'b': // extern control
419
                case 'c': // extern control with debug request
424
                case 'c': // extern control with debug request
420
                        Decode64((uint8_t *) &ExternControl,sizeof(ExternControl), 3,ReceivedBytes);
425
                        Decode64((uint8_t *) &ExternControl,sizeof(ExternControl), 3,ReceivedBytes);
421
                        #define KEY1    0x01
426
                        #define KEY1    0x01
422
                        #define KEY2    0x02
427
                        #define KEY2    0x02
423
                        #define KEY3    0x04
428
                        #define KEY3    0x04
424
                        #define KEY4    0x08
429
                        #define KEY4    0x08
425
                        #define KEY5    0x10
430
                        #define KEY5    0x10
426
                        // use right arrow at display for switching the calstate
431
                        // use right arrow at display for switching the calstate
427
                        if(ExternControl.RemoteButtons & KEY2)
432
                        if(ExternControl.RemoteButtons & KEY2)
428
                        {
433
                        {
429
                                ExternData.CalState++;
434
                                ExternData.CalState++;
430
                                if(ExternData.CalState == 6) ExternData.CalState = 0;
435
                                if(ExternData.CalState == 6) ExternData.CalState = 0;
431
                        }
436
                        }
432
                        ExternData.Attitude[0] = ExternControl.Par1;
437
                        ExternData.Attitude[0] = ExternControl.Par1;
433
            ExternData.Attitude[1] = ExternControl.Par2;
438
            ExternData.Attitude[1] = ExternControl.Par2;
434
            PC_Connected = 255;
439
            PC_Connected = 255;
435
                        break;
440
                        break;
436
 
441
 
437
                case 'h':// x-1 display columns
442
                case 'h':// x-1 display columns
438
                        PC_Connected = 255;
443
                        PC_Connected = 255;
439
                        break;
444
                        break;
440
 
445
 
441
                case 'v': // get version and board release
446
                case 'v': // get version and board release
442
                        RequestVerInfo = TRUE;
447
                        RequestVerInfo = TRUE;
443
                        PC_Connected = 255;
448
                        PC_Connected = 255;
444
                        break;
449
                        break;
445
 
450
 
446
                case 'a':// Labels of the Analog Debug outputs
451
                case 'a':// Labels of the Analog Debug outputs
447
                        Decode64((uint8_t *) &tmp_char_arr2[0], sizeof(tmp_char_arr2), 3, ReceivedBytes);
452
                        Decode64((uint8_t *) &tmp_char_arr2[0], sizeof(tmp_char_arr2), 3, ReceivedBytes);
448
                        RequestDebugLabel = tmp_char_arr2[0];
453
                        RequestDebugLabel = tmp_char_arr2[0];
449
                        PC_Connected = 255;
454
                        PC_Connected = 255;
450
                        break;
455
                        break;
451
 
456
 
452
                case 'g':// get debug data
457
                case 'g':// get debug data
453
                        RequestDebugData = TRUE;
458
                        RequestDebugData = TRUE;
454
                        PC_Connected = 255;
459
                        PC_Connected = 255;
455
                        break;
460
                        break;
456
        }
461
        }
457
        // unlock the rxd buffer after processing
462
        // unlock the rxd buffer after processing
458
        rxd_buffer_locked = FALSE;
463
        rxd_buffer_locked = FALSE;
459
}
464
}
460
 
465
 
461
 
466
 
462
 
467
 
463
//---------------------------------------------------------------------------------------------
468
//---------------------------------------------------------------------------------------------
464
void USART0_TransmitTxData(void)
469
void USART0_TransmitTxData(void)
465
{
470
{
466
        if(!(UCSR0B & (1 << TXEN0))) return;
471
        if(!(UCSR0B & (1 << TXEN0))) return;
467
 
472
 
468
        if(!txd_complete) return;
473
        if(!txd_complete) return;
469
 
474
 
470
    if(CheckDelay(Debug_Timer) && txd_complete)
475
    if(CheckDelay(Debug_Timer) && txd_complete)
471
    {
476
    {
472
                SetDebugValues();
477
                SetDebugValues();
473
                SendOutData('D',MySlaveAddr,(uint8_t *) &DebugOut,sizeof(DebugOut));
478
                SendOutData('D',MySlaveAddr,(uint8_t *) &DebugOut,sizeof(DebugOut));
474
                Debug_Timer = SetDelay(250);
479
                Debug_Timer = SetDelay(250);
475
    }
480
    }
476
 
481
 
477
    if(RequestDebugLabel != 255)
482
    if(RequestDebugLabel != 255)
478
    {
483
    {
479
                SendOutData('A',RequestDebugLabel + '0',(uint8_t *) ANALOG_LABEL[RequestDebugLabel],16);
484
                SendOutData('A',RequestDebugLabel + '0',(uint8_t *) ANALOG_LABEL[RequestDebugLabel],16);
480
                RequestDebugLabel = 255;
485
                RequestDebugLabel = 255;
481
        }
486
        }
482
 
487
 
483
        if(RequestVerInfo && txd_complete)
488
        if(RequestVerInfo && txd_complete)
484
    {
489
    {
485
                SendOutData('V',MySlaveAddr,(uint8_t *) &VersionInfo, sizeof(VersionInfo));
490
                SendOutData('V',MySlaveAddr,(uint8_t *) &VersionInfo, sizeof(VersionInfo));
486
                RequestVerInfo = FALSE;
491
                RequestVerInfo = FALSE;
487
    }
492
    }
488
 
493
 
489
        if(RequestDebugData && txd_complete)
494
        if(RequestDebugData && txd_complete)
490
        {
495
        {
491
                SetDebugValues();
496
                SetDebugValues();
492
                SendOutData('G',MySlaveAddr,(uint8_t *) &ExternControl,sizeof(ExternControl));
497
                SendOutData('G',MySlaveAddr,(uint8_t *) &ExternControl,sizeof(ExternControl));
493
                RequestDebugData = FALSE;
498
                RequestDebugData = FALSE;
494
        }
499
        }
495
 
500
 
496
    if(RequestCompassHeading && txd_complete)
501
    if(RequestCompassHeading && txd_complete)
497
        {
502
        {
498
                SendOutData('K',MySlaveAddr,(uint8_t *) &I2C_Heading, sizeof(I2C_Heading));
503
                SendOutData('K',MySlaveAddr,(uint8_t *) &I2C_Heading, sizeof(I2C_Heading));
499
                RequestCompassHeading = FALSE;
504
                RequestCompassHeading = FALSE;
500
        }
505
        }
501
}
506
}
502
 
507