RX and TX buffers
This commit is contained in:
parent
402e8e64ab
commit
5d1d0f9541
33
src/main.rs
33
src/main.rs
|
@ -19,10 +19,12 @@ use usb_device::prelude::*;
|
||||||
use usbd_serial::{SerialPort, USB_CLASS_CDC};
|
use usbd_serial::{SerialPort, USB_CLASS_CDC};
|
||||||
|
|
||||||
#[cfg(not(feature = "atomic"))]
|
#[cfg(not(feature = "atomic"))]
|
||||||
use bbqueue::{consts::*, BBBuffer, ConstBBBuffer, Producer};
|
use bbqueue::{consts::*, BBBuffer, ConstBBBuffer, Consumer, Producer};
|
||||||
|
|
||||||
static RX_QUEUE: BBBuffer<U128> = BBBuffer(ConstBBBuffer::new());
|
static RX_QUEUE: BBBuffer<U128> = BBBuffer(ConstBBBuffer::new());
|
||||||
static RX_PROD: Mutex<RefCell<Option<Producer<U128>>>> = Mutex::new(RefCell::new(None));
|
static RX_PROD: Mutex<RefCell<Option<Producer<U128>>>> = Mutex::new(RefCell::new(None));
|
||||||
|
static TX_QUEUE: BBBuffer<U128> = BBBuffer(ConstBBBuffer::new());
|
||||||
|
static TX_CONS: Mutex<RefCell<Option<Consumer<U128>>>> = Mutex::new(RefCell::new(None));
|
||||||
|
|
||||||
static mut USB_BUS: Option<UsbBusAllocator<UsbBusType>> = None;
|
static mut USB_BUS: Option<UsbBusAllocator<UsbBusType>> = None;
|
||||||
static USB_DEVICE: Mutex<RefCell<Option<UsbDevice<'static, UsbBusType>>>> =
|
static USB_DEVICE: Mutex<RefCell<Option<UsbDevice<'static, UsbBusType>>>> =
|
||||||
|
@ -72,6 +74,7 @@ fn main() -> ! {
|
||||||
}
|
}
|
||||||
|
|
||||||
let (rx_prod, mut rx_cons) = RX_QUEUE.try_split().unwrap();
|
let (rx_prod, mut rx_cons) = RX_QUEUE.try_split().unwrap();
|
||||||
|
let (mut tx_prod, tx_cons) = TX_QUEUE.try_split().unwrap();
|
||||||
|
|
||||||
interrupt_free(|cs| {
|
interrupt_free(|cs| {
|
||||||
SERIAL.borrow(cs).replace(Some(SerialPort::new(usb_bus)));
|
SERIAL.borrow(cs).replace(Some(SerialPort::new(usb_bus)));
|
||||||
|
@ -85,6 +88,7 @@ fn main() -> ! {
|
||||||
));
|
));
|
||||||
|
|
||||||
RX_PROD.borrow(cs).replace(Some(rx_prod));
|
RX_PROD.borrow(cs).replace(Some(rx_prod));
|
||||||
|
TX_CONS.borrow(cs).replace(Some(tx_cons));
|
||||||
});
|
});
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -108,11 +112,11 @@ fn main() -> ! {
|
||||||
}
|
}
|
||||||
_ => "",
|
_ => "",
|
||||||
};
|
};
|
||||||
interrupt_free(|cs| {
|
|
||||||
if let Some(serial) = SERIAL.borrow(cs).borrow_mut().deref_mut() {
|
let mut wgr = tx_prod.grant_exact(resp.len()).unwrap();
|
||||||
serial.write(resp.as_bytes()).unwrap();
|
wgr.buf().copy_from_slice(resp.as_bytes());
|
||||||
}
|
wgr.commit(resp.len());
|
||||||
});
|
usb_trigger_write();
|
||||||
}
|
}
|
||||||
|
|
||||||
let len = rgr.len();
|
let len = rgr.len();
|
||||||
|
@ -152,4 +156,21 @@ fn usb_interrupt() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
usb_trigger_write();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usb_trigger_write() {
|
||||||
|
interrupt_free(|cs| {
|
||||||
|
if let Some(serial) = SERIAL.borrow(cs).borrow_mut().deref_mut() {
|
||||||
|
if let Some(tx_cons) = TX_CONS.borrow(cs).borrow_mut().deref_mut() {
|
||||||
|
if let Ok(rgr) = tx_cons.read() {
|
||||||
|
if let Ok(bytes_written) = serial.write(&rgr) {
|
||||||
|
rgr.release(bytes_written)
|
||||||
|
} else {
|
||||||
|
rgr.release(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue