Subversion Repositories MK3Mag

Rev

Rev 63 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
12 hbuss 1
/*#######################################################################################
2
MK3Mag 3D-Magnet sensor
32 holgerb 3
!!! THIS IS NOT FREE SOFTWARE !!!
12 hbuss 4
#######################################################################################*/
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// + Copyright (c) 05.2008 Holger Buss
7
// + Thanks to Ilja Fähnrich (P_Latzhalter)
62 ingob 8
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
12 hbuss 9
// + www.MikroKopter.com
10
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
62 ingob 11
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
12 hbuss 12
// + mit unserer Zustimmung zulässig
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32 holgerb 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.
12 hbuss 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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32 holgerb 19
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
12 hbuss 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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
62 ingob 30
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur 
31
// + mit unserer Zustimmung zulässig
32
/// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12 hbuss 33
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
34
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32 holgerb 35
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
12 hbuss 36
// + this list of conditions and the following disclaimer.
62 ingob 37
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
12 hbuss 38
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
39
// +     from this software without specific prior written permission.
32 holgerb 40
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
12 hbuss 41
// +     for non-commercial use (directly or indirectly)
32 holgerb 42
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
12 hbuss 43
// +     with our written permission
44
// +     Exception: A preassembled MK3Mag, purchased from www.mikrokopter.de may be used as a part of commercial systems
45
// +     In case of doubt please contact: info@MikroKopter.de
32 holgerb 46
// +   * If sources or documentations are redistributet on other webpages, our webpage (http://www.MikroKopter.de) must be
47
// +     clearly linked as origin
12 hbuss 48
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
49
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
52
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 holgerb 58
// +  POSSIBILITY OF SUCH DAMAGE.
12 hbuss 59
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 60
#include <avr/io.h>
32 holgerb 61
#include <avr/interrupt.h>
1 ingob 62
#include <util/twi.h>
32 holgerb 63
#include "twislave.h"
64
#include "uart.h"
1 ingob 65
#include "main.h"
32 holgerb 66
#include "timer0.h"
67
#include "led.h"
1 ingob 68
 
69
 
32 holgerb 70
volatile uint8_t I2C_RxBufferSize = 0, I2C_TxBufferSize = 0;
71
volatile uint8_t *I2C_TxBuffer = 0, *I2C_RxBuffer = 0;
72
volatile uint8_t Tx_Idx = 0, Rx_Idx = 0;
3 ingob 73
 
32 holgerb 74
volatile uint8_t I2C_PrimRxBuffer[10];
3 ingob 75
 
32 holgerb 76
uint8_t NC_Connected = 0;
77
I2C_Heading_t       I2C_Heading;
78
I2C_WriteAttitude_t I2C_WriteAttitude;
79
I2C_Mag_t           I2C_Mag;
80
I2C_Version_t       I2C_Version;
81
I2C_WriteCal_t      I2C_WriteCal;
82
 
63 holgerb 83
// send ACK after recieving a byte / ACK is expected after transmitting a byte
84
#define TWCR_ACK TWCR           = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC)
85
// send no ACK after recieving a byte / No ACK is expected after transmitting a byte
86
#define TWCR_NACK TWCR          = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC)
87
// switched to the non adressed slave mode
88
#define TWCR_RESET TWCR         = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC)
89
// The bit pattern for TWCR_ACK and TWCR_RESET are equal. This is no errro but used for better understanding.
90
#define TWCR_CLEARBUS TWCR      = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|(0<<TWWC)
32 holgerb 91
 
92
void I2C_Init(void)
93
{
94
 
95
        uint8_t sreg;
96
 
97
        // backup status register
98
        sreg = SREG;
99
    // disable global interrupts
100
        cli();
101
 
102
        // SCK/SCL and  MISO/SDA are at put together on the same connector pin in the schematic
103
 
104
        // set PB4 (SCK) and PB5 (MISO) as input pull up
105
        DDRB &= ~((1<<DDB4)|(1<<DDB5));
106
        PORTB |= ((1<<PORTB4)|(1<<PORTB5));
107
 
108
        // set PC4 (SDA) and PC5 (SCL) as input tristate
109
        DDRC &= ~((1<<DDC4)|(1<<DDC5));
110
        PORTC &= ~((1<<PORTC4)|(1<<PORTC5));
111
 
112
        I2C_TxBuffer = 0;
113
        Tx_Idx = 0;
114
        I2C_TxBufferSize = 0;
115
        I2C_RxBuffer = 0;
116
        Rx_Idx = 0;
117
        I2C_RxBufferSize = 0;
118
 
63 holgerb 119
        TWCR = ~(1<<TWSTA)|(1<<TWSTO);
32 holgerb 120
        TWCR|= (1<<TWEA) | (1<<TWEN)|(1<<TWIE);
121
 
122
        // set own address
123
        // set own address in the upper 7 bits
124
    TWAR = I2C_SLAVE_ADDRESS; // set own address only the upper 7 bits are relevant
125
        // TWI Control Register
126
        // enable TWI Acknowledge Bit (TWEA = 1)
127
        // disable TWI START Condition Bit (TWSTA = 0), SLAVE
128
        // disable TWI  STOP Condition Bit (TWSTO = 0), SLAVE
129
        // enable TWI (TWEN = 1)
130
        // enable TWI Interrupt (TWIE = 1)
131
        TWCR |= (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|(0<<TWWC);
132
        // update version info
133
        I2C_Version.Major = VERSION_MAJOR;
134
        I2C_Version.Minor = VERSION_MINOR;
41 killagreg 135
        I2C_Version.Patch = VERSION_PATCH;
136
        I2C_Version.Compatible = NC_I2C_COMPATIBLE;
63 holgerb 137
    TWCR_RESET;
32 holgerb 138
        // resore status register
139
        SREG = sreg;
1 ingob 140
}
32 holgerb 141
 
142
 
1 ingob 143
ISR (TWI_vect)
32 holgerb 144
{
145
        uint8_t data;
146
        static uint8_t crc;
147
        // check event
148
        switch (TW_STATUS)
149
        {
150
        case TW_SR_SLA_ACK: // slave addressed in receiver mode and ack has been returned
151
            Rx_Idx = 0xFF;  // reset rx buffer pointer
152
                        TWCR_ACK;       // trigger receiving of first data byte and send ack afterwards
1 ingob 153
            return;
32 holgerb 154
 
155
        case TW_SR_DATA_ACK: // data has been received and ack has been returned
156
                data = TWDR;
157
                        if (Rx_Idx  == 0xFF)
37 killagreg 158
                        {       // if the first byte after slave addressing was received
32 holgerb 159
                                switch(data)
160
                                {
161
                                        case I2C_CMD_VERSION:
162
                                                I2C_TxBuffer = (uint8_t *)&I2C_Version;
3 ingob 163
                                                I2C_TxBufferSize = sizeof(I2C_Version);
32 holgerb 164
                                                I2C_RxBuffer = 0;
3 ingob 165
                                                I2C_RxBufferSize = 0;
32 holgerb 166
                                                break;
7 hbuss 167
 
32 holgerb 168
                                        case I2C_CMD_WRITE_CAL:
40 killagreg 169
                                                I2C_TxBuffer = (uint8_t *)&I2C_WriteCal;
170
                                                I2C_TxBufferSize = sizeof(I2C_WriteCal);
32 holgerb 171
                                                I2C_RxBuffer = (uint8_t *)&I2C_WriteCal;
7 hbuss 172
                                                I2C_RxBufferSize = sizeof(I2C_WriteCal);
32 holgerb 173
                                                break;
7 hbuss 174
 
32 holgerb 175
                                        case I2C_CMD_READ_MAG:
176
                                                I2C_TxBuffer = (uint8_t *)&I2C_Mag;
3 ingob 177
                                                I2C_TxBufferSize = sizeof(I2C_Mag);
32 holgerb 178
                                                I2C_RxBuffer = 0;
3 ingob 179
                                                I2C_RxBufferSize = 0;
32 holgerb 180
 
181
                                                I2C_Mag.MagX = MagX;
182
                                                I2C_Mag.MagY = MagY;
183
                                                I2C_Mag.MagZ = MagZ;
184
                                                break;
185
 
186
                                        case I2C_CMD_READ_HEADING:
187
                                                I2C_TxBuffer = (uint8_t *)&I2C_Heading;
3 ingob 188
                                                I2C_TxBufferSize = sizeof(I2C_Heading);
32 holgerb 189
                                                I2C_RxBuffer =  (uint8_t *)&I2C_WriteAttitude;
190
                                                I2C_RxBufferSize = sizeof(I2C_WriteAttitude);
54 killagreg 191
                                                I2C_Heading.Heading = Heading; // get heading
32 holgerb 192
                                                AttitudeSource = ATTITUDE_SOURCE_I2C;
193
                                                Orientation = ORIENTATION_NC;
194
                                                NC_Connected = 255;
195
                                                break;
196
                                        default: // unknown command id
197
                                                I2C_RxBuffer = 0;
198
                                                I2C_RxBufferSize = 0;
199
                                                I2C_TxBuffer = 0;
200
                                                I2C_TxBufferSize = 0;
201
                                        break;
202
                                }
203
                                Rx_Idx = 0; // set rx buffer index to start of the buffer
204
                                crc = data;
205
                        }
206
                        else // Rx_Idx  != 0xFF
207
                        {
208
                                // fill receiver buffer with the byte that has been received
209
                                // if buffer exist and there is still some free space
210
                                if(Rx_Idx  < I2C_RxBufferSize)
211
                                {
212
                                        I2C_PrimRxBuffer[Rx_Idx] = data;
213
                                        crc += data;
214
                                }
215
                                else if (Rx_Idx == I2C_RxBufferSize) // crc byte was transfered
216
                                {       // if checksum matched
59 killagreg 217
                                        crc = ~crc; //flip all bits inthe checksum
32 holgerb 218
                                        if(crc == data)
219
                                        {   // and RxBuffer exist
220
                                                if(I2C_RxBuffer != 0)
221
                                                {       // copy data to rx buffer
222
                                                        for(data = 0; data < I2C_RxBufferSize; data++)
223
                                                        {
224
                                                                I2C_RxBuffer[data] = I2C_PrimRxBuffer[data];
225
                                                        }
226
                                                }
227
                                                DebugOut.Analog[31]++;
228
                                        }
229
                                        else
230
                                        {
231
                                                DebugOut.Analog[30]++;
232
                                        }
233
                                }
234
                                // else ignore data
235
                                Rx_Idx++;
236
                        }
237
                        TWCR_ACK;
238
            return;
16 holgerb 239
 
32 holgerb 240
        case TW_ST_SLA_ACK: // slave transmitter selected
241
                // reset index  to start of tx buffer
242
            Tx_Idx = 0;
243
            crc = 0;
244
            // if tx buffer exist and there is at least one byte to transfer
245
                        if((I2C_TxBuffer != 0) && (I2C_TxBufferSize > 1))
246
                        {
247
                                data = I2C_TxBuffer[Tx_Idx];
248
 
3 ingob 249
                        }
250
                        else
32 holgerb 251
                        {   // send 0x00 if no tx buffer exist or all bytes of the tx buffer have been transmitted
252
                                data = 0x00;
253
                        }
254
                        crc += data;
255
                        Tx_Idx++;
256
                        TWDR = data;
257
                        TWCR_ACK;
258
            return;
259
 
260
                case TW_ST_DATA_ACK: // data byte has been transmitted ack has been received
261
                        // put next byte from tx buffer to the data register
262
                        if((I2C_TxBuffer != 0) && (Tx_Idx < I2C_TxBufferSize))
3 ingob 263
                        {
32 holgerb 264
                        data = I2C_TxBuffer[Tx_Idx];
265
                        crc += data;
3 ingob 266
                        }
32 holgerb 267
                        else if (Tx_Idx == I2C_TxBufferSize)
268
                        {   // send crc byte at the end
59 killagreg 269
                                data = ~crc;
32 holgerb 270
                        }
271
                        else
272
                        {
273
                                data = 0x00;
274
                        }
275
                        Tx_Idx++;
276
                        TWDR = data;
3 ingob 277
                        TWCR_ACK;
1 ingob 278
            return;
32 holgerb 279
 
280
        case TW_BUS_ERROR:  // Bus-Error
281
            TWCR_CLEARBUS;      // free bus, reset to nonselected slave
282
            return;
283
 
284
                case TW_ST_DATA_NACK:   // data transmitted, NACK received
285
                case TW_ST_LAST_DATA:   // last data byte transmitted, ACK received
286
                case TW_SR_STOP:                // stop or repeated start condition received while selected
287
                default:
288
                        TWCR_RESET; // switch to the not addressed slave mode, own SLA will be recognized
289
                return;
290
        }
1 ingob 291
}
292
 
293