Rev 886 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 886 | Rev 903 | ||
---|---|---|---|
1 | /*############################################################################ |
1 | /*############################################################################ |
2 | ############################################################################*/ |
2 | ############################################################################*/ |
3 | 3 | ||
4 | #include <avr/io.h> |
4 | #include <avr/io.h> |
5 | #include <avr/interrupt.h> |
5 | #include <avr/interrupt.h> |
6 | 6 | ||
7 | #include "main.h" |
7 | #include "main.h" |
8 | #include "twimaster.h" |
8 | #include "twimaster.h" |
9 | #include "fc.h" |
9 | #include "fc.h" |
10 | 10 | ||
11 | volatile uint8_t twi_state = 0; |
11 | volatile uint8_t twi_state = 0; |
12 | volatile uint8_t motor = 0; |
12 | volatile uint8_t motor = 0; |
13 | volatile uint8_t motor_rx[8]; |
13 | volatile uint8_t motor_rx[MOTOR_COUNT*2]; |
14 | 14 | ||
15 | /**************************************************/ |
15 | /**************************************************/ |
16 | /* Initialize I2C (TWI) */ |
16 | /* Initialize I2C (TWI) */ |
17 | /**************************************************/ |
17 | /**************************************************/ |
18 | void I2C_Init(void) |
18 | void I2C_Init(void) |
19 | { |
19 | { |
20 | uint8_t sreg = SREG; |
20 | uint8_t sreg = SREG; |
21 | cli(); |
21 | cli(); |
22 | 22 | ||
23 | // SDA is INPUT |
23 | // SDA is INPUT |
24 | DDRC &= ~(1<<DDC1); |
24 | DDRC &= ~(1<<DDC1); |
25 | // SCL is output |
25 | // SCL is output |
26 | DDRC |= (1<<DDC0); |
26 | DDRC |= (1<<DDC0); |
27 | // pull up SDA |
27 | // pull up SDA |
28 | PORTC |= (1<<PORTC0)|(1<<PORTC1); |
28 | PORTC |= (1<<PORTC0)|(1<<PORTC1); |
29 | 29 | ||
30 | // TWI Status Register |
30 | // TWI Status Register |
31 | // prescaler 1 (TWPS1 = 0, TWPS0 = 0) |
31 | // prescaler 1 (TWPS1 = 0, TWPS0 = 0) |
32 | TWSR &= ~((1<<TWPS1)|(1<<TWPS0)); |
32 | TWSR &= ~((1<<TWPS1)|(1<<TWPS0)); |
33 | 33 | ||
34 | // set TWI Bit Rate Register |
34 | // set TWI Bit Rate Register |
35 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
35 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
36 | 36 | ||
37 | SREG = sreg; |
37 | SREG = sreg; |
38 | } |
38 | } |
39 | 39 | ||
40 | /****************************************/ |
40 | /****************************************/ |
41 | /* Start I2C */ |
41 | /* Start I2C */ |
42 | /****************************************/ |
42 | /****************************************/ |
43 | void I2C_Start(void) |
43 | void I2C_Start(void) |
44 | { |
44 | { |
45 | // TWI Control Register |
45 | // TWI Control Register |
46 | // clear TWI interrupt flag (TWINT=1) |
46 | // clear TWI interrupt flag (TWINT=1) |
47 | // disable TWI Acknowledge Bit (TWEA = 0) |
47 | // disable TWI Acknowledge Bit (TWEA = 0) |
48 | // enable TWI START Condition Bit (TWSTA = 1), MASTER |
48 | // enable TWI START Condition Bit (TWSTA = 1), MASTER |
49 | // disable TWI STOP Condition Bit (TWSTO = 0) |
49 | // disable TWI STOP Condition Bit (TWSTO = 0) |
50 | // disable TWI Write Collision Flag (TWWC = 0) |
50 | // disable TWI Write Collision Flag (TWWC = 0) |
51 | // enable i2c (TWIE = 1) |
51 | // enable i2c (TWIE = 1) |
52 | // enable TWI Interrupt (TWIE = 1) |
52 | // enable TWI Interrupt (TWIE = 1) |
53 | TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN) | (1<<TWIE); |
53 | TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN) | (1<<TWIE); |
54 | } |
54 | } |
55 | 55 | ||
56 | /****************************************/ |
56 | /****************************************/ |
57 | /* Stop I2C */ |
57 | /* Stop I2C */ |
58 | /****************************************/ |
58 | /****************************************/ |
59 | void I2C_Stop(void) |
59 | void I2C_Stop(void) |
60 | { |
60 | { |
61 | // TWI Control Register |
61 | // TWI Control Register |
62 | // clear TWI interrupt flag (TWINT=1) |
62 | // clear TWI interrupt flag (TWINT=1) |
63 | // disable TWI Acknowledge Bit (TWEA = 0) |
63 | // disable TWI Acknowledge Bit (TWEA = 0) |
64 | // diable TWI START Condition Bit (TWSTA = 1), no MASTER |
64 | // diable TWI START Condition Bit (TWSTA = 1), no MASTER |
65 | // enable TWI STOP Condition Bit (TWSTO = 1) |
65 | // enable TWI STOP Condition Bit (TWSTO = 1) |
66 | // disable TWI Write Collision Flag (TWWC = 0) |
66 | // disable TWI Write Collision Flag (TWWC = 0) |
67 | // enable i2c (TWIE = 1) |
67 | // enable i2c (TWIE = 1) |
68 | // disable TWI Interrupt (TWIE = 0) |
68 | // disable TWI Interrupt (TWIE = 0) |
69 | TWCR = (1<<TWINT) | (1<<TWSTO) | (1<<TWEN); |
69 | TWCR = (1<<TWINT) | (1<<TWSTO) | (1<<TWEN); |
70 | } |
70 | } |
71 | 71 | ||
72 | /****************************************/ |
72 | /****************************************/ |
73 | /* Reset I2C */ |
73 | /* Reset I2C */ |
74 | /****************************************/ |
74 | /****************************************/ |
75 | void I2C_Reset(void) |
75 | void I2C_Reset(void) |
76 | { |
76 | { |
77 | // stop i2c bus |
77 | // stop i2c bus |
78 | I2C_Stop(); |
78 | I2C_Stop(); |
79 | twi_state = 0; |
79 | twi_state = 0; |
80 | motor = TWDR; // ?? |
80 | motor = TWDR; // ?? |
81 | motor = 0; |
81 | motor = 0; |
82 | TWCR = (1<<TWINT); // reset to original state incl. interrupt flag reset |
82 | TWCR = (1<<TWINT); // reset to original state incl. interrupt flag reset |
83 | TWAMR = 0; |
83 | TWAMR = 0; |
84 | TWAR = 0; |
84 | TWAR = 0; |
85 | TWDR = 0; |
85 | TWDR = 0; |
86 | TWSR = 0; |
86 | TWSR = 0; |
87 | TWBR = 0; |
87 | TWBR = 0; |
88 | I2C_Init(); |
88 | I2C_Init(); |
89 | I2C_Start(); |
89 | I2C_Start(); |
90 | I2C_WriteByte(0); |
90 | I2C_WriteByte(0); |
91 | } |
91 | } |
92 | 92 | ||
93 | /****************************************/ |
93 | /****************************************/ |
94 | /* Write to I2C */ |
94 | /* Write to I2C */ |
95 | /****************************************/ |
95 | /****************************************/ |
96 | void I2C_WriteByte(int8_t byte) |
96 | void I2C_WriteByte(int8_t byte) |
97 | { |
97 | { |
98 | // move byte to send into TWI Data Register |
98 | // move byte to send into TWI Data Register |
99 | TWDR = byte; |
99 | TWDR = byte; |
100 | // clear interrupt flag (TWINT = 1) |
100 | // clear interrupt flag (TWINT = 1) |
101 | // enable i2c bus (TWEN = 1) |
101 | // enable i2c bus (TWEN = 1) |
102 | // enable intterupt (TWIW = 1) |
102 | // enable intterupt (TWIW = 1) |
103 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
103 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
104 | } |
104 | } |
105 | 105 | ||
106 | 106 | ||
107 | /****************************************/ |
107 | /****************************************/ |
108 | /* Receive byte and send ACK */ |
108 | /* Receive byte and send ACK */ |
109 | /****************************************/ |
109 | /****************************************/ |
110 | void I2C_ReceiveByte(void) |
110 | void I2C_ReceiveByte(void) |
111 | { |
111 | { |
112 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA); |
112 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA); |
113 | } |
113 | } |
114 | 114 | ||
115 | /****************************************/ |
115 | /****************************************/ |
116 | /* I2C receive last byte and send no ACK*/ |
116 | /* I2C receive last byte and send no ACK*/ |
117 | /****************************************/ |
117 | /****************************************/ |
118 | void I2C_ReceiveLastByte(void) |
118 | void I2C_ReceiveLastByte(void) |
119 | { |
119 | { |
120 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
120 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
121 | } |
121 | } |
122 | 122 | ||
123 | 123 | ||
124 | /****************************************/ |
124 | /****************************************/ |
125 | /* I2C ISR */ |
125 | /* I2C ISR */ |
126 | /****************************************/ |
126 | /****************************************/ |
127 | ISR (TWI_vect) |
127 | ISR (TWI_vect) |
128 | 128 | ||
129 | { |
129 | { |
130 | static uint8_t motorread = 0; |
130 | static uint8_t motorread = 0; |
131 | 131 | ||
132 | switch (twi_state++) // First i2s_start from SendMotorData() |
132 | switch (twi_state++) // First i2s_start from SendMotorData() |
133 | { |
133 | { |
134 | // Master Transmit |
134 | // Master Transmit |
135 | case 0: // Send SLA-W |
135 | case 0: // Send SLA-W |
136 | I2C_WriteByte(0x52+(motor*2)); |
136 | I2C_WriteByte(0x52+(motor*2)); |
137 | break; |
137 | break; |
138 | case 1: // Send Data to Salve |
138 | case 1: // Send Data to Salve |
- | 139 | #ifdef HEXAKOPTER |
|
- | 140 | switch(motor++) |
|
- | 141 | { |
|
- | 142 | case 0: |
|
- | 143 | I2C_WriteByte(Motor_FrontLeft); |
|
- | 144 | break; |
|
- | 145 | case 1: |
|
- | 146 | I2C_WriteByte(Motor_RearRight); |
|
- | 147 | break; |
|
- | 148 | case 2: |
|
- | 149 | I2C_WriteByte(Motor_FrontRight); |
|
- | 150 | break; |
|
- | 151 | case 3: |
|
- | 152 | I2C_WriteByte(Motor_RearLeft); |
|
- | 153 | break; |
|
- | 154 | case 4: |
|
- | 155 | I2C_WriteByte(Motor_Right); |
|
- | 156 | break; |
|
- | 157 | case 5: |
|
- | 158 | I2C_WriteByte(Motor_Left); |
|
- | 159 | break; |
|
- | 160 | } |
|
- | 161 | ||
- | 162 | #else |
|
139 | switch(motor++) |
163 | switch(motor++) |
140 | { |
164 | { |
141 | case 0: |
165 | case 0: |
142 | I2C_WriteByte(Motor_Front); |
166 | I2C_WriteByte(Motor_Front); |
143 | break; |
167 | break; |
144 | case 1: |
168 | case 1: |
145 | I2C_WriteByte(Motor_Rear); |
169 | I2C_WriteByte(Motor_Rear); |
146 | break; |
170 | break; |
147 | case 2: |
171 | case 2: |
148 | I2C_WriteByte(Motor_Right); |
172 | I2C_WriteByte(Motor_Right); |
149 | break; |
173 | break; |
150 | case 3: |
174 | case 3: |
151 | I2C_WriteByte(Motor_Left); |
175 | I2C_WriteByte(Motor_Left); |
152 | break; |
176 | break; |
153 | } |
177 | } |
- | 178 | #endif |
|
154 | break; |
179 | break; |
155 | case 2: // repeat case 0+1 for all Slaves |
180 | case 2: // repeat case 0+1 for all Slaves |
156 | if (motor<4) twi_state = 0; |
181 | if (motor<MOTOR_COUNT) twi_state = 0; |
157 | I2C_Start(); // Repeated start -> switch salve or switch Master Transmit -> Master Receive |
182 | I2C_Start(); // Repeated start -> switch salve or switch Master Transmit -> Master Receive |
158 | break; |
183 | break; |
159 | 184 | ||
160 | // Master Receive |
185 | // Master Receive |
161 | case 3: // Send SLA-R |
186 | case 3: // Send SLA-R |
162 | I2C_WriteByte(0x53+(motorread*2)); |
187 | I2C_WriteByte(0x53+(motorread*2)); |
163 | break; |
188 | break; |
164 | case 4: |
189 | case 4: |
165 | //Transmit 1st byte |
190 | //Transmit 1st byte |
166 | I2C_ReceiveByte(); |
191 | I2C_ReceiveByte(); |
167 | break; |
192 | break; |
168 | case 5: //Read 1st byte and transmit 2nd Byte |
193 | case 5: //Read 1st byte and transmit 2nd Byte |
169 | motor_rx[motorread] = TWDR; |
194 | motor_rx[motorread] = TWDR; |
170 | I2C_ReceiveLastByte(); |
195 | I2C_ReceiveLastByte(); |
171 | break; |
196 | break; |
172 | case 6: |
197 | case 6: |
173 | //Read 2nd byte |
198 | //Read 2nd byte |
174 | motor_rx[motorread+4] = TWDR; |
199 | motor_rx[motorread+4] = TWDR; |
175 | motorread++; |
- | |
176 | if (motorread > 3) motorread=0; |
200 | if (++motorread >= MOTOR_COUNT) motorread=0; |
177 | 201 | ||
178 | default: |
202 | default: |
179 | I2C_Stop(); |
203 | I2C_Stop(); |
180 | twi_state = 0; |
204 | twi_state = 0; |
181 | I2CTimeout = 10; |
205 | I2CTimeout = 10; |
182 | motor = 0; |
206 | motor = 0; |
183 | } |
207 | } |
184 | } |
208 | } |
185 | 209 |