new i2c protocol

This commit is contained in:
Olaf Rempel 2010-12-26 18:47:02 +01:00
parent cb19bb91ec
commit 5fa8d27b5c

View File

@ -27,13 +27,39 @@
extern struct blmc_ blmc;
extern struct ee_param params;
/* SLA+R */
#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
#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 */
const static uint8_t info[16] = "blmc m8-v1.2 ";
@ -41,6 +67,8 @@ ISR(TWI_vect)
{
static uint8_t cmd;
static uint8_t bcnt;
uint8_t data;
uint8_t ack = (1<<TWEA);
switch (TWSR & 0xF8) {
/* SLA + W received, ACK returned -> receive Data and ACK */
@ -51,43 +79,78 @@ ISR(TWI_vect)
/* 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:
case CMD_SET_PARAM:
case CMD_GET_PARAM:
data = TWDR;
switch (bcnt) {
case 0:
switch (data) {
case CMD_SWITCH_APPLICATION:
case CMD_WRITE_MEMORY:
cmd = data;
bcnt++;
break;
default:
blmc.flags |= FLAG_I2C_ACTIVE;
setpwm(data);
cmd = CMD_WAIT;
bcnt = 0;
break;
}
break;
} else if (cmd == CMD_SET_PWM) {
blmc.flags |= FLAG_I2C_ACTIVE;
setpwm(TWDR);
bcnt = 0;
case 1:
switch (cmd) {
case CMD_SWITCH_APPLICATION:
if (data == BOOTTYPE_BOOTLOADER) {
wdt_enable(WDTO_15MS);
} else if (cmd == CMD_SET_PARAM) {
((uint8_t *)&params)[bcnt++ -1] = TWDR;
if (bcnt > sizeof(params)) {
write_parameters();
bcnt = 0;
} else {
ack = (0<<TWEA);
}
break;
case CMD_WRITE_MEMORY:
bcnt++;
if (data == MEMTYPE_PARAMETERS) {
cmd = CMD_WRITE_PARAMETERS;
} else {
ack = (0<<TWEA);
}
break;
default:
ack = (0<<TWEA);
break;
}
break;
} else {
bcnt = 0;
case 2: /* ignore address bytes */
case 3:
bcnt++;
break;
default:
switch (cmd) {
case CMD_WRITE_PARAMETERS:
((uint8_t *)&params)[bcnt++ -4] = data;
if (bcnt == sizeof(params)) {
write_parameters();
ack = (0<<TWEA);
}
break;
default:
ack = (0<<TWEA);
break;
}
break;
}
TWCR |= (1<<TWINT) | (1<<TWEA);
if (ack == 0x00)
bcnt = 0;
TWCR |= (1<<TWINT) | ack;
break;
/* SLA+R received, ACK returned -> send data */
@ -96,55 +159,27 @@ ISR(TWI_vect)
/* prev. SLA+R, data sent, ACK returned -> send data */
case 0xB8:
switch (cmd) {
case CMD_GET_INFO:
TWDR = info[bcnt++];
case CMD_WAIT:
data = 0x00; /* TODO: transmit Current and MaxPWM */
break;
case CMD_READ_VERSION:
data = 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 *)&params)[bcnt++];
case CMD_READ_PARAMETERS:
data = ((uint8_t *)&params)[bcnt++];
if (bcnt > sizeof(params))
bcnt = 0;
break;
default:
TWDR = 0xFF;
data = 0xFF;
break;
}
TWDR = data;
TWCR |= (1<<TWINT) | (1<<TWEA);
break;