Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
1612 dongfang 1
#include <avr/io.h>
2
#include <avr/interrupt.h>
3
#include <util/twi.h>
4
#include <util/delay.h>
2189 - 5
#include <stdio.h>
1612 dongfang 6
#include "twimaster.h"
2189 - 7
//#include "configuration.h"
8
//#include "controlMixer.h"
1965 - 9
#include "analog.h"
1612 dongfang 10
 
2189 - 11
volatile uint8_t twi_state;
12
volatile uint8_t dac_channel;
13
volatile uint8_t writeIndex;
14
volatile uint8_t readIndex;
1821 - 15
volatile uint16_t I2CTimeout = 100;
2189 - 16
uint8_t missingMotor;
17
MLBLC_t mkblcs[MAX_I2CCHANNELS];
18
uint8_t DACChannel;
1612 dongfang 19
 
20
#define SCL_CLOCK  200000L
21
#define I2C_TIMEOUT 30000
22
 
23
/**************************************************
24
 * Initialize I2C (TWI)                        
25
 **************************************************/
26
void I2C_init(void) {
1821 - 27
        uint8_t i;
28
        uint8_t sreg = SREG;
29
        cli();
30
 
31
        // SDA is INPUT
32
        DDRC &= ~(1 << DDC1);
33
        // SCL is output
34
        DDRC |= (1 << DDC0);
35
        // pull up SDA
36
        PORTC |= (1 << PORTC0) | (1 << PORTC1);
37
 
38
        // TWI Status Register
39
        // prescaler 1 (TWPS1 = 0, TWPS0 = 0)
40
        TWSR &= ~((1 << TWPS1) | (1 << TWPS0));
41
 
42
        // set TWI Bit Rate Register
2189 - 43
        TWBR = ((F_CPU / SCL_CLOCK) - 16) / 2;
1821 - 44
 
45
        twi_state = TWI_STATE_MOTOR_TX;
2189 - 46
        writeIndex = 0;
47
        readIndex = 0;
1821 - 48
 
2189 - 49
        for (i = 0; i < MAX_I2CCHANNELS; i++) {
50
          mkblcs[i].present = 0;
51
          mkblcs[i].maxPWM = 0;
1821 - 52
        }
53
 
54
        SREG = sreg;
1612 dongfang 55
}
56
 
57
/****************************************
58
 * Start I2C                          
59
 ****************************************/
2189 - 60
void I2C_start(uint8_t start_state) {
1821 - 61
        twi_state = start_state;
62
        // TWI Control Register
63
        // clear TWI interrupt flag (TWINT=1)
64
        // disable TWI Acknowledge Bit (TWEA = 0)
65
        // enable TWI START Condition Bit (TWSTA = 1), MASTER
66
        // disable TWI STOP Condition Bit (TWSTO = 0)
67
        // disable TWI Write Collision Flag (TWWC = 0)
68
        // enable i2c (TWEN = 1)
69
        // enable TWI Interrupt (TWIE = 1)
70
        TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN) | (1 << TWIE);
1612 dongfang 71
}
72
 
73
/****************************************
74
 * Stop I2C                          
75
 ****************************************/
2189 - 76
void I2C_stop(uint8_t start_state) {
1821 - 77
        twi_state = start_state;
78
        // TWI Control Register
79
        // clear TWI interrupt flag (TWINT=1)
80
        // disable TWI Acknowledge Bit (TWEA = 0)
81
        // diable TWI START Condition Bit (TWSTA = 1), no MASTER
82
        // enable TWI STOP Condition Bit (TWSTO = 1)
83
        // disable TWI Write Collision Flag (TWWC = 0)
84
        // enable i2c (TWEN = 1)
85
        // disable TWI Interrupt (TWIE = 0)
86
        TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN);
1612 dongfang 87
}
88
 
89
/****************************************
90
 *    Write to I2C                      
91
 ****************************************/
2189 - 92
void I2C_writeByte(int8_t byte) {
1821 - 93
        // move byte to send into TWI Data Register
94
        TWDR = byte;
95
        // clear interrupt flag (TWINT = 1)
96
        // enable i2c bus (TWEN = 1)
97
        // enable interrupt (TWIE = 1)
98
        TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWIE);
1612 dongfang 99
}
100
 
101
/****************************************
102
 * Receive byte and send ACK        
103
 ****************************************/
2189 - 104
void I2C_receiveByte(void) {
1821 - 105
        TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWIE) | (1 << TWEA);
1612 dongfang 106
}
107
 
108
/****************************************
109
 * I2C receive last byte and send no ACK
110
 ****************************************/
2189 - 111
void I2C_receiveLastByte(void) {
1821 - 112
        TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWIE);
1612 dongfang 113
}
114
 
115
/****************************************
116
 * Reset I2C                        
117
 ****************************************/
2189 - 118
void I2C_reset(void) {
1821 - 119
        // stop i2c bus
2189 - 120
        I2C_stop(TWI_STATE_MOTOR_TX);
1821 - 121
        twi_state = 0;
2189 - 122
        writeIndex = TWDR;
123
        writeIndex = 0;
124
        readIndex = 0;
1821 - 125
        TWCR = (1 << TWINT); // reset to original state incl. interrupt flag reset
126
        TWAMR = 0;
127
        TWAR = 0;
128
        TWDR = 0;
129
        TWSR = 0;
130
        TWBR = 0;
131
        I2C_init();
2189 - 132
        I2C_start(TWI_STATE_MOTOR_TX);
1612 dongfang 133
}
134
 
135
/****************************************
136
 * I2C ISR
137
 ****************************************/
1821 - 138
ISR (TWI_vect)
139
{
140
        static uint8_t missing_motor = 0;
141
        switch (twi_state++) { // First i2c_start from SendMotorData()
142
        // Master Transmit
143
        case 0: // TWI_STATE_MOTOR_TX
144
                // skip motor if not used in mixer
2189 - 145
                while ((outputMixer[writeIndex].outputType != OUTPUT_TYPE_MOTOR) && (writeIndex < MAX_I2CCHANNELS))
146
                        writeIndex++;
147
                if (writeIndex >= MAX_I2CCHANNELS) { // writing finished, read now
148
                        writeIndex = 0;
1821 - 149
                        twi_state = TWI_STATE_MOTOR_RX;
2189 - 150
                        I2C_writeByte(0x53 + (readIndex * 2)); // select slave adress in rx mode
1821 - 151
                } else
2189 - 152
                        I2C_writeByte(0x52 + (writeIndex * 2)); // select slave adress in tx mode
1821 - 153
                break;
154
        case 1: // Send Data to Slave
2189 - 155
                //I2C_writeByte(outputs[writeIndex]>>LOG_CONTROL_OUTPUT_SCALING); // transmit throttle value.
156
            I2C_writeByte(mkblcs[writeIndex].throttle); // transmit throttle value.
1821 - 157
                break;
158
        case 2: // repeat case 0+1 for all motors
159
                if (TWSR == TW_MT_DATA_NACK) { // Data transmitted, NACK received
160
                        if (!missing_motor)
2189 - 161
                                missing_motor = writeIndex + 1;
162
                        if (++mkblcs[writeIndex].error == 0)
163
                          mkblcs[writeIndex].error = 255; // increment error counter and handle overflow
1821 - 164
                }
2189 - 165
                I2C_stop(TWI_STATE_MOTOR_TX);
1821 - 166
                I2CTimeout = 10;
2189 - 167
                writeIndex++; // next motor
168
                I2C_start(TWI_STATE_MOTOR_TX); // Repeated start -> switch slave or switch Master Transmit -> Master Receive
1821 - 169
                break;
170
                // Master Receive Data
171
        case 3:
172
                if (TWSR != TW_MR_SLA_ACK) { //  SLA+R transmitted, if not ACK received
173
                        // no response from the addressed slave received
2189 - 174
                  mkblcs[readIndex].present = 0;
175
                        readIndex++; // next motor
176
                        if (readIndex >= MAX_I2CCHANNELS)
177
                                readIndex = 0; // restart reading of first motor if we have reached the last one
178
                        I2C_stop(TWI_STATE_MOTOR_TX);
1821 - 179
                } else {
2189 - 180
                  mkblcs[readIndex].present = ('1' - '-') + readIndex;
181
                        I2C_receiveByte(); //Transmit 1st byte
1821 - 182
                }
183
                missingMotor = missing_motor;
184
                missing_motor = 0;
185
                break;
186
        case 4: //Read 1st byte and transmit 2nd Byte
2189 - 187
          mkblcs[readIndex].current = TWDR;
188
                I2C_receiveLastByte(); // nack
1821 - 189
                break;
190
        case 5:
191
                //Read 2nd byte
2189 - 192
          mkblcs[readIndex].maxPWM = TWDR;
193
                readIndex++; // next motor
194
                if (readIndex >= MAX_I2CCHANNELS)
195
                        readIndex = 0; // restart reading of first motor if we have reached the last one
196
                I2C_stop(TWI_STATE_MOTOR_TX);
1821 - 197
                break;
198
 
199
                // Writing ADC values.
200
        case 7:
2189 - 201
                I2C_writeByte(0x98); // Address the DAC
1821 - 202
                break;
203
 
204
        case 8:
2189 - 205
                I2C_writeByte(0x10 + (DACChannel << 1)); // Select DAC Channel (0x10 = A, 0x12 = B, 0x14 = C)
1821 - 206
                break;
207
 
208
        case 9:
2189 - 209
                I2C_writeByte(gyroAmplifierOffset.offsets[DACChannel]);
1821 - 210
                break;
211
 
212
        case 10:
2189 - 213
                I2C_writeByte(0x80); // 2nd byte for all channels is 0x80
1821 - 214
                break;
215
 
216
        case 11:
2189 - 217
                I2C_stop(TWI_STATE_MOTOR_TX);
1821 - 218
                I2CTimeout = 10;
219
                // repeat case 7...10 until all DAC Channels are updated
220
                if (DACChannel < 2) {
221
                        DACChannel++; // jump to next channel
2189 - 222
                        I2C_start(TWI_STATE_GYRO_OFFSET_TX); // start transmission for next channel
1821 - 223
                } else {
224
                        DACChannel = 0; // reset dac channel counter
225
                }
226
                break;
227
 
228
        default:
2189 - 229
                I2C_stop(TWI_STATE_MOTOR_TX);
1821 - 230
                I2CTimeout = 10;
2189 - 231
                writeIndex = 0;
232
                readIndex = 0;
1821 - 233
        }
1612 dongfang 234
}
235
 
236
extern void twi_diagnostics(void) {
1821 - 237
        // Check connected BL-Ctrls
238
        uint8_t i;
1612 dongfang 239
 
1821 - 240
        printf("\n\rFound BL-Ctrl: ");
1612 dongfang 241
 
2189 - 242
        for (i=0; i<MAX_I2CCHANNELS; i++) {
243
                mkblcs[i].throttle = 0;
1821 - 244
        }
1612 dongfang 245
 
2189 - 246
        I2C_start(TWI_STATE_MOTOR_TX);
1821 - 247
        _delay_ms(2);
248
 
2189 - 249
        readIndex = 0; // read the first I2C-Data
1821 - 250
 
2189 - 251
        for (i = 0; i < MAX_I2CCHANNELS; i++) {
252
                I2C_start(TWI_STATE_MOTOR_TX);
1821 - 253
                _delay_ms(2);
2189 - 254
                if (mkblcs[i].present)
1821 - 255
                        printf("%d ",i+1);
256
        }
257
 
2189 - 258
        for (i = 0; i < MAX_I2CCHANNELS; i++) {
259
                if (!mkblcs[i].present && outputMixer[i].outputType == OUTPUT_TYPE_MOTOR)
1821 - 260
                        printf("\n\r\n\r!! MISSING BL-CTRL: %d !!",i + 1);
2189 - 261
                mkblcs[i].error = 0;
1821 - 262
        }
1612 dongfang 263
}