| 1 |
/* |
| 2 |
* dfu-programmer |
| 3 |
* |
| 4 |
* $Id$ |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 19 |
*/ |
| 20 |
|
| 21 |
#ifndef __DFU_H__ |
| 22 |
#define __DFU_H__ |
| 23 |
|
| 24 |
#include <libusb-1.0/libusb.h> |
| 25 |
#include <stdint.h> |
| 26 |
#include <stddef.h> |
| 27 |
#include <stdbool.h> |
| 28 |
|
| 29 |
#ifdef __cplusplus |
| 30 |
extern "C" { |
| 31 |
#endif |
| 32 |
|
| 33 |
#include "dfu-device.h" |
| 34 |
|
| 35 |
/* DFU states */ |
| 36 |
#define STATE_APP_IDLE 0x00 |
| 37 |
#define STATE_APP_DETACH 0x01 |
| 38 |
#define STATE_DFU_IDLE 0x02 |
| 39 |
#define STATE_DFU_DOWNLOAD_SYNC 0x03 |
| 40 |
#define STATE_DFU_DOWNLOAD_BUSY 0x04 |
| 41 |
#define STATE_DFU_DOWNLOAD_IDLE 0x05 |
| 42 |
#define STATE_DFU_MANIFEST_SYNC 0x06 |
| 43 |
#define STATE_DFU_MANIFEST 0x07 |
| 44 |
#define STATE_DFU_MANIFEST_WAIT_RESET 0x08 |
| 45 |
#define STATE_DFU_UPLOAD_IDLE 0x09 |
| 46 |
#define STATE_DFU_ERROR 0x0a |
| 47 |
|
| 48 |
|
| 49 |
/* DFU status */ |
| 50 |
#define DFU_STATUS_OK 0x00 |
| 51 |
#define DFU_STATUS_ERROR_TARGET 0x01 |
| 52 |
#define DFU_STATUS_ERROR_FILE 0x02 |
| 53 |
#define DFU_STATUS_ERROR_WRITE 0x03 |
| 54 |
#define DFU_STATUS_ERROR_ERASE 0x04 |
| 55 |
#define DFU_STATUS_ERROR_CHECK_ERASED 0x05 |
| 56 |
#define DFU_STATUS_ERROR_PROG 0x06 |
| 57 |
#define DFU_STATUS_ERROR_VERIFY 0x07 |
| 58 |
#define DFU_STATUS_ERROR_ADDRESS 0x08 |
| 59 |
#define DFU_STATUS_ERROR_NOTDONE 0x09 |
| 60 |
#define DFU_STATUS_ERROR_FIRMWARE 0x0a |
| 61 |
#define DFU_STATUS_ERROR_VENDOR 0x0b |
| 62 |
#define DFU_STATUS_ERROR_USBR 0x0c |
| 63 |
#define DFU_STATUS_ERROR_POR 0x0d |
| 64 |
#define DFU_STATUS_ERROR_UNKNOWN 0x0e |
| 65 |
#define DFU_STATUS_ERROR_STALLEDPKT 0x0f |
| 66 |
|
| 67 |
|
| 68 |
/* This is based off of DFU_GETSTATUS |
| 69 |
* |
| 70 |
* 1 unsigned byte bStatus |
| 71 |
* 3 unsigned byte bwPollTimeout |
| 72 |
* 1 unsigned byte bState |
| 73 |
* 1 unsigned byte iString |
| 74 |
*/ |
| 75 |
|
| 76 |
typedef struct { |
| 77 |
uint8_t bStatus; |
| 78 |
uint32_t bwPollTimeout; |
| 79 |
uint8_t bState; |
| 80 |
uint8_t iString; |
| 81 |
} dfu_status_t; |
| 82 |
|
| 83 |
void dfu_set_transaction_num( dfu_device_t *device, uint16_t newNum ); |
| 84 |
/* set / reset the wValue parameter to a given value. this number is |
| 85 |
* significant for stm32 device commands (see dfu-device.h) |
| 86 |
*/ |
| 87 |
|
| 88 |
uint16_t dfu_get_transaction_num( dfu_device_t *device ); |
| 89 |
/* get the current transaction number (can be used to calculate address |
| 90 |
* offset for stm32 devices --- see dfu-device.h) |
| 91 |
*/ |
| 92 |
|
| 93 |
int32_t dfu_detach( dfu_device_t *device, const int32_t timeout ); |
| 94 |
/* DFU_DETACH Request (DFU Spec 1.0, Section 5.1) |
| 95 |
* |
| 96 |
* device - the dfu device to communicate with |
| 97 |
* timeout - the timeout in ms the USB device should wait for a pending |
| 98 |
* USB reset before giving up and terminating the operation |
| 99 |
* |
| 100 |
* returns 0 or < 0 on error |
| 101 |
*/ |
| 102 |
|
| 103 |
int32_t dfu_download( dfu_device_t *device, const size_t length, uint8_t* data ); |
| 104 |
/* DFU_DNLOAD Request (DFU Spec 1.0, Section 6.1.1) |
| 105 |
* |
| 106 |
* device - the dfu device to communicate with |
| 107 |
* length - the total number of bytes to transfer to the USB |
| 108 |
* device - must be less than wTransferSize |
| 109 |
* data - the data to transfer |
| 110 |
* |
| 111 |
* returns the number of bytes written or < 0 on error |
| 112 |
*/ |
| 113 |
|
| 114 |
int32_t dfu_upload( dfu_device_t *device, const size_t length, uint8_t* data ); |
| 115 |
/* DFU_UPLOAD Request (DFU Spec 1.0, Section 6.2) |
| 116 |
* |
| 117 |
* device - the dfu device to communicate with |
| 118 |
* length - the maximum number of bytes to receive from the USB |
| 119 |
* device - must be less than wTransferSize |
| 120 |
* data - the buffer to put the received data in |
| 121 |
* |
| 122 |
* returns the number of bytes received or < 0 on error |
| 123 |
*/ |
| 124 |
|
| 125 |
int32_t dfu_get_status( dfu_device_t *device, dfu_status_t *status ); |
| 126 |
/* DFU_GETSTATUS Request (DFU Spec 1.0, Section 6.1.2) |
| 127 |
* |
| 128 |
* device - the dfu device to communicate with |
| 129 |
* status - the data structure to be populated with the results |
| 130 |
* |
| 131 |
* return the 0 if successful or < 0 on an error |
| 132 |
*/ |
| 133 |
|
| 134 |
int32_t dfu_clear_status( dfu_device_t *device ); |
| 135 |
/* DFU_CLRSTATUS Request (DFU Spec 1.0, Section 6.1.3) |
| 136 |
* |
| 137 |
* device - the dfu device to communicate with |
| 138 |
* |
| 139 |
* return 0 or < 0 on an error |
| 140 |
*/ |
| 141 |
|
| 142 |
int32_t dfu_get_state( dfu_device_t *device ); |
| 143 |
/* DFU_GETSTATE Request (DFU Spec 1.0, Section 6.1.5) |
| 144 |
* |
| 145 |
* device - the dfu device to communicate with |
| 146 |
* |
| 147 |
* returns the state or < 0 on error |
| 148 |
*/ |
| 149 |
|
| 150 |
int32_t dfu_abort( dfu_device_t *device ); |
| 151 |
/* DFU_ABORT Request (DFU Spec 1.0, Section 6.1.4) |
| 152 |
* |
| 153 |
* device - the dfu device to communicate with |
| 154 |
* |
| 155 |
* returns 0 or < 0 on an error |
| 156 |
*/ |
| 157 |
|
| 158 |
|
| 159 |
struct libusb_device |
| 160 |
*dfu_device_init( const uint32_t vendor, |
| 161 |
const uint32_t product, |
| 162 |
const uint32_t bus, |
| 163 |
const uint32_t dev_addr, |
| 164 |
dfu_device_t *device, |
| 165 |
const bool initial_abort, |
| 166 |
const bool honor_interfaceclass, |
| 167 |
libusb_context *usb_context ); |
| 168 |
/* dfu_device_init is designed to find one of the usb devices which match |
| 169 |
* the vendor and product parameters passed in. |
| 170 |
* |
| 171 |
* vendor - the vender number of the device to look for |
| 172 |
* product - the product number of the device to look for |
| 173 |
* [out] device - the dfu device to communicate with |
| 174 |
* |
| 175 |
* return a pointer to the usb_device if found, or NULL otherwise |
| 176 |
*/ |
| 177 |
|
| 178 |
char* dfu_status_to_string( const int32_t status ); |
| 179 |
/* Used to convert the DFU status to a string. |
| 180 |
* |
| 181 |
* status - the status to convert |
| 182 |
* |
| 183 |
* returns the status name or "unknown status" |
| 184 |
*/ |
| 185 |
|
| 186 |
char* dfu_state_to_string( const int32_t state ); |
| 187 |
/* Used to convert the DFU state to a string. |
| 188 |
* |
| 189 |
* state - the state to convert |
| 190 |
* |
| 191 |
* returns the state name or "unknown state" |
| 192 |
*/ |
| 193 |
|
| 194 |
#ifdef __cplusplus |
| 195 |
} |
| 196 |
#endif |
| 197 |
|
| 198 |
#endif |