X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=lib%2Fccdbg-io.c;h=3606c57cf960461d9a19ecfb879e070385c097bb;hp=2947678561d4ac964e99e988fd52d39efe06cbdb;hb=cc0495b7028f4b1189a00707d828a68534d1dea2;hpb=9025eb792861930e6af918d2727c4f5d97a69936 diff --git a/lib/ccdbg-io.c b/lib/ccdbg-io.c index 29476785..3606c57c 100644 --- a/lib/ccdbg-io.c +++ b/lib/ccdbg-io.c @@ -18,16 +18,41 @@ #include "ccdbg.h" #include +#ifdef CP_USB_ASYNC +#include "cp-usb-async.h" +#else +#include "cp-usb.h" +#endif + +static uint32_t cc_clock_us = CC_CLOCK_US; +static uint32_t cc_reset_us = CC_RESET_US; + +void +ccdbg_set_clock(uint32_t us) +{ + cc_clock_us = us; +} void ccdbg_half_clock(struct ccdbg *dbg) { struct timespec req, rem; - req.tv_sec = (CC_CLOCK_US / 2) / 1000000; - req.tv_nsec = ((CC_CLOCK_US / 2) % 1000000) * 1000; + req.tv_sec = (cc_clock_us / 2) / 1000000; + req.tv_nsec = ((cc_clock_us / 2) % 1000000) * 1000; nanosleep(&req, &rem); } +void +ccdbg_wait_reset(struct ccdbg *dbg) +{ + struct timespec req, rem; + + ccdbg_sync_io(dbg); + req.tv_sec = (cc_reset_us) / 1000000; + req.tv_nsec = ((cc_reset_us) % 1000000) * 1000; + nanosleep(&req, &rem); +} + struct ccdbg * ccdbg_open(void) { @@ -38,31 +63,29 @@ ccdbg_open(void) perror("calloc"); return NULL; } - dbg->clock = 1; -#ifdef USE_KERNEL - dbg->fd = open("/dev/ttyUSB0", 2); - if (dbg->fd < 0) { - perror(file); - free(dbg); +#ifdef CP_USB_ASYNC + dbg->cp_async = cp_usb_async_open(); + if (!dbg->cp_async) { + free (dbg); return NULL; } - cccp_init(dbg); - cccp_write(dbg, CC_CLOCK, CC_CLOCK); #else - cp_usb_init(dbg); + dbg->cp = cp_usb_open (); + if (!dbg->cp) { + free (dbg); + return NULL; + } #endif - dbg->clock = 1; return dbg; } void ccdbg_close(struct ccdbg *dbg) { -#if USE_KERNEL - cccp_fini(dbg); - close (dbg->fd); +#ifdef CP_USB_ASYNC + cp_usb_async_close(dbg->cp_async); #else - cp_usb_fini(dbg); + cp_usb_close(dbg->cp); #endif free (dbg); } @@ -70,21 +93,29 @@ ccdbg_close(struct ccdbg *dbg) int ccdbg_write(struct ccdbg *dbg, uint8_t mask, uint8_t value) { -#if USE_KERNEL - return cccp_write(dbg, mask, value); +#ifdef CP_USB_ASYNC + cp_usb_async_write(dbg->cp_async, mask, value); #else - cp_usb_write(dbg, mask, value); - return 0; + cp_usb_write(dbg->cp, mask, value); #endif + return 0; } -uint8_t -ccdbg_read(struct ccdbg *dbg) +void +ccdbg_read(struct ccdbg *dbg, uint8_t *valuep) { -#if USE_KERNEL - return cccp_read_all(dbg); +#ifdef CP_USB_ASYNC + cp_usb_async_read(dbg->cp_async, valuep); #else - return cp_usb_read(dbg); + *valuep = cp_usb_read(dbg->cp); +#endif +} + +void +ccdbg_sync_io(struct ccdbg *dbg) +{ +#ifdef CP_USB_ASYNC + cp_usb_async_sync(dbg->cp_async); #endif } @@ -134,6 +165,7 @@ ccdbg_send_byte(struct ccdbg *dbg, uint8_t byte) if (bit == 3) ccdbg_debug(CC_DEBUG_BITBANG, "\n"); } + ccdbg_sync_io(dbg); } void @@ -143,42 +175,47 @@ ccdbg_send_bytes(struct ccdbg *dbg, uint8_t *bytes, int nbytes) ccdbg_send_byte(dbg, *bytes++); } -uint8_t -ccdbg_recv_bit(struct ccdbg *dbg, int first) +void +ccdbg_recv_bit(struct ccdbg *dbg, int first, uint8_t *bit) { uint8_t mask = first ? CC_DATA : 0; - uint8_t read; ccdbg_send(dbg, CC_CLOCK|mask|CC_RESET_N, CC_CLOCK|CC_DATA|CC_RESET_N); - read = ccdbg_read(dbg); + ccdbg_read(dbg, bit); ccdbg_send(dbg, CC_CLOCK| CC_RESET_N, CC_RESET_N); - return (read & CC_DATA) ? 1 : 0; } -uint8_t -ccdbg_recv_byte(struct ccdbg *dbg, int first) +void +ccdbg_recv_byte(struct ccdbg *dbg, int first, uint8_t *bytep) { uint8_t byte = 0; + uint8_t bits[8]; int bit; ccdbg_debug(CC_DEBUG_BITBANG, "#\n# Recv byte\n#\n"); + for (bit = 0; bit < 8; bit++) { + ccdbg_recv_bit(dbg, first, &bits[bit]); + first = 0; + } + ccdbg_sync_io(dbg); for (bit = 0; bit < 8; bit++) { byte = byte << 1; - byte |= ccdbg_recv_bit(dbg, first); + byte |= (bits[bit] & CC_DATA) ? 1 : 0; + ccdbg_print("#\t%c %c %c\n", CC_DATA, bits[bit]); if (bit == 3) ccdbg_debug(CC_DEBUG_BITBANG, "\n"); - first = 0; } ccdbg_debug(CC_DEBUG_BITBANG, "#\n# Recv 0x%02x\n#\n", byte); - return byte; + *bytep = byte; } void ccdbg_recv_bytes(struct ccdbg *dbg, uint8_t *bytes, int nbytes) { + int i; int first = 1; - while (nbytes--) { - *bytes++ = ccdbg_recv_byte(dbg, first); + for (i = 0; i < nbytes; i++) { + ccdbg_recv_byte(dbg, first, &bytes[i]); first = 0; } }