Subversion Repositories FlightCtrl

Rev

Rev 2108 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2108 Rev 2109
Line 1... Line 1...
1
#include "twimaster.h"
1
#include "twimaster.h"
2
#include "analog.h"
2
#include "analog.h"
-
 
3
#include "output.h"
3
#include <inttypes.h>
4
#include <inttypes.h>
4
#include <util/twi.h>
5
#include <util/twi.h>
5
#include <avr/interrupt.h>
6
#include <avr/interrupt.h>
Line 6... Line 7...
6
 
7
 
Line 41... Line 42...
41
void I2CReceiveLastByte(void) {
42
void I2CReceiveLastByte(void) {
42
        TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWIE);
43
        TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWIE);
43
}
44
}
Line 44... Line 45...
44
 
45
 
-
 
46
void I2CReset(void) {
45
void I2CReset(void) {
47
  debugOut.analog[29]++;
46
        I2CStop();
48
        I2CStop();
47
        TWCR = (1 << TWINT); // reset to original state incl. interrupt flag reset
49
        TWCR = (1 << TWINT); // reset to original state incl. interrupt flag reset
48
        TWAMR = 0;
50
        TWAMR = 0;
49
        TWAR = 0;
51
        TWAR = 0;
Line 59... Line 61...
59
        twiState = TWI_STATE_INIT_0;
61
        twiState = TWI_STATE_INIT_0;
60
        I2CStart();
62
        I2CStart();
61
}
63
}
Line 62... Line 64...
62
 
64
 
-
 
65
void twimaster_startCycle(void) {
63
void twimaster_startCycle(void) {
66
        if (sensorDataReady & TWI_DATA_READY) { // if OK last time
64
        twiState = TWI_STATE_LOOP_0;
67
          twiState = TWI_STATE_LOOP_0;
-
 
68
          I2CStart();
-
 
69
        } else {                                // go get em.
-
 
70
          I2CReset();
65
        I2CStart();
71
        }
Line 66... Line 72...
66
}
72
}
67
 
73
 
68
const uint8_t EXPECTED_STATUS[] = { START, MT_SLA_ACK, MT_DATA_ACK,
74
const uint8_t EXPECTED_STATUS[] = { START, MT_SLA_ACK, MT_DATA_ACK,
Line 143... Line 149...
143
        case TWI_STATE_LOOP_0 + 5 + sizeof(IMU3200SensorInputs) - 1: // last data byte
149
        case TWI_STATE_LOOP_0 + 5 + sizeof(IMU3200SensorInputs) - 1: // last data byte
144
                ((uint8_t*) IMU3200SensorInputs)[dataIndex] = TWDR;
150
                ((uint8_t*) IMU3200SensorInputs)[dataIndex] = TWDR;
145
                // Dont re-init the gyro but just restart the loop.
151
                // Dont re-init the gyro but just restart the loop.
146
                I2CStop();
152
                I2CStop();
147
                sensorDataReady |= TWI_DATA_READY;
153
                sensorDataReady |= TWI_DATA_READY;
-
 
154
                debugOut.analog[28]++;
148
                break;
155
                break;
149
        }
156
        }
150
}
157
}