Skip to content

Instantly share code, notes, and snippets.

@rjwalsh
Created May 18, 2014 00:09
Show Gist options
  • Save rjwalsh/df9f3546b1bbf758a6c6 to your computer and use it in GitHub Desktop.
Save rjwalsh/df9f3546b1bbf758a6c6 to your computer and use it in GitHub Desktop.
diff --git a/database.c b/database.c
index aa90d3e..952b4f9 100644
--- a/database.c
+++ b/database.c
@@ -12,4 +12,5 @@ device_t *get_device_by_name(const char *name) {
if(!strcmp(name, device->name))
return(device);
}
+ return NULL;
}
diff --git a/easyconfig.h b/easyconfig.h
index 59f37ee..d1c696f 100644
--- a/easyconfig.h
+++ b/easyconfig.h
@@ -23,4 +23,5 @@ void Config_set_str(const char *par_name, const char *par_value);
int Config_get_int(const char *par_name);
void Config_set_int(const char *par_name, unsigned int value);
+int Config_open(const char *path);
int Config_close();
diff --git a/main.c b/main.c
index 96f9cb5..bce89af 100644
--- a/main.c
+++ b/main.c
@@ -108,7 +108,7 @@ int get_file_size(const char *filename) {
return(size);
}
-int update_status(char *status_msg, char *fmt, ...) {
+void update_status(char *status_msg, char *fmt, ...) {
va_list args;
va_start(args, fmt);
printf("\r\e[K%s", status_msg);
@@ -116,7 +116,7 @@ int update_status(char *status_msg, char *fmt, ...) {
fflush(stdout);
}
-int compare_memory(char *buf1, char *buf2, int size, char *c1, char *c2) {
+int compare_memory(unsigned char *buf1, unsigned char *buf2, int size, unsigned char *c1, unsigned char *c2) {
int i;
for(i = 0; i < size; i++) {
if(buf1[i] != buf2[i]) {
@@ -129,7 +129,7 @@ int compare_memory(char *buf1, char *buf2, int size, char *c1, char *c2) {
}
/* RAM-centric IO operations */
-void read_page_ram(minipro_handle_t *handle, char *buf, unsigned int type, const char *name, int size) {
+void read_page_ram(minipro_handle_t *handle, unsigned char *buf, unsigned int type, const char *name, int size) {
char status_msg[24];
sprintf(status_msg, "Reading %s... ", name);
@@ -153,7 +153,7 @@ void read_page_ram(minipro_handle_t *handle, char *buf, unsigned int type, const
update_status(status_msg, "OK\n");
}
-void write_page_ram(minipro_handle_t *handle, char *buf, unsigned int type, const char *name, int size) {
+void write_page_ram(minipro_handle_t *handle, unsigned char *buf, unsigned int type, const char *name, int size) {
char status_msg[24];
sprintf(status_msg, "Writing %s... ", name);
@@ -183,7 +183,7 @@ void read_page_file(minipro_handle_t *handle, const char *filename, unsigned int
PERROR("Couldn't open file for writing");
}
- char *buf = malloc(size);
+ unsigned char *buf = malloc(size);
if(!buf) {
ERROR("Can't malloc");
}
@@ -201,7 +201,7 @@ void write_page_file(minipro_handle_t *handle, const char *filename, unsigned in
PERROR("Couldn't open file for reading");
}
- char *buf = malloc(size);
+ unsigned char *buf = malloc(size);
if(!buf) {
ERROR("Can't malloc");
}
@@ -223,7 +223,7 @@ void read_fuses(minipro_handle_t *handle, const char *filename, fuse_decl_t *fus
minipro_begin_transaction(handle);
int i, d;
char data_length = 0, opcode = fuses[0].minipro_cmd;
- char buf[11];
+ unsigned char buf[11];
for(i = 0; fuses[i].name; i++) {
data_length += fuses[i].length;
if(fuses[i].minipro_cmd < opcode) {
@@ -260,7 +260,7 @@ void write_fuses(minipro_handle_t *handle, const char *filename, fuse_decl_t *fu
minipro_begin_transaction(handle);
int i, d;
char data_length = 0, opcode = fuses[0].minipro_cmd;
- char buf[11];
+ unsigned char buf[11];
for(i = 0; fuses[i].name; i++) {
data_length += fuses[i].length;
if(fuses[i].minipro_cmd < opcode) {
@@ -294,14 +294,14 @@ void verify_page_file(minipro_handle_t *handle, const char *filename, unsigned i
/* Loading file */
int file_size = get_file_size(filename);
- char *file_data = malloc(file_size);
+ unsigned char *file_data = malloc(file_size);
fread(file_data, 1, file_size, file);
fclose(file);
minipro_begin_transaction(handle);
/* Downloading data from chip*/
- char *chip_data = malloc(size);
+ unsigned char *chip_data = malloc(size);
read_page_ram(handle, chip_data, type, name, size);
unsigned char c1, c2;
int idx = compare_memory(file_data, chip_data, file_size, &c1, &c2);
diff --git a/minipro-query-db.c b/minipro-query-db.c
index f1c20cf..75c3ed6 100644
--- a/minipro-query-db.c
+++ b/minipro-query-db.c
@@ -38,7 +38,7 @@ void print_device_info(device_t *device) {
}
printf("\n");
- char package_details[4];
+ unsigned char package_details[4];
format_int(package_details, device->package_details, 4, MP_LITTLE_ENDIAN);
/* Package info */
printf("Package: ");
diff --git a/minipro.c b/minipro.c
index 47a2e7e..6fd051b 100644
--- a/minipro.c
+++ b/minipro.c
@@ -1,20 +1,26 @@
#include <libusb.h>
#include <stdlib.h>
#include <string.h>
+#ifdef __APPLE__
+#include <stdio.h>
+#else
#include <malloc.h>
+#endif
#include "minipro.h"
#include "byte_utils.h"
#include "error.h"
minipro_handle_t *minipro_open(device_t *device) {
+ int ret;
minipro_handle_t *handle = malloc(sizeof(minipro_handle_t));
if(handle == NULL) {
ERROR("Couldn't malloc");
}
- if(libusb_init(&(handle->ctx)) < 0) {
+ ret = libusb_init(&(handle->ctx));
+ if(ret < 0) {
free(handle);
- ERROR("Error initializing libusb");
+ ERROR2("Error initializing libusb: %s", libusb_strerror(ret));
}
handle->usb_handle = libusb_open_device_with_vid_pid(handle->ctx, 0x04d8, 0xe11c);
@@ -35,7 +41,7 @@ void minipro_close(minipro_handle_t *handle) {
unsigned char msg[MAX_WRITE_BUFFER_SIZE];
-static void msg_init(char *out_buf, char cmd, device_t *device) {
+static void msg_init(unsigned char *out_buf, unsigned char cmd, device_t *device) {
out_buf[0] = cmd;
out_buf[1] = device->protocol_id;
out_buf[2] = device->variant;
@@ -49,19 +55,25 @@ static void msg_init(char *out_buf, char cmd, device_t *device) {
}
-static unsigned int msg_transfer(minipro_handle_t *handle, char *buf, unsigned int length, int direction) {
+static unsigned int msg_transfer(minipro_handle_t *handle, unsigned char *buf, int length, int direction) {
int bytes_transferred;
- libusb_bulk_transfer(handle->usb_handle, (1 | direction), buf, length, &bytes_transferred, 0);
+ int ret;
+ ret = libusb_claim_interface(handle->usb_handle, 0);
+ if(ret != 0) ERROR2("IO error: claim_interface: %s\n", libusb_strerror(ret));
+ ret = libusb_bulk_transfer(handle->usb_handle, (1 | direction), buf, length, &bytes_transferred, 0);
+ if(ret != 0) ERROR2("IO error: bulk_transfer: %s\n", libusb_strerror(ret));
+ ret = libusb_release_interface(handle->usb_handle, 0);
+ if(ret != 0) ERROR2("IO error: release_interface: %s\n", libusb_strerror(ret));
if(bytes_transferred != length) ERROR2("IO error: expected %d bytes but %d bytes transferred\n", length, bytes_transferred);
return bytes_transferred;
}
#ifndef TEST
-static unsigned int msg_send(minipro_handle_t *handle, char *buf, unsigned int length) {
+static unsigned int msg_send(minipro_handle_t *handle, unsigned char *buf, int length) {
return msg_transfer(handle, buf, length, LIBUSB_ENDPOINT_OUT);
}
-static unsigned int msg_recv(minipro_handle_t *handle, char *buf, unsigned int length) {
+static unsigned int msg_recv(minipro_handle_t *handle, unsigned char *buf, int length) {
return msg_transfer(handle, buf, length, LIBUSB_ENDPOINT_IN);
}
#endif
@@ -79,7 +91,7 @@ void minipro_end_transaction(minipro_handle_t *handle) {
}
int minipro_get_status(minipro_handle_t *handle) {
- char buf[32];
+ unsigned char buf[32];
msg_init(msg, MP_REQUEST_STATUS1_MSG2, handle->device);
msg_send(handle, msg, 5);
msg_recv(handle, buf, 32);
@@ -91,7 +103,7 @@ int minipro_get_status(minipro_handle_t *handle) {
return(load_int(buf, 2, MP_LITTLE_ENDIAN));
}
-void minipro_read_block(minipro_handle_t *handle, unsigned int type, unsigned int addr, char *buf) {
+void minipro_read_block(minipro_handle_t *handle, unsigned int type, unsigned int addr, unsigned char *buf) {
msg_init(msg, type, handle->device);
format_int(&(msg[2]), handle->device->read_buffer_size, 2, MP_LITTLE_ENDIAN);
format_int(&(msg[4]), addr, 3, MP_LITTLE_ENDIAN);
@@ -99,7 +111,7 @@ void minipro_read_block(minipro_handle_t *handle, unsigned int type, unsigned in
msg_recv(handle, buf, handle->device->read_buffer_size);
}
-void minipro_write_block(minipro_handle_t *handle, unsigned int type, unsigned int addr, char *buf) {
+void minipro_write_block(minipro_handle_t *handle, unsigned int type, unsigned int addr, unsigned char *buf) {
msg_init(msg, type, handle->device);
format_int(&(msg[2]), handle->device->write_buffer_size, 2, MP_LITTLE_ENDIAN);
format_int(&(msg[4]), addr, 3, MP_LITTLE_ENDIAN);
@@ -116,7 +128,7 @@ int minipro_get_chip_id(minipro_handle_t *handle) {
return(load_int(&(msg[2]), handle->device->chip_id_bytes_count, MP_BIG_ENDIAN));
}
-void minipro_read_fuses(minipro_handle_t *handle, unsigned int type, unsigned int length, char *buf) {
+void minipro_read_fuses(minipro_handle_t *handle, unsigned int type, unsigned int length, unsigned char *buf) {
msg_init(msg, type, handle->device);
msg[2]=(type==18 && length==4)?2:1; // note that PICs with 1 config word will show length==2
msg[5]=0x10;
@@ -125,7 +137,7 @@ void minipro_read_fuses(minipro_handle_t *handle, unsigned int type, unsigned in
memcpy(buf, &(msg[7]), length);
}
-void minipro_write_fuses(minipro_handle_t *handle, unsigned int type, unsigned int length, char *buf) {
+void minipro_write_fuses(minipro_handle_t *handle, unsigned int type, unsigned int length, unsigned char *buf) {
// Perform actual writing
switch(type & 0xf0) {
case 0x10:
@@ -159,8 +171,8 @@ void minipro_write_fuses(minipro_handle_t *handle, unsigned int type, unsigned i
}
}
-int minipro_get_system_info(minipro_handle_t *handle, minipro_system_info_t *out) {
- char buf[40];
+void minipro_get_system_info(minipro_handle_t *handle, minipro_system_info_t *out) {
+ unsigned char buf[40];
memset(msg, 0x0, 5);
msg[0] = MP_GET_SYSTEM_INFO;
msg_send(handle, msg, 5);
@@ -196,7 +208,7 @@ int minipro_get_system_info(minipro_handle_t *handle, minipro_system_info_t *out
}
void minipro_prepare_writing(minipro_handle_t *handle) {
- char buf[10];
+ unsigned char buf[10];
msg_init(msg, MP_PREPARE_WRITING, handle->device);
format_int(&(msg[2]), 0x03, 2, MP_LITTLE_ENDIAN);
msg[2] = handle->device->write_unlock;
diff --git a/minipro.h b/minipro.h
index dda9557..b343970 100644
--- a/minipro.h
+++ b/minipro.h
@@ -48,8 +48,12 @@ void minipro_close(minipro_handle_t *handle);
void minipro_begin_transaction(minipro_handle_t *handle);
void minipro_end_transaction(minipro_handle_t *handle);
int minipro_get_status(minipro_handle_t *handle);
-void minipro_read_block(minipro_handle_t *handle, unsigned int type, unsigned int addr, char *buf);
-void minipro_write_block(minipro_handle_t *handle, unsigned int type, unsigned int addr, char *buf);
+void minipro_read_block(minipro_handle_t *handle, unsigned int type, unsigned int addr, unsigned char *buf);
+void minipro_write_block(minipro_handle_t *handle, unsigned int type, unsigned int addr, unsigned char *buf);
int minipro_get_chip_id(minipro_handle_t *handle);
+void minipro_read_fuses(minipro_handle_t *handle, unsigned int type, unsigned int length, unsigned char *buf);
+void minipro_write_fuses(minipro_handle_t *handle, unsigned int type, unsigned int length, unsigned char *buf);
+void minipro_prepare_writing(minipro_handle_t *handle);
+void minipro_get_system_info(minipro_handle_t *handle, minipro_system_info_t *out);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment