1 #include "usbd_core.h"
2 #include "usb_printer.h"
3
4 /*!< endpoint address */
5 #define PRINTER_IN_EP 0x81
6 #define PRINTER_IN_EP_SIZE 0x40
7 #define PRINTER_OUT_EP 0x02
8 #define PRINTER_OUT_EP_SIZE 0x40
9
10 #define USBD_VID 0x5A5A
11 #define USBD_PID 0xA5A5
12 #define USBD_MAX_POWER 100
13 #define USBD_LANGID_STRING 0x0409
14
15 /*!< config descriptor size */
16 #define USB_CONFIG_SIZE (32)
17
18 /*!< global descriptor */
19 static const uint8_t printer_descriptor[] = {
20 USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0000, 0x01),
21 USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_SELF_POWERED, USBD_MAX_POWER),
22 USB_INTERFACE_DESCRIPTOR_INIT(0x00, 0x00, 0x02, 0x07, 0x01, 0x02, 0x00),
23 USB_ENDPOINT_DESCRIPTOR_INIT(PRINTER_IN_EP, 0x02, PRINTER_IN_EP_SIZE, 0x00),
24 USB_ENDPOINT_DESCRIPTOR_INIT(PRINTER_OUT_EP, 0x02, PRINTER_OUT_EP_SIZE, 0x00),
25 ///////////////////////////////////////
26 /// string0 descriptor
27 ///////////////////////////////////////
28 USB_LANGID_INIT(USBD_LANGID_STRING),
29 ///////////////////////////////////////
30 /// string1 descriptor
31 ///////////////////////////////////////
32 0x14, /* bLength */
33 USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
34 'C', 0x00, /* wcChar0 */
35 'h', 0x00, /* wcChar1 */
36 'e', 0x00, /* wcChar2 */
37 'r', 0x00, /* wcChar3 */
38 'r', 0x00, /* wcChar4 */
39 'y', 0x00, /* wcChar5 */
40 'U', 0x00, /* wcChar6 */
41 'S', 0x00, /* wcChar7 */
42 'B', 0x00, /* wcChar8 */
43 ///////////////////////////////////////
44 /// string2 descriptor
45 ///////////////////////////////////////
46 0x2A, /* bLength */
47 USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
48 'C', 0x00, /* wcChar0 */
49 'h', 0x00, /* wcChar1 */
50 'e', 0x00, /* wcChar2 */
51 'r', 0x00, /* wcChar3 */
52 'r', 0x00, /* wcChar4 */
53 'y', 0x00, /* wcChar5 */
54 'U', 0x00, /* wcChar6 */
55 'S', 0x00, /* wcChar7 */
56 'B', 0x00, /* wcChar8 */
57 ' ', 0x00, /* wcChar9 */
58 'P', 0x00, /* wcChar10 */
59 'R', 0x00, /* wcChar11 */
60 'I', 0x00, /* wcChar12 */
61 'N', 0x00, /* wcChar13 */
62 'T', 0x00, /* wcChar14 */
63 ' ', 0x00, /* wcChar15 */
64 'D', 0x00, /* wcChar16 */
65 'E', 0x00, /* wcChar17 */
66 'M', 0x00, /* wcChar18 */
67 'O', 0x00, /* wcChar19 */
68 ///////////////////////////////////////
69 /// string3 descriptor
70 ///////////////////////////////////////
71 0x16, /* bLength */
72 USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
73 '2', 0x00, /* wcChar0 */
74 '0', 0x00, /* wcChar1 */
75 '2', 0x00, /* wcChar2 */
76 '2', 0x00, /* wcChar3 */
77 '1', 0x00, /* wcChar4 */
78 '2', 0x00, /* wcChar5 */
79 '3', 0x00, /* wcChar6 */
80 '4', 0x00, /* wcChar7 */
81 '5', 0x00, /* wcChar8 */
82 '6', 0x00, /* wcChar9 */
83 #ifdef CONFIG_USB_HS
84 ///////////////////////////////////////
85 /// device qualifier descriptor
86 ///////////////////////////////////////
87 0x0a,
88 USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
89 0x00,
90 0x02,
91 0x02,
92 0x02,
93 0x01,
94 0x40,
95 0x01,
96 0x00,
97 #endif
98 0x00
99 };
100
101 USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[PRINTER_OUT_EP_SIZE];
102 USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[PRINTER_IN_EP_SIZE];
103
usbd_event_handler(uint8_t event)104 void usbd_event_handler(uint8_t event)
105 {
106 switch (event) {
107 case USBD_EVENT_RESET:
108 break;
109 case USBD_EVENT_CONNECTED:
110 break;
111 case USBD_EVENT_DISCONNECTED:
112 break;
113 case USBD_EVENT_RESUME:
114 break;
115 case USBD_EVENT_SUSPEND:
116 break;
117 case USBD_EVENT_CONFIGURED:
118 /* setup first out ep read transfer */
119 usbd_ep_start_read(PRINTER_OUT_EP, read_buffer, PRINTER_OUT_EP_SIZE);
120 break;
121 case USBD_EVENT_SET_REMOTE_WAKEUP:
122 break;
123 case USBD_EVENT_CLR_REMOTE_WAKEUP:
124 break;
125
126 default:
127 break;
128 }
129 }
130
usbd_printer_bulk_out(uint8_t ep,uint32_t nbytes)131 void usbd_printer_bulk_out(uint8_t ep, uint32_t nbytes)
132 {
133 USB_LOG_RAW("actual out len:%d\r\n", nbytes);
134 // for (int i = 0; i < 100; i++) {
135 // printf("%02x ", read_buffer[i]);
136 // }
137 // printf("\r\n");
138 /* setup next out ep read transfer */
139 usbd_ep_start_read(PRINTER_OUT_EP, read_buffer, PRINTER_OUT_EP_SIZE);
140 }
141
usbd_printer_bulk_in(uint8_t ep,uint32_t nbytes)142 void usbd_printer_bulk_in(uint8_t ep, uint32_t nbytes)
143 {
144 USB_LOG_RAW("actual in len:%d\r\n", nbytes);
145
146 if ((nbytes % PRINTER_IN_EP_SIZE) == 0 && nbytes) {
147 /* send zlp */
148 usbd_ep_start_write(PRINTER_IN_EP, NULL, 0);
149 } else {
150 }
151 }
152
153 /*!< endpoint call back */
154 struct usbd_endpoint printer_out_ep = {
155 .ep_addr = PRINTER_OUT_EP,
156 .ep_cb = usbd_printer_bulk_out
157 };
158
159 struct usbd_endpoint printer_in_ep = {
160 .ep_addr = PRINTER_IN_EP,
161 .ep_cb = usbd_printer_bulk_in
162 };
163
164 struct usbd_interface intf0;
165
166 static const uint8_t printer_device_id[] =
167 {
168 0x00, 51,
169 'M','F','G',':','C','B','M',';',
170 'C','M','D',':','G','D','I',';',
171 'M','D','L',':','C','B','M','1','0','0','0',';',
172 'C','L','S',':','P','R','I','N','T','E','R',';',
173 'M','O','D','E',':','G','D','I',';'
174 };
175
printer_init(void)176 void printer_init(void)
177 {
178 usbd_desc_register(printer_descriptor);
179 usbd_add_interface(usbd_printer_init_intf(&intf0, printer_device_id, sizeof(printer_device_id)));
180 usbd_add_endpoint(&printer_out_ep);
181 usbd_add_endpoint(&printer_in_ep);
182
183 usbd_initialize();
184 }
185