Subversion Repositories FlightCtrl

Rev

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