Skip to content

Commit

Permalink
Merge pull request #1759 from stefanrueger/ch341a
Browse files Browse the repository at this point in the history
Make programmer ch341a libavrdude ready
  • Loading branch information
stefanrueger authored Apr 25, 2024
2 parents b4bbf30 + 9bbbe39 commit ff7ab85
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/ch341a.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@
#include <sys/time.h>

#ifdef USE_LIBUSB_1_0
static libusb_context *ctx = NULL;

static int libusb_to_errno(int result) {
switch (result) {
case LIBUSB_SUCCESS:
Expand Down Expand Up @@ -96,6 +94,9 @@ static int libusb_to_errno(int result) {
struct pdata {
libusb_device_handle *usbhandle;
int sckfreq_hz;

libusb_context *ctx;
int USB_init; // Used in ch341a_open()
};

#define PDATA(pgm) ((struct pdata *)(pgm->cookie))
Expand Down Expand Up @@ -204,13 +205,12 @@ static int ch341a_open(PROGRAMMER *pgm, const char *port) {
int pid, vid, j, r;
int errorCode = USB_ERROR_NOTFOUND;
libusb_device_handle *handle = NULL;
static int didUsbInit = 0;

pmsg_trace("ch341a_open(\"%s\")\n", port);

if(!didUsbInit) {
didUsbInit = 1;
libusb_init(&ctx);
if(!PDATA(pgm)->USB_init) {
PDATA(pgm)->USB_init = 1;
libusb_init(&PDATA(pgm)->ctx);
}

if(usbpid) {
Expand All @@ -223,7 +223,7 @@ static int ch341a_open(PROGRAMMER *pgm, const char *port) {
vid = pgm->usbvid? pgm->usbvid: CH341A_VID;

libusb_device **dev_list;
int dev_list_len = libusb_get_device_list(ctx, &dev_list);
int dev_list_len = libusb_get_device_list(PDATA(pgm)->ctx, &dev_list);

for(j = 0; j < dev_list_len; ++j) {
libusb_device *dev = dev_list[j];
Expand Down Expand Up @@ -252,7 +252,7 @@ static int ch341a_open(PROGRAMMER *pgm, const char *port) {
if((r = libusb_claim_interface(PDATA(pgm)->usbhandle, 0))) {
pmsg_error("libusb_claim_interface failed, return value %d (%s)\n", r, libusb_error_name(r));
libusb_close(PDATA(pgm)->usbhandle);
libusb_exit(ctx);
libusb_exit(PDATA(pgm)->ctx);
return -1;
}
return 0;
Expand All @@ -272,7 +272,7 @@ static void ch341a_close(PROGRAMMER *pgm) {
libusb_release_interface(PDATA(pgm)->usbhandle, 0);
libusb_close(PDATA(pgm)->usbhandle);
}
libusb_exit(ctx);
libusb_exit(PDATA(pgm)->ctx);
}


Expand Down Expand Up @@ -429,11 +429,12 @@ static int ch341a_spi_program_enable(const PROGRAMMER *pgm, const AVRPART *p) {

// Interface management
static void ch341a_setup(PROGRAMMER *pgm) {
pgm->cookie = cfg_malloc(__func__, sizeof(struct pdata));
pgm->cookie = mmt_malloc(sizeof(struct pdata));
}

static void ch341a_teardown(PROGRAMMER *pgm) {
free(pgm->cookie);
mmt_free(pgm->cookie);
pgm->cookie = NULL;
}

// Dummy functions
Expand Down Expand Up @@ -487,4 +488,4 @@ void ch341a_initpgm(PROGRAMMER * pgm) {
}
#endif // !defined(HAVE_LIBUSB_1_0)

const char ch341a_desc[] = "Driver for \"ch341a\"-type programmers";
const char ch341a_desc[] = "Programmer chip CH341A (AVR must have minimum F_CPU of 6.8 MHz)";

0 comments on commit ff7ab85

Please sign in to comment.