Root/atusb/fw/usb/usb.h

Source at commit 9b2696b8488dbc39b0fdcdefdfad775913cb6e2d created 7 years 5 months ago.
By Werner Almesberger, atusb/fw/usb/: enable MCU reset on USB bus reset after config selection
1/*
2 * fw/usb//usb.h - USB hardware setup and standard device requests
3 *
4 * Written 2008, 2009, 2011, 2013 by Werner Almesberger
5 * Copyright 2008, 2009, 2011, 2013 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#ifndef USB_H
15#define USB_H
16
17
18#include <stdbool.h>
19#include <stdint.h>
20
21
22/*
23 * Descriptor types
24 *
25 * Reuse libusb naming scheme (/usr/include/usb.h)
26 */
27
28#define USB_DT_DEVICE 1
29#define USB_DT_CONFIG 2
30#define USB_DT_STRING 3
31#define USB_DT_INTERFACE 4
32#define USB_DT_ENDPOINT 5
33
34/*
35 * Device classes
36 *
37 * Reuse libusb naming scheme (/usr/include/usb.h)
38 */
39
40#define USB_CLASS_PER_INTERFACE 0xfe
41#define USB_CLASS_VENDOR_SPEC 0xff
42
43/*
44 * Configuration attributes
45 */
46
47#define USB_ATTR_BUS_POWERED 0x80
48#define USB_ATTR_SELF_POWERED 0x40
49#define USB_ATTR_REMOTE_WAKEUP 0x20
50
51/*
52 * Setup request types
53 */
54
55#define TO_DEVICE(req) (0x00 | (req) << 8)
56#define FROM_DEVICE(req) (0x80 | (req) << 8)
57#define TO_INTERFACE(req) (0x01 | (req) << 8)
58#define FROM_INTERFACE(req) (0x81 | (req) << 8)
59#define TO_ENDPOINT(req) (0x02 | (req) << 8)
60#define FROM_ENDPOINT(req) (0x82 | (req) << 8)
61
62/*
63 * Setup requests
64 */
65
66#define GET_STATUS 0x00
67#define CLEAR_FEATURE 0x01
68#define SET_FEATURE 0x03
69#define SET_ADDRESS 0x05
70#define GET_DESCRIPTOR 0x06
71#define SET_DESCRIPTOR 0x07
72#define GET_CONFIGURATION 0x08
73#define SET_CONFIGURATION 0x09
74#define GET_INTERFACE 0x0a
75#define SET_INTERFACE 0x0b
76#define SYNCH_FRAME 0x0c
77
78/*
79 * USB Language ID codes
80 *
81 * http://www.usb.org/developers/docs/USB_LANGIDs.pdf
82 */
83
84#define USB_LANGID_ENGLISH_US 0x409
85
86
87/*
88 * Odd. sdcc seems to think "x" assumes the size of the destination, i.e.,
89 * uint8_t. Hence the cast.
90 */
91
92#define LE(x) ((uint16_t) (x) & 0xff), ((uint16_t) (x) >> 8)
93
94#define LO(x) (((uint8_t *) &(x))[0])
95#define HI(x) (((uint8_t *) &(x))[1])
96
97
98#ifdef LOW_SPEED
99#define EP0_SIZE 8
100#else
101#define EP0_SIZE 64
102#endif
103
104#define EP1_SIZE 64 /* simplify */
105
106
107enum ep_state {
108    EP_IDLE,
109    EP_RX,
110    EP_TX,
111    EP_STALL,
112};
113
114struct ep_descr {
115    enum ep_state state;
116    uint8_t *buf;
117    uint8_t *end;
118    uint8_t size;
119    void (*callback)(void *user);
120    void *user;
121};
122
123struct setup_request {
124    uint8_t bmRequestType;
125    uint8_t bRequest;
126    uint16_t wValue;
127    uint16_t wIndex;
128    uint16_t wLength;
129};
130
131
132extern const uint8_t device_descriptor[];
133extern const uint8_t config_descriptor[];
134extern struct ep_descr eps[];
135
136extern bool (*user_setup)(const struct setup_request *setup);
137extern void (*user_set_interface)(int nth);
138extern bool (*user_get_descriptor)(uint8_t type, uint8_t index,
139    const uint8_t **reply, uint8_t *size);
140extern void (*user_reset)(void);
141
142
143#define usb_left(ep) ((ep)->end-(ep)->buf)
144#define usb_send(ep, buf, size, callback, user) \
145    usb_io(ep, EP_TX, (void *) buf, size, callback, user)
146#define usb_recv(ep, buf, size, callback, user) \
147    usb_io(ep, EP_RX, buf, size, callback, user)
148
149void usb_io(struct ep_descr *ep, enum ep_state state, uint8_t *buf,
150    uint8_t size, void (*callback)(void *user), void *user);
151
152bool handle_setup(const struct setup_request *setup);
153void set_addr(uint8_t addr);
154void usb_ep_change(struct ep_descr *ep);
155void usb_reset(void);
156void usb_enable_bus_reset(void);
157void usb_init(void);
158
159#endif /* !USB_H */
160

Archive Download this file



interactive