|
17 | 17 | #ifndef USBPHY_H
|
18 | 18 | #define USBPHY_H
|
19 | 19 |
|
20 |
| -#include <stdint.h> |
| 20 | +#include "USBPhyTypes.h" |
| 21 | +#include "USBPhyEvents.h" |
21 | 22 |
|
22 |
| -/** |
23 |
| - * \defgroup hal_usb_device USB Device HAL |
24 |
| - * Abstract interface to physical USB hardware |
| 23 | +/** Abstract interface to physical USB hardware |
25 | 24 | *
|
26 | 25 | * # Defined behavior
|
27 | 26 | * * Any endpoint configurations which fit in the parameters of the table returned
|
28 |
| - * by USBPhy::endpointTable can be used. |
| 27 | + * by USBPhy::endpoint_table can be used. |
29 | 28 | * * All endpoints in any valid endpoint configuration can be used concurrently
|
30 | 29 | * * Device supports use of at least one control, bulk, interrupt and
|
31 | 30 | * isochronous in each direction at the same time - at least 8 endpoints.
|
|
49 | 48 | * * Devices behavior is undefined if latency is greater than 10ms when a reset occurs - see USB spec 7.1.7.5
|
50 | 49 | * * Calling any of the USBPhy::endpoint_* functions on endpoint 0
|
51 | 50 | *
|
52 |
| - * # Potential bugs |
53 |
| - * * Processing control packets in the wrong order when multiple packets are present |
54 |
| - * * Back to back setup packets handled incorrectly |
55 |
| - * * Signal corruption not handled correctly |
56 |
| - * * USB race conditions |
| 51 | + * # Notes |
| 52 | + * * Make sure USB packets are processed in the correct order when multiple packets are present. |
| 53 | + * Typically IN endpoints should be handled before OUT endpoints if both are pending. |
| 54 | + * * Setup packets may be resent if there is noise on the USB line. The USBPhy should be able |
| 55 | + * to gracefully handle this scenario and respond to the setup packet with an ACK. |
| 56 | + * * Bi-directional protocols making use of alternating IN and OUT phases should not rely |
| 57 | + * on the last ACK an IN transfer to indicate that the OUT phase should start. Instead, |
| 58 | + * the OUT phase should be started at the same time the last IN transfer is started. This |
| 59 | + * is because the ACK to the last in transfer may be dropped if there is noise on the USB |
| 60 | + * line. If dropped it will only get re-sent on the next IN phase. More info on this can be |
| 61 | + * found in section 8.5.3.3 of the USB spec. |
57 | 62 | *
|
| 63 | + * @ingroup usb_device_core |
58 | 64 | */
|
59 |
| - |
60 |
| -typedef uint8_t usb_ep_t; |
61 |
| - |
62 |
| -typedef enum { |
63 |
| - USB_EP_TYPE_CTRL = 0, |
64 |
| - USB_EP_TYPE_ISO = 1, |
65 |
| - USB_EP_TYPE_BULK = 2, |
66 |
| - USB_EP_TYPE_INT = 3 |
67 |
| -} usb_ep_type_t; |
68 |
| - |
69 |
| -enum { |
70 |
| - USB_EP_ATTR_ALLOW_CTRL = 1 << USB_EP_TYPE_CTRL, |
71 |
| - USB_EP_ATTR_ALLOW_BULK = 1 << USB_EP_TYPE_BULK, |
72 |
| - USB_EP_ATTR_ALLOW_INT = 1 << USB_EP_TYPE_INT, |
73 |
| - USB_EP_ATTR_ALLOW_ISO = 1 << USB_EP_TYPE_ISO, |
74 |
| - USB_EP_ATTR_ALLOW_ALL = USB_EP_ATTR_ALLOW_CTRL | USB_EP_ATTR_ALLOW_BULK | |
75 |
| - USB_EP_ATTR_ALLOW_INT | USB_EP_ATTR_ALLOW_ISO, |
76 |
| - |
77 |
| - USB_EP_ATTR_DIR_IN = 0 << 4, |
78 |
| - USB_EP_ATTR_DIR_OUT = 1 << 4, |
79 |
| - USB_EP_ATTR_DIR_IN_OR_OUT = 2 << 4, |
80 |
| - USB_EP_ATTR_DIR_IN_AND_OUT = 3 << 4, |
81 |
| - USB_EP_ATTR_DIR_MASK = 3 << 4 |
82 |
| -}; |
83 |
| -typedef uint8_t usb_ep_attr_t; |
84 |
| - |
85 |
| -struct usb_ep_entry_t { |
86 |
| - usb_ep_attr_t attributes; |
87 |
| - uint8_t byte_cost; |
88 |
| - uint16_t base_cost; |
89 |
| -}; |
90 |
| - |
91 |
| -struct usb_ep_table_t { |
92 |
| - uint32_t resources; |
93 |
| - usb_ep_entry_t table[16]; |
94 |
| -}; |
95 |
| - |
96 |
| -class USBPhyEvents { |
97 |
| -public: |
98 |
| - USBPhyEvents() {}; |
99 |
| - virtual ~USBPhyEvents() {}; |
100 |
| - |
101 |
| - /** |
102 |
| - * Callback called when a bus reset occurs |
103 |
| - * @note called in the contex of USBPhy::process |
104 |
| - */ |
105 |
| - virtual void reset() = 0; |
106 |
| - |
107 |
| - /** |
108 |
| - * Callback called when an endpoint 0 setup packet is received |
109 |
| - * @note called in the contex of USBPhy::process |
110 |
| - */ |
111 |
| - virtual void ep0_setup() = 0; |
112 |
| - |
113 |
| - /** |
114 |
| - * Callback called when an endpoint 0 out packet is received |
115 |
| - * @note called in the contex of USBPhy::process |
116 |
| - */ |
117 |
| - virtual void ep0_out() = 0; |
118 |
| - |
119 |
| - /** |
120 |
| - * Callback called when an endpoint 0 in packet is received |
121 |
| - * @note called in the contex of USBPhy::process |
122 |
| - */ |
123 |
| - virtual void ep0_in() = 0; |
124 |
| - |
125 |
| - /** |
126 |
| - * Callback called USB power is applied or removed |
127 |
| - * |
128 |
| - * @param powered true if USB power is present, false otherwise |
129 |
| - * @note called in the contex of USBPhy::process |
130 |
| - */ |
131 |
| - virtual void power(bool powered) = 0; |
132 |
| - |
133 |
| - /** |
134 |
| - * Callback called when entering or leaving suspend mode |
135 |
| - * |
136 |
| - * @param suspended true if entering suspend mode false otherwise |
137 |
| - * @note called in the contex of USBPhy::process |
138 |
| - */ |
139 |
| - virtual void suspend(bool suspended) = 0; |
140 |
| - |
141 |
| - /** |
142 |
| - * Callback called on start of frame |
143 |
| - * |
144 |
| - * @param frame_number The current frame number |
145 |
| - * @note This callback is enabled/disabled by |
146 |
| - * calling USBPhy::sof_enable / USBPhy::sof_disable |
147 |
| - * @note called in the contex of USBPhy::process |
148 |
| - */ |
149 |
| - virtual void sof(int frame_number) = 0; |
150 |
| - |
151 |
| - /** |
152 |
| - * Callback called on the reception of an OUT packet |
153 |
| - * |
154 |
| - * @param endpoint Endpoint which received the OUT packet |
155 |
| - * @note called in the contex of USBPhy::process |
156 |
| - */ |
157 |
| - virtual void out(usb_ep_t endpoint) = 0; |
158 |
| - |
159 |
| - /** |
160 |
| - * Callback called on the transmission of an IN packet |
161 |
| - * |
162 |
| - * @param endpoint Endpoint which sent the IN packet |
163 |
| - * @note called in the contex of USBPhy::process |
164 |
| - */ |
165 |
| - virtual void in(usb_ep_t endpoint) = 0; |
166 |
| - |
167 |
| - /** |
168 |
| - * Callback called to indicate the USB processing needs to be done |
169 |
| - */ |
170 |
| - virtual void start_process() = 0; |
171 |
| -}; |
172 |
| - |
173 | 65 | class USBPhy {
|
174 | 66 | public:
|
175 | 67 | USBPhy() {};
|
|
0 commit comments