[tuxdroid-svn] r1204 - firmware/tuxup/trunk
Status: Beta
Brought to you by:
ks156
From: Paul_R <c2m...@c2...> - 2008-05-28 11:01:17
|
Author: Paul_R Date: 2008-05-28 13:01:17 +0200 (Wed, 28 May 2008) New Revision: 1204 Modified: firmware/tuxup/trunk/Makefile firmware/tuxup/trunk/bootloader.c firmware/tuxup/trunk/bootloader.h firmware/tuxup/trunk/main.c Log: * Added the compatibility with the HID interface. Modified: firmware/tuxup/trunk/Makefile =================================================================== --- firmware/tuxup/trunk/Makefile 2008-05-28 11:00:43 UTC (rev 1203) +++ firmware/tuxup/trunk/Makefile 2008-05-28 11:01:17 UTC (rev 1204) @@ -11,14 +11,15 @@ LIB = -lusb ## Objects that must be built in order to link -FILES = main.c usb-connection.c bootloader.c +FILES = main.c usb-connection.c bootloader.c tux_hid_unix.c tux_misc.c ## Build all: $(TARGET) ## Compile and link -tuxup: main.c bootloader.c bootloader.h usb-connection.c usb-connection.h tux-api.h version.h common/commands.h - ${CC} ${LIB} ${CFLAGS} -o tuxup main.c bootloader.c usb-connection.c +tuxup: main.c bootloader.c bootloader.h usb-connection.c usb-connection.h tux_hid_unix.c tux_hid_unix.h tux_misc.c tux_misc.h tux-api.h version.h common/commands.h + ${CC} ${LIB} ${CFLAGS} -o tuxup main.c bootloader.c usb-connection.c \ + tux_hid_unix.c tux_misc.c clean : -rm -f $(TARGET) *.o Modified: firmware/tuxup/trunk/bootloader.c =================================================================== --- firmware/tuxup/trunk/bootloader.c 2008-05-28 11:00:43 UTC (rev 1203) +++ firmware/tuxup/trunk/bootloader.c 2008-05-28 11:01:17 UTC (rev 1204) @@ -26,22 +26,21 @@ * @brief Boot loader that sends data to the dongle which forwards it on I2C * to reprogram all CPU's. */ - #include <stdio.h> #include <stdint.h> #include <unistd.h> -//#include <stdlib.h> -//#include <errno.h> +#include <stdlib.h> +#include <time.h> #include <string.h> -//#include <fcntl.h> -//#include <sys/types.h> - #include "usb-connection.h" +#include "tux_hid_unix.h" #include "tux-api.h" /* Whether to display verbose messages. */ extern int verbose; +bool HID; + /* Debug commands */ //#define keybreak() {puts("Press return to read feedback"); getchar();} //#define keybreak() {usleep(5000);} @@ -51,16 +50,24 @@ #define TRUE 1 #define FALSE 0 +#define USB_TIMEOUT 5 +#define BOOT_INIT_ACK 10 +#define BOOT_EXIT_ACK 2 + /* USB bootloader commands */ #define BOOT_INIT 1 #define BOOT_FILLPAGE 2 #define BOOT_EXIT 3 +static bool wait_status(uint8_t value, int timeout); + typedef uint32_t FILE_Addr_t; typedef uint32_t FILE_SegmentLen_t; typedef unsigned FILE_LineNum_t; typedef uint8_t FILE_ParsedLen_t; +static uint8_t counter; + static enum mem_type_t mem_type; typedef struct @@ -295,12 +302,27 @@ /* set the last bit to 1 to indicate eeprom type to the bootloader */ data_buffer[2] |= 0x80; } - ret = usb_send_commands(parser->dev_handle, data_buffer, 36); + if (HID) + { + ret = tux_hid_write(36, data_buffer); + } + else + { + ret = usb_send_commands(parser->dev_handle, data_buffer, 36); + } #if (PRINT_DATA) printf("Status of the first packet sent: %d\n", ret); #endif - if (ret != 36) - return FALSE; + if (HID) + { + if (!ret) + return FALSE; + } + else + { + if (ret != 36) + return FALSE; + } /* * Send second packet @@ -308,33 +330,75 @@ for (i = 2; i < 34; i++) data_buffer[i] = parser->segmentData[idx++]; keybreak(); - ret = usb_send_commands(parser->dev_handle, data_buffer, 34); + if (HID) + { + ret = tux_hid_write(34, data_buffer); + } + else + { + ret = usb_send_commands(parser->dev_handle, data_buffer, 34); + } #if (PRINT_DATA) printf("Status of the second packet sent: %d\n", ret); #endif - if (ret != 34) - return FALSE; + if (HID) + { + if (!ret) + return FALSE; + } + else + { + if (ret != 34) + return FALSE; + } keybreak(); /* * Bootlader status command and result */ - ret = usb_get_commands(parser->dev_handle, data_buffer, 5); + if (HID) + { + ret = (wait_status(counter, USB_TIMEOUT)); + tux_hid_read(5, data_buffer); + counter ++; + } + else + { + ret = usb_get_commands(parser->dev_handle, data_buffer, 5); + } #if (PRINT_DATA) printf("Status of feedback from bootloader: %x\n", ret); #endif - if ((ret == 5) && (data_buffer[0] == 0xF0) && (data_buffer[1] == 0)) + if (HID) { - printf("."); - fflush (stdout); - return TRUE; + if ((ret) && (data_buffer[0] == 0xF0) && (data_buffer[1] == 0)) + { + printf("."); + fflush (stdout); + return TRUE; + } + else + { + fprintf(stderr, + "Bootloading failed, program aborted at dongle reply.\n"); + exit(1); + } } else { - fprintf(stderr, - "Bootloading failed, program aborted at dongle reply.\n"); - exit(1); + if ((ret == 5) && (data_buffer[0] == 0xF0) && (data_buffer[1] == 0)) + { + printf("."); + fflush (stdout); + return TRUE; + } + else + { + fprintf(stderr, + "Bootloading failed, program aborted at dongle reply.\n"); + exit(1); + } } } @@ -616,13 +680,35 @@ data_buffer[2] = cpu_address; data_buffer[3] = page_size; data_buffer[4] = packet_total; + + counter = 0; + + if (HID) + { + ret = tux_hid_write(5, data_buffer); + sleep(1); + } + else + { + ret = usb_send_commands(dev_h, data_buffer, 5); + } - ret = usb_send_commands(dev_h, data_buffer, 5); + if (HID) + { + if (!wait_status(BOOT_INIT_ACK, USB_TIMEOUT) || !ret) + { + fprintf(stderr, "Initialization failed\n"); + return FALSE; + } + } + else + { #if (PRINT_DATA) - printf("Boot init status: %x\n", ret); + printf("Boot init status: %x\n", ret); #endif - if (ret != 5) - return FALSE; /* initialization failed */ + if (ret != 5) + return FALSE; /* initialization failed */ + } /* Bootloader: parse hex file and send data */ if (FILE_ParseFile(dev_h, filename)) @@ -635,7 +721,44 @@ data_buffer[3] = 0; data_buffer[4] = 0; - ret = usb_send_commands(dev_h, data_buffer, 5); - ret = usb_get_commands(dev_h, data_buffer, 5); + if (HID) + { + tux_hid_write(5, data_buffer); + tux_hid_read(5, data_buffer); + if (!wait_status(BOOT_EXIT_ACK, USB_TIMEOUT)) + { + fprintf(stderr, "Bootloader exit failed \n"); + return FALSE; + } + } + else + { + ret = usb_send_commands(dev_h, data_buffer, 5); + ret = usb_get_commands(dev_h, data_buffer, 5); + } return rc; } +/* + * Wait a specific ACK. + * This function wait for a specific value of the second parameter of the + * bootloader ACK. + */ +static bool wait_status(uint8_t value, int timeout) +{ + uint8_t data_buffer[5]; + uint8_t sttime = time(NULL); + uint8_t edtime = 0; + + tux_hid_read(5, data_buffer); + while (data_buffer[2] != value || data_buffer[0] != 0xF0) + { + edtime = time(NULL); + if (difftime(edtime, sttime) > timeout) + { + return 0; + } + tux_hid_read(5, data_buffer); + usleep(5000); + } + return 1; +} Modified: firmware/tuxup/trunk/bootloader.h =================================================================== --- firmware/tuxup/trunk/bootloader.h 2008-05-28 11:00:43 UTC (rev 1203) +++ firmware/tuxup/trunk/bootloader.h 2008-05-28 11:01:17 UTC (rev 1204) @@ -21,6 +21,8 @@ #ifndef bootloader_h #define bootloader_h +#include <stdbool.h> +extern bool HID; int bootload(usb_dev_handle * dev_h, uint8_t cpu_address, uint8_t mem_type, const char *filename); Modified: firmware/tuxup/trunk/main.c =================================================================== --- firmware/tuxup/trunk/main.c 2008-05-28 11:00:43 UTC (rev 1203) +++ firmware/tuxup/trunk/main.c 2008-05-28 11:01:17 UTC (rev 1204) @@ -19,18 +19,22 @@ /* $Id$ */ +#include <stdlib.h> #include <stdio.h> #include <stdint.h> #include <getopt.h> +#include <unistd.h> #include <time.h> #include <string.h> #include <linux/limits.h> #include "tux-api.h" #include "usb-connection.h" +#include "tux_hid_unix.h" #include "bootloader.h" #include "version.h" #include "common/defines.h" +#include "tux_misc.h" #define countof(X) ( (size_t) ( sizeof(X)/sizeof*(X) ) ) @@ -110,21 +114,38 @@ if (usb_connected) return; - /* detect Tux on USB */ - for (;;) + /* First, try to found a HID device */ + if (!(tux_hid_capture(TUX_VID, TUX_PID))) { - device = usb_find_tux(); - if (device != NULL || wait == 0) - break; + /* Unable to capture the device : + * Try with the libusb */ + for (;;) + { + device = usb_find_tux(); + if (device != NULL || wait == 0) + break; - sleep(1); - wait--; + sleep(1); + wait--; + } + + if (device == NULL) + { + fprintf(stderr, "The dongle was not found, now exiting.\n"); + exit(1); + } + else + { + printf("Dongle found, using LibUSB \n"); + /* The dongle is a libusb device */ + HID = 0; + } } - - if (device == NULL) + else { - fprintf(stderr, "The dongle was not found, now exiting.\n"); - exit(1); + printf("HID Dongle found \n"); + /* The dongle is a HID device */ + HID = 1; } if (verbose) @@ -133,18 +154,21 @@ /* Check if we have the old firmware that requires entering * bootloader mode manually, exits with a message that explains what * to do in such a case. */ - if (device->descriptor.bcdDevice < 0x030) + if (!HID) { - fprintf(stderr, msg_old_firmware); - exit(1); + if (device->descriptor.bcdDevice < 0x030) + { + fprintf(stderr, msg_old_firmware); + exit(1); + } + + /* open USB device */ + if ((dev_h = usb_open_tux(device)) == NULL) + { + fprintf(stderr, "USB DEVICE INIT ERROR \n"); + exit(1); + } } - - /* open USB device */ - if ((dev_h = usb_open_tux(device)) == NULL) - { - fprintf(stderr, "USB DEVICE INIT ERROR \n"); - exit(1); - } if (verbose) printf("Interface configured \n\n"); usb_connected = 1; @@ -156,8 +180,15 @@ return; if (verbose) printf("Closing interface ...\n"); - usb_release_interface(dev_h, USB_COMMAND); - usb_close(dev_h); + if (HID) + { + tux_hid_release(); + } + else + { + usb_release_interface(dev_h, USB_COMMAND); + usb_close(dev_h); + } if (verbose) printf(" ... interface closed \n"); usb_connected = 0; @@ -369,20 +400,28 @@ } fux_connect(); /* Enter bootloader mode. */ - ret = usb_send_commands(dev_h, send_data, 5); - if (ret == 5) + if (HID) { - if (verbose) - fprintf(stdout, "Switched to bootloader mode.\n"); + tux_hid_write(5, send_data); sleep(1); } else { - fprintf(stderr, "Switching to bootloader mode failed.\n"); - return 1; + ret = usb_send_commands(dev_h, send_data, 5); + if (ret == 5) + { + if (verbose) + fprintf(stdout, "Switched to bootloader mode.\n"); + sleep(1); + } + + else + { + fprintf(stderr, "Switching to bootloader mode failed.\n"); + return 1; + } } } - ret = system("dfu-programmer at89c5130 erase"); if (ret) { @@ -635,14 +674,17 @@ if (!pretend) { uint8_t send_data[5] = { 0x01, 0x01, 0x00, 0x00, 0xFE }; - ret = usb_send_commands(dev_h, send_data, 5); + if (HID) + { + tux_hid_write(5, send_data); + } + else + { + ret = usb_send_commands(dev_h, send_data, 5); + } fux_disconnect(); } - /* In case of error of some sort, we return it. */ - if (ret) - return ret; - /* Print time elapsed for programming. */ end_time = time(NULL); if (!pretend) |