Subversion Repositories MK3Mag

Rev

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

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