Rev 2492 | 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" |
2443 | holgerb | 63 | #include "main.h" |
1 | ingob | 64 | |
2466 | holgerb | 65 | volatile uint8_t twi_state = TWI_STATE_MOTOR_TX, ReadBlSize = 9; |
1662 | killagreg | 66 | volatile uint8_t dac_channel = 0; |
67 | volatile uint8_t motor_write = 0; |
||
68 | volatile uint8_t motor_read = 0; |
||
1744 | holgerb | 69 | volatile uint8_t I2C_TransferActive = 0; |
2407 | holgerb | 70 | uint8_t Max_I2C_Packets = 12; |
1479 | killagreg | 71 | |
1662 | killagreg | 72 | volatile uint16_t I2CTimeout = 100; |
73 | |||
74 | uint8_t MissingMotor = 0; |
||
75 | |||
76 | volatile uint8_t BLFlags = 0; |
||
77 | |||
1479 | killagreg | 78 | MotorData_t Motor[MAX_MOTORS]; |
2418 | holgerb | 79 | RedundantBl_t RedundantMotor[MAX_MOTORS]; |
1479 | killagreg | 80 | |
1662 | killagreg | 81 | // bit mask for witch BL the configuration should be sent |
82 | volatile uint16_t BLConfig_WriteMask = 0; |
||
83 | // bit mask for witch BL the configuration should be read |
||
84 | volatile uint16_t BLConfig_ReadMask = 0; |
||
85 | // buffer for BL Configuration |
||
86 | BLConfig_t BLConfig; |
||
1648 | killagreg | 87 | |
1662 | killagreg | 88 | #define I2C_WriteByte(byte) {TWDR = byte; TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);} |
89 | #define I2C_ReceiveByte() {TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);} |
||
90 | #define I2C_ReceiveLastByte() {TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);} |
||
1 | ingob | 91 | |
1662 | killagreg | 92 | #define SCL_CLOCK 200000L |
93 | #define I2C_TIMEOUT 30000 |
||
94 | #define TWI_BASE_ADDRESS 0x52 |
||
95 | |||
96 | /**************************************************/ |
||
97 | /* Initialize I2C (TWI) */ |
||
98 | /**************************************************/ |
||
99 | |||
1743 | holgerb | 100 | void I2C_Init(char clear) |
1 | ingob | 101 | { |
1662 | killagreg | 102 | uint8_t i; |
103 | uint8_t sreg = SREG; |
||
104 | cli(); |
||
1648 | killagreg | 105 | |
1662 | killagreg | 106 | // SDA is INPUT |
107 | DDRC &= ~(1<<DDC1); |
||
108 | // SCL is output |
||
109 | DDRC |= (1<<DDC0); |
||
110 | // pull up SDA |
||
111 | PORTC |= (1<<PORTC0)|(1<<PORTC1); |
||
1648 | killagreg | 112 | |
1662 | killagreg | 113 | // TWI Status Register |
114 | // prescaler 1 (TWPS1 = 0, TWPS0 = 0) |
||
115 | TWSR &= ~((1<<TWPS1)|(1<<TWPS0)); |
||
116 | |||
117 | // set TWI Bit Rate Register |
||
118 | TWBR = ((F_CPU/SCL_CLOCK)-16)/2; |
||
119 | |||
120 | twi_state = TWI_STATE_MOTOR_TX; |
||
121 | motor_write = 0; |
||
122 | motor_read = 0; |
||
123 | |||
1743 | holgerb | 124 | if(clear) for(i=0; i < MAX_MOTORS; i++) |
1648 | killagreg | 125 | { |
2466 | holgerb | 126 | Motor[i].Version = 0; |
127 | Motor[i].SetPoint = 0; |
||
1648 | killagreg | 128 | Motor[i].SetPointLowerBits = 0; |
2466 | holgerb | 129 | Motor[i].State = 0; |
130 | Motor[i].ReadMode = BL_READMODE_STATUS; |
||
131 | Motor[i].Current = 0; |
||
132 | Motor[i].MaxPWM = 0; |
||
133 | Motor[i].Temperature = 0; |
||
134 | Motor[i].NotReadyCnt = 0; |
||
2468 | holgerb | 135 | Motor[i].RPM = 0; |
2466 | holgerb | 136 | Motor[i].reserved1 = 0; |
137 | Motor[i].Voltage = 0; |
||
138 | Motor[i].SlaveI2cError = 0; |
||
139 | Motor[i].VersionMajor = 0; |
||
140 | Motor[i].VersionMinor = 0; |
||
1648 | killagreg | 141 | } |
1743 | holgerb | 142 | sei(); |
1662 | killagreg | 143 | SREG = sreg; |
1 | ingob | 144 | } |
145 | |||
1662 | killagreg | 146 | void I2C_Reset(void) |
173 | holgerb | 147 | { |
1662 | killagreg | 148 | // stop i2c bus |
149 | I2C_Stop(TWI_STATE_MOTOR_TX); |
||
150 | TWCR = (1<<TWINT); // reset to original state incl. interrupt flag reset |
||
1648 | killagreg | 151 | TWAMR = 0; |
152 | TWAR = 0; |
||
153 | TWDR = 0; |
||
154 | TWSR = 0; |
||
155 | TWBR = 0; |
||
1765 | killagreg | 156 | I2C_TransferActive = 0; |
1743 | holgerb | 157 | I2C_Init(0); |
1662 | killagreg | 158 | I2C_WriteByte(0); |
159 | BLFlags |= BLFLAG_READ_VERSION; |
||
173 | holgerb | 160 | } |
1 | ingob | 161 | |
1662 | killagreg | 162 | /****************************************/ |
163 | /* I2C ISR */ |
||
164 | /****************************************/ |
||
165 | ISR (TWI_vect) |
||
166 | { |
||
167 | static uint8_t missing_motor = 0, motor_read_temperature = 0; |
||
168 | static uint8_t *pBuff = 0; |
||
169 | static uint8_t BuffLen = 0; |
||
2407 | holgerb | 170 | static uint8_t max_packets = 0; |
1662 | killagreg | 171 | switch (twi_state++) |
1648 | killagreg | 172 | { |
1662 | killagreg | 173 | // Master Transmit |
174 | case 0: // TWI_STATE_MOTOR_TX |
||
1744 | holgerb | 175 | I2C_TransferActive = 1; |
1662 | killagreg | 176 | // skip motor if not used in mixer |
177 | while((Mixer.Motor[motor_write][MIX_GAS] <= 0) && (motor_write < MAX_MOTORS)) motor_write++; |
||
2407 | holgerb | 178 | motor_write %= MAX_MOTORS; |
179 | if(++max_packets > Max_I2C_Packets) // writing finished, read now |
||
1648 | killagreg | 180 | { |
2407 | holgerb | 181 | max_packets = 0; |
1662 | killagreg | 182 | BLConfig_WriteMask = 0; // reset configuration bitmask |
2407 | holgerb | 183 | //motor_write = 0; // reset motor write counter for next cycle |
1662 | killagreg | 184 | twi_state = TWI_STATE_MOTOR_RX; |
185 | I2C_WriteByte(TWI_BASE_ADDRESS + TW_READ + (motor_read<<1) ); // select slave address in rx mode |
||
1648 | killagreg | 186 | } |
1662 | killagreg | 187 | else I2C_WriteByte(TWI_BASE_ADDRESS + TW_WRITE + (motor_write<<1) ); // select slave address in tx mode |
1648 | killagreg | 188 | break; |
1662 | killagreg | 189 | case 1: // Send Data to Slave |
190 | I2C_WriteByte(Motor[motor_write].SetPoint); // transmit setpoint |
||
191 | // if old version has been detected |
||
192 | if(!(Motor[motor_write].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)) |
||
1648 | killagreg | 193 | { |
194 | twi_state = 4; //jump over sending more data |
||
195 | } |
||
1662 | killagreg | 196 | // the new version has been detected |
197 | else if(!( (Motor[motor_write].SetPointLowerBits && (RequiredMotors < 7)) || BLConfig_WriteMask || BLConfig_ReadMask ) ) |
||
198 | { // or LowerBits are zero and no BlConfig should be sent (saves round trip time) |
||
199 | twi_state = 4; //jump over sending more data |
||
1648 | killagreg | 200 | } |
201 | break; |
||
1662 | killagreg | 202 | case 2: // lower bits of setpoint (higher resolution) |
1666 | killagreg | 203 | if ((0x0001<<motor_write) & BLConfig_ReadMask) |
1662 | killagreg | 204 | { |
1666 | killagreg | 205 | Motor[motor_write].ReadMode = BL_READMODE_CONFIG; // configuration request |
1648 | killagreg | 206 | } |
207 | else |
||
208 | { |
||
1666 | killagreg | 209 | Motor[motor_write].ReadMode = BL_READMODE_STATUS; // normal status request |
1648 | killagreg | 210 | } |
1662 | killagreg | 211 | // send read mode and the lower bits of setpoint |
1666 | killagreg | 212 | I2C_WriteByte((Motor[motor_write].ReadMode<<3)|(Motor[motor_write].SetPointLowerBits & 0x07)); |
1662 | killagreg | 213 | // configuration tranmission request? |
214 | if((0x0001<<motor_write) & BLConfig_WriteMask) |
||
215 | { // redirect tx pointer to configuration data |
||
216 | pBuff = (uint8_t*)&BLConfig; // select config for motor |
||
217 | BuffLen = sizeof(BLConfig_t); |
||
1651 | killagreg | 218 | } |
1662 | killagreg | 219 | else |
220 | { // jump to end of transmission for that motor |
||
221 | twi_state = 4; |
||
1648 | killagreg | 222 | } |
223 | break; |
||
1662 | killagreg | 224 | case 3: // send configuration |
225 | I2C_WriteByte(*pBuff); |
||
226 | pBuff++; |
||
227 | if(--BuffLen > 0) twi_state = 3; // if there are some bytes left |
||
228 | break; |
||
229 | case 4: // repeat case 0-4 for all motors |
||
230 | if(TWSR == TW_MT_DATA_NACK) // Data transmitted, NACK received |
||
1648 | killagreg | 231 | { |
1662 | killagreg | 232 | if(!missing_motor) missing_motor = motor_write + 1; |
233 | 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 | 234 | } |
1662 | killagreg | 235 | I2C_Stop(TWI_STATE_MOTOR_TX); |
1648 | killagreg | 236 | I2CTimeout = 10; |
1662 | killagreg | 237 | motor_write++; // next motor |
238 | I2C_Start(TWI_STATE_MOTOR_TX); // Repeated start -> switch slave or switch Master Transmit -> Master Receive |
||
1648 | killagreg | 239 | break; |
1662 | killagreg | 240 | // Master Receive Data |
241 | case 5: // TWI_STATE_MOTOR_RX |
||
242 | if(TWSR != TW_MR_SLA_ACK) // SLA+R transmitted but no ACK received |
||
243 | { // no response from the addressed slave received |
||
244 | Motor[motor_read].State &= ~MOTOR_STATE_PRESENT_MASK; // clear present bit |
||
245 | if(++motor_read >= MAX_MOTORS) |
||
246 | { // all motors read |
||
247 | motor_read = 0; // restart from beginning |
||
248 | BLConfig_ReadMask = 0; // reset read configuration bitmask |
||
249 | if(++motor_read_temperature >= MAX_MOTORS) |
||
1683 | killagreg | 250 | { |
1662 | killagreg | 251 | motor_read_temperature = 0; |
1648 | killagreg | 252 | BLFlags &= ~BLFLAG_READ_VERSION; |
253 | } |
||
254 | } |
||
255 | BLFlags |= BLFLAG_TX_COMPLETE; |
||
1662 | killagreg | 256 | I2C_Stop(TWI_STATE_MOTOR_TX); |
1744 | holgerb | 257 | I2C_TransferActive = 0; |
1648 | killagreg | 258 | } |
259 | else |
||
1662 | killagreg | 260 | { // motor successfully addressed |
261 | Motor[motor_read].State |= MOTOR_STATE_PRESENT_MASK; // set present bit |
||
262 | if(Motor[motor_read].Version & MOTOR_STATE_NEW_PROTOCOL_MASK) |
||
1648 | killagreg | 263 | { |
1662 | killagreg | 264 | // new BL found |
1666 | killagreg | 265 | switch(Motor[motor_read].ReadMode) |
1662 | killagreg | 266 | { |
1666 | killagreg | 267 | case BL_READMODE_CONFIG: |
1662 | killagreg | 268 | pBuff = (uint8_t*)&BLConfig; |
269 | BuffLen = sizeof(BLConfig_t); |
||
2370 | holgerb | 270 | Motor[motor_read].ReadMode = BL_READMODE_STATUS; // only once |
1662 | killagreg | 271 | break; |
1666 | killagreg | 272 | case BL_READMODE_STATUS: |
1662 | killagreg | 273 | pBuff = (uint8_t*)&(Motor[motor_read].Current); |
2466 | holgerb | 274 | if(motor_read == motor_read_temperature) BuffLen = ReadBlSize; // read Current, MaxPwm & Temp (is 3 or 9) |
1662 | killagreg | 275 | else BuffLen = 1;// read Current only |
276 | break; |
||
277 | } |
||
1648 | killagreg | 278 | } |
1662 | killagreg | 279 | else // old BL version |
280 | { |
||
281 | pBuff = (uint8_t*)&(Motor[motor_read].Current); |
||
1672 | killagreg | 282 | if((BLFlags & BLFLAG_READ_VERSION) || (motor_read == motor_read_temperature)) BuffLen = 2; // Current & MaxPwm |
1662 | killagreg | 283 | else BuffLen = 1; // read Current only |
284 | } |
||
285 | if(BuffLen == 1) |
||
286 | { |
||
287 | I2C_ReceiveLastByte(); // read last byte |
||
288 | } |
||
1638 | holgerb | 289 | else |
1648 | killagreg | 290 | { |
1662 | killagreg | 291 | I2C_ReceiveByte(); // read next byte |
1642 | killagreg | 292 | } |
1648 | killagreg | 293 | } |
294 | MissingMotor = missing_motor; |
||
295 | missing_motor = 0; |
||
296 | break; |
||
1662 | killagreg | 297 | case 6: // receive bytes |
298 | *pBuff = TWDR; |
||
2443 | holgerb | 299 | pBuff++; // set Pointer to next element : Motor[].Current,Motor[].Temperature |
1662 | killagreg | 300 | BuffLen--; |
301 | if(BuffLen>1) |
||
302 | { |
||
303 | I2C_ReceiveByte(); // read next byte |
||
304 | } |
||
305 | else if (BuffLen == 1) |
||
306 | { |
||
307 | I2C_ReceiveLastByte(); // read last byte |
||
308 | } |
||
2443 | holgerb | 309 | else // nothing left -> ready |
1662 | killagreg | 310 | { |
311 | if(BLFlags & BLFLAG_READ_VERSION) |
||
1648 | killagreg | 312 | { |
2380 | holgerb | 313 | if(!(FC_StatusFlags & FC_STATUS_MOTOR_RUN)) |
314 | { |
||
315 | if((Motor[motor_read].MaxPWM & 252) == 248) Motor[motor_read].Version |= MOTOR_STATE_NEW_PROTOCOL_MASK; |
||
316 | else Motor[motor_read].Version = 0; |
||
2386 | holgerb | 317 | if(Motor[motor_read].MaxPWM == 248) Motor[motor_read].Version |= (MOTOR_STATE_FAST_MODE | MOTOR_STATE_BL30); |
318 | else |
||
319 | if(Motor[motor_read].MaxPWM == 249) Motor[motor_read].Version |= MOTOR_STATE_BL30; |
||
2380 | holgerb | 320 | } |
1648 | killagreg | 321 | } |
2443 | holgerb | 322 | |
323 | if(FC_StatusFlags & FC_STATUS_FLY) |
||
324 | { |
||
325 | // Starting -> 40 |
||
326 | // I2C-Setpoint is zero -> 250 |
||
327 | // 255 -> Running and no Redundancy |
||
328 | // 254 -> Running and active Redundancy |
||
329 | if(Motor[motor_read].MaxPWM < 254) |
||
330 | { |
||
331 | Motor[motor_read].NotReadyCnt++; |
||
332 | #if (defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)) |
||
333 | SpeakHoTT = SPEAK_ERR_MOTOR; |
||
334 | #endif |
||
335 | } |
||
336 | } |
||
1662 | killagreg | 337 | if(++motor_read >= MAX_MOTORS) |
1648 | killagreg | 338 | { |
1662 | killagreg | 339 | motor_read = 0; // restart from beginning |
340 | BLConfig_ReadMask = 0; // reset read configuration bitmask |
||
341 | if(++motor_read_temperature >= MAX_MOTORS) |
||
1648 | killagreg | 342 | { |
1662 | killagreg | 343 | motor_read_temperature = 0; |
344 | BLFlags &= ~BLFLAG_READ_VERSION; |
||
1648 | killagreg | 345 | } |
346 | } |
||
1662 | killagreg | 347 | I2C_Stop(TWI_STATE_MOTOR_TX); |
348 | BLFlags |= BLFLAG_TX_COMPLETE; |
||
1744 | holgerb | 349 | I2C_TransferActive = 0; |
1662 | killagreg | 350 | return; |
351 | } |
||
352 | twi_state = 6; // if there are some bytes left |
||
353 | break; |
||
2426 | holgerb | 354 | /* |
1662 | killagreg | 355 | // writing Gyro-Offsets |
356 | case 18: |
||
357 | I2C_WriteByte(0x98); // Address the DAC |
||
358 | break; |
||
359 | |||
360 | case 19: |
||
361 | I2C_WriteByte(0x10 + (dac_channel * 2)); // Select DAC Channel (0x10 = A, 0x12 = B, 0x14 = C) |
||
362 | break; |
||
363 | |||
364 | case 20: |
||
365 | switch(dac_channel) |
||
366 | { |
||
367 | case 0: |
||
368 | I2C_WriteByte(AnalogOffsetNick); // 1st byte for Channel A |
||
369 | break; |
||
370 | case 1: |
||
371 | I2C_WriteByte(AnalogOffsetRoll); // 1st byte for Channel B |
||
372 | break; |
||
373 | case 2: |
||
374 | I2C_WriteByte(AnalogOffsetGier); // 1st byte for Channel C |
||
375 | break; |
||
376 | } |
||
377 | break; |
||
378 | |||
379 | case 21: |
||
380 | I2C_WriteByte(0x80); // 2nd byte for all channels is 0x80 |
||
381 | break; |
||
382 | |||
383 | case 22: |
||
384 | I2C_Stop(TWI_STATE_MOTOR_TX); |
||
1744 | holgerb | 385 | I2C_TransferActive = 0; |
1662 | killagreg | 386 | I2CTimeout = 10; |
1665 | killagreg | 387 | // repeat case 18...22 until all DAC Channels are updated |
1662 | killagreg | 388 | if(dac_channel < 2) |
389 | { |
||
390 | dac_channel ++; // jump to next channel |
||
391 | I2C_Start(TWI_STATE_GYRO_OFFSET_TX); // start transmission for next channel |
||
392 | } |
||
393 | else |
||
1665 | killagreg | 394 | { |
1662 | killagreg | 395 | dac_channel = 0; // reset dac channel counter |
1665 | killagreg | 396 | BLFlags |= BLFLAG_TX_COMPLETE; |
1662 | killagreg | 397 | } |
398 | break; |
||
2426 | holgerb | 399 | */ |
1662 | killagreg | 400 | default: |
401 | I2C_Stop(TWI_STATE_MOTOR_TX); |
||
402 | BLFlags |= BLFLAG_TX_COMPLETE; |
||
403 | I2CTimeout = 10; |
||
404 | motor_write = 0; |
||
405 | motor_read = 0; |
||
1744 | holgerb | 406 | I2C_TransferActive = 0; |
1662 | killagreg | 407 | break; |
1648 | killagreg | 408 | } |
1662 | killagreg | 409 | |
1638 | holgerb | 410 | } |
1639 | holgerb | 411 | |
1662 | killagreg | 412 | |
413 | uint8_t I2C_WriteBLConfig(uint8_t motor) |
||
1648 | killagreg | 414 | { |
2466 | holgerb | 415 | uint8_t i, packets; |
1683 | killagreg | 416 | uint16_t timer; |
1665 | killagreg | 417 | if(MotorenEin || PC_MotortestActive) return(BLCONFIG_ERR_MOTOR_RUNNING); // not when motors are running! |
418 | if(motor > MAX_MOTORS) return (BLCONFIG_ERR_MOTOR_NOT_EXIST); // motor does not exist! |
||
1662 | killagreg | 419 | if(motor) |
420 | { |
||
1665 | killagreg | 421 | if(!(Motor[motor-1].State & MOTOR_STATE_PRESENT_MASK)) return(BLCONFIG_ERR_MOTOR_NOT_EXIST); // motor does not exist! |
422 | if(!(Motor[motor-1].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)) return(BLCONFIG_ERR_HW_NOT_COMPATIBLE); // not a new BL! |
||
1662 | killagreg | 423 | } |
424 | // check BL configuration to send |
||
2370 | holgerb | 425 | if((BLConfig.Revision & 0x0B) != BLCONFIG_REVISION) return (BLCONFIG_ERR_SW_NOT_COMPATIBLE); // bad revison |
1662 | killagreg | 426 | i = RAM_Checksum((uint8_t*)&BLConfig, sizeof(BLConfig_t) - 1); |
1665 | killagreg | 427 | if(i != BLConfig.crc) return(BLCONFIG_ERR_CHECKSUM); // bad checksum |
1662 | killagreg | 428 | |
2466 | holgerb | 429 | packets = Max_I2C_Packets; |
430 | Max_I2C_Packets = 12; |
||
431 | I2CTimeout = 100; |
||
432 | |||
433 | timer = SetDelay(100); |
||
1683 | killagreg | 434 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
1662 | killagreg | 435 | |
436 | // prepare the bitmask |
||
2466 | holgerb | 437 | if(!motor) BLConfig_WriteMask = 0x0FFF; // 0 means all -> all motors at once with the same configuration |
438 | else BLConfig_WriteMask = 0x0001<<(motor-1); //only one specific motor |
||
1665 | killagreg | 439 | |
1662 | killagreg | 440 | motor_write = 0; |
2466 | holgerb | 441 | motor_read = 0; |
1648 | killagreg | 442 | // needs at least MAX_MOTORS loops of 2 ms (12*2ms = 24ms) |
2466 | holgerb | 443 | timer = SetDelay(1000); |
1648 | killagreg | 444 | do |
445 | { |
||
1662 | killagreg | 446 | I2C_Start(TWI_STATE_MOTOR_TX); // start an i2c transmission |
1683 | killagreg | 447 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
2466 | holgerb | 448 | } while(BLConfig_WriteMask && !CheckDelay(timer)); // repeat until the BL config has been sent |
449 | Max_I2C_Packets = packets; |
||
1683 | killagreg | 450 | if(BLConfig_WriteMask) return(BLCONFIG_ERR_MOTOR_NOT_EXIST); |
1665 | killagreg | 451 | return(BLCONFIG_SUCCESS); |
1648 | killagreg | 452 | } |
453 | |||
1662 | killagreg | 454 | uint8_t I2C_ReadBLConfig(uint8_t motor) |
455 | { |
||
456 | uint8_t i; |
||
1683 | killagreg | 457 | uint16_t timer; |
1648 | killagreg | 458 | |
1665 | killagreg | 459 | if(MotorenEin || PC_MotortestActive) return(BLCONFIG_ERR_MOTOR_RUNNING); // not when motors are running! |
1673 | killagreg | 460 | if(motor > MAX_MOTORS) return (BLCONFIG_ERR_MOTOR_NOT_EXIST); // motor does not exist! |
461 | if(motor == 0) return (BLCONFIG_ERR_READ_NOT_POSSIBLE); |
||
1665 | killagreg | 462 | if(!(Motor[motor-1].State & MOTOR_STATE_PRESENT_MASK)) return(BLCONFIG_ERR_MOTOR_NOT_EXIST); // motor does not exist! |
463 | if(!(Motor[motor-1].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)) return(BLCONFIG_ERR_HW_NOT_COMPATIBLE); // not a new BL! |
||
464 | |||
2466 | holgerb | 465 | timer = SetDelay(1000); |
1683 | killagreg | 466 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
1648 | killagreg | 467 | |
1662 | killagreg | 468 | // prepare the bitmask |
469 | BLConfig_ReadMask = 0x0001<<(motor-1); |
||
1665 | killagreg | 470 | |
2466 | holgerb | 471 | motor_write = 0; |
472 | motor_read = 0; |
||
1662 | killagreg | 473 | BLConfig.Revision = 0; // bad revision |
474 | BLConfig.crc = 0; // bad checksum |
||
475 | // needs at least MAX_MOTORS loops of 2 ms (12*2ms = 24ms) |
||
476 | do |
||
477 | { |
||
478 | I2C_Start(TWI_STATE_MOTOR_TX); // start an i2c transmission |
||
1683 | killagreg | 479 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
2466 | holgerb | 480 | } while(BLConfig_ReadMask && !CheckDelay(timer)); // repeat until the BL config has been received from all motors |
1662 | killagreg | 481 | // validate result |
2370 | holgerb | 482 | if((BLConfig.Revision & 0x0B) != BLCONFIG_REVISION) return (BLCONFIG_ERR_SW_NOT_COMPATIBLE); // bad revison |
1662 | killagreg | 483 | i = RAM_Checksum((uint8_t*)&BLConfig, sizeof(BLConfig_t) - 1); |
1665 | killagreg | 484 | if(i != BLConfig.crc) return(BLCONFIG_ERR_CHECKSUM); // bad checksum |
485 | return(BLCONFIG_SUCCESS); |
||
1662 | killagreg | 486 | } |
1665 | killagreg | 487 |