Subversion Repositories MK3Mag

Rev

Rev 28 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
12 hbuss 1
/*#######################################################################################
2
MK3Mag 3D-Magnet sensor
19 killagreg 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)
8
// + Nur für den privaten Gebrauch
9
// + www.MikroKopter.com
10
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19 killagreg 11
// + Die Portierung 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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19 killagreg 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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19 killagreg 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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19 killagreg 32
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
12 hbuss 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.
19 killagreg 37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
12 hbuss 38
// +     for non-commercial use (directly or indirectly)
19 killagreg 39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
12 hbuss 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
19 killagreg 43
// +   * If sources or documentations are redistributet on other webpages, our webpage (http://www.MikroKopter.de) must be
44
// +     clearly linked as origin
12 hbuss 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
19 killagreg 55
// +  POSSIBILITY OF SUCH DAMAGE.
12 hbuss 56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 57
#include <avr/io.h>
19 killagreg 58
#include <avr/interrupt.h>
1 ingob 59
#include <util/twi.h>
19 killagreg 60
#include "twislave.h"
61
#include "uart.h"
1 ingob 62
#include "main.h"
19 killagreg 63
#include "timer0.h"
22 killagreg 64
#include "led.h"
1 ingob 65
 
66
 
23 killagreg 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;
3 ingob 70
 
26 killagreg 71
volatile uint8_t I2C_PrimRxBuffer[10];
3 ingob 72
 
29 killagreg 73
uint8_t NC_Connected = 0;
23 killagreg 74
struct I2C_Heading_t       I2C_Heading;
75
struct I2C_WriteAttitude_t I2C_WriteAttitude;
76
struct I2C_Mag_t           I2C_Mag;
77
struct I2C_Version_t       I2C_Version;
78
struct I2C_WriteCal_t      I2C_WriteCal;
19 killagreg 79
 
80
 
81
void I2C_Init(void)
82
{
22 killagreg 83
 
84
        uint8_t sreg;
85
 
86
        // backup status register
87
        sreg = SREG;
88
    // disable global interrupts
19 killagreg 89
        cli();
90
 
22 killagreg 91
        // SCK/SCL and  MISO/SDA are at put together on the same connector pin in the schematic
19 killagreg 92
 
23 killagreg 93
        // set PB4 (SCK) and PB5 (MISO) as input pull up
19 killagreg 94
        DDRB &= ~((1<<DDB4)|(1<<DDB5));
23 killagreg 95
        PORTB |= ((1<<PORTB4)|(1<<PORTB5));
19 killagreg 96
 
22 killagreg 97
        // set PC4 (SDA) and PC5 (SCL) as input tristate
19 killagreg 98
        DDRC &= ~((1<<DDC4)|(1<<DDC5));
22 killagreg 99
        PORTC &= ~((1<<PORTC4)|(1<<PORTC5));
19 killagreg 100
 
22 killagreg 101
        I2C_TxBuffer = 0;
102
        Tx_Idx = 0;
103
        I2C_TxBufferSize = 0;
104
        I2C_RxBuffer = 0;
105
        Rx_Idx = 0;
106
        I2C_RxBufferSize = 0;
107
 
108
 
109
 
110
        TWCR &= ~(1<<TWSTA)|(1<<TWSTO);
111
        TWCR|= (1<<TWEA) | (1<<TWEN)|(1<<TWIE);
112
 
113
        // set own address
114
        // set own address in the upper 7 bits
115
    TWAR = I2C_SLAVE_ADDRESS; // set own address only the upper 7 bits are relevant
19 killagreg 116
        // TWI Control Register
117
        // enable TWI Acknowledge Bit (TWEA = 1)
118
        // disable TWI START Condition Bit (TWSTA = 0), SLAVE
22 killagreg 119
        // disable TWI  STOP Condition Bit (TWSTO = 0), SLAVE
120
        // enable TWI (TWEN = 1)
19 killagreg 121
        // enable TWI Interrupt (TWIE = 1)
23 killagreg 122
        TWCR |= (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|(0<<TWWC);
19 killagreg 123
        // update version info
124
        I2C_Version.Major = VERSION_MAJOR;
125
        I2C_Version.Minor = VERSION_MINOR;
126
        I2C_Version.Compatible = I2C_PROTOCOL_COMP;
127
 
22 killagreg 128
        // resore status register
19 killagreg 129
        SREG = sreg;
1 ingob 130
}
19 killagreg 131
 
22 killagreg 132
// send ACK after recieving a byte / ACK is expected after transmitting a byte
23 killagreg 133
#define TWCR_ACK TWCR           = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC)
22 killagreg 134
// send no ACK after recieving a byte / No ACK is expected after transmitting a byte
23 killagreg 135
#define TWCR_NACK TWCR          = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC)
22 killagreg 136
// switched to the non adressed slave mode
23 killagreg 137
#define TWCR_RESET TWCR         = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC)
22 killagreg 138
// The bit pattern for TWCR_ACK and TWCR_RESET are equal. This is no errro but used for better understanding.
23 killagreg 139
#define TWCR_CLEARBUS TWCR      = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|(0<<TWWC)
19 killagreg 140
 
1 ingob 141
ISR (TWI_vect)
19 killagreg 142
{
22 killagreg 143
        uint8_t data;
25 killagreg 144
        static uint8_t crc;
19 killagreg 145
        // check event
22 killagreg 146
        switch (TW_STATUS)
19 killagreg 147
        {
22 killagreg 148
        case TW_SR_SLA_ACK: // slave addressed in receiver mode and ack has been returned
149
            Rx_Idx = 0xFF;  // reset rx buffer pointer
150
                        TWCR_ACK;       // trigger receiving of first data byte and send ack afterwards
1 ingob 151
            return;
19 killagreg 152
 
22 killagreg 153
        case TW_SR_DATA_ACK: // data has been received and ack has been returned
154
                data = TWDR;
155
                        if (Rx_Idx  == 0xFF)
156
                        {       // if the fisrt byte after slave addressing was received
157
                                switch(data)
19 killagreg 158
                                {
159
                                        case I2C_CMD_VERSION:
160
                                                I2C_TxBuffer = (uint8_t *)&I2C_Version;
3 ingob 161
                                                I2C_TxBufferSize = sizeof(I2C_Version);
23 killagreg 162
                                                I2C_RxBuffer = 0;
3 ingob 163
                                                I2C_RxBufferSize = 0;
19 killagreg 164
                                                break;
165
 
166
                                        case I2C_CMD_WRITE_CAL:
23 killagreg 167
                                                I2C_TxBuffer = 0;
7 hbuss 168
                                                I2C_TxBufferSize = 0;
19 killagreg 169
                                                I2C_RxBuffer = (uint8_t *)&I2C_WriteCal;
7 hbuss 170
                                                I2C_RxBufferSize = sizeof(I2C_WriteCal);
19 killagreg 171
                                                break;
7 hbuss 172
 
19 killagreg 173
                                        case I2C_CMD_READ_MAG:
174
                                                I2C_TxBuffer = (uint8_t *)&I2C_Mag;
3 ingob 175
                                                I2C_TxBufferSize = sizeof(I2C_Mag);
23 killagreg 176
                                                I2C_RxBuffer = 0;
3 ingob 177
                                                I2C_RxBufferSize = 0;
19 killagreg 178
 
28 killagreg 179
                                                I2C_Mag.MagX = MagX;
180
                                                I2C_Mag.MagY = MagY;
181
                                                I2C_Mag.MagZ = MagZ;
19 killagreg 182
                                                break;
183
 
184
                                        case I2C_CMD_READ_HEADING:
185
                                                I2C_TxBuffer = (uint8_t *)&I2C_Heading;
3 ingob 186
                                                I2C_TxBufferSize = sizeof(I2C_Heading);
19 killagreg 187
                                                I2C_RxBuffer =  (uint8_t *)&I2C_WriteAttitude;
188
                                                I2C_RxBufferSize = sizeof(I2C_WriteAttitude);
3 ingob 189
                                                I2C_Heading.Heading = Heading;
28 killagreg 190
                                                AttitudeSource = ATTITUDE_SOURCE_I2C;
29 killagreg 191
                                                Orientation = ORIENTATION_NC;
192
                                                NC_Connected = 255;
19 killagreg 193
                                                break;
23 killagreg 194
                                        default: // unknown command id
22 killagreg 195
                                                I2C_RxBuffer = 0;
196
                                                I2C_RxBufferSize = 0;
197
                                                I2C_TxBuffer = 0;
198
                                                I2C_TxBufferSize = 0;
199
                                        break;
19 killagreg 200
                                }
22 killagreg 201
                                Rx_Idx = 0; // set rx buffer index to start of the buffer
25 killagreg 202
                                crc = data;
3 ingob 203
                        }
22 killagreg 204
                        else // Rx_Idx  != 0xFF
3 ingob 205
                        {
23 killagreg 206
                                // fill receiver buffer with the byte that has been received
207
                                // if buffer exist and there is still some free space
26 killagreg 208
                                if(Rx_Idx  < I2C_RxBufferSize)
23 killagreg 209
                                {
26 killagreg 210
                                        I2C_PrimRxBuffer[Rx_Idx] = data;
25 killagreg 211
                                        crc += data;
22 killagreg 212
                                }
26 killagreg 213
                                else if (Rx_Idx == I2C_RxBufferSize) // crc byte was transfered
214
                                {       // if checksum matched
25 killagreg 215
                                        if(crc == data)
26 killagreg 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
25 killagreg 227
                                        {
26 killagreg 228
                                                DebugOut.Analog[30]++;
25 killagreg 229
                                        }
230
                                }
231
                                // else ignore data
232
                                Rx_Idx++;
3 ingob 233
                        }
23 killagreg 234
                        TWCR_ACK;
1 ingob 235
            return;
19 killagreg 236
 
23 killagreg 237
        case TW_ST_SLA_ACK: // slave transmitter selected
238
                // reset index  to start of tx buffer
239
            Tx_Idx = 0;
25 killagreg 240
            crc = 0;
241
            // if tx buffer exist and there is at least one byte to transfer
23 killagreg 242
                        if((I2C_TxBuffer != 0) && (I2C_TxBufferSize > 1))
243
                        {
25 killagreg 244
                                data = I2C_TxBuffer[Tx_Idx];
245
 
22 killagreg 246
                        }
23 killagreg 247
                        else
248
                        {   // send 0x00 if no tx buffer exist or all bytes of the tx buffer have been transmitted
25 killagreg 249
                                data = 0x00;
23 killagreg 250
                        }
25 killagreg 251
                        crc += data;
252
                        Tx_Idx++;
253
                        TWDR = data;
23 killagreg 254
                        TWCR_ACK;
255
            return;
22 killagreg 256
 
257
                case TW_ST_DATA_ACK: // data byte has been transmitted ack has been received
23 killagreg 258
                        // put next byte from tx buffer to the data register
259
                        if((I2C_TxBuffer != 0) && (Tx_Idx < I2C_TxBufferSize))
22 killagreg 260
                        {
25 killagreg 261
                        data = I2C_TxBuffer[Tx_Idx];
262
                        crc += data;
22 killagreg 263
                        }
25 killagreg 264
                        else if (Tx_Idx == I2C_TxBufferSize)
265
                        {   // send crc byte at the end
266
                                data = crc;
267
                        }
23 killagreg 268
                        else
25 killagreg 269
                        {
270
                                data = 0x00;
22 killagreg 271
                        }
25 killagreg 272
                        Tx_Idx++;
273
                        TWDR = data;
23 killagreg 274
                        TWCR_ACK;
19 killagreg 275
            return;
276
 
22 killagreg 277
        case TW_BUS_ERROR:  // Bus-Error
23 killagreg 278
            TWCR_CLEARBUS;      // free bus, reset to nonselected slave
19 killagreg 279
            return;
280
 
22 killagreg 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:
285
                        TWCR_RESET; // switch to the not addressed slave mode, own SLA will be recognized
286
                return;
19 killagreg 287
        }
1 ingob 288
}
289
 
290