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