Rev 1342 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1227 | killagreg | 1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 | // + Copyright (c) 04.2007 Holger Buss |
||
3 | // + Nur für den privaten Gebrauch |
||
4 | // + www.MikroKopter.com |
||
5 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
6 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
||
7 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
||
8 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
||
9 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
||
10 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
||
11 | // + Verkauf von Luftbildaufnahmen, usw. |
||
12 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
13 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
||
14 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
||
15 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
16 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
||
17 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
||
18 | // + eindeutig als Ursprung verlinkt werden |
||
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
20 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
||
21 | // + Benutzung auf eigene Gefahr |
||
22 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
||
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
24 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
||
25 | // + mit unserer Zustimmung zulässig |
||
26 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
27 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
||
28 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
29 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
||
30 | // + this list of conditions and the following disclaimer. |
||
31 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
||
32 | // + from this software without specific prior written permission. |
||
33 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
||
34 | // + for non-commercial use (directly or indirectly) |
||
35 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
||
36 | // + with our written permission |
||
37 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
||
38 | // + clearly linked as origin |
||
39 | // + * porting to systems other than hardware from www.mikrokopter.de is not allowed |
||
40 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||
41 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
42 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
43 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
44 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
45 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
46 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
47 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||
48 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||
49 | // + POSSIBILITY OF SUCH DAMAGE. |
||
50 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
51 | |||
52 | #include <avr/io.h> |
||
53 | #include <avr/interrupt.h> |
||
54 | #include <util/twi.h> |
||
55 | #include "main.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; |
||
65 | |||
66 | volatile uint16_t I2CTimeout = 100; |
||
67 | |||
68 | uint8_t MissingMotor = 0; |
||
69 | |||
70 | |||
71 | MotorData_t Motor[MAX_MOTORS]; |
||
72 | |||
73 | #define SCL_CLOCK 200000L |
||
74 | #define I2C_TIMEOUT 30000 |
||
75 | |||
76 | /**************************************************/ |
||
77 | /* Initialize I2C (TWI) */ |
||
78 | /**************************************************/ |
||
79 | void I2C_Init(void) |
||
80 | { |
||
81 | uint8_t i; |
||
82 | uint8_t sreg = SREG; |
||
83 | cli(); |
||
84 | |||
85 | // SDA is INPUT |
||
86 | DDRC &= ~(1<<DDC1); |
||
87 | // SCL is output |
||
88 | DDRC |= (1<<DDC0); |
||
89 | // pull up SDA |
||
90 | PORTC |= (1<<PORTC0)|(1<<PORTC1); |
||
91 | |||
92 | // TWI Status Register |
||
93 | // prescaler 1 (TWPS1 = 0, TWPS0 = 0) |
||
94 | TWSR &= ~((1<<TWPS1)|(1<<TWPS0)); |
||
95 | |||
96 | // set TWI Bit Rate Register |
||
97 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
||
98 | |||
99 | twi_state = TWI_STATE_MOTOR_TX; |
||
100 | motor_write = 0; |
||
101 | motor_read = 0; |
||
102 | |||
103 | for(i=0; i < MAX_MOTORS; i++) |
||
104 | { |
||
105 | Motor[i].SetPoint = 0; |
||
106 | Motor[i].Present = 0; |
||
107 | Motor[i].Error = 0; |
||
108 | Motor[i].MaxPWM = 0; |
||
109 | } |
||
110 | |||
111 | SREG = sreg; |
||
112 | } |
||
113 | |||
114 | /****************************************/ |
||
115 | /* Start I2C */ |
||
116 | /****************************************/ |
||
117 | void I2C_Start(uint8_t start_state) |
||
118 | { |
||
119 | twi_state = start_state; |
||
120 | // TWI Control Register |
||
121 | // clear TWI interrupt flag (TWINT=1) |
||
122 | // disable TWI Acknowledge Bit (TWEA = 0) |
||
123 | // enable TWI START Condition Bit (TWSTA = 1), MASTER |
||
124 | // disable TWI STOP Condition Bit (TWSTO = 0) |
||
125 | // disable TWI Write Collision Flag (TWWC = 0) |
||
126 | // enable i2c (TWEN = 1) |
||
127 | // enable TWI Interrupt (TWIE = 1) |
||
128 | TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN) | (1<<TWIE); |
||
129 | } |
||
130 | |||
131 | /****************************************/ |
||
132 | /* Stop I2C */ |
||
133 | /****************************************/ |
||
134 | void I2C_Stop(uint8_t start_state) |
||
135 | { |
||
136 | twi_state = start_state; |
||
137 | // TWI Control Register |
||
138 | // clear TWI interrupt flag (TWINT=1) |
||
139 | // disable TWI Acknowledge Bit (TWEA = 0) |
||
140 | // diable TWI START Condition Bit (TWSTA = 1), no MASTER |
||
141 | // enable TWI STOP Condition Bit (TWSTO = 1) |
||
142 | // disable TWI Write Collision Flag (TWWC = 0) |
||
143 | // enable i2c (TWEN = 1) |
||
144 | // disable TWI Interrupt (TWIE = 0) |
||
145 | TWCR = (1<<TWINT) | (1<<TWSTO) | (1<<TWEN); |
||
146 | } |
||
147 | |||
148 | |||
149 | /****************************************/ |
||
150 | /* Write to I2C */ |
||
151 | /****************************************/ |
||
152 | void I2C_WriteByte(int8_t byte) |
||
153 | { |
||
154 | // move byte to send into TWI Data Register |
||
155 | TWDR = byte; |
||
156 | // clear interrupt flag (TWINT = 1) |
||
157 | // enable i2c bus (TWEN = 1) |
||
158 | // enable interrupt (TWIE = 1) |
||
159 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
||
160 | } |
||
161 | |||
162 | |||
163 | /****************************************/ |
||
164 | /* Receive byte and send ACK */ |
||
165 | /****************************************/ |
||
166 | void I2C_ReceiveByte(void) |
||
167 | { |
||
168 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA); |
||
169 | } |
||
170 | |||
171 | /****************************************/ |
||
172 | /* I2C receive last byte and send no ACK*/ |
||
173 | /****************************************/ |
||
174 | void I2C_ReceiveLastByte(void) |
||
175 | { |
||
176 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
||
177 | } |
||
178 | |||
179 | |||
180 | /****************************************/ |
||
181 | /* Reset I2C */ |
||
182 | /****************************************/ |
||
183 | void I2C_Reset(void) |
||
184 | { |
||
185 | // stop i2c bus |
||
186 | I2C_Stop(TWI_STATE_MOTOR_TX); |
||
187 | twi_state = 0; |
||
188 | motor_write = TWDR; |
||
189 | motor_write = 0; |
||
190 | motor_read = 0; |
||
191 | TWCR = (1<<TWINT); // reset to original state incl. interrupt flag reset |
||
192 | TWAMR = 0; |
||
193 | TWAR = 0; |
||
194 | TWDR = 0; |
||
195 | TWSR = 0; |
||
196 | TWBR = 0; |
||
197 | I2C_Init(); |
||
198 | I2C_Start(TWI_STATE_MOTOR_TX); |
||
199 | } |
||
200 | |||
201 | |||
202 | /****************************************/ |
||
203 | /* I2C ISR */ |
||
204 | /****************************************/ |
||
205 | ISR (TWI_vect) |
||
206 | { |
||
207 | static uint8_t missing_motor = 0; |
||
208 | |||
209 | switch (twi_state++) // First i2c_start from SendMotorData() |
||
210 | { |
||
211 | // Master Transmit |
||
212 | case 0: // TWI_STATE_MOTOR_TX |
||
213 | // skip motor if not used in mixer |
||
214 | while((Mixer.Motor[motor_write][MIX_GAS] <= 0) && (motor_write < MAX_MOTORS)) motor_write++; |
||
215 | if(motor_write >= MAX_MOTORS) // writing finished, read now |
||
216 | { |
||
217 | motor_write = 0; |
||
218 | twi_state = TWI_STATE_MOTOR_RX; |
||
219 | I2C_WriteByte(0x53 + (motor_read * 2) ); // select slave adress in rx mode |
||
220 | } |
||
221 | else I2C_WriteByte(0x52 + (motor_write * 2) ); // select slave adress in tx mode |
||
222 | break; |
||
223 | case 1: // Send Data to Slave |
||
224 | I2C_WriteByte(Motor[motor_write].SetPoint); // transmit rotation rate setpoint |
||
225 | break; |
||
226 | case 2: // repeat case 0+1 for all motors |
||
227 | if(TWSR == TW_MT_DATA_NACK) // Data transmitted, NACK received |
||
228 | { |
||
229 | if(!missing_motor) missing_motor = motor_write + 1; |
||
230 | if(++Motor[motor_write].Error == 0) Motor[motor_write].Error = 255; // increment error counter and handle overflow |
||
231 | } |
||
232 | I2C_Stop(TWI_STATE_MOTOR_TX); |
||
233 | I2CTimeout = 10; |
||
234 | motor_write++; // next motor |
||
235 | I2C_Start(TWI_STATE_MOTOR_TX); // Repeated start -> switch slave or switch Master Transmit -> Master Receive |
||
236 | break; |
||
237 | // Master Receive Data |
||
238 | case 3: |
||
239 | if(TWSR != TW_MR_SLA_ACK) // SLA+R transmitted, if not ACK received |
||
240 | { // no response from the addressed slave received |
||
241 | Motor[motor_read].Present = 0; |
||
242 | motor_read++; // next motor |
||
243 | if(motor_read >= MAX_MOTORS) motor_read = 0; // restart reading of first motor if we have reached the last one |
||
244 | I2C_Stop(TWI_STATE_MOTOR_TX); |
||
245 | } |
||
246 | else |
||
247 | { |
||
248 | Motor[motor_read].Present = ('1' - '-') + motor_read; |
||
249 | I2C_ReceiveByte(); //Transmit 1st byte |
||
250 | } |
||
251 | MissingMotor = missing_motor; |
||
252 | missing_motor = 0; |
||
253 | break; |
||
254 | case 4: //Read 1st byte and transmit 2nd Byte |
||
255 | Motor[motor_read].Current = TWDR; |
||
256 | I2C_ReceiveLastByte(); // nack |
||
257 | break; |
||
258 | case 5: |
||
259 | //Read 2nd byte |
||
260 | Motor[motor_read].MaxPWM = TWDR;; |
||
261 | motor_read++; // next motor |
||
262 | if(motor_read >= MAX_MOTORS) motor_read = 0; // restart reading of first motor if we have reached the last one |
||
263 | I2C_Stop(TWI_STATE_MOTOR_TX); |
||
264 | break; |
||
265 | |||
266 | // writing Gyro-Offsets |
||
267 | case 7: |
||
268 | I2C_WriteByte(0x98); // Address the DAC |
||
269 | break; |
||
270 | |||
271 | case 8: |
||
272 | I2C_WriteByte(0x10 + (dac_channel * 2)); // Select DAC Channel (0x10 = A, 0x12 = B, 0x14 = C) |
||
273 | break; |
||
274 | |||
275 | case 9: |
||
276 | switch(dac_channel) |
||
277 | { |
||
278 | case 0: |
||
279 | I2C_WriteByte(DacOffsetGyroNick); // 1st byte for Channel A |
||
280 | break; |
||
281 | case 1: |
||
282 | I2C_WriteByte(DacOffsetGyroRoll); // 1st byte for Channel B |
||
283 | break; |
||
284 | case 2: |
||
285 | I2C_WriteByte(DacOffsetGyroYaw ); // 1st byte for Channel C |
||
286 | break; |
||
287 | } |
||
288 | break; |
||
289 | |||
290 | case 10: |
||
291 | I2C_WriteByte(0x80); // 2nd byte for all channels is 0x80 |
||
292 | break; |
||
293 | |||
294 | case 11: |
||
295 | I2C_Stop(TWI_STATE_MOTOR_TX); |
||
296 | I2CTimeout = 10; |
||
297 | // repeat case 7...10 until all DAC Channels are updated |
||
298 | if(dac_channel < 2) |
||
299 | { |
||
300 | dac_channel ++; // jump to next channel |
||
301 | I2C_Start(TWI_STATE_GYRO_OFFSET_TX); // start transmission for next channel |
||
302 | } |
||
303 | else |
||
304 | { // data to last motor send |
||
305 | dac_channel = 0; // reset dac channel counter |
||
306 | } |
||
307 | break; |
||
308 | |||
309 | default: |
||
310 | I2C_Stop(TWI_STATE_MOTOR_TX); |
||
311 | I2CTimeout = 10; |
||
312 | motor_write = 0; |
||
313 | motor_read = 0; |
||
314 | } |
||
315 | } |