#include <avr/io.h>
-#include <avr/sleep.h>
#include <avr/interrupt.h>
+#include <avr/sleep.h>
+#include <string.h>
#include <stdio.h>
#include "usart.h"
#include "i2c.h"
+#include "ipmb.h"
+#include "chassis.h"
#include "bmc.h"
-uint8_t ipmb_csum(unsigned char *buf, int len)
-{
- uint8_t csum = 0x00;
- int i;
-
- for(i = 0; i < len; i++) {
- csum += buf[i];
- }
-
- return -csum;
-}
-
-void decode_bmc_cmd(unsigned char *buf, int len)
-{
- int i;
-
- printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
- printf("Connection Header:\n");
- printf("\trs Slave Addr.: 0x%02x\n", buf[0]);
- printf("\tnetFn: 0x%02x, LUN: 0x%02x\n", (buf[1]>>2)&0x3f, (buf[1] & 0x03));
- printf("\tChecksum: 0x%02x (%s)\n", buf[2],
- (buf[2] == ipmb_csum(buf, 2)) ? "OK" : "Wrong");
- printf("Data:\n");
- printf("\trq Slave Addr.: 0x%02x\n", buf[3]);
- printf("\trqSeq: 0x%02x, rqLUN: 0x%02x\n", (buf[4]>>2)&0x3f, (buf[4] & 0x03));
- printf("\tcmd: 0x%02x\n", buf[5]);
- printf("\tData: ");
- for(i = 6; i < (len - 1); i++) {
- printf("0x%02x ", buf[i]);
- }
- printf("\n");
- printf("\tChecksum: 0x%02x (%s)\n", buf[len-1],
- (buf[len-1] == ipmb_csum(buf+3, len-4)) ? "OK" : "Wrong");
- printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
-}
+#define SLEEP_MODE
int main(void)
{
- DDRB = 0xff;
- PORTB = 0xff;
+ unsigned char buf[24];
+ unsigned int len;
+ chassis_init();
usart_init();
- printf("\n");
-
i2c_init();
- printf("Waiting for I2C...\n");
+ set_sleep_mode(SLEEP_MODE_IDLE);
sei();
while(1) {
-#if 1
- sleep_mode();
+ cli();
+ while (i2c_done) {
+ len = i2c_len;
+ memcpy(buf, (unsigned char*)i2c_databuf, len);
+ i2c_done = 0x00;
+ sei();
+
+ decode_ipmb_pkt(buf, len);
+
+ cli();
+ }
+
+#ifdef SLEEP_MODE
+ sleep_enable();
+#endif
+ sei();
+#ifdef SLEEP_MODE
+ sleep_cpu();
+ sleep_disable();
#endif
}