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