new i2c protocol
This commit is contained in:
parent
cb19bb91ec
commit
5fa8d27b5c
171
i2c-slave.c
171
i2c-slave.c
@ -27,13 +27,39 @@
|
|||||||
extern struct blmc_ blmc;
|
extern struct blmc_ blmc;
|
||||||
extern struct ee_param params;
|
extern struct ee_param params;
|
||||||
|
|
||||||
|
/* SLA+R */
|
||||||
#define CMD_WAIT 0x00
|
#define CMD_WAIT 0x00
|
||||||
#define CMD_GET_INFO 0x10
|
#define CMD_READ_VERSION 0x01
|
||||||
#define CMD_SET_PWM 0x21
|
#define CMD_READ_MEMORY 0x02
|
||||||
#define CMD_GET_STATUS 0x22
|
/* internal mappings */
|
||||||
#define CMD_SET_PARAM 0x23
|
#define CMD_READ_CHIPINFO (0x10 | CMD_READ_MEMORY)
|
||||||
#define CMD_GET_PARAM 0x24
|
#define CMD_READ_FLASH (0x20 | CMD_READ_MEMORY)
|
||||||
#define CMD_BOOT_LOADER 0x2F
|
#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 ";
|
const static uint8_t info[16] = "blmc m8-v1.2 ";
|
||||||
|
|
||||||
@ -41,6 +67,8 @@ ISR(TWI_vect)
|
|||||||
{
|
{
|
||||||
static uint8_t cmd;
|
static uint8_t cmd;
|
||||||
static uint8_t bcnt;
|
static uint8_t bcnt;
|
||||||
|
uint8_t data;
|
||||||
|
uint8_t ack = (1<<TWEA);
|
||||||
|
|
||||||
switch (TWSR & 0xF8) {
|
switch (TWSR & 0xF8) {
|
||||||
/* SLA + W received, ACK returned -> receive Data and ACK */
|
/* 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 */
|
/* prev. SLA + W, data received, ACK returned -> receive Data and ACK */
|
||||||
case 0x80:
|
case 0x80:
|
||||||
if (bcnt == 0) {
|
data = TWDR;
|
||||||
cmd = TWDR;
|
switch (bcnt) {
|
||||||
switch (cmd) {
|
case 0:
|
||||||
case CMD_BOOT_LOADER:
|
switch (data) {
|
||||||
wdt_enable(WDTO_15MS);
|
case CMD_SWITCH_APPLICATION:
|
||||||
|
case CMD_WRITE_MEMORY:
|
||||||
case CMD_GET_INFO:
|
cmd = data;
|
||||||
case CMD_SET_PWM:
|
|
||||||
case CMD_GET_STATUS:
|
|
||||||
case CMD_SET_PARAM:
|
|
||||||
case CMD_GET_PARAM:
|
|
||||||
bcnt++;
|
bcnt++;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
blmc.flags |= FLAG_I2C_ACTIVE;
|
||||||
|
setpwm(data);
|
||||||
cmd = CMD_WAIT;
|
cmd = CMD_WAIT;
|
||||||
bcnt = 0;
|
bcnt = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
} else if (cmd == CMD_SET_PWM) {
|
case 1:
|
||||||
blmc.flags |= FLAG_I2C_ACTIVE;
|
switch (cmd) {
|
||||||
setpwm(TWDR);
|
case CMD_SWITCH_APPLICATION:
|
||||||
bcnt = 0;
|
if (data == BOOTTYPE_BOOTLOADER) {
|
||||||
|
wdt_enable(WDTO_15MS);
|
||||||
} else if (cmd == CMD_SET_PARAM) {
|
|
||||||
((uint8_t *)¶ms)[bcnt++ -1] = TWDR;
|
|
||||||
if (bcnt > sizeof(params)) {
|
|
||||||
write_parameters();
|
|
||||||
bcnt = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
bcnt = 0;
|
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;
|
||||||
|
|
||||||
|
case 2: /* ignore address bytes */
|
||||||
|
case 3:
|
||||||
|
bcnt++;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
switch (cmd) {
|
||||||
|
case CMD_WRITE_PARAMETERS:
|
||||||
|
((uint8_t *)¶ms)[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;
|
break;
|
||||||
|
|
||||||
/* SLA+R received, ACK returned -> send data */
|
/* SLA+R received, ACK returned -> send data */
|
||||||
@ -96,55 +159,27 @@ ISR(TWI_vect)
|
|||||||
/* prev. SLA+R, data sent, ACK returned -> send data */
|
/* prev. SLA+R, data sent, ACK returned -> send data */
|
||||||
case 0xB8:
|
case 0xB8:
|
||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
case CMD_GET_INFO:
|
case CMD_WAIT:
|
||||||
TWDR = info[bcnt++];
|
data = 0x00; /* TODO: transmit Current and MaxPWM */
|
||||||
|
break;
|
||||||
|
|
||||||
|
case CMD_READ_VERSION:
|
||||||
|
data = info[bcnt++];
|
||||||
bcnt %= sizeof(info);
|
bcnt %= sizeof(info);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CMD_GET_STATUS:
|
case CMD_READ_PARAMETERS:
|
||||||
switch (bcnt++) {
|
data = ((uint8_t *)¶ms)[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 *)¶ms)[bcnt++];
|
|
||||||
if (bcnt > sizeof(params))
|
if (bcnt > sizeof(params))
|
||||||
bcnt = 0;
|
bcnt = 0;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
TWDR = 0xFF;
|
data = 0xFF;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TWDR = data;
|
||||||
TWCR |= (1<<TWINT) | (1<<TWEA);
|
TWCR |= (1<<TWINT) | (1<<TWEA);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user