change "baremetal" to "std" as a feature

has the opposite meaning, makes it easier to just include the
things that we don't want in kernel or loader mode
This commit is contained in:
bunnie 2024-01-27 16:52:39 +08:00
parent 38fe939c91
commit 74219d58f5
4 changed files with 33 additions and 15 deletions

View file

@ -6,8 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
xous-api-names = "0.9.55"
log = "0.4.14"
xous-api-names = { version = "0.9.55", optional = true }
log = { version = "0.4.14", optional = true }
utralib = { version = "0.1.24", default-features = false, features = [
"cramium-soc",
] }
@ -22,6 +22,6 @@ num-traits = { version = "0.2.14", default-features = false }
xous = { version = "0.9.57", features = ["v2p"] }
[features]
baremetal = []
std = ["log", "xous-api-names"]
derive-rkyv = ["rkyv"]
default = []

View file

@ -46,6 +46,7 @@ impl IframRange {
/// scope, but the trade-off is that IFRAM is very limited in capacity
/// and hanging on to chunks much longer than necessary can lead to memory
/// exhaustion.
#[cfg(feature = "std")]
pub unsafe fn request(length: usize, bank: Option<IframBank>) -> Option<Self> {
REFCOUNT.fetch_add(1, Ordering::Relaxed);
let xns = xous_api_names::XousNames::new().unwrap();
@ -152,6 +153,7 @@ impl Drop for IframRange {
// This probably never happens, but also probably doesn't need to be a hard-panic
// if it does happen because it simply degrades performance; it does not impact
// correctness.
#[cfg(feature = "std")]
log::error!("Couldn't de-allocate IframRange: {:?}, IFRAM memory is leaking!", e)
}
}

View file

@ -1,4 +1,4 @@
#![no_std]
#![cfg_attr(not(feature = "std"), no_std)]
pub mod ifram;
pub mod iox;

View file

@ -404,8 +404,10 @@ impl Uart {
/// the clock manually as well once the object is dropped...
///
/// Allocates a 4096-deep buffer for tx/rx purposes: the first 2048 bytes
/// are used for Tx, the second 2048 bytes for Rx.
#[cfg(not(feature = "baremetal"))]
/// are used for Tx, the second 2048 bytes for Rx. If this buffer size has
/// to change, be sure to update the loader, as it takes this as an assumption
/// since no IFRAM allocator is running at that time.
#[cfg(feature = "std")]
pub unsafe fn new(channel: UartChannel, baud: u32, clk_freq: u32) -> Self {
assert!(UART_RX_BUF_SIZE + UART_TX_BUF_SIZE == 4096, "Configuration error in UDMA UART");
let bank_addr = match channel {
@ -452,6 +454,19 @@ impl Uart {
}
}
pub fn set_baud(&self, baud: u32, clk_freq: u32) {
let clk_counter: u32 = (clk_freq + baud / 2) / baud;
// setup baud, bits, parity, etc.
// safety: this is safe to call as long as the base address points at a valid UART.
unsafe {
self.csr
.base()
.add(Bank::Custom.into())
.add(UartReg::Setup.into())
.write_volatile(0x0306 | (clk_counter << 16));
}
}
pub fn disable(&mut self) {
self.wait_tx_done();
// safe only in the context of a UART UDMA address
@ -476,7 +491,7 @@ impl Uart {
pub fn wait_tx_done(&self) {
while self.udma_busy(Bank::Tx) {
#[cfg(not(feature = "baremetal"))]
#[cfg(feature = "std")]
xous::yield_slice();
}
while self.tx_busy() {}
@ -484,23 +499,23 @@ impl Uart {
pub fn wait_rx_done(&self) {
while self.udma_busy(Bank::Rx) {
#[cfg(not(feature = "baremetal"))]
#[cfg(feature = "std")]
xous::yield_slice();
}
}
/// `buf` is assumed to be a virtual address (in Xous), or a machine address
/// `buf` is assumed to be a virtual address (in `std`), or a machine address
/// (in baremetal mode). This function is safe because it will operate as intended
/// within a given environment, so long as the `baremetal` flag is applied correctly.
/// within a given environment, so long as the `std` flag is applied correctly.
///
/// In "baremetal" mode, it's *also* assumed that `buf` is range-checked to be valid
/// When not in `std`, it's *also* assumed that `buf` is range-checked to be valid
/// for the UDMA engine.
///
/// returns: total length of bytes written
pub fn write(&mut self, buf: &[u8]) -> usize {
let mut writelen = 0;
for chunk in buf.chunks(UART_TX_BUF_SIZE) {
#[cfg(not(feature = "baremetal"))]
#[cfg(feature = "std")]
{
self.ifram.as_slice_mut()[..chunk.len()].copy_from_slice(chunk);
// safety: the slice is in the physical range for the UDMA, and length-checked
@ -513,7 +528,7 @@ impl Uart {
}
writelen += chunk.len();
}
#[cfg(feature = "baremetal")]
#[cfg(not(feature = "std"))]
unsafe {
self.udma_enqueue(Bank::Tx, chunk, CFG_EN | CFG_SIZE_8);
writelen += chunk.len();
@ -526,7 +541,7 @@ impl Uart {
pub fn read(&mut self, buf: &mut [u8]) {
for chunk in buf.chunks_mut(UART_RX_BUF_SIZE) {
#[cfg(not(feature = "baremetal"))]
#[cfg(feature = "std")]
unsafe {
self.udma_enqueue(
Bank::Rx,
@ -534,7 +549,7 @@ impl Uart {
CFG_EN | CFG_SIZE_8,
);
}
#[cfg(feature = "baremetal")]
#[cfg(not(feature = "std"))]
unsafe {
self.udma_enqueue(
Bank::Rx,
@ -704,6 +719,7 @@ impl Spim {
///
/// Return: the function can return None if it can't allocate enough memory
/// for the requested tx/rx length.
#[cfg(feature = "std")]
pub unsafe fn new(
channel: SpimChannel,
spi_clk_freq: u32,