Subversion Repositories FlightCtrl

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1538 killagreg 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
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
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 "main.h"
57
#include "eeprom.h"
58
#include "twimaster.h"
59
#include "fc.h"
60
#include "analog.h"
61
 
62
volatile uint8_t twi_state              = TWI_STATE_MOTOR_TX;
63
volatile uint8_t dac_channel    = 0;
64
volatile uint8_t motor_write    = 0;
65
volatile uint8_t motor_read     = 0;
66
 
67
volatile uint16_t I2CTimeout = 100;
68
 
69
uint8_t MissingMotor  = 0;
70
 
71
 
72
MotorData_t Motor[MAX_MOTORS];
73
 
74
#define SCL_CLOCK  200000L
75
#define I2C_TIMEOUT 30000
76
 
77
/**************************************************/
78
/*   Initialize I2C (TWI)                         */
79
/**************************************************/
80
void I2C_Init(void)
81
{
82
        uint8_t i;
83
        uint8_t sreg = SREG;
84
        cli();
85
 
86
        // SDA is INPUT
87
        DDRC  &= ~(1<<DDC1);
88
        // SCL is output
89
        DDRC |= (1<<DDC0);
90
        // pull up SDA
91
        PORTC |= (1<<PORTC0)|(1<<PORTC1);
92
 
93
        // TWI Status Register
94
        // prescaler 1 (TWPS1 = 0, TWPS0 = 0)
95
        TWSR &= ~((1<<TWPS1)|(1<<TWPS0));
96
 
97
        // set TWI Bit Rate Register
98
        TWBR = ((SYSCLK/SCL_CLOCK)-16)/2;
99
 
100
        twi_state               = TWI_STATE_MOTOR_TX;
101
        motor_write     = 0;
102
        motor_read              = 0;
103
 
104
        for(i=0; i < MAX_MOTORS; i++)
105
        {
106
                Motor[i].SetPoint       = 0;
107
                Motor[i].Present        = 0;
108
                Motor[i].Error          = 0;
109
                Motor[i].MaxPWM         = 0;
110
        }
111
 
112
        SREG = sreg;
113
}
114
 
115
/****************************************/
116
/*   Start I2C                          */
117
/****************************************/
118
void I2C_Start(uint8_t start_state)
119
{
120
        twi_state = start_state;
121
        // TWI Control Register
122
        // clear TWI interrupt flag (TWINT=1)
123
        // disable TWI Acknowledge Bit (TWEA = 0)
124
        // enable TWI START Condition Bit (TWSTA = 1), MASTER
125
        // disable TWI STOP Condition Bit (TWSTO = 0)
126
        // disable TWI Write Collision Flag (TWWC = 0)
127
        // enable i2c (TWEN = 1)
128
        // enable TWI Interrupt (TWIE = 1)
129
    TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN) | (1<<TWIE);
130
}
131
 
132
/****************************************/
133
/*    Stop I2C                          */
134
/****************************************/
135
void I2C_Stop(uint8_t start_state)
136
{
137
        twi_state = start_state;
138
        // TWI Control Register
139
        // clear TWI interrupt flag (TWINT=1)
140
        // disable TWI Acknowledge Bit (TWEA = 0)
141
        // diable TWI START Condition Bit (TWSTA = 1), no MASTER
142
        // enable TWI STOP Condition Bit (TWSTO = 1)
143
        // disable TWI Write Collision Flag (TWWC = 0)
144
        // enable i2c (TWEN = 1)
145
        // disable TWI Interrupt (TWIE = 0)
146
    TWCR = (1<<TWINT) | (1<<TWSTO) | (1<<TWEN);
147
}
148
 
149
 
150
/****************************************/
151
/*    Write to I2C                      */
152
/****************************************/
153
void I2C_WriteByte(int8_t byte)
154
{
155
        // move byte to send into TWI Data Register
156
        TWDR = byte;
157
        // clear interrupt flag (TWINT = 1)
158
        // enable i2c bus (TWEN = 1)
159
        // enable interrupt (TWIE = 1)
160
        TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
161
}
162
 
163
 
164
/****************************************/
165
/*    Receive byte and send ACK         */
166
/****************************************/
167
void I2C_ReceiveByte(void)
168
{
169
        TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);
170
}
171
 
172
/****************************************/
173
/* I2C receive last byte and send no ACK*/
174
/****************************************/
175
void I2C_ReceiveLastByte(void)
176
{
177
        TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
178
}
179
 
180
 
181
/****************************************/
182
/*    Reset I2C                         */
183
/****************************************/
184
void I2C_Reset(void)
185
{
186
        // stop i2c bus
187
        I2C_Stop(TWI_STATE_MOTOR_TX);
188
        twi_state               = 0;
189
        motor_write     = TWDR;
190
        motor_write     = 0;
191
        motor_read              = 0;
192
        TWCR = (1<<TWINT); // reset to original state incl. interrupt flag reset
193
        TWAMR = 0;
194
        TWAR = 0;
195
        TWDR = 0;
196
        TWSR = 0;
197
        TWBR = 0;
198
        I2C_Init();
199
        I2C_Start(TWI_STATE_MOTOR_TX);
200
}
201
 
202
 
203
/****************************************/
204
/*        I2C ISR                       */
205
/****************************************/
206
ISR (TWI_vect)
207
{
208
        static uint8_t missing_motor    = 0;
209
 
210
    switch (twi_state++) // First i2c_start from SendMotorData()
211
        {
212
                // Master Transmit
213
        case 0: // TWI_STATE_MOTOR_TX
214
                        // skip motor if not used in mixer
215
                        while((Mixer.Motor[motor_write][MIX_GAS] <= 0) && (motor_write < MAX_MOTORS)) motor_write++;
216
                        if(motor_write >= MAX_MOTORS) // writing finished, read now
217
                        {
218
                                        motor_write = 0;
219
                                        twi_state = TWI_STATE_MOTOR_RX;
220
                                        I2C_WriteByte(0x53 + (motor_read * 2) ); // select slave adress in rx mode
221
                                }
222
                else I2C_WriteByte(0x52 + (motor_write * 2) ); // select slave adress in tx mode
223
                break;
224
        case 1: // Send Data to Slave
225
                                I2C_WriteByte(Motor[motor_write].SetPoint); // transmit rotation rate setpoint
226
                break;
227
        case 2: // repeat case 0+1 for all motors
228
                                if(TWSR == TW_MT_DATA_NACK) // Data transmitted, NACK received
229
                                {
230
                                        if(!missing_motor) missing_motor = motor_write + 1;
231
                                        if(++Motor[motor_write].Error == 0) Motor[motor_write].Error = 255; // increment error counter and handle overflow
232
                                }
233
                        I2C_Stop(TWI_STATE_MOTOR_TX);
234
                        I2CTimeout = 10;
235
                        motor_write++; // next motor
236
                I2C_Start(TWI_STATE_MOTOR_TX); // Repeated start -> switch slave or switch Master Transmit -> Master Receive
237
                break;
238
        // Master Receive Data
239
        case 3:
240
                        if(TWSR != TW_MR_SLA_ACK) //  SLA+R transmitted, if not ACK received
241
                        {       // no response from the addressed slave received
242
                                Motor[motor_read].Present = 0;
243
                                        motor_read++; // next motor
244
                                        if(motor_read >= MAX_MOTORS) motor_read = 0; // restart reading of first motor if we have reached the last one
245
                                        I2C_Stop(TWI_STATE_MOTOR_TX);
246
                                }
247
                else
248
                {
249
                                        Motor[motor_read].Present = ('1' - '-') + motor_read;
250
                                        I2C_ReceiveByte(); //Transmit 1st byte
251
                                }
252
                                MissingMotor = missing_motor;
253
                                missing_motor = 0;
254
                break;
255
        case 4: //Read 1st byte and transmit 2nd Byte
256
                                Motor[motor_read].Current = TWDR;
257
                                I2C_ReceiveLastByte(); // nack
258
                                break;
259
        case 5:
260
                //Read 2nd byte
261
                                Motor[motor_read].MaxPWM = TWDR;;
262
                                motor_read++; // next motor
263
                                if(motor_read >= MAX_MOTORS) motor_read = 0; // restart reading of first motor if we have reached the last one
264
                I2C_Stop(TWI_STATE_MOTOR_TX);
265
                break;
266
 
267
                // writing Gyro-Offsets
268
                case 7:
269
                                I2C_WriteByte(0x98); // Address the DAC
270
                                break;
271
 
272
                case 8:
273
                                I2C_WriteByte(0x10 + (dac_channel * 2)); // Select DAC Channel (0x10 = A, 0x12 = B, 0x14 = C)
274
                                break;
275
 
276
                case 9:
277
                                switch(dac_channel)
278
                                {
279
                                        case 0:
280
                                                        I2C_WriteByte(DacOffsetGyroNick); // 1st byte for Channel A
281
                                                        break;
282
                                        case 1:
283
                                                        I2C_WriteByte(DacOffsetGyroRoll); // 1st byte for Channel B
284
                                                        break;
285
                                        case 2:
286
                                                        I2C_WriteByte(DacOffsetGyroYaw ); // 1st byte for Channel C
287
                                                        break;
288
                                }
289
                                break;
290
 
291
                case 10:
292
                                I2C_WriteByte(0x80); // 2nd byte for all channels is 0x80
293
                                break;
294
 
295
                case 11:
296
                                I2C_Stop(TWI_STATE_MOTOR_TX);
297
                                I2CTimeout = 10;
298
                                // repeat case 7...10 until all DAC Channels are updated
299
                                if(dac_channel < 2)
300
                                {
301
                                        dac_channel ++;         // jump to next channel
302
                                        I2C_Start(TWI_STATE_GYRO_OFFSET_TX);            // start transmission for next channel
303
                                }
304
                                else
305
                                {       // data to last motor send
306
                                        dac_channel = 0; // reset dac channel counter
307
                                }
308
                break;
309
 
310
        default:
311
                I2C_Stop(TWI_STATE_MOTOR_TX);
312
                I2CTimeout = 10;
313
                motor_write = 0;
314
                motor_read = 0;
315
        }
316
}