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