MakeBlock Drive Updated
Updated library for MakeBlock Ranger
Loading...
Searching...
No Matches
MeUSBHost.h
Go to the documentation of this file.
1
45#ifndef MeUSBHost_H
46#define MeUSBHost_H
47
48#include <Arduino.h>
49#include <stdint.h>
50#include <stdbool.h>
51#include "MeConfig.h"
52#include "MePort.h"
53#include "MeSerial.h"
54#define USB2_0 1
55#define USB1_0 0
56
57#define CH375_MAX_DATA_LEN 0x40 /* max package length */
58
59 /* ********************************************************************************************************************* */
60 /* Commands */
61
62#define CMD_RESET_ALL 0x05 /* Reset Device */
63
64#define CMD_CHECK_EXIST 0x06 /* Chip check */
65 /* Input: any (ep:0x12)*/
66 /* Output: output inverse of input (ep:0x21) */
67
68#define CMD_SET_USB_ID 0x12 /* Device Mode: Setup the VID and PID in device mode */
69 /* Input: VID lower byte, VID higher byte, PID lower byte, PID higher byte */
70
71#define CMD_SET_USB_ADDR 0x13 /* Setup the usb address */
72 /* Input: address value */
73
74#define CMD_SET_USB_MODE 0x15 /* Setup the module work mode, with enable or disable state */
75 /* Input: mode */
76 /* 00H=Device mode disabled, 01H=Device mode enabled with external firmware, 02H=Device mode enabled with internal firmware, 03H=Device mode enabled with internal firmware and interrupt endpoint */
77 /* 04H=Host Mode disabled, 05H=Host Mode enabled, 06H=Host Mode enabled with SOF package, 07H=Host Mode enabled and reset USB bus */
78 /* Output: status( CMD_RET_SUCCESS or CMD_RET_ABORT, otherswise unfinished ) */
79
80#define CMD_SET_ENDP2 0x18 /* Device Mode: setup endpoint 0 receiver */
81 /* Input: Workmode */
82 /* Bit 7~6: 1x set sync trigger flag to x, 0x don't change the sync flag */
83 /* Bit3~0 token ack type: 0000-Ready ACK, 1110-Busy NAK, 1111-Error STALL */
84
85#define CMD_SET_ENDP3 0x19 /* Device Mode: setup endpoint 0 transmitter */
86
87#define CMD_SET_ENDP4 0x1A /* Device Mode: setup endpoint 1 receiver */
88
89#define CMD_SET_ENDP5 0x1B /* Device Mode: setup endpoint 1 transmitter */
90
91#define CMD_SET_ENDP6 0x1C /* Device Mode: setup endpoint 2 receiver */
92
93#define CMD_SET_ENDP7 0x1D /* Device Mode: setup endpoint 2 transmitter */
94
95
96#define CMD_GET_TOGGLE 0x0A /* get the out token sync state */
97 /* Input: 0x1A */
98 /* Output: sync state */
99 /* bit4: 1:OUT token synced, 0:OUT token unsync */
100
101#define CMD_GET_STATUS 0x22 /* Get interrupt state */
102 /* Output: interrupt state */
103
104#define CMD_UNLOCK_USB 0x23 /* Device Mode: release buffer */
105
106#define CMD_RD_USB_DATA 0x28 /* block read from usb device, and release buffer */
107 /* Output: length, data stream */
108
109#define CMD_WR_USB_DATA3 0x29 /* Device Mode: write to usb endpoint 0 */
110 /* Input: length, data stream */
111
112#define CMD_WR_USB_DATA5 0x2A /* Device Mode: write to usb endpoint 1 */
113 /* Input: length, data stream */
114
115#define CMD_WR_USB_DATA7 0x2B /* write to usb endpoint 2 */
116 /* Input: length, data stream */
117
118 /* ************************************************************************** */
119 /* Host Mode, only for CH375 */
120
121#define CMD_SET_BAUDRATE 0x02 /* Serial Mode: setup serial baudrate */
122 /* Input: baudrate pll divider */
123 /* Output: State( CMD_RET_SUCCESS or CMD_RET_ABORT, otherswise unfinished ) */
124
125#define CMD_ABORT_NAK 0x17 /* Host Mode: abort nak retry */
126
127#define CMD_SET_RETRY 0x0B /* Host Mode: setup usb token retry times */
128 /* Input: 0x25, setup retry times */
129 /* bit7=1 for infinite retry, bit3~0 retry times*/
130
131#define CMD_ISSUE_TOKEN 0x4F /* Host Mode: issue the token */
132 /* Input: token */
133 /* bit3~0:token type, bit7~4:endpoint */
134 /* Output interrupt state */
135
136#define CMD_CLR_STALL 0x41 /* Host Mode: clear the endpoint Error */
137 /* Input: endpoint index */
138 /* Output interrupt state */
139
140#define CMD_SET_ADDRESS 0x45 /* Host Mode: Control-setup usb address */
141 /* Input: address */
142 /* Output interrupt state */
143
144#define CMD_GET_DESCR 0x46 /* Host Mode: Control-get descriptor */
145 /* Input: descriptor type 0:config 1:device */
146 /* Output interrupt state */
147
148#define CMD_SET_CONFIG 0x49 /* Host Mode: Control-config and enable the device */
149 /* Input: config */
150 /* Output interrupt state */
151
152#define CMD_DISK_INIT 0x51 /* Host Mode: init usb storage device */
153 /* Output interrupt state */
154
155#define CMD_DISK_RESET 0x52 /* Host Mode: reset usb storage device */
156 /* Output interrupt state */
157
158#define CMD_DISK_SIZE 0x53 /* Host Mode: get the capacity of usb storage device */
159 /* Output interrupt state */
160
161#define CMD_DISK_READ 0x54 /* Host Mode: read from usb storage device(512byte) */
162 /* Input: LBA sector address(32bit, LSB), Sector(01H~FFH) */
163 /* Output interrupt state */
164
165#define CMD_DISK_RD_GO 0x55 /* Host Mode: continus read */
166 /* Output interrupt state */
167
168#define CMD_DISK_WRITE 0x56 /* Host Mode: write to usb storage device(512byte) */
169 /* Input: LBA sector address(32bit, LSB), Sector(01H~FFH) */
170 /* Output interrupt state */
171
172#define CMD_DISK_WR_GO 0x57 /* Host Mode: continus write */
173 /* Output interrupt state */
174
175 /* ************************************************************************** */
176 /* only for CH372A/CH375A */
177
178#define CMD_GET_IC_VER 0x01 /* get chip version */
179 /* Output: Version( bit7:1, bit6:0, bit5~0:Version ) */
180 /* CH375:5FH(invalid), CH375A:0A2H */
181
182#define CMD_ENTER_SLEEP 0x03 /* into sleep mode */
183
184#define CMD_RD_USB_DATA0 0x27 /* read from usb endpoint */
185 /* Output: length, data stream */
186
187#define CMD_DELAY_100US 0x0F /* Parallel : delay 100uS */
188 /* Output: 0 during delay, none zero after delay */
189
190#define CMD_CHK_SUSPEND 0x0B /* Device Mode: check usb bus hung */
191 /* Input: 10H, TYPE */
192 /* TYPE:00H=don't check usb hung, 04H=check hung every 50ms, 05H=check hung every 10ms */
193
194#define CMD_SET_SYS_FREQ 0x04 /* setup system working frequency */
195 /* Input: frequency */
196 /* 00H=12MHz, 01H=1.5MHz */
197
198 /* ************************************************************************** */
199 /* only for CH375A */
200
201#define CMD_TEST_CONNECT 0x16 /* Host Mode: check usb device connection */
202 /* Output: state( USB_INT_CONNECT of USB_INT_DISCONNECT, otherswise unfinished ) */
203
204#define CMD_AUTO_SETUP 0x4D /* Host Mode: auto setup usb device */
205 /* Output interrupt state */
206
207#define CMD_ISSUE_TKN_X 0x4E /* Host Mode: issue sync token */
208 /* Input: sync flag, token */
209 /* sync flag: bit7:IN endpoint sync flag, OUT endpoint sync flag, bit5~0:should be zero */
210 /* token:bit3~0:token type, bit7~4:endpoint */
211 /* Output interrupt state */
212
213
214 /* ********************************************************************************************************************* */
215 /* operate mode */
216
217#define CMD_RET_SUCCESS 0x51 /* success */
218#define CMD_RET_ABORT 0x5F /* fail */
219
220 /* ********************************************************************************************************************* */
221 /* USB interrupt state */
222
223 /* special interrupt state, only for CH372A/CH375A */
224#define USB_INT_USB_SUSPEND 0x05 /* usb bus susspend */
225#define USB_INT_WAKE_UP 0x06 /* wake up */
226
227 /* 0XH for USBDevice Mode */
228#define USB_INT_EP0_SETUP 0x0C /* USB ep0 SETUP */
229#define USB_INT_EP0_OUT 0x00 /* USB ep0 OUT */
230#define USB_INT_EP0_IN 0x08 /* USB ep0 IN */
231#define USB_INT_EP1_OUT 0x01 /* USB ep1 OUT */
232#define USB_INT_EP1_IN 0x09 /* USB ep1 IN */
233#define USB_INT_EP2_OUT 0x02 /* USB ep2 OUT */
234#define USB_INT_EP2_IN 0x0A /* USB ep2 IN */
235 /* USB_INT_BUS_RESET 0x0000XX11B */ /* USB BUS RESET */
236#define USB_INT_BUS_RESET1 0x03 /* USB BUS RESET */
237#define USB_INT_BUS_RESET2 0x07 /* USB BUS RESET */
238#define USB_INT_BUS_RESET3 0x0B /* USB BUS RESET */
239#define USB_INT_BUS_RESET4 0x0F /* USB BUS RESET */
240
241 /* 2XH-3XH fail state response, only for CH375/CH375A */
242 /* bit7~6 00 */
243 /* bit5 1 */
244 /* bit4 if package sync */
245 /* bit3~0 fail response from usb device: 0010=ACK, 1010=NAK, 1110=STALL, 0011=DATA0, 1011=DATA1, XX00=TimeOut */
246 /* USB_INT_RET_ACK 0x001X0010B */ /* Error:Ack to IN token */
247 /* USB_INT_RET_NAK 0x001X1010B */ /* Error:NAK */
248 /* USB_INT_RET_STALL 0x001X1110B */ /* Error:STALL */
249 /* USB_INT_RET_DATA0 0x001X0011B */ /* Error:DATA0 to OUT/SETUP*/
250 /* USB_INT_RET_DATA1 0x001X1011B */ /* Error:DATA1 to OUT/SETUP */
251 /* USB_INT_RET_TOUT 0x001XXX00B */ /* Error:TimeOut */
252 /* USB_INT_RET_TOGX 0x0010X011B */ /* Error:Unsync to IN */
253 /* USB_INT_RET_PID 0x001XXXXXB */ /* Error:refer to PID */
254
255 /* 0x1X control state, only for CH375/CH375A */
256
257#define USB_INT_SUCCESS 0x14 /* token successful sent */
258#define USB_INT_CONNECT 0x15 /* detect usb device plug in */
259#define USB_INT_DISCONNECT 0x16 /* detect usb device plug out */
260#define USB_INT_BUF_OVER 0x17 /* USB control buffer overflow */
261#define USB_INT_DISK_READ 0x1D /* USB storage, read request */
262#define USB_INT_DISK_WRITE 0x1E /* USB storage, write request */
263#define USB_INT_DISK_ERR 0x1F /* USB storage, fail */
264
265 /* ********************************************************************************************************************* */
266 /* USB package identifer PID, for host mode */
267#define DEF_USB_PID_NULL 0x00 /* reserve PID, not defined */
268#define DEF_USB_PID_SOF 0x05
269#define DEF_USB_PID_SETUP 0x0D
270#define DEF_USB_PID_IN 0x09
271#define DEF_USB_PID_OUT 0x01
272#define DEF_USB_PID_ACK 0x02
273#define DEF_USB_PID_NAK 0x0A
274#define DEF_USB_PID_STALL 0x0E
275#define DEF_USB_PID_DATA0 0x03
276#define DEF_USB_PID_DATA1 0x0B
277#define DEF_USB_PID_PRE 0x0C
278
279 /* USB request type, for external firmware mode */
280#define DEF_USB_REQ_READ 0x80 /* control read request */
281#define DEF_USB_REQ_WRITE 0x00 /* control write request */
282#define DEF_USB_REQ_TYPE 0x60 /* control type request */
283#define DEF_USB_REQ_STAND 0x00 /* standard request */
284#define DEF_USB_REQ_CLASS 0x20 /* device class request */
285#define DEF_USB_REQ_VENDOR 0x40 /* vendor request */
286#define DEF_USB_REQ_RESERVE 0x60 /* reserved request */
287
288 /* USB standard device request, RequestType bit6~5=00(Standard), for external firmware mode */
289#define DEF_USB_CLR_FEATURE 0x01
290#define DEF_USB_SET_FEATURE 0x03
291#define DEF_USB_GET_STATUS 0x00
292#define DEF_USB_SET_ADDRESS 0x05
293#define DEF_USB_GET_DESCR 0x06
294#define DEF_USB_SET_DESCR 0x07
295#define DEF_USB_GET_CONFIG 0x08
296#define DEF_USB_SET_CONFIG 0x09
297#define DEF_USB_GET_INTERF 0x0A
298#define DEF_USB_SET_INTERF 0x0B
299#define DEF_USB_SYNC_FRAME 0x0C
300
301//Define struct
302/* ********************************************************************************************************************* */
304 uint8_t bLength;
305 uint8_t bDescriptorType;
306 unsigned short bcdUSB;
307 uint8_t bDeviceClass;
308 uint8_t bDeviceSubClass;
309 uint8_t bDeviceProtocol;
310 uint8_t bMaxPacketSize0;
311 unsigned short idVendor;
312 unsigned short idProduct;
313 unsigned short bcdDevice;
314 uint8_t iManufacturer;
315 uint8_t iProduct;
316 uint8_t iSerialNumber;
317 uint8_t bNumConfigurations;
319
321 uint8_t bLength;
322 uint8_t bDescriptorType;
323 unsigned short wTotalLength;
324 uint8_t bNumInterfaces;
325 uint8_t bConfigurationvalue;
326 uint8_t iConfiguration;
327 uint8_t bmAttributes;
328 uint8_t MaxPower;
330
332 uint8_t bLength;
333 uint8_t bDescriptorType;
334 uint8_t bInterfaceNumber;
335 uint8_t bAlternateSetting;
336 uint8_t bNumEndpoints;
337 uint8_t bInterfaceClass;
338 uint8_t bInterfaceSubClass;
339 uint8_t bInterfaceProtocol;
340 uint8_t iInterface;
342
344 uint8_t bLength;
345 uint8_t bDescriptorType;
346 uint8_t bEndpointAddress;
347 uint8_t bmAttributes;
348 unsigned short wMaxPacketSize;
349 uint8_t bInterval;
351
353 USB_CFG_DESCR cfg_descr;
354 USB_ITF_DESCR itf_descr;
355 USB_ENDP_DESCR endp_descr[4];
357
363class MeUSBHost : public MePort
364{
365public:
366 bool ch375_online;
367 bool device_online;
368 bool device_ready;
369 uint8_t RECV_BUFFER[ CH375_MAX_DATA_LEN ];
370
377 MeUSBHost();
378
379 MeUSBHost(uint8_t s1, uint8_t s2);
380
387 MeUSBHost(uint8_t port);
388
403 void init(int8_t type);
404
419 int16_t initHIDDevice();
420
435 int16_t probeDevice();
436
451 void resetBus();
452
467 uint8_t host_recv();
468
469private:
470 MeSerial *HSerial;
471 int16_t stallCount;
472 int8_t usbtype;
473
488 uint8_t CH375_RD();
489
504 void CH375_WR(uint8_t c);
505
520 int16_t set_usb_mode(int16_t mode);
521
536 uint8_t getIrq();
537
552 void toggle_send();
553
568 void toggle_recv();
569
584 uint8_t issue_token( uint8_t endp_and_pid );
585
602 void wr_usb_data( uint8_t len, uint8_t *buf );
603
620 uint8_t rd_usb_data( uint8_t *buf );
621
636 int16_t get_version();
637
652 void set_freq(void);
653
668 uint8_t set_addr( uint8_t addr );
669
684 uint8_t set_config(uint8_t cfg);
685
700 uint8_t clr_stall6(void);
701
716 uint8_t get_desr(uint8_t type);
717};
718#endif
719
Configuration file of library.
Header for MePort.cpp module.
Header for for MeSerial.cpp module.
Port Mapping for RJ25.
Definition MePort.h:128
uint8_t s2
Definition MePort.h:444
uint8_t s1
Definition MePort.h:438
Driver for serial.
Definition MeSerial.h:67
Driver for Me USB Host module.
Definition MeUSBHost.h:364
MeUSBHost()
Definition MeUSBHost.cpp:77
void resetBus()
Definition MeUSBHost.cpp:535
int16_t probeDevice()
Definition MeUSBHost.cpp:693
void init(int8_t type)
Definition MeUSBHost.cpp:568
int16_t initHIDDevice()
Definition MeUSBHost.cpp:597
uint8_t host_recv()
Definition MeUSBHost.cpp:479
Definition MeUSBHost.h:352
Definition MeUSBHost.h:320
Definition MeUSBHost.h:303
Definition MeUSBHost.h:343
Definition MeUSBHost.h:331