Subversion Repositories FlightCtrl

Rev

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