Subversion Repositories FlightCtrl

Rev

Rev 1665 | Rev 1672 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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