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