Subversion Repositories MK3Mag

Rev

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

Rev 17 Rev 32
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
 
-
 
58
#include <avr/io.h>
57
#include <avr/io.h>
-
 
58
#include <avr/interrupt.h>
59
#include <util/twi.h>
59
#include <util/twi.h>
-
 
60
#include "twislave.h"
-
 
61
#include "uart.h"
60
#include "main.h"
62
#include "main.h"
-
 
63
#include "timer0.h"
-
 
64
#include "led.h"
-
 
65
 
-
 
66
 
-
 
67
volatile uint8_t I2C_RxBufferSize = 0, I2C_TxBufferSize = 0;
-
 
68
volatile uint8_t *I2C_TxBuffer = 0, *I2C_RxBuffer = 0;
-
 
69
volatile uint8_t Tx_Idx = 0, Rx_Idx = 0;
-
 
70
 
-
 
71
volatile uint8_t I2C_PrimRxBuffer[10];
-
 
72
 
-
 
73
uint8_t NC_Connected = 0;
-
 
74
I2C_Heading_t       I2C_Heading;
-
 
75
I2C_WriteAttitude_t I2C_WriteAttitude;
-
 
76
I2C_Mag_t           I2C_Mag;
-
 
77
I2C_Version_t       I2C_Version;
-
 
78
I2C_WriteCal_t      I2C_WriteCal;
-
 
79
 
-
 
80
 
-
 
81
void I2C_Init(void)
-
 
82
{
-
 
83
 
-
 
84
        uint8_t sreg;
-
 
85
 
-
 
86
        // backup status register
-
 
87
        sreg = SREG;
-
 
88
    // disable global interrupts
-
 
89
        cli();
-
 
90
 
-
 
91
        // SCK/SCL and  MISO/SDA are at put together on the same connector pin in the schematic
-
 
92
 
-
 
93
        // set PB4 (SCK) and PB5 (MISO) as input pull up
-
 
94
        DDRB &= ~((1<<DDB4)|(1<<DDB5));
-
 
95
        PORTB |= ((1<<PORTB4)|(1<<PORTB5));
-
 
96
 
-
 
97
        // set PC4 (SDA) and PC5 (SCL) as input tristate
-
 
98
        DDRC &= ~((1<<DDC4)|(1<<DDC5));
-
 
99
        PORTC &= ~((1<<PORTC4)|(1<<PORTC5));
-
 
100
 
-
 
101
        I2C_TxBuffer = 0;
-
 
102
        Tx_Idx = 0;
-
 
103
        I2C_TxBufferSize = 0;
-
 
104
        I2C_RxBuffer = 0;
-
 
105
        Rx_Idx = 0;
-
 
106
        I2C_RxBufferSize = 0;
Line 61... Line -...
61
 
-
 
-
 
107
 
-
 
108
 
62
unsigned char I2C_RxBufferSize, I2C_TxBufferSize;
109
 
63
unsigned char *I2C_TxBuffer, *I2C_RxBuffer;
110
        TWCR &= ~(1<<TWSTA)|(1<<TWSTO);
64
unsigned char Tx_Idx=0, Rx_Idx=0, I2C_Direction;
-
 
65
unsigned char I2C_Command;
-
 
66
unsigned char Tx_Idx, Rx_Idx;
111
        TWCR|= (1<<TWEA) | (1<<TWEN)|(1<<TWIE);
67
 
112
 
68
struct str_I2C_Heading       I2C_Heading;
-
 
69
struct str_I2C_WriteNickRoll I2C_WriteNickRoll;
113
        // set own address
70
struct str_I2C_Mag           I2C_Mag;
114
        // set own address in the upper 7 bits
71
struct str_I2C_EEPROM        I2C_ReadEEPROM, I2C_WriteEEPROM;
115
    TWAR = I2C_SLAVE_ADDRESS; // set own address only the upper 7 bits are relevant
72
struct str_I2C_Version       I2C_Version;
116
        // TWI Control Register
73
struct str_I2C_WriteCal      I2C_WriteCal;
117
        // enable TWI Acknowledge Bit (TWEA = 1)
74
struct str_I2C_Acc           I2C_Acc;
-
 
75
 
-
 
76
 
118
        // disable TWI START Condition Bit (TWSTA = 0), SLAVE
77
//############################################################################
119
        // disable TWI  STOP Condition Bit (TWSTO = 0), SLAVE
78
//I2C (TWI) Interface Init
120
        // enable TWI (TWEN = 1)
79
void InitIC2_Slave(void)
121
        // enable TWI Interrupt (TWIE = 1)
80
//############################################################################
-
 
81
{
122
        TWCR |= (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|(0<<TWWC);
82
    TWAR = I2C_SLAVE_ADDRESS; // Eigene Adresse setzen 
-
 
83
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);
-
 
84
       
-
 
85
       
123
        // update version info
86
        I2C_Version.Hauptversion = VersionInfo.Hauptversion;
124
        I2C_Version.Major = VERSION_MAJOR;
87
        I2C_Version.Nebenversion = VersionInfo.Nebenversion;
125
        I2C_Version.Minor = VERSION_MINOR;
88
        I2C_Version.Comp = 1;
126
        I2C_Version.Compatible = I2C_PROTOCOL_COMP;
-
 
127
 
-
 
128
        // resore status register
89
       
129
        SREG = sreg;
-
 
130
}
-
 
131
 
90
}
132
// send ACK after recieving a byte / ACK is expected after transmitting a byte
91
#define TWCR_ACK TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC)
133
#define TWCR_ACK TWCR           = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC)
92
//############################################################################
134
// send no ACK after recieving a byte / No ACK is expected after transmitting a byte
-
 
135
#define TWCR_NACK TWCR          = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC)
-
 
136
// switched to the non adressed slave mode
93
//ISR, die bei einem Ereignis auf dem Bus ausgelöst wird. Im Register TWSR befindet
137
#define TWCR_RESET TWCR         = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC)
-
 
138
// The bit pattern for TWCR_ACK and TWCR_RESET are equal. This is no errro but used for better understanding.
-
 
139
#define TWCR_CLEARBUS TWCR      = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|(0<<TWWC)
94
//sich dann ein Statuscode, anhand dessen die Situation festgestellt werden kann.
140
 
95
ISR (TWI_vect)
-
 
96
//############################################################################
141
ISR (TWI_vect)
-
 
142
{
97
{  
143
        uint8_t data;
98
    switch (TWSR & 0xF8)
144
        static uint8_t crc;
99
        {  
145
        // check event
-
 
146
        switch (TW_STATUS)
100
        case SR_SLA_ACK:  
147
        {
101
            //TWCR |= (1<<TWINT);
148
        case TW_SR_SLA_ACK: // slave addressed in receiver mode and ack has been returned
102
            Rx_Idx = 0;
-
 
103
                       
149
            Rx_Idx = 0xFF;  // reset rx buffer pointer
104
                        TWCR_ACK;
150
                        TWCR_ACK;       // trigger receiving of first data byte and send ack afterwards
105
            return;
-
 
106
        // Daten Empfangen
-
 
107
        case SR_PREV_ACK:
151
            return;
-
 
152
 
-
 
153
        case TW_SR_DATA_ACK: // data has been received and ack has been returned
108
           
154
                data = TWDR;
109
                        if (Rx_Idx  == 0)
155
                        if (Rx_Idx  == 0xFF)
110
                        { I2C_Command = TWDR;
-
 
111
                         
156
                        {       // if the fisrt byte after slave addressing was received
112
                          switch(I2C_Command)
157
                                switch(data)
113
                          {
158
                                {
114
                             case I2C_CMD_VERSION:
159
                                        case I2C_CMD_VERSION:
115
                                                I2C_TxBuffer = (unsigned char *)&I2C_Version;
160
                                                I2C_TxBuffer = (uint8_t *)&I2C_Version;
-
 
161
                                                I2C_TxBufferSize = sizeof(I2C_Version);
116
                                                I2C_TxBufferSize = sizeof(I2C_Version);
162
                                                I2C_RxBuffer = 0;
117
                                                I2C_RxBufferSize = 0;
163
                                                I2C_RxBufferSize = 0;
118
                                break;
-
 
119
                                case I2C_CMD_WRITE_EEPROM:
-
 
120
                                                I2C_TxBufferSize = 0;
-
 
121
                                                I2C_RxBuffer = (unsigned char *)&I2C_WriteEEPROM;
-
 
122
                                                I2C_RxBufferSize = sizeof(I2C_WriteEEPROM);
-
 
Line 123... Line 164...
123
                                break;
164
                                                break;
-
 
165
 
124
 
166
                                        case I2C_CMD_WRITE_CAL:
125
                                case I2C_CMD_WRITE_CAL:
167
                                                I2C_TxBuffer = 0;
126
                                                I2C_TxBufferSize = 0;
168
                                                I2C_TxBufferSize = 0;
127
                                                I2C_RxBuffer = (unsigned char *)&I2C_WriteCal;
169
                                                I2C_RxBuffer = (uint8_t *)&I2C_WriteCal;
Line 128... Line -...
128
                                                I2C_RxBufferSize = sizeof(I2C_WriteCal);
-
 
129
                                break;
-
 
130
 
-
 
131
                                case I2C_CMD_READ_EEPROM:
-
 
132
                                                I2C_TxBuffer = (unsigned char *)&I2C_ReadEEPROM.Inhalt;
-
 
133
                                                I2C_TxBufferSize = 2;
-
 
134
                                                I2C_RxBuffer = (unsigned char *)&I2C_ReadEEPROM;
170
                                                I2C_RxBufferSize = sizeof(I2C_WriteCal);
135
                                                I2C_RxBufferSize = 1;
171
                                                break;
136
                                break;
172
 
-
 
173
                                        case I2C_CMD_READ_MAG:
137
                                case I2C_CMD_READ_MAG:
174
                                                I2C_TxBuffer = (uint8_t *)&I2C_Mag;
138
                                                I2C_TxBuffer = (unsigned char *)&I2C_Mag;
175
                                                I2C_TxBufferSize = sizeof(I2C_Mag);
139
                                                I2C_TxBufferSize = sizeof(I2C_Mag);
176
                                                I2C_RxBuffer = 0;
140
                                                I2C_RxBufferSize = 0;
177
                                                I2C_RxBufferSize = 0;
141
                                               
178
 
142
                                                I2C_Mag.MagX = MagnetN;
-
 
143
                                                I2C_Mag.MagY = MagnetR;
-
 
144
                                                I2C_Mag.MagZ = MagnetZ;
-
 
145
                                               
-
 
146
                                break;
-
 
147
                                case I2C_CMD_READ_ACC:
-
 
148
                                                I2C_TxBuffer = (unsigned char *)&I2C_Acc;
-
 
149
                                                I2C_TxBufferSize = sizeof(I2C_Acc);
-
 
150
                                                I2C_RxBufferSize = 0;
-
 
151
                                                I2C_Acc.AccN = RawAccN;
179
                                                I2C_Mag.MagX = MagX;
152
                                                I2C_Acc.AccR = RawAccR;
-
 
153
                                                I2C_Acc.AccZ = RawAccZ;
-
 
154
                                break;
-
 
155
                                case I2C_CMD_READ_HEADING:
-
 
156
                                                I2C_TxBuffer = (unsigned char *)&I2C_Heading;
-
 
157
                                                I2C_TxBufferSize = sizeof(I2C_Heading);
180
                                                I2C_Mag.MagY = MagY;
158
                                                I2C_RxBuffer =  (unsigned char *)&I2C_WriteNickRoll;
-
 
159
                                                I2C_RxBufferSize = sizeof(I2C_WriteNickRoll);
-
 
160
                                               
-
 
161
                                                I2C_Heading.Heading = Heading;
-
 
162
                                                ExternData.Winkel[NICK] = I2C_WriteNickRoll.Nick;
-
 
163
                                                ExternData.Winkel[ROLL] = I2C_WriteNickRoll.Roll;
181
                                                I2C_Mag.MagZ = MagZ;
164
                        AngleSource = 1;
182
                                                break;
165
                                break;
183
 
-
 
184
                                        case I2C_CMD_READ_HEADING:
166
                                case I2C_CMD_READ_HEADING_INTERNAL_ACC:
185
                                                I2C_TxBuffer = (uint8_t *)&I2C_Heading;
167
                                                I2C_TxBuffer = (unsigned char *)&I2C_Heading;
-
 
168
                                                I2C_TxBufferSize = sizeof(I2C_Heading);
186
                                                I2C_TxBufferSize = sizeof(I2C_Heading);
-
 
187
                                                I2C_RxBuffer =  (uint8_t *)&I2C_WriteAttitude;
169
                                            I2C_RxBufferSize = 0;
188
                                                I2C_RxBufferSize = sizeof(I2C_WriteAttitude);
-
 
189
                                                I2C_Heading.Heading = Heading;
-
 
190
                                                AttitudeSource = ATTITUDE_SOURCE_I2C;
-
 
191
                                                Orientation = ORIENTATION_NC;
-
 
192
                                                NC_Connected = 255;
170
                                       
193
                                                break;
-
 
194
                                        default: // unknown command id
171
                                                I2C_Heading.Heading = Heading;
195
                                                I2C_RxBuffer = 0;
172
                                                ExternData.Winkel[NICK] = 0;
196
                                                I2C_RxBufferSize = 0;
173
                                                ExternData.Winkel[ROLL] = 0;
-
 
174
                        AngleSource = 0;
197
                                                I2C_TxBuffer = 0;
-
 
198
                                                I2C_TxBufferSize = 0;
-
 
199
                                        break;
175
                                break;
200
                                }
176
 
201
                                Rx_Idx = 0; // set rx buffer index to start of the buffer
177
                          }
202
                                crc = data;
-
 
203
                        }
-
 
204
                        else // Rx_Idx  != 0xFF
-
 
205
                        {
-
 
206
                                // fill receiver buffer with the byte that has been received
-
 
207
                                // if buffer exist and there is still some free space
-
 
208
                                if(Rx_Idx  < I2C_RxBufferSize)
-
 
209
                                {
178
                        }
210
                                        I2C_PrimRxBuffer[Rx_Idx] = data;
-
 
211
                                        crc += data;
-
 
212
                                }
-
 
213
                                else if (Rx_Idx == I2C_RxBufferSize) // crc byte was transfered
-
 
214
                                {       // if checksum matched
-
 
215
                                        if(crc == data)
-
 
216
                                        {   // and RxBuffer exist
-
 
217
                                                if(I2C_RxBuffer != 0)
-
 
218
                                                {       // copy data to rx buffer
-
 
219
                                                        for(data = 0; data < I2C_RxBufferSize; data++)
-
 
220
                                                        {
-
 
221
                                                                I2C_RxBuffer[data] = I2C_PrimRxBuffer[data];
-
 
222
                                                        }
-
 
223
                                                }
-
 
224
                                                DebugOut.Analog[31]++;
-
 
225
                                        }
-
 
226
                                        else
-
 
227
                                        {
-
 
228
                                                DebugOut.Analog[30]++;
-
 
229
                                        }
179
                        else
230
                                }
180
                        {
-
 
181
                          if ((Rx_Idx-1) < I2C_RxBufferSize) I2C_RxBuffer[Rx_Idx-1] = TWDR;
-
 
182
                        }
-
 
183
                       
-
 
184
                        Rx_Idx++;
-
 
185
                       
-
 
186
                       
231
                                // else ignore data
187
            I2C_Timeout = 500;
232
                                Rx_Idx++;
188
            //TWCR |= (1<<TWINT);
-
 
189
                        TWCR_ACK;
233
                        }
190
            return;
234
                        TWCR_ACK;
-
 
235
            return;
191
        // Daten Senden
236
 
-
 
237
        case TW_ST_SLA_ACK: // slave transmitter selected
-
 
238
                // reset index  to start of tx buffer
-
 
239
            Tx_Idx = 0;
192
               
240
            crc = 0;
193
        case SW_SLA_ACK:  
241
            // if tx buffer exist and there is at least one byte to transfer
194
            Tx_Idx = 0;
242
                        if((I2C_TxBuffer != 0) && (I2C_TxBufferSize > 1))
-
 
243
                        {
-
 
244
                                data = I2C_TxBuffer[Tx_Idx];
195
                       
245
 
196
                        if (I2C_TxBufferSize > 0) TWDR = I2C_TxBuffer[Tx_Idx++];
246
                        }
197
                       
247
                        else
198
//            TWCR |= (1<<TWINT) | (1<< TWEA);
248
                        {   // send 0x00 if no tx buffer exist or all bytes of the tx buffer have been transmitted
199
             TWCR_ACK;
249
                                data = 0x00;
200
            return;
-
 
201
        // Daten Senden
250
                        }
202
                case SW_DATA_ACK:
-
 
203
            if (Tx_Idx < I2C_TxBufferSize) TWDR = I2C_TxBuffer[Tx_Idx++];
251
                        crc += data;
204
                         else TWDR = 0x00;
252
                        Tx_Idx++;
-
 
253
                        TWDR = data;
-
 
254
                        TWCR_ACK;
-
 
255
            return;
-
 
256
 
-
 
257
                case TW_ST_DATA_ACK: // data byte has been transmitted ack has been received
205
                        //TWCR |= (1<<TWINT) | (1<< TWEA);
258
                        // put next byte from tx buffer to the data register
206
                        TWCR_ACK;
259
                        if((I2C_TxBuffer != 0) && (Tx_Idx < I2C_TxBufferSize))
-
 
260
                        {
207
            return;
261
                        data = I2C_TxBuffer[Tx_Idx];
208
        // Bus-Fehler zurücksetzen
262
                        crc += data;
-
 
263
                        }
-
 
264
                        else if (Tx_Idx == I2C_TxBufferSize)
-
 
265
                        {   // send crc byte at the end
-
 
266
                                data = crc;
-
 
267
                        }
-
 
268
                        else
-
 
269
                        {
-
 
270
                                data = 0x00;
-
 
271
                        }
-
 
272
                        Tx_Idx++;
-
 
273
                        TWDR = data;
209
        case TWI_BUS_ERR_2:
274
                        TWCR_ACK;
210
            TWCR |=(1<<TWSTO) | (1<<TWINT);
275
            return;
211
        // Bus-Fehler zurücksetzen   
276
 
-
 
277
        case TW_BUS_ERROR:  // Bus-Error
-
 
278
            TWCR_CLEARBUS;      // free bus, reset to nonselected slave
212
        case TWI_BUS_ERR_1:
279
            return;
-
 
280
 
-
 
281
                case TW_ST_DATA_NACK:   // data transmitted, NACK received
-
 
282
                case TW_ST_LAST_DATA:   // last data byte transmitted, ACK received
-
 
283
                case TW_SR_STOP:                // stop or repeated start condition received while selected
-
 
284
                default:
213
            TWCR |=(1<<TWSTO) | (1<<TWINT);
285
                        TWCR_RESET; // switch to the not addressed slave mode, own SLA will be recognized