1use crate::bus::{InterfaceNumber, UsbBus, UsbBusAllocator};
29use crate::class::UsbClass;
30use crate::control::{ControlIn, ControlOut, Recipient, RequestType};
31use crate::descriptor::{DescriptorWriter, cdc_descriptor, cdc_protocol, cdc_subclass, class_code};
32use crate::endpoint::{EndpointIn, EndpointOut};
33use crate::error::{UsbError, UsbResult};
34
35mod req {
38 pub const SET_LINE_CODING: u8 = 0x20;
39 pub const GET_LINE_CODING: u8 = 0x21;
40 pub const SET_CONTROL_LINE_STATE: u8 = 0x22;
41 pub const SEND_BREAK: u8 = 0x23;
42}
43
44#[derive(Clone, Copy, Debug)]
48#[cfg_attr(feature = "defmt", derive(defmt::Format))]
49pub struct LineCoding {
50 pub baud_rate: u32,
52 pub stop_bits: u8,
54 pub parity: u8,
56 pub data_bits: u8,
58}
59
60impl Default for LineCoding {
61 fn default() -> Self {
62 Self {
63 baud_rate: 115_200,
64 stop_bits: 0,
65 parity: 0,
66 data_bits: 8,
67 }
68 }
69}
70
71impl LineCoding {
72 fn to_bytes(self) -> [u8; 7] {
73 let b = self.baud_rate.to_le_bytes();
74 [
75 b[0],
76 b[1],
77 b[2],
78 b[3],
79 self.stop_bits,
80 self.parity,
81 self.data_bits,
82 ]
83 }
84
85 fn from_bytes(b: &[u8]) -> Option<Self> {
86 if b.len() < 7 {
87 return None;
88 }
89 Some(Self {
90 baud_rate: u32::from_le_bytes([b[0], b[1], b[2], b[3]]),
91 stop_bits: b[4],
92 parity: b[5],
93 data_bits: b[6],
94 })
95 }
96}
97
98pub struct CdcAcm<'a, B: UsbBus> {
104 comm_if: InterfaceNumber,
105 data_if: InterfaceNumber,
106 int_in: EndpointIn<'a, B>,
107 bulk_in: EndpointIn<'a, B>,
108 bulk_out: EndpointOut<'a, B>,
109 line_coding: LineCoding,
110 dtr: bool,
111 rts: bool,
112 configured: bool,
113}
114
115impl<'a, B: UsbBus> CdcAcm<'a, B> {
116 pub fn new(alloc: &'a UsbBusAllocator<B>) -> Self {
121 let comm_if = alloc.interface();
122 let data_if = alloc.interface();
123 let int_in = alloc.interrupt(8, 16);
124 let bulk_size = B::MAX_PACKET_SIZE;
125 let bulk_in = alloc.bulk(bulk_size);
126 let bulk_out = alloc.bulk(bulk_size);
127 Self {
128 comm_if,
129 data_if,
130 int_in,
131 bulk_in,
132 bulk_out,
133 line_coding: LineCoding::default(),
134 dtr: false,
135 rts: false,
136 configured: false,
137 }
138 }
139
140 pub fn read(&self, buf: &mut [u8]) -> UsbResult<usize> {
147 if !self.configured {
148 return Err(UsbError::InvalidState);
149 }
150 self.bulk_out.read(buf)
151 }
152
153 pub fn write(&self, data: &[u8]) -> UsbResult<usize> {
158 if !self.configured {
159 return Err(UsbError::InvalidState);
160 }
161 self.bulk_in.write(data)
162 }
163
164 pub fn write_all(&self, data: &[u8], mut budget: u32) -> usize {
170 if !self.configured {
171 return 0;
172 }
173 let max_packet = self.bulk_in.max_packet_size() as usize;
174 let mut sent = 0;
175 while sent < data.len() {
176 let end = (sent + max_packet).min(data.len());
177 loop {
178 match self.bulk_in.write(&data[sent..end]) {
179 Ok(_) => break,
180 Err(UsbError::WouldBlock) if budget > 0 => {
181 budget -= 1;
182 core::hint::spin_loop();
183 }
184 Err(_) => return sent,
185 }
186 }
187 sent = end;
188 }
189 sent
190 }
191
192 #[inline]
194 pub fn communication_interface(&self) -> InterfaceNumber {
195 self.comm_if
196 }
197
198 #[inline]
200 pub fn data_interface(&self) -> InterfaceNumber {
201 self.data_if
202 }
203
204 pub fn dtr(&self) -> bool {
206 self.dtr
207 }
208
209 pub fn rts(&self) -> bool {
211 self.rts
212 }
213
214 pub fn port_open(&self) -> bool {
216 self.configured && self.dtr
217 }
218
219 pub fn configured(&self) -> bool {
221 self.configured
222 }
223
224 pub fn line_coding(&self) -> LineCoding {
226 self.line_coding
227 }
228}
229
230impl<B: UsbBus> UsbClass<B> for CdcAcm<'_, B> {
233 fn configuration_descriptors(&self, w: &mut DescriptorWriter) -> UsbResult<()> {
234 w.iad(
236 self.comm_if,
237 2, class_code::CDC,
239 cdc_subclass::ACM,
240 cdc_protocol::NONE,
241 )?;
242
243 w.interface(
245 self.comm_if,
246 1, class_code::CDC,
248 cdc_subclass::ACM,
249 cdc_protocol::NONE,
250 )?;
251
252 w.write(
254 cdc_descriptor::CS_INTERFACE,
255 &[cdc_descriptor::HEADER, 0x10, 0x01], )?;
257 w.write(
259 cdc_descriptor::CS_INTERFACE,
260 &[
261 cdc_descriptor::CALL_MANAGEMENT,
262 0x00, self.data_if.into(),
264 ],
265 )?;
266 w.write(
268 cdc_descriptor::CS_INTERFACE,
269 &[
270 cdc_descriptor::ABSTRACT_CONTROL_MANAGEMENT,
271 0x02, ],
273 )?;
274 w.write(
276 cdc_descriptor::CS_INTERFACE,
277 &[
278 cdc_descriptor::UNION,
279 self.comm_if.into(),
280 self.data_if.into(),
281 ],
282 )?;
283 w.endpoint(
285 self.int_in.address(),
286 self.int_in.ep_type(),
287 self.int_in.max_packet_size(),
288 self.int_in.interval(),
289 )?;
290
291 w.interface(
293 self.data_if,
294 2, class_code::CDC_DATA,
296 0x00,
297 0x00,
298 )?;
299 w.endpoint(
300 self.bulk_out.address(),
301 self.bulk_out.ep_type(),
302 self.bulk_out.max_packet_size(),
303 0,
304 )?;
305 w.endpoint(
306 self.bulk_in.address(),
307 self.bulk_in.ep_type(),
308 self.bulk_in.max_packet_size(),
309 0,
310 )?;
311
312 Ok(())
313 }
314
315 fn control_in(&mut self, xfer: ControlIn<'_, B>) {
316 let req = xfer.request();
317
318 if req.request_type != RequestType::Class || req.recipient != Recipient::Interface {
319 return;
320 }
321 if req.index as u8 != u8::from(self.comm_if) {
322 return;
323 }
324
325 match req.request {
326 req::GET_LINE_CODING => {
327 let bytes = self.line_coding.to_bytes();
328 let _ = xfer.accept_with(&bytes);
329 }
330 _ => {
331 let _ = xfer.reject();
332 }
333 }
334 }
335
336 fn control_out(&mut self, xfer: ControlOut<'_, B>) {
337 let req = xfer.request();
338
339 if req.request_type != RequestType::Class || req.recipient != Recipient::Interface {
340 return;
341 }
342 if req.index as u8 != u8::from(self.comm_if) {
343 return;
344 }
345
346 match req.request {
347 req::SET_LINE_CODING => {
348 if let Some(lc) = LineCoding::from_bytes(xfer.data()) {
349 self.line_coding = lc;
350 #[cfg(feature = "defmt")]
351 crate::log::debug!("CDC: SET_LINE_CODING baud={}", lc.baud_rate);
352 }
353 let _ = xfer.accept();
354 }
355 req::SET_CONTROL_LINE_STATE => {
356 let value = req.value;
357 self.dtr = value & 0x01 != 0;
358 self.rts = value & 0x02 != 0;
359 #[cfg(feature = "defmt")]
360 crate::log::debug!("CDC: control line state dtr={} rts={}", self.dtr, self.rts);
361 let _ = xfer.accept();
362 }
363 req::SEND_BREAK => {
364 let _ = xfer.accept();
366 }
367 _ => {
368 let _ = xfer.reject();
369 }
370 }
371 }
372
373 fn configure(&mut self) {
374 self.configured = true;
375 }
376
377 fn deconfigure(&mut self) {
378 self.configured = false;
379 }
380
381 fn reset(&mut self) {
382 self.configured = false;
383 self.dtr = false;
384 self.rts = false;
385 }
386}