Rev 885 | Go to most recent revision | Show entire file | Regard 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" |
|
Line 4... | Line 9... | ||
4 | #include "main.h" |
9 | #include "fc.h" |
5 | 10 | ||
6 | unsigned char twi_state = 0; |
- | |
7 | unsigned char motor = 0; |
11 | volatile uint8_t twi_state = 0; |
8 | unsigned char motorread = 0; |
12 | volatile uint8_t motor = 0; |
9 | unsigned char motor_rx[8]; |
13 | volatile uint8_t motor_rx[8]; |
10 | 14 | ||
- | 15 | /**************************************************/ |
|
11 | //############################################################################ |
16 | /* Initialize I2C (TWI) */ |
12 | //Initzialisieren der I2C (TWI) Schnittstelle |
- | |
13 | 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); |
|
14 | //############################################################################ |
25 | // SCL is output |
15 | { |
26 | DDRC |= (1<<DDC0); |
16 | TWSR = 0; |
27 | // pull up SDA |
- | 28 | PORTC |= (1<<PORTC0)|(1<<PORTC1); |
|
- | 29 | ||
- | 30 | // TWI Status Register |
|
Line 17... | Line -... | ||
17 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
- | |
18 | } |
- | |
19 | 31 | // prescaler 1 (TWPS1 = 0, TWPS0 = 0) |
|
20 | //############################################################################ |
- | |
21 | //Start I2C |
- | |
22 | char i2c_start(void) |
32 | TWSR &= ~((1<<TWPS1)|(1<<TWPS0)); |
23 | //############################################################################ |
- | |
24 | { |
- | |
Line 25... | Line -... | ||
25 | TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE); |
- | |
26 | return(0); |
33 | |
27 | } |
- | |
28 | - | ||
29 | //############################################################################ |
- | |
30 | //Start I2C |
- | |
31 | void i2c_stop(void) |
34 | // set TWI Bit Rate Register |
Line -... | Line 35... | ||
- | 35 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
|
- | 36 | ||
- | 37 | SREG = sreg; |
|
32 | //############################################################################ |
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) |
|
33 | { |
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 | ||
- | 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 |
|
- | 65 | // enable TWI STOP Condition Bit (TWSTO = 1) |
|
- | 66 | // disable TWI Write Collision Flag (TWWC = 0) |
|
- | 67 | // enable i2c (TWIE = 1) |
|
- | 68 | // disable TWI Interrupt (TWIE = 0) |
|
- | 69 | TWCR = (1<<TWINT) | (1<<TWSTO) | (1<<TWEN); |
|
- | 70 | } |
|
34 | TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT); |
71 | |
- | 72 | /****************************************/ |
|
35 | } |
73 | /* Reset I2C */ |
36 | 74 | /****************************************/ |
|
37 | void i2c_reset(void) |
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; |
Line 49... | Line 87... | ||
49 | TWBR = 0; |
87 | TWBR = 0; |
50 | i2c_init(); |
88 | I2C_Init(); |
- | 89 | I2C_Start(); |
|
51 | i2c_start(); |
90 | I2C_WriteByte(0); |
52 | i2c_write_byte(0); |
- | |
53 | } |
91 | } |
54 | 92 | ||
55 | //############################################################################ |
93 | /****************************************/ |
- | 94 | /* Write to I2C */ |
|
- | 95 | /****************************************/ |
|
- | 96 | void I2C_WriteByte(int8_t byte) |
|
56 | //Start I2C |
97 | { |
- | 98 | // move byte to send into TWI Data Register |
|
- | 99 | TWDR = byte; |
|
Line -... | Line 100... | ||
- | 100 | // clear interrupt flag (TWINT = 1) |
|
- | 101 | // enable i2c bus (TWEN = 1) |
|
- | 102 | // enable intterupt (TWIW = 1) |
|
57 | char i2c_write_byte(char byte) |
103 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
- | 104 | } |
|
- | 105 | ||
- | 106 | ||
Line -... | Line 107... | ||
- | 107 | /****************************************/ |
|
- | 108 | /* Receive byte and send ACK */ |
|
- | 109 | /****************************************/ |
|
- | 110 | void I2C_ReceiveByte(void) |
|
- | 111 | { |
|
- | 112 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA); |
|
58 | //############################################################################ |
113 | } |
Line -... | Line 114... | ||
- | 114 | ||
59 | { |
115 | /****************************************/ |
60 | TWSR = 0x00; |
116 | /* I2C receive last byte and send no ACK*/ |
- | 117 | /****************************************/ |
|
61 | TWDR = byte; |
118 | void I2C_ReceiveLastByte(void) |
62 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
- | |
- | 119 | { |
|
63 | 120 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
|
- | 121 | } |
|
- | 122 | ||
64 | return(0); |
123 | |
65 | 124 | /****************************************/ |
|
- | 125 | /* I2C ISR */ |
|
66 | } |
126 | /****************************************/ |
67 | 127 | ISR (TWI_vect) |
|
68 | //############################################################################ |
128 | |
69 | //Start I2C |
129 | { |
70 | SIGNAL (TWI_vect) |
130 | static uint8_t motorread = 0; |
71 | //############################################################################ |
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; |
- | |
87 | case 2: |
146 | break; |
88 | i2c_write_byte(Motor_Rechts); |
147 | case 2: |
89 | break; |
148 | I2C_WriteByte(Motor_Right); |
90 | case 3: |
- | |
91 | i2c_write_byte(Motor_Links); |
149 | break; |
Line 92... | Line 150... | ||
92 | break; |
150 | case 3: |
93 | } |
151 | I2C_WriteByte(Motor_Left); |
94 | break; |
152 | break; |
95 | case 2: |
153 | } |
96 | i2c_stop(); |
154 | break; |
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(); |
- | |
100 | break; |
- | |
101 | - | ||
102 | //Liest Daten von Motor |
- | |
103 | case 3: |
- | |
104 | i2c_write_byte(0x53+(motorread*2)); |
- | |
105 | break; |
- | |
106 | case 4: |
- | |
107 | switch(motorread) |
- | |
108 | { |
- | |
109 | case 0: |
- | |
110 | i2c_write_byte(Motor_Vorne); |
- | |
111 | break; |
- | |
112 | case 1: |
157 | I2C_Start(); // Repeated start -> switch salve or switch Master Transmit -> Master Receive |
113 | i2c_write_byte(Motor_Hinten); |
158 | break; |
114 | break; |
159 | |
115 | case 2: |
- | |
116 | i2c_write_byte(Motor_Rechts); |
- | |
117 | break; |
- | |
118 | case 3: |
- | |
119 | i2c_write_byte(Motor_Links); |
160 | // Master Receive |
120 | break; |
- | |
121 | } |
- | |
122 | break; |
- | |
123 | case 5: //1 Byte vom Motor lesen |
- | |
124 | motor_rx[motorread] = TWDR; |
- | |
125 | - | ||
126 | case 6: |
- | |
127 | switch(motorread) |
- | |
128 | { |
- | |
129 | case 0: |
- | |
130 | i2c_write_byte(Motor_Vorne); |
- | |
131 | break; |
- | |
132 | case 1: |
161 | case 3: // Send SLA-R |
- | 162 | I2C_WriteByte(0x53+(motorread*2)); |
|
133 | i2c_write_byte(Motor_Hinten); |
163 | break; |
134 | break; |
164 | case 4: |
135 | case 2: |
165 | //Transmit 1st byte |
136 | i2c_write_byte(Motor_Rechts); |
166 | I2C_ReceiveByte(); |
- | 167 | break; |
|
137 | break; |
168 | case 5: //Read 1st byte and transmit 2nd Byte |
138 | case 3: |
169 | motor_rx[motorread] = TWDR; |
139 | i2c_write_byte(Motor_Links); |
170 | I2C_ReceiveLastByte(); |
- | 171 | break; |
|
- | 172 | case 6: |
|
140 | break; |
173 | //Read 2nd byte |
141 | } |
- | |
142 | break; |
174 | motor_rx[motorread+4] = TWDR; |