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