Subversion Repositories MK3Mag

Rev

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

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