Rev 687 | Go to most recent revision | Show entire file | Regard 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) |
17 | /**************************************************/ |
- | 18 | void i2c_init(void) |
|
- | 19 | { |
|
- | 20 | uint8_t sreg = SREG; |
|
- | 21 | cli(); |
|
- | 22 | ||
- | 23 | // SDA is INPUT |
|
- | 24 | DDRC &= ~(1<<DDC1); |
|
19 | //############################################################################ |
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)); |
|
20 | { |
33 | |
21 | TWSR = 0; |
- | |
Line 22... | Line -... | ||
22 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
- | |
23 | } |
- | |
24 | - | ||
25 | //############################################################################ |
- | |
26 | //Start I2C |
- | |
27 | char i2c_start(void) |
- | |
28 | //############################################################################ |
34 | // set TWI Bit Rate Register |
29 | { |
35 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
Line -... | Line 36... | ||
- | 36 | ||
- | 37 | SREG = sreg; |
|
- | 38 | } |
|
- | 39 | ||
- | 40 | /****************************************/ |
|
- | 41 | /* Start I2C */ |
|
- | 42 | /****************************************/ |
|
- | 43 | void i2c_start(void) |
|
30 | TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE); |
44 | { |
- | 45 | // TWI Control Register |
|
- | 46 | // clear TWI interrupt flag (TWINT=1) |
|
31 | return(0); |
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); |
|
- | 54 | } |
|
32 | } |
55 | |
33 | - | ||
34 | //############################################################################ |
56 | /****************************************/ |
- | 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 |
|
35 | //Start I2C |
65 | // enable TWI STOP Condition Bit (TWSTO = 1) |
36 | void i2c_stop(void) |
66 | // disable TWI Write Collision Flag (TWWC = 0) |
Line -... | Line 67... | ||
- | 67 | // enable i2c (TWIE = 1) |
|
- | 68 | // disable TWI Interrupt (TWIE = 0) |
|
- | 69 | TWCR = (1<<TWINT) | (1<<TWSTO) | (1<<TWEN); |
|
37 | //############################################################################ |
70 | } |
38 | { |
- | |
39 | TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT); |
71 | |
- | 72 | /****************************************/ |
|
40 | } |
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; |
Line 54... | Line 87... | ||
54 | TWBR = 0; |
87 | TWBR = 0; |
55 | i2c_init(); |
88 | i2c_init(); |
- | 89 | i2c_start(); |
|
56 | i2c_start(); |
90 | i2c_write_byte(0); |
57 | i2c_write_byte(0); |
- | |
58 | } |
91 | } |
59 | 92 | ||
60 | //############################################################################ |
93 | /****************************************/ |
- | 94 | /* Write to I2C */ |
|
- | 95 | /****************************************/ |
|
- | 96 | void i2c_write_byte(int8_t byte) |
|
61 | //Start I2C |
97 | { |
- | 98 | // move byte to send into TWI Data Register |
|
Line 62... | Line -... | ||
62 | int8_t i2c_write_byte(int8_t byte) |
- | |
Line -... | Line 99... | ||
- | 99 | TWDR = byte; |
|
- | 100 | // clear interrupt flag (TWINT = 1) |
|
- | 101 | // enable i2c bus (TWEN = 1) |
|
- | 102 | // enable intterupt (TWIW = 1) |
|
- | 103 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
|
- | 104 | } |
|
63 | //############################################################################ |
105 | |
Line 64... | Line 106... | ||
64 | { |
106 | |
65 | TWSR = 0x00; |
107 | /****************************************/ |
- | 108 | /* Receive byte and send ACK */ |
|
66 | TWDR = byte; |
109 | /****************************************/ |
67 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
- | |
68 | 110 | void i2c_receive_byte(void) |
|
- | 111 | { |
|
- | 112 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA); |
|
- | 113 | } |
|
- | 114 | ||
- | 115 | /****************************************/ |
|
- | 116 | /* I2C receive last byte and send no ACK*/ |
|
- | 117 | /****************************************/ |
|
69 | return(0); |
118 | void i2c_receive_last_byte(void) |
- | 119 | { |
|
70 | 120 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
|
- | 121 | } |
|
- | 122 | ||
- | 123 | ||
71 | } |
124 | /****************************************/ |
- | 125 | /* I2C ISR */ |
|
- | 126 | /****************************************/ |
|
72 | 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 |
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) |
165 | //Transmit 1st byte |
113 | { |
- | |
114 | case 0: |
- | |
115 | i2c_write_byte(Motor_Vorne); |
166 | i2c_receive_byte(); |
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; |
- | |
126 | } |
- | |
127 | break; |
167 | break; |
128 | case 5: //1 Byte vom Motor lesen |
168 | case 5: //Read 1st byte and transmit 2nd Byte |
129 | motor_rx[motorread] = TWDR; |
- | |
130 | - | ||
131 | case 6: |
- | |
132 | switch(motorread) |
- | |
133 | { |
- | |
134 | case 0: |
- | |
135 | i2c_write_byte(Motor_Vorne); |
169 | motor_rx[motorread] = TWDR; |
136 | break; |
- | |
137 | case 1: |
- | |
138 | i2c_write_byte(Motor_Hinten); |
170 | i2c_receive_last_byte(); |
139 | break; |
- | |
140 | case 2: |
- | |
141 | i2c_write_byte(Motor_Rechts); |
- | |
142 | break; |
171 | break; |
143 | case 3: |
- | |
144 | i2c_write_byte(Motor_Links); |
172 | case 6: |
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 | } |