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