Refactor Uart initialization

This commit is contained in:
Jan Niehusmann 2021-12-02 21:33:21 +00:00
parent 6ab9cd7ed3
commit 26fa532fa3
5 changed files with 42 additions and 36 deletions

View file

@ -94,13 +94,12 @@ fn main() -> ! {
&mut pac.RESETS, &mut pac.RESETS,
); );
let mut uart = hal::uart::UartPeripheral::<_, _>::enable( let mut uart = hal::uart::UartPeripheral::<_, _>::new(pac.UART0, &mut pac.RESETS)
pac.UART0, .enable(
&mut pac.RESETS, hal::uart::common_configs::_115200_8_N_1,
hal::uart::common_configs::_115200_8_N_1, clocks.peripheral_clock.into(),
clocks.peripheral_clock.into(), )
) .unwrap();
.unwrap();
// UART TX (characters sent from RP2040) on pin 1 (GPIO0) // UART TX (characters sent from RP2040) on pin 1 (GPIO0)
let _tx_pin = pins.gpio0.into_mode::<hal::gpio::FunctionUart>(); let _tx_pin = pins.gpio0.into_mode::<hal::gpio::FunctionUart>();

View file

@ -85,13 +85,12 @@ fn main() -> ! {
); );
// Create a UART driver // Create a UART driver
let mut uart = hal::uart::UartPeripheral::<_, _>::enable( let mut uart = hal::uart::UartPeripheral::<_, _>::new(pac.UART0, &mut pac.RESETS)
pac.UART0, .enable(
&mut pac.RESETS, hal::uart::common_configs::_9600_8_N_1,
hal::uart::common_configs::_9600_8_N_1, clocks.peripheral_clock.into(),
clocks.peripheral_clock.into(), )
) .unwrap();
.unwrap();
// UART TX (characters sent from pico) on pin 1 (GPIO0) and RX (on pin 2 (GPIO1) // UART TX (characters sent from pico) on pin 1 (GPIO0) and RX (on pin 2 (GPIO1)
let _tx_pin = pins.gpio0.into_mode::<hal::gpio::FunctionUart>(); let _tx_pin = pins.gpio0.into_mode::<hal::gpio::FunctionUart>();

View file

@ -80,13 +80,12 @@ fn main() -> ! {
&mut pac.RESETS, &mut pac.RESETS,
); );
let mut uart = hal::uart::UartPeripheral::<_, _>::enable( let mut uart = hal::uart::UartPeripheral::<_, _>::new(pac.UART0, &mut pac.RESETS)
pac.UART0, .enable(
&mut pac.RESETS, hal::uart::common_configs::_9600_8_N_1,
hal::uart::common_configs::_9600_8_N_1, clocks.peripheral_clock.into(),
clocks.peripheral_clock.into(), )
) .unwrap();
.unwrap();
// UART TX (characters sent from RP2040) on pin 1 (GPIO0) // UART TX (characters sent from RP2040) on pin 1 (GPIO0)
let _tx_pin = pins.gpio0.into_mode::<hal::gpio::FunctionUart>(); let _tx_pin = pins.gpio0.into_mode::<hal::gpio::FunctionUart>();

View file

@ -82,13 +82,12 @@ fn main() -> ! {
&mut pac.RESETS, &mut pac.RESETS,
); );
let mut uart = hal::uart::UartPeripheral::<_, _>::enable( let mut uart = hal::uart::UartPeripheral::<_, _>::new(pac.UART0, &mut pac.RESETS)
pac.UART0, .enable(
&mut pac.RESETS, hal::uart::common_configs::_9600_8_N_1,
hal::uart::common_configs::_9600_8_N_1, clocks.peripheral_clock.into(),
clocks.peripheral_clock.into(), )
) .unwrap();
.unwrap();
// UART TX (characters sent from RP2040) on pin 1 (GPIO0) // UART TX (characters sent from RP2040) on pin 1 (GPIO0)
let _tx_pin = pins.gpio0.into_mode::<hal::gpio::FunctionUart>(); let _tx_pin = pins.gpio0.into_mode::<hal::gpio::FunctionUart>();

View file

@ -17,12 +17,12 @@
//! let mut clocks = init_clocks_and_plls(XOSC_CRYSTAL_FREQ, peripherals.XOSC, peripherals.CLOCKS, peripherals.PLL_SYS, peripherals.PLL_USB, &mut peripherals.RESETS, &mut watchdog).ok().unwrap(); //! let mut clocks = init_clocks_and_plls(XOSC_CRYSTAL_FREQ, peripherals.XOSC, peripherals.CLOCKS, peripherals.PLL_SYS, peripherals.PLL_USB, &mut peripherals.RESETS, &mut watchdog).ok().unwrap();
//! //!
//! // Need to perform clock init before using UART or it will freeze. //! // Need to perform clock init before using UART or it will freeze.
//! let uart = UartPeripheral::<_, _>::enable( //! let uart = UartPeripheral::<_, _>::new(peripherals.UART0, &mut peripherals.RESETS)
//! peripherals.UART0, //! .enable(
//! &mut peripherals.RESETS,
//! uart::common_configs::_9600_8_N_1, //! uart::common_configs::_9600_8_N_1,
//! clocks.peripheral_clock.into(), //! clocks.peripheral_clock.into(),
//! ).unwrap(); //! )
//! .unwrap();
//! //!
//! // Set up UART on GP0 and GP1 (Pico pins 1 and 2) //! // Set up UART on GP0 and GP1 (Pico pins 1 and 2)
//! let _tx_pin = pins.gpio0.into_mode::<FunctionUart>(); //! let _tx_pin = pins.gpio0.into_mode::<FunctionUart>();
@ -222,15 +222,25 @@ impl<S: State, D: UartDevice> UartPeripheral<S, D> {
} }
impl<D: UartDevice> UartPeripheral<Disabled, D> { impl<D: UartDevice> UartPeripheral<Disabled, D> {
/// Creates an UartPeripheral in Disabled state.
pub fn new(device: D, resets: &mut pac::RESETS) -> UartPeripheral<Disabled, D> {
device.reset_bring_up(resets);
UartPeripheral {
device,
config: common_configs::_9600_8_N_1, // placeholder
effective_baudrate: Baud(0),
_state: Disabled,
}
}
/// Enables the provided UART device with the given configuration. /// Enables the provided UART device with the given configuration.
pub fn enable( pub fn enable(
mut device: D, self,
resets: &mut pac::RESETS,
config: UartConfig, config: UartConfig,
frequency: Hertz, frequency: Hertz,
) -> Result<UartPeripheral<Enabled, D>, Error> { ) -> Result<UartPeripheral<Enabled, D>, Error> {
device.reset_bring_up(resets); let mut device = self.free();
let effective_baudrate = configure_baudrate(&mut device, &config.baudrate, &frequency)?; let effective_baudrate = configure_baudrate(&mut device, &config.baudrate, &frequency)?;
device.uartlcr_h.write(|w| { device.uartlcr_h.write(|w| {