Kiibohd Controller
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

dfu.h 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Copyright (c) 2011,2012 Simon Schubert <[email protected]>.
  2. * Modifications by Jacob Alexander 2014-2015 <[email protected]>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #pragma once
  18. // ----- Compiler Includes -----
  19. #include <sys/types.h>
  20. // ----- Defines -----
  21. #define USB_FUNCTION_DFU_IFACE_COUNT 1
  22. #ifndef USB_DFU_TRANSFER_SIZE
  23. // Sector size is the same as the program flash size
  24. #if defined(_mk20dx128vlf5_)
  25. #define USB_DFU_TRANSFER_SIZE FLASH_SECTOR_SIZE
  26. // Sector size is double the program flash size
  27. #elif defined(_mk20dx256vlh7_ )
  28. #define USB_DFU_TRANSFER_SIZE FLASH_SECTOR_SIZE / 2
  29. #endif
  30. #endif
  31. #define USB_FUNCTION_DESC_DFU_DECL \
  32. struct dfu_function_desc
  33. #define USB_FUNCTION_DFU_IFACE_COUNT 1
  34. #define USB_FUNCTION_DFU_RX_EP_COUNT 0
  35. #define USB_FUNCTION_DFU_TX_EP_COUNT 0
  36. // ----- Enumerations -----
  37. enum dfu_dev_subclass {
  38. USB_DEV_SUBCLASS_APP_DFU = 0x01
  39. };
  40. enum dfu_dev_proto {
  41. USB_DEV_PROTO_DFU_APP = 0x01,
  42. USB_DEV_PROTO_DFU_DFU = 0x02
  43. };
  44. enum dfu_ctrl_req_code {
  45. USB_CTRL_REQ_DFU_DETACH = 0,
  46. USB_CTRL_REQ_DFU_DNLOAD = 1,
  47. USB_CTRL_REQ_DFU_UPLOAD = 2,
  48. USB_CTRL_REQ_DFU_GETSTATUS = 3,
  49. USB_CTRL_REQ_DFU_CLRSTATUS = 4,
  50. USB_CTRL_REQ_DFU_GETSTATE = 5,
  51. USB_CTRL_REQ_DFU_ABORT = 6
  52. };
  53. enum dfu_status {
  54. DFU_STATUS_async = 0xff,
  55. DFU_STATUS_OK = 0x00,
  56. DFU_STATUS_errTARGET = 0x01,
  57. DFU_STATUS_errFILE = 0x02,
  58. DFU_STATUS_errWRITE = 0x03,
  59. DFU_STATUS_errERASE = 0x04,
  60. DFU_STATUS_errCHECK_ERASED = 0x05,
  61. DFU_STATUS_errPROG = 0x06,
  62. DFU_STATUS_errVERIFY = 0x07,
  63. DFU_STATUS_errADDRESS = 0x08,
  64. DFU_STATUS_errNOTDONE = 0x09,
  65. DFU_STATUS_errFIRMWARE = 0x0a,
  66. DFU_STATUS_errVENDOR = 0x0b,
  67. DFU_STATUS_errUSBR = 0x0c,
  68. DFU_STATUS_errPOR = 0x0d,
  69. DFU_STATUS_errUNKNOWN = 0x0e,
  70. DFU_STATUS_errSTALLEDPKT = 0x0f
  71. };
  72. enum dfu_state {
  73. DFU_STATE_appIDLE = 0,
  74. DFU_STATE_appDETACH = 1,
  75. DFU_STATE_dfuIDLE = 2,
  76. DFU_STATE_dfuDNLOAD_SYNC = 3,
  77. DFU_STATE_dfuDNBUSY = 4,
  78. DFU_STATE_dfuDNLOAD_IDLE = 5,
  79. DFU_STATE_dfuMANIFEST_SYNC = 6,
  80. DFU_STATE_dfuMANIFEST = 7,
  81. DFU_STATE_dfuMANIFEST_WAIT_RESET = 8,
  82. DFU_STATE_dfuUPLOAD_IDLE = 9,
  83. DFU_STATE_dfuERROR = 10
  84. };
  85. // ----- Structs -----
  86. struct dfu_status_t {
  87. enum dfu_status bStatus : 8;
  88. uint32_t bwPollTimeout : 24;
  89. enum dfu_state bState : 8;
  90. uint8_t iString;
  91. } __packed;
  92. CTASSERT_SIZE_BYTE(struct dfu_status_t, 6);
  93. typedef enum dfu_status (*dfu_setup_read_t)(size_t off, size_t *len, void **buf);
  94. typedef enum dfu_status (*dfu_setup_write_t)(size_t off, size_t len, void **buf);
  95. typedef enum dfu_status (*dfu_finish_write_t)(void *, size_t off, size_t len);
  96. typedef void (*dfu_detach_t)(void);
  97. struct dfu_ctx {
  98. struct usbd_function_ctx_header header;
  99. enum dfu_state state;
  100. enum dfu_status status;
  101. dfu_setup_read_t setup_read;
  102. dfu_setup_write_t setup_write;
  103. dfu_finish_write_t finish_write;
  104. size_t off;
  105. size_t len;
  106. };
  107. struct dfu_desc_functional {
  108. uint8_t bLength;
  109. struct usb_desc_type_t bDescriptorType; /* = class DFU/0x1 FUNCTIONAL */
  110. union {
  111. struct {
  112. uint8_t can_download : 1;
  113. uint8_t can_upload : 1;
  114. uint8_t manifestation_tolerant : 1;
  115. uint8_t will_detach : 1;
  116. uint8_t _rsvd0 : 4;
  117. };
  118. uint8_t bmAttributes;
  119. };
  120. uint16_t wDetachTimeOut;
  121. uint16_t wTransferSize;
  122. struct usb_bcd_t bcdDFUVersion;
  123. } __packed;
  124. CTASSERT_SIZE_BYTE(struct dfu_desc_functional, 9);
  125. struct dfu_function_desc {
  126. struct usb_desc_iface_t iface;
  127. struct dfu_desc_functional dfu;
  128. };
  129. extern const struct usbd_function dfu_function;
  130. extern const struct usbd_function dfu_app_function;
  131. // ----- Functions -----
  132. void dfu_write_done( enum dfu_status, struct dfu_ctx *ctx );
  133. void dfu_init( dfu_setup_read_t setup_read, dfu_setup_write_t setup_write, dfu_finish_write_t finish_write, struct dfu_ctx *ctx );
  134. void dfu_app_init( dfu_detach_t detachcb );