1#![doc = crate::before_snippet!()]
47#![doc = crate::before_snippet!()]
60#![doc = crate::before_snippet!()]
78#[instability::unstable]
125use core::task::Poll;
126use core::{convert::Infallible, marker::PhantomData};
127
128use procmacros::handler;
129
130use crate::{
131 asynch::AtomicWaker,
132 pac::usb_device::RegisterBlock,
133 peripheral::{Peripheral, PeripheralRef},
134 peripherals::{Interrupt, USB_DEVICE},
135 system::{Cpu, PeripheralClockControl},
136 Async,
137 Blocking,
138 DriverMode,
139};
140
141type Error = Infallible;
143
144pub struct UsbSerialJtag<'d, Dm: DriverMode> {
146 rx: UsbSerialJtagRx<'d, Dm>,
147 tx: UsbSerialJtagTx<'d, Dm>,
148}
149
150pub struct UsbSerialJtagTx<'d, Dm: DriverMode> {
152 peripheral: PeripheralRef<'d, USB_DEVICE>,
153 phantom: PhantomData<Dm>,
154}
155
156pub struct UsbSerialJtagRx<'d, Dm: DriverMode> {
158 peripheral: PeripheralRef<'d, USB_DEVICE>,
159 phantom: PhantomData<Dm>,
160}
161
162impl<'d, Dm> UsbSerialJtagTx<'d, Dm>
163where
164 Dm: DriverMode,
165{
166 fn new_inner(peripheral: impl Peripheral<P = USB_DEVICE> + 'd) -> Self {
167 crate::into_ref!(peripheral);
168 Self {
169 peripheral,
170 phantom: PhantomData,
171 }
172 }
173
174 fn regs(&self) -> &RegisterBlock {
175 self.peripheral.register_block()
176 }
177
178 pub fn write(&mut self, data: &[u8]) -> Result<(), Error> {
180 for chunk in data.chunks(64) {
181 for byte in chunk {
182 self.regs()
183 .ep1()
184 .write(|w| unsafe { w.rdwr_byte().bits(*byte) });
185 }
186 self.regs().ep1_conf().modify(|_, w| w.wr_done().set_bit());
187
188 while self.regs().ep1_conf().read().bits() & 0b011 == 0b000 {
190 }
192 }
193
194 Ok(())
195 }
196
197 pub fn write_byte_nb(&mut self, word: u8) -> nb::Result<(), Error> {
200 if self
201 .regs()
202 .ep1_conf()
203 .read()
204 .serial_in_ep_data_free()
205 .bit_is_set()
206 {
207 unsafe { self.regs().ep1().write(|w| w.rdwr_byte().bits(word)) };
209
210 Ok(())
211 } else {
212 Err(nb::Error::WouldBlock)
213 }
214 }
215
216 pub fn flush_tx(&mut self) -> Result<(), Error> {
218 self.regs().ep1_conf().modify(|_, w| w.wr_done().set_bit());
219
220 while self.regs().ep1_conf().read().bits() & 0b011 == 0b000 {
222 }
224
225 Ok(())
226 }
227
228 pub fn flush_tx_nb(&mut self) -> nb::Result<(), Error> {
230 self.regs().ep1_conf().modify(|_, w| w.wr_done().set_bit());
231
232 if self.regs().ep1_conf().read().bits() & 0b011 == 0b000 {
234 Err(nb::Error::WouldBlock)
235 } else {
236 Ok(())
237 }
238 }
239}
240
241impl<'d, Dm> UsbSerialJtagRx<'d, Dm>
242where
243 Dm: DriverMode,
244{
245 fn new_inner(peripheral: impl Peripheral<P = USB_DEVICE> + 'd) -> Self {
246 crate::into_ref!(peripheral);
247 Self {
248 peripheral,
249 phantom: PhantomData,
250 }
251 }
252
253 fn regs(&self) -> &RegisterBlock {
254 self.peripheral.register_block()
255 }
256
257 pub fn read_byte(&mut self) -> nb::Result<u8, Error> {
259 if self
261 .regs()
262 .ep1_conf()
263 .read()
264 .serial_out_ep_data_avail()
265 .bit_is_set()
266 {
267 let value = self.regs().ep1().read().rdwr_byte().bits();
268
269 Ok(value)
270 } else {
271 Err(nb::Error::WouldBlock)
272 }
273 }
274
275 pub fn drain_rx_fifo(&mut self, buf: &mut [u8]) -> usize {
279 let mut count = 0;
280 while let Ok(value) = self.read_byte() {
281 buf[count] = value;
282 count += 1;
283 if count == buf.len() {
284 break;
285 }
286 }
287 count
288 }
289
290 pub fn listen_rx_packet_recv_interrupt(&mut self) {
292 self.regs()
293 .int_ena()
294 .modify(|_, w| w.serial_out_recv_pkt().set_bit());
295 }
296
297 pub fn unlisten_rx_packet_recv_interrupt(&mut self) {
299 self.regs()
300 .int_ena()
301 .modify(|_, w| w.serial_out_recv_pkt().clear_bit());
302 }
303
304 pub fn rx_packet_recv_interrupt_set(&mut self) -> bool {
306 self.regs()
307 .int_st()
308 .read()
309 .serial_out_recv_pkt()
310 .bit_is_set()
311 }
312
313 pub fn reset_rx_packet_recv_interrupt(&mut self) {
315 self.regs()
316 .int_clr()
317 .write(|w| w.serial_out_recv_pkt().clear_bit_by_one());
318 }
319}
320
321impl<'d> UsbSerialJtag<'d, Blocking> {
322 pub fn new(usb_device: impl Peripheral<P = USB_DEVICE> + 'd) -> Self {
324 Self::new_inner(usb_device)
325 }
326
327 pub fn into_async(mut self) -> UsbSerialJtag<'d, Async> {
330 self.set_interrupt_handler(async_interrupt_handler);
331
332 UsbSerialJtag {
333 rx: UsbSerialJtagRx {
334 peripheral: self.rx.peripheral,
335 phantom: PhantomData,
336 },
337 tx: UsbSerialJtagTx {
338 peripheral: self.tx.peripheral,
339 phantom: PhantomData,
340 },
341 }
342 }
343}
344
345impl crate::private::Sealed for UsbSerialJtag<'_, Blocking> {}
346
347#[instability::unstable]
348impl crate::interrupt::InterruptConfigurable for UsbSerialJtag<'_, Blocking> {
349 fn set_interrupt_handler(&mut self, handler: crate::interrupt::InterruptHandler) {
350 self.set_interrupt_handler(handler);
351 }
352}
353
354impl<'d, Dm> UsbSerialJtag<'d, Dm>
355where
356 Dm: DriverMode,
357{
358 fn new_inner(usb_device: impl Peripheral<P = USB_DEVICE> + 'd) -> Self {
359 PeripheralClockControl::enable(crate::system::Peripheral::UsbDevice);
362
363 crate::into_ref!(usb_device);
364
365 usb_device.disable_tx_interrupts();
366 usb_device.disable_rx_interrupts();
367
368 #[cfg(any(esp32c3, esp32s3))]
369 {
370 use crate::soc::efuse::*;
371
372 if Efuse::read_bit(USB_EXCHG_PINS) {
375 usb_device.register_block().conf0().modify(|_, w| {
376 w.pad_pull_override().set_bit();
377 w.dm_pullup().clear_bit();
378 w.dp_pullup().set_bit()
379 });
380 }
381 }
382
383 crate::into_ref!(usb_device);
384
385 Self {
386 rx: UsbSerialJtagRx::new_inner(unsafe { usb_device.clone_unchecked() }),
387 tx: UsbSerialJtagTx::new_inner(usb_device),
388 }
389 }
390 pub fn split(self) -> (UsbSerialJtagRx<'d, Dm>, UsbSerialJtagTx<'d, Dm>) {
394 (self.rx, self.tx)
395 }
396
397 pub fn write(&mut self, data: &[u8]) -> Result<(), Error> {
399 self.tx.write(data)
400 }
401
402 pub fn write_byte_nb(&mut self, word: u8) -> nb::Result<(), Error> {
405 self.tx.write_byte_nb(word)
406 }
407
408 pub fn flush_tx(&mut self) -> Result<(), Error> {
410 self.tx.flush_tx()
411 }
412
413 pub fn flush_tx_nb(&mut self) -> nb::Result<(), Error> {
415 self.tx.flush_tx_nb()
416 }
417
418 pub fn read_byte(&mut self) -> nb::Result<u8, Error> {
420 self.rx.read_byte()
421 }
422
423 pub fn listen_rx_packet_recv_interrupt(&mut self) {
425 self.rx.listen_rx_packet_recv_interrupt()
426 }
427
428 pub fn unlisten_rx_packet_recv_interrupt(&mut self) {
430 self.rx.unlisten_rx_packet_recv_interrupt()
431 }
432
433 pub fn rx_packet_recv_interrupt_set(&mut self) -> bool {
435 self.rx.rx_packet_recv_interrupt_set()
436 }
437
438 pub fn reset_rx_packet_recv_interrupt(&mut self) {
440 self.rx.reset_rx_packet_recv_interrupt()
441 }
442
443 #[instability::unstable]
448 pub fn set_interrupt_handler(&mut self, handler: crate::interrupt::InterruptHandler) {
449 for core in crate::system::Cpu::other() {
450 crate::interrupt::disable(core, Interrupt::USB_DEVICE);
451 }
452 unsafe { crate::interrupt::bind_interrupt(Interrupt::USB_DEVICE, handler.handler()) };
453 unwrap!(crate::interrupt::enable(
454 Interrupt::USB_DEVICE,
455 handler.priority()
456 ));
457 }
458}
459
460#[doc(hidden)]
462pub trait Instance: crate::private::Sealed {
463 fn register_block(&self) -> &RegisterBlock;
465
466 fn disable_tx_interrupts(&self) {
468 self.register_block()
469 .int_ena()
470 .modify(|_, w| w.serial_in_empty().clear_bit());
471
472 self.register_block()
473 .int_clr()
474 .write(|w| w.serial_in_empty().clear_bit_by_one());
475 }
476
477 fn disable_rx_interrupts(&self) {
479 self.register_block()
480 .int_ena()
481 .modify(|_, w| w.serial_out_recv_pkt().clear_bit());
482
483 self.register_block()
484 .int_clr()
485 .write(|w| w.serial_out_recv_pkt().clear_bit_by_one());
486 }
487}
488
489impl Instance for USB_DEVICE {
490 #[inline(always)]
491 fn register_block(&self) -> &RegisterBlock {
492 USB_DEVICE::regs()
493 }
494}
495
496impl<Dm> core::fmt::Write for UsbSerialJtag<'_, Dm>
497where
498 Dm: DriverMode,
499{
500 fn write_str(&mut self, s: &str) -> core::fmt::Result {
501 core::fmt::Write::write_str(&mut self.tx, s)
502 }
503}
504
505impl<Dm> core::fmt::Write for UsbSerialJtagTx<'_, Dm>
506where
507 Dm: DriverMode,
508{
509 fn write_str(&mut self, s: &str) -> core::fmt::Result {
510 self.write(s.as_bytes()).map_err(|_| core::fmt::Error)?;
511 Ok(())
512 }
513}
514
515#[instability::unstable]
516impl<Dm> ufmt_write::uWrite for UsbSerialJtag<'_, Dm>
517where
518 Dm: DriverMode,
519{
520 type Error = Error;
521
522 #[inline]
523 fn write_str(&mut self, s: &str) -> Result<(), Self::Error> {
524 ufmt_write::uWrite::write_str(&mut self.tx, s)
525 }
526
527 #[inline]
528 fn write_char(&mut self, ch: char) -> Result<(), Self::Error> {
529 ufmt_write::uWrite::write_char(&mut self.tx, ch)
530 }
531}
532
533#[instability::unstable]
534impl<Dm> ufmt_write::uWrite for UsbSerialJtagTx<'_, Dm>
535where
536 Dm: DriverMode,
537{
538 type Error = Error;
539
540 #[inline]
541 fn write_str(&mut self, s: &str) -> Result<(), Self::Error> {
542 self.write(s.as_bytes())?;
543 Ok(())
544 }
545
546 #[inline]
547 fn write_char(&mut self, ch: char) -> Result<(), Self::Error> {
548 let mut buffer = [0u8; 4];
549 self.write(ch.encode_utf8(&mut buffer).as_bytes())?;
550
551 Ok(())
552 }
553}
554
555#[instability::unstable]
556impl<Dm> embedded_io::ErrorType for UsbSerialJtag<'_, Dm>
557where
558 Dm: DriverMode,
559{
560 type Error = Error;
561}
562
563#[instability::unstable]
564impl<Dm> embedded_io::ErrorType for UsbSerialJtagTx<'_, Dm>
565where
566 Dm: DriverMode,
567{
568 type Error = Error;
569}
570
571#[instability::unstable]
572impl<Dm> embedded_io::ErrorType for UsbSerialJtagRx<'_, Dm>
573where
574 Dm: DriverMode,
575{
576 type Error = Error;
577}
578
579#[instability::unstable]
580impl<Dm> embedded_io::Read for UsbSerialJtag<'_, Dm>
581where
582 Dm: DriverMode,
583{
584 fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
585 embedded_io::Read::read(&mut self.rx, buf)
586 }
587}
588
589#[instability::unstable]
590impl<Dm> embedded_io::Read for UsbSerialJtagRx<'_, Dm>
591where
592 Dm: DriverMode,
593{
594 fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
595 loop {
596 let count = self.drain_rx_fifo(buf);
597 if count > 0 {
598 return Ok(count);
599 }
600 }
601 }
602}
603
604#[instability::unstable]
605impl<Dm> embedded_io::Write for UsbSerialJtag<'_, Dm>
606where
607 Dm: DriverMode,
608{
609 fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
610 embedded_io::Write::write(&mut self.tx, buf)
611 }
612
613 fn flush(&mut self) -> Result<(), Self::Error> {
614 embedded_io::Write::flush(&mut self.tx)
615 }
616}
617
618#[instability::unstable]
619impl<Dm> embedded_io::Write for UsbSerialJtagTx<'_, Dm>
620where
621 Dm: DriverMode,
622{
623 fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
624 self.write(buf)?;
625
626 Ok(buf.len())
627 }
628
629 fn flush(&mut self) -> Result<(), Self::Error> {
630 self.flush_tx()
631 }
632}
633
634static WAKER_TX: AtomicWaker = AtomicWaker::new();
636static WAKER_RX: AtomicWaker = AtomicWaker::new();
637
638#[must_use = "futures do nothing unless you `.await` or poll them"]
639struct UsbSerialJtagWriteFuture<'d> {
640 peripheral: PeripheralRef<'d, USB_DEVICE>,
641}
642
643impl<'d> UsbSerialJtagWriteFuture<'d> {
644 fn new(peripheral: impl Peripheral<P = USB_DEVICE> + 'd) -> Self {
645 crate::into_ref!(peripheral);
646 peripheral
649 .register_block()
650 .int_ena()
651 .modify(|_, w| w.serial_in_empty().set_bit());
652
653 Self { peripheral }
654 }
655
656 fn event_bit_is_clear(&self) -> bool {
657 self.peripheral
658 .register_block()
659 .int_ena()
660 .read()
661 .serial_in_empty()
662 .bit_is_clear()
663 }
664}
665
666impl core::future::Future for UsbSerialJtagWriteFuture<'_> {
667 type Output = ();
668
669 fn poll(
670 self: core::pin::Pin<&mut Self>,
671 cx: &mut core::task::Context<'_>,
672 ) -> core::task::Poll<Self::Output> {
673 WAKER_TX.register(cx.waker());
674 if self.event_bit_is_clear() {
675 Poll::Ready(())
676 } else {
677 Poll::Pending
678 }
679 }
680}
681
682#[must_use = "futures do nothing unless you `.await` or poll them"]
683struct UsbSerialJtagReadFuture<'d> {
684 peripheral: PeripheralRef<'d, USB_DEVICE>,
685}
686
687impl<'d> UsbSerialJtagReadFuture<'d> {
688 fn new(peripheral: impl Peripheral<P = USB_DEVICE> + 'd) -> Self {
689 crate::into_ref!(peripheral);
690 peripheral
693 .register_block()
694 .int_ena()
695 .modify(|_, w| w.serial_out_recv_pkt().set_bit());
696
697 Self { peripheral }
698 }
699
700 fn event_bit_is_clear(&self) -> bool {
701 self.peripheral
702 .register_block()
703 .int_ena()
704 .read()
705 .serial_out_recv_pkt()
706 .bit_is_clear()
707 }
708}
709
710impl core::future::Future for UsbSerialJtagReadFuture<'_> {
711 type Output = ();
712
713 fn poll(
714 self: core::pin::Pin<&mut Self>,
715 cx: &mut core::task::Context<'_>,
716 ) -> core::task::Poll<Self::Output> {
717 WAKER_RX.register(cx.waker());
718 if self.event_bit_is_clear() {
719 Poll::Ready(())
720 } else {
721 Poll::Pending
722 }
723 }
724}
725
726impl<'d> UsbSerialJtag<'d, Async> {
727 pub fn into_blocking(self) -> UsbSerialJtag<'d, Blocking> {
730 crate::interrupt::disable(Cpu::current(), Interrupt::USB_DEVICE);
731 UsbSerialJtag {
732 rx: UsbSerialJtagRx {
733 peripheral: self.rx.peripheral,
734 phantom: PhantomData,
735 },
736 tx: UsbSerialJtagTx {
737 peripheral: self.tx.peripheral,
738 phantom: PhantomData,
739 },
740 }
741 }
742}
743
744impl UsbSerialJtagTx<'_, Async> {
745 async fn write_async(&mut self, words: &[u8]) -> Result<(), Error> {
746 for chunk in words.chunks(64) {
747 for byte in chunk {
748 self.regs()
749 .ep1()
750 .write(|w| unsafe { w.rdwr_byte().bits(*byte) });
751 }
752 self.regs().ep1_conf().modify(|_, w| w.wr_done().set_bit());
753
754 UsbSerialJtagWriteFuture::new(self.peripheral.reborrow()).await;
755 }
756
757 Ok(())
758 }
759
760 async fn flush_tx_async(&mut self) -> Result<(), Error> {
761 if self
762 .regs()
763 .jfifo_st()
764 .read()
765 .out_fifo_empty()
766 .bit_is_clear()
767 {
768 UsbSerialJtagWriteFuture::new(self.peripheral.reborrow()).await;
769 }
770
771 Ok(())
772 }
773}
774
775impl UsbSerialJtagRx<'_, Async> {
776 async fn read_async(&mut self, buf: &mut [u8]) -> Result<usize, Error> {
777 if buf.is_empty() {
778 return Ok(0);
779 }
780
781 loop {
782 let read = self.drain_rx_fifo(buf);
783 if read > 0 {
784 return Ok(read);
785 }
786 UsbSerialJtagReadFuture::new(self.peripheral.reborrow()).await;
787 }
788 }
789}
790
791#[instability::unstable]
792impl embedded_io_async::Write for UsbSerialJtag<'_, Async> {
793 async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
794 embedded_io_async::Write::write(&mut self.tx, buf).await
795 }
796
797 async fn flush(&mut self) -> Result<(), Self::Error> {
798 embedded_io_async::Write::flush(&mut self.tx).await
799 }
800}
801
802#[instability::unstable]
803impl embedded_io_async::Write for UsbSerialJtagTx<'_, Async> {
804 async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
805 self.write_async(buf).await?;
806
807 Ok(buf.len())
808 }
809
810 async fn flush(&mut self) -> Result<(), Self::Error> {
811 self.flush_tx_async().await
812 }
813}
814
815#[instability::unstable]
816impl embedded_io_async::Read for UsbSerialJtag<'_, Async> {
817 async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
818 embedded_io_async::Read::read(&mut self.rx, buf).await
819 }
820}
821
822#[instability::unstable]
823impl embedded_io_async::Read for UsbSerialJtagRx<'_, Async> {
824 async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
825 self.read_async(buf).await
826 }
827}
828
829#[handler]
830fn async_interrupt_handler() {
831 let usb = USB_DEVICE::regs();
832 let interrupts = usb.int_st().read();
833
834 let tx = interrupts.serial_in_empty().bit_is_set();
835 let rx = interrupts.serial_out_recv_pkt().bit_is_set();
836
837 if tx {
838 usb.int_ena().modify(|_, w| w.serial_in_empty().clear_bit());
839 }
840 if rx {
841 usb.int_ena()
842 .modify(|_, w| w.serial_out_recv_pkt().clear_bit());
843 }
844
845 usb.int_clr().write(|w| {
846 w.serial_in_empty()
847 .clear_bit_by_one()
848 .serial_out_recv_pkt()
849 .clear_bit_by_one()
850 });
851
852 if rx {
853 WAKER_RX.wake();
854 }
855 if tx {
856 WAKER_TX.wake();
857 }
858}