blmc/i2c-slave.c

211 lines
5.0 KiB
C
Raw Permalink Normal View History

2007-09-20 22:54:11 +02:00
/***************************************************************************
* 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"
2008-02-19 15:59:59 +01:00
#include "eeprom.h"
2007-09-20 22:54:11 +02:00
extern struct blmc_ blmc;
2008-02-19 15:59:59 +01:00
extern struct ee_param params;
2007-09-20 22:54:11 +02:00
2010-12-26 18:47:02 +01:00
/* SLA+R */
2007-09-24 19:43:10 +02:00
#define CMD_WAIT 0x00
2010-12-26 18:47:02 +01:00
#define CMD_READ_VERSION 0x01
#define CMD_READ_MEMORY 0x02
/* internal mappings */
#define CMD_READ_CHIPINFO (0x10 | CMD_READ_MEMORY)
#define CMD_READ_FLASH (0x20 | CMD_READ_MEMORY)
#define CMD_READ_EEPROM (0x30 | CMD_READ_MEMORY)
#define CMD_READ_PARAMETERS (0x40 | CMD_READ_MEMORY) /* only in APP */
/* SLA+W */
#define CMD_SWITCH_APPLICATION CMD_READ_VERSION
#define CMD_WRITE_MEMORY CMD_READ_MEMORY
/* internal mappings */
#define CMD_BOOT_BOOTLOADER (0x10 | CMD_SWITCH_APPLICATION) /* only in APP */
#define CMD_BOOT_APPLICATION (0x20 | CMD_SWITCH_APPLICATION)
#define CMD_WRITE_CHIPINFO (0x10 | CMD_WRITE_MEMORY) /* invalid */
#define CMD_WRITE_FLASH (0x20 | CMD_WRITE_MEMORY)
#define CMD_WRITE_EEPROM (0x30 | CMD_WRITE_MEMORY)
#define CMD_WRITE_PARAMETERS (0x40 | CMD_WRITE_MEMORY) /* only in APP */
/* CMD_SWITCH_APPLICATION parameter */
#define BOOTTYPE_BOOTLOADER 0x00 /* only in APP */
#define BOOTTYPE_APPLICATION 0x80
/* CMD_{READ|WRITE}_* parameter */
#define MEMTYPE_CHIPINFO 0x00
#define MEMTYPE_FLASH 0x01
#define MEMTYPE_EEPROM 0x02
#define MEMTYPE_PARAMETERS 0x03 /* only in APP */
2007-09-24 19:43:10 +02:00
2008-06-23 12:30:35 +02:00
const static uint8_t info[16] = "blmc m8-v1.2 ";
2007-09-20 22:54:11 +02:00
ISR(TWI_vect)
{
2007-09-24 19:43:10 +02:00
static uint8_t cmd;
static uint8_t bcnt;
2010-12-26 18:47:02 +01:00
uint8_t data;
uint8_t ack = (1<<TWEA);
2007-09-20 22:54:11 +02:00
switch (TWSR & 0xF8) {
/* SLA + W received, ACK returned -> receive Data and ACK */
case 0x60:
2007-09-24 19:43:10 +02:00
bcnt = 0;
2007-09-20 22:54:11 +02:00
TWCR |= (1<<TWINT) | (1<<TWEA);
break;
/* prev. SLA + W, data received, ACK returned -> receive Data and ACK */
case 0x80:
2010-12-26 18:47:02 +01:00
data = TWDR;
switch (bcnt) {
case 0:
switch (data) {
case CMD_SWITCH_APPLICATION:
case CMD_WRITE_MEMORY:
cmd = data;
2007-09-24 19:43:10 +02:00
bcnt++;
break;
2007-09-20 22:54:11 +02:00
2007-09-24 19:43:10 +02:00
default:
2010-12-26 18:47:02 +01:00
blmc.flags |= FLAG_I2C_ACTIVE;
setpwm(data);
2007-09-24 19:43:10 +02:00
cmd = CMD_WAIT;
bcnt = 0;
break;
}
break;
2010-12-26 18:47:02 +01:00
case 1:
switch (cmd) {
case CMD_SWITCH_APPLICATION:
if (data == BOOTTYPE_BOOTLOADER) {
wdt_enable(WDTO_15MS);
2007-09-20 22:54:11 +02:00
2010-12-26 18:47:02 +01:00
} else {
ack = (0<<TWEA);
}
2007-09-20 22:54:11 +02:00
break;
2010-12-26 18:47:02 +01:00
case CMD_WRITE_MEMORY:
bcnt++;
if (data == MEMTYPE_PARAMETERS) {
cmd = CMD_WRITE_PARAMETERS;
2007-09-20 22:54:11 +02:00
2010-12-26 18:47:02 +01:00
} else {
ack = (0<<TWEA);
}
2007-09-20 22:54:11 +02:00
break;
2010-12-26 18:47:02 +01:00
default:
ack = (0<<TWEA);
2007-09-20 22:54:11 +02:00
break;
2010-12-26 18:47:02 +01:00
}
break;
2007-09-20 22:54:11 +02:00
2010-12-26 18:47:02 +01:00
case 2: /* ignore address bytes */
case 3:
bcnt++;
break;
2007-09-20 22:54:11 +02:00
2010-12-26 18:47:02 +01:00
default:
switch (cmd) {
case CMD_WRITE_PARAMETERS:
((uint8_t *)&params)[bcnt++ -4] = data;
if (bcnt == sizeof(params)) {
write_parameters();
ack = (0<<TWEA);
}
2007-09-24 19:43:10 +02:00
break;
default:
2010-12-26 18:47:02 +01:00
ack = (0<<TWEA);
2007-09-20 22:54:11 +02:00
break;
}
break;
2010-12-26 18:47:02 +01:00
}
if (ack == 0x00)
bcnt = 0;
TWCR |= (1<<TWINT) | ack;
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_WAIT:
2011-06-05 18:23:29 +02:00
switch (bcnt++) {
case 0:
data = blmc.current & 0xFF;
break;
case 1:
data = 0xFF - blmc.pwm_limit;
break;
default:
data = 0xFF;
break;
}
2010-12-26 18:47:02 +01:00
break;
case CMD_READ_VERSION:
data = info[bcnt++];
bcnt %= sizeof(info);
break;
2007-09-20 22:54:11 +02:00
2010-12-26 18:47:02 +01:00
case CMD_READ_PARAMETERS:
data = ((uint8_t *)&params)[bcnt++];
2008-02-19 15:59:59 +01:00
if (bcnt > sizeof(params))
bcnt = 0;
break;
2007-09-20 22:54:11 +02:00
default:
2010-12-26 18:47:02 +01:00
data = 0xFF;
2007-09-20 22:54:11 +02:00
break;
}
2010-12-26 18:47:02 +01:00
TWDR = data;
2007-09-24 19:43:10 +02:00
TWCR |= (1<<TWINT) | (1<<TWEA);
2007-09-20 22:54:11 +02:00
break;
/* STOP or repeated START */
case 0xA0:
/* Data transmitted, NACK returned */
case 0xC0:
TWCR |= (1<<TWINT) | (1<<TWEA);
break;
2007-09-24 19:43:10 +02:00
/* failsave -> reset Hardware */
default:
2007-09-20 22:54:11 +02:00
TWCR |= (1<<TWINT) | (1<<TWSTO) | (1<<TWEA);
break;
}
}