]> git.zerfleddert.de Git - rsbs2/blobdiff - bmc/i2c.c
ipmb messages from RSBS2 card
[rsbs2] / bmc / i2c.c
index 992d6ce4c840f2cb6324c2067396352f631e1578..7104758551a25b280296ea999091638557a56f00 100644 (file)
--- a/bmc/i2c.c
+++ b/bmc/i2c.c
@@ -2,24 +2,63 @@
 #include <avr/interrupt.h>
 #include <stdio.h>
 #include "i2c.h"
+#include "bmc.h"
 
 #define TWCR_ACK TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC);  
 #define TWCR_NACK TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC);
 #define TWCR_RESET TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|(0<<TWWC);  
 
+static volatile unsigned char databuf[12];
+static volatile uint8_t pos = 0x00;
+
 void i2c_init()
 {
-       TWAR = BMC_ADDR;
-       TWCR &= ~(1<<TWSTA)|(1<<TWSTO);
-       TWCR|= (1<<TWEA) | (1<<TWEN)|(1<<TWIE); 
+       TWAR = BMC_ADDR & 0xfe;
+       TWDR = 0x00;
+       TWCR &= ~((1<<TWSTA) | (1<<TWSTO));
+       TWCR |= ((1<<TWEA) | (1<<TWEN) | (1<<TWIE)); 
+       printf("Status: 0x%02x\n", TW_STATUS);
+       PORTC = 0x03;
 }
 
-ISR (TWI_vect)
+ISR (TWI_vect, ISR_BLOCK)
 {
-       printf("Interrupt, Status: %02x!\n", TW_STATUS);
-
        switch (TW_STATUS) {
+               case TW_SR_SLA_ACK:
+#ifdef DEBUG
+                       printf("I2C: Slave 0x%02x adressed\n", TWDR);
+#endif
+                       pos = 0x00;
+                       TWCR_ACK;
+                       break;
+
+               case TW_SR_DATA_ACK:
+#ifdef DEBUG
+                       printf("I2C: Data received: 0x%02x\n", TWDR);
+#endif
+                       databuf[pos] = TWDR;
+                       pos++;
+                       TWCR_ACK;
+                       break;
+
+               case TW_SR_STOP:
+#ifdef DEBUG
+                       printf("I2C: STOP received\n");
+#endif
+                       decode_bmc_cmd((unsigned char*)databuf, pos);
+                       pos = 0x00;
+                       TWCR_RESET;
+                       break;
+
+               case TW_ST_SLA_ACK:
+               case TW_ST_DATA_ACK:
+                       printf("I2C: Data requested\n");
+                       TWDR = 0x00;
+                       TWCR_ACK;
+                       break;
+
                default:
+                       printf("I2C: Unimplemented status 0x%02x\n", TW_STATUS);
                        TWCR_RESET;
                        break;
        }
Impressum, Datenschutz