|
| 1 | +#include "Arduino.h" |
| 2 | + |
| 3 | +#include <esp32-hal-tinyusb.h> |
| 4 | +#include <esp_system.h> |
| 5 | + |
| 6 | +// defines an "Update" object accessed only by this translation unit |
| 7 | +// (also, the object requires MD5Builder internally) |
| 8 | +namespace { |
| 9 | +// ignore '{anonymous}::MD5Builder::...() defined but not used' warnings |
| 10 | +#pragma GCC diagnostic push |
| 11 | +#pragma GCC diagnostic ignored "-Wunused-function" |
| 12 | +#include "../../libraries/Update/src/Updater.cpp" |
| 13 | +#include "../../cores/esp32/MD5Builder.cpp" |
| 14 | +#pragma GCC diagnostic pop |
| 15 | +} |
| 16 | + |
| 17 | +#define ALT_COUNT 1 |
| 18 | + |
| 19 | +//--------------------------------------------------------------------+ |
| 20 | +// DFU callbacks |
| 21 | +// Note: alt is used as the partition number, in order to support multiple partitions like FLASH, EEPROM, etc. |
| 22 | +//--------------------------------------------------------------------+ |
| 23 | + |
| 24 | +uint16_t load_dfu_ota_descriptor(uint8_t * dst, uint8_t * itf) |
| 25 | +{ |
| 26 | +#define DFU_ATTRS (DFU_ATTR_CAN_DOWNLOAD | DFU_ATTR_CAN_UPLOAD | DFU_ATTR_MANIFESTATION_TOLERANT) |
| 27 | + |
| 28 | + uint8_t str_index = tinyusb_add_string_descriptor("Arduino DFU"); |
| 29 | + uint8_t descriptor[TUD_DFU_DESC_LEN(ALT_COUNT)] = { |
| 30 | + // Interface number, string index, attributes, detach timeout, transfer size */ |
| 31 | + TUD_DFU_DESCRIPTOR(*itf, ALT_COUNT, str_index, DFU_ATTRS, 100, CFG_TUD_DFU_XFER_BUFSIZE), |
| 32 | + }; |
| 33 | + *itf+=1; |
| 34 | + memcpy(dst, descriptor, TUD_DFU_DESC_LEN(ALT_COUNT)); |
| 35 | + return TUD_DFU_DESC_LEN(ALT_COUNT); |
| 36 | +} |
| 37 | + |
| 38 | +// Invoked right before tud_dfu_download_cb() (state=DFU_DNBUSY) or tud_dfu_manifest_cb() (state=DFU_MANIFEST) |
| 39 | +// Application return timeout in milliseconds (bwPollTimeout) for the next download/manifest operation. |
| 40 | +// During this period, USB host won't try to communicate with us. |
| 41 | +uint32_t tud_dfu_get_timeout_cb(uint8_t alt, uint8_t state) |
| 42 | +{ |
| 43 | + if ( state == DFU_DNBUSY ) |
| 44 | + { |
| 45 | + // longest delay for Flash writing |
| 46 | + return 10; |
| 47 | + } |
| 48 | + else if (state == DFU_MANIFEST) |
| 49 | + { |
| 50 | + // time for esp32_ota_set_boot_partition to check final image |
| 51 | + return 100; |
| 52 | + } |
| 53 | + |
| 54 | + return 0; |
| 55 | +} |
| 56 | + |
| 57 | +// Invoked when received DFU_DNLOAD (wLength>0) following by DFU_GETSTATUS (state=DFU_DNBUSY) requests |
| 58 | +// This callback could be returned before flashing op is complete (async). |
| 59 | +// Once finished flashing, application must call tud_dfu_finish_flashing() |
| 60 | +void tud_dfu_download_cb(uint8_t alt, uint16_t block_num, uint8_t const* data, uint16_t length) |
| 61 | +{ |
| 62 | + if (!Update.isRunning()) |
| 63 | + { |
| 64 | + // this is the first data block, start update if possible |
| 65 | + if (!Update.begin()) |
| 66 | + { |
| 67 | + tud_dfu_finish_flashing(DFU_STATUS_ERR_TARGET); |
| 68 | + return; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // write a block of data to Flash |
| 73 | + // XXX: Update API is needlessly non-const |
| 74 | + size_t written = Update.write(const_cast<uint8_t*>(data), length); |
| 75 | + tud_dfu_finish_flashing((written == length) ? DFU_STATUS_OK : DFU_STATUS_ERR_WRITE); |
| 76 | +} |
| 77 | + |
| 78 | +// Invoked when download process is complete, received DFU_DNLOAD (wLength=0) following by DFU_GETSTATUS (state=Manifest) |
| 79 | +// Application can do checksum, or actual flashing if buffered entire image previously. |
| 80 | +// Once finished flashing, application must call tud_dfu_finish_flashing() |
| 81 | +void tud_dfu_manifest_cb(uint8_t alt) |
| 82 | +{ |
| 83 | + (void) alt; |
| 84 | + bool ok = Update.end(true); |
| 85 | + |
| 86 | + // flashing op for manifest is complete |
| 87 | + tud_dfu_finish_flashing(ok? DFU_STATUS_OK : DFU_STATUS_ERR_VERIFY); |
| 88 | +} |
| 89 | + |
| 90 | +// Invoked when received DFU_UPLOAD request |
| 91 | +// Application must populate data with up to length bytes and |
| 92 | +// Return the number of written bytes |
| 93 | +uint16_t tud_dfu_upload_cb(uint8_t alt, uint16_t block_num, uint8_t* data, uint16_t length) |
| 94 | +{ |
| 95 | + (void) alt; |
| 96 | + (void) block_num; |
| 97 | + (void) data; |
| 98 | + (void) length; |
| 99 | + |
| 100 | + // not implemented |
| 101 | + return 0; |
| 102 | +} |
| 103 | + |
| 104 | +// Invoked when the Host has terminated a download or upload transfer |
| 105 | +void tud_dfu_abort_cb(uint8_t alt) |
| 106 | +{ |
| 107 | + (void) alt; |
| 108 | + // ignore |
| 109 | +} |
| 110 | + |
| 111 | +// Invoked when a DFU_DETACH request is received |
| 112 | +void tud_dfu_detach_cb(void) |
| 113 | +{ |
| 114 | + // done, reboot |
| 115 | + esp_restart(); |
| 116 | +} |
0 commit comments