From 27b32bd462487ec251368f4f2b990ae2c491d4a1 Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Tue, 25 Jan 2011 13:43:30 +0100 Subject: [PATCH] working version --- blmc.c | 14 ++++---------- eeprom.c | 10 +++------- eeprom.h | 9 +++++---- i2c-slave.c | 55 ++++++++++++++++++++++++++++++++++------------------- main.c | 22 +++++++++++++++------ 5 files changed, 63 insertions(+), 47 deletions(-) diff --git a/blmc.c b/blmc.c index 410f678..cf6c359 100644 --- a/blmc.c +++ b/blmc.c @@ -39,13 +39,13 @@ void trigger_adc(uint8_t channel) /* * turn on ADC with interrupts - * start conversion with 1/16 of F_CPU - * 1/500kHz * 25 => 50us conversion time + * start conversion with 1/32 of F_CPU + * 1/250kHz * 25 => 100us conversion time */ ADCSRA = (1< disable EN_C, disable PWM_B, enable EN_B */ PORTD &= ~PHASE_C_EN; @@ -235,11 +232,8 @@ void setpwm(uint8_t pwm) OCR2A = pwm; OCR2B = pwm; - /* disable all drivers */ + /* disable PWM signales, enable all drivers -> brake */ if (pwm == 0) { - PORTB &= ~PHASE_A_EN; - PORTD &= ~(PHASE_B_EN | PHASE_C_EN); - TCCR0A &= ~PHASE_B_OC; TCCR2A &= ~(PHASE_A_OC | PHASE_C_OC); diff --git a/eeprom.c b/eeprom.c index 9e4fa12..ee93aa4 100644 --- a/eeprom.c +++ b/eeprom.c @@ -16,11 +16,12 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include #include #include #include "eeprom.h" -static const struct ee_param defaults = DEFAULT_PARAMETERS; +static struct ee_param defaults PROGMEM = DEFAULT_PARAMETERS; struct ee_param params; struct ee_param params_in_eeprom EEMEM = DEFAULT_PARAMETERS; @@ -48,12 +49,7 @@ uint8_t read_parameters(void) crc = _crc_ccitt_update(crc, *tmp++); if (crc != 0x0000) { - i = sizeof(struct ee_param); - uint8_t *src = (uint8_t *)&defaults; - uint8_t *dst = (uint8_t *)¶ms; - while (i--) - *dst++ = *src++; - + memcpy_P(¶ms, &defaults, sizeof(struct ee_param)); write_parameters(); return 1; } diff --git a/eeprom.h b/eeprom.h index 5acb97a..220a488 100644 --- a/eeprom.h +++ b/eeprom.h @@ -22,16 +22,17 @@ struct ee_param { }; #define DEFAULT_PARAMETERS { \ - .spinup_ticks = 1000, \ - .spinup_tick = 25, \ - .spinup_step = 24, \ + .spinup_ticks = 2000, \ + .spinup_tick = 24, \ + .spinup_step = 4, \ .spinup_wait = 10, \ - .spinup_pwm = 0x0f, \ + .spinup_pwm = 25, \ .pwm_min = 0x08, \ .pwm_max = 0xff, \ .current_limit = 120, \ .current_max = 0x3ff, \ .voltage_min = 0x000, \ + .crc16 = 0xDB76, \ }; uint8_t read_parameters(void); diff --git a/i2c-slave.c b/i2c-slave.c index ac852dc..d591137 100644 --- a/i2c-slave.c +++ b/i2c-slave.c @@ -61,20 +61,19 @@ extern struct ee_param params; -const static uint8_t info[16] = "blctrl m88-v1.2"; +const static uint8_t info[16] = "blctrl m88-v2.0"; ISR(TWI_vect) { static uint8_t cmd; static uint8_t bcnt; uint8_t data; - uint8_t ack = (1< receive Data and ACK */ case 0x60: bcnt = 0; - TWCR |= (1< receive Data and ACK */ @@ -103,9 +102,6 @@ ISR(TWI_vect) case CMD_SWITCH_APPLICATION: if (data == BOOTTYPE_BOOTLOADER) { wdt_enable(WDTO_15MS); - - } else { - ack = (0< send data */ @@ -160,7 +147,35 @@ ISR(TWI_vect) case 0xB8: switch (cmd) { case CMD_WAIT: - data = 0x00; /* TODO: transmit Current and MaxPWM */ + switch (bcnt++) { + case 0: data = blmc.pwm - blmc.pwm_limit; + break; + + case 1: data = blmc.pwm; + break; + + case 2: data = (blmc.rpm & 0xFF); + break; + + case 3: data = (blmc.rpm >> 8); + break; + + case 4: data = (blmc.current & 0xFF); + break; + + case 5: data = (blmc.current >> 8); + break; + + case 6: data = (blmc.voltage & 0xFF); + break; + + case 7: data = (blmc.voltage >> 8); + break; + + default: + data = 0x00; + break; + } break; case CMD_READ_VERSION: @@ -180,19 +195,19 @@ ISR(TWI_vect) } TWDR = data; - TWCR |= (1< reset Hardware */ default: - TWCR |= (1< #include +#include #include "main.h" #include "blmc.h" @@ -63,7 +64,7 @@ ISR(TIMER1_COMPB_vect) blmc.flags &= ~(FLAG_RUN_MASK); blmc.flags |= FLAG_PWM_SPINUP | FLAG_COM_SPINUP; } - +#if 0 /* no i2c cmd in the last 20ms */ if (!(blmc.flags & FLAG_I2C_ACTIVE)) { /* already in i2c timeout, turn off motor */ @@ -75,9 +76,8 @@ ISR(TIMER1_COMPB_vect) } else { blmc.flags &= ~FLAG_I2C_TIMEOUT; } - -// TODO: currently disabled -// blmc.flags &= ~FLAG_I2C_ACTIVE; +#endif + blmc.flags &= ~FLAG_I2C_ACTIVE; } /* set pwm again (adjust current limit) */ @@ -100,7 +100,6 @@ ISR(TIMER1_COMPB_vect) else adc_chan = SENSE_CURRENT; } - /* led blink timer */ static uint8_t led_timer = 0; led_timer = (led_timer +1) & 0x1F; @@ -151,12 +150,18 @@ int main(void) TCCR0B = (1< 200ms) */ if (blmc.flags & FLAG_COM_SPINUP) spinup(); + + sleep_mode(); }; + return 0; }