diff options
-rw-r--r-- | cctools/lib/Makefile.am | 5 | ||||
-rw-r--r-- | cctools/lib/cc-bitbang.c | 32 | ||||
-rw-r--r-- | cctools/lib/cccp.c | 112 | ||||
-rw-r--r-- | cctools/lib/cccp.h | 41 | ||||
-rw-r--r-- | cctools/lib/cp-usb.c | 157 | ||||
-rw-r--r-- | cctools/lib/cp-usb.h | 36 |
6 files changed, 3 insertions, 380 deletions
diff --git a/cctools/lib/Makefile.am b/cctools/lib/Makefile.am index 4d9ded3a..251ef6e6 100644 --- a/cctools/lib/Makefile.am +++ b/cctools/lib/Makefile.am @@ -5,6 +5,7 @@ AM_CFLAGS=$(WARN_CFLAGS) $(LIBUSB_CFLAGS) libcc_a_SOURCES = \ ccdbg-command.c \ ccdbg-debug.c \ + ccdbg-debug.h \ ccdbg-flash.c \ ccdbg.h \ ccdbg-hex.c \ @@ -17,5 +18,5 @@ libcc_a_SOURCES = \ cc-usb.h \ cc-bitbang.c \ cc-bitbang.h \ - cp-usb.c \ - cp-usb-async.c + cp-usb-async.c \ + cp-usb-async.h diff --git a/cctools/lib/cc-bitbang.c b/cctools/lib/cc-bitbang.c index 1d3ba476..a5d15739 100644 --- a/cctools/lib/cc-bitbang.c +++ b/cctools/lib/cc-bitbang.c @@ -22,20 +22,10 @@ #include "ccdbg-debug.h" #include "cc-bitbang.h" -#define CP_USB_ASYNC - -#ifdef CP_USB_ASYNC #include "cp-usb-async.h" -#else -#include "cp-usb.h" -#endif struct cc_bitbang { -#ifdef CP_USB_ASYNC struct cp_usb_async *cp_async; -#else - struct cp_usb *cp; -#endif }; static uint32_t cc_clock_us = CC_CLOCK_US; @@ -77,30 +67,18 @@ cc_bitbang_open(void) perror("calloc"); return NULL; } -#ifdef CP_USB_ASYNC bb->cp_async = cp_usb_async_open(); if (!bb->cp_async) { free (bb); return NULL; } -#else - bb->cp = cp_usb_open (); - if (!bb->cp) { - free (bb); - return NULL; - } -#endif return bb; } void cc_bitbang_close(struct cc_bitbang *bb) { -#ifdef CP_USB_ASYNC cp_usb_async_close(bb->cp_async); -#else - cp_usb_close(bb->cp); -#endif free (bb); } @@ -140,30 +118,20 @@ cc_bitbang_reset(struct cc_bitbang *bb) int cc_bitbang_write(struct cc_bitbang *bb, uint8_t mask, uint8_t value) { -#ifdef CP_USB_ASYNC cp_usb_async_write(bb->cp_async, mask, value); -#else - cp_usb_write(bb->cp, mask, value); -#endif return 0; } void cc_bitbang_read(struct cc_bitbang *bb, uint8_t *valuep) { -#ifdef CP_USB_ASYNC cp_usb_async_read(bb->cp_async, valuep); -#else - *valuep = cp_usb_read(bb->cp); -#endif } void cc_bitbang_sync(struct cc_bitbang *bb) { -#ifdef CP_USB_ASYNC cp_usb_async_sync(bb->cp_async); -#endif } static char diff --git a/cctools/lib/cccp.c b/cctools/lib/cccp.c deleted file mode 100644 index 34e866e8..00000000 --- a/cctools/lib/cccp.c +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Copyright © 2008 Keith Packard <keithp@keithp.com> - * - * 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; either version 2 of the License, or - * (at your option) any later version. - * - * 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 "ccdbg.h" - -static void -say(char *name, uint8_t bits) -{ - printf("%s: ", name); - if (bits & CC_RESET_N) - printf ("R "); - else - printf (". "); - if (bits & CC_CLOCK) - printf ("C "); - else - printf (". "); - if (bits & CC_DATA) - printf ("D\n"); - else - printf (".\n"); -} - -static void -_cccp_write(struct ccdbg *dbg, uint8_t mask, uint8_t value) -{ - uint16_t set; - int ret; - - set = (mask) | (value << 8); - dbg->debug_data = (dbg->debug_data & ~mask) | (value & mask); - ret = ioctl(dbg->fd, CP2101_IOCTL_GPIOSET, &set); - if (ret < 0) - perror("CP2101_IOCTL_GPIOSET"); -} - -void -cccp_write(struct ccdbg *dbg, uint8_t mask, uint8_t value) -{ - _cccp_write(dbg, mask, value); -// say("w", dbg->debug_data); -} - -uint8_t -cccp_read_all(struct ccdbg *dbg) -{ - int ret; - uint8_t get; - ret = ioctl(dbg->fd, CP2101_IOCTL_GPIOGET, &get); - if (ret < 0) { - perror("CP2101_IOCTL_GPIOGET"); - get = 0; - } - return get; -} - -uint8_t -cccp_read(struct ccdbg *dbg, uint8_t mask) -{ - uint8_t pull_up; - uint8_t get; - - /* tri-state the bits of interest */ - pull_up = (~dbg->debug_data) & mask; - if (pull_up) - _cccp_write(dbg, pull_up, pull_up); - get = cccp_read_all(dbg); - say("\t\tr", get); - return get & mask; -} - -void -cccp_init(struct ccdbg *dbg) -{ - /* set all of the GPIOs to a known state */ - cccp_write(dbg, 0xf, 0xf); -} - -void -cccp_fini(struct ccdbg *dbg) -{ - /* set all of the GPIOs to a known state */ - cccp_write(dbg, 0xf, 0xf); - dbg->clock = 1; -} - -cccp_open() -{ - dbg->fd = open("/dev/ttyUSB0", 2); - if (dbg->fd < 0) { - perror(file); - free(dbg); - return NULL; - } - cccp_init(dbg); - cccp_write(dbg, CC_CLOCK, CC_CLOCK); -} diff --git a/cctools/lib/cccp.h b/cctools/lib/cccp.h deleted file mode 100644 index eecdbb49..00000000 --- a/cctools/lib/cccp.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright © 2008 Keith Packard <keithp@keithp.com> - * - * 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; either version 2 of the License, or - * (at your option) any later version. - * - * 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. - */ - -/* - * Interface for using a CP2103 to talk to a CC1111 - */ - -#ifndef _CCCP_H_ -#define _CCCP_H_ - -void -cccp_write(struct ccdbg *dbg, uint8_t mask, uint8_t value); - -uint8_t -cccp_read_all(struct ccdbg *dbg); - -uint8_t -cccp_read(struct ccdbg *dbg, uint8_t mask); - -void -cccp_init(struct ccdbg *dbg); - -void -cccp_fini(struct ccdbg *dbg); - -#endif /* _CCCP_H_ */ diff --git a/cctools/lib/cp-usb.c b/cctools/lib/cp-usb.c deleted file mode 100644 index 530848db..00000000 --- a/cctools/lib/cp-usb.c +++ /dev/null @@ -1,157 +0,0 @@ -/* - * Copyright © 2008 Keith Packard <keithp@keithp.com> - * - * 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; either version 2 of the License, or - * (at your option) any later version. - * - * 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. - */ - -/* - * libusb interface to the GPIO pins on a CP2103. - * - * Various magic constants came from the cp210x driver published by silabs. - */ - -#include "cp-usb.h" -#include <stdio.h> -#include <errno.h> -#include <libusb.h> - -struct cp_usb { - usb_dev_handle *usb_dev; - uint8_t gpio; -}; - -#define CP2101_UART 0x00 -#define UART_ENABLE 0x0001 -#define UART_DISABLE 0x0000 -#define REQTYPE_HOST_TO_DEVICE 0x41 -#define REQTYPE_DEVICE_TO_HOST 0xc1 - -static int -cp_usb_gpio_get(struct cp_usb *cp, uint8_t *gpio_get) -{ - return usb_control_msg(cp->usb_dev, /* dev */ - 0xc0, /* request */ - 0xff, /* requesttype */ - 0x00c2, /* value */ - 0, /* index */ - (char *) gpio_get, /* bytes */ - 1, /* size */ - 300); /* timeout */ -} - -static int -cp_usb_gpio_set(struct cp_usb *cp, uint8_t mask, uint8_t value) -{ - uint16_t gpio_set = ((uint16_t) value << 8) | mask; - - return usb_control_msg(cp->usb_dev, /* dev */ - 0x40, /* request */ - 0xff, /* requesttype */ - 0x37e1, /* value */ - gpio_set, /* index */ - NULL, /* bytes */ - 0, /* size */ - 300); /* timeout */ -} - -static int -cp_usb_uart_enable_disable(struct cp_usb *cp, uint16_t enable) -{ - return usb_control_msg(cp->usb_dev, - CP2101_UART, - REQTYPE_HOST_TO_DEVICE, - enable, - 0, - NULL, - 0, - 300); -} - -struct cp_usb * -cp_usb_open(void) -{ - struct cp_usb *cp; - usb_dev_handle *dev_handle; - struct usb_device *dev = NULL; - struct usb_bus *bus, *busses; - int interface; - int ret; - uint8_t gpio; - - usb_init(); - usb_find_busses(); - usb_find_devices(); - - busses = usb_get_busses(); - for (bus = busses; bus; bus = bus->next) { - for (dev = bus->devices; dev; dev = dev->next) { - if (dev->descriptor.idVendor == 0x10c4 && - dev->descriptor.idProduct == 0xea60) - break; - } - if (dev) - break; - } - if (!dev){ - perror("No CP2103 found"); - return NULL; - } - cp = calloc(sizeof(struct cp_usb), 1); - interface = 0; - dev_handle = usb_open(dev); - usb_detach_kernel_driver_np(dev_handle, interface); - usb_claim_interface(dev_handle, interface); - cp->usb_dev = dev_handle; - ret = cp_usb_uart_enable_disable(cp, UART_DISABLE); - cp->gpio = 0xf; - ret = cp_usb_gpio_set(cp, 0xf, cp->gpio); - ret = cp_usb_gpio_get(cp, &gpio); - return cp; -} - -void -cp_usb_close(struct cp_usb *cp) -{ - cp_usb_uart_enable_disable(cp, UART_DISABLE); - usb_close(cp->usb_dev); - free(cp); -} - -void -cp_usb_write(struct cp_usb *cp, uint8_t mask, uint8_t value) -{ - uint8_t new_gpio; - int ret; - - new_gpio = (cp->gpio & ~mask) | (value & mask); - if (new_gpio != cp->gpio) { - ret = cp_usb_gpio_set(cp, new_gpio ^ cp->gpio, new_gpio); - if (ret < 0) - perror("gpio_set"); - cp->gpio = new_gpio; - } -} - -uint8_t -cp_usb_read(struct cp_usb *cp) -{ - int ret; - uint8_t gpio; - - ret = cp_usb_gpio_get(cp, &gpio); - if (ret < 0) - perror("gpio_get"); - return gpio; -} diff --git a/cctools/lib/cp-usb.h b/cctools/lib/cp-usb.h deleted file mode 100644 index 3e5f25ff..00000000 --- a/cctools/lib/cp-usb.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright © 2008 Keith Packard <keithp@keithp.com> - * - * 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; either version 2 of the License, or - * (at your option) any later version. - * - * 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. - */ - -#ifndef _CP_USB_H_ -#define _CP_USB_H_ -#include <usb.h> - -struct cp_usb * -cp_usb_open(void); - -void -cp_usb_close(struct cp_usb *cp); - -void -cp_usb_write(struct cp_usb *cp, uint8_t mask, uint8_t value); - -uint8_t -cp_usb_read(struct cp_usb *cp); - - -#endif |