Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
1 ingob 1
/*############################################################################
2
############################################################################*/
3
 
4
#include "main.h"
5
 
918 hbuss 6
volatile unsigned char twi_state = 0;
1 ingob 7
unsigned char motor = 0;
1210 hbuss 8
unsigned char motorread = 0,MissingMotor = 0;
1209 hbuss 9
unsigned char motor_rx[16],motor_rx2[16];
1210 hbuss 10
unsigned char MotorPresent[MAX_MOTORS];
1211 hbuss 11
unsigned char MotorError[MAX_MOTORS];
1 ingob 12
 
13
//############################################################################
14
//Initzialisieren der I2C (TWI) Schnittstelle
15
void i2c_init(void)
16
//############################################################################
17
{
18
  TWSR = 0;
19
  TWBR = ((SYSCLK/SCL_CLOCK)-16)/2;
20
}
21
 
22
//############################################################################
23
//Start I2C
1209 hbuss 24
void i2c_start(void)
1 ingob 25
//############################################################################
26
{
27
    TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE);
28
}
29
 
30
//############################################################################
31
void i2c_stop(void)
32
//############################################################################
33
{
34
    TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT);
35
}
36
 
173 holgerb 37
void i2c_reset(void)
1 ingob 38
//############################################################################
173 holgerb 39
{
40
                 i2c_stop();                
41
                 twi_state = 0;
42
                 motor = TWDR;
43
                 motor = 0;
44
                 TWCR = 0x80;
45
                 TWAMR = 0;
46
                 TWAR = 0;
47
                 TWDR = 0;
48
                 TWSR = 0;
49
                 TWBR = 0;
50
                 i2c_init();
51
                 i2c_start();
52
                 i2c_write_byte(0);
53
}
54
 
55
//############################################################################
1209 hbuss 56
void i2c_write_byte(char byte)
1 ingob 57
//############################################################################
58
{
59
    TWSR = 0x00;
60
    TWDR = byte;
61
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
62
}
63
 
1209 hbuss 64
/****************************************/
65
/*    Write to I2C                      */
66
/****************************************/
67
void I2C_WriteByte(int8_t byte)
68
{
69
    // move byte to send into TWI Data Register
70
    TWDR = byte;
71
    // clear interrupt flag (TWINT = 1)
72
    // enable i2c bus (TWEN = 1)
73
    // enable interrupt (TWIE = 1)
74
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
75
}
76
 
77
/****************************************/
78
/*    Receive byte and send ACK         */
79
/****************************************/
80
void I2C_ReceiveByte(void)
81
{
82
   TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);
83
}
84
 
85
/****************************************/
86
/* I2C receive last byte and send no ACK*/
87
/****************************************/
88
void I2C_ReceiveLastByte(void)
89
{
90
   TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
91
}
92
 
93
 
94
 
1 ingob 95
//############################################################################
96
SIGNAL (TWI_vect)
97
//############################################################################
98
{
1211 hbuss 99
 static unsigned char missing_motor;
1210 hbuss 100
     switch(twi_state++)
1 ingob 101
        {
1210 hbuss 102
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
103
// Writing the Data
104
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 105
        case 0:
1210 hbuss 106
                        while(Mixer.Motor[motor][0] <= 0 && motor < MAX_MOTORS) motor++;  // skip if not used
107
                                if(motor == MAX_MOTORS)  // writing finished -> now read 
108
                                {
109
                                 motor = 0;
110
                                 twi_state = 3;
111
                                 i2c_write_byte(0x53+(motorread*2));
112
                                 }
1209 hbuss 113
                                else i2c_write_byte(0x52+(motor*2));
1 ingob 114
                break;
115
        case 1:
1209 hbuss 116
                i2c_write_byte(Motor[motor++]);
1111 hbuss 117
                break;
118
        case 2:
1211 hbuss 119
                if(TWSR == 0x30) { if(!missing_motor) missing_motor = motor; if(++MotorError[motor-1] == 0) MotorError[motor-1] = 255;}
1111 hbuss 120
                i2c_stop();
1210 hbuss 121
                I2CTimeout = 10;
1209 hbuss 122
                twi_state = 0;
1111 hbuss 123
                i2c_start();  
124
                break;
1210 hbuss 125
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
126
// Reading Data
127
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1111 hbuss 128
        case 3:
1211 hbuss 129
               //Transmit 1st byte for reading
1210 hbuss 130
                if(TWSR != 0x40)  // Error?
1211 hbuss 131
                 {
132
                  MotorPresent[motorread] = 0;
133
                  motorread++;
134
                  if(motorread >= MAX_MOTORS) motorread = 0;
135
                  i2c_stop();
1210 hbuss 136
                  twi_state = 0;
137
                 }
1211 hbuss 138
                 else
139
                 {
1210 hbuss 140
                  MotorPresent[motorread] = ('1' - '-') + motorread;
1211 hbuss 141
                  I2C_ReceiveByte();
142
                 }
1210 hbuss 143
                MissingMotor = missing_motor;
1211 hbuss 144
                missing_motor = 0;
1111 hbuss 145
                break;
1210 hbuss 146
        case 4: //Read 1st byte and transmit 2nd Byte
1111 hbuss 147
                motor_rx[motorread] = TWDR;
1209 hbuss 148
                I2C_ReceiveLastByte(); //nack
149
                break;
1210 hbuss 150
        case 5:
1209 hbuss 151
                //Read 2nd byte
152
                motor_rx2[motorread++] = TWDR;
1211 hbuss 153
                if(motorread >= MAX_MOTORS) motorread = 0;
1111 hbuss 154
                i2c_stop();
1209 hbuss 155
                twi_state = 0;
1111 hbuss 156
                break;
1211 hbuss 157
 
1210 hbuss 158
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
159
// writing Gyro-Offset
160
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
161
        case 8:
1111 hbuss 162
                i2c_write_byte(0x98); // Address of the DAC
163
                break;
164
        case 9:
165
                i2c_write_byte(0x10); // Update Channel A
166
                break;
167
        case 10:
168
                i2c_write_byte(AnalogOffsetNick); // Value
169
                break;
170
        case 11:
171
                i2c_write_byte(0x80); // Value
172
                break;
173
        case 12:
174
                i2c_stop();              
175
                I2CTimeout = 10;
176
                i2c_start();  
177
                break;
178
        case 13:
179
                i2c_write_byte(0x98); // Address of the DAC
180
                break;
181
        case 14:
182
                i2c_write_byte(0x12); // Update Channel B
183
                break;
184
        case 15:
185
                i2c_write_byte(AnalogOffsetRoll); // Value
186
                break;
187
        case 16:
188
                i2c_write_byte(0x80); // Value
189
                break;
190
        case 17:
191
                i2c_stop();              
192
                I2CTimeout = 10;
193
                i2c_start();  
194
                break;
195
        case 18:
196
                i2c_write_byte(0x98); // Address of the DAC
197
                break;
198
        case 19:
199
                i2c_write_byte(0x14); // Update Channel C
200
                break;
201
        case 20:
202
                i2c_write_byte(AnalogOffsetGier); // Value
203
                break;
204
        case 21:
205
                i2c_write_byte(0x80); // Value
206
                break;
207
        case 22:
208
                i2c_stop();              
209
                I2CTimeout = 10;
210
                twi_state = 0;
211
                break;
1209 hbuss 212
        default: twi_state = 0;
213
                break;        
214
                }
1111 hbuss 215
 TWCR |= 0x80;
216
}