working version
This commit is contained in:
commit
8027ce29b7
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
*.o
|
||||
*.d
|
||||
blmc-config
|
||||
*.bin
|
||||
*.hex
|
30
Makefile
Normal file
30
Makefile
Normal file
@ -0,0 +1,30 @@
|
||||
TARGET = blmc-config
|
||||
|
||||
CFLAGS = -Wall -O2 -MD -MP -MF $(*F).d
|
||||
LDFLAGS = $(shell pkg-config --libs gtk+-2.0)
|
||||
CFLAGS += $(shell pkg-config --cflags gtk+-2.0)
|
||||
|
||||
# ------
|
||||
|
||||
SRC := $(wildcard *.c)
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
$(TARGET): $(SRC:.c=.o)
|
||||
@echo " Linking file: $@"
|
||||
@$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS) > /dev/null
|
||||
|
||||
%.o: %.c
|
||||
@echo " Building file: $<"
|
||||
@$(CC) -c $(CFLAGS) $< -o $@
|
||||
|
||||
clean:
|
||||
rm -rf $(TARGET) *.o *.d
|
||||
|
||||
driver:
|
||||
sudo rmmod lp ppdev
|
||||
sudo modprobe i2c-parport type=3
|
||||
sudo modprobe i2c-dev
|
||||
sudo chmod 666 /dev/i2c-0
|
||||
|
||||
-include $(shell find -name *.d 2> /dev/null)
|
37
blmc-config.c
Normal file
37
blmc-config.c
Normal file
@ -0,0 +1,37 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 06/2008 by Olaf Rempel *
|
||||
* razzor@kopf-tisch.de *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; version 2 of the License *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
#include <stdio.h>
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
#include "gtk2-gui.h"
|
||||
#include "i2c.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
gtk_init (&argc, &argv);
|
||||
|
||||
GtkWidget *window = gui_create_window();
|
||||
|
||||
i2c_enumerate_interfaces();
|
||||
|
||||
gtk_widget_show_all(window);
|
||||
gtk_main();
|
||||
|
||||
return 0;
|
||||
}
|
1057
gtk2-gui.c
Normal file
1057
gtk2-gui.c
Normal file
File diff suppressed because it is too large
Load Diff
11
gtk2-gui.h
Normal file
11
gtk2-gui.h
Normal file
@ -0,0 +1,11 @@
|
||||
#ifndef _GTK2_GUI_H_
|
||||
#define _GTK2_GUI_H_
|
||||
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
GtkWidget* gui_create_window(void);
|
||||
|
||||
void gui_add_i2c_interface(const char *path);
|
||||
void gui_flush_i2c_interfaces(void);
|
||||
|
||||
#endif /* _GTK2_GUI_H_ */
|
270
i2c.c
Normal file
270
i2c.c
Normal file
@ -0,0 +1,270 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 09/2007 by Olaf Rempel *
|
||||
* razzor@kopf-tisch.de *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; version 2 of the License, *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the *
|
||||
* Free Software Foundation, Inc., *
|
||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||||
***************************************************************************/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#include <linux/i2c.h>
|
||||
#include <linux/i2c-dev.h>
|
||||
|
||||
#include "gtk2-gui.h"
|
||||
#include "i2c.h"
|
||||
|
||||
/* TWIBOOT commands */
|
||||
#define CMD_GET_INFO 0x10
|
||||
#define CMD_GET_SIGNATURE 0x11
|
||||
#define CMD_WRITE_FLASH 0x12
|
||||
#define CMD_READ_FLASH 0x13
|
||||
#define CMD_WRITE_EEPROM 0x14
|
||||
#define CMD_READ_EEPROM 0x15
|
||||
#define CMD_BOOT_APPLICATION 0x1F
|
||||
|
||||
#define COOKIE 0x4711
|
||||
#define WRITE_COOKIE COOKIE
|
||||
|
||||
/* blctrl commands */
|
||||
//#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
|
||||
|
||||
static int i2c_connected;
|
||||
|
||||
void i2c_enumerate_interfaces(void)
|
||||
{
|
||||
gui_flush_i2c_interfaces();
|
||||
|
||||
char path[64];
|
||||
strcpy(path, "/dev");
|
||||
|
||||
DIR *dp = opendir(path);
|
||||
if (dp == NULL) {
|
||||
perror("opendir()");
|
||||
return;
|
||||
}
|
||||
|
||||
char *ptr = path + strlen(path);
|
||||
*ptr = '/';
|
||||
|
||||
struct dirent *dentry;
|
||||
while ((dentry = readdir(dp)) != NULL) {
|
||||
if (!strcmp(dentry->d_name, "."))
|
||||
continue;
|
||||
|
||||
if (!strcmp(dentry->d_name, ".."))
|
||||
continue;
|
||||
|
||||
if (strncmp(dentry->d_name, "i2c-", 4) != 0)
|
||||
continue;
|
||||
|
||||
strcpy(ptr +1, dentry->d_name);
|
||||
|
||||
struct stat statbuf;
|
||||
if (stat(path, &statbuf) == -1) {
|
||||
perror("stat()");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!S_ISCHR(statbuf.st_mode))
|
||||
continue;
|
||||
|
||||
/* TODO: check permissions */
|
||||
|
||||
gui_add_i2c_interface(path);
|
||||
}
|
||||
closedir(dp);
|
||||
}
|
||||
|
||||
void i2c_close(int fd)
|
||||
{
|
||||
i2c_connected = 0;
|
||||
i2c_close(fd);
|
||||
}
|
||||
|
||||
int i2c_open(const char *path)
|
||||
{
|
||||
int fd = open(path, O_RDWR);
|
||||
if (fd < 0) {
|
||||
perror("open()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
unsigned long funcs;
|
||||
if (ioctl(fd, I2C_FUNCS, &funcs)) {
|
||||
perror("ioctl(I2C_FUNCS)");
|
||||
i2c_close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!(funcs & I2C_FUNC_I2C)) {
|
||||
fprintf(stderr, "I2C_FUNC_I2C not supported!\n");
|
||||
i2c_close(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
i2c_connected = 1;
|
||||
return fd;
|
||||
}
|
||||
|
||||
void i2c_set_address(int fd, int address)
|
||||
{
|
||||
if (ioctl(fd, I2C_SLAVE, address) < 0) {
|
||||
perror("ioctl(I2C_SLAVE)");
|
||||
i2c_close(fd);
|
||||
}
|
||||
}
|
||||
|
||||
int i2c_isconnected(void)
|
||||
{
|
||||
return i2c_connected;
|
||||
}
|
||||
|
||||
|
||||
void i2c_cmd_bootloader(int fd)
|
||||
{
|
||||
char cmd[] = { CMD_BOOT_LOADER };
|
||||
write(fd, cmd, 1);
|
||||
}
|
||||
|
||||
void i2c_cmd_application(int fd)
|
||||
{
|
||||
char cmd[] = { CMD_BOOT_APPLICATION };
|
||||
write(fd, cmd, 1);
|
||||
}
|
||||
|
||||
void i2c_cmd_getinfo(int fd, char *buf, int size)
|
||||
{
|
||||
char cmd[] = { CMD_GET_INFO };
|
||||
write(fd, cmd, 1);
|
||||
|
||||
size = MIN(16, size);
|
||||
memset(buf, 0, size);
|
||||
read(fd, buf, size);
|
||||
|
||||
int i;
|
||||
for (i = 0; i < size; i++)
|
||||
buf[i] &= ~0x80;
|
||||
}
|
||||
|
||||
void i2c_cmd_getsignature(int fd, unsigned char *buf, int size)
|
||||
{
|
||||
char cmd[] = { CMD_GET_SIGNATURE };
|
||||
write(fd, cmd, 1);
|
||||
|
||||
memset(buf, 0, size);
|
||||
read(fd, buf, MIN(4, size));
|
||||
}
|
||||
|
||||
void i2c_cmd_getparameters(int fd, struct blmc_parameter *blmc)
|
||||
{
|
||||
char cmd[] = { CMD_GET_PARAM };
|
||||
write(fd, cmd, 1);
|
||||
|
||||
memset(blmc, 0, sizeof(struct blmc_parameter));
|
||||
read(fd, blmc, sizeof(struct blmc_parameter));
|
||||
}
|
||||
|
||||
void i2c_cmd_setparameters(int fd, struct blmc_parameter *blmc, int persistent)
|
||||
{
|
||||
char cmd[sizeof(struct blmc_parameter) +1];
|
||||
cmd[0] = CMD_SET_PARAM;
|
||||
memcpy(cmd +1, blmc, sizeof(struct blmc_parameter));
|
||||
|
||||
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 fd_file = open(filename, O_RDONLY);
|
||||
if (fd_file < 0) {
|
||||
perror("open()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct stat statbuf;
|
||||
if (fstat(fd_file, &statbuf) == -1) {
|
||||
perror("stat()");
|
||||
close(fd_file);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int file_size = statbuf.st_size;
|
||||
int address = 0, progress = 0;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void i2c_cmd_setpwm(int fd, int pwm)
|
||||
{
|
||||
char cmd[] = { CMD_SET_PWM, pwm };
|
||||
write(fd, cmd, 2);
|
||||
}
|
||||
|
||||
void i2c_cmd_getstatus(int fd, struct blmc_status *status)
|
||||
{
|
||||
char cmd[] = { CMD_GET_STATUS };
|
||||
write(fd, cmd, 1);
|
||||
|
||||
memset(status, 0, sizeof(struct blmc_status));
|
||||
read(fd, status, sizeof(struct blmc_status));
|
||||
}
|
54
i2c.h
Normal file
54
i2c.h
Normal file
@ -0,0 +1,54 @@
|
||||
#ifndef _I2C_H_
|
||||
#define _I2C_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void i2c_enumerate_interfaces(void);
|
||||
void i2c_close(int fd);
|
||||
int i2c_open(const char *path);
|
||||
void i2c_set_address(int fd, int address);
|
||||
int i2c_isconnected(void);
|
||||
|
||||
void i2c_cmd_bootloader(int fd);
|
||||
void i2c_cmd_application(int fd);
|
||||
void i2c_cmd_getinfo(int fd, char *buf, int size);
|
||||
void i2c_cmd_getsignature(int fd, unsigned char *buf, int size);
|
||||
|
||||
struct blmc_parameter {
|
||||
uint16_t spinup_ticks;
|
||||
|
||||
uint8_t spinup_tick;
|
||||
uint8_t spinup_step;
|
||||
|
||||
uint8_t spinup_wait;
|
||||
uint8_t spinup_pwm;
|
||||
|
||||
uint8_t pwm_min;
|
||||
uint8_t pwm_max;
|
||||
|
||||
uint16_t current_limit;
|
||||
uint16_t current_max;
|
||||
|
||||
uint16_t voltage_min;
|
||||
|
||||
uint16_t crc16;
|
||||
};
|
||||
|
||||
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));
|
||||
|
||||
void i2c_cmd_setpwm(int fd, int pwm);
|
||||
|
||||
struct blmc_status {
|
||||
uint8_t pwm_real;
|
||||
uint8_t pwm;
|
||||
uint16_t rpm;
|
||||
uint16_t current;
|
||||
uint16_t voltage;
|
||||
};
|
||||
|
||||
void i2c_cmd_getstatus(int fd, struct blmc_status *status);
|
||||
|
||||
#endif /* _I2C_H_ */
|
Loading…
Reference in New Issue
Block a user