µ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++ distribution.
3 * (https://github.com/micro-os-plus)
4 * Copyright (c) 2016-2023 Liviu Ionescu. All rights reserved.
5 * Copyright (c) 2013-2014 ARM Ltd.
6 *
7 * Permission to use, copy, modify, and/or distribute this software
8 * for any purpose is hereby granted, under the terms of the MIT license.
9 *
10 * If a copy of the license was not distributed with this file, it can
11 * be obtained from https://opensource.org/licenses/mit/.
12 */
13
14/*
15 * The code is inspired by ARM CMSIS Driver_USBD.h file, v2.01,
16 * and tries to remain functionally close to the CMSIS specifications.
17 */
18
19#ifndef CMSIS_PLUS_DRIVER_USB_DEVICE_H_
20#define CMSIS_PLUS_DRIVER_USB_DEVICE_H_
21
22// ----------------------------------------------------------------------------
23
24#ifdef __cplusplus
25
26// ----------------------------------------------------------------------------
27
30
31#include <cstdint>
32#include <cstddef>
33
34// ----------------------------------------------------------------------------
35
36#pragma GCC diagnostic push
37#if defined(__clang__)
38#pragma clang diagnostic ignored "-Wc++98-compat"
39#endif
40
41// ----------------------------------------------------------------------------
42
43namespace os
44{
45 namespace driver
46 {
47 namespace usb
48 {
49 namespace device
50 {
51 // ==================================================================
52
53#pragma GCC diagnostic push
54#if defined(__clang__)
55#pragma clang diagnostic ignored "-Wpadded"
56#elif defined(__GNUC__)
57#pragma GCC diagnostic ignored "-Wpadded"
58#endif
59
64 {
65 public:
66
67 // For compatibility with ARM CMSIS, these bits should be
68 // exactly in this order.
69
72
75
78
79 };
80
81#pragma GCC diagnostic pop
82
83 // ==================================================================
84 // ----- USB Device Status -----
85
86#pragma GCC diagnostic push
87#if defined(__clang__)
88#pragma clang diagnostic ignored "-Wpadded"
89#elif defined(__GNUC__)
90#pragma GCC diagnostic ignored "-Wpadded"
91#endif
92
96 class Status
97 {
98 public:
99
100 bool
101 is_vbus_on (void) const noexcept;
102
103 speed_t
104 get_speed (void) const noexcept;
105
106 bool
107 is_active (void) const noexcept;
108
109 // ----------------------------------------------------------------
110
111 public:
112
113 // For compatibility with ARM CMSIS, these bits should be
114 // exactly in this order.
115
117 bool vbus :1;
118
121
123 bool active :1;
124 };
125
126#pragma GCC diagnostic pop
127
128 // ==================================================================
129 // ----- USB Device Events -----
130
137 : event_t
138 {
139 //
140
142 vbus_on = (1UL << 0),
143
145 vbus_off = (1UL << 1),
146
148 reset = (1UL << 2),
149
151 high_speed = (1UL << 3),
152
154 suspend = (1UL << 4),
155
157 resume = (1UL << 5)
158 };
159
160 // ==================================================================
161 // ----- USB Device Endpoint Events -----
162
167 : event_t
168 {
169 //
170
172 setup = (1UL << 0),
173
175 out = (1UL << 1),
176
178 in = (1UL << 2),
179 };
180
181 // ------------------------------------------------------------------
182
183 typedef void
184 (*signal_device_event_t) (const void* object, event_t event);
185
186 typedef void
187 (*signal_endpoint_event_t) (const void* object, endpoint_t ep_addr,
188 event_t event);
189
190 } /* namespace device */
191
192 // ====================================================================
193
194#pragma GCC diagnostic push
195#if defined(__clang__)
196#pragma clang diagnostic ignored "-Wpadded"
197#elif defined(__GNUC__)
198#pragma GCC diagnostic ignored "-Wpadded"
199#endif
200
201 class Device : public Base
202 {
203
204 public:
205
206 // ------------------------------------------------------------------
207
208 Device () noexcept;
209
210 Device (const Device&) = delete;
211
212 Device (Device&&) = delete;
213
214 Device&
215 operator= (const Device&) = delete;
216
217 Device&
218 operator= (Device&&) = delete;
219
220 virtual
221 ~Device () noexcept override;
222
223 // ------------------------------------------------------------------
224
232 void
233 register_device_callback (device::signal_device_event_t cb_func,
234 const void* cb_object = nullptr) noexcept;
235
236 void
237 register_endpoint_callback (device::signal_endpoint_event_t cb_func,
238 const void* cb_object = nullptr) noexcept;
239
240 // ------------------------------------------------------------------
241
246 const device::Capabilities&
247 get_capabilities (void) noexcept;
248
254 connect (void) noexcept;
255
261 disconnect (void) noexcept;
262
267 device::Status&
268 get_status (void) noexcept;
269
275 wakeup_remote (void) noexcept;
276
283 configure_address (device_address_t dev_addr) noexcept;
284
291 read_setup_packet (uint8_t* buf) noexcept;
292
298 get_frame_number (void) noexcept;
299
311 packet_size_t ep_max_packet_size) noexcept;
312
321 unconfigure_endpoint (endpoint_t ep_addr) noexcept;
322
334 stall_endpoint (endpoint_t ep_addr, bool stall) noexcept;
335
346 transfer (endpoint_t ep_addr, uint8_t* data, std::size_t num) noexcept;
347
355 std::size_t
356 get_transfer_count (endpoint_t ep_addr) noexcept;
357
366 abort_transfer (endpoint_t ep_addr) noexcept;
367
374 void
375 signal_device_event (event_t event) noexcept;
376
386 void
387 signal_endpoint_event (endpoint_t ep_addr, event_t event) noexcept;
388
389 protected:
390
391 virtual const device::Capabilities&
392 do_get_capabilities (void) noexcept = 0;
393
394 virtual return_t
395 do_connect (void) noexcept = 0;
396
397 virtual return_t
398 do_disconnect (void) noexcept = 0;
399
400 virtual device::Status&
401 do_get_status (void) noexcept = 0;
402
403 virtual return_t
404 do_wakeup_remote (void) noexcept = 0;
405
406 virtual return_t
407 do_configure_address (device_address_t dev_addr) noexcept = 0;
408
409 virtual return_t
410 do_read_setup_packet (uint8_t* buf) noexcept = 0;
411
412 virtual frame_number_t
413 do_get_frame_number (void) noexcept = 0;
414
415 virtual return_t
417 packet_size_t ep_max_packet_size) noexcept = 0;
418
419 virtual return_t
420 do_unconfigure_endpoint (endpoint_t ep_addr) noexcept = 0;
421
422 virtual return_t
423 do_stall_endpoint (endpoint_t ep_addr, bool stall) noexcept = 0;
424
425 virtual return_t
426 do_transfer (endpoint_t ep_addr, uint8_t* data, std::size_t num)
427 noexcept = 0;
428
429 virtual std::size_t
430 do_get_transfer_count (endpoint_t ep_addr) noexcept = 0;
431
432 virtual return_t
433 do_abort_transfer (endpoint_t ep_addr) noexcept = 0;
434
435 private:
436
438 device::signal_device_event_t cb_device_func_;
439
441 const void* cb_device_object_;
442
444 device::signal_endpoint_event_t cb_endpoint_func_;
445
448
449 protected:
450
451 device::Status status_;
452 };
453
454#pragma GCC diagnostic pop
455
456 // ----------------------------------------------------------------------
457 // ----- Definitions -----
458
459 namespace device
460 {
461 // --------------------------------------------------------------------
462
463 inline bool
464 Status::is_vbus_on (void) const noexcept
465 {
466 return vbus;
467 }
468
469 inline speed_t
470 Status::get_speed (void) const noexcept
471 {
472 return speed;
473 }
474
475 inline bool
476 Status::is_active (void) const noexcept
477 {
478 return active;
479 }
480
481 } /* namespace device */
482
483 // --------------------------------------------------------------------
484
485 inline const device::Capabilities&
487 {
488 return do_get_capabilities ();
489 }
490
491 inline device::Status&
492 Device::get_status (void) noexcept
493 {
494 return do_get_status ();
495 }
496
497 inline return_t
498 Device::connect (void) noexcept
499 {
500 return do_connect ();
501 }
502
503 inline return_t
504 Device::disconnect (void) noexcept
505 {
506 return do_disconnect ();
507 }
508
509 inline return_t
510 Device::wakeup_remote (void) noexcept
511 {
512 return do_wakeup_remote ();
513 }
514
515 inline return_t
516 Device::configure_address (uint8_t dev_addr) noexcept
517 {
518 return do_configure_address (dev_addr);
519 }
520
521 inline frame_number_t
523 {
524 return do_get_frame_number ();
525 }
526
527 inline return_t
529 packet_size_t ep_max_packet_size) noexcept
530 {
531 return do_configure_endpoint (ep_addr, ep_type, ep_max_packet_size);
532 }
533
534 inline return_t
536 {
537 return do_unconfigure_endpoint (ep_addr);
538 }
539
540 inline return_t
541 Device::stall_endpoint (endpoint_t ep_addr, bool stall) noexcept
542 {
543 return do_stall_endpoint (ep_addr, stall);
544 }
545
546 inline std::size_t
548 {
549 return do_get_transfer_count (ep_addr);
550 }
551
552 inline return_t
554 {
555 return do_abort_transfer (ep_addr);
556 }
557
558 } /* namespace usb */
559 } /* namespace driver */
560} /* namespace os */
561
562#pragma GCC diagnostic pop
563
564// ----------------------------------------------------------------------------
565
566#endif /* __cplusplus */
567
568// ----------------------------------------------------------------------------
569
570#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:498
virtual const device::Capabilities & do_get_capabilities(void) noexcept=0
device::Status status_
Definition usb-device.h:451
return_t unconfigure_endpoint(endpoint_t ep_addr) noexcept
Unconfigure USB Endpoint.
Definition usb-device.h:535
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:547
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:438
return_t wakeup_remote(void) noexcept
Trigger USB Remote Wakeup.
Definition usb-device.h:510
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:486
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:444
const void * cb_endpoint_object_
Pointer to object instance associated with the endpoint callback.
Definition usb-device.h:447
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:516
return_t disconnect(void) noexcept
Disconnect USB Device.
Definition usb-device.h:504
frame_number_t get_frame_number(void) noexcept
Get current USB Frame Number.
Definition usb-device.h:522
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:553
const void * cb_device_object_
Pointer to object instance associated with the device callback.
Definition usb-device.h:441
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:528
device::Status & get_status(void) noexcept
Get current USB Device Status.
Definition usb-device.h:492
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:541
USB device driver capabilities.
Definition usb-device.h:64
bool vbus_detection
< VBUS detection
Definition usb-device.h:71
bool event_vbus_on
Signal VBUS Off event.
Definition usb-device.h:74
speed_t get_speed(void) const noexcept
Definition usb-device.h:470
speed_t speed
USB Device active flag.
Definition usb-device.h:120
bool is_vbus_on(void) const noexcept
Definition usb-device.h:464
bool is_active(void) const noexcept
Definition usb-device.h:476
bool vbus
< USB Device VBUS flag
Definition usb-device.h:117
void(* signal_endpoint_event_t)(const void *object, endpoint_t ep_addr, event_t event)
Definition usb-device.h:187
Endpoint_event
USB Device Endpoint Events.
Definition usb-device.h:168
@ setup
SETUP Packet.
Definition usb-device.h:172
Device_event
USB Device Events.
Definition usb-device.h:138
@ vbus_on
USB Device VBUS On.
Definition usb-device.h:142
@ high_speed
USB Suspend occurred.
Definition usb-device.h:151
@ suspend
USB Resume occurred.
Definition usb-device.h:154
@ reset
USB switch to High Speed occurred.
Definition usb-device.h:148
@ vbus_off
USB Reset occurred.
Definition usb-device.h:145
void(* signal_device_event_t)(const void *object, event_t event)
Definition usb-device.h:184
uint8_t speed_t
Definition usb.h:46
uint8_t endpoint_t
Definition usb.h:68
uint8_t device_address_t
Definition usb.h:106
uint16_t packet_size_t
Definition usb.h:99
uint16_t frame_number_t
Definition usb.h:104
Endpoint_type
Endpoint type.
Definition usb.h:82
int32_t return_t
Definition common.h:47
uint32_t event_t
Definition common.h:46
System namespace.
Standard std namespace.