µ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-device.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_USBD.h file, v2.01,
15 * and tries to remain functionally close to the CMSIS specifications.
16 */
17
18#ifndef CMSIS_PLUS_DRIVER_USB_DEVICE_H_
19#define CMSIS_PLUS_DRIVER_USB_DEVICE_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 device
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
70
72 bool event_vbus_on : 1;
73
76 };
77
78#pragma GCC diagnostic pop
79
80 // ====================================================================
81 // ----- USB Device Status -----
82
83#pragma GCC diagnostic push
84#if defined(__clang__)
85#pragma clang diagnostic ignored "-Wpadded"
86#elif defined(__GNUC__)
87#pragma GCC diagnostic ignored "-Wpadded"
88#endif
89
93 class Status
94 {
95 public:
96 bool
97 is_vbus_on (void) const noexcept;
98
100 get_speed (void) const noexcept;
101
102 bool
103 is_active (void) const noexcept;
104
105 // ------------------------------------------------------------------
106
107 public:
108 // For compatibility with ARM CMSIS, these bits should be
109 // exactly in this order.
110
112 bool vbus : 1;
113
116
118 bool active : 1;
119 };
120
121#pragma GCC diagnostic pop
122
123 // ====================================================================
124 // ----- USB Device Events -----
125
132 {
133 //
134
136 vbus_on = (1UL << 0),
137
139 vbus_off = (1UL << 1),
140
142 reset = (1UL << 2),
143
145 high_speed = (1UL << 3),
146
148 suspend = (1UL << 4),
149
151 resume = (1UL << 5)
152 };
153
154 // ====================================================================
155 // ----- USB Device Endpoint Events -----
156
161 {
163 setup = (1UL << 0),
164
166 out = (1UL << 1),
167
169 in = (1UL << 2),
170 };
171
172 // --------------------------------------------------------------------
173
174 typedef void (*signal_device_event_t) (const void* object,
175 event_t event);
176
177 typedef void (*signal_endpoint_event_t) (const void* object,
178 endpoint_t ep_addr,
179 event_t event);
180
181 } /* namespace device */
182
183 // ======================================================================
184
185#pragma GCC diagnostic push
186#if defined(__clang__)
187#pragma clang diagnostic ignored "-Wpadded"
188#elif defined(__GNUC__)
189#pragma GCC diagnostic ignored "-Wpadded"
190#endif
191
192 class Device : public Base
193 {
194
195 public:
196 // --------------------------------------------------------------------
197
198 Device () noexcept;
199
200 Device (const Device&) = delete;
201
202 Device (Device&&) = delete;
203
204 Device&
206 = delete;
207
208 Device&
210 = delete;
211
212 virtual ~Device () noexcept override;
213
214 // --------------------------------------------------------------------
215
223 void
224 register_device_callback (device::signal_device_event_t cb_func,
225 const void* cb_object = nullptr) noexcept;
226
227 void
228 register_endpoint_callback (device::signal_endpoint_event_t cb_func,
229 const void* cb_object = nullptr) noexcept;
230
231 // --------------------------------------------------------------------
232
237 const device::Capabilities&
238 get_capabilities (void) noexcept;
239
245 connect (void) noexcept;
246
252 disconnect (void) noexcept;
253
258 device::Status&
259 get_status (void) noexcept;
260
266 wakeup_remote (void) noexcept;
267
274 configure_address (device_address_t dev_addr) noexcept;
275
282 read_setup_packet (uint8_t* buf) noexcept;
283
289 get_frame_number (void) noexcept;
290
302 packet_size_t ep_max_packet_size) noexcept;
303
312 unconfigure_endpoint (endpoint_t ep_addr) noexcept;
313
325 stall_endpoint (endpoint_t ep_addr, bool stall) noexcept;
326
338 transfer (endpoint_t ep_addr, uint8_t* data, std::size_t num) noexcept;
339
347 std::size_t
348 get_transfer_count (endpoint_t ep_addr) noexcept;
349
358 abort_transfer (endpoint_t ep_addr) noexcept;
359
366 void
367 signal_device_event (event_t event) noexcept;
368
378 void
379 signal_endpoint_event (endpoint_t ep_addr, event_t event) noexcept;
380
381 protected:
382 virtual const device::Capabilities&
383 do_get_capabilities (void) noexcept
384 = 0;
385
386 virtual return_t
387 do_connect (void) noexcept
388 = 0;
389
390 virtual return_t
391 do_disconnect (void) noexcept
392 = 0;
393
394 virtual device::Status&
395 do_get_status (void) noexcept
396 = 0;
397
398 virtual return_t
399 do_wakeup_remote (void) noexcept
400 = 0;
401
402 virtual return_t
404 = 0;
405
406 virtual return_t
407 do_read_setup_packet (uint8_t* buf) noexcept
408 = 0;
409
410 virtual frame_number_t
411 do_get_frame_number (void) noexcept
412 = 0;
413
414 virtual return_t
416 packet_size_t ep_max_packet_size) noexcept
417 = 0;
418
419 virtual return_t
421 = 0;
422
423 virtual return_t
424 do_stall_endpoint (endpoint_t ep_addr, bool stall) noexcept
425 = 0;
426
427 virtual return_t
428 do_transfer (endpoint_t ep_addr, uint8_t* data,
429 std::size_t num) noexcept
430 = 0;
431
432 virtual std::size_t
434 = 0;
435
436 virtual return_t
437 do_abort_transfer (endpoint_t ep_addr) noexcept
438 = 0;
439
440 private:
442 device::signal_device_event_t cb_device_func_;
443
445 const void* cb_device_object_;
446
448 device::signal_endpoint_event_t cb_endpoint_func_;
449
452
453 protected:
454 device::Status status_;
455 };
456
457#pragma GCC diagnostic pop
458
459 // ----------------------------------------------------------------------
460 // ----- Definitions -----
461
462 namespace device
463 {
464 // --------------------------------------------------------------------
465
466 inline bool
467 Status::is_vbus_on (void) const noexcept
468 {
469 return vbus;
470 }
471
472 inline speed_t
473 Status::get_speed (void) const noexcept
474 {
475 return speed;
476 }
477
478 inline bool
479 Status::is_active (void) const noexcept
480 {
481 return active;
482 }
483
484 } /* namespace device */
485
486 // ----------------------------------------------------------------------
487
488 inline const device::Capabilities&
490 {
491 return do_get_capabilities ();
492 }
493
494 inline device::Status&
495 Device::get_status (void) noexcept
496 {
497 return do_get_status ();
498 }
499
500 inline return_t
501 Device::connect (void) noexcept
502 {
503 return do_connect ();
504 }
505
506 inline return_t
507 Device::disconnect (void) noexcept
508 {
509 return do_disconnect ();
510 }
511
512 inline return_t
513 Device::wakeup_remote (void) noexcept
514 {
515 return do_wakeup_remote ();
516 }
517
518 inline return_t
519 Device::configure_address (uint8_t dev_addr) noexcept
520 {
521 return do_configure_address (dev_addr);
522 }
523
524 inline frame_number_t
526 {
527 return do_get_frame_number ();
528 }
529
530 inline return_t
532 packet_size_t ep_max_packet_size) noexcept
533 {
534 return do_configure_endpoint (ep_addr, ep_type, ep_max_packet_size);
535 }
536
537 inline return_t
539 {
540 return do_unconfigure_endpoint (ep_addr);
541 }
542
543 inline return_t
544 Device::stall_endpoint (endpoint_t ep_addr, bool stall) noexcept
545 {
546 return do_stall_endpoint (ep_addr, stall);
547 }
548
549 inline std::size_t
551 {
552 return do_get_transfer_count (ep_addr);
553 }
554
555 inline return_t
557 {
558 return do_abort_transfer (ep_addr);
559 }
560
561 } /* namespace usb */
562 } /* namespace driver */
563} /* namespace os */
564
565#pragma GCC diagnostic pop
566
567// ----------------------------------------------------------------------------
568
569#endif /* __cplusplus */
570
571// ----------------------------------------------------------------------------
572
573#endif /* CMSIS_PLUS_DRIVER_USB_DEVICE_H_ */
virtual return_t do_stall_endpoint(endpoint_t ep_addr, bool stall) noexcept=0
virtual return_t do_configure_address(device_address_t dev_addr) noexcept=0
virtual std::size_t do_get_transfer_count(endpoint_t ep_addr) noexcept=0
void signal_endpoint_event(endpoint_t ep_addr, event_t event) noexcept
Signal USB Endpoint Event.
return_t connect(void) noexcept
Connect USB Device.
Definition usb-device.h:501
virtual const device::Capabilities & do_get_capabilities(void) noexcept=0
device::Status status_
Definition usb-device.h:454
return_t unconfigure_endpoint(endpoint_t ep_addr) noexcept
Unconfigure USB Endpoint.
Definition usb-device.h:538
virtual return_t do_wakeup_remote(void) noexcept=0
virtual return_t do_unconfigure_endpoint(endpoint_t ep_addr) noexcept=0
std::size_t get_transfer_count(endpoint_t ep_addr) noexcept
Get result of USB Endpoint transfer.
Definition usb-device.h:550
virtual ~Device() noexcept override
void signal_device_event(event_t event) noexcept
Signal device events.
virtual frame_number_t do_get_frame_number(void) noexcept=0
device::signal_device_event_t cb_device_func_
Pointer to static function that implements the device callback.
Definition usb-device.h:442
return_t wakeup_remote(void) noexcept
Trigger USB Remote Wakeup.
Definition usb-device.h:513
return_t read_setup_packet(uint8_t *buf) noexcept
Read setup packet received over Control Endpoint.
const device::Capabilities & get_capabilities(void) noexcept
Get driver capabilities.
Definition usb-device.h:489
virtual return_t do_transfer(endpoint_t ep_addr, uint8_t *data, std::size_t num) noexcept=0
virtual return_t do_connect(void) noexcept=0
Device & operator=(const Device &)=delete
virtual return_t do_abort_transfer(endpoint_t ep_addr) noexcept=0
return_t transfer(endpoint_t ep_addr, uint8_t *data, std::size_t num) noexcept
Read data from or Write data to USB Endpoint.
void register_device_callback(device::signal_device_event_t cb_func, const void *cb_object=nullptr) noexcept
Register device event callback.
Device(Device &&)=delete
device::signal_endpoint_event_t cb_endpoint_func_
Pointer to static function that implements the endpoint callback.
Definition usb-device.h:448
const void * cb_endpoint_object_
Pointer to object instance associated with the endpoint callback.
Definition usb-device.h:451
virtual return_t do_configure_endpoint(endpoint_t ep_addr, Endpoint_type ep_type, packet_size_t ep_max_packet_size) noexcept=0
virtual return_t do_read_setup_packet(uint8_t *buf) noexcept=0
virtual return_t do_disconnect(void) noexcept=0
return_t configure_address(device_address_t dev_addr) noexcept
Set USB Device Address.
Definition usb-device.h:519
return_t disconnect(void) noexcept
Disconnect USB Device.
Definition usb-device.h:507
frame_number_t get_frame_number(void) noexcept
Get current USB Frame Number.
Definition usb-device.h:525
void register_endpoint_callback(device::signal_endpoint_event_t cb_func, const void *cb_object=nullptr) noexcept
return_t abort_transfer(endpoint_t ep_addr) noexcept
Abort current USB Endpoint transfer.
Definition usb-device.h:556
const void * cb_device_object_
Pointer to object instance associated with the device callback.
Definition usb-device.h:445
return_t configure_endpoint(endpoint_t ep_addr, Endpoint_type ep_type, packet_size_t ep_max_packet_size) noexcept
Configure USB Endpoint.
Definition usb-device.h:531
device::Status & get_status(void) noexcept
Get current USB Device Status.
Definition usb-device.h:495
virtual device::Status & do_get_status(void) noexcept=0
Device(const Device &)=delete
return_t stall_endpoint(endpoint_t ep_addr, bool stall) noexcept
Set/Clear Stall for USB Endpoint.
Definition usb-device.h:544
USB device driver capabilities.
Definition usb-device.h:63
bool event_vbus_off
Signal VBUS Off event.
Definition usb-device.h:75
bool event_vbus_on
Signal VBUS On event.
Definition usb-device.h:72
speed_t get_speed(void) const noexcept
Definition usb-device.h:473
speed_t speed
USB Device speed setting (ARM_USB_SPEED_xxx).
Definition usb-device.h:115
bool is_vbus_on(void) const noexcept
Definition usb-device.h:467
bool active
USB Device active flag.
Definition usb-device.h:118
bool is_active(void) const noexcept
Definition usb-device.h:479
bool vbus
USB Device VBUS flag.
Definition usb-device.h:112
void(* signal_endpoint_event_t)(const void *object, endpoint_t ep_addr, event_t event)
Definition usb-device.h:177
Endpoint_event
USB Device Endpoint Events.
Definition usb-device.h:161
@ setup
SETUP Packet.
Definition usb-device.h:163
@ out
OUT Packet(s).
Definition usb-device.h:166
Device_event
USB Device Events.
Definition usb-device.h:132
@ vbus_on
USB Device VBUS On.
Definition usb-device.h:136
@ high_speed
USB switch to High Speed occurred.
Definition usb-device.h:145
@ suspend
USB Suspend occurred.
Definition usb-device.h:148
@ reset
USB Reset occurred.
Definition usb-device.h:142
@ vbus_off
USB Device VBUS Off.
Definition usb-device.h:139
@ resume
USB Resume occurred.
Definition usb-device.h:151
void(* signal_device_event_t)(const void *object, event_t event)
Definition usb-device.h:174
uint8_t speed_t
Definition usb.h:45
uint8_t endpoint_t
Definition usb.h:64
uint8_t device_address_t
Definition usb.h:99
uint16_t packet_size_t
Definition usb.h:92
uint16_t frame_number_t
Definition usb.h:97
Endpoint_type
Endpoint type.
Definition usb.h:77
int32_t return_t
Definition common.h:46
uint32_t event_t
Definition common.h:45
System namespace.
Standard std namespace.