Skip to content

Commit

Permalink
hil: uart: force baud rate to be nonzero
Browse files Browse the repository at this point in the history
  • Loading branch information
bradjc committed Feb 26, 2025
1 parent 24465d3 commit bea1b9b
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 17 deletions.
3 changes: 1 addition & 2 deletions boards/components/src/debug_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use kernel::collections::ring_buffer::RingBuffer;
use kernel::component::Component;
use kernel::hil;
use kernel::hil::uart;
use kernel::hil::uart::BaudRate;

// The sum of the output_buf and internal_buf is set to a multiple of 1024 bytes in order to avoid excessive
// padding between kernel memory and application memory (which often needs to be aligned to at
Expand Down Expand Up @@ -185,7 +184,7 @@ impl<U: uart::Uart<'static> + uart::Transmit<'static> + 'static, const BUF_SIZE_
}

let _ = self.uart.configure(uart::Parameters {
baud_rate: U::BaudRate::from_nonzero(115200),
baud_rate: core::num::NonZeroU32::new(115200).unwrap().into(),
width: uart::Width::Eight,
stop_bits: uart::StopBits::One,
parity: uart::Parity::None,
Expand Down
3 changes: 1 addition & 2 deletions capsules/extra/src/nrf51822_serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use core::cmp;
use kernel::grant::{AllowRoCount, AllowRwCount, Grant, UpcallCount};
use kernel::hil;
use kernel::hil::uart;
use kernel::hil::uart::BaudRate;
use kernel::processbuffer::{ReadableProcessBuffer, WriteableProcessBuffer};
use kernel::syscall::{CommandReturn, SyscallDriver};
use kernel::utilities::cells::{OptionalCell, TakeCell};
Expand Down Expand Up @@ -119,7 +118,7 @@ impl<'a, U: uart::UartAdvanced<'a>> Nrf51822Serialization<'a, U> {

pub fn initialize(&self) {
let _ = self.uart.configure(uart::Parameters {
baud_rate: U::BaudRate::from_nonzero(250000),
baud_rate: core::num::NonZeroU32::new(250000).unwrap().into(),
width: uart::Width::Eight,
stop_bits: uart::StopBits::One,
parity: uart::Parity::Even,
Expand Down
2 changes: 1 addition & 1 deletion capsules/extra/src/usb/cdc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ impl<'a, U: hil::usb::UsbController<'a>, A: 'a + Alarm<'a>> hil::usb::Client<'a>
}

impl<'a, U: hil::usb::UsbController<'a>, A: 'a + Alarm<'a>> uart::Configure for CdcAcm<'a, U, A> {
type BaudRate = ();
type BaudRate = uart::NonexistentBaudRate;

fn configure(&self, _parameters: uart::Parameters<Self::BaudRate>) -> Result<(), ErrorCode> {
// Since this is not a real UART, we don't need to consider these
Expand Down
6 changes: 3 additions & 3 deletions chips/nrf52/src/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ pub enum UarteBaudRate {
Baud1000000,
}

impl uart::BaudRate for UarteBaudRate {
fn from_nonzero(baud_rate: u32) -> Self {
match baud_rate {
impl From<core::num::NonZeroU32> for UarteBaudRate {
fn from(baud_rate: core::num::NonZeroU32) -> Self {
match baud_rate.get() {
1200 => UarteBaudRate::Baud1200,
2400 => UarteBaudRate::Baud2400,
4800 => UarteBaudRate::Baud4800,
Expand Down
2 changes: 1 addition & 1 deletion chips/segger/src/rtt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ impl<'a, A: hil::time::Alarm<'a>> hil::time::AlarmClient for SeggerRtt<'a, A> {
// Dummy implementation so this can act as the underlying UART for a
// virtualized UART MUX. -pal 1/10/19
impl<'a, A: hil::time::Alarm<'a>> uart::Configure for SeggerRtt<'a, A> {
type BaudRate = ();
type BaudRate = uart::NonexistentBaudRate;

fn configure(&self, _parameters: uart::Parameters<Self::BaudRate>) -> Result<(), ErrorCode> {
Err(ErrorCode::FAIL)
Expand Down
16 changes: 8 additions & 8 deletions kernel/src/hil/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ pub enum Width {
Eight = 8,
}

pub trait BaudRate {
fn from_nonzero(baud_rate: u32) -> Self;
}
// pub trait BaudRate: {}

impl BaudRate for () {
fn from_nonzero(_baud_rate: u32) -> Self {
()
#[derive(Copy, Clone)]
pub struct NonexistentBaudRate {}
impl From<core::num::NonZeroU32> for NonexistentBaudRate {
fn from(_baud_rate: core::num::NonZeroU32) -> Self {
NonexistentBaudRate {}
}
}

/// UART parameters for configuring the bus.
#[derive(Copy, Clone, Debug)]
pub struct Parameters<BaudRateType: BaudRate> {
pub struct Parameters<BaudRateType: From<core::num::NonZeroU32> + Copy> {
/// Baud rate in bit/s.
pub baud_rate: BaudRateType,
/// Number of bits per word.
Expand Down Expand Up @@ -120,7 +120,7 @@ impl<T: ReceiveClient + TransmitClient> Client for T {}

/// Trait for configuring a UART.
pub trait Configure {
type BaudRate: BaudRate + Copy;
type BaudRate: From<core::num::NonZeroU32> + Copy;

/// Set the configuration parameters for the UART bus.
///
Expand Down

0 comments on commit bea1b9b

Please sign in to comment.