]> git.zerfleddert.de Git - rsbs2/blame - bmc/i2c.c
make sure i2c rxbuf does not overrun
[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));
7f52e040 24 PORTC = 0x03;
99e4226b
MG
25}
26
d5193055
MG
27void 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));
27f7ea19 58 while(TWCR & (1<<TWSTO)) {}
d5193055 59
27f7ea19 60#ifdef DEBUG
da7751cb
MG
61 printf("I2C Data sent\n");
62#endif
d5193055
MG
63
64out:
65 TWDR = 0x00;
66 TWCR = old_TWCR;
67 SREG = old_SREG;
68}
69
7f52e040 70ISR (TWI_vect, ISR_BLOCK)
99e4226b 71{
da7751cb
MG
72 if (i2c_done)
73 TWCR_RESET;
74
99e4226b 75 switch (TW_STATUS) {
77ad1a84
MG
76 case TW_SR_SLA_ACK:
77#ifdef DEBUG
78 printf("I2C: Slave 0x%02x adressed\n", TWDR);
79#endif
da7751cb
MG
80 i2c_pos = 0x00;
81 i2c_databuf[i2c_pos] = TWDR;
82 i2c_pos++;
77ad1a84
MG
83 TWCR_ACK;
84 break;
85
86 case TW_SR_DATA_ACK:
87#ifdef DEBUG
88 printf("I2C: Data received: 0x%02x\n", TWDR);
89#endif
a44532dc
MG
90 if (i2c_pos >= sizeof(i2c_databuf)) {
91 TWCR_RESET;
92 i2c_pos = 0x00;
93 break;
94 }
da7751cb
MG
95 i2c_databuf[i2c_pos] = TWDR;
96 i2c_pos++;
77ad1a84
MG
97 TWCR_ACK;
98 break;
99
100 case TW_SR_STOP:
101#ifdef DEBUG
102 printf("I2C: STOP received\n");
103#endif
da7751cb
MG
104 i2c_len = i2c_pos;
105 i2c_pos = 0x00;
106 i2c_done = 0x01;
77ad1a84
MG
107 TWCR_RESET;
108 break;
109
77ad1a84 110 default:
f9d5c6e0 111#ifdef DEBUG
77ad1a84 112 printf("I2C: Unimplemented status 0x%02x\n", TW_STATUS);
f9d5c6e0 113#endif
77ad1a84
MG
114 TWCR_RESET;
115 break;
99e4226b
MG
116 }
117}
Impressum, Datenschutz