]> git.zerfleddert.de Git - rsbs2/blob - bmc/i2c.c
e479fd32e50092ca5ceef796913a881a603d2c3a
[rsbs2] / bmc / i2c.c
1 #include <util/twi.h>
2 #include <avr/interrupt.h>
3 #include <stdio.h>
4 #include "i2c.h"
5 #include "bmc.h"
6 #include "ipmb.h"
7
8 #define TWCR_ACK TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC);
9 #define TWCR_NACK TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC);
10 #define TWCR_RESET TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|(0<<TWWC);
11
12 volatile unsigned char i2c_databuf[12];
13 volatile uint8_t i2c_len = 0x00;
14 static volatile uint8_t i2c_pos = 0x00;
15 volatile uint8_t i2c_done = 0x00;
16
17 void i2c_init()
18 {
19 TWBR = 0xff;
20 TWAR = BMC_ADDR & 0xfe;
21 TWDR = 0x00;
22 TWCR &= ~((1<<TWSTA) | (1<<TWSTO));
23 TWCR |= ((1<<TWEA) | (1<<TWEN) | (1<<TWIE));
24 PORTC = 0x03;
25 }
26
27 void i2c_send(unsigned char *buf, int len)
28 {
29 uint8_t old_TWCR = TWCR;
30 uint8_t old_SREG = SREG;
31 int i;
32
33 cli();
34
35 TWCR = ((1<<TWINT) | (1<<TWSTA) | (1<<TWEN)); /* Send start */
36
37 while(!(TWCR & (1<<TWINT))) {}
38 if ((TW_STATUS & 0xf8) != TW_START)
39 goto out;
40
41 TWDR = buf[0]; /* SLA_W */
42 TWCR = ((1<<TWINT) | (1<<TWEN));
43
44 while(!(TWCR & (1<<TWINT))) {}
45 if ((TW_STATUS & 0xf8) != TW_MT_SLA_ACK)
46 goto out;
47
48 for(i = 1; i < len; i++) {
49 TWDR = buf[i]; /* Send Data */
50 TWCR = ((1<<TWINT) | (1<<TWEN));
51
52 while(!(TWCR & (1<<TWINT))) {}
53 if ((TW_STATUS & 0xf8) != TW_MT_DATA_ACK)
54 goto out;
55 }
56
57 TWCR = ((1<<TWINT) | (1<<TWEN) | (1<<TWSTO));
58 while(!(TWCR & (1<<TWSTO))) {}
59
60 #if 1
61 /* Timing problem... */
62 printf("I2C Data sent\n");
63 #endif
64
65 out:
66 TWDR = 0x00;
67 TWCR = old_TWCR;
68 SREG = old_SREG;
69 }
70
71 ISR (TWI_vect, ISR_BLOCK)
72 {
73 if (i2c_done)
74 TWCR_RESET;
75
76 switch (TW_STATUS) {
77 case TW_SR_SLA_ACK:
78 #ifdef DEBUG
79 printf("I2C: Slave 0x%02x adressed\n", TWDR);
80 #endif
81 i2c_pos = 0x00;
82 i2c_databuf[i2c_pos] = TWDR;
83 i2c_pos++;
84 TWCR_ACK;
85 break;
86
87 case TW_SR_DATA_ACK:
88 #ifdef DEBUG
89 printf("I2C: Data received: 0x%02x\n", TWDR);
90 #endif
91 i2c_databuf[i2c_pos] = TWDR;
92 i2c_pos++;
93 TWCR_ACK;
94 break;
95
96 case TW_SR_STOP:
97 #ifdef DEBUG
98 printf("I2C: STOP received\n");
99 #endif
100 i2c_len = i2c_pos;
101 i2c_pos = 0x00;
102 i2c_done = 0x01;
103 TWCR_RESET;
104 break;
105
106 default:
107 printf("I2C: Unimplemented status 0x%02x\n", TW_STATUS);
108 TWCR_RESET;
109 break;
110 }
111 }
Impressum, Datenschutz