]>
cvs.zerfleddert.de Git - rsbs2/blob - bmc/i2c.c
6550c92fc62b084bc6d572cc8ae90b046e3d9045
2 #include <avr/interrupt.h>
7 #define TWCR_ACK TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC);
8 #define TWCR_NACK TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC);
9 #define TWCR_RESET TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|(0<<TWWC);
11 static volatile unsigned char databuf
[12];
12 static volatile uint8_t pos
= 0x00;
16 TWAR
= BMC_ADDR
& 0xfe;
18 TWCR
&= ~((1<<TWSTA
) | (1<<TWSTO
));
19 TWCR
|= ((1<<TWEA
) | (1<<TWEN
) | (1<<TWIE
));
20 printf("Status: 0x%02x\n", TW_STATUS
);
24 void i2c_send(unsigned char *buf
, int len
)
26 uint8_t old_TWCR
= TWCR
;
27 uint8_t old_SREG
= SREG
;
32 TWCR
= ((1<<TWINT
) | (1<<TWSTA
) | (1<<TWEN
)); /* Send start */
34 while(!(TWCR
& (1<<TWINT
))) {}
35 if ((TW_STATUS
& 0xf8) != TW_START
)
38 TWDR
= buf
[0]; /* SLA_W */
39 TWCR
= ((1<<TWINT
) | (1<<TWEN
));
41 while(!(TWCR
& (1<<TWINT
))) {}
42 if ((TW_STATUS
& 0xf8) != TW_MT_SLA_ACK
)
45 for(i
= 1; i
< len
; i
++) {
46 TWDR
= buf
[i
]; /* Send Data */
47 TWCR
= ((1<<TWINT
) | (1<<TWEN
));
49 while(!(TWCR
& (1<<TWINT
))) {}
50 if ((TW_STATUS
& 0xf8) != TW_MT_DATA_ACK
)
54 TWCR
= ((1<<TWINT
) | (1<<TWEN
) | (1<<TWSTO
));
56 printf("I2C Data sent 0x%02x\n", TW_STATUS
);
65 ISR (TWI_vect
, ISR_BLOCK
)
70 printf("I2C: Slave 0x%02x adressed\n", TWDR
);
80 printf("I2C: Data received: 0x%02x\n", TWDR
);
89 printf("I2C: STOP received\n");
91 decode_ipmb_pkt((unsigned char*)databuf
, pos
);
97 printf("I2C: Unimplemented status 0x%02x\n", TW_STATUS
);