Mistake on this page?
Report an issue in GitHub or email us
USBCDC.h
1 /*
2  * Copyright (c) 2018-2019, 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_H
19 #define USBCDC_H
20 
21 /* These headers are included for child class. */
22 #include "USBDescriptor.h"
23 #include "USBDevice_Types.h"
24 
25 #include "USBDevice.h"
26 #include "OperationList.h"
27 
28 class AsyncOp;
29 
30 /**
31  * \defgroup drivers_USBCDC USBCDC class
32  * \ingroup drivers-public-api-usb
33  * @{
34  */
35 
36 class USBCDC: public USBDevice {
37 public:
38 
39  /**
40  * Basic constructor
41  *
42  * Construct this object optionally connecting and blocking until it is ready.
43  *
44  * @note Do not use this constructor in derived classes.
45  *
46  * @param connect_blocking true to perform a blocking connect, false to start in a disconnected state
47  * @param vendor_id Your vendor_id
48  * @param product_id Your product_id
49  * @param product_release Your product_release
50  */
51  USBCDC(bool connect_blocking = true, uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001);
52 
53  /**
54  * Fully featured constructor
55  *
56  * Construct this object with the supplied USBPhy and parameters. The user
57  * this object is responsible for calling connect() or init().
58  *
59  * @note Derived classes must use this constructor and call init() or
60  * connect() themselves. Derived classes should also call deinit() in
61  * their destructor. This ensures that no interrupts can occur when the
62  * object is partially constructed or destroyed.
63  *
64  * @param phy USB phy to use
65  * @param vendor_id Your vendor_id
66  * @param product_id Your product_id
67  * @param product_release Your product_release
68  */
69  USBCDC(USBPhy *phy, uint16_t vendor_id, uint16_t product_id, uint16_t product_release);
70 
71  /**
72  * Destroy this object
73  *
74  * Any classes which inherit from this class must call deinit
75  * before this destructor runs.
76  */
77  virtual ~USBCDC();
78 
79  /**
80  * Check if this class is ready
81  *
82  * @return true if a terminal is connected, false otherwise
83  */
84  bool ready();
85 
86  /**
87  * Block until the terminal is connected
88  */
89  void wait_ready();
90 
91  /*
92  * Send a buffer
93  *
94  * This function blocks until the full contents have been sent.
95  *
96  * @param buffer buffer to be sent
97  * @param size length of the buffer
98  * @returns true if successful false if interrupted due to a state change
99  */
100  bool send(uint8_t *buffer, uint32_t size);
101 
102  /**
103  * Send what there is room for
104  *
105  * @param buffer data to send
106  * @param size maximum number of bytes to send
107  * @param actual a pointer to where to store the number of bytes sent
108  * @param now true to start data transmission, false to wait
109  */
110  void send_nb(uint8_t *buffer, uint32_t size, uint32_t *actual, bool now = true);
111 
112  /*
113  * Read a buffer from a certain endpoint. Warning: blocking
114  *
115  * Blocks until at least one byte of data has been read (actual != NULL) or
116  * until the full size has been read (actual == NULL).
117  *
118  * @param buffer buffer where will be stored bytes
119  * @param size the maximum number of bytes to read
120  * @param actual A pointer to where to store the number of bytes actually read
121  * or NULL to read the full size
122  * @returns true if successful false if interrupted due to a state change
123  */
124  bool receive(uint8_t *buffer, uint32_t size, uint32_t *actual = NULL);
125 
126  /**
127  * Read from the receive buffer
128  *
129  * @param buffer buffer to fill with data
130  * @param size maximum number of bytes read
131  * @param actual a pointer to where to store the number of bytes actually received
132  */
133  void receive_nb(uint8_t *buffer, uint32_t size, uint32_t *actual);
134 
135 protected:
136  /*
137  * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
138  *
139  * @returns pointer to the device descriptor
140  */
141  virtual const uint8_t *device_desc();
142 
143  /*
144  * Get string product descriptor
145  *
146  * @returns pointer to the string product descriptor
147  */
148  virtual const uint8_t *string_iproduct_desc();
149 
150  /*
151  * Get string interface descriptor
152  *
153  * @returns pointer to the string interface descriptor
154  */
155  virtual const uint8_t *string_iinterface_desc();
156 
157  /*
158  * Get configuration descriptor
159  *
160  * @param index descriptor index
161  * @returns pointer to the configuration descriptor
162  */
163  virtual const uint8_t *configuration_desc(uint8_t index);
164 
165  /*
166  * Called by USBCallback_requestCompleted when CDC line coding is changed
167  * Warning: Called in ISR
168  *
169  * @param baud The baud rate
170  * @param bits The number of bits in a word (5-8)
171  * @param parity The parity
172  * @param stop The number of stop bits (1 or 2)
173  */
174  virtual void line_coding_changed(int baud, int bits, int parity, int stop) {};
175 
176  /*
177  * Called when there is data that can be read
178  */
179  virtual void data_rx() {}
180 
181  /*
182  * Called when there is space in the TX buffer
183  */
184  virtual void data_tx() {}
185 
186 protected:
187 
188  class AsyncWrite;
189  class AsyncRead;
190  class AsyncWait;
191 
192  virtual void callback_reset();
193  virtual void callback_state_change(DeviceState new_state);
194  virtual void callback_request(const setup_packet_t *setup);
195  virtual void callback_request_xfer_done(const setup_packet_t *setup, bool aborted);
196  virtual void callback_set_configuration(uint8_t configuration);
197  virtual void callback_set_interface(uint16_t interface, uint8_t alternate);
198 
199  void _init();
200 
201  void _change_terminal_connected(bool connected);
202 
203  void _send_isr_start();
204  void _send_isr();
205 
206  void _receive_isr_start();
207  void _receive_isr();
208 
209  usb_ep_t _bulk_in;
210  usb_ep_t _bulk_out;
211  usb_ep_t _int_in;
212 
213  uint8_t _cdc_line_coding[7];
214  uint8_t _cdc_new_line_coding[7];
215  uint8_t _config_descriptor[75];
216 
217  OperationList<AsyncWait> _connected_list;
218  bool _terminal_connected;
219 
220  OperationList<AsyncWrite> _tx_list;
221  bool _tx_in_progress;
222  uint8_t _tx_buffer[64];
223  uint8_t *_tx_buf;
224  uint32_t _tx_size;
225 
226  OperationList<AsyncRead> _rx_list;
227  bool _rx_in_progress;
228  uint8_t _rx_buffer[64];
229  uint8_t *_rx_buf;
230  uint32_t _rx_size;
231 };
232 
233 /** @}*/
234 
235 #endif
virtual ~USBCDC()
Destroy this object.
Definition: USBCDC.h:36
void receive_nb(uint8_t *buffer, uint32_t size, uint32_t *actual)
Read from the receive buffer.
Abstract interface to physical USB hardware.
Definition: USBPhy.h:82
virtual void callback_request(const setup_packet_t *setup)
Called by USBDevice on Endpoint0 request.
bool ready()
Check if this class is ready.
virtual void callback_state_change(DeviceState new_state)
Called when USB changes state.
virtual void callback_reset()
Called by USBDevice layer on bus reset.
void send_nb(uint8_t *buffer, uint32_t size, uint32_t *actual, bool now=true)
Send what there is room for.
Core USB Device driver.
Definition: USBDevice.h:38
USBCDC(bool connect_blocking=true, uint16_t vendor_id=0x1f00, uint16_t product_id=0x2012, 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.
void wait_ready()
Block until the terminal is connected.
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.