]> git.zerfleddert.de Git - rsbs2/blame - bmc/i2c.c
decode packets
[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"
77ad1a84 5#include "bmc.h"
99e4226b
MG
6
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);
10
77ad1a84
MG
11static volatile unsigned char databuf[12];
12static volatile uint8_t pos = 0x00;
13
99e4226b
MG
14void i2c_init()
15{
7f52e040
MG
16 TWAR = BMC_ADDR & 0xfe;
17 TWDR = 0x00;
18 TWCR &= ~((1<<TWSTA) | (1<<TWSTO));
19 TWCR |= ((1<<TWEA) | (1<<TWEN) | (1<<TWIE));
20 printf("Status: 0x%02x\n", TW_STATUS);
21 PORTC = 0x03;
99e4226b
MG
22}
23
7f52e040 24ISR (TWI_vect, ISR_BLOCK)
99e4226b 25{
99e4226b 26 switch (TW_STATUS) {
77ad1a84
MG
27 case TW_SR_SLA_ACK:
28#ifdef DEBUG
29 printf("I2C: Slave 0x%02x adressed\n", TWDR);
30#endif
31 pos = 0x00;
20ef0f39
MG
32 databuf[pos] = TWDR;
33 pos++;
77ad1a84
MG
34 TWCR_ACK;
35 break;
36
37 case TW_SR_DATA_ACK:
38#ifdef DEBUG
39 printf("I2C: Data received: 0x%02x\n", TWDR);
40#endif
41 databuf[pos] = TWDR;
42 pos++;
43 TWCR_ACK;
44 break;
45
46 case TW_SR_STOP:
47#ifdef DEBUG
48 printf("I2C: STOP received\n");
49#endif
50 decode_bmc_cmd((unsigned char*)databuf, pos);
51 pos = 0x00;
52 TWCR_RESET;
53 break;
54
77ad1a84
MG
55 default:
56 printf("I2C: Unimplemented status 0x%02x\n", TW_STATUS);
57 TWCR_RESET;
58 break;
99e4226b
MG
59 }
60}
Impressum, Datenschutz