]> cvs.zerfleddert.de Git - rsbs2/blame - bmc/i2c.c
acknowledge all interrupts when sending, fixes a crash...
[rsbs2] / bmc / i2c.c
CommitLineData
99e4226b
MG
1#include <util/twi.h>
2#include <avr/interrupt.h>
3#include <stdio.h>
4#include "i2c.h"
da7751cb 5#include "bmc.h"
d5193055 6#include "ipmb.h"
99e4226b
MG
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
45320371 12volatile unsigned char i2c_databuf[24];
da7751cb
MG
13volatile uint8_t i2c_len = 0x00;
14static volatile uint8_t i2c_pos = 0x00;
15volatile uint8_t i2c_done = 0x00;
77ad1a84 16
99e4226b
MG
17void i2c_init()
18{
da7751cb 19 TWBR = 0xff;
7f52e040
MG
20 TWAR = BMC_ADDR & 0xfe;
21 TWDR = 0x00;
22 TWCR &= ~((1<<TWSTA) | (1<<TWSTO));
23 TWCR |= ((1<<TWEA) | (1<<TWEN) | (1<<TWIE));
e641fce6 24#ifdef __AVR_ATmega16__
7f52e040 25 PORTC = 0x03;
e641fce6
MG
26#else
27#error "Don't know how to set pullups for this chip, please add support"
28#endif
99e4226b
MG
29}
30
d5193055
MG
31void 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))) {}
87f4338b
MG
42 if (TW_STATUS != TW_START) {
43#ifdef DEBUG
44 printf("I2C: error sending START\n");
45#endif
d5193055 46 goto out;
87f4338b 47 }
d5193055
MG
48
49 TWDR = buf[0]; /* SLA_W */
50 TWCR = ((1<<TWINT) | (1<<TWEN));
51
52 while(!(TWCR & (1<<TWINT))) {}
87f4338b
MG
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 }
d5193055
MG
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))) {}
87f4338b
MG
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 }
d5193055
MG
71 }
72
27f7ea19 73#ifdef DEBUG
da7751cb
MG
74 printf("I2C Data sent\n");
75#endif
d5193055 76
87f4338b
MG
77out2:
78 TWCR = ((1<<TWINT) | (1<<TWEN) | (1<<TWSTO));
79 while(TWCR & (1<<TWSTO)) {}
80
81 TWCR = (1<<TWINT) | (1<<TWEN);
d5193055 82out:
d5193055
MG
83 TWCR = old_TWCR;
84 SREG = old_SREG;
85}
86
7f52e040 87ISR (TWI_vect, ISR_BLOCK)
99e4226b 88{
da7751cb
MG
89 if (i2c_done)
90 TWCR_RESET;
91
87f4338b 92 switch (TW_STATUS) {
77ad1a84
MG
93 case TW_SR_SLA_ACK:
94#ifdef DEBUG
95 printf("I2C: Slave 0x%02x adressed\n", TWDR);
96#endif
da7751cb
MG
97 i2c_pos = 0x00;
98 i2c_databuf[i2c_pos] = TWDR;
99 i2c_pos++;
77ad1a84
MG
100 TWCR_ACK;
101 break;
102
103 case TW_SR_DATA_ACK:
104#ifdef DEBUG
105 printf("I2C: Data received: 0x%02x\n", TWDR);
106#endif
a44532dc
MG
107 if (i2c_pos >= sizeof(i2c_databuf)) {
108 TWCR_RESET;
109 i2c_pos = 0x00;
110 break;
111 }
da7751cb
MG
112 i2c_databuf[i2c_pos] = TWDR;
113 i2c_pos++;
77ad1a84
MG
114 TWCR_ACK;
115 break;
116
117 case TW_SR_STOP:
118#ifdef DEBUG
119 printf("I2C: STOP received\n");
120#endif
da7751cb
MG
121 i2c_len = i2c_pos;
122 i2c_pos = 0x00;
123 i2c_done = 0x01;
77ad1a84
MG
124 TWCR_RESET;
125 break;
126
87f4338b
MG
127 case TW_NO_INFO:
128#ifdef DEBUG
129 printf("I2C: TW_NO_INFO received status 0x%2x\n", TW_STATUS);
130#endif
131 TWCR |= (1<<TWINT);
132 break;
133
77ad1a84 134 default:
f9d5c6e0 135#ifdef DEBUG
87f4338b 136 printf("I2C: Unimplemented status 0x%02x\n", TW_STATUS);
f9d5c6e0 137#endif
77ad1a84
MG
138 TWCR_RESET;
139 break;
99e4226b
MG
140 }
141}
Impressum, Datenschutz