Mistake on this page?
Report an issue in GitHub or email us
USBCDC_ECM.h
1 /*
2  * Copyright (c) 2018, Arm Limited and affiliates.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef USBCDC_ECM_H
19 #define USBCDC_ECM_H
20 
21 #include "USBDescriptor.h"
22 #include "USBDevice.h"
23 #include "ByteBuffer.h"
24 #include "Mutex.h"
25 #include "EventFlags.h"
26 #include "EventQueue.h"
27 #include "Thread.h"
28 #include "Callback.h"
29 
30 #define MAX_PACKET_SIZE_INT (64)
31 #define MAX_PACKET_SIZE_BULK (64)
32 #define MAX_PACKET_SIZE_EP0 (64)
33 #define DEFAULT_CONFIGURATION (1)
34 
35 #define PACKET_TYPE_PROMISCUOUS (1<<0)
36 #define PACKET_TYPE_ALL_MULTICAST (1<<1)
37 #define PACKET_TYPE_DIRECTED (1<<2)
38 #define PACKET_TYPE_BROADCAST (1<<3)
39 #define PACKET_TYPE_MULTICAST (1<<4)
40 
41 class USBCDC_ECM: public USBDevice {
42 public:
43 
44  /**
45  * Basic constructor
46  *
47  * Construct this object optionally connecting and blocking until it is ready.
48  *
49  * @note Do not use this constructor in derived classes.
50  *
51  * @param connect_blocking true to perform a blocking connect, false to start in a disconnected state
52  * @param vendor_id Your vendor_id
53  * @param product_id Your product_id
54  * @param product_release Your product_release
55  */
56 
57  USBCDC_ECM(bool connect_blocking = true, uint16_t vendor_id = 0x0700, uint16_t product_id = 0x0101, uint16_t product_release = 0x0001);
58 
59  /**
60  * Fully featured constructor
61  *
62  * Construct this object with the supplied USBPhy and parameters. The user
63  * this object is responsible for calling connect() or init().
64  *
65  * @note Derived classes must use this constructor and call init() or
66  * connect() themselves. Derived classes should also call deinit() in
67  * their destructor. This ensures that no interrupts can occur when the
68  * object is partially constructed or destroyed.
69  *
70  * @param phy USB phy to use
71  * @param vendor_id Your vendor_id
72  * @param product_id Your product_id
73  * @param product_release Your product_release
74  */
75  USBCDC_ECM(USBPhy *phy, uint16_t vendor_id, uint16_t product_id, uint16_t product_release);
76 
77  /**
78  * Destroy this object
79  *
80  * Any classes which inherit from this class must call deinit
81  * before this destructor runs.
82  */
83  virtual ~USBCDC_ECM();
84 
85  /**
86  * Check if this class is ready
87  *
88  * @return true if configured, false otherwise
89  */
90  bool ready();
91 
92  /**
93  * Block until this device is configured
94  */
95  void wait_ready();
96 
97  /**
98  * Send a buffer
99  *
100  * This function blocks until the full contents have been sent.
101  *
102  * @param buffer buffer to be sent
103  * @param size length of the buffer
104  * @returns true if successful false if interrupted due to a state change
105  */
106  bool send(uint8_t *buffer, uint32_t size);
107 
108  /**
109  * Read from the receive buffer
110  *
111  * @param buffer buffer to fill with data
112  * @param size maximum number of bytes read
113  * @param actual a pointer to where to store the number of bytes actually received
114  */
115  void receive_nb(uint8_t *buffer, uint32_t size, uint32_t *actual);
116 
117  /**
118  * Return ethernet packet filter bitmap
119  *
120  * The Packet Filter is the inclusive OR of the bitmap
121  * D0: PACKET_TYPE_PROMISCUOUS
122  * D1: PACKET_TYPE_ALL_MULTICAST
123  * D2: PACKET_TYPE_DIRECTED
124  * D3: PACKET_TYPE_BROADCAST
125  * D4: PACKET_TYPE_MULTICAST
126  * D5-D15: Reserved (zero)
127  *
128  * @return ethernet packet filter bitmap
129  */
130  uint16_t read_packet_filter();
131 
132  /**
133  * Attach a callback for when an ethernet packet is received
134  *
135  * @param cb code to call when a packet is received
136  */
137  void attach_rx(mbed::Callback<void()> cb);
138 
139  /**
140  * Attach a callback for when a request to configure device ethernet
141  * packet filter is received
142  *
143  * @param cb code to call when a packet filter request is received
144  */
145  void attach_filter(mbed::Callback<void()> cb);
146 
147 protected:
148 
149  /*
150  * Called when USB changes state
151  *
152  * @param new_state The new state of the USBDevice
153  *
154  * Warning: Called in ISR context
155  */
156  virtual void callback_state_change(DeviceState new_state);
157 
158  /*
159  * This is used to handle extensions to standard requests
160  * and class specific requests with a data phase
161  */
162  virtual void callback_request_xfer_done(const setup_packet_t *setup, bool aborted);
163 
164  /*
165  * Called by USBDevice layer. Set configuration of the device.
166  * For instance, you can add all endpoints that you need on this function.
167  *
168  * @param configuration Number of the configuration
169  * @returns true if class handles this request
170  */
171  virtual void callback_set_configuration(uint8_t configuration);
172 
173  /*
174  * Called by USBDevice layer in response to set_interface.
175  *
176  * Upon reception of this command endpoints of any previous interface
177  * if any must be removed with endpoint_remove and new endpoint added with
178  * endpoint_add.
179  *
180  * @param configuration Number of the configuration
181  *
182  * Warning: Called in ISR context
183  */
184  virtual void callback_set_interface(uint16_t interface, uint8_t alternate);
185 
186  /*
187  * Get device descriptor.
188  *
189  * @returns pointer to the device descriptor
190  */
191  virtual const uint8_t *device_desc();
192 
193  /*
194  * Get string product descriptor
195  *
196  * @returns pointer to the string product descriptor
197  */
198  virtual const uint8_t *string_iproduct_desc();
199 
200  /*
201  * Get string configuration descriptor
202  *
203  * @returns pointer to the string configuration descriptor
204  */
205  virtual const uint8_t *string_iconfiguration_desc();
206 
207  /*
208  * Get string serial descriptor
209  *
210  * @returns pointer to the string serial descriptor
211  */
212  virtual const uint8_t *string_iserial_desc();
213 
214  /*
215  * Get configuration descriptor
216  *
217  * @returns pointer to the configuration descriptor
218  */
219  virtual const uint8_t *configuration_desc(uint8_t index);
220 
221  /*
222  * This is used to handle extensions to standard requests
223  * and class specific requests
224  */
225  virtual void callback_request(const setup_packet_t *setup);
226 
227  /*
228  * Called by USBDevice layer on bus reset.
229  *
230  * complete_reset must be called after
231  * the device is fully reset.
232  *
233  * Warning: Called in ISR context
234  */
235  virtual void callback_reset();
236 
237  uint8_t device_descriptor[18];
238 
239 private:
240 
241  usb_ep_t _int_in;
242  usb_ep_t _bulk_in;
243  usb_ep_t _bulk_out;
244 
245  uint8_t _config_descriptor[80];
246  uint8_t _string_imac_addr[26];
247 
248  uint8_t _bulk_buf[MAX_PACKET_SIZE_BULK];
249  uint16_t _packet_filter;
250  ByteBuffer _rx_queue;
251 
252  rtos::EventFlags _flags;
253  rtos::Mutex _write_mutex;
254 
255  events::EventQueue _queue;
256  rtos::Thread _thread;
257  mbed::Callback<void()> _callback_rx;
258  mbed::Callback<void()> _callback_filter;
259 
260  void _init();
261  void _int_callback();
262  void _bulk_in_callback();
263  void _bulk_out_callback();
264  bool _notify_network_connection(uint8_t value);
265  bool _notify_connection_speed_change(uint32_t up, uint32_t down);
266  bool _write_bulk(uint8_t *buffer, uint32_t size);
267  void _notify_connect();
268 };
269 
270 #endif
void wait_ready()
Block until this device is configured.
The Thread class allow defining, creating, and controlling thread functions in the system...
Definition: Thread.h:85
virtual ~USBCDC_ECM()
Destroy this object.
The EventFlags class is used to control event flags or wait for event flags other threads control...
Definition: EventFlags.h:49
EventQueue.
Definition: EventQueue.h:52
uint16_t read_packet_filter()
Return ethernet packet filter bitmap.
void attach_rx(mbed::Callback< void()> cb)
Attach a callback for when an ethernet packet is received.
void receive_nb(uint8_t *buffer, uint32_t size, uint32_t *actual)
Read from the receive buffer.
virtual void callback_state_change(DeviceState new_state)
Called when USB changes state.
bool ready()
Check if this class is ready.
void attach_filter(mbed::Callback< void()> cb)
Attach a callback for when a request to configure device ethernet packet filter is received...
Abstract interface to physical USB hardware.
Definition: USBPhy.h:82
virtual void callback_reset()
Called by USBDevice layer on bus reset.
The Mutex class is used to synchronize the execution of threads.
Definition: Mutex.h:66
Core USB Device driver.
Definition: USBDevice.h:44
USBCDC_ECM(bool connect_blocking=true, uint16_t vendor_id=0x0700, uint16_t product_id=0x0101, uint16_t product_release=0x0001)
Basic constructor.
virtual void callback_request_xfer_done(const setup_packet_t *setup, bool aborted)
Called by USBDevice on data stage completion.
Callback class based on template specialization.
Definition: Callback.h:39
bool send(uint8_t *buffer, uint32_t size)
Send a buffer.
virtual void callback_request(const setup_packet_t *setup)
Called by USBDevice on Endpoint0 request.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.