Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2751 | - | 1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + www.MikroKopter.com |
||
3 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
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. |
||
10 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
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. |
||
28 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
29 | // + Software LICENSING TERMS |
||
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
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. |
||
52 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
53 | |||
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" |
||
61 | #include "uart.h" |
||
62 | #include "timer0.h" |
||
63 | #include "main.h" |
||
64 | |||
65 | volatile uint8_t twi_state = TWI_STATE_MOTOR_TX, ReadBlSize = 9; |
||
66 | volatile uint8_t dac_channel = 0; |
||
67 | volatile uint8_t motor_write = 0; |
||
68 | volatile uint8_t motor_read = 0; |
||
69 | volatile uint8_t I2C_TransferActive = 0; |
||
70 | uint8_t Max_I2C_Packets = 12; |
||
71 | |||
72 | volatile uint16_t I2CTimeout = 100; |
||
73 | |||
74 | uint8_t MissingMotor = 0; |
||
75 | |||
76 | volatile uint8_t BLFlags = 0; |
||
77 | |||
78 | MotorData_t Motor[MAX_MOTORS]; |
||
79 | RedundantBl_t RedundantMotor[MAX_MOTORS]; |
||
80 | |||
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; |
||
87 | |||
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);} |
||
91 | |||
92 | #define SCL_CLOCK 200000L |
||
93 | #define I2C_TIMEOUT 30000 |
||
94 | #define TWI_BASE_ADDRESS 0x52 |
||
95 | |||
96 | /**************************************************/ |
||
97 | /* Initialize I2C (TWI) */ |
||
98 | /**************************************************/ |
||
99 | |||
100 | void I2C_Init(char clear) |
||
101 | { |
||
102 | uint8_t i; |
||
103 | uint8_t sreg = SREG; |
||
104 | cli(); |
||
105 | |||
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); |
||
112 | |||
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 | |||
124 | if(clear) for(i=0; i < MAX_MOTORS; i++) |
||
125 | { |
||
126 | Motor[i].Version = 0; |
||
127 | Motor[i].SetPoint = 0; |
||
128 | Motor[i].SetPointLowerBits = 0; |
||
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; |
||
135 | Motor[i].RPM = 0; |
||
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; |
||
141 | } |
||
142 | sei(); |
||
143 | SREG = sreg; |
||
144 | } |
||
145 | |||
146 | void I2C_Reset(void) |
||
147 | { |
||
148 | // stop i2c bus |
||
149 | I2C_Stop(TWI_STATE_MOTOR_TX); |
||
150 | TWCR = (1<<TWINT); // reset to original state incl. interrupt flag reset |
||
151 | TWAMR = 0; |
||
152 | TWAR = 0; |
||
153 | TWDR = 0; |
||
154 | TWSR = 0; |
||
155 | TWBR = 0; |
||
156 | I2C_TransferActive = 0; |
||
157 | I2C_Init(0); |
||
158 | I2C_WriteByte(0); |
||
159 | BLFlags |= BLFLAG_READ_VERSION; |
||
160 | } |
||
161 | |||
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; |
||
170 | static uint8_t max_packets = 0; |
||
171 | switch (twi_state++) |
||
172 | { |
||
173 | // Master Transmit |
||
174 | case 0: // TWI_STATE_MOTOR_TX |
||
175 | I2C_TransferActive = 1; |
||
176 | // skip motor if not used in mixer |
||
177 | while((Mixer.Motor[motor_write][MIX_GAS] <= 0) && (motor_write < MAX_MOTORS)) motor_write++; |
||
178 | motor_write %= MAX_MOTORS; |
||
179 | if(++max_packets > Max_I2C_Packets) // writing finished, read now |
||
180 | { |
||
181 | max_packets = 0; |
||
182 | BLConfig_WriteMask = 0; // reset configuration bitmask |
||
183 | //motor_write = 0; // reset motor write counter for next cycle |
||
184 | twi_state = TWI_STATE_MOTOR_RX; |
||
185 | I2C_WriteByte(TWI_BASE_ADDRESS + TW_READ + (motor_read<<1) ); // select slave address in rx mode |
||
186 | } |
||
187 | else I2C_WriteByte(TWI_BASE_ADDRESS + TW_WRITE + (motor_write<<1) ); // select slave address in tx mode |
||
188 | break; |
||
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)) |
||
193 | { |
||
194 | twi_state = 4; //jump over sending more data |
||
195 | } |
||
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 |
||
200 | } |
||
201 | break; |
||
202 | case 2: // lower bits of setpoint (higher resolution) |
||
203 | if ((0x0001<<motor_write) & BLConfig_ReadMask) |
||
204 | { |
||
205 | Motor[motor_write].ReadMode = BL_READMODE_CONFIG; // configuration request |
||
206 | } |
||
207 | else |
||
208 | { |
||
209 | Motor[motor_write].ReadMode = BL_READMODE_STATUS; // normal status request |
||
210 | } |
||
211 | // send read mode and the lower bits of setpoint |
||
212 | I2C_WriteByte((Motor[motor_write].ReadMode<<3)|(Motor[motor_write].SetPointLowerBits & 0x07)); |
||
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); |
||
218 | } |
||
219 | else |
||
220 | { // jump to end of transmission for that motor |
||
221 | twi_state = 4; |
||
222 | } |
||
223 | break; |
||
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 |
||
231 | { |
||
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 |
||
234 | } |
||
235 | I2C_Stop(TWI_STATE_MOTOR_TX); |
||
236 | I2CTimeout = 10; |
||
237 | motor_write++; // next motor |
||
238 | I2C_Start(TWI_STATE_MOTOR_TX); // Repeated start -> switch slave or switch Master Transmit -> Master Receive |
||
239 | break; |
||
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) |
||
250 | { |
||
251 | motor_read_temperature = 0; |
||
252 | BLFlags &= ~BLFLAG_READ_VERSION; |
||
253 | } |
||
254 | } |
||
255 | BLFlags |= BLFLAG_TX_COMPLETE; |
||
256 | I2C_Stop(TWI_STATE_MOTOR_TX); |
||
257 | I2C_TransferActive = 0; |
||
258 | } |
||
259 | else |
||
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) |
||
263 | { |
||
264 | // new BL found |
||
265 | switch(Motor[motor_read].ReadMode) |
||
266 | { |
||
267 | case BL_READMODE_CONFIG: |
||
268 | pBuff = (uint8_t*)&BLConfig; |
||
269 | BuffLen = sizeof(BLConfig_t); |
||
270 | Motor[motor_read].ReadMode = BL_READMODE_STATUS; // only once |
||
271 | break; |
||
272 | case BL_READMODE_STATUS: |
||
273 | pBuff = (uint8_t*)&(Motor[motor_read].Current); |
||
274 | if(motor_read == motor_read_temperature) BuffLen = ReadBlSize; // read Current, MaxPwm & Temp (is 3 or 9) |
||
275 | else BuffLen = 1;// read Current only |
||
276 | break; |
||
277 | } |
||
278 | } |
||
279 | else // old BL version |
||
280 | { |
||
281 | pBuff = (uint8_t*)&(Motor[motor_read].Current); |
||
282 | if((BLFlags & BLFLAG_READ_VERSION) || (motor_read == motor_read_temperature)) BuffLen = 2; // Current & MaxPwm |
||
283 | else BuffLen = 1; // read Current only |
||
284 | } |
||
285 | if(BuffLen == 1) |
||
286 | { |
||
287 | I2C_ReceiveLastByte(); // read last byte |
||
288 | } |
||
289 | else |
||
290 | { |
||
291 | I2C_ReceiveByte(); // read next byte |
||
292 | } |
||
293 | } |
||
294 | MissingMotor = missing_motor; |
||
295 | missing_motor = 0; |
||
296 | break; |
||
297 | case 6: // receive bytes |
||
298 | *pBuff = TWDR; |
||
299 | pBuff++; // set Pointer to next element : Motor[].Current,Motor[].Temperature |
||
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 | } |
||
309 | else // nothing left -> ready |
||
310 | { |
||
311 | if(BLFlags & BLFLAG_READ_VERSION) |
||
312 | { |
||
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; |
||
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; |
||
320 | } |
||
321 | } |
||
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 | } |
||
337 | if(++motor_read >= MAX_MOTORS) |
||
338 | { |
||
339 | motor_read = 0; // restart from beginning |
||
340 | BLConfig_ReadMask = 0; // reset read configuration bitmask |
||
341 | if(++motor_read_temperature >= MAX_MOTORS) |
||
342 | { |
||
343 | motor_read_temperature = 0; |
||
344 | BLFlags &= ~BLFLAG_READ_VERSION; |
||
345 | } |
||
346 | } |
||
347 | I2C_Stop(TWI_STATE_MOTOR_TX); |
||
348 | BLFlags |= BLFLAG_TX_COMPLETE; |
||
349 | I2C_TransferActive = 0; |
||
350 | return; |
||
351 | } |
||
352 | twi_state = 6; // if there are some bytes left |
||
353 | break; |
||
354 | /* |
||
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); |
||
385 | I2C_TransferActive = 0; |
||
386 | I2CTimeout = 10; |
||
387 | // repeat case 18...22 until all DAC Channels are updated |
||
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 |
||
394 | { |
||
395 | dac_channel = 0; // reset dac channel counter |
||
396 | BLFlags |= BLFLAG_TX_COMPLETE; |
||
397 | } |
||
398 | break; |
||
399 | */ |
||
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; |
||
406 | I2C_TransferActive = 0; |
||
407 | break; |
||
408 | } |
||
409 | |||
410 | } |
||
411 | |||
412 | |||
413 | uint8_t I2C_WriteBLConfig(uint8_t motor) |
||
414 | { |
||
415 | uint8_t i, packets; |
||
416 | uint16_t timer; |
||
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! |
||
419 | if(motor) |
||
420 | { |
||
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! |
||
423 | } |
||
424 | // check BL configuration to send |
||
425 | if((BLConfig.Revision & 0x0B) != BLCONFIG_REVISION) return (BLCONFIG_ERR_SW_NOT_COMPATIBLE); // bad revison |
||
426 | i = RAM_Checksum((uint8_t*)&BLConfig, sizeof(BLConfig_t) - 1); |
||
427 | if(i != BLConfig.crc) return(BLCONFIG_ERR_CHECKSUM); // bad checksum |
||
428 | |||
429 | packets = Max_I2C_Packets; |
||
430 | Max_I2C_Packets = MAX_MOTORS; |
||
431 | I2CTimeout = 100; |
||
432 | |||
433 | timer = SetDelay(100); |
||
434 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
||
435 | |||
436 | // prepare the bitmask |
||
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 |
||
439 | |||
440 | motor_write = 0; |
||
441 | motor_read = 0; |
||
442 | // needs at least MAX_MOTORS loops of 2 ms (12*2ms = 24ms) |
||
443 | timer = SetDelay(1000); |
||
444 | do |
||
445 | { |
||
446 | I2C_Start(TWI_STATE_MOTOR_TX); // start an i2c transmission |
||
447 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
||
448 | } while(BLConfig_WriteMask && !CheckDelay(timer)); // repeat until the BL config has been sent |
||
449 | Max_I2C_Packets = packets; |
||
450 | if(BLConfig_WriteMask) return(BLCONFIG_ERR_MOTOR_NOT_EXIST); |
||
451 | return(BLCONFIG_SUCCESS); |
||
452 | } |
||
453 | |||
454 | uint8_t I2C_ReadBLConfig(uint8_t motor) |
||
455 | { |
||
456 | uint8_t i; |
||
457 | uint16_t timer; |
||
458 | |||
459 | if(MotorenEin || PC_MotortestActive) return(BLCONFIG_ERR_MOTOR_RUNNING); // not when motors are running! |
||
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); |
||
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 | |||
465 | timer = SetDelay(1000); |
||
466 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
||
467 | |||
468 | // prepare the bitmask |
||
469 | BLConfig_ReadMask = 0x0001<<(motor-1); |
||
470 | |||
471 | motor_write = 0; |
||
472 | motor_read = 0; |
||
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 |
||
479 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
||
480 | } while(BLConfig_ReadMask && !CheckDelay(timer)); // repeat until the BL config has been received from all motors |
||
481 | // validate result |
||
482 | if((BLConfig.Revision & 0x0B) != BLCONFIG_REVISION) return (BLCONFIG_ERR_SW_NOT_COMPATIBLE); // bad revison |
||
483 | i = RAM_Checksum((uint8_t*)&BLConfig, sizeof(BLConfig_t) - 1); |
||
484 | if(i != BLConfig.crc) return(BLCONFIG_ERR_CHECKSUM); // bad checksum |
||
485 | return(BLCONFIG_SUCCESS); |
||
486 | } |
||
487 |