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