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