USBMSD: use MemoryPool instead of ahbmalloc
[clinton/Smoothieware.git] / src / libs / USBDevice / DFU.h
1 #ifndef _DFU_H
2 #define _DFU_H
3
4 #include "USB.h"
5
6 #include "USBEndpoints.h"
7 #include "USBDescriptor.h"
8 #include "USBDevice_Types.h"
9
10 #include "Module.h"
11
12 #include <stdint.h>
13
14 #define REQ_DFU_DETACH 0x0
15 #define REQ_DFU_DNLOAD 0x1
16 #define REQ_DFU_UPLOAD 0x2
17 #define REQ_DFU_GETSTATUS 0x3
18 #define REQ_DFU_CLRSTATUS 0x4
19 #define REQ_DFU_GETSTATE 0x5
20 #define REQ_DFU_ABORT 0x6
21
22 #define DL_DFU_FUNCTIONAL_DESCRIPTOR 0x09
23 #define DT_DFU_FUNCTIONAL_DESCRIPTOR 0x21
24
25 #define DFU_VERSION_1_1 0x0110
26
27 #define DFU_INTERFACE_CLASS 0xFE
28 #define DFU_INTERFACE_SUBCLASS 0x01
29 #define DFU_INTERFACE_PROTOCOL_RUNTIME 0x01
30 #define DFU_INTERFACE_PROTOCOL_DFUMODE 0x02
31
32 #define DFU_BMATTRIBUTES_WILLDETACH (1<<3)
33 #define DFU_BMATTRIBUTES_MANIFEST_TOLERANT (1<<2) /* device keeps talking while flashing? */
34 #define DFU_BMATTRIBUTES_CANUPLOAD (1<<1)
35 #define DFU_BMATTRIBUTES_CANDOWNLOAD (1<<0)
36
37 #define DFU_DETACH 0
38 #define DFU_DNLOAD 1
39 #define DFU_UPLOAD 2
40 #define DFU_GETSTATUS 3
41 #define DFU_CLRSTATUS 4
42 #define DFU_GETSTATE 5
43 #define DFU_ABORT 6
44
45 class DFU;
46
47 typedef struct __attribute__ ((packed))
48 {
49 uint8_t bLength;
50 uint8_t bDescriptorType;
51 uint8_t bmAttributes;
52 uint16_t wDetachTimeout;
53 uint16_t wTransferSize;
54 uint16_t bcdDFUVersion;
55 } DFU_functional_descriptor;
56
57 typedef struct __attribute__ ((packed))
58 {
59 uint8_t bStatus;
60 uint32_t bwPollTimeout:24;
61 uint8_t bState;
62 uint8_t iString;
63 } DFU_Status_Response;
64
65
66 class DFU : public USB_Class_Receiver, public Module {
67 public:
68 DFU(USB *);
69
70 virtual bool USBEvent_Request(CONTROL_TRANSFER&);
71 virtual bool USBEvent_RequestComplete(CONTROL_TRANSFER&, uint8_t *buf, uint32_t length);
72
73 void on_module_loaded(void);
74 void on_idle(void*);
75
76 protected:
77 USB *usb;
78
79 DFU_functional_descriptor dfu_descriptor;
80 usbdesc_interface dfu_interface;
81
82 usbdesc_string_l(12) dfu_string;
83
84 DFU_Status_Response dfu_status;
85
86 int prep_for_detach;
87 };
88
89 #endif /* _DFU_H */