148 lines
3.5 KiB
C
148 lines
3.5 KiB
C
/***************************************************************************
|
|
* 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 <avr/io.h>
|
|
#include <avr/interrupt.h>
|
|
#include <avr/wdt.h>
|
|
|
|
#include "main.h"
|
|
#include "blmc.h"
|
|
|
|
extern struct blmc_ blmc;
|
|
|
|
#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.0";
|
|
|
|
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<<TWINT) | (1<<TWEA);
|
|
break;
|
|
|
|
/* prev. SLA + W, data received, ACK returned -> 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:
|
|
bcnt++;
|
|
break;
|
|
|
|
default:
|
|
cmd = CMD_WAIT;
|
|
bcnt = 0;
|
|
break;
|
|
}
|
|
|
|
} else if (bcnt == 1) {
|
|
if (cmd == CMD_SET_PWM)
|
|
setpwm(TWDR);
|
|
|
|
bcnt = 0;
|
|
|
|
} else {
|
|
bcnt = 0;
|
|
}
|
|
|
|
TWCR |= (1<<TWINT) | (1<<TWEA);
|
|
break;
|
|
|
|
/* SLA+R received, ACK returned -> 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 >> 8);
|
|
break;
|
|
|
|
case 3: TWDR = (blmc.rpm & 0xFF);
|
|
break;
|
|
|
|
case 4: TWDR = (blmc.current >> 8);
|
|
break;
|
|
|
|
case 5: TWDR = (blmc.current & 0xFF);
|
|
break;
|
|
|
|
case 6: TWDR = (blmc.voltage >> 8);
|
|
break;
|
|
|
|
case 7: TWDR = (blmc.voltage & 0xFF);
|
|
bcnt = 0;
|
|
break;
|
|
|
|
default:
|
|
bcnt = 0;
|
|
break;
|
|
}
|
|
break;
|
|
|
|
default:
|
|
TWDR = 0xFF;
|
|
break;
|
|
}
|
|
|
|
TWCR |= (1<<TWINT) | (1<<TWEA);
|
|
break;
|
|
|
|
/* STOP or repeated START */
|
|
case 0xA0:
|
|
/* Data transmitted, NACK returned */
|
|
case 0xC0:
|
|
TWCR |= (1<<TWINT) | (1<<TWEA);
|
|
break;
|
|
|
|
/* failsave -> reset Hardware */
|
|
default:
|
|
TWCR |= (1<<TWINT) | (1<<TWSTO) | (1<<TWEA);
|
|
break;
|
|
}
|
|
}
|