µ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++ 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
21#include <cassert>
22
23// ----------------------------------------------------------------------------
24
25#if defined(__clang__)
26#pragma clang diagnostic ignored "-Wc++98-compat"
27#endif
28
29// ----------------------------------------------------------------------------
30
31namespace os
32{
33 namespace driver
34 {
35 namespace usb
36 {
37 // ----------------------------------------------------------------------
38
39 Device::Device () noexcept
40 {
41 trace::printf ("%s() %p\n", __func__, this);
42
43 cb_device_func_ = nullptr;
44 cb_device_object_ = nullptr;
45
46 cb_endpoint_func_ = nullptr;
47 cb_endpoint_object_ = nullptr;
48 }
49
50 Device::~Device () noexcept
51 {
52 trace::printf ("%s() %p\n", __func__, this);
53 }
54
55 void
57 const void* cb_object) noexcept
58 {
59 cb_device_func_ = cb_func;
60 cb_device_object_ = cb_object;
61 }
62
63 void
65 device::signal_endpoint_event_t cb_func, 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, std::size_t num) noexcept
82 {
83 assert (data != nullptr);
84 if (num == 0)
85 {
86 return RETURN_OK;
87 }
88 return do_transfer (ep_addr, data, num);
89 }
90
91 // ----------------------------------------------------------------------
92
93 void
95 {
96 if (cb_device_func_ != nullptr)
97 {
98 // Forward event to registered callback.
99 cb_device_func_ (cb_device_object_, event);
100 }
101 }
102
103 void
105 {
106 if (cb_endpoint_func_ != nullptr)
107 {
108 // Forward event to registered callback.
109 cb_endpoint_func_ (cb_endpoint_object_, ep_addr, event);
110 }
111 }
112
113 } /* namespace usb */
114 } /* namespace driver */
115} /* namespace os */
116
117// ----------------------------------------------------------------------------
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:438
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:444
const void * cb_endpoint_object_
Pointer to object instance associated with the endpoint callback.
Definition usb-device.h:447
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:441
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:60
void(* signal_endpoint_event_t)(const void *object, endpoint_t ep_addr, event_t event)
Definition usb-device.h:187
void(* signal_device_event_t)(const void *object, event_t event)
Definition usb-device.h:184
uint8_t endpoint_t
Definition usb.h:68
constexpr return_t RETURN_OK
< Operation succeeded
Definition common.h:53
int32_t return_t
Definition common.h:47
uint32_t event_t
Definition common.h:46
System namespace.