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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #ifndef _USB_DFU_H
  18. #define _USB_DFU_H
  19. // ----- Compiler Includes -----
  20. #include <sys/types.h>
  21. // ----- Defines -----
  22. #define USB_FUNCTION_DFU_IFACE_COUNT 1
  23. #ifndef USB_DFU_TRANSFER_SIZE
  24. // Sector size is the same as the program flash size
  25. #if defined(_mk20dx128vlf5_)
  26. #define USB_DFU_TRANSFER_SIZE FLASH_SECTOR_SIZE
  27. // Sector size is double the program flash size
  28. #elif defined(_mk20dx256vlh7_ )
  29. #define USB_DFU_TRANSFER_SIZE FLASH_SECTOR_SIZE / 2
  30. #endif
  31. #endif
  32. #define USB_FUNCTION_DESC_DFU_DECL \
  33. struct dfu_function_desc
  34. #define USB_FUNCTION_DFU_IFACE_COUNT 1
  35. #define USB_FUNCTION_DFU_RX_EP_COUNT 0
  36. #define USB_FUNCTION_DFU_TX_EP_COUNT 0
  37. // ----- Enumerations -----
  38. enum dfu_dev_subclass {
  39. USB_DEV_SUBCLASS_APP_DFU = 0x01
  40. };
  41. enum dfu_dev_proto {
  42. USB_DEV_PROTO_DFU_APP = 0x01,
  43. USB_DEV_PROTO_DFU_DFU = 0x02
  44. };
  45. enum dfu_ctrl_req_code {
  46. USB_CTRL_REQ_DFU_DETACH = 0,
  47. USB_CTRL_REQ_DFU_DNLOAD = 1,
  48. USB_CTRL_REQ_DFU_UPLOAD = 2,
  49. USB_CTRL_REQ_DFU_GETSTATUS = 3,
  50. USB_CTRL_REQ_DFU_CLRSTATUS = 4,
  51. USB_CTRL_REQ_DFU_GETSTATE = 5,
  52. USB_CTRL_REQ_DFU_ABORT = 6
  53. };
  54. enum dfu_status {
  55. DFU_STATUS_async = 0xff,
  56. DFU_STATUS_OK = 0x00,
  57. DFU_STATUS_errTARGET = 0x01,
  58. DFU_STATUS_errFILE = 0x02,
  59. DFU_STATUS_errWRITE = 0x03,
  60. DFU_STATUS_errERASE = 0x04,
  61. DFU_STATUS_errCHECK_ERASED = 0x05,
  62. DFU_STATUS_errPROG = 0x06,
  63. DFU_STATUS_errVERIFY = 0x07,
  64. DFU_STATUS_errADDRESS = 0x08,
  65. DFU_STATUS_errNOTDONE = 0x09,
  66. DFU_STATUS_errFIRMWARE = 0x0a,
  67. DFU_STATUS_errVENDOR = 0x0b,
  68. DFU_STATUS_errUSBR = 0x0c,
  69. DFU_STATUS_errPOR = 0x0d,
  70. DFU_STATUS_errUNKNOWN = 0x0e,
  71. DFU_STATUS_errSTALLEDPKT = 0x0f
  72. };
  73. enum dfu_state {
  74. DFU_STATE_appIDLE = 0,
  75. DFU_STATE_appDETACH = 1,
  76. DFU_STATE_dfuIDLE = 2,
  77. DFU_STATE_dfuDNLOAD_SYNC = 3,
  78. DFU_STATE_dfuDNBUSY = 4,
  79. DFU_STATE_dfuDNLOAD_IDLE = 5,
  80. DFU_STATE_dfuMANIFEST_SYNC = 6,
  81. DFU_STATE_dfuMANIFEST = 7,
  82. DFU_STATE_dfuMANIFEST_WAIT_RESET = 8,
  83. DFU_STATE_dfuUPLOAD_IDLE = 9,
  84. DFU_STATE_dfuERROR = 10
  85. };
  86. // ----- Structs -----
  87. struct dfu_status_t {
  88. enum dfu_status bStatus : 8;
  89. uint32_t bwPollTimeout : 24;
  90. enum dfu_state bState : 8;
  91. uint8_t iString;
  92. } __packed;
  93. CTASSERT_SIZE_BYTE(struct dfu_status_t, 6);
  94. typedef enum dfu_status (*dfu_setup_read_t)(size_t off, size_t *len, void **buf);
  95. typedef enum dfu_status (*dfu_setup_write_t)(size_t off, size_t len, void **buf);
  96. typedef enum dfu_status (*dfu_finish_write_t)(void *, size_t off, size_t len);
  97. typedef void (*dfu_detach_t)(void);
  98. struct dfu_ctx {
  99. struct usbd_function_ctx_header header;
  100. enum dfu_state state;
  101. enum dfu_status status;
  102. dfu_setup_read_t setup_read;
  103. dfu_setup_write_t setup_write;
  104. dfu_finish_write_t finish_write;
  105. size_t off;
  106. size_t len;
  107. };
  108. struct dfu_desc_functional {
  109. uint8_t bLength;
  110. struct usb_desc_type_t bDescriptorType; /* = class DFU/0x1 FUNCTIONAL */
  111. union {
  112. struct {
  113. uint8_t can_download : 1;
  114. uint8_t can_upload : 1;
  115. uint8_t manifestation_tolerant : 1;
  116. uint8_t will_detach : 1;
  117. uint8_t _rsvd0 : 4;
  118. };
  119. uint8_t bmAttributes;
  120. };
  121. uint16_t wDetachTimeOut;
  122. uint16_t wTransferSize;
  123. struct usb_bcd_t bcdDFUVersion;
  124. } __packed;
  125. CTASSERT_SIZE_BYTE(struct dfu_desc_functional, 9);
  126. struct dfu_function_desc {
  127. struct usb_desc_iface_t iface;
  128. struct dfu_desc_functional dfu;
  129. };
  130. extern const struct usbd_function dfu_function;
  131. extern const struct usbd_function dfu_app_function;
  132. // ----- Functions -----
  133. void dfu_write_done( enum dfu_status, struct dfu_ctx *ctx );
  134. 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 );
  135. void dfu_app_init( dfu_detach_t detachcb );
  136. #endif