Subversion Repositories MK3Mag

Rev

Rev 18 | Rev 21 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 18 Rev 19
Line 1... Line 1...
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
Line 27... Line 27...
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
 
-
 
-
 
57
#include <avr/io.h>
-
 
58
#include <avr/interrupt.h>
-
 
59
#include <avr/wdt.h>
58
#include "main.h"
60
#include "main.h"
59
#include "uart.h"
61
#include "uart.h"
-
 
62
#include "timer0.h"
-
 
63
#include "twislave.h"
-
 
64
 
-
 
65
 
-
 
66
#define FALSE   0
-
 
67
#define TRUE    1
-
 
68
 
-
 
69
#define TXD_BUFFER_LEN  100
-
 
70
#define RXD_BUFFER_LEN  100
Line -... Line 71...
-
 
71
 
-
 
72
volatile uint8_t txd_buffer[TXD_BUFFER_LEN];
-
 
73
volatile uint8_t rxd_buffer_locked = FALSE;
60
 
74
volatile uint8_t rxd_buffer[RXD_BUFFER_LEN];
61
#define MAX_SENDE_BUFF     100
75
volatile uint8_t txd_complete = TRUE;
Line 62... Line -...
62
#define MAX_EMPFANGS_BUFF  100
-
 
63
 
76
volatile uint8_t ReceivedBytes = 0;
64
unsigned volatile char SIO_Sollwert = 0;
-
 
65
unsigned volatile char SioTmp = 0;
-
 
66
unsigned volatile char SendeBuffer[MAX_SENDE_BUFF];
-
 
67
unsigned volatile char RxdBuffer[MAX_EMPFANGS_BUFF];
-
 
68
unsigned volatile char NeuerDatensatzEmpfangen = 0;
-
 
69
unsigned volatile char UebertragungAbgeschlossen = 1;
-
 
70
unsigned char GetVersionAnforderung = 0,DebugTextAnforderung = 0,DebugGetAnforderung = 0, KompassAntwort = 0;
-
 
71
unsigned char MeineSlaveAdresse;
-
 
72
unsigned char MotorTest[4] = {0,0,0,0};
-
 
73
unsigned volatile char AnzahlEmpfangsBytes = 0;
-
 
74
unsigned char PcZugriff;
77
 
75
 
78
uint8_t RequestVerInfo                  = FALSE;
76
struct str_DebugOut       DebugOut;
79
uint8_t RequestDebugData                = FALSE;
77
struct str_ExternData     ExternData;
-
 
Line -... Line 80...
-
 
80
uint8_t RequestDebugLabel               = FALSE;
Line -... Line 81...
-
 
81
uint8_t RequestCompassHeading   = FALSE;
Line 78... Line 82...
78
struct str_ExternControl  ExternControl;
82
 
-
 
83
uint8_t MySlaveAddr = 0;
-
 
84
 
-
 
85
//unsigned char,DebugGetAnforderung = 0,
Line -... Line 86...
-
 
86
 
-
 
87
DebugOut_t              DebugOut;
79
struct str_VersionInfo    VersionInfo;
88
ExternData_t    ExternData;
80
 
89
ExternControl_t ExternControl;
81
 
90
VersionInfo_t   VersionInfo;
82
 
91
 
83
int Debug_Timer;
92
uint16_t  Debug_Timer;
84
 
93
 
85
const unsigned char ANALOG_TEXT[32][16] =
94
const uint8_t ANALOG_LABEL[32][16] =
86
{
95
{
87
   //1234567890123456
96
   //1234567890123456
88
    "Magnet N        ", //0
97
    "Magnet X        ", //0
89
    "Magnet R        ",
98
    "Magnet Y        ",
90
    "Magnet Z        ",
99
    "Magnet Z        ",
91
    "Raw    N        ",
100
    "Raw X           ",
92
    "Raw    R        ",
101
    "Raw Y           ",
93
    "Raw    Z        ", //5
102
    "Raw Z           ", //5
94
    "Lage N          ",
103
    "Attitude Nick   ",
95
    "Lage R          ",
104
    "Attitude Roll   ",
96
    "Xmin            ",
105
    "XOffset         ",
97
    "Xmax            ",
106
    "XRange          ",
98
    "Ymin            ", //10
107
    "YOffset         ", //10
99
    "Ymax            ",
108
    "YRange          ",
100
    "Zmin            ",
109
    "ZOffset         ",
101
    "ZMax            ",
110
    "ZRange          ",
102
    "Calstate        ",
111
    "Calstate        ",
103
    "Kompass         ", //15
112
    "Heading Request ", //15
104
    "User0           ",
113
    "User0           ",
105
    "User1           ",
114
    "User1           ",
106
    "Analog18        ",
115
    "Analog18        ",
107
    "Analog19        ",
116
    "Analog19        ",
108
    "Analog20        ", //20
117
    "Analog20        ", //20
Line 118... Line 127...
118
    "Analog30        ", //30
127
    "Analog30        ", //30
119
    "Analog31        "
128
    "Analog31        "
120
};
129
};
Line -... Line 130...
-
 
130
 
-
 
131
 
-
 
132
/****************************************************************/
-
 
133
/*              Initialization of the USART0                    */
-
 
134
/****************************************************************/
-
 
135
void USART0_Init (void)
-
 
136
{
-
 
137
        uint8_t sreg = SREG;
-
 
138
 
-
 
139
        uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * BAUD_RATE) - 1);
-
 
140
 
-
 
141
        // disable all interrupts before configuration
-
 
142
        cli();
-
 
143
 
-
 
144
        // disable RX-Interrupt
-
 
145
        UCSR0B &= ~(1 << RXCIE0);
-
 
146
        // disable TX-Interrupt
-
 
147
        UCSR0B &= ~(1 << TXCIE0);
-
 
148
 
-
 
149
        // set direction of RXD0 and TXD0 pins
-
 
150
 
-
 
151
        // set RXD0 (PD0) as an input pin
-
 
152
        PORTD |= (1 << PORTD0);
-
 
153
        DDRD  &= ~(1 << DDD0);
-
 
154
        // set TXD0 (PD1) as an output pin
-
 
155
        PORTD |= (1 << PORTD1);
-
 
156
        DDRD  |= (1 << DDD1);
-
 
157
 
-
 
158
        // USART0 Baud Rate Register
-
 
159
        // set clock divider
-
 
160
        UBRR0H = (uint8_t)(ubrr >> 8);
-
 
161
        UBRR0L = (uint8_t)ubrr;
-
 
162
 
-
 
163
        // USART0 Control and Status Register A, B, C
-
 
164
 
-
 
165
        // enable double speed operation in
-
 
166
        UCSR0A |= (1 << U2X0);
-
 
167
        // enable receiver and transmitter in
-
 
168
        UCSR0B |= (1 << TXEN0) | (1 << RXEN0);
-
 
169
        // set asynchronous mode
-
 
170
        UCSR0C &= ~(1 << UMSEL01);
-
 
171
        UCSR0C &= ~(1 << UMSEL00);
-
 
172
        // no parity
-
 
173
        UCSR0C &= ~(1 << UPM01);
-
 
174
        UCSR0C &= ~(1 << UPM00);
-
 
175
        // 1 stop bit
-
 
176
        UCSR0C &= ~(1 << USBS0);
-
 
177
        // 8-bit
-
 
178
        UCSR0B &= ~(1 << UCSZ02);
-
 
179
        UCSR0C |=  (1 << UCSZ01);
-
 
180
        UCSR0C |=  (1 << UCSZ00);
-
 
181
 
-
 
182
        // flush receive buffer
-
 
183
        while ( UCSR0A & (1<<RXC0) ) UDR0;
-
 
184
 
-
 
185
        // enable interrupts at the end
-
 
186
        // enable RX-Interrupt
-
 
187
        UCSR0B |= (1 << RXCIE0);
-
 
188
        // enable TX-Interrupt
-
 
189
        UCSR0B |= (1 << TXCIE0);
-
 
190
 
-
 
191
        rxd_buffer_locked = FALSE;
-
 
192
        txd_complete = TRUE;
-
 
193
 
-
 
194
        Debug_Timer = SetDelay(200);
-
 
195
 
-
 
196
 
-
 
197
        // restore global interrupt flags
-
 
198
    SREG = sreg;
-
 
199
 
-
 
200
        VersionInfo.Major = VERSION_MAJOR;
Line 121... Line -...
121
 
-
 
122
 
201
        VersionInfo.Minor = VERSION_MINOR;
123
 
-
 
124
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
125
//++ Sende-Part der Datenübertragung
-
 
126
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
127
SIGNAL(INT_VEC_TX)
-
 
128
{
-
 
129
 static unsigned int ptr = 0;
-
 
130
 unsigned char tmp_tx;
202
    VersionInfo.PCCompatible = VERSION_COMPATIBLE;
131
 if(!UebertragungAbgeschlossen)  
-
 
132
  {
203
 
133
   ptr++;                    // die [0] wurde schon gesendet
-
 
134
   tmp_tx = SendeBuffer[ptr];  
204
    // Version beim Start ausgeben (nicht schön, aber geht... )
135
   if((tmp_tx == '\r') || (ptr == MAX_SENDE_BUFF))
-
 
136
    {
-
 
137
     ptr = 0;
-
 
138
     UebertragungAbgeschlossen = 1;
-
 
139
    }
-
 
140
   UDR = tmp_tx;
205
        uart_putchar ('\n');uart_putchar ('C');uart_putchar ('P');uart_putchar (':');
Line -... Line 206...
-
 
206
        uart_putchar ('V');uart_putchar (0x30 + VERSION_MAJOR);uart_putchar ('.');uart_putchar (0x30 + VERSION_MINOR/10); uart_putchar (0x30 + VERSION_MINOR%10);
141
  }
207
    uart_putchar ('\n');
142
  else ptr = 0;
208
}
143
}
-
 
144
 
-
 
145
void SendUart(void)
-
 
146
{
-
 
147
 static unsigned int ptr = 0;
-
 
148
 unsigned char tmp_tx;
209
 
149
 if(!(USR & 0x40)) return;
-
 
150
 if(!UebertragungAbgeschlossen)  
210
// ---------------------------------------------------------------------------------
151
  {
-
 
152
   ptr++;                    // die [0] wurde schon gesendet
-
 
153
   tmp_tx = SendeBuffer[ptr];  
-
 
154
   if((tmp_tx == '\r') || (ptr == MAX_SENDE_BUFF))
-
 
155
    {
-
 
156
     ptr = 0;
-
 
157
     UebertragungAbgeschlossen = 1;
-
 
158
    }
-
 
159
   USR |= (1<TXC0);
211
void USART0_EnableTXD(void)
Line 160... Line 212...
160
   UDR = tmp_tx;
212
{
161
  }
213
        DDRD |= (1<<DDD1);              // set TXD pin as output
162
  else ptr = 0;
214
        UCSR0B |= (1 << TXEN0);         // enable TX in USART
163
}
-
 
164
 
-
 
165
// --------------------------------------------------------------------------
-
 
166
void Decode64(unsigned char *ptrOut, unsigned char len, unsigned char ptrIn,unsigned char max)  // Wohin mit den Daten; Wie lang; Wo im RxdBuffer
-
 
167
{
-
 
168
 unsigned char a,b,c,d;
-
 
169
 unsigned char ptr = 0;
-
 
170
 unsigned char x,y,z;
-
 
171
 while(len)
-
 
172
  {
-
 
173
   a = RxdBuffer[ptrIn++] - '=';
-
 
174
   b = RxdBuffer[ptrIn++] - '=';
215
}
175
   c = RxdBuffer[ptrIn++] - '=';
216
 
176
   d = RxdBuffer[ptrIn++] - '=';
-
 
177
   if(ptrIn > max - 2) break;     // nicht mehr Daten verarbeiten, als empfangen wurden
-
 
178
 
-
 
179
   x = (a << 2) | (b >> 4);
-
 
180
   y = ((b & 0x0f) << 4) | (c >> 2);
-
 
181
   z = ((c & 0x03) << 6) | d;
-
 
182
 
-
 
183
   if(len--) ptrOut[ptr++] = x; else break;
217
// ---------------------------------------------------------------------------------
Line -... Line 218...
-
 
218
void USART0_DisableTXD(void)
-
 
219
{
-
 
220
        UCSR0B &= ~(1 << TXEN0);        // disable TXD in USART
-
 
221
        DDRD &= ~(1<<DDD1);             // set TXD pin as input
-
 
222
}
-
 
223
 
-
 
224
/****************************************************************/
-
 
225
/*               USART0 transmitter ISR                         */
-
 
226
/****************************************************************/
-
 
227
ISR(USART_TX_vect)
-
 
228
{
-
 
229
        static uint16_t ptr_txd_buffer = 0;
-
 
230
        uint8_t tmp_tx;
-
 
231
        if(!txd_complete) // transmission not completed
-
 
232
        {
-
 
233
                ptr_txd_buffer++;                    // die [0] wurde schon gesendet
-
 
234
                tmp_tx = txd_buffer[ptr_txd_buffer];
-
 
235
                // if terminating character or end of txd buffer was reached
-
 
236
                if((tmp_tx == '\r') || (ptr_txd_buffer == TXD_BUFFER_LEN))
-
 
237
                {
-
 
238
                        ptr_txd_buffer = 0; // reset txd pointer
-
 
239
                        txd_complete = 1; // stop transmission
Line 184... Line 240...
184
   if(len--) ptrOut[ptr++] = y; else break;
240
                }
185
   if(len--) ptrOut[ptr++] = z; else break;
241
                UDR0 = tmp_tx; // send current byte will trigger this ISR again
186
  }
242
        }
187
 
243
        // transmission completed
188
}
244
        else ptr_txd_buffer = 0;
189
 
245
}
190
 
246
 
191
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
247
/****************************************************************/
192
//++ Empfangs-Part der Datenübertragung
248
/*               USART0 receiver ISR                            */
193
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
249
/****************************************************************/
194
SIGNAL(INT_VEC_RX)
-
 
195
{
250
ISR(USART_RX_vect)
-
 
251
{
196
 static unsigned int crc;
252
        static uint16_t crc;
197
 static unsigned char crc1,crc2,buf_ptr;
253
        static uint8_t ptr_rxd_buffer = 0;
198
 static unsigned char UartState = 0;
-
 
199
 unsigned char CrcOkay = 0;
254
        uint8_t crc1, crc2;
200
 
255
        uint8_t c;
201
 SioTmp = UDR;
256
 
202
 if(buf_ptr >= MAX_EMPFANGS_BUFF)    UartState = 0;
257
        c = UDR0;  // catch the received byte
203
 if(SioTmp == '\r' && UartState == 2)
258
 
-
 
259
        if(rxd_buffer_locked) return; // if rxd buffer is locked immediately return
204
  {
260
 
205
   UartState = 0;
-
 
206
   crc -= RxdBuffer[buf_ptr-2];
261
        // the rxd buffer is unlocked
207
   crc -= RxdBuffer[buf_ptr-1];
262
        if((ptr_rxd_buffer == 0) && (c == '#')) // if rxd buffer is empty and syncronisation character is received
208
   crc %= 4096;
263
        {
209
   crc1 = '=' + crc / 64;
264
                rxd_buffer[ptr_rxd_buffer++] = c; // copy 1st byte to buffer
-
 
265
                crc = c; // init crc
210
   crc2 = '=' + crc % 64;
266
        }
211
   CrcOkay = 0;
267
        #if 0
212
   if((crc1 == RxdBuffer[buf_ptr-2]) && (crc2 == RxdBuffer[buf_ptr-1])) CrcOkay = 1; else { CrcOkay = 0; };
-
 
213
   if(CrcOkay) // Datensatz schon verarbeitet
268
        else if (ptr_rxd_buffer == 1) // handle address
-
 
269
        {
214
    {
270
                rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
-
 
271
                crc += c; // update crc
-
 
272
        }
215
     NeuerDatensatzEmpfangen = 1;
273
        #endif
216
         AnzahlEmpfangsBytes = buf_ptr;
274
        else if (ptr_rxd_buffer < RXD_BUFFER_LEN) // collect incomming bytes
217
     RxdBuffer[buf_ptr] = '\r';
275
        {
218
         if((RxdBuffer[2] == 'R')) wdt_enable(WDTO_250MS); // Reset-Commando
-
 
219
//     uart_putchar(RxdBuffer[2]);       
276
                if(c != '\r') // no termination character
-
 
277
                {
220
        }                                
278
                        rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
221
  }
279
                        crc += c; // update crc
-
 
280
                }
222
  else
281
                else // termination character was received
223
  switch(UartState)
282
                {
224
  {
283
                        // the last 2 bytes are no subject for checksum calculation
225
   case 0:
284
                        // they are the checksum itself
226
          if(SioTmp == '#' && !NeuerDatensatzEmpfangen) UartState = 1;  // Startzeichen und Daten schon verarbeitet
285
                        crc -= rxd_buffer[ptr_rxd_buffer-2];
227
                  buf_ptr = 0;
286
                        crc -= rxd_buffer[ptr_rxd_buffer-1];
228
                  RxdBuffer[buf_ptr++] = SioTmp;
-
 
229
                  crc = SioTmp;
287
                        // calculate checksum from transmitted data
230
          break;
288
                        crc %= 4096;
231
   case 1: // Adresse auswerten
289
                        crc1 = '=' + crc / 64;
232
                  UartState++;
290
                        crc2 = '=' + crc % 64;
233
                  RxdBuffer[buf_ptr++] = SioTmp;
291
                        // compare checksum to transmitted checksum bytes
234
                  crc += SioTmp;
292
                        if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1]))
235
                  break;
293
                        {   // checksum valid
236
   case 2: //  Eingangsdaten sammeln
294
                                rxd_buffer_locked = TRUE;          // lock the rxd buffer
-
 
295
                                ReceivedBytes = ptr_rxd_buffer;    // store number of received bytes
-
 
296
                                rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character
-
 
297
                                // 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
-
 
299
                        }
237
                  RxdBuffer[buf_ptr] = SioTmp;
300
                        else
238
                  if(buf_ptr < MAX_EMPFANGS_BUFF) buf_ptr++;
301
                        {       // checksum invalid
-
 
302
                                rxd_buffer_locked = FALSE; // unlock rxd buffer
-
 
303
                        }
239
                  else UartState = 0;
304
                        ptr_rxd_buffer = 0; // reset rxd buffer pointer
240
                  crc += SioTmp;
305
                }
Line 241... Line 306...
241
                  break;
306
        }
242
   default:
307
        else // rxd buffer overrun
243
          UartState = 0;
308
        {
244
          break;
309
                ptr_rxd_buffer = 0; // reset rxd buffer
245
  }
310
                rxd_buffer_locked = FALSE; // unlock rxd buffer
246
 
311
        }
247
};
312
}
248
 
313
 
249
 
314
 
250
// --------------------------------------------------------------------------
315
// --------------------------------------------------------------------------
251
void AddCRC(unsigned int wieviele)
316
void AddCRC(uint16_t datalen)
252
{
317
{
253
 unsigned int tmpCRC = 0,i;
318
        uint16_t tmpCRC = 0, i;
254
 for(i = 0; i < wieviele;i++)
319
        for(i = 0; i < datalen; i++)
255
  {
320
        {
Line -... Line 321...
-
 
321
                tmpCRC += txd_buffer[i];
256
   tmpCRC += SendeBuffer[i];
322
        }
257
  }
323
        tmpCRC %= 4096;
258
   tmpCRC %= 4096;
324
        txd_buffer[i++] = '=' + tmpCRC / 64;
259
   SendeBuffer[i++] = '=' + tmpCRC / 64;
325
        txd_buffer[i++] = '=' + tmpCRC % 64;
260
   SendeBuffer[i++] = '=' + tmpCRC % 64;
326
        txd_buffer[i++] = '\r';
261
   SendeBuffer[i++] = '\r';
327
        txd_complete = FALSE;
262
  UebertragungAbgeschlossen = 0;
328
        UDR0 = txd_buffer[0]; // initiates the transmission (continued in the TXD ISR)
263
  UDR = SendeBuffer[0];
329
}
264
}
330
 
265
 
331
 
266
 
332
 
267
// --------------------------------------------------------------------------
333
// --------------------------------------------------------------------------
268
void SendOutData(unsigned char cmd,unsigned char modul, unsigned char *snd, unsigned char len)
334
void SendOutData(uint8_t cmd,uint8_t module, uint8_t *snd, uint8_t len)
269
{
335
{
270
 unsigned int pt = 0;
336
        uint16_t pt = 0;
271
 unsigned char a,b,c;
337
        uint8_t a,b,c;
272
 unsigned char ptr = 0;
338
        uint8_t ptr = 0;
273
 
339
 
274
 SendeBuffer[pt++] = '#';               // Startzeichen
340
        txd_buffer[pt++] = '#';         // Start character
275
 SendeBuffer[pt++] = modul;             // Adresse (a=0; b=1,...)
341
        txd_buffer[pt++] = module;      // Address (a=0; b=1,...)
276
 SendeBuffer[pt++] = cmd;                       // Commando
342
        txd_buffer[pt++] = cmd;         // Command
277
 
343
 
278
 while(len)
344
        while(len)
Line 279... Line -...
279
  {
-
 
280
   if(len) { a = snd[ptr++]; len--;} else a = 0;
-
 
281
   if(len) { b = snd[ptr++]; len--;} else b = 0;
-
 
282
   if(len) { c = snd[ptr++]; len--;} else c = 0;
-
 
283
   SendeBuffer[pt++] = '=' + (a >> 2);
-
 
284
   SendeBuffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
-
 
285
   SendeBuffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
-
 
286
   SendeBuffer[pt++] = '=' + ( c & 0x3f);
-
 
287
  }
-
 
288
 AddCRC(pt);
-
 
289
}
-
 
290
 
-
 
291
 
-
 
292
 
-
 
293
//############################################################################
-
 
294
//Routine für die Serielle Ausgabe
-
 
295
int uart_putchar (char c)
-
 
296
//############################################################################
345
        {
297
{
346
                if(len) { a = snd[ptr++]; len--;} else a = 0;
298
    if(!(UCR & (1 << TXEN))) return (0);
347
                if(len) { b = snd[ptr++]; len--;} else b = 0;
-
 
348
                if(len) { c = snd[ptr++]; len--;} else c = 0;
-
 
349
                txd_buffer[pt++] = '=' + (a >> 2);
-
 
350
                txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
-
 
351
                txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
-
 
352
                txd_buffer[pt++] = '=' + ( c & 0x3f);
-
 
353
        }
-
 
354
        AddCRC(pt); // add checksum after data block and initates the transmission
-
 
355
}
-
 
356
 
-
 
357
 
-
 
358
// --------------------------------------------------------------------------
-
 
359
void Decode64(uint8_t *ptrOut, uint8_t len, uint8_t ptrIn, uint8_t max)
-
 
360
{
-
 
361
        uint8_t a,b,c,d;
-
 
362
        uint8_t ptr = 0;
-
 
363
        uint8_t x,y,z;
-
 
364
        while(len)
-
 
365
        {
-
 
366
                a = rxd_buffer[ptrIn++] - '=';
299
        if (c == '\n')
367
                b = rxd_buffer[ptrIn++] - '=';
Line -... Line 368...
-
 
368
                c = rxd_buffer[ptrIn++] - '=';
300
                uart_putchar('\r');
369
                d = rxd_buffer[ptrIn++] - '=';
301
        //Warten solange bis Zeichen gesendet wurde
370
                if(ptrIn > max - 2) break;
302
        loop_until_bit_is_set(USR, UDRE);
-
 
303
        //Ausgabe des Zeichens
-
 
304
        UDR = c;
371
 
305
       
372
                x = (a << 2) | (b >> 4);
-
 
373
                y = ((b & 0x0f) << 4) | (c >> 2);
-
 
374
                z = ((c & 0x03) << 6) | d;
-
 
375
 
-
 
376
                if(len--) ptrOut[ptr++] = x; else break;
-
 
377
                if(len--) ptrOut[ptr++] = y; else break;
-
 
378
                if(len--) ptrOut[ptr++] = z; else break;
-
 
379
        }
-
 
380
}
Line 306... Line -...
306
        return (0);
-
 
307
}
-
 
308
 
-
 
309
// --------------------------------------------------------------------------
-
 
Line -... Line 381...
-
 
381
 
-
 
382
 
-
 
383
// --------------------------------------------------------------------------
310
void WriteProgramData(unsigned int pos, unsigned char wert)
384
int16_t uart_putchar (int8_t c)
-
 
385
{
Line 311... Line 386...
311
{
386
        // if tx is not enabled return immediatly
312
}
-
 
Line 313... Line 387...
313
 
387
        if(!(UCSR0B & (1 << TXEN0))) return (0);
-
 
388
        if (c == '\n') uart_putchar('\r');
314
//############################################################################
389
        // wait until previous character was send
315
//INstallation der Seriellen Schnittstelle
390
        loop_until_bit_is_set(UCSR0A, UDRE0);
316
void UART_Init (void)
391
        // send character
317
//############################################################################
392
        UDR0 = c;
318
{
-
 
319
        //Enable TXEN im Register UCR TX-Data Enable & RX Enable
-
 
320
 
-
 
321
        UCR=(1 << TXEN) | (1 << RXEN);
393
        return (0);
322
    // UART Double Speed (U2X)
-
 
Line -... Line 394...
-
 
394
}
-
 
395
 
-
 
396
 
-
 
397
// --------------------------------------------------------------------------
-
 
398
void USART0_ProcessRxData(void)
-
 
399
{
Line 323... Line -...
323
        USR   |= (1<<U2X);          
-
 
324
        // RX-Interrupt Freigabe
-
 
325
 
-
 
326
        UCSRB |= (1<<RXCIE);    // serieller Empfangsinterrupt       
-
 
327
 
-
 
328
        // TX-Interrupt Freigabe
-
 
329
        UCSRB |= (1<<TXCIE);          
-
 
330
 
-
 
331
        //Teiler wird gesetzt 
-
 
332
        UBRR= (SYSCLK / (BAUD_RATE * 8L) -1 );
-
 
333
        //öffnet einen Kanal für printf (STDOUT)
-
 
334
//      fdevopen (uart_putchar, NULL);
-
 
335
    Debug_Timer = SetDelay(200);  
-
 
336
    // Version beim Start ausgeben (nicht schön, aber geht... ) 
-
 
337
        uart_putchar ('\n');uart_putchar ('C');uart_putchar ('P');uart_putchar (':');
-
 
338
        uart_putchar ('V');uart_putchar (0x30 + VERSION_HAUPTVERSION);uart_putchar ('.');uart_putchar (0x30 + VERSION_NEBENVERSION/10); uart_putchar (0x30 + VERSION_NEBENVERSION%10);
-
 
339
    uart_putchar ('\n');
-
 
340
}
-
 
341
 
-
 
342
 
-
 
343
void BearbeiteRxDaten(void)
-
 
344
{
-
 
345
 if(!NeuerDatensatzEmpfangen) return;
-
 
346
// unsigned int tmp_int_arr1[1];
-
 
347
// unsigned int tmp_int_arr2[2];
-
 
348
// unsigned int tmp_int_arr3[3];
-
 
349
 unsigned char tmp_char_arr2[2];
-
 
350
// unsigned char tmp_char_arr3[3];
-
 
351
// unsigned char tmp_char_arr4[4];
-
 
352
 //if(!MotorenEin) 
-
 
353
 PcZugriff = 255;
400
        // if data in the rxd buffer are not locked immediately return
354
 
-
 
355
  switch(RxdBuffer[2])
-
 
356
  {
401
        if(!rxd_buffer_locked) return;
357
   case 'w':// Lagewinkel
402
 
358
            Decode64((unsigned char *) &ExternData,sizeof(ExternData),3,AnzahlEmpfangsBytes);
-
 
359
            DebugOut.Analog[15]++;
-
 
360
            KompassAntwort = 1;
-
 
361
            break;
-
 
362
   case 'c':
-
 
363
   case 'b':
-
 
364
                        Decode64((unsigned char *) &ExternControl,sizeof(ExternControl),3,AnzahlEmpfangsBytes);
-
 
365
            ExternData.Winkel[0] = ExternControl.Par1;
-
 
366
            ExternData.Winkel[1] = ExternControl.Par2;
403
        uint8_t tmp_char_arr2[2]; // local char buffer
367
            break;
-
 
368
   case 'v': // Version-Anforderung     und Ausbaustufe
-
 
369
            GetVersionAnforderung = 1;
404
 
370
            PC_Connected = 255;
-
 
371
            break;                                                               
-
 
372
 
-
 
373
   case 'a':// Texte der Analogwerte
-
 
374
            Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes);
405
        switch(rxd_buffer[2])
375
            DebugTextAnforderung = tmp_char_arr2[0];
-
 
376
            PC_Connected = 255;
-
 
377
                        break;
-
 
378
   case 'g':// "Get"-Anforderung für Debug-Daten 
-
 
379
            // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
-
 
380
            PC_Connected = 255;
-
 
381
            DebugGetAnforderung = 1;
-
 
382
            break;
406
        {
383
   case 'h':// x-1 Displayzeilen
407
           case 'w':// Attitude
-
 
408
                        Decode64((uint8_t *) &ExternData, sizeof(ExternData), 3, ReceivedBytes);
384
            PC_Connected = 255;
409
                        DebugOut.Analog[15]++;
385
            break;
410
                        RequestCompassHeading = TRUE;
-
 
411
                        break;
-
 
412
 
386
/*
413
                case 'b': // extern control
-
 
414
                case 'c': // extern control with debug request
387
   case 'b':
415
                        Decode64((uint8_t *) &ExternControl,sizeof(ExternControl), 3,ReceivedBytes);
388
                        Decode64((unsigned char *) &ExternControl,sizeof(ExternControl),3,AnzahlEmpfangsBytes);
416
                        ExternData.Attitude[0] = ExternControl.Par1;
389
                        RemoteTasten |= ExternControl.RemoteTasten;
417
            ExternData.Attitude[1] = ExternControl.Par2;
390
            ConfirmFrame = ExternControl.Frame;
418
                        break;
391
            break;
-
 
392
   case 'c':
-
 
393
                        Decode64((unsigned char *) &ExternControl,sizeof(ExternControl),3,AnzahlEmpfangsBytes);
-
 
394
                        RemoteTasten |= ExternControl.RemoteTasten;
-
 
395
            ConfirmFrame = ExternControl.Frame;
-
 
396
            DebugDataAnforderung = 1;
-
 
397
            break;
-
 
398
   case 'h':// x-1 Displayzeilen
-
 
399
            Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes);
-
 
400
            RemoteTasten |= tmp_char_arr2[0];
-
 
401
                        if(tmp_char_arr2[1] == 255) NurKanalAnforderung = 1; else NurKanalAnforderung = 0; // keine Displaydaten
-
 
402
                        DebugDisplayAnforderung = 1;
-
 
403
                        break;
-
 
404
   case 't':// Motortest
-
 
405
            Decode64((unsigned char *) &MotorTest[0],sizeof(MotorTest),3,AnzahlEmpfangsBytes);
-
 
406
                        break;
-
 
407
   case 'k':// Keys von DubWise
-
 
408
            Decode64((unsigned char *) &DubWiseKeys[0],sizeof(DubWiseKeys),3,AnzahlEmpfangsBytes);
-
 
409
                        ConfirmFrame = DubWiseKeys[3];
-
 
410
                        break;
-
 
411
   case 'q':// "Get"-Anforderung für Settings
-
 
412
            // Bei Get werden die vom PC einstellbaren Werte vom PC zurückgelesen
-
 
413
            Decode64((unsigned char *) &tmp_char_arr2[0],sizeof(tmp_char_arr2),3,AnzahlEmpfangsBytes);
-
 
414
            if(tmp_char_arr2[0] != 0xff)
-
 
415
             {
-
 
416
                          if(tmp_char_arr2[0] > 5) tmp_char_arr2[0] = 5;
-
 
417
                  ReadParameterSet(tmp_char_arr2[0], (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);                   
-
 
418
                  SendOutData('L' + tmp_char_arr2[0] -1,MeineSlaveAdresse,(unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE);
-
 
419
             }
419
 
420
             else
420
                case 'h':// x-1 display columns
421
                  SendOutData('L' + GetActiveParamSetNumber()-1,MeineSlaveAdresse,(unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE);
421
                        PC_Connected = 255;
422
             
422
                        break;
Line -... Line 423...
-
 
423
 
423
            break;
424
                case 'v': // get version and board release
424
       
425
                        RequestVerInfo = TRUE;
425
   case 'l':
426
                        PC_Connected = 255;
426
   case 'm':
-
 
427
   case 'n':
-
 
428
   case 'o':
427
                        break;
429
   case 'p': // Parametersatz speichern
-
 
430
            Decode64((unsigned char *) &EE_Parameter.Kanalbelegung[0],STRUCT_PARAM_LAENGE,3,AnzahlEmpfangsBytes);
-
 
431
                        WriteParameterSet(RxdBuffer[2] - 'l' + 1, (unsigned char *) &EE_Parameter.Kanalbelegung[0], STRUCT_PARAM_LAENGE);
-
 
432
            eeprom_write_byte(&EEPromArray[EEPROM_ADR_ACTIVE_SET], RxdBuffer[2] - 'l' + 1);  // aktiven Datensatz merken
-
 
433
            Umschlag180Nick = (long) EE_Parameter.WinkelUmschlagNick * 2500L;
428
 
434
            Umschlag180Roll = (long) EE_Parameter.WinkelUmschlagRoll * 2500L;
-
 
435
            Piep(GetActiveParamSetNumber());
429
                case 'a':// Labels of the Analog Debug outputs
436
         break;
430
                        Decode64((uint8_t *) &tmp_char_arr2[0], sizeof(tmp_char_arr2), 3, ReceivedBytes);
437
*/             
431
                        RequestDebugLabel = tmp_char_arr2[0];
438
         
432
                        PC_Connected = 255;
439
  }
-
 
440
// DebugOut.AnzahlZyklen =  Debug_Timer_Intervall;
433
                        break;
441
 NeuerDatensatzEmpfangen = 0;
-
 
442
}
-
 
443
 
-
 
444
 
434
 
445
//---------------------------------------------------------------------------------------------
435
                case 'g':// get debug data
446
void DatenUebertragung(void)  
-
 
447
{
-
 
448
 if((CheckDelay(Debug_Timer) && UebertragungAbgeschlossen))      // im Singlestep-Betrieb in jedem Schtitt senden
-
 
449
         {
-
 
450
         SetDebugValues();
-
 
451
          SendOutData('D',MeineSlaveAdresse,(unsigned char *) &DebugOut,sizeof(DebugOut));
436
                        RequestDebugData = TRUE;
452
          Debug_Timer = SetDelay(250);   // Sendeintervall
-
 
Line -... Line 437...
-
 
437
                        PC_Connected = 255;
-
 
438
                        break;
-
 
439
        }
-
 
440
        // unlock the rxd buffer after processing
-
 
441
        rxd_buffer_locked = FALSE;
-
 
442
}
-
 
443
 
-
 
444
 
-
 
445
 
-
 
446
//---------------------------------------------------------------------------------------------
-
 
447
void USART0_TransmitTxData(void)
-
 
448
{
-
 
449
        if(!(UCSR0B & (1 << TXEN0))) return;
-
 
450
 
-
 
451
        if(!txd_complete) return;
-
 
452
 
-
 
453
    if(CheckDelay(Debug_Timer) && txd_complete)
-
 
454
    {
-
 
455
                SetDebugValues();
-
 
456
                SendOutData('D',MySlaveAddr,(uint8_t *) &DebugOut,sizeof(DebugOut));
-
 
457
                Debug_Timer = SetDelay(250);
-
 
458
    }
-
 
459
 
-
 
460
    if(RequestDebugLabel != 255)
-
 
461
    {