µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
usb-host.h
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2016-2025 Liviu Ionescu. All rights reserved.
4 * Copyright (c) 2013-2014 ARM Ltd.
5 *
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose is hereby granted, under the terms of the MIT license.
8 *
9 * If a copy of the license was not distributed with this file, it can
10 * be obtained from https://opensource.org/licenses/mit.
11 */
12
13/*
14 * The code is inspired by ARM CMSIS Driver_USBH.h file, v2.01,
15 * and tries to remain functionally close to the CMSIS specifications.
16 */
17
18#ifndef CMSIS_PLUS_DRIVER_USB_HOST_H_
19#define CMSIS_PLUS_DRIVER_USB_HOST_H_
20
21// ----------------------------------------------------------------------------
22
23#ifdef __cplusplus
24
25// ----------------------------------------------------------------------------
26
29
30#include <cstdint>
31#include <cstddef>
32
33// ----------------------------------------------------------------------------
34
35#pragma GCC diagnostic push
36#if defined(__clang__)
37#pragma clang diagnostic ignored "-Wc++98-compat"
38#endif
39
40// ----------------------------------------------------------------------------
41
42namespace os
43{
44 namespace driver
45 {
46 namespace usb
47 {
48 namespace host
49 {
50 // ====================================================================
51
52#pragma GCC diagnostic push
53#if defined(__clang__)
54#pragma clang diagnostic ignored "-Wpadded"
55#elif defined(__GNUC__)
56#pragma GCC diagnostic ignored "-Wpadded"
57#endif
58
63 {
64 public:
65 // For compatibility with ARM CMSIS, these bits should be
66 // exactly in this order.
67
69 uint32_t port_mask : 15;
70
72 bool auto_split : 1;
73
75 bool event_connect : 1;
76
79
82 };
83
84#pragma GCC diagnostic pop
85
86 // ====================================================================
87 // ----- USB Host Status -----
88
89#pragma GCC diagnostic push
90#if defined(__clang__)
91#pragma clang diagnostic ignored "-Wpadded"
92#elif defined(__GNUC__)
93#pragma GCC diagnostic ignored "-Wpadded"
94#endif
95
99 class Status
100 {
101 public:
102 bool
103 is_connected (void) const noexcept;
104
105 bool
106 is_overcurrent (void) const noexcept;
107
108 speed_t
109 get_speed (void) const noexcept;
110
111 // ------------------------------------------------------------------
112
113 public:
114 // For compatibility with ARM CMSIS, these bits should be
115 // exactly in this order.
116
118 bool connected : 1;
119
121 bool overcurrent : 1;
122
125 };
126
127#pragma GCC diagnostic pop
128
129 // ====================================================================
130 // ----- USB Host Port Events -----
131
136 {
138 connect = (1UL << 0),
139
141 disconnect = (1UL << 1),
142
144 overcurrent = (1UL << 2),
145
147 reset = (1UL << 3),
148
150 suspend = (1UL << 4),
151
153 resume = (1UL << 5),
154
156 remote_hangup = (1UL << 6)
157 };
158
159 // ====================================================================
160 // ----- USB Host Pipe Event -----
161
168 {
170 transfer_complete = (1UL << 0),
171
173 handshake_nak = (1UL << 1),
174
176 handshake_nyet = (1UL << 2),
177
179 handshake_mdata = (1UL << 3),
180
182 handshake_stall = (1UL << 4),
183
185 handshake_err = (1UL << 5),
186
188 bus_err = (1UL << 6)
189 };
190
191 // --------------------------------------------------------------------
192
193 typedef void (*signal_port_event_t) (const void* object, port_t port,
194 event_t event);
195
196 typedef void (*signal_pipe_event_t) (const void* object, pipe_t pipe,
197 event_t event);
198
199 } /* namespace host */
200
201 // ======================================================================
202
203#pragma GCC diagnostic push
204#if defined(__clang__)
205#pragma clang diagnostic ignored "-Wpadded"
206#elif defined(__GNUC__)
207#pragma GCC diagnostic ignored "-Wpadded"
208#endif
209
210 class Host : public Base
211 {
212
213 public:
214 // --------------------------------------------------------------------
215
216 Host () noexcept;
217
218 Host (const Host&) = delete;
219
220 Host (Host&&) = delete;
221
222 Host&
224 = delete;
225
226 Host&
228 = delete;
229
230 virtual ~Host () noexcept override;
231
232 // --------------------------------------------------------------------
233
240 void
241 register_port_callback (host::signal_port_event_t cb_func,
242 const void* cb_object = nullptr) noexcept;
243
244 void
245 register_pipe_callback (host::signal_pipe_event_t cb_func,
246 const void* cb_object = nullptr) noexcept;
247
248 // --------------------------------------------------------------------
249
250 const host::Capabilities&
251 get_capabilities (void) noexcept;
252
254 power_port_vbus (port_t port, bool vbus) noexcept;
255
257 reset_port (port_t port) noexcept;
258
260 suspend_port (port_t port) noexcept;
261
263 resume_port (port_t port) noexcept;
264
265 host::Status&
266 get_port_status (port_t port) noexcept;
267
268 pipe_t
269 create_pipe (device_address_t dev_addr, speed_t dev_speed,
270 hub_addr_t hub_addr, hub_port_t hub_port,
271 endpoint_t ep_addr, endpoint_type_t ep_type,
272 packet_size_t ep_max_packet_size,
273 polling_interval_t ep_interval) noexcept;
274
276 modify_pipe (pipe_t pipe, device_address_t dev_addr, speed_t dev_speed,
277 hub_addr_t hub_addr, hub_port_t hub_port,
278 packet_size_t ep_max_packet_size) noexcept;
279
281 delete_pipe (pipe_t pipe) noexcept;
282
284 reset_pipe (pipe_t pipe) noexcept;
285
287 transfer (pipe_t pipe, uint32_t packet, uint8_t* data,
288 std::size_t num) noexcept;
289
290 std::size_t
291 get_transfer_count (pipe_t pipe) noexcept;
292
294 abort_transfer (pipe_t pipe) noexcept;
295
296 uint16_t
297 get_frame_number (void) noexcept;
298
299 void
300 signal_port_event (port_t port, event_t event) noexcept;
301
302 void
303 signal_pipe_event (pipe_t pipe, event_t event) noexcept;
304
305 protected:
306 virtual const host::Capabilities&
307 do_get_capabilities (void) noexcept
308 = 0;
309
310 virtual return_t
311 do_power_port_vbus (port_t port, bool vbus) noexcept
312 = 0;
313
314 virtual return_t
315 do_reset_port (port_t port) noexcept
316 = 0;
317
318 virtual return_t
319 do_suspend_port (port_t port) noexcept
320 = 0;
321
322 virtual return_t
323 do_resume_port (port_t port) noexcept
324 = 0;
325
326 virtual host::Status&
328 = 0;
329
330 virtual pipe_t
332 hub_addr_t hub_addr, hub_port_t hub_port,
333 endpoint_t ep_addr, endpoint_type_t ep_type,
334 packet_size_t ep_max_packet_size,
335 polling_interval_t ep_interval) noexcept
336 = 0;
337
338 virtual return_t
340 speed_t dev_speed, hub_addr_t hub_addr,
341 hub_port_t hub_port,
342 packet_size_t ep_max_packet_size) noexcept
343 = 0;
344
345 virtual return_t
346 do_delete_pipe (pipe_t pipe) noexcept
347 = 0;
348
349 virtual return_t
350 do_reset_pipe (pipe_t pipe) noexcept
351 = 0;
352
353 virtual return_t
354 do_transfer (pipe_t pipe, uint32_t packet, uint8_t* data,
355 std::size_t num) noexcept
356 = 0;
357
358 virtual std::size_t
360 = 0;
361
362 virtual return_t
363 do_abort_transfer (pipe_t pipe) noexcept
364 = 0;
365
366 virtual uint16_t
367 do_get_frame_number (void) noexcept
368 = 0;
369
370 private:
372 host::signal_port_event_t cb_port_func_;
373
375 const void* cb_port_object_;
376
378 host::signal_pipe_event_t cb_pipe_func_;
379
381 const void* cb_pipe_object_;
382
383 protected:
384 host::Status status_;
385 };
386
387#pragma GCC diagnostic pop
388
389 // ----------------------------------------------------------------------
390 // ----- Definitions -----
391
392 namespace host
393 {
394 // --------------------------------------------------------------------
395
396 inline bool
397 Status::is_connected (void) const noexcept
398 {
399 return connected;
400 }
401
402 inline bool
403 Status::is_overcurrent (void) const noexcept
404 {
405 return overcurrent;
406 }
407
408 inline speed_t
409 Status::get_speed (void) const noexcept
410 {
411 return speed;
412 }
413
414 } /* namespace host */
415
416 // ----------------------------------------------------------------------
417
418 inline const host::Capabilities&
420 {
421 return do_get_capabilities ();
422 }
423
424 inline return_t
425 Host::power_port_vbus (port_t port, bool vbus) noexcept
426 {
427 return do_power_port_vbus (port, vbus);
428 }
429
430 inline return_t
431 Host::reset_port (port_t port) noexcept
432 {
433 return do_reset_port (port);
434 }
435
436 inline return_t
438 {
439 return do_suspend_port (port);
440 }
441
442 inline return_t
443 Host::resume_port (port_t port) noexcept
444 {
445 return do_resume_port (port);
446 }
447
448 inline host::Status&
450 {
451 return do_get_port_status (port);
452 }
453
454 inline pipe_t
456 hub_addr_t hub_addr, hub_port_t hub_port,
457 endpoint_t ep_addr, endpoint_type_t ep_type,
458 packet_size_t ep_max_packet_size,
459 polling_interval_t ep_interval) noexcept
460 {
461 return do_create_pipe (dev_addr, dev_speed, hub_addr, hub_port,
462 ep_addr, ep_type, ep_max_packet_size,
463 ep_interval);
464 }
465
466 inline return_t
468 speed_t dev_speed, hub_addr_t hub_addr,
469 hub_port_t hub_port,
470 packet_size_t ep_max_packet_size) noexcept
471 {
472 return do_modify_pipe (pipe, dev_addr, dev_speed, hub_addr, hub_port,
473 ep_max_packet_size);
474 }
475
476 inline return_t
477 Host::delete_pipe (pipe_t pipe) noexcept
478 {
479 return do_delete_pipe (pipe);
480 }
481
482 inline return_t
483 Host::reset_pipe (pipe_t pipe) noexcept
484 {
485 return do_reset_pipe (pipe);
486 }
487
488 inline std::size_t
490 {
491 return do_get_transfer_count (pipe);
492 }
493
494 inline return_t
496 {
497 return do_abort_transfer (pipe);
498 }
499
500 inline uint16_t
502 {
503 return do_get_frame_number ();
504 }
505
506 } /* namespace usb */
507 } /* namespace driver */
508} /* namespace os */
509
510#pragma GCC diagnostic pop
511
512// ----------------------------------------------------------------------------
513
514#endif /* __cplusplus */
515
516// ----------------------------------------------------------------------------
517
518#endif /* CMSIS_PLUS_DRIVER_USB_HOST_H_ */
Host(const Host &)=delete
virtual return_t do_power_port_vbus(port_t port, bool vbus) noexcept=0
host::signal_port_event_t cb_port_func_
Pointer to static function that implements the port callback.
Definition usb-host.h:372
virtual return_t do_suspend_port(port_t port) noexcept=0
return_t reset_port(port_t port) noexcept
Definition usb-host.h:431
return_t delete_pipe(pipe_t pipe) noexcept
Definition usb-host.h:477
Host & operator=(const Host &)=delete
const host::Capabilities & get_capabilities(void) noexcept
Definition usb-host.h:419
virtual host::Status & do_get_port_status(port_t port) noexcept=0
virtual return_t do_reset_pipe(pipe_t pipe) noexcept=0
virtual return_t do_delete_pipe(pipe_t pipe) noexcept=0
Host(Host &&)=delete
host::Status & get_port_status(port_t port) noexcept
Definition usb-host.h:449
pipe_t create_pipe(device_address_t dev_addr, speed_t dev_speed, hub_addr_t hub_addr, hub_port_t hub_port, endpoint_t ep_addr, endpoint_type_t ep_type, packet_size_t ep_max_packet_size, polling_interval_t ep_interval) noexcept
Definition usb-host.h:455
void register_port_callback(host::signal_port_event_t cb_func, const void *cb_object=nullptr) noexcept
Register port event callback.
Definition usb-host.cpp:55
virtual return_t do_resume_port(port_t port) noexcept=0
void signal_pipe_event(pipe_t pipe, event_t event) noexcept
Definition usb-host.cpp:97
virtual uint16_t do_get_frame_number(void) noexcept=0
virtual return_t do_modify_pipe(pipe_t pipe, device_address_t dev_addr, speed_t dev_speed, hub_addr_t hub_addr, hub_port_t hub_port, packet_size_t ep_max_packet_size) noexcept=0
host::signal_pipe_event_t cb_pipe_func_
Pointer to static function that implements the pipe callback.
Definition usb-host.h:378
return_t transfer(pipe_t pipe, uint32_t packet, uint8_t *data, std::size_t num) noexcept
Definition usb-host.cpp:73
return_t power_port_vbus(port_t port, bool vbus) noexcept
Definition usb-host.h:425
return_t resume_port(port_t port) noexcept
Definition usb-host.h:443
virtual return_t do_abort_transfer(pipe_t pipe) noexcept=0
return_t modify_pipe(pipe_t pipe, device_address_t dev_addr, speed_t dev_speed, hub_addr_t hub_addr, hub_port_t hub_port, packet_size_t ep_max_packet_size) noexcept
Definition usb-host.h:467
virtual pipe_t do_create_pipe(device_address_t dev_addr, speed_t dev_speed, hub_addr_t hub_addr, hub_port_t hub_port, endpoint_t ep_addr, endpoint_type_t ep_type, packet_size_t ep_max_packet_size, polling_interval_t ep_interval) noexcept=0
const void * cb_pipe_object_
Pointer to object instance associated with the pipe callback.
Definition usb-host.h:381
virtual const host::Capabilities & do_get_capabilities(void) noexcept=0
virtual std::size_t do_get_transfer_count(pipe_t pipe) noexcept=0
return_t suspend_port(port_t port) noexcept
Definition usb-host.h:437
const void * cb_port_object_
Pointer to object instance associated with the port callback.
Definition usb-host.h:375
return_t reset_pipe(pipe_t pipe) noexcept
Definition usb-host.h:483
std::size_t get_transfer_count(pipe_t pipe) noexcept
Definition usb-host.h:489
uint16_t get_frame_number(void) noexcept
Definition usb-host.h:501
host::Status status_
Definition usb-host.h:384
virtual return_t do_reset_port(port_t port) noexcept=0
virtual ~Host() noexcept override
Definition usb-host.cpp:49
void register_pipe_callback(host::signal_pipe_event_t cb_func, const void *cb_object=nullptr) noexcept
Definition usb-host.cpp:63
void signal_port_event(port_t port, event_t event) noexcept
Definition usb-host.cpp:87
return_t abort_transfer(pipe_t pipe) noexcept
Definition usb-host.h:495
virtual return_t do_transfer(pipe_t pipe, uint32_t packet, uint8_t *data, std::size_t num) noexcept=0
USB host driver capabilities.
Definition usb-host.h:63
bool event_connect
Signal Connect event.
Definition usb-host.h:75
bool event_overcurrent
Signal Overcurrent event.
Definition usb-host.h:81
uint32_t port_mask
Root HUB available Ports Mask.
Definition usb-host.h:69
bool auto_split
Automatic SPLIT packet handling.
Definition usb-host.h:72
bool event_disconnect
Signal Disconnect event.
Definition usb-host.h:78
speed_t get_speed(void) const noexcept
Definition usb-host.h:409
bool is_connected(void) const noexcept
Definition usb-host.h:397
bool is_overcurrent(void) const noexcept
Definition usb-host.h:403
bool overcurrent
USB Host Port overcurrent flag.
Definition usb-host.h:121
speed_t speed
USB Host Port speed setting (ARM_USB_SPEED_xxx).
Definition usb-host.h:124
bool connected
USB Host Port connected flag.
Definition usb-host.h:118
Port_event
USB Host Port Events.
Definition usb-host.h:136
@ resume
USB Resume occurred.
Definition usb-host.h:153
@ disconnect
USB Device Disconnected from Port.
Definition usb-host.h:141
@ suspend
USB Suspend occurred.
Definition usb-host.h:150
@ remote_hangup
USB Device activated Remote Wakeup.
Definition usb-host.h:156
@ overcurrent
USB Device caused Overcurrent.
Definition usb-host.h:144
@ connect
USB Device Connected to Port.
Definition usb-host.h:138
@ reset
USB Reset completed.
Definition usb-host.h:147
void(* signal_pipe_event_t)(const void *object, pipe_t pipe, event_t event)
Definition usb-host.h:196
Pipe_event
USB Host Pipe Event.
Definition usb-host.h:168
@ handshake_nak
NAK Handshake received.
Definition usb-host.h:173
@ transfer_complete
Transfer completed.
Definition usb-host.h:170
@ handshake_stall
STALL Handshake received.
Definition usb-host.h:182
@ handshake_err
ERR Handshake received.
Definition usb-host.h:185
@ handshake_mdata
MDATA Handshake received.
Definition usb-host.h:179
@ bus_err
Bus Error detected.
Definition usb-host.h:188
@ handshake_nyet
NYET Handshake received.
Definition usb-host.h:176
void(* signal_port_event_t)(const void *object, port_t port, event_t event)
Definition usb-host.h:193
uint8_t speed_t
Definition usb.h:45
uint8_t endpoint_t
Definition usb.h:64
uint32_t pipe_t
Definition usb.h:102
uint8_t hub_addr_t
Definition usb.h:105
uint8_t device_address_t
Definition usb.h:99
uint8_t endpoint_type_t
Definition usb.h:71
uint16_t packet_size_t
Definition usb.h:92
uint8_t port_t
Definition usb.h:103
uint8_t polling_interval_t
Definition usb.h:108
uint8_t hub_port_t
Definition usb.h:106
int32_t return_t
Definition common.h:46
uint32_t event_t
Definition common.h:45
System namespace.
Standard std namespace.