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