Rev 2386 | Rev 2418 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1662 | killagreg | 1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + www.MikroKopter.com |
||
3 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
2050 | holgerb | 4 | // + Software Nutzungsbedingungen (english version: see below) |
5 | // + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt - |
||
6 | // + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den |
||
7 | // + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool |
||
8 | // + - nachfolgend Software genannt - nur für private Zwecke zu nutzen. |
||
9 | // + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig. |
||
1662 | killagreg | 10 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2050 | holgerb | 11 | // + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im |
12 | // + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu. |
||
13 | // + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie |
||
14 | // + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden. |
||
15 | // + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren |
||
16 | // + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
||
17 | // + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren |
||
18 | // + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand |
||
19 | // + des Mitverschuldens offen. |
||
20 | // + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet. |
||
21 | // + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
||
22 | // + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern. |
||
23 | // + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang |
||
24 | // + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt. |
||
25 | // + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software. |
||
26 | // + #### ENDE DER NUTZUNGSBEDINGUNGEN ####' |
||
27 | // + Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar. |
||
1662 | killagreg | 28 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2050 | holgerb | 29 | // + Software LICENSING TERMS |
1662 | killagreg | 30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2050 | holgerb | 31 | // + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor - |
32 | // + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware |
||
33 | // + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*. |
||
34 | // + The Software may only be used with the Licensor's products. |
||
35 | // + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this |
||
36 | // + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this |
||
37 | // + agreement shall be the property of the Licensor. |
||
38 | // + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other |
||
39 | // + features that can be used to identify the program may not be altered or defaced by the customer. |
||
40 | // + The customer shall be responsible for taking reasonable precautions |
||
41 | // + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the |
||
42 | // + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and |
||
43 | // + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product |
||
44 | // + liability. However, the Licensor shall be entitled to the defense of contributory negligence. |
||
45 | // + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test |
||
46 | // + the software for his purpose before any operational usage. The customer will backup his data before using the software. |
||
47 | // + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data |
||
48 | // + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations. |
||
49 | // + *) The territory aspect only refers to the place where the Software is used, not its programmed range. |
||
50 | // + #### END OF LICENSING TERMS #### |
||
51 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
||
1662 | killagreg | 52 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 | ingob | 53 | |
1662 | killagreg | 54 | #include <avr/io.h> |
55 | #include <avr/interrupt.h> |
||
56 | #include <util/twi.h> |
||
57 | #include "eeprom.h" |
||
58 | #include "twimaster.h" |
||
59 | #include "fc.h" |
||
60 | #include "analog.h" |
||
1665 | killagreg | 61 | #include "uart.h" |
1683 | killagreg | 62 | #include "timer0.h" |
1 | ingob | 63 | |
1662 | killagreg | 64 | volatile uint8_t twi_state = TWI_STATE_MOTOR_TX; |
65 | volatile uint8_t dac_channel = 0; |
||
66 | volatile uint8_t motor_write = 0; |
||
67 | volatile uint8_t motor_read = 0; |
||
1744 | holgerb | 68 | volatile uint8_t I2C_TransferActive = 0; |
2407 | holgerb | 69 | uint8_t Max_I2C_Packets = 12; |
1479 | killagreg | 70 | |
1662 | killagreg | 71 | volatile uint16_t I2CTimeout = 100; |
72 | |||
73 | uint8_t MissingMotor = 0; |
||
74 | |||
75 | volatile uint8_t BLFlags = 0; |
||
76 | |||
1479 | killagreg | 77 | MotorData_t Motor[MAX_MOTORS]; |
78 | |||
1662 | killagreg | 79 | // bit mask for witch BL the configuration should be sent |
80 | volatile uint16_t BLConfig_WriteMask = 0; |
||
81 | // bit mask for witch BL the configuration should be read |
||
82 | volatile uint16_t BLConfig_ReadMask = 0; |
||
83 | // buffer for BL Configuration |
||
84 | BLConfig_t BLConfig; |
||
1648 | killagreg | 85 | |
1662 | killagreg | 86 | #define I2C_WriteByte(byte) {TWDR = byte; TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);} |
87 | #define I2C_ReceiveByte() {TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);} |
||
88 | #define I2C_ReceiveLastByte() {TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);} |
||
1 | ingob | 89 | |
1662 | killagreg | 90 | #define SCL_CLOCK 200000L |
91 | #define I2C_TIMEOUT 30000 |
||
92 | #define TWI_BASE_ADDRESS 0x52 |
||
93 | |||
94 | /**************************************************/ |
||
95 | /* Initialize I2C (TWI) */ |
||
96 | /**************************************************/ |
||
97 | |||
1743 | holgerb | 98 | void I2C_Init(char clear) |
1 | ingob | 99 | { |
1662 | killagreg | 100 | uint8_t i; |
101 | uint8_t sreg = SREG; |
||
102 | cli(); |
||
1648 | killagreg | 103 | |
1662 | killagreg | 104 | // SDA is INPUT |
105 | DDRC &= ~(1<<DDC1); |
||
106 | // SCL is output |
||
107 | DDRC |= (1<<DDC0); |
||
108 | // pull up SDA |
||
109 | PORTC |= (1<<PORTC0)|(1<<PORTC1); |
||
1648 | killagreg | 110 | |
1662 | killagreg | 111 | // TWI Status Register |
112 | // prescaler 1 (TWPS1 = 0, TWPS0 = 0) |
||
113 | TWSR &= ~((1<<TWPS1)|(1<<TWPS0)); |
||
114 | |||
115 | // set TWI Bit Rate Register |
||
116 | TWBR = ((F_CPU/SCL_CLOCK)-16)/2; |
||
117 | |||
118 | twi_state = TWI_STATE_MOTOR_TX; |
||
119 | motor_write = 0; |
||
120 | motor_read = 0; |
||
121 | |||
1743 | holgerb | 122 | if(clear) for(i=0; i < MAX_MOTORS; i++) |
1648 | killagreg | 123 | { |
124 | Motor[i].Version = 0; |
||
125 | Motor[i].SetPoint = 0; |
||
126 | Motor[i].SetPointLowerBits = 0; |
||
127 | Motor[i].State = 0; |
||
1666 | killagreg | 128 | Motor[i].ReadMode = BL_READMODE_STATUS; |
1648 | killagreg | 129 | Motor[i].Current = 0; |
130 | Motor[i].MaxPWM = 0; |
||
1666 | killagreg | 131 | Motor[i].Temperature = 0; |
1648 | killagreg | 132 | } |
1743 | holgerb | 133 | sei(); |
1662 | killagreg | 134 | SREG = sreg; |
1 | ingob | 135 | } |
136 | |||
1662 | killagreg | 137 | void I2C_Reset(void) |
173 | holgerb | 138 | { |
1662 | killagreg | 139 | // stop i2c bus |
140 | I2C_Stop(TWI_STATE_MOTOR_TX); |
||
141 | TWCR = (1<<TWINT); // reset to original state incl. interrupt flag reset |
||
1648 | killagreg | 142 | TWAMR = 0; |
143 | TWAR = 0; |
||
144 | TWDR = 0; |
||
145 | TWSR = 0; |
||
146 | TWBR = 0; |
||
1765 | killagreg | 147 | I2C_TransferActive = 0; |
1743 | holgerb | 148 | I2C_Init(0); |
1662 | killagreg | 149 | I2C_WriteByte(0); |
150 | BLFlags |= BLFLAG_READ_VERSION; |
||
173 | holgerb | 151 | } |
1 | ingob | 152 | |
1662 | killagreg | 153 | /****************************************/ |
154 | /* I2C ISR */ |
||
155 | /****************************************/ |
||
156 | ISR (TWI_vect) |
||
157 | { |
||
158 | static uint8_t missing_motor = 0, motor_read_temperature = 0; |
||
159 | static uint8_t *pBuff = 0; |
||
160 | static uint8_t BuffLen = 0; |
||
2407 | holgerb | 161 | static uint8_t max_packets = 0; |
1662 | killagreg | 162 | switch (twi_state++) |
1648 | killagreg | 163 | { |
1662 | killagreg | 164 | // Master Transmit |
165 | case 0: // TWI_STATE_MOTOR_TX |
||
1744 | holgerb | 166 | I2C_TransferActive = 1; |
1662 | killagreg | 167 | // skip motor if not used in mixer |
168 | while((Mixer.Motor[motor_write][MIX_GAS] <= 0) && (motor_write < MAX_MOTORS)) motor_write++; |
||
2407 | holgerb | 169 | motor_write %= MAX_MOTORS; |
170 | if(++max_packets > Max_I2C_Packets) // writing finished, read now |
||
1648 | killagreg | 171 | { |
2407 | holgerb | 172 | max_packets = 0; |
1662 | killagreg | 173 | BLConfig_WriteMask = 0; // reset configuration bitmask |
2407 | holgerb | 174 | //motor_write = 0; // reset motor write counter for next cycle |
1662 | killagreg | 175 | twi_state = TWI_STATE_MOTOR_RX; |
176 | I2C_WriteByte(TWI_BASE_ADDRESS + TW_READ + (motor_read<<1) ); // select slave address in rx mode |
||
1648 | killagreg | 177 | } |
1662 | killagreg | 178 | else I2C_WriteByte(TWI_BASE_ADDRESS + TW_WRITE + (motor_write<<1) ); // select slave address in tx mode |
1648 | killagreg | 179 | break; |
1662 | killagreg | 180 | case 1: // Send Data to Slave |
181 | I2C_WriteByte(Motor[motor_write].SetPoint); // transmit setpoint |
||
182 | // if old version has been detected |
||
183 | if(!(Motor[motor_write].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)) |
||
1648 | killagreg | 184 | { |
185 | twi_state = 4; //jump over sending more data |
||
186 | } |
||
1662 | killagreg | 187 | // the new version has been detected |
188 | else if(!( (Motor[motor_write].SetPointLowerBits && (RequiredMotors < 7)) || BLConfig_WriteMask || BLConfig_ReadMask ) ) |
||
189 | { // or LowerBits are zero and no BlConfig should be sent (saves round trip time) |
||
190 | twi_state = 4; //jump over sending more data |
||
1648 | killagreg | 191 | } |
192 | break; |
||
1662 | killagreg | 193 | case 2: // lower bits of setpoint (higher resolution) |
1666 | killagreg | 194 | if ((0x0001<<motor_write) & BLConfig_ReadMask) |
1662 | killagreg | 195 | { |
1666 | killagreg | 196 | Motor[motor_write].ReadMode = BL_READMODE_CONFIG; // configuration request |
1648 | killagreg | 197 | } |
198 | else |
||
199 | { |
||
1666 | killagreg | 200 | Motor[motor_write].ReadMode = BL_READMODE_STATUS; // normal status request |
1648 | killagreg | 201 | } |
1662 | killagreg | 202 | // send read mode and the lower bits of setpoint |
1666 | killagreg | 203 | I2C_WriteByte((Motor[motor_write].ReadMode<<3)|(Motor[motor_write].SetPointLowerBits & 0x07)); |
1662 | killagreg | 204 | // configuration tranmission request? |
205 | if((0x0001<<motor_write) & BLConfig_WriteMask) |
||
206 | { // redirect tx pointer to configuration data |
||
207 | pBuff = (uint8_t*)&BLConfig; // select config for motor |
||
208 | BuffLen = sizeof(BLConfig_t); |
||
1651 | killagreg | 209 | } |
1662 | killagreg | 210 | else |
211 | { // jump to end of transmission for that motor |
||
212 | twi_state = 4; |
||
1648 | killagreg | 213 | } |
214 | break; |
||
1662 | killagreg | 215 | case 3: // send configuration |
216 | I2C_WriteByte(*pBuff); |
||
217 | pBuff++; |
||
218 | if(--BuffLen > 0) twi_state = 3; // if there are some bytes left |
||
219 | break; |
||
220 | case 4: // repeat case 0-4 for all motors |
||
221 | if(TWSR == TW_MT_DATA_NACK) // Data transmitted, NACK received |
||
1648 | killagreg | 222 | { |
1662 | killagreg | 223 | if(!missing_motor) missing_motor = motor_write + 1; |
224 | if((Motor[motor_write].State & MOTOR_STATE_ERROR_MASK) < MOTOR_STATE_ERROR_MASK) Motor[motor_write].State++; // increment error counter and handle overflow |
||
1648 | killagreg | 225 | } |
1662 | killagreg | 226 | I2C_Stop(TWI_STATE_MOTOR_TX); |
1648 | killagreg | 227 | I2CTimeout = 10; |
1662 | killagreg | 228 | motor_write++; // next motor |
229 | I2C_Start(TWI_STATE_MOTOR_TX); // Repeated start -> switch slave or switch Master Transmit -> Master Receive |
||
1648 | killagreg | 230 | break; |
1662 | killagreg | 231 | // Master Receive Data |
232 | case 5: // TWI_STATE_MOTOR_RX |
||
233 | if(TWSR != TW_MR_SLA_ACK) // SLA+R transmitted but no ACK received |
||
234 | { // no response from the addressed slave received |
||
235 | Motor[motor_read].State &= ~MOTOR_STATE_PRESENT_MASK; // clear present bit |
||
236 | if(++motor_read >= MAX_MOTORS) |
||
237 | { // all motors read |
||
238 | motor_read = 0; // restart from beginning |
||
239 | BLConfig_ReadMask = 0; // reset read configuration bitmask |
||
240 | if(++motor_read_temperature >= MAX_MOTORS) |
||
1683 | killagreg | 241 | { |
1662 | killagreg | 242 | motor_read_temperature = 0; |
1648 | killagreg | 243 | BLFlags &= ~BLFLAG_READ_VERSION; |
244 | } |
||
245 | } |
||
246 | BLFlags |= BLFLAG_TX_COMPLETE; |
||
1662 | killagreg | 247 | I2C_Stop(TWI_STATE_MOTOR_TX); |
1744 | holgerb | 248 | I2C_TransferActive = 0; |
1648 | killagreg | 249 | } |
250 | else |
||
1662 | killagreg | 251 | { // motor successfully addressed |
252 | Motor[motor_read].State |= MOTOR_STATE_PRESENT_MASK; // set present bit |
||
253 | if(Motor[motor_read].Version & MOTOR_STATE_NEW_PROTOCOL_MASK) |
||
1648 | killagreg | 254 | { |
1662 | killagreg | 255 | // new BL found |
1666 | killagreg | 256 | switch(Motor[motor_read].ReadMode) |
1662 | killagreg | 257 | { |
1666 | killagreg | 258 | case BL_READMODE_CONFIG: |
1662 | killagreg | 259 | pBuff = (uint8_t*)&BLConfig; |
260 | BuffLen = sizeof(BLConfig_t); |
||
2370 | holgerb | 261 | Motor[motor_read].ReadMode = BL_READMODE_STATUS; // only once |
1662 | killagreg | 262 | break; |
1666 | killagreg | 263 | case BL_READMODE_STATUS: |
1662 | killagreg | 264 | pBuff = (uint8_t*)&(Motor[motor_read].Current); |
265 | if(motor_read == motor_read_temperature) BuffLen = 3; // read Current, MaxPwm & Temp |
||
266 | else BuffLen = 1;// read Current only |
||
267 | break; |
||
268 | } |
||
1648 | killagreg | 269 | } |
1662 | killagreg | 270 | else // old BL version |
271 | { |
||
272 | pBuff = (uint8_t*)&(Motor[motor_read].Current); |
||
1672 | killagreg | 273 | if((BLFlags & BLFLAG_READ_VERSION) || (motor_read == motor_read_temperature)) BuffLen = 2; // Current & MaxPwm |
1662 | killagreg | 274 | else BuffLen = 1; // read Current only |
275 | } |
||
276 | if(BuffLen == 1) |
||
277 | { |
||
278 | I2C_ReceiveLastByte(); // read last byte |
||
279 | } |
||
1638 | holgerb | 280 | else |
1648 | killagreg | 281 | { |
1662 | killagreg | 282 | I2C_ReceiveByte(); // read next byte |
1642 | killagreg | 283 | } |
1648 | killagreg | 284 | } |
285 | MissingMotor = missing_motor; |
||
286 | missing_motor = 0; |
||
287 | break; |
||
1662 | killagreg | 288 | case 6: // receive bytes |
289 | *pBuff = TWDR; |
||
290 | pBuff++; |
||
291 | BuffLen--; |
||
292 | if(BuffLen>1) |
||
293 | { |
||
294 | I2C_ReceiveByte(); // read next byte |
||
295 | } |
||
296 | else if (BuffLen == 1) |
||
297 | { |
||
298 | I2C_ReceiveLastByte(); // read last byte |
||
299 | } |
||
300 | else // nothing left |
||
301 | { |
||
302 | if(BLFlags & BLFLAG_READ_VERSION) |
||
1648 | killagreg | 303 | { |
2380 | holgerb | 304 | // if(!(FC_StatusFlags & FC_STATUS_MOTOR_RUN) && ((Motor[motor_read].MaxPWM & 252) == 248)) Motor[motor_read].Version |= MOTOR_STATE_NEW_PROTOCOL_MASK; |
305 | // else Motor[motor_read].Version = 0; |
||
306 | if(!(FC_StatusFlags & FC_STATUS_MOTOR_RUN)) |
||
307 | { |
||
308 | if((Motor[motor_read].MaxPWM & 252) == 248) Motor[motor_read].Version |= MOTOR_STATE_NEW_PROTOCOL_MASK; |
||
309 | else Motor[motor_read].Version = 0; |
||
2386 | holgerb | 310 | if(Motor[motor_read].MaxPWM == 248) Motor[motor_read].Version |= (MOTOR_STATE_FAST_MODE | MOTOR_STATE_BL30); |
311 | else |
||
312 | if(Motor[motor_read].MaxPWM == 249) Motor[motor_read].Version |= MOTOR_STATE_BL30; |
||
2380 | holgerb | 313 | } |
1648 | killagreg | 314 | } |
1662 | killagreg | 315 | if(++motor_read >= MAX_MOTORS) |
1648 | killagreg | 316 | { |
1662 | killagreg | 317 | motor_read = 0; // restart from beginning |
318 | BLConfig_ReadMask = 0; // reset read configuration bitmask |
||
319 | if(++motor_read_temperature >= MAX_MOTORS) |
||
1648 | killagreg | 320 | { |
1662 | killagreg | 321 | motor_read_temperature = 0; |
322 | BLFlags &= ~BLFLAG_READ_VERSION; |
||
1648 | killagreg | 323 | } |
324 | } |
||
1662 | killagreg | 325 | I2C_Stop(TWI_STATE_MOTOR_TX); |
326 | BLFlags |= BLFLAG_TX_COMPLETE; |
||
1744 | holgerb | 327 | I2C_TransferActive = 0; |
1662 | killagreg | 328 | return; |
329 | } |
||
330 | twi_state = 6; // if there are some bytes left |
||
331 | break; |
||
332 | |||
333 | // writing Gyro-Offsets |
||
334 | case 18: |
||
335 | I2C_WriteByte(0x98); // Address the DAC |
||
336 | break; |
||
337 | |||
338 | case 19: |
||
339 | I2C_WriteByte(0x10 + (dac_channel * 2)); // Select DAC Channel (0x10 = A, 0x12 = B, 0x14 = C) |
||
340 | break; |
||
341 | |||
342 | case 20: |
||
343 | switch(dac_channel) |
||
344 | { |
||
345 | case 0: |
||
346 | I2C_WriteByte(AnalogOffsetNick); // 1st byte for Channel A |
||
347 | break; |
||
348 | case 1: |
||
349 | I2C_WriteByte(AnalogOffsetRoll); // 1st byte for Channel B |
||
350 | break; |
||
351 | case 2: |
||
352 | I2C_WriteByte(AnalogOffsetGier); // 1st byte for Channel C |
||
353 | break; |
||
354 | } |
||
355 | break; |
||
356 | |||
357 | case 21: |
||
358 | I2C_WriteByte(0x80); // 2nd byte for all channels is 0x80 |
||
359 | break; |
||
360 | |||
361 | case 22: |
||
362 | I2C_Stop(TWI_STATE_MOTOR_TX); |
||
1744 | holgerb | 363 | I2C_TransferActive = 0; |
1662 | killagreg | 364 | I2CTimeout = 10; |
1665 | killagreg | 365 | // repeat case 18...22 until all DAC Channels are updated |
1662 | killagreg | 366 | if(dac_channel < 2) |
367 | { |
||
368 | dac_channel ++; // jump to next channel |
||
369 | I2C_Start(TWI_STATE_GYRO_OFFSET_TX); // start transmission for next channel |
||
370 | } |
||
371 | else |
||
1665 | killagreg | 372 | { |
1662 | killagreg | 373 | dac_channel = 0; // reset dac channel counter |
1665 | killagreg | 374 | BLFlags |= BLFLAG_TX_COMPLETE; |
1662 | killagreg | 375 | } |
376 | break; |
||
377 | default: |
||
378 | I2C_Stop(TWI_STATE_MOTOR_TX); |
||
379 | BLFlags |= BLFLAG_TX_COMPLETE; |
||
380 | I2CTimeout = 10; |
||
381 | motor_write = 0; |
||
382 | motor_read = 0; |
||
1744 | holgerb | 383 | I2C_TransferActive = 0; |
1662 | killagreg | 384 | break; |
1648 | killagreg | 385 | } |
1662 | killagreg | 386 | |
1638 | holgerb | 387 | } |
1639 | holgerb | 388 | |
1662 | killagreg | 389 | |
390 | uint8_t I2C_WriteBLConfig(uint8_t motor) |
||
1648 | killagreg | 391 | { |
1662 | killagreg | 392 | uint8_t i; |
1683 | killagreg | 393 | uint16_t timer; |
1662 | killagreg | 394 | |
1665 | killagreg | 395 | if(MotorenEin || PC_MotortestActive) return(BLCONFIG_ERR_MOTOR_RUNNING); // not when motors are running! |
396 | if(motor > MAX_MOTORS) return (BLCONFIG_ERR_MOTOR_NOT_EXIST); // motor does not exist! |
||
1662 | killagreg | 397 | if(motor) |
398 | { |
||
1665 | killagreg | 399 | if(!(Motor[motor-1].State & MOTOR_STATE_PRESENT_MASK)) return(BLCONFIG_ERR_MOTOR_NOT_EXIST); // motor does not exist! |
400 | if(!(Motor[motor-1].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)) return(BLCONFIG_ERR_HW_NOT_COMPATIBLE); // not a new BL! |
||
1662 | killagreg | 401 | } |
402 | // check BL configuration to send |
||
2370 | holgerb | 403 | if((BLConfig.Revision & 0x0B) != BLCONFIG_REVISION) return (BLCONFIG_ERR_SW_NOT_COMPATIBLE); // bad revison |
1662 | killagreg | 404 | i = RAM_Checksum((uint8_t*)&BLConfig, sizeof(BLConfig_t) - 1); |
1665 | killagreg | 405 | if(i != BLConfig.crc) return(BLCONFIG_ERR_CHECKSUM); // bad checksum |
1662 | killagreg | 406 | |
1683 | killagreg | 407 | timer = SetDelay(2000); |
408 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
||
1662 | killagreg | 409 | |
410 | // prepare the bitmask |
||
411 | if(!motor) // 0 means all |
||
412 | { |
||
413 | BLConfig_WriteMask = 0xFF; // all motors at once with the same configuration |
||
414 | } |
||
415 | else //only one specific motor |
||
416 | { |
||
417 | BLConfig_WriteMask = 0x0001<<(motor-1); |
||
418 | } |
||
1648 | killagreg | 419 | for(i = 0; i < MAX_MOTORS; i++) |
420 | { |
||
1662 | killagreg | 421 | if((0x0001<<i) & BLConfig_WriteMask) |
422 | { |
||
423 | Motor[i].SetPoint = 0; |
||
424 | Motor[i].SetPointLowerBits = 0; |
||
425 | } |
||
1648 | killagreg | 426 | } |
1665 | killagreg | 427 | |
1662 | killagreg | 428 | motor_write = 0; |
1648 | killagreg | 429 | // needs at least MAX_MOTORS loops of 2 ms (12*2ms = 24ms) |
430 | do |
||
431 | { |
||
1662 | killagreg | 432 | I2C_Start(TWI_STATE_MOTOR_TX); // start an i2c transmission |
1683 | killagreg | 433 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
434 | }while(BLConfig_WriteMask && !CheckDelay(timer)); // repeat until the BL config has been sent |
||
435 | if(BLConfig_WriteMask) return(BLCONFIG_ERR_MOTOR_NOT_EXIST); |
||
1665 | killagreg | 436 | return(BLCONFIG_SUCCESS); |
1648 | killagreg | 437 | } |
438 | |||
1662 | killagreg | 439 | uint8_t I2C_ReadBLConfig(uint8_t motor) |
440 | { |
||
441 | uint8_t i; |
||
1683 | killagreg | 442 | uint16_t timer; |
1648 | killagreg | 443 | |
1665 | killagreg | 444 | if(MotorenEin || PC_MotortestActive) return(BLCONFIG_ERR_MOTOR_RUNNING); // not when motors are running! |
1673 | killagreg | 445 | if(motor > MAX_MOTORS) return (BLCONFIG_ERR_MOTOR_NOT_EXIST); // motor does not exist! |
446 | if(motor == 0) return (BLCONFIG_ERR_READ_NOT_POSSIBLE); |
||
1665 | killagreg | 447 | if(!(Motor[motor-1].State & MOTOR_STATE_PRESENT_MASK)) return(BLCONFIG_ERR_MOTOR_NOT_EXIST); // motor does not exist! |
448 | if(!(Motor[motor-1].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)) return(BLCONFIG_ERR_HW_NOT_COMPATIBLE); // not a new BL! |
||
449 | |||
1683 | killagreg | 450 | timer = SetDelay(2000); |
451 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
||
1648 | killagreg | 452 | |
1662 | killagreg | 453 | // prepare the bitmask |
454 | BLConfig_ReadMask = 0x0001<<(motor-1); |
||
1665 | killagreg | 455 | |
1662 | killagreg | 456 | for(i = 0; i < MAX_MOTORS; i++) |
457 | { |
||
458 | if((0x0001<<i) & BLConfig_ReadMask) |
||
459 | { |
||
460 | Motor[i].SetPoint = 0; |
||
461 | Motor[i].SetPointLowerBits = 0; |
||
462 | } |
||
463 | } |
||
1665 | killagreg | 464 | |
1662 | killagreg | 465 | motor_read = 0; |
466 | BLConfig.Revision = 0; // bad revision |
||
467 | BLConfig.crc = 0; // bad checksum |
||
468 | // needs at least MAX_MOTORS loops of 2 ms (12*2ms = 24ms) |
||
469 | do |
||
470 | { |
||
471 | I2C_Start(TWI_STATE_MOTOR_TX); // start an i2c transmission |
||
1683 | killagreg | 472 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
473 | }while(BLConfig_ReadMask && !CheckDelay(timer)); // repeat until the BL config has been received from all motors |
||
1662 | killagreg | 474 | // validate result |
2370 | holgerb | 475 | if((BLConfig.Revision & 0x0B) != BLCONFIG_REVISION) return (BLCONFIG_ERR_SW_NOT_COMPATIBLE); // bad revison |
1662 | killagreg | 476 | i = RAM_Checksum((uint8_t*)&BLConfig, sizeof(BLConfig_t) - 1); |
1665 | killagreg | 477 | if(i != BLConfig.crc) return(BLCONFIG_ERR_CHECKSUM); // bad checksum |
478 | return(BLCONFIG_SUCCESS); |
||
1662 | killagreg | 479 | } |
1665 | killagreg | 480 |