/*************************************************************************** * Copyright (C) 09/2007 by Olaf Rempel * * razzor@kopf-tisch.de * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; version 2 of the License, * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include #include #include #include "main.h" #include "blmc.h" #include "eeprom.h" extern struct blmc_ blmc; extern struct ee_param params; #define CMD_WAIT 0x00 #define CMD_GET_INFO 0x10 #define CMD_SET_PWM 0x21 #define CMD_GET_STATUS 0x22 #define CMD_SET_PARAM 0x23 #define CMD_GET_PARAM 0x24 #define CMD_BOOT_LOADER 0x2F const static uint8_t info[16] = "blctrl m8-v1.2"; ISR(TWI_vect) { static uint8_t cmd; static uint8_t bcnt; switch (TWSR & 0xF8) { /* SLA + W received, ACK returned -> receive Data and ACK */ case 0x60: bcnt = 0; TWCR |= (1< receive Data and ACK */ case 0x80: if (bcnt == 0) { cmd = TWDR; switch (cmd) { case CMD_BOOT_LOADER: wdt_enable(WDTO_15MS); case CMD_GET_INFO: case CMD_SET_PWM: case CMD_GET_STATUS: case CMD_SET_PARAM: case CMD_GET_PARAM: bcnt++; break; default: cmd = CMD_WAIT; bcnt = 0; break; } } else if (cmd == CMD_SET_PWM) { blmc.flags |= FLAG_I2C_ACTIVE; setpwm(TWDR); bcnt = 0; } else if (cmd == CMD_SET_PARAM) { ((uint8_t *)¶ms)[bcnt++ -1] = TWDR; if (bcnt > sizeof(params)) { write_parameters(); bcnt = 0; } } else { bcnt = 0; } TWCR |= (1< send data */ case 0xA8: bcnt = 0; /* prev. SLA+R, data sent, ACK returned -> send data */ case 0xB8: switch (cmd) { case CMD_GET_INFO: TWDR = info[bcnt++]; bcnt %= sizeof(info); break; case CMD_GET_STATUS: switch (bcnt++) { case 0: TWDR = blmc.pwm - blmc.pwm_limit; break; case 1: TWDR = blmc.pwm; break; case 2: TWDR = (blmc.rpm & 0xFF); break; case 3: TWDR = (blmc.rpm >> 8); break; case 4: TWDR = (blmc.current & 0xFF); break; case 5: TWDR = (blmc.current >> 8); break; case 6: TWDR = (blmc.voltage & 0xFF); break; case 7: TWDR = (blmc.voltage >> 8); bcnt = 0; break; default: bcnt = 0; break; } break; case CMD_GET_PARAM: TWDR = ((uint8_t *)¶ms)[bcnt++]; if (bcnt > sizeof(params)) bcnt = 0; break; default: TWDR = 0xFF; break; } TWCR |= (1< reset Hardware */ default: TWCR |= (1<