Browse Source

new i2c protocol

master
Olaf Rempel 10 years ago
parent
commit
5fa8d27b5c
1 changed files with 101 additions and 66 deletions
  1. +101
    -66
      i2c-slave.c

+ 101
- 66
i2c-slave.c 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,100 +79,107 @@ 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;
}
} else if (cmd == CMD_SET_PWM) {
blmc.flags |= FLAG_I2C_ACTIVE;
setpwm(TWDR);
bcnt = 0;
} else if (cmd == CMD_SET_PARAM) {
((uint8_t *)&params)[bcnt++ -1] = TWDR;
if (bcnt > sizeof(params)) {
write_parameters();
bcnt = 0;
}
} else {
bcnt = 0;
}
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) {
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;
case 1: TWDR = blmc.pwm;
break;
case 1:
switch (cmd) {
case CMD_SWITCH_APPLICATION:
if (data == BOOTTYPE_BOOTLOADER) {
wdt_enable(WDTO_15MS);
case 2: TWDR = (blmc.rpm & 0xFF);
} else {
ack = (0<<TWEA);
}
break;
case 3: TWDR = (blmc.rpm >> 8);
break;
case CMD_WRITE_MEMORY:
bcnt++;
if (data == MEMTYPE_PARAMETERS) {
cmd = CMD_WRITE_PARAMETERS;
case 4: TWDR = (blmc.current & 0xFF);
} else {
ack = (0<<TWEA);
}
break;
case 5: TWDR = (blmc.current >> 8);
default:
ack = (0<<TWEA);
break;
}
break;
case 6: TWDR = (blmc.voltage & 0xFF);
break;
case 2: /* ignore address bytes */
case 3:
bcnt++;
break;
case 7: TWDR = (blmc.voltage >> 8);
bcnt = 0;
default:
switch (cmd) {
case CMD_WRITE_PARAMETERS:
((uint8_t *)&params)[bcnt++ -4] = data;
if (bcnt == sizeof(params)) {
write_parameters();
ack = (0<<TWEA);
}
break;
default:
bcnt = 0;
ack = (0<<TWEA);
break;
}
break;
}
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:
data = 0x00; /* TODO: transmit Current and MaxPWM */
break;
case CMD_READ_VERSION:
data = info[bcnt++];
bcnt %= sizeof(info);
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;


Loading…
Cancel
Save