diff options
| author | Dániel Buga <[email protected]> | 2025-04-17 22:29:15 +0200 |
|---|---|---|
| committer | Dániel Buga <[email protected]> | 2025-04-17 22:29:15 +0200 |
| commit | b0eacf0eecbef48ac526da6bce250189e4999ce5 (patch) | |
| tree | a0ab5e3c782facc1f054b7902a8fc55f6b4ec98c /embassy-usb/src | |
| parent | e410e65b830f5486a9d15b87039817aa4668e06f (diff) | |
Add optional trace endpoint
Diffstat (limited to 'embassy-usb/src')
| -rw-r--r-- | embassy-usb/src/class/cmsis_dap_v2.rs | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/embassy-usb/src/class/cmsis_dap_v2.rs b/embassy-usb/src/class/cmsis_dap_v2.rs index 41f6be5dd..a94e3ddb7 100644 --- a/embassy-usb/src/class/cmsis_dap_v2.rs +++ b/embassy-usb/src/class/cmsis_dap_v2.rs | |||
| @@ -39,13 +39,17 @@ impl State { | |||
| 39 | pub struct CmsisDapV2Class<'d, D: Driver<'d>> { | 39 | pub struct CmsisDapV2Class<'d, D: Driver<'d>> { |
| 40 | read_ep: D::EndpointOut, | 40 | read_ep: D::EndpointOut, |
| 41 | write_ep: D::EndpointIn, | 41 | write_ep: D::EndpointIn, |
| 42 | trace_ep: Option<D::EndpointIn>, | ||
| 42 | max_packet_size: u16, | 43 | max_packet_size: u16, |
| 43 | } | 44 | } |
| 44 | 45 | ||
| 45 | impl<'d, D: Driver<'d>> CmsisDapV2Class<'d, D> { | 46 | impl<'d, D: Driver<'d>> CmsisDapV2Class<'d, D> { |
| 46 | /// Creates a new CmsisDapV2Class with the provided UsbBus and `max_packet_size` in bytes. For | 47 | /// Creates a new CmsisDapV2Class with the provided UsbBus and `max_packet_size` in bytes. For |
| 47 | /// full-speed devices, `max_packet_size` has to be 64. | 48 | /// full-speed devices, `max_packet_size` has to be 64. |
| 48 | pub fn new(builder: &mut Builder<'d, D>, state: &'d mut State, max_packet_size: u16) -> Self { | 49 | /// |
| 50 | /// The `trace` parameter enables the trace output endpoint. This is optional and can be | ||
| 51 | /// disabled if the probe does not support trace output. | ||
| 52 | pub fn new(builder: &mut Builder<'d, D>, state: &'d mut State, max_packet_size: u16, trace: bool) -> Self { | ||
| 49 | // DAP - Custom Class 0 | 53 | // DAP - Custom Class 0 |
| 50 | let iface_string = builder.string(); | 54 | let iface_string = builder.string(); |
| 51 | let mut function = builder.function(0xFF, 0, 0); | 55 | let mut function = builder.function(0xFF, 0, 0); |
| @@ -59,6 +63,11 @@ impl<'d, D: Driver<'d>> CmsisDapV2Class<'d, D> { | |||
| 59 | let mut alt = interface.alt_setting(0xFF, 0, 0, Some(iface_string)); | 63 | let mut alt = interface.alt_setting(0xFF, 0, 0, Some(iface_string)); |
| 60 | let read_ep = alt.endpoint_bulk_out(max_packet_size); | 64 | let read_ep = alt.endpoint_bulk_out(max_packet_size); |
| 61 | let write_ep = alt.endpoint_bulk_in(max_packet_size); | 65 | let write_ep = alt.endpoint_bulk_in(max_packet_size); |
| 66 | let trace_ep = if trace { | ||
| 67 | Some(alt.endpoint_bulk_in(max_packet_size)) | ||
| 68 | } else { | ||
| 69 | None | ||
| 70 | }; | ||
| 62 | drop(function); | 71 | drop(function); |
| 63 | 72 | ||
| 64 | builder.handler(state.control.write(Control { iface_string })); | 73 | builder.handler(state.control.write(Control { iface_string })); |
| @@ -66,6 +75,7 @@ impl<'d, D: Driver<'d>> CmsisDapV2Class<'d, D> { | |||
| 66 | CmsisDapV2Class { | 75 | CmsisDapV2Class { |
| 67 | read_ep, | 76 | read_ep, |
| 68 | write_ep, | 77 | write_ep, |
| 78 | trace_ep, | ||
| 69 | max_packet_size, | 79 | max_packet_size, |
| 70 | } | 80 | } |
| 71 | } | 81 | } |
| @@ -86,6 +96,23 @@ impl<'d, D: Driver<'d>> CmsisDapV2Class<'d, D> { | |||
| 86 | Ok(()) | 96 | Ok(()) |
| 87 | } | 97 | } |
| 88 | 98 | ||
| 99 | /// Write data to the host via the trace output endpoint. | ||
| 100 | /// | ||
| 101 | /// Returns `EndpointError::Disabled` if the trace output endpoint is not enabled. | ||
| 102 | pub async fn write_trace(&mut self, data: &[u8]) -> Result<(), EndpointError> { | ||
| 103 | let Some(ep) = self.trace_ep.as_mut() else { | ||
| 104 | return Err(EndpointError::Disabled); | ||
| 105 | }; | ||
| 106 | |||
| 107 | for chunk in data.chunks(self.max_packet_size as usize) { | ||
| 108 | ep.write(chunk).await?; | ||
| 109 | } | ||
| 110 | if data.len() % self.max_packet_size as usize == 0 { | ||
| 111 | ep.write(&[]).await?; | ||
| 112 | } | ||
| 113 | Ok(()) | ||
| 114 | } | ||
| 115 | |||
| 89 | /// Read data from the host. | 116 | /// Read data from the host. |
| 90 | pub async fn read_packet(&mut self, data: &mut [u8]) -> Result<usize, EndpointError> { | 117 | pub async fn read_packet(&mut self, data: &mut [u8]) -> Result<usize, EndpointError> { |
| 91 | let mut n = 0; | 118 | let mut n = 0; |
