Subversion Repositories FlightCtrl

Rev

Rev 687 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 687 Rev 694
Line 6... Line 6...
6
 
6
 
7
#include "main.h"
7
#include "main.h"
8
#include "twimaster.h"
8
#include "twimaster.h"
Line 9... Line 9...
9
#include "fc.h"
9
#include "fc.h"
10
 
10
 
11
unsigned char twi_state = 0;
-
 
12
unsigned char motor = 0;
11
volatile uint8_t twi_state = 0;
13
unsigned char motorread = 0;
12
volatile uint8_t motor = 0;
14
unsigned char motor_rx[8];
13
volatile uint8_t motor_rx[8];
15
 
14
 
-
 
15
/**************************************************/
16
//############################################################################
16
/*   Initialize I2C (TWI)                         */
17
//Initzialisieren der I2C (TWI) Schnittstelle
-
 
18
void i2c_init(void)
-
 
19
//############################################################################
-
 
20
{
-
 
21
  TWSR = 0;
-
 
22
  TWBR = ((SYSCLK/SCL_CLOCK)-16)/2;
-
 
23
}
-
 
24
 
-
 
25
//############################################################################
-
 
26
//Start I2C
-
 
27
char i2c_start(void)
17
/**************************************************/
28
//############################################################################
-
 
29
{
18
void i2c_init(void)
30
    TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE);
19
{
Line -... Line 20...
-
 
20
        uint8_t sreg = SREG;
-
 
21
        cli();
-
 
22
 
-
 
23
        // SDA is INPUT
-
 
24
        DDRC  &= ~(1<<DDC1);
-
 
25
        // SCL is output
-
 
26
        DDRC |= (1<<DDC0);
-
 
27
        // pull up SDA
-
 
28
        PORTC |= (1<<PORTC0)|(1<<PORTC1);
-
 
29
 
-
 
30
        // TWI Status Register
-
 
31
        // prescaler 1 (TWPS1 = 0, TWPS0 = 0)
-
 
32
        TWSR &= ~((1<<TWPS1)|(1<<TWPS0));
-
 
33
 
-
 
34
        // set TWI Bit Rate Register
-
 
35
        TWBR = ((SYSCLK/SCL_CLOCK)-16)/2;
-
 
36
 
-
 
37
        SREG = sreg;
-
 
38
}
-
 
39
 
-
 
40
/****************************************/
-
 
41
/*   Start I2C                          */
-
 
42
/****************************************/
-
 
43
void i2c_start(void)
-
 
44
{
31
    return(0);
45
        // TWI Control Register
-
 
46
        // clear TWI interrupt flag (TWINT=1)
-
 
47
        // disable TWI Acknowledge Bit (TWEA = 0)
32
}
48
        // enable TWI START Condition Bit (TWSTA = 1), MASTER
-
 
49
        // disable TWI STOP Condition Bit (TWSTO = 0)
-
 
50
        // disable TWI Write Collision Flag (TWWC = 0)
-
 
51
        // enable i2c (TWIE = 1)
-
 
52
        // enable TWI Interrupt (TWIE = 1)
-
 
53
    TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN) | (1<<TWIE);
-
 
54
}
-
 
55
 
33
 
56
/****************************************/
34
//############################################################################
-
 
35
//Start I2C
57
/*    Stop I2C                          */
-
 
58
/****************************************/
-
 
59
void i2c_stop(void)
-
 
60
{
-
 
61
        // TWI Control Register
-
 
62
        // clear TWI interrupt flag (TWINT=1)
-
 
63
        // disable TWI Acknowledge Bit (TWEA = 0)
-
 
64
        // diable TWI START Condition Bit (TWSTA = 1), no MASTER
-
 
65
        // enable TWI STOP Condition Bit (TWSTO = 1)
36
void i2c_stop(void)
66
        // disable TWI Write Collision Flag (TWWC = 0)
37
//############################################################################
67
        // enable i2c (TWIE = 1)
Line -... Line 68...
-
 
68
        // disable TWI Interrupt (TWIE = 0)
-
 
69
    TWCR = (1<<TWINT) | (1<<TWSTO) | (1<<TWEN);
-
 
70
}
38
{
71
 
39
    TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT);
-
 
40
}
72
/****************************************/
-
 
73
/*    Reset I2C                         */
41
 
74
/****************************************/
42
void i2c_reset(void)
75
void i2c_reset(void)
43
//############################################################################
76
{
44
{
77
        // stop i2c bus
45
                 i2c_stop();
78
        i2c_stop();
46
                 twi_state = 0;
79
        twi_state = 0;
47
                 motor = TWDR;
80
        motor = TWDR; // ??
48
                 motor = 0;
81
        motor = 0;
49
                 TWCR = 0x80;
82
        TWCR = (1<<TWINT); // reset to original state incl. interrupt flag reset
50
                 TWAMR = 0;
83
        TWAMR = 0;
51
                 TWAR = 0;
84
        TWAR = 0;
52
                 TWDR = 0;
85
        TWDR = 0;
53
                 TWSR = 0;
86
        TWSR = 0;
54
                 TWBR = 0;
87
        TWBR = 0;
55
                 i2c_init();
88
        i2c_init();
56
                 i2c_start();
89
        i2c_start();
57
                 i2c_write_byte(0);
90
        i2c_write_byte(0);
-
 
91
}
58
}
92
 
59
 
-
 
60
//############################################################################
93
/****************************************/
61
//Start I2C
94
/*    Write to I2C                      */
62
int8_t i2c_write_byte(int8_t byte)
95
/****************************************/
-
 
96
void i2c_write_byte(int8_t byte)
-
 
97
{
-
 
98
    // move byte to send into TWI Data Register
63
//############################################################################
99
    TWDR = byte;
-
 
100
    // clear interrupt flag (TWINT = 1)
Line 64... Line -...
64
{
-
 
Line -... Line 101...
-
 
101
    // enable i2c bus (TWEN = 1)
-
 
102
    // enable intterupt (TWIW = 1)
-
 
103
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
-
 
104
}
-
 
105
 
-
 
106
 
-
 
107
/****************************************/
-
 
108
/*    Receive byte and send ACK         */
-
 
109
/****************************************/
-
 
110
void i2c_receive_byte(void)
-
 
111
{
-
 
112
   TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);
-
 
113
}
-
 
114
 
65
    TWSR = 0x00;
115
/****************************************/
Line -... Line 116...
-
 
116
/* I2C receive last byte and send no ACK*/
66
    TWDR = byte;
117
/****************************************/
67
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
118
void i2c_receive_last_byte(void)
-
 
119
{
68
 
120
   TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
69
    return(0);
-
 
-
 
121
}
70
 
122
 
-
 
123
 
-
 
124
/****************************************/
71
}
125
/*        I2C ISR                       */
72
 
126
/****************************************/
-
 
127
ISR (TWI_vect)
73
//############################################################################
128
 
74
//Start I2C
129
{
75
SIGNAL (TWI_vect)
130
        static uint8_t motorread = 0;
76
//############################################################################
131
 
77
{
132
    switch (twi_state++) // First i2s_start from SendMotorData()
78
    switch (twi_state++)
133
        {
79
        {
134
                // Master Transmit
80
        case 0:
135
        case 0: // Send SLA-W
81
                i2c_write_byte(0x52+(motor*2));
136
                i2c_write_byte(0x52+(motor*2));
Line 95... Line 150...
95
                    case 3:
150
                    case 3:
96
                            i2c_write_byte(Motor_Links);
151
                            i2c_write_byte(Motor_Links);
97
                            break;
152
                            break;
98
                    }
153
                    }
99
                break;
154
                break;
100
        case 2:
-
 
101
                i2c_stop();
155
        case 2: // repeat case 0+1 for all Slaves
102
                if (motor<4) twi_state = 0;
156
                if (motor<4) twi_state = 0;
103
                else motor = 0;
-
 
104
                i2c_start();
157
                i2c_start(); // Repeated start -> switch salve or switch Master Transmit -> Master Receive
105
                break;
158
                break;
Line 106... Line 159...
106
 
159
 
107
        //Liest Daten von Motor
160
        // Master Receive
108
        case 3:
161
        case 3: // Send SLA-R
109
                i2c_write_byte(0x53+(motorread*2));
162
                i2c_write_byte(0x53+(motorread*2));
110
                break;
163
                break;
111
        case 4:
164
        case 4:
112
                switch(motorread)
-
 
113
                    {
-
 
114
                    case 0:
-
 
115
                        i2c_write_byte(Motor_Vorne);
-
 
116
                        break;
-
 
117
                    case 1:
-
 
118
                        i2c_write_byte(Motor_Hinten);
-
 
119
                        break;
-
 
120
                    case 2:
-
 
121
                        i2c_write_byte(Motor_Rechts);
-
 
122
                        break;
-
 
123
                    case 3:
-
 
124
                        i2c_write_byte(Motor_Links);
-
 
125
                        break;
165
                //Transmit 1st byte
126
                    }
166
                                i2c_receive_byte();
127
                break;
167
                break;
128
        case 5: //1 Byte vom Motor lesen
168
        case 5: //Read 1st byte and transmit 2nd Byte
-
 
169
                motor_rx[motorread] = TWDR;
129
                motor_rx[motorread] = TWDR;
170
                                i2c_receive_last_byte();
130
 
171
                                break;
131
        case 6:
-
 
132
                switch(motorread)
-
 
133
                    {
-
 
134
                    case 0:
-
 
135
                        i2c_write_byte(Motor_Vorne);
172
        case 6:
136
                        break;
-
 
137
                    case 1:
-
 
138
                        i2c_write_byte(Motor_Hinten);
-
 
139
                        break;
-
 
140
                    case 2:
-
 
141
                        i2c_write_byte(Motor_Rechts);
-
 
142
                        break;
-
 
143
                    case 3:
-
 
144
                        i2c_write_byte(Motor_Links);
-
 
145
                        break;
-
 
146
                    }
-
 
147
                break;
-
 
148
        case 7: //2 Byte vom Motor lesen
173
                //Read 2nd byte
149
                motor_rx[motorread+4] = TWDR;
174
                                motor_rx[motorread+4] = TWDR;
150
                motorread++;
175
                                motorread++;
-
 
176
                if (motorread > 3) motorread=0;
-
 
177
 
151
                if (motorread>3) motorread=0;
178
        default:
152
                i2c_stop();
-
 
153
                I2CTimeout = 10;
179
                i2c_stop();
-
 
180
                twi_state = 0;
-
 
181
                I2CTimeout = 10;
154
                twi_state = 0;
182
                motor = 0;
155
        }
-
 
156
 TWCR |= 0x80;
183
        }