Rev 1644 | Rev 1650 | 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; |
1648 | killagreg | 7 | unsigned char motor = 0; |
1210 | hbuss | 8 | unsigned char motorread = 0,MissingMotor = 0; |
1648 | killagreg | 9 | unsigned char BLFlags = 0; |
1479 | killagreg | 10 | |
11 | MotorData_t Motor[MAX_MOTORS]; |
||
1648 | killagreg | 12 | BLConfig_t BLConfig[MAX_MOTORS]; |
1479 | killagreg | 13 | |
1648 | killagreg | 14 | unsigned char *pTxBuff = (uint8_t*)&BLConfig[0]; |
15 | |||
1322 | hbuss | 16 | unsigned int I2CError = 0; |
1 | ingob | 17 | |
1648 | killagreg | 18 | #define BL_CONF_SYNC_BYTE '#' |
19 | |||
1 | ingob | 20 | //############################################################################ |
21 | //Initzialisieren der I2C (TWI) Schnittstelle |
||
22 | void i2c_init(void) |
||
23 | //############################################################################ |
||
24 | { |
||
1648 | killagreg | 25 | unsigned char i; |
26 | |||
27 | TWSR = 0; |
||
28 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
||
29 | |||
30 | for(i=0; i < MAX_MOTORS; i++) |
||
31 | { |
||
32 | Motor[i].Version = 0; |
||
33 | Motor[i].SetPoint = 0; |
||
34 | Motor[i].SetPointLowerBits = 0; |
||
35 | Motor[i].State = 0; |
||
36 | Motor[i].Current = 0; |
||
37 | Motor[i].MaxPWM = 0; |
||
38 | BLConfig[i].SetMask = MASK_SET_PWM_SCALING|MASK_SET_CURRENT_LIMIT|MASK_SET_TEMP_LIMIT|MASK_SET_CURRENT_SCALING|MASK_SET_BITCONFIG; |
||
39 | BLConfig[i].PwmScaling = 255; // MaxPWM |
||
40 | BLConfig[i].CurrentLimit = 30; // Current Limit in A |
||
41 | BLConfig[i].TempLimit = 99; // Temperature Limit in °C |
||
42 | BLConfig[i].CurrentScaling = 64; // Current Scaling |
||
43 | BLConfig[i].BitConfig = 0; // BitConfig |
||
44 | } |
||
1 | ingob | 45 | } |
46 | |||
173 | holgerb | 47 | void i2c_reset(void) |
1 | ingob | 48 | //############################################################################ |
173 | holgerb | 49 | { |
1648 | killagreg | 50 | I2C_Stop(); |
51 | twi_state = 0; |
||
52 | motor = TWDR; |
||
53 | motor = 0; |
||
54 | TWCR = 0x80; |
||
55 | TWAMR = 0; |
||
56 | TWAR = 0; |
||
57 | TWDR = 0; |
||
58 | TWSR = 0; |
||
59 | TWBR = 0; |
||
60 | i2c_init(); |
||
61 | I2C_Start(); |
||
62 | i2c_write_byte(0); |
||
173 | holgerb | 63 | } |
1 | ingob | 64 | |
1648 | killagreg | 65 | |
1 | ingob | 66 | //############################################################################ |
67 | SIGNAL (TWI_vect) |
||
68 | //############################################################################ |
||
69 | { |
||
1648 | killagreg | 70 | static unsigned char missing_motor = 0, byte_counter = 0, crc = 0, read_more = 0, motorread_temperature = 0; |
71 | J4High; |
||
72 | switch(twi_state++) |
||
73 | { |
||
1210 | hbuss | 74 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
75 | // Writing the Data |
||
76 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1648 | killagreg | 77 | case 0: |
78 | while(Mixer.Motor[motor][MIX_GAS] <= 0 && motor < MAX_MOTORS) motor++; // skip if not used |
||
79 | if(motor == MAX_MOTORS) // writing finished -> now read |
||
80 | { |
||
81 | motor = 0; |
||
82 | twi_state = 5; |
||
83 | i2c_write_byte(0x53+(motorread*2)); |
||
84 | } |
||
1209 | hbuss | 85 | else i2c_write_byte(0x52+(motor*2)); |
1648 | killagreg | 86 | break; |
87 | case 1: |
||
88 | i2c_write_byte(Motor[motor].SetPoint); |
||
89 | if(!(Motor[motor].Version & MOTOR_STATE_NEW_PROTOCOL_MASK) ) |
||
90 | { |
||
91 | twi_state = 4; //jump over sending more data |
||
92 | } |
||
93 | else if(!(Motor[motor].SetPointLowerBits || (BLFlags & BLFLAG_SEND_CONFIG))) |
||
94 | { |
||
95 | twi_state = 4; // skip state |
||
96 | } |
||
97 | break; |
||
98 | case 2: // lower bits of setpoint (higher resolution) |
||
99 | i2c_write_byte((Motor[motor].SetPointLowerBits << 1) & 0x07); // send the lower bits of setpoint |
||
100 | // transmit config only on demand and the motors are not running and only for one motor per round trip |
||
101 | if( (BLFlags & BLFLAG_SEND_CONFIG) && !MotorenEin && (motor == motorread)) |
||
102 | { // prepare sending of configuration |
||
103 | byte_counter = 0; // reset send byte counter |
||
104 | crc = 0xAA; // init checksum |
||
105 | } |
||
106 | else |
||
107 | { // jump to state for end of transmission for that motor |
||
108 | twi_state = 4; |
||
109 | } |
||
110 | break; |
||
111 | case 3: |
||
112 | if(!byte_counter) // first byte? |
||
113 | { |
||
114 | i2c_write_byte(BL_CONF_SYNC_BYTE); |
||
115 | crc += BL_CONF_SYNC_BYTE; // update crc |
||
116 | pTxBuff = (uint8_t*)&BLConfig[motor]; // select configuration for motor |
||
117 | } |
||
118 | else if(byte_counter >= sizeof(BLConfig_t)+1) // last byte? |
||
119 | { // send crc byte at the end |
||
120 | i2c_write_byte(crc); |
||
121 | } |
||
122 | else // transmit configuration to BLs |
||
123 | { |
||
124 | i2c_write_byte(pTxBuff[byte_counter-1]); // submit next byte |
||
125 | crc += pTxBuff[byte_counter-1]; // update crc |
||
126 | twi_state = 3; // stay in this state |
||
127 | } |
||
128 | byte_counter++; // next byte |
||
129 | break; |
||
130 | case 4: |
||
131 | |||
132 | if(TWSR == 0x30) |
||
133 | { |
||
134 | if(!missing_motor) missing_motor = motor + 1; |
||
135 | if((Motor[motor].State & MOTOR_STATE_ERROR_MASK) < MOTOR_STATE_ERROR_MASK) Motor[motor].State++; // increment error counter and handle overflow |
||
136 | } |
||
137 | I2C_Stop(); |
||
138 | I2CTimeout = 10; |
||
139 | motor++; |
||
140 | twi_state = 0; |
||
141 | I2C_Start(); |
||
142 | break; |
||
1210 | hbuss | 143 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
144 | // Reading Data |
||
145 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
146 | case 5: |
||
1648 | killagreg | 147 | //Transmit 1st byte for reading |
148 | if(TWSR != 0x40) // Error? |
||
149 | { |
||
150 | Motor[motorread].State &= ~MOTOR_STATE_PRESENT_MASK; // clear present bit |
||
151 | if(++motorread >= MAX_MOTORS) |
||
1638 | holgerb | 152 | { |
1648 | killagreg | 153 | BLFlags &= ~BLFLAG_SEND_CONFIG; |
154 | motorread = 0; |
||
155 | if(++motorread_temperature >= MAX_MOTORS) |
||
156 | { |
||
157 | motorread_temperature = 0; |
||
158 | BLFlags &= ~BLFLAG_READ_VERSION; |
||
159 | } |
||
160 | } |
||
161 | I2C_Stop(); |
||
162 | twi_state = 0; |
||
163 | BLFlags |= BLFLAG_TX_COMPLETE; |
||
164 | } |
||
165 | else |
||
166 | { |
||
167 | Motor[motorread].State |= MOTOR_STATE_PRESENT_MASK; // set present bit |
||
168 | if(motorread == motorread_temperature) |
||
169 | { |
||
170 | read_more = 1; |
||
171 | I2C_ReceiveByte(); |
||
172 | } |
||
1638 | holgerb | 173 | else |
1648 | killagreg | 174 | { |
175 | read_more = 0; |
||
176 | I2C_ReceiveLastByte(); |
||
1642 | killagreg | 177 | } |
1648 | killagreg | 178 | } |
179 | MissingMotor = missing_motor; |
||
180 | missing_motor = 0; |
||
181 | break; |
||
182 | case 6: //Read 1st byte and transmit 2nd Byte |
||
1638 | holgerb | 183 | Motor[motorread].Current = TWDR; |
1648 | killagreg | 184 | if(read_more) |
185 | { |
||
186 | I2C_ReceiveByte() //ack |
||
187 | |||
188 | } |
||
1643 | holgerb | 189 | else |
1648 | killagreg | 190 | { |
191 | if(++motorread >= MAX_MOTORS) |
||
192 | { |
||
193 | motorread = 0; // restart from beginning |
||
194 | BLFlags &= ~BLFLAG_SEND_CONFIG; |
||
195 | if(++motorread_temperature >= MAX_MOTORS) |
||
196 | { |
||
197 | motorread_temperature = 0; |
||
198 | BLFlags &= ~BLFLAG_READ_VERSION; |
||
199 | } |
||
200 | } |
||
1643 | holgerb | 201 | I2C_Stop(); |
202 | twi_state = 0; |
||
1648 | killagreg | 203 | BLFlags |= BLFLAG_TX_COMPLETE; |
204 | } |
||
1638 | holgerb | 205 | break; |
1648 | killagreg | 206 | case 7: |
1638 | holgerb | 207 | //Read 2nd byte and transmit 3rd Byte |
208 | Motor[motorread].MaxPWM = TWDR; |
||
1648 | killagreg | 209 | if(BLFlags & BLFLAG_READ_VERSION) |
210 | { |
||
211 | if(TWDR == 250) |
||
212 | { |
||
213 | if(!MotorenEin) Motor[motorread].Version |= MOTOR_STATE_NEW_PROTOCOL_MASK; |
||
214 | } |
||
215 | else |
||
216 | { |
||
217 | Motor[motorread].Version = 0; |
||
218 | } |
||
219 | } |
||
1638 | holgerb | 220 | I2C_ReceiveLastByte(); //nack |
221 | break; |
||
1648 | killagreg | 222 | case 8: // read next |
1638 | holgerb | 223 | Motor[motorread].Temperature = TWDR; |
1648 | killagreg | 224 | if(++motorread >= MAX_MOTORS) |
225 | { |
||
226 | motorread = 0; // restart reading of first motor |
||
227 | BLFlags &= ~BLFLAG_SEND_CONFIG; |
||
228 | if(++motorread_temperature >= MAX_MOTORS) |
||
229 | { |
||
230 | motorread_temperature = 0; |
||
231 | BLFlags &= ~BLFLAG_READ_VERSION; |
||
232 | } |
||
233 | } |
||
1639 | holgerb | 234 | I2C_Stop(); |
1648 | killagreg | 235 | BLFlags |= BLFLAG_TX_COMPLETE; |
1638 | holgerb | 236 | twi_state = 0; |
1643 | holgerb | 237 | break; |
1638 | holgerb | 238 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
239 | // writing Gyro-Offset |
||
240 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
241 | case 18: |
||
242 | i2c_write_byte(0x98); // Address of the DAC |
||
243 | break; |
||
244 | case 19: |
||
245 | i2c_write_byte(0x10); // Update Channel A |
||
246 | break; |
||
247 | case 20: |
||
248 | i2c_write_byte(AnalogOffsetNick); // Value |
||
249 | break; |
||
250 | case 21: |
||
251 | i2c_write_byte(0x80); // Value |
||
252 | break; |
||
253 | case 22: |
||
1639 | holgerb | 254 | I2C_Stop(); |
1638 | holgerb | 255 | I2CTimeout = 10; |
1639 | holgerb | 256 | I2C_Start(); |
1638 | holgerb | 257 | break; |
258 | case 23: |
||
259 | i2c_write_byte(0x98); // Address of the DAC |
||
260 | break; |
||
261 | case 24: |
||
262 | i2c_write_byte(0x12); // Update Channel B |
||
263 | break; |
||
264 | case 25: |
||
265 | i2c_write_byte(AnalogOffsetRoll); // Value |
||
266 | break; |
||
267 | case 26: |
||
268 | i2c_write_byte(0x80); // Value |
||
269 | break; |
||
270 | case 27: |
||
1639 | holgerb | 271 | I2C_Stop(); |
1638 | holgerb | 272 | I2CTimeout = 10; |
1639 | holgerb | 273 | I2C_Start(); |
1638 | holgerb | 274 | break; |
275 | case 28: |
||
276 | i2c_write_byte(0x98); // Address of the DAC |
||
277 | break; |
||
278 | case 29: |
||
279 | i2c_write_byte(0x14); // Update Channel C |
||
280 | break; |
||
281 | case 30: |
||
282 | i2c_write_byte(AnalogOffsetGier); // Value |
||
283 | break; |
||
284 | case 31: |
||
285 | i2c_write_byte(0x80); // Value |
||
286 | break; |
||
287 | case 32: |
||
1639 | holgerb | 288 | I2C_Stop(); |
1638 | holgerb | 289 | I2CTimeout = 10; |
290 | twi_state = 0; |
||
291 | break; |
||
292 | default: twi_state = 0; |
||
293 | break; |
||
1648 | killagreg | 294 | } |
295 | TWCR |= 0x80; |
||
296 | J4Low; |
||
1638 | holgerb | 297 | } |
1639 | holgerb | 298 | |
1648 | killagreg | 299 | void I2C_SendBLConfig(void) |
300 | { |
||
301 | unsigned char i; |
||
302 | while(!(BLFlags & BLFLAG_TX_COMPLETE)); //wait for complete transfer |
||
303 | BLFlags |= BLFLAG_SEND_CONFIG; // enable sending of BL config |
||
304 | for(i = 0; i < MAX_MOTORS; i++) |
||
305 | { |
||
306 | Motor[i].SetPoint = 0; |
||
307 | Motor[i].SetPointLowerBits = 0; |
||
308 | } |
||
309 | motorread = 0; |
||
310 | // needs at least MAX_MOTORS loops of 2 ms (12*2ms = 24ms) |
||
311 | do |
||
312 | { |
||
313 | twi_state = 0; |
||
314 | I2C_Start(); // start an i2c transmission |
||
315 | while(!(BLFlags & BLFLAG_TX_COMPLETE)); //wait for complete transfer |
||
316 | }while(BLFlags & BLFLAG_SEND_CONFIG); // repeat until the BL config has been send to all motors |
||
317 | } |
||
318 | |||
319 | |||
320 |