From 6793f109279fefde1b27c054b3a1f57c34e6cfff Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Sat, 13 Jun 2009 15:29:38 +0200 Subject: [PATCH] work --- Makefile | 2 +- gtk2-gui.c | 30 ++++++-------- i2c.c | 119 ++++++++++++++++++++++++++++++++++++----------------- i2c.h | 6 ++- 4 files changed, 99 insertions(+), 58 deletions(-) diff --git a/Makefile b/Makefile index 8d90a0f..b3e4b83 100644 --- a/Makefile +++ b/Makefile @@ -27,4 +27,4 @@ driver: sudo modprobe i2c-dev sudo chmod 666 /dev/i2c-0 --include $(shell find -name *.d 2> /dev/null) +-include $(shell find . -name \*.d 2> /dev/null) diff --git a/gtk2-gui.c b/gtk2-gui.c index a2a01ee..cdf1871 100644 --- a/gtk2-gui.c +++ b/gtk2-gui.c @@ -90,7 +90,7 @@ static void update_status_boxes(struct blmc_status *status) snprintf(buf, sizeof(buf), "%d", status->pwm_real); gtk_entry_set_text(GTK_ENTRY(entry402), buf); - snprintf(buf, sizeof(buf), "%d", status->rpm); + snprintf(buf, sizeof(buf), "%d", status->rpm * 60 / 42); gtk_entry_set_text(GTK_ENTRY(entry403), buf); snprintf(buf, sizeof(buf), "%d", status->current); @@ -134,6 +134,7 @@ static void on_button101_clicked(GtkButton *button, gpointer user_data) } } +/* fetch bootloader info 150ms after bootloader was requested */ static gint bootloader_cb(gpointer data) { char info_buf[16]; @@ -143,7 +144,7 @@ static gint bootloader_cb(gpointer data) i2c_cmd_getsignature(i2c_fd, sig_buf, sizeof(sig_buf)); char msg_buf[64]; - snprintf(msg_buf, sizeof(msg_buf), "%-16s (sig: 0x%02x%02x%02x)\n", + snprintf(msg_buf, sizeof(msg_buf), "bootloader: %-16s (sig: 0x%02x%02x%02x)\n", info_buf, sig_buf[0], sig_buf[1], sig_buf[2]); add_message(msg_buf); @@ -157,6 +158,7 @@ static void on_button102_clicked(GtkButton *button, gpointer user_data) g_timeout_add(150, bootloader_cb, NULL); } +/* fetch application info 150ms after application was started */ static gint application_cb(gpointer data) { char info_buf[16]; @@ -165,7 +167,7 @@ static gint application_cb(gpointer data) i2c_cmd_getparameters(i2c_fd, &blmc_parameters); char msg_buf[64]; - snprintf(msg_buf, sizeof(msg_buf), "%-16s\n", info_buf); + snprintf(msg_buf, sizeof(msg_buf), "application: %-16s\n", info_buf); add_message(msg_buf); update_parameter_boxes(); @@ -185,18 +187,14 @@ static void on_button201_clicked(GtkButton *button, gpointer user_data) add_message("sorry, not implemented yet\n"); } -static void progress_cb(double progress) +/* progress-bar callback */ +static void progress_cb(int pos, int size) { - if (progress < 0.0) - progress = 0.0; - else if (progress > 1.0) - progress = 1.0; - char buf[16]; - snprintf(buf, sizeof(buf), "%3.0f%%", progress * 100); + snprintf(buf, sizeof(buf), "%3d%%", pos * 100 / size); gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progressbar201), buf); - gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progressbar201), progress); + gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progressbar201), (double)pos / (double)size); gdk_window_process_updates(gtk_widget_get_parent_window(progressbar201), 1); } @@ -299,7 +297,7 @@ static gint pwm_update_cb(gpointer data) /* Motor Togglebutton */ static void on_togglebutton401_toggled(GtkToggleButton *togglebutton, gpointer user_data) { - g_timeout_add(50, pwm_update_cb, NULL); + g_timeout_add(250, pwm_update_cb, NULL); } static unsigned int i2c_interface_count; @@ -612,9 +610,7 @@ GtkWidget * gui_create_window (void) gtk_table_attach (GTK_TABLE (table201), filechooserbutton201, 0, 2, 1, 2, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 5, 0); - g_object_set (filechooserbutton201, - "width-chars", 15, - NULL); + g_object_set (filechooserbutton201, "width-chars", 15, NULL); label202 = gtk_label_new ("EEPROM:"); gtk_widget_show (label202); @@ -627,9 +623,7 @@ GtkWidget * gui_create_window (void) gtk_table_attach (GTK_TABLE (table201), filechooserbutton202, 3, 5, 1, 2, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 5, 0); - g_object_set (filechooserbutton202, - "width-chars", 16, - NULL); + g_object_set (filechooserbutton202, "width-chars", 16, NULL); button207 = gtk_button_new_with_mnemonic ("VERIFY"); gtk_widget_show (button207); diff --git a/i2c.c b/i2c.c index 798b443..4f4e9ab 100644 --- a/i2c.c +++ b/i2c.c @@ -46,6 +46,8 @@ #define COOKIE 0x4711 #define WRITE_COOKIE COOKIE +#define FLASH_BLKSIZE 0x40 + /* blctrl commands */ //#define CMD_GET_INFO 0x10 #define CMD_SET_PWM 0x21 @@ -199,7 +201,81 @@ void i2c_cmd_setparameters(int fd, struct blmc_parameter *blmc, int persistent) write(fd, cmd, sizeof(struct blmc_parameter) + (persistent ? 1 : -1)); } -int i2c_write_flash(int fd, const char *filename, void (*progress_cb)(double progress)) +int i2c_write_flash_buf(int fd, char *data, int size, void (*progress_cb)(int pos, int size)) +{ + int pos = 0; + while (pos < size) { + progress_cb(pos, size); + + char buf[FLASH_BLKSIZE +5]; + buf[0] = CMD_WRITE_FLASH; + buf[1] = (pos >> 8) & 0xFF; + buf[2] = pos & 0xFF; + buf[3] = (COOKIE >> 8) & 0xFF; + buf[4] = COOKIE & 0xFF; + + /* copy data and pad with 0xFF */ + int len = MIN(FLASH_BLKSIZE, size - pos); + memcpy(buf +5, &data[pos], len); + memset(buf +5 +len, 0xFF, FLASH_BLKSIZE - len); + + write(fd, buf, sizeof(buf)); + pos += len; + } + + progress_cb(pos, size); + return pos; +} + +int i2c_read_flash_buf(int fd, char *data, int size, void (*progress_cb)(int pos, int size)) +{ + int pos = 0; + while (pos < size) { + progress_cb(pos, size); + + char cmd[3]; + cmd[0] = CMD_READ_FLASH; + cmd[1] = (pos >> 8) & 0xFF; + cmd[2] = pos & 0xFF; + write(fd, cmd, 3); + + int len = MIN(FLASH_BLKSIZE, size - pos); + read(fd, data + pos, len); + + pos += len; + } + + progress_cb(pos, size); + return pos; +} + +int i2c_verify_flash_buf(int fd, char *data, int size, void (*progress_cb)(int pos, int size)) +{ + int pos = 0; + while (pos < size) { + progress_cb(pos, size); + + char cmd[3]; + cmd[0] = CMD_READ_FLASH; + cmd[1] = (pos >> 8) & 0xFF; + cmd[2] = pos & 0xFF; + write(fd, cmd, 3); + + char buf2[FLASH_BLKSIZE]; + int len = MIN(FLASH_BLKSIZE, size - pos); + read(fd, buf2, len); + + if (memcmp(data + pos, buf2, len) != 0) + break; + + pos += len; + } + + progress_cb(pos, size); + return pos; +} + +int i2c_write_flash(int fd, const char *filename, void (*progress_cb)(int pos, int size)) { int fd_file = open(filename, O_RDONLY); if (fd_file < 0) { @@ -214,44 +290,11 @@ int i2c_write_flash(int fd, const char *filename, void (*progress_cb)(double pro return -1; } - int file_size = statbuf.st_size; - int address = 0, progress = 0; + char *data = malloc(statbuf.st_size); + if (data == NULL) + return -1; - while (1) { - progress_cb((double)progress / (double)file_size); - - char buf[64 +5], buf2[64 + 5]; - int len = read(fd_file, buf +5, sizeof(buf) -5); - if (len <= 0) - break; - - else if (len < 64) - memset(buf + len +5, 0xFF, sizeof(buf) - len -5); - - buf[0] = CMD_WRITE_FLASH; - buf[1] = (address >> 8) & 0xFF; - buf[2] = address & 0xFF; - buf[3] = (COOKIE >> 8) & 0xFF; - buf[4] = COOKIE & 0xFF; - write(fd, buf, sizeof(buf)); - - buf[0] = CMD_READ_FLASH; - buf[1] = (address >> 8) & 0xFF; - buf[2] = address & 0xFF; - write(fd, buf, 3); - read(fd, buf2, 64); - - address += 64; - progress += len; - - if (memcmp(buf +5, buf2, 64) != 0) { - progress = -progress; - break; - } - } - - close(fd_file); - return progress; + return i2c_write_flash_buf(fd, data, statbuf.st_size, progress_cb); } void i2c_cmd_setpwm(int fd, int pwm) diff --git a/i2c.h b/i2c.h index d3c8cc7..268bf57 100644 --- a/i2c.h +++ b/i2c.h @@ -37,7 +37,11 @@ struct blmc_parameter { void i2c_cmd_getparameters(int fd, struct blmc_parameter *blmc); void i2c_cmd_setparameters(int fd, struct blmc_parameter *blmc, int persistent); -int i2c_write_flash(int fd, const char *filename, void (*progress_cb)(double progress)); +int i2c_write_flash(int fd, const char *filename, void (*progress_cb)(int pos, int size)); + +int i2c_write_flash_buf(int fd, char *data, int size, void (*progress_cb)(int pos, int size)); +int i2c_read_flash_buf(int fd, char *data, int size, void (*progress_cb)(int pos, int size)); +int i2c_verify_flash_buf(int fd, char *data, int size, void (*progress_cb)(int pos, int size)); void i2c_cmd_setpwm(int fd, int pwm);