]> cvs.zerfleddert.de Git - rsbs2/blob - bmc/i2c.c
TWI inetrrupts aren't asserted when interrupts are disabled, strange...
[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[24];
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 #ifdef __AVR_ATmega16__
25 PORTC = 0x03;
26 #else
27 #error "Don't know how to set pullups for this chip, please add support"
28 #endif
29 }
30
31 void i2c_send(unsigned char *buf, int len)
32 {
33 uint8_t old_TWCR = TWCR;
34 uint8_t old_SREG = SREG;
35 int i;
36
37 cli();
38
39 TWCR = ((1<<TWINT) | (1<<TWSTA) | (1<<TWEN)); /* Send start */
40
41 while(!(TWCR & (1<<TWINT))) {}
42 if (TW_STATUS != TW_START) {
43 #ifdef DEBUG
44 printf("I2C: error sending START\n");
45 #endif
46 goto out;
47 }
48
49 TWDR = buf[0]; /* SLA_W */
50 TWCR = ((1<<TWINT) | (1<<TWEN));
51
52 while(!(TWCR & (1<<TWINT))) {}
53 if (TW_STATUS != TW_MT_SLA_ACK) {
54 #ifdef DEBUG
55 printf("I2C: error sending SLA_W\n");
56 #endif
57 goto out2;
58 }
59
60 for(i = 1; i < len; i++) {
61 TWDR = buf[i]; /* Send Data */
62 TWCR = ((1<<TWINT) | (1<<TWEN));
63
64 while(!(TWCR & (1<<TWINT))) {}
65 if (TW_STATUS != TW_MT_DATA_ACK) {
66 #ifdef DEBUG
67 printf("I2C: error sending DATA byte %d\n", i);
68 #endif
69 goto out2;
70 }
71 }
72
73 #ifdef DEBUG
74 printf("I2C Data sent\n");
75 #endif
76
77 out2:
78 TWCR = ((1<<TWINT) | (1<<TWEN) | (1<<TWSTO));
79 while(TWCR & (1<<TWSTO)) {}
80
81 out:
82 SREG = old_SREG;
83 TWCR = old_TWCR | (1<<TWINT);
84 }
85
86 ISR (TWI_vect, ISR_BLOCK)
87 {
88 if (i2c_done)
89 TWCR_RESET;
90
91 switch (TW_STATUS) {
92 case TW_SR_SLA_ACK:
93 #ifdef DEBUG
94 printf("I2C: Slave 0x%02x adressed\n", TWDR);
95 #endif
96 i2c_pos = 0x00;
97 i2c_databuf[i2c_pos] = TWDR;
98 i2c_pos++;
99 TWCR_ACK;
100 break;
101
102 case TW_SR_DATA_ACK:
103 #ifdef DEBUG
104 printf("I2C: Data received: 0x%02x\n", TWDR);
105 #endif
106 if (i2c_pos >= sizeof(i2c_databuf)) {
107 TWCR_RESET;
108 i2c_pos = 0x00;
109 break;
110 }
111 i2c_databuf[i2c_pos] = TWDR;
112 i2c_pos++;
113 TWCR_ACK;
114 break;
115
116 case TW_SR_STOP:
117 #ifdef DEBUG
118 printf("I2C: STOP received\n");
119 #endif
120 i2c_len = i2c_pos;
121 i2c_pos = 0x00;
122 i2c_done = 0x01;
123 TWCR_RESET;
124 break;
125
126 case TW_NO_INFO:
127 #ifdef DEBUG
128 printf("I2C: TW_NO_INFO received status 0x%2x\n", TW_STATUS);
129 #endif
130 TWCR |= (1<<TWINT);
131 break;
132
133 default:
134 #ifdef DEBUG
135 printf("I2C: Unimplemented status 0x%02x\n", TW_STATUS);
136 #endif
137 TWCR_RESET;
138 break;
139 }
140 }
Impressum, Datenschutz