Rev 1455 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1455 | acid | 1 | |
2 | #include <string.h> |
||
3 | #include <avr/io.h> |
||
4 | #include <util/twi.h> |
||
5 | #include <avr/interrupt.h> |
||
6 | #include "servoboard.h" |
||
7 | #include "twislave.h" |
||
8 | |||
1456 | acid | 9 | uint8_t I2C_RXBuffer[32]; |
10 | uint8_t Byte_Counter = 0; |
||
1455 | acid | 11 | |
12 | void InitIC2_Slave(uint8_t adr) |
||
13 | { |
||
14 | TWAR = adr; |
||
15 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA); |
||
16 | } |
||
17 | |||
18 | uint8_t calc_crc(uint8_t *buffer, uint8_t l) { |
||
19 | uint8_t crc = 0xff; |
||
20 | uint8_t i; |
||
21 | for(i = 0; i < l; i++) { |
||
22 | crc = crc ^ buffer[i]; |
||
23 | } |
||
24 | return crc; |
||
25 | } |
||
26 | |||
27 | ISR (TWI_vect) { |
||
28 | |||
1456 | acid | 29 | static char cnt = 0; |
1455 | acid | 30 | if (pwm_neutral_programming_mode == 0 && cnt++ == 0) { |
1456 | acid | 31 | if (PORTD & 0x80) PORTD &= ~0x80; else PORTD |= 0x80; |
1455 | acid | 32 | } |
33 | |||
1456 | acid | 34 | switch (TWSR & 0xF8) { |
1455 | acid | 35 | case SR_SLA_ACK: |
36 | TWCR |= (1<<TWINT); |
||
37 | Byte_Counter = 0; |
||
38 | return; |
||
39 | case SR_PREV_ACK: |
||
40 | if (Byte_Counter < 32) { |
||
41 | I2C_RXBuffer[Byte_Counter++] = TWDR; |
||
42 | } |
||
43 | if (Byte_Counter == 7 && pwm_neutral_programming_mode == 0) { |
||
44 | if (calc_crc(&I2C_RXBuffer, 6) == I2C_RXBuffer[6]) { |
||
45 | memcpy(pwm_position, I2C_RXBuffer, 6); |
||
46 | } |
||
47 | } |
||
48 | TWCR |= (1<<TWINT); |
||
49 | return; |
||
50 | case TWI_BUS_ERR_2: |
||
51 | TWCR |=(1<<TWSTO) | (1<<TWINT); |
||
52 | case TWI_BUS_ERR_1: |
||
53 | TWCR |=(1<<TWSTO) | (1<<TWINT); |
||
1456 | acid | 54 | } |
55 | TWCR =(1<<TWEA) | (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
||
56 | |||
1455 | acid | 57 | } |
58 | |||
59 |