Subversion Repositories FlightCtrl

Rev

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

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