new i2c protocol
This commit is contained in:
parent
eea082635e
commit
9b486c68e2
91
i2c-slave.c
91
i2c-slave.c
@ -25,60 +25,74 @@
|
||||
|
||||
extern struct blmc_ blmc;
|
||||
|
||||
#define CMD_NONE 0x00
|
||||
#define CMD_SETPWM 0x10
|
||||
#define CMD_GETSTAT 0x20
|
||||
#define CMD_REBOOT 0x80
|
||||
#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 = CMD_NONE;
|
||||
static uint8_t cmd;
|
||||
static uint8_t bcnt;
|
||||
|
||||
switch (TWSR & 0xF8) {
|
||||
/* SLA + W received, ACK returned -> receive Data and ACK */
|
||||
case 0x60:
|
||||
cmd = CMD_NONE;
|
||||
bcnt = 0;
|
||||
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:
|
||||
if (bcnt == 0) {
|
||||
cmd = TWDR;
|
||||
break;
|
||||
|
||||
/* set pwm */
|
||||
case CMD_SETPWM:
|
||||
setpwm(TWDR);
|
||||
cmd = CMD_NONE;
|
||||
break;
|
||||
|
||||
/* set parameters */
|
||||
case CMD_REBOOT:
|
||||
if (TWDR == 0x42)
|
||||
switch (cmd) {
|
||||
case CMD_BOOT_LOADER:
|
||||
wdt_enable(WDTO_15MS);
|
||||
cmd = CMD_NONE;
|
||||
break;
|
||||
|
||||
/* rest invalid */
|
||||
default:
|
||||
cmd = CMD_NONE;
|
||||
break;
|
||||
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);
|
||||
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 & 0xF0) {
|
||||
/* get Current */
|
||||
case CMD_GETSTAT:
|
||||
switch (cmd++ & 0x0F) {
|
||||
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;
|
||||
|
||||
@ -101,18 +115,21 @@ ISR(TWI_vect)
|
||||
break;
|
||||
|
||||
case 7: TWDR = (blmc.voltage & 0xFF);
|
||||
cmd = CMD_NONE;
|
||||
bcnt = 0;
|
||||
break;
|
||||
|
||||
default:
|
||||
bcnt = 0;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
/* rest invalid */
|
||||
default:
|
||||
cmd = CMD_NONE;
|
||||
TWDR = 0xFF;
|
||||
break;
|
||||
}
|
||||
|
||||
TWCR |= (1<<TWINT);
|
||||
TWCR |= (1<<TWINT) | (1<<TWEA);
|
||||
break;
|
||||
|
||||
/* STOP or repeated START */
|
||||
@ -122,8 +139,8 @@ ISR(TWI_vect)
|
||||
TWCR |= (1<<TWINT) | (1<<TWEA);
|
||||
break;
|
||||
|
||||
/* Illegal state -> reset Hardware */
|
||||
case 0xF8:
|
||||
/* failsave -> reset Hardware */
|
||||
default:
|
||||
TWCR |= (1<<TWINT) | (1<<TWSTO) | (1<<TWEA);
|
||||
break;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user