add bfutil and libusb-1.0
This commit is contained in:
1 parent
9bcccc5524
commit
df80f1065d
6 files changed
+2438
No files matched your search
@@ -0,0 +1,56 @@
|
||||
CC = gcc
|
||||
LD = gcc
|
||||
CFLAGS += -O0 -g3 -Wall -std=gnu99
|
||||
LIBS += -lusb-1.0
|
||||
OBJDIR = _build
|
||||
|
||||
|
||||
|
||||
ifeq ($(OS),Windows_NT)
|
||||
LIBS += -Llibusb-1.0
|
||||
BINFILE = bfutil.exe
|
||||
BINDIR := win32
|
||||
COPYDLL = cp libusb-1.0/libusb-1.0.dll $(BINDIR)/libusb-1.0.dll
|
||||
else
|
||||
BINFILE = bfutil
|
||||
COPYDLL =
|
||||
|
||||
OS := $(shell uname -s)
|
||||
ifeq ($(OS),Linux)
|
||||
ifeq ($(MBIT),32)
|
||||
BINDIR :=linux32
|
||||
CFLAGS +=-m32
|
||||
else
|
||||
LBITS := $(shell getconf LONG_BIT)
|
||||
ifeq ($(LBITS),64)
|
||||
BINDIR := linux64
|
||||
else
|
||||
BINDIR := linux32
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
ifeq ($(OS),Darwin)
|
||||
BINDIR := macos
|
||||
endif
|
||||
endif
|
||||
|
||||
BINDIR := ../binaries/$(BINDIR)
|
||||
$(info $(BINDIR))
|
||||
|
||||
all: clean $(OBJDIR) bfutil
|
||||
|
||||
.PHONY: $(OBJDIR)
|
||||
$(OBJDIR):
|
||||
@mkdir -p $(OBJDIR)
|
||||
@mkdir -p $(BINDIR)
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
$(CC) -c -o $@ $< $(CFLAGS)
|
||||
|
||||
bfutil: $(OBJDIR)/bfutil.o
|
||||
$(LD) -o $(BINDIR)/$(BINFILE) $^ $(CFLAGS) $(LIBS)
|
||||
$(COPYDLL)
|
||||
|
||||
clean:
|
||||
@rm -rf $(OBJDIR)
|
||||
@rm -f $(BINDIR)/$(BINFILE)
|
||||
@@ -0,0 +1,313 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file bfutil.c (Bluefruit Util)
|
||||
@author hathach
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
3. Neither the name of the copyright holders nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
|
||||
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "libusb-1.0/libusb.h"
|
||||
|
||||
#if defined(_WIN32)
|
||||
|
||||
#include <windows.h>
|
||||
static inline void sleep_ms(int ms)
|
||||
{
|
||||
Sleep(ms);
|
||||
}
|
||||
|
||||
|
||||
#else
|
||||
|
||||
#include <unistd.h>
|
||||
static inline void sleep_ms(int ms)
|
||||
{
|
||||
usleep(ms*1000);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* MACRO TYPEDEF CONSTANT ENUM
|
||||
*------------------------------------------------------------------*/
|
||||
#define ASSERT(x) assert( (x) == LIBUSB_SUCCESS )
|
||||
|
||||
enum
|
||||
{
|
||||
RESET_DELAY = 1000, // 1 second delay
|
||||
RESET_MSC_DELAY = 2000, // MSC available require more time for USB to enumerate
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
ARDUINO_ADDR = 0x080C0000,
|
||||
FEATHERLIB_ADDR = 0x08010000
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
USB_VID = 0xcafe,
|
||||
USB_PID = 0x4011,
|
||||
|
||||
USB_PID_MSC = 0x8010,
|
||||
USB_DFU_PID = 0x0008,
|
||||
};
|
||||
|
||||
// SDEP Command Type
|
||||
enum
|
||||
{
|
||||
SDEP_MSGTYPE_COMMAND = 0x10,
|
||||
SDEP_MSGTYPE_RESPONSE = 0x20,
|
||||
SDEP_MSGTYPE_ALERT = 0x40,
|
||||
SDEP_MSGTYPE_ERROR = 0x80,
|
||||
};
|
||||
|
||||
// SDEP Command Constant
|
||||
enum
|
||||
{
|
||||
// General Purpose
|
||||
SDEP_CMD_RESET = 0x0001, // HW reset
|
||||
SDEP_CMD_FACTORYRESET = 0x0002, // Factory reset
|
||||
SDEP_CMD_DFU = 0x0003, // Enter DFU mode
|
||||
SDEP_CMD_INFO = 0x0004, // System information
|
||||
SDEP_CMD_NVM_RESET = 0x0005, // Reset DCT
|
||||
SDEP_CMD_ERROR_STRING = 0x0006, // Get descriptive error string
|
||||
SDEP_CMD_COMMAND_STRING = 0x0007, // Get descriptive SDEP command string
|
||||
SDEP_CMD_SFLASH_ERASEALL = 0x0008, // Erase SPI flash memory
|
||||
|
||||
// DEBUG Commands
|
||||
SDEP_CMD_STACKDUMP = 0x0300, // Dump the stack
|
||||
SDEP_CMD_STACKSIZE = 0x0301, // Get stack size
|
||||
SDEP_CMD_HEAPDUMP = 0x0302, // Dump the heap
|
||||
SDEP_CMD_HEAPSIZE = 0x0303, // Get heap size
|
||||
SDEP_CMD_THREADLIST_PRINT = 0x0304, // Get thread information
|
||||
SDEP_CMD_THREAD_INFO = 0x0305, // Get thread info
|
||||
SDEP_CMD_ARDUINO_HEAPSIZE = 0x0306, // Get Arduino heap size
|
||||
};
|
||||
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* VARIABLE DECLARATION
|
||||
*------------------------------------------------------------------*/
|
||||
libusb_device_handle* udev = NULL;
|
||||
|
||||
bool _dfu_mode = false;
|
||||
bool _msc_enbled = false;
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* FUNCTION DECLARATION
|
||||
*------------------------------------------------------------------*/
|
||||
void print_help(void);
|
||||
void sdep_syscmd(uint16_t cmd);
|
||||
|
||||
void arduino_upgrade(char* dfu_util, char* binfile);
|
||||
void featherlib_upgrade(char* dfu_util, char* binfile);
|
||||
|
||||
struct {
|
||||
const char* command;
|
||||
uint16_t sdep_cmdid;
|
||||
void (*func) (char*, char*);
|
||||
}cmd_table[] =
|
||||
{
|
||||
{ "arduino_upgrade" , 0 , arduino_upgrade },
|
||||
{ "enter_dfu" , SDEP_CMD_DFU , NULL },
|
||||
{ "erase_spiflash" , SDEP_CMD_SFLASH_ERASEALL , NULL },
|
||||
{ "factory_reset" , SDEP_CMD_FACTORYRESET , NULL },
|
||||
{ "featherlib_upgrade" , 0 , featherlib_upgrade },
|
||||
{ "info" , SDEP_CMD_INFO , NULL },
|
||||
{ "nvm_reset" , SDEP_CMD_NVM_RESET , NULL },
|
||||
{ "reboot" , SDEP_CMD_RESET , NULL },
|
||||
};
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* IMPLEMENTATION
|
||||
*------------------------------------------------------------------*/
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char* command = argv[1];
|
||||
|
||||
if ( argc <= 1 || (!strcmp("--help", command)) )
|
||||
{
|
||||
print_help();
|
||||
return 0;
|
||||
}
|
||||
|
||||
ASSERT( libusb_init(NULL) );
|
||||
|
||||
// Open device, try with all PIDs
|
||||
udev = libusb_open_device_with_vid_pid(NULL, USB_VID, USB_PID);
|
||||
|
||||
if (!udev)
|
||||
{
|
||||
udev = libusb_open_device_with_vid_pid(NULL, USB_VID, USB_PID_MSC);
|
||||
if (udev) _msc_enbled = true;
|
||||
}
|
||||
|
||||
if (!udev)
|
||||
{
|
||||
udev = libusb_open_device_with_vid_pid(NULL, USB_VID, USB_DFU_PID);
|
||||
if(udev) _dfu_mode = true;
|
||||
}
|
||||
|
||||
if (!udev)
|
||||
{
|
||||
printf("Bluefruit Feather Board is not detected !!!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
printf("Bluefruit Feather Board opened !!!\n");
|
||||
return 0;
|
||||
|
||||
for(int i=0; i<sizeof(cmd_table)/sizeof(cmd_table[0]); i++)
|
||||
{
|
||||
if (!strcmp(cmd_table[i].command, command))
|
||||
{
|
||||
// Execute SDEP commnad by ID
|
||||
if ( cmd_table[i].sdep_cmdid )
|
||||
{
|
||||
// skip if command is ENTER_DFU and we are already in dfu mode
|
||||
if ( !((cmd_table[i].sdep_cmdid == SDEP_CMD_DFU) && _dfu_mode) )
|
||||
{
|
||||
sdep_syscmd(cmd_table[i].sdep_cmdid);
|
||||
}
|
||||
}
|
||||
// Execute function by pointer
|
||||
else if ( cmd_table[i].func )
|
||||
{
|
||||
// cmd_table[i].func();
|
||||
}else
|
||||
{
|
||||
assert(0);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// close usb if not closed already
|
||||
if (udev)
|
||||
{
|
||||
libusb_close(udev);
|
||||
libusb_exit(NULL);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/*
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
void sdep_syscmd(uint16_t cmd)
|
||||
{
|
||||
assert( cmd <= SDEP_CMD_SFLASH_ERASEALL);
|
||||
|
||||
ASSERT( libusb_control_transfer(udev, 0x40, SDEP_MSGTYPE_COMMAND, cmd, 0, NULL, 0, 0) );
|
||||
|
||||
// For system commands, only INFO has response data
|
||||
if ( cmd == SDEP_CMD_INFO )
|
||||
{
|
||||
char info[512] = { 0 };
|
||||
int len;
|
||||
|
||||
sleep_ms(100);
|
||||
len = libusb_control_transfer(udev, 0xC0, SDEP_MSGTYPE_RESPONSE, cmd, 0, (unsigned char*) info, sizeof(info), 0);
|
||||
|
||||
if ( len <= 0 )
|
||||
{
|
||||
printf("error = %d\n", len);
|
||||
}else
|
||||
{
|
||||
printf("%s\n", info+4);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void download_bin(char* dfu_util, char* binfile, uint32_t addr)
|
||||
{
|
||||
// Enter DFU mode first
|
||||
if (!_dfu_mode)
|
||||
{
|
||||
sdep_syscmd(SDEP_CMD_DFU);
|
||||
|
||||
// Close libusb since dfu-util need to access to upload
|
||||
libusb_close(udev);
|
||||
libusb_exit(NULL);
|
||||
udev = NULL;
|
||||
|
||||
sleep_ms(RESET_DELAY);
|
||||
}
|
||||
|
||||
char cmd[1024];
|
||||
sprintf(cmd, "%s -d 0x239A:0x0008 -a 0 -s 0x%08X:leave -D \"%s\"", dfu_util, addr, binfile);
|
||||
|
||||
system(cmd);
|
||||
|
||||
// Also print out the dfu-util command for debug purpose
|
||||
puts("Flash command");
|
||||
puts(cmd);
|
||||
|
||||
// Delay a bit before return, otherwise enumeration time will cause
|
||||
// Arduino IDE fails to re-open Serial
|
||||
sleep_ms ( _msc_enbled ? RESET_MSC_DELAY : RESET_DELAY );
|
||||
}
|
||||
|
||||
void arduino_upgrade(char* dfu_util, char* binfile)
|
||||
{
|
||||
download_bin(dfu_util, binfile, ARDUINO_ADDR);
|
||||
}
|
||||
|
||||
void featherlib_upgrade(char* dfu_util, char* binfile)
|
||||
{
|
||||
download_bin(dfu_util, binfile, FEATHERLIB_ADDR);
|
||||
}
|
||||
|
||||
|
||||
void print_help(void)
|
||||
{
|
||||
printf("Usage: feather_dfu.py [OPTIONS] COMMAND [ARGS]...\n\n");
|
||||
printf("Options:\n");
|
||||
printf(" --help Show this message and exit.\n\n");
|
||||
|
||||
printf("Commands:\n");
|
||||
// printf(" enter_dfu Force board into DFU mode.\n");
|
||||
printf(" factory_reset Perform a factory reset of the board.\n");
|
||||
printf(" info Print information about the connected board.\n");
|
||||
printf(" reboot Perform a reboot/reset of the board.\n");
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
libusb 1.0 Windows binary snapshot - README
|
||||
|
||||
*********************************************************************
|
||||
* The latest version of this snapshot can always be downloaded at: *
|
||||
* https://sourceforge.net/projects/libusb/files/ *
|
||||
*********************************************************************
|
||||
|
||||
o Visual Studio:
|
||||
- Open existing or create a new project for your application
|
||||
- Copy libusb.h, from the include\libusb-1.0\ directory, into your project and
|
||||
make sure that the location where the file reside appears in the 'Additional
|
||||
Include Directories' section (Configuration Properties -> C/C++ -> General).
|
||||
- Copy the relevant .lib file from MS32\ or MS64\ and add 'libusb-1.0.lib' to
|
||||
your 'Additional Dependencies' (Configuration Properties -> Linker -> Input)
|
||||
Also make sure that the directory where libusb-1.0.lib resides is added to
|
||||
'Additional Library Directories' (Configuration Properties -> Linker
|
||||
-> General)
|
||||
- If you use the static version of the libusb library, make sure that
|
||||
'Runtime Library' is set to 'Multi-threaded DLL (/MD)' (Configuration
|
||||
Properties -> C/C++ -> Code Generation).
|
||||
NB: If your application requires /MT (Multi-threaded/libCMT), you need to
|
||||
recompile a static libusb 1.0 library from source.
|
||||
- Compile and run your application. If you use the DLL version of libusb-1.0,
|
||||
remember that you need to have a copy of the DLL either in the runtime
|
||||
directory or in system32
|
||||
|
||||
o WDK/DDK:
|
||||
- The following is an example of a sources files that you can use to compile
|
||||
a libusb 1.0 based console application. In this sample ..\libusb\ is the
|
||||
directory where you would have copied libusb.h as well as the relevant
|
||||
libusb-1.0.lib
|
||||
|
||||
TARGETNAME=your_app
|
||||
TARGETTYPE=PROGRAM
|
||||
USE_MSVCRT=1
|
||||
UMTYPE=console
|
||||
INCLUDES=..\libusb;$(DDK_INC_PATH)
|
||||
TARGETLIBS=..\libusb\libusb-1.0.lib
|
||||
SOURCES=your_app.c
|
||||
|
||||
- Note that if you plan to use libCMT instead of MSVCRT (USE_LIBCMT=1 instead
|
||||
of USE_MSVCRT=1), you will need to recompile libusb to use libCMT. This can
|
||||
easily be achieved, in the DDK environment, by running 'ddk_build /MT'
|
||||
|
||||
o MinGW/cygwin
|
||||
- Copy libusb.h, from include/libusb-1.0/ to your default include directory,
|
||||
and copy the MinGW32/ or MinGW64/ .a files to your default library directory.
|
||||
Or, if you don't want to use the default locations, make sure that you feed
|
||||
the relevant -I and -L options to the compiler.
|
||||
- Add the '-lusb-1.0' linker option when compiling.
|
||||
|
||||
o Additional information:
|
||||
- The libusb 1.0 API documentation can be accessed at:
|
||||
http://api.libusb.info
|
||||
- For some libusb samples (including source), please have a look in examples/
|
||||
- For additional information on the libusb 1.0 Windows backend please visit:
|
||||
http://windows.libusb.info
|
||||
- The MinGW and MS generated DLLs are fully interchangeable, provided that you
|
||||
use the import libs provided or generate one from the .def also provided.
|
||||
- If you find any issue, please visit http://libusb.info/ and check the
|
||||
Support section
|
||||
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user