Subversion Repositories MK3Mag

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12 hbuss 1
/*#######################################################################################
2
MK3Mag 3D-Magnet sensor
3
!!! THIS IS NOT FREE SOFTWARE !!!                                                      
4
#######################################################################################*/
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// + Copyright (c) 05.2008 Holger Buss
7
// + Thanks to Ilja Fähnrich (P_Latzhalter)
8
// + Nur für den privaten Gebrauch
9
// + www.MikroKopter.com
10
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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. 
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
18
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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
21
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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"
24
// + eindeutig als Ursprung verlinkt werden
25
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
26
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
27
// + Benutzung auf eigene Gefahr
28
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
29
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// + Redistributions of source code (with or without modifications) must retain the above copyright notice, 
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
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.
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet 
38
// +     for non-commercial use (directly or indirectly)
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted 
40
// +     with our written permission
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
43
// +   * If sources or documentations are redistributet on other webpages, our webpage (http://www.MikroKopter.de) must be 
44
// +     clearly linked as origin 
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
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
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
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
55
// +  POSSIBILITY OF SUCH DAMAGE. 
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 57
 
58
#include <avr/io.h>
59
#include <util/twi.h>
60
#include "main.h"
61
 
3 ingob 62
unsigned char I2C_RxBufferSize, I2C_TxBufferSize;
63
unsigned char *I2C_TxBuffer, *I2C_RxBuffer;
64
unsigned char Tx_Idx=0, Rx_Idx=0, I2C_Direction;
65
unsigned char I2C_Command;
66
unsigned char Tx_Idx, Rx_Idx;
1 ingob 67
 
3 ingob 68
struct str_I2C_Heading       I2C_Heading;
69
struct str_I2C_WriteNickRoll I2C_WriteNickRoll;
70
struct str_I2C_Mag           I2C_Mag;
71
struct str_I2C_EEPROM        I2C_ReadEEPROM, I2C_WriteEEPROM;
72
struct str_I2C_Version       I2C_Version;
7 hbuss 73
struct str_I2C_WriteCal      I2C_WriteCal;
3 ingob 74
 
75
 
76
 
1 ingob 77
//############################################################################
78
//I2C (TWI) Interface Init
3 ingob 79
void InitIC2_Slave(void)
1 ingob 80
//############################################################################
81
{
3 ingob 82
    TWAR = I2C_SLAVE_ADDRESS; // Eigene Adresse setzen 
1 ingob 83
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);
3 ingob 84
 
85
 
86
        I2C_Version.Hauptversion = VersionInfo.Hauptversion;
87
        I2C_Version.Nebenversion = VersionInfo.Nebenversion;
88
        I2C_Version.Comp = 1;
89
 
1 ingob 90
}
3 ingob 91
#define TWCR_ACK TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC)
1 ingob 92
//############################################################################
93
//ISR, die bei einem Ereignis auf dem Bus ausgelöst wird. Im Register TWSR befindet
94
//sich dann ein Statuscode, anhand dessen die Situation festgestellt werden kann.
95
ISR (TWI_vect)
96
//############################################################################
97
{  
98
    switch (TWSR & 0xF8)
99
        {  
100
        case SR_SLA_ACK:  
3 ingob 101
            //TWCR |= (1<<TWINT);
102
            Rx_Idx = 0;
103
 
104
                        TWCR_ACK;
1 ingob 105
            return;
106
        // Daten Empfangen
107
        case SR_PREV_ACK:
3 ingob 108
 
109
                        if (Rx_Idx  == 0)
110
                        { I2C_Command = TWDR;
111
 
112
                          switch(I2C_Command)
113
                          {
114
                             case I2C_CMD_VERSION:
115
                                                I2C_TxBuffer = (unsigned char *)&I2C_Version;
116
                                                I2C_TxBufferSize = sizeof(I2C_Version);
117
                                                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);
123
                                break;
7 hbuss 124
 
125
                                case I2C_CMD_WRITE_CAL:
126
                                                I2C_TxBufferSize = 0;
127
                                                I2C_RxBuffer = (unsigned char *)&I2C_WriteCal;
128
                                                I2C_RxBufferSize = sizeof(I2C_WriteCal);
129
                                break;
130
 
3 ingob 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;
135
                                                I2C_RxBufferSize = 1;
136
                                break;
137
                                case I2C_CMD_READ_MAG:
138
                                                I2C_TxBuffer = (unsigned char *)&I2C_Mag;
139
                                                I2C_TxBufferSize = sizeof(I2C_Mag);
140
                                                I2C_RxBufferSize = 0;
141
 
142
                                                I2C_Mag.MagX = MagnetN;
143
                                                I2C_Mag.MagY = MagnetR;
144
                                                I2C_Mag.MagZ = MagnetZ;
145
 
146
                                break;
147
                                case I2C_CMD_READ_HEADING:
148
                                                I2C_TxBuffer = (unsigned char *)&I2C_Heading;
149
                                                I2C_TxBufferSize = sizeof(I2C_Heading);
150
                                                I2C_RxBuffer =  (unsigned char *)&I2C_WriteNickRoll;
151
                                                I2C_RxBufferSize = sizeof(I2C_WriteNickRoll);
152
 
153
                                                I2C_Heading.Heading = Heading;
7 hbuss 154
                                                ExternData.Winkel[NICK] = I2C_WriteNickRoll.Nick;
155
                                                ExternData.Winkel[ROLL] = I2C_WriteNickRoll.Roll;
3 ingob 156
                                break;
157
                          }
158
                        }
159
                        else
160
                        {
4 ingob 161
                          if ((Rx_Idx-1) < I2C_RxBufferSize) I2C_RxBuffer[Rx_Idx-1] = TWDR;
3 ingob 162
                        }
163
 
164
                        Rx_Idx++;
165
 
166
 
1 ingob 167
            I2C_Timeout = 500;
3 ingob 168
            //TWCR |= (1<<TWINT);
169
                        TWCR_ACK;
1 ingob 170
            return;
171
        // Daten Senden
3 ingob 172
 
1 ingob 173
        case SW_SLA_ACK:  
3 ingob 174
            Tx_Idx = 0;
175
 
176
                        if (I2C_TxBufferSize > 0) TWDR = I2C_TxBuffer[Tx_Idx++];
177
 
178
//            TWCR |= (1<<TWINT) | (1<< TWEA);
179
             TWCR_ACK;
1 ingob 180
            return;
181
        // Daten Senden
3 ingob 182
                case SW_DATA_ACK:
183
            if (Tx_Idx < I2C_TxBufferSize) TWDR = I2C_TxBuffer[Tx_Idx++];
184
                         else TWDR = 0x00;
185
                        //TWCR |= (1<<TWINT) | (1<< TWEA);
186
                        TWCR_ACK;
1 ingob 187
            return;
188
        // Bus-Fehler zurücksetzen
189
        case TWI_BUS_ERR_2:
190
            TWCR |=(1<<TWSTO) | (1<<TWINT);
191
        // Bus-Fehler zurücksetzen   
192
        case TWI_BUS_ERR_1:
193
            TWCR |=(1<<TWSTO) | (1<<TWINT);
194
        }
195
    TWCR =(1<<TWEA) | (1<<TWINT) | (1<<TWEN) | (1<<TWIE); // TWI Reset
196
}
197
 
198