AVR based brushless motor controller
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

130 lines
3.2 KiB

/***************************************************************************
* 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_NONE 0x00
#define CMD_SETPWM 0x10
#define CMD_GETSTAT 0x20
#define CMD_REBOOT 0x80
ISR(TWI_vect)
{
static uint8_t cmd = CMD_NONE;
switch (TWSR & 0xF8) {
/* SLA + W received, ACK returned -> receive Data and ACK */
case 0x60:
cmd = CMD_NONE;
TWCR |= (1<<TWINT) | (1<<TWEA);
break;
/* prev. SLA + W, data received, ACK returned -> receive Data and ACK */
case 0x80:
switch (cmd & 0xF0) {
/* First bytes -> Command */
case CMD_NONE:
cmd = TWDR;
break;
/* set pwm */
case CMD_SETPWM:
setpwm(TWDR);
cmd = CMD_NONE;
break;
/* set parameters */
case CMD_REBOOT:
if (TWDR == 0x42)
wdt_enable(WDTO_15MS);
cmd = CMD_NONE;
break;
/* rest invalid */
default:
cmd = CMD_NONE;
break;
}
TWCR |= (1<<TWINT);
break;
/* SLA+R received, ACK returned -> send data */
case 0xA8:
/* prev. SLA+R, data sent, ACK returned -> send data */
case 0xB8:
switch (cmd & 0xF0) {
/* get Current */
case CMD_GETSTAT:
switch (cmd++ & 0x0F) {
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);
cmd = CMD_NONE;
break;
}
break;
/* rest invalid */
default:
cmd = CMD_NONE;
break;
}
TWCR |= (1<<TWINT);
break;
/* STOP or repeated START */
case 0xA0:
/* Data transmitted, NACK returned */
case 0xC0:
TWCR |= (1<<TWINT) | (1<<TWEA);
break;
/* Illegal state -> reset Hardware */
case 0xF8:
TWCR |= (1<<TWINT) | (1<<TWSTO) | (1<<TWEA);
break;
}
}