new i2c protocol

This commit is contained in:
Olaf Rempel 2007-09-24 19:43:10 +02:00
parent eea082635e
commit 9b486c68e2

View File

@ -25,60 +25,74 @@
extern struct blmc_ blmc; extern struct blmc_ blmc;
#define CMD_NONE 0x00 #define CMD_WAIT 0x00
#define CMD_SETPWM 0x10 #define CMD_GET_INFO 0x10
#define CMD_GETSTAT 0x20 #define CMD_SET_PWM 0x21
#define CMD_REBOOT 0x80 #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) ISR(TWI_vect)
{ {
static uint8_t cmd = CMD_NONE; static uint8_t cmd;
static uint8_t bcnt;
switch (TWSR & 0xF8) { switch (TWSR & 0xF8) {
/* SLA + W received, ACK returned -> receive Data and ACK */ /* SLA + W received, ACK returned -> receive Data and ACK */
case 0x60: case 0x60:
cmd = CMD_NONE; bcnt = 0;
TWCR |= (1<<TWINT) | (1<<TWEA); TWCR |= (1<<TWINT) | (1<<TWEA);
break; break;
/* prev. SLA + W, data received, ACK returned -> receive Data and ACK */ /* prev. SLA + W, data received, ACK returned -> receive Data and ACK */
case 0x80: case 0x80:
switch (cmd & 0xF0) { if (bcnt == 0) {
/* First bytes -> Command */
case CMD_NONE:
cmd = TWDR; cmd = TWDR;
break; switch (cmd) {
case CMD_BOOT_LOADER:
/* set pwm */
case CMD_SETPWM:
setpwm(TWDR);
cmd = CMD_NONE;
break;
/* set parameters */
case CMD_REBOOT:
if (TWDR == 0x42)
wdt_enable(WDTO_15MS); wdt_enable(WDTO_15MS);
cmd = CMD_NONE;
case CMD_GET_INFO:
case CMD_SET_PWM:
case CMD_GET_STATUS:
bcnt++;
break; break;
/* rest invalid */
default: default:
cmd = CMD_NONE; cmd = CMD_WAIT;
bcnt = 0;
break; break;
} }
TWCR |= (1<<TWINT); } else if (bcnt == 1) {
if (cmd == CMD_SET_PWM)
setpwm(TWDR);
bcnt = 0;
} else {
bcnt = 0;
}
TWCR |= (1<<TWINT) | (1<<TWEA);
break; break;
/* SLA+R received, ACK returned -> send data */ /* SLA+R received, ACK returned -> send data */
case 0xA8: case 0xA8:
bcnt = 0;
/* prev. SLA+R, data sent, ACK returned -> send data */ /* prev. SLA+R, data sent, ACK returned -> send data */
case 0xB8: case 0xB8:
switch (cmd & 0xF0) { switch (cmd) {
/* get Current */ case CMD_GET_INFO:
case CMD_GETSTAT: TWDR = info[bcnt++];
switch (cmd++ & 0x0F) { bcnt %= sizeof(info);
break;
case CMD_GET_STATUS:
switch (bcnt++) {
case 0: TWDR = blmc.pwm - blmc.pwm_limit; case 0: TWDR = blmc.pwm - blmc.pwm_limit;
break; break;
@ -101,18 +115,21 @@ ISR(TWI_vect)
break; break;
case 7: TWDR = (blmc.voltage & 0xFF); case 7: TWDR = (blmc.voltage & 0xFF);
cmd = CMD_NONE; bcnt = 0;
break;
}
break; break;
/* rest invalid */
default: default:
cmd = CMD_NONE; bcnt = 0;
break;
}
break;
default:
TWDR = 0xFF;
break; break;
} }
TWCR |= (1<<TWINT); TWCR |= (1<<TWINT) | (1<<TWEA);
break; break;
/* STOP or repeated START */ /* STOP or repeated START */
@ -122,8 +139,8 @@ ISR(TWI_vect)
TWCR |= (1<<TWINT) | (1<<TWEA); TWCR |= (1<<TWINT) | (1<<TWEA);
break; break;
/* Illegal state -> reset Hardware */ /* failsave -> reset Hardware */
case 0xF8: default:
TWCR |= (1<<TWINT) | (1<<TWSTO) | (1<<TWEA); TWCR |= (1<<TWINT) | (1<<TWSTO) | (1<<TWEA);
break; break;
} }