Rev 1400 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1400 | acid | 1 | /*############################################################################ |
2 | ############################################################################*/ |
||
3 | |||
4 | #include "main.h" |
||
5 | |||
6 | volatile unsigned char twi_state = 0; |
||
7 | unsigned char motor = 0; |
||
8 | unsigned char motorread = 0,MissingMotor = 0; |
||
9 | unsigned char motor_rx[16],motor_rx2[16]; |
||
10 | unsigned char MotorPresent[MAX_MOTORS]; |
||
11 | unsigned char MotorError[MAX_MOTORS]; |
||
12 | unsigned int I2CError = 0; |
||
13 | |||
14 | //############################################################################ |
||
15 | //Initzialisieren der I2C (TWI) Schnittstelle |
||
16 | void i2c_init(void) |
||
17 | //############################################################################ |
||
18 | { |
||
19 | TWSR = 0; |
||
1452 | acid | 20 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
1400 | acid | 21 | } |
22 | |||
23 | //############################################################################ |
||
24 | //Start I2C |
||
1452 | acid | 25 | void i2c_start(void) |
1400 | acid | 26 | //############################################################################ |
27 | { |
||
28 | TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE); |
||
29 | } |
||
30 | |||
31 | //############################################################################ |
||
32 | void i2c_stop(void) |
||
33 | //############################################################################ |
||
34 | { |
||
35 | TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT); |
||
36 | } |
||
37 | |||
38 | void i2c_reset(void) |
||
39 | //############################################################################ |
||
40 | { |
||
1452 | acid | 41 | i2c_stop(); |
1400 | acid | 42 | twi_state = 0; |
43 | motor = TWDR; |
||
44 | motor = 0; |
||
45 | TWCR = 0x80; |
||
46 | TWAMR = 0; |
||
47 | TWAR = 0; |
||
48 | TWDR = 0; |
||
49 | TWSR = 0; |
||
50 | TWBR = 0; |
||
51 | i2c_init(); |
||
52 | i2c_start(); |
||
53 | i2c_write_byte(0); |
||
54 | } |
||
55 | |||
56 | //############################################################################ |
||
57 | void i2c_write_byte(char byte) |
||
58 | //############################################################################ |
||
1452 | acid | 59 | { |
1400 | acid | 60 | TWSR = 0x00; |
61 | TWDR = byte; |
||
62 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
||
63 | } |
||
64 | |||
65 | /****************************************/ |
||
66 | /* Write to I2C */ |
||
67 | /****************************************/ |
||
68 | void I2C_WriteByte(int8_t byte) |
||
69 | { |
||
70 | // move byte to send into TWI Data Register |
||
71 | TWDR = byte; |
||
72 | // clear interrupt flag (TWINT = 1) |
||
73 | // enable i2c bus (TWEN = 1) |
||
74 | // enable interrupt (TWIE = 1) |
||
75 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
||
76 | } |
||
77 | |||
78 | /****************************************/ |
||
79 | /* Receive byte and send ACK */ |
||
80 | /****************************************/ |
||
81 | void I2C_ReceiveByte(void) |
||
82 | { |
||
83 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA); |
||
84 | } |
||
85 | |||
86 | /****************************************/ |
||
87 | /* I2C receive last byte and send no ACK*/ |
||
88 | /****************************************/ |
||
89 | void I2C_ReceiveLastByte(void) |
||
90 | { |
||
91 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
||
92 | } |
||
93 | |||
1452 | acid | 94 | uint8_t calc_crc(uint8_t *buffer, uint8_t l) { |
95 | uint8_t crc = 0xff; |
||
96 | uint8_t i; |
||
97 | for(i = 0; i < l; i++) { |
||
98 | crc = crc ^ buffer[i]; |
||
99 | } |
||
100 | return crc; |
||
101 | } |
||
1400 | acid | 102 | |
103 | //############################################################################ |
||
104 | SIGNAL (TWI_vect) |
||
105 | //############################################################################ |
||
106 | { |
||
107 | static unsigned char missing_motor; |
||
108 | switch(twi_state++) |
||
109 | { |
||
110 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
111 | // writing Gyro-Offset |
||
112 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1452 | acid | 113 | case 8: |
1400 | acid | 114 | i2c_write_byte(0x98); // Address of the DAC |
115 | break; |
||
1452 | acid | 116 | case 9: |
1400 | acid | 117 | i2c_write_byte(0x10); // Update Channel A |
118 | break; |
||
1452 | acid | 119 | case 10: |
1400 | acid | 120 | i2c_write_byte(AnalogOffsetNick); // Value |
121 | break; |
||
1452 | acid | 122 | case 11: |
1400 | acid | 123 | i2c_write_byte(0x80); // Value |
124 | break; |
||
1452 | acid | 125 | case 12: |
126 | i2c_stop(); |
||
1400 | acid | 127 | I2CTimeout = 10; |
1452 | acid | 128 | i2c_start(); |
1400 | acid | 129 | break; |
1452 | acid | 130 | case 13: |
1400 | acid | 131 | i2c_write_byte(0x98); // Address of the DAC |
132 | break; |
||
1452 | acid | 133 | case 14: |
1400 | acid | 134 | i2c_write_byte(0x12); // Update Channel B |
135 | break; |
||
1452 | acid | 136 | case 15: |
1400 | acid | 137 | i2c_write_byte(AnalogOffsetRoll); // Value |
138 | break; |
||
1452 | acid | 139 | case 16: |
1400 | acid | 140 | i2c_write_byte(0x80); // Value |
141 | break; |
||
1452 | acid | 142 | case 17: |
143 | i2c_stop(); |
||
1400 | acid | 144 | I2CTimeout = 10; |
1452 | acid | 145 | i2c_start(); |
1400 | acid | 146 | break; |
1452 | acid | 147 | case 18: |
1400 | acid | 148 | i2c_write_byte(0x98); // Address of the DAC |
149 | break; |
||
1452 | acid | 150 | case 19: |
1400 | acid | 151 | i2c_write_byte(0x14); // Update Channel C |
152 | break; |
||
1452 | acid | 153 | case 20: |
1400 | acid | 154 | i2c_write_byte(AnalogOffsetGier); // Value |
155 | break; |
||
1452 | acid | 156 | case 21: |
1400 | acid | 157 | i2c_write_byte(0x80); // Value |
158 | break; |
||
1452 | acid | 159 | case 22: |
160 | i2c_stop(); |
||
1400 | acid | 161 | I2CTimeout = 10; |
162 | twi_state = 0; |
||
163 | break; |
||
1452 | acid | 164 | |
165 | case 0: |
||
166 | i2c_write_byte(0x82); // servo board |
||
167 | twi_state = 100; |
||
168 | break; |
||
169 | case 100: // servo 1 |
||
170 | i2c_write_byte(servoValues[0]); |
||
171 | break; |
||
172 | case 101: // servo 2 |
||
173 | i2c_write_byte(servoValues[1]); |
||
174 | break; |
||
175 | case 102: // servo 3 |
||
176 | i2c_write_byte(servoValues[2]); |
||
177 | break; |
||
178 | case 103: // servo 4 |
||
179 | i2c_write_byte(servoValues[3]); |
||
180 | break; |
||
181 | case 104: // servo 5 |
||
182 | i2c_write_byte(servoValues[4]); |
||
183 | break; |
||
184 | case 105: // servo 6 |
||
185 | i2c_write_byte(servoValues[5]); |
||
186 | break; |
||
187 | case 106: |
||
188 | i2c_write_byte(calc_crc(&servoValues, 6)); |
||
189 | break; |
||
190 | case 107: |
||
191 | i2c_stop(); |
||
192 | I2CTimeout = 10; |
||
193 | twi_state = 0; |
||
194 | break; |
||
1400 | acid | 195 | default: twi_state = 0; |
1452 | acid | 196 | break; |
1400 | acid | 197 | } |
198 | TWCR |= 0x80; |
||
199 | } |