µ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.cpp
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
20#include <cassert>
21
22// ----------------------------------------------------------------------------
23
24#if defined(__clang__)
25#pragma clang diagnostic ignored "-Wc++98-compat"
26#endif
27
28// ----------------------------------------------------------------------------
29
30namespace os
31{
32 namespace driver
33 {
34 namespace usb
35 {
36 // ----------------------------------------------------------------------
37
38 Device::Device () noexcept
39 {
40 trace::printf ("%s() %p\n", __func__, this);
41
42 cb_device_func_ = nullptr;
43 cb_device_object_ = nullptr;
44
45 cb_endpoint_func_ = nullptr;
46 cb_endpoint_object_ = nullptr;
47 }
48
49 Device::~Device () noexcept
50 {
51 trace::printf ("%s() %p\n", __func__, this);
52 }
53
54 void
56 const void* cb_object) noexcept
57 {
58 cb_device_func_ = cb_func;
59 cb_device_object_ = cb_object;
60 }
61
62 void
65 const void* cb_object) noexcept
66 {
67 cb_endpoint_func_ = cb_func;
68 cb_endpoint_object_ = cb_object;
69 }
70
71 // ----------------------------------------------------------------------
72
74 Device::read_setup_packet (uint8_t* buf) noexcept
75 {
76 assert (buf != nullptr);
77 return do_read_setup_packet (buf);
78 }
79
81 Device::transfer (endpoint_t ep_addr, uint8_t* data,
82 std::size_t num) noexcept
83 {
84 assert (data != nullptr);
85 if (num == 0)
86 {
87 return RETURN_OK;
88 }
89 return do_transfer (ep_addr, data, num);
90 }
91
92 // ----------------------------------------------------------------------
93
94 void
96 {
97 if (cb_device_func_ != nullptr)
98 {
99 // Forward event to registered callback.
100 cb_device_func_ (cb_device_object_, event);
101 }
102 }
103
104 void
106 event_t event) noexcept
107 {
108 if (cb_endpoint_func_ != nullptr)
109 {
110 // Forward event to registered callback.
111 cb_endpoint_func_ (cb_endpoint_object_, ep_addr, event);
112 }
113 }
114
115 } /* namespace usb */
116 } /* namespace driver */
117} /* namespace os */
118
119// ----------------------------------------------------------------------------
void signal_endpoint_event(endpoint_t ep_addr, event_t event) noexcept
Signal USB Endpoint Event.
virtual ~Device() noexcept override
void signal_device_event(event_t event) noexcept
Signal device events.
device::signal_device_event_t cb_device_func_
Pointer to static function that implements the device callback.
Definition usb-device.h:442
return_t read_setup_packet(uint8_t *buf) noexcept
Read setup packet received over Control Endpoint.
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::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
void register_endpoint_callback(device::signal_endpoint_event_t cb_func, const void *cb_object=nullptr) noexcept
const void * cb_device_object_
Pointer to object instance associated with the device callback.
Definition usb-device.h:445
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:59
void(* signal_endpoint_event_t)(const void *object, endpoint_t ep_addr, event_t event)
Definition usb-device.h:177
void(* signal_device_event_t)(const void *object, event_t event)
Definition usb-device.h:174
uint8_t endpoint_t
Definition usb.h:64
constexpr return_t RETURN_OK
Definition common.h:52
int32_t return_t
Definition common.h:46
uint32_t event_t
Definition common.h:45
System namespace.