Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2136 | - | 1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + Copyright (c) Holger Buss, Ingo Busker |
||
3 | // + Nur f?r den privaten Gebrauch |
||
4 | // + www.MikroKopter.com |
||
5 | // + porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed |
||
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
7 | // + Es gilt f?r das gesamte Projekt (Hardware, Software, Bin?rfiles, Sourcecode und Dokumentation), |
||
8 | // + dass eine Nutzung (auch auszugsweise) nur f?r den privaten (nicht-kommerziellen) Gebrauch zul?ssig ist. |
||
9 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
||
10 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
||
11 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Best?ckung und Verkauf von Platinen oder Baus?tzen, |
||
12 | // + Verkauf von Luftbildaufnahmen, usw. |
||
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
14 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder ver?ffentlicht, |
||
15 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright m?ssen dann beiliegen |
||
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
17 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
||
18 | // + auf anderen Webseiten oder sonstigen Medien ver?ffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
||
19 | // + eindeutig als Ursprung verlinkt werden |
||
20 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
21 | // + Keine Gew?hr auf Fehlerfreiheit, Vollst?ndigkeit oder Funktion |
||
22 | // + Benutzung auf eigene Gefahr |
||
23 | // + Wir ?bernehmen keinerlei Haftung f?r direkte oder indirekte Personen- oder Sachsch?den |
||
24 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
25 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
||
26 | // + mit unserer Zustimmung zul?ssig |
||
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
28 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
||
29 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
30 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
||
31 | // + this list of conditions and the following disclaimer. |
||
32 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
||
33 | // + from this software without specific prior written permission. |
||
34 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
||
35 | // + for non-commercial use (directly or indirectly) |
||
36 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
||
37 | // + with our written permission |
||
38 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
||
39 | // + clearly linked as origin |
||
40 | // + * porting to systems other than hardware from www.mikrokopter.de is not allowed |
||
41 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||
42 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
43 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
44 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
45 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
46 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
47 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
48 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||
49 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||
50 | // + POSSIBILITY OF SUCH DAMAGE. |
||
51 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
52 | |||
53 | //############################################################################ |
||
54 | //# HISTORY twimaster.c |
||
55 | //# |
||
56 | //# 13.05.2013 Cebra |
||
57 | //# - chg: #define USE_I2CMOTORTEST, I2C Funktionen schaltbar |
||
58 | //# |
||
59 | //############################################################################ |
||
60 | |||
61 | #include "../cpu.h" |
||
62 | #include <avr/io.h> |
||
63 | #include <avr/interrupt.h> |
||
64 | #include <util/twi.h> |
||
65 | #include <util/delay.h> |
||
66 | #include "../eeprom/eeprom.h" |
||
67 | #include "../motortest/twimaster.h" |
||
68 | #include "../timer/timer.h" |
||
69 | |||
70 | volatile uint8_t twi_state = TWI_STATE_MOTOR_TX; |
||
71 | volatile uint8_t dac_channel = 0; |
||
72 | volatile uint8_t motor_write = 0; |
||
73 | volatile uint8_t motor_read = 0; |
||
74 | volatile uint8_t I2C_TransferActive = 0; |
||
75 | |||
76 | volatile uint16_t I2CTimeout = 100; |
||
77 | |||
78 | uint8_t MissingMotor = 0; |
||
79 | uint8_t RequiredMotors = 1; |
||
80 | char MotorenEin = 0; |
||
81 | |||
82 | volatile uint8_t BLFlags = 0; |
||
83 | |||
84 | MotorData_t Motor[MAX_MOTORS]; |
||
85 | |||
86 | // bit mask for witch BL the configuration should be sent |
||
87 | volatile uint16_t BLConfig_WriteMask = 0; |
||
88 | // bit mask for witch BL the configuration should be read |
||
89 | volatile uint16_t BLConfig_ReadMask = 0; |
||
90 | // buffer for BL Configuration |
||
91 | BLConfig_t BLConfig; |
||
92 | |||
93 | #define I2C_WriteByte(byte) {TWDR = byte; TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);} |
||
94 | #define I2C_ReceiveByte() {TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);} |
||
95 | #define I2C_ReceiveLastByte() {TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);} |
||
96 | |||
97 | #define SCL_CLOCK 200000L |
||
98 | #define I2C_TIMEOUT 30000 |
||
99 | #define TWI_BASE_ADDRESS 0x52 |
||
100 | |||
101 | |||
102 | |||
103 | |||
104 | uint8_t RAM_Checksum(uint8_t* pBuffer, uint16_t len) |
||
105 | { |
||
106 | uint8_t crc = 0xAA; |
||
107 | uint16_t i; |
||
108 | |||
109 | for(i=0; i<len; i++) |
||
110 | { |
||
111 | crc += pBuffer[i]; |
||
112 | } |
||
113 | return crc; |
||
114 | } |
||
115 | |||
116 | |||
117 | |||
118 | //-------------------------------------------------------------- |
||
119 | // Initialize I2C (TWI) |
||
120 | // |
||
121 | void I2C_Init(char clear) |
||
122 | { |
||
123 | |||
124 | #ifdef USE_I2CMOTORTEST |
||
125 | uint8_t i; |
||
126 | uint8_t sreg = SREG; |
||
127 | cli(); |
||
128 | |||
129 | // SDA is INPUT |
||
130 | DDRC &= ~(1<<DDC1); |
||
131 | // SCL is output |
||
132 | DDRC |= (1<<DDC0); |
||
133 | // pull up SDA |
||
134 | //PORTC |= (1<<PORTC0)|(1<<PORTC1); |
||
135 | |||
136 | // TWI Status Register |
||
137 | // prescaler 1 (TWPS1 = 0, TWPS0 = 0) |
||
138 | TWSR &= ~((1<<TWPS1)|(1<<TWPS0)); |
||
139 | |||
140 | // set TWI Bit Rate Register |
||
141 | TWBR = ((F_CPU/SCL_CLOCK)-16)/2; |
||
142 | |||
143 | twi_state = TWI_STATE_MOTOR_TX; |
||
144 | motor_write = 0; |
||
145 | motor_read = 0; |
||
146 | |||
147 | if(clear) for(i=0; i < MAX_MOTORS; i++) |
||
148 | { |
||
149 | Motor[i].Version = 0; |
||
150 | Motor[i].SetPoint = 0; |
||
151 | Motor[i].SetPointLowerBits = 0; |
||
152 | Motor[i].State = 0; |
||
153 | Motor[i].ReadMode = BL_READMODE_STATUS; |
||
154 | Motor[i].Current = 0; |
||
155 | Motor[i].MaxPWM = 0; |
||
156 | Motor[i].Temperature = 0; |
||
157 | } |
||
158 | sei(); |
||
159 | SREG = sreg; |
||
160 | #endif |
||
161 | } |
||
162 | |||
163 | |||
164 | #ifdef USE_I2CMOTORTEST |
||
165 | //-------------------------------------------------------------- |
||
166 | void I2C_Reset(void) |
||
167 | { |
||
168 | // stop i2c bus |
||
169 | I2C_Stop(TWI_STATE_MOTOR_TX); |
||
170 | TWCR = (1<<TWINT); // reset to original state incl. interrupt flag reset |
||
171 | TWAMR = 0; |
||
172 | TWAR = 0; |
||
173 | TWDR = 0; |
||
174 | TWSR = 0; |
||
175 | TWBR = 0; |
||
176 | I2C_TransferActive = 0; |
||
177 | I2C_Init(0); |
||
178 | I2C_WriteByte(0); |
||
179 | BLFlags |= BLFLAG_READ_VERSION; |
||
180 | } |
||
181 | #endif |
||
182 | |||
183 | #ifdef USE_I2CMOTORTEST |
||
184 | //-------------------------------------------------------------- |
||
185 | // I2C ISR |
||
186 | // |
||
187 | ISR (TWI_vect) |
||
188 | { |
||
189 | static uint8_t missing_motor = 0, motor_read_temperature = 0; |
||
190 | static uint8_t *pBuff = 0; |
||
191 | static uint8_t BuffLen = 0; |
||
192 | |||
193 | switch (twi_state++) |
||
194 | { |
||
195 | // Master Transmit |
||
196 | |||
197 | case 0: // TWI_STATE_MOTOR_TX |
||
198 | I2C_TransferActive = 1; |
||
199 | // skip motor if not used in mixer |
||
200 | // while((Mixer.Motor[motor_write][MIX_GAS] <= 0) && (motor_write < MAX_MOTORS)) motor_write++; |
||
201 | if(motor_write >= MAX_MOTORS) // writing finished, read now |
||
202 | { |
||
203 | BLConfig_WriteMask = 0; // reset configuration bitmask |
||
204 | motor_write = 0; // reset motor write counter for next cycle |
||
205 | twi_state = TWI_STATE_MOTOR_RX; |
||
206 | I2C_WriteByte(TWI_BASE_ADDRESS + TW_READ + (motor_read<<1) ); // select slave address in rx mode |
||
207 | } |
||
208 | else I2C_WriteByte(TWI_BASE_ADDRESS + TW_WRITE + (motor_write<<1) ); // select slave address in tx mode |
||
209 | break; |
||
210 | |||
211 | case 1: // Send Data to Slave |
||
212 | I2C_WriteByte(Motor[motor_write].SetPoint); // transmit setpoint |
||
213 | // if old version has been detected |
||
214 | if(!(Motor[motor_write].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)) |
||
215 | { |
||
216 | twi_state = 4; //jump over sending more data |
||
217 | } |
||
218 | // the new version has been detected |
||
219 | else if(!( (Motor[motor_write].SetPointLowerBits && (RequiredMotors < 7)) || BLConfig_WriteMask || BLConfig_ReadMask ) ) |
||
220 | { // or LowerBits are zero and no BlConfig should be sent (saves round trip time) |
||
221 | twi_state = 4; //jump over sending more data |
||
222 | } |
||
223 | break; |
||
224 | |||
225 | case 2: // lower bits of setpoint (higher resolution) |
||
226 | if ((0x0001<<motor_write) & BLConfig_ReadMask) |
||
227 | { |
||
228 | Motor[motor_write].ReadMode = BL_READMODE_CONFIG; // configuration request |
||
229 | } |
||
230 | else |
||
231 | { |
||
232 | Motor[motor_write].ReadMode = BL_READMODE_STATUS; // normal status request |
||
233 | } |
||
234 | // send read mode and the lower bits of setpoint |
||
235 | I2C_WriteByte((Motor[motor_write].ReadMode<<3)|(Motor[motor_write].SetPointLowerBits & 0x07)); |
||
236 | // configuration tranmission request? |
||
237 | if((0x0001<<motor_write) & BLConfig_WriteMask) |
||
238 | { // redirect tx pointer to configuration data |
||
239 | pBuff = (uint8_t*)&BLConfig; // select config for motor |
||
240 | BuffLen = sizeof(BLConfig_t); |
||
241 | } |
||
242 | else |
||
243 | { // jump to end of transmission for that motor |
||
244 | twi_state = 4; |
||
245 | } |
||
246 | break; |
||
247 | |||
248 | case 3: // send configuration |
||
249 | I2C_WriteByte(*pBuff); |
||
250 | pBuff++; |
||
251 | if(--BuffLen > 0) |
||
252 | twi_state = 3; // if there are some bytes left |
||
253 | break; |
||
254 | |||
255 | case 4: // repeat case 0-4 for all motors |
||
256 | if(TWSR == TW_MT_DATA_NACK) // Data transmitted, NACK received |
||
257 | { |
||
258 | if(!missing_motor) |
||
259 | missing_motor = motor_write + 1; |
||
260 | |||
261 | if((Motor[motor_write].State & MOTOR_STATE_ERROR_MASK) < MOTOR_STATE_ERROR_MASK) |
||
262 | Motor[motor_write].State++; // increment error counter and handle overflow |
||
263 | } |
||
264 | I2C_Stop(TWI_STATE_MOTOR_TX); |
||
265 | I2CTimeout = 10; |
||
266 | motor_write++; // next motor |
||
267 | I2C_Start(TWI_STATE_MOTOR_TX); // Repeated start -> switch slave or switch Master Transmit -> Master Receive |
||
268 | break; |
||
269 | |||
270 | // Master Receive Data |
||
271 | case 5: // TWI_STATE_MOTOR_RX |
||
272 | if(TWSR != TW_MR_SLA_ACK) // SLA+R transmitted but no ACK received |
||
273 | { // no response from the addressed slave received |
||
274 | Motor[motor_read].State &= ~MOTOR_STATE_PRESENT_MASK; // clear present bit |
||
275 | if(++motor_read >= MAX_MOTORS) |
||
276 | { // all motors read |
||
277 | motor_read = 0; // restart from beginning |
||
278 | BLConfig_ReadMask = 0; // reset read configuration bitmask |
||
279 | if(++motor_read_temperature >= MAX_MOTORS) |
||
280 | { |
||
281 | motor_read_temperature = 0; |
||
282 | BLFlags &= ~BLFLAG_READ_VERSION; |
||
283 | } |
||
284 | } |
||
285 | BLFlags |= BLFLAG_TX_COMPLETE; |
||
286 | I2C_Stop(TWI_STATE_MOTOR_TX); |
||
287 | I2C_TransferActive = 0; |
||
288 | } |
||
289 | else |
||
290 | { // motor successfully addressed |
||
291 | Motor[motor_read].State |= MOTOR_STATE_PRESENT_MASK; // set present bit |
||
292 | if(Motor[motor_read].Version & MOTOR_STATE_NEW_PROTOCOL_MASK) |
||
293 | { |
||
294 | // new BL found |
||
295 | switch(Motor[motor_read].ReadMode) |
||
296 | { |
||
297 | case BL_READMODE_CONFIG: |
||
298 | pBuff = (uint8_t*)&BLConfig; |
||
299 | BuffLen = sizeof(BLConfig_t); |
||
300 | break; |
||
301 | |||
302 | case BL_READMODE_STATUS: |
||
303 | pBuff = (uint8_t*)&(Motor[motor_read].Current); |
||
304 | if(motor_read == motor_read_temperature) BuffLen = 3; // read Current, MaxPwm & Temp |
||
305 | else BuffLen = 1;// read Current only |
||
306 | break; |
||
307 | } |
||
308 | } |
||
309 | else // old BL version |
||
310 | { |
||
311 | pBuff = (uint8_t*)&(Motor[motor_read].Current); |
||
312 | if((BLFlags & BLFLAG_READ_VERSION) || (motor_read == motor_read_temperature)) BuffLen = 2; // Current & MaxPwm |
||
313 | else BuffLen = 1; // read Current only |
||
314 | } |
||
315 | if(BuffLen == 1) |
||
316 | { |
||
317 | I2C_ReceiveLastByte(); // read last byte |
||
318 | } |
||
319 | else |
||
320 | { |
||
321 | I2C_ReceiveByte(); // read next byte |
||
322 | } |
||
323 | } |
||
324 | MissingMotor = missing_motor; |
||
325 | missing_motor = 0; |
||
326 | break; |
||
327 | |||
328 | case 6: // receive bytes |
||
329 | *pBuff = TWDR; |
||
330 | pBuff++; |
||
331 | BuffLen--; |
||
332 | if(BuffLen>1) |
||
333 | { |
||
334 | I2C_ReceiveByte(); // read next byte |
||
335 | } |
||
336 | else if (BuffLen == 1) |
||
337 | { |
||
338 | I2C_ReceiveLastByte(); // read last byte |
||
339 | } |
||
340 | else // nothing left |
||
341 | { |
||
342 | if(BLFlags & BLFLAG_READ_VERSION) |
||
343 | { |
||
344 | // if(!(FC_StatusFlags & FC_STATUS_MOTOR_RUN) && (Motor[motor_read].MaxPWM == 250) ) Motor[motor_read].Version |= MOTOR_STATE_NEW_PROTOCOL_MASK; |
||
345 | if((Motor[motor_read].MaxPWM == 250) ) Motor[motor_read].Version |= MOTOR_STATE_NEW_PROTOCOL_MASK; |
||
346 | else Motor[motor_read].Version = 0; |
||
347 | } |
||
348 | if(++motor_read >= MAX_MOTORS) |
||
349 | { |
||
350 | motor_read = 0; // restart from beginning |
||
351 | BLConfig_ReadMask = 0; // reset read configuration bitmask |
||
352 | if(++motor_read_temperature >= MAX_MOTORS) |
||
353 | { |
||
354 | motor_read_temperature = 0; |
||
355 | BLFlags &= ~BLFLAG_READ_VERSION; |
||
356 | } |
||
357 | } |
||
358 | I2C_Stop(TWI_STATE_MOTOR_TX); |
||
359 | BLFlags |= BLFLAG_TX_COMPLETE; |
||
360 | I2C_TransferActive = 0; |
||
361 | return; |
||
362 | } |
||
363 | twi_state = 6; // if there are some bytes left |
||
364 | break; |
||
365 | |||
366 | case 21: |
||
367 | I2C_WriteByte(0x80); // 2nd byte for all channels is 0x80 |
||
368 | break; |
||
369 | |||
370 | case 22: |
||
371 | I2C_Stop(TWI_STATE_MOTOR_TX); |
||
372 | I2C_TransferActive = 0; |
||
373 | I2CTimeout = 10; |
||
374 | // repeat case 18...22 until all DAC Channels are updated |
||
375 | if(dac_channel < 2) |
||
376 | { |
||
377 | dac_channel ++; // jump to next channel |
||
378 | I2C_Start(TWI_STATE_GYRO_OFFSET_TX); // start transmission for next channel |
||
379 | } |
||
380 | else |
||
381 | { |
||
382 | dac_channel = 0; // reset dac channel counter |
||
383 | BLFlags |= BLFLAG_TX_COMPLETE; |
||
384 | } |
||
385 | break; |
||
386 | |||
387 | default: |
||
388 | I2C_Stop(TWI_STATE_MOTOR_TX); |
||
389 | BLFlags |= BLFLAG_TX_COMPLETE; |
||
390 | I2CTimeout = 10; |
||
391 | motor_write = 0; |
||
392 | motor_read = 0; |
||
393 | I2C_TransferActive = 0; |
||
394 | break; |
||
395 | } |
||
396 | |||
397 | } |
||
398 | |||
399 | |||
400 | //-------------------------------------------------------------- |
||
401 | uint8_t I2C_WriteBLConfig(uint8_t motor) |
||
402 | { |
||
403 | uint8_t i; |
||
404 | uint16_t timer; |
||
405 | |||
406 | // if(MotorenEin || PC_MotortestActive) |
||
407 | // return(BLCONFIG_ERR_MOTOR_RUNNING); // not when motors are running! |
||
408 | |||
409 | if(MotorenEin) |
||
410 | return(BLCONFIG_ERR_MOTOR_RUNNING); // not when motors are running! |
||
411 | |||
412 | if(motor > MAX_MOTORS) |
||
413 | return (BLCONFIG_ERR_MOTOR_NOT_EXIST); // motor does not exist! |
||
414 | |||
415 | if(motor) |
||
416 | { |
||
417 | if(!(Motor[motor-1].State & MOTOR_STATE_PRESENT_MASK)) |
||
418 | return(BLCONFIG_ERR_MOTOR_NOT_EXIST); // motor does not exist! |
||
419 | |||
420 | if(!(Motor[motor-1].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)) |
||
421 | return(BLCONFIG_ERR_HW_NOT_COMPATIBLE); // not a new BL! |
||
422 | } |
||
423 | |||
424 | // check BL configuration to send |
||
425 | if(BLConfig.Revision != BLCONFIG_REVISION) |
||
426 | return (BLCONFIG_ERR_SW_NOT_COMPATIBLE); // bad revison |
||
427 | |||
428 | i = RAM_Checksum((uint8_t*)&BLConfig, sizeof(BLConfig_t) - 1); |
||
429 | |||
430 | if(i != BLConfig.crc) |
||
431 | return(BLCONFIG_ERR_CHECKSUM); // bad checksum |
||
432 | |||
433 | timer = SetDelay(2000); |
||
434 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
||
435 | |||
436 | // prepare the bitmask |
||
437 | if(!motor) // 0 means all |
||
438 | { |
||
439 | BLConfig_WriteMask = 0xFF; // all motors at once with the same configuration |
||
440 | } |
||
441 | else //only one specific motor |
||
442 | { |
||
443 | BLConfig_WriteMask = 0x0001<<(motor-1); |
||
444 | } |
||
445 | for(i = 0; i < MAX_MOTORS; i++) |
||
446 | { |
||
447 | if((0x0001<<i) & BLConfig_WriteMask) |
||
448 | { |
||
449 | Motor[i].SetPoint = 0; |
||
450 | Motor[i].SetPointLowerBits = 0; |
||
451 | } |
||
452 | } |
||
453 | |||
454 | motor_write = 0; |
||
455 | // needs at least MAX_MOTORS loops of 2 ms (12*2ms = 24ms) |
||
456 | do |
||
457 | { |
||
458 | I2C_Start(TWI_STATE_MOTOR_TX); // start an i2c transmission |
||
459 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
||
460 | } |
||
461 | while(BLConfig_WriteMask && !CheckDelay(timer)); // repeat until the BL config has been sent |
||
462 | |||
463 | if(BLConfig_WriteMask) return(BLCONFIG_ERR_MOTOR_NOT_EXIST); |
||
464 | return(BLCONFIG_SUCCESS); |
||
465 | } |
||
466 | |||
467 | |||
468 | //-------------------------------------------------------------- |
||
469 | uint8_t I2C_ReadBLConfig(uint8_t motor) |
||
470 | { |
||
471 | uint8_t i; |
||
472 | uint16_t timer; |
||
473 | |||
474 | // if(MotorenEin || PC_MotortestActive) |
||
475 | return(BLCONFIG_ERR_MOTOR_RUNNING); // not when motors are running! |
||
476 | |||
477 | if(MotorenEin) |
||
478 | return(BLCONFIG_ERR_MOTOR_RUNNING); // not when motors are running! |
||
479 | |||
480 | if(motor > MAX_MOTORS) |
||
481 | return (BLCONFIG_ERR_MOTOR_NOT_EXIST); // motor does not exist! |
||
482 | |||
483 | if(motor == 0) |
||
484 | return (BLCONFIG_ERR_READ_NOT_POSSIBLE); |
||
485 | |||
486 | if(!(Motor[motor-1].State & MOTOR_STATE_PRESENT_MASK)) |
||
487 | return(BLCONFIG_ERR_MOTOR_NOT_EXIST); // motor does not exist! |
||
488 | |||
489 | if(!(Motor[motor-1].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)) |
||
490 | return(BLCONFIG_ERR_HW_NOT_COMPATIBLE); // not a new BL! |
||
491 | |||
492 | timer = SetDelay(2000); |
||
493 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
||
494 | |||
495 | // prepare the bitmask |
||
496 | BLConfig_ReadMask = 0x0001<<(motor-1); |
||
497 | |||
498 | for(i = 0; i < MAX_MOTORS; i++) |
||
499 | { |
||
500 | if((0x0001<<i) & BLConfig_ReadMask) |
||
501 | { |
||
502 | Motor[i].SetPoint = 0; |
||
503 | Motor[i].SetPointLowerBits = 0; |
||
504 | } |
||
505 | } |
||
506 | |||
507 | motor_read = 0; |
||
508 | BLConfig.Revision = 0; // bad revision |
||
509 | BLConfig.crc = 0; // bad checksum |
||
510 | // needs at least MAX_MOTORS loops of 2 ms (12*2ms = 24ms) |
||
511 | do |
||
512 | { |
||
513 | I2C_Start(TWI_STATE_MOTOR_TX); // start an i2c transmission |
||
514 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
||
515 | }while(BLConfig_ReadMask && !CheckDelay(timer)); // repeat until the BL config has been received from all motors |
||
516 | // validate result |
||
517 | if(BLConfig.Revision != BLCONFIG_REVISION) return (BLCONFIG_ERR_SW_NOT_COMPATIBLE); // bad revison |
||
518 | i = RAM_Checksum((uint8_t*)&BLConfig, sizeof(BLConfig_t) - 1); |
||
519 | if(i != BLConfig.crc) return(BLCONFIG_ERR_CHECKSUM); // bad checksum |
||
520 | return(BLCONFIG_SUCCESS); |
||
521 | } |
||
522 | #endif |
||
523 |