Created
December 24, 2021 05:50
-
-
Save 9names/0a670e374097f36bff704733d25afc83 to your computer and use it in GitHub Desktop.
Test code for review https://github.com/rp-rs/rp-hal/pull/210
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![no_std] | |
#![no_main] | |
use core::fmt::Write; | |
use cortex_m_rt::entry; | |
use defmt::*; | |
use defmt_rtt as _; | |
use embedded_time::fixed_point::FixedPoint; | |
use hal::pac; | |
use panic_probe as _; | |
use rp2040_hal as hal; | |
use rp2040_hal::clocks::Clock; | |
#[link_section = ".boot2"] | |
#[used] | |
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080; | |
const XTAL_FREQ_HZ: u32 = 12_000_000u32; | |
fn clear_uart_errors() { | |
unsafe { | |
let uart = pac::UART0::ptr(); | |
(*uart).uartdr.write(|w| w.bits(1)); | |
} | |
} | |
#[entry] | |
fn main() -> ! { | |
let mut pac = pac::Peripherals::take().unwrap(); | |
let core = pac::CorePeripherals::take().unwrap(); | |
let mut watchdog = hal::Watchdog::new(pac.WATCHDOG); | |
let clocks = hal::clocks::init_clocks_and_plls( | |
XTAL_FREQ_HZ, | |
pac.XOSC, | |
pac.CLOCKS, | |
pac.PLL_SYS, | |
pac.PLL_USB, | |
&mut pac.RESETS, | |
&mut watchdog, | |
) | |
.ok() | |
.unwrap(); | |
let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().integer()); | |
let sio = hal::Sio::new(pac.SIO); | |
let pins = hal::gpio::Pins::new( | |
pac.IO_BANK0, | |
pac.PADS_BANK0, | |
sio.gpio_bank0, | |
&mut pac.RESETS, | |
); | |
let uart_pins = ( | |
// UART TX (characters sent from RP2040) on pin 1 (GPIO0) | |
pins.gpio0.into_mode::<hal::gpio::FunctionUart>(), | |
// UART RX (characters reveived by RP2040) on pin 2 (GPIO1) | |
pins.gpio1.into_mode::<hal::gpio::FunctionUart>(), | |
// UART CTS (Clear to send input pin, high when sending is okay) on pin 4 (GPIO2) | |
// This pin is expected to be observed by the transmitter | |
pins.gpio2.into_mode::<hal::gpio::FunctionUart>(), | |
// UART RTS (Request to send output pin, high when sending is okay) on pin 5 (GPIO3) | |
// This pin is expected to be driven by the receiver | |
pins.gpio3.into_mode::<hal::gpio::FunctionUart>(), | |
); | |
let mut uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS) | |
.enable( | |
hal::uart::common_configs::_9600_8_N_1, | |
clocks.peripheral_clock.into(), | |
) | |
.unwrap(); | |
println!("UART example"); | |
uart.write_full_blocking(b"UART example\r\n"); | |
let mut value = 0u32; | |
let mut read_buffer: [u8; 40] = [0; 40]; | |
const DELAY_READ_COUNT: u32 = 1; | |
let mut delay_read = DELAY_READ_COUNT; | |
loop { | |
clear_uart_errors(); | |
println!("Writing {}", value); | |
let write = writeln!(uart, "value: {:02}\r", value); | |
if let Err(_err) = write { | |
println!("write error"); | |
} | |
clear_uart_errors(); | |
println!("Finished writing"); | |
delay_read -= 1; | |
if delay_read == 0 { | |
loop { | |
println!("Start read loop"); | |
let read = uart.read_raw(&mut read_buffer); | |
match read { | |
Ok(bytes) => { | |
if bytes > 0 { | |
println!("Bytes received: {}", bytes); | |
let read_slice = &read_buffer[0..(bytes - 1)]; | |
println!("Received: {:a}", read_slice); | |
} else { | |
println!("No bytes to read"); | |
break; | |
} | |
} | |
Err(e) => { | |
match e { | |
nb::Error::Other(o) => match o.err_type { | |
hal::uart::ReadErrorType::Overrun => { | |
println!("Other error: Overrun"); | |
clear_uart_errors(); | |
} | |
hal::uart::ReadErrorType::Break => println!("Other error: Break"), | |
hal::uart::ReadErrorType::Parity => println!("Other error: Parity"), | |
hal::uart::ReadErrorType::Framing => { | |
println!("Other error: Framing"); | |
clear_uart_errors(); | |
} | |
}, | |
nb::Error::WouldBlock => println!("Would block"), | |
} | |
break; | |
} | |
} | |
} | |
delay_read = DELAY_READ_COUNT; | |
} | |
delay.delay_ms(100); | |
value += 1 | |
} | |
} | |
// End of file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uart1_pins_split-join_flow-control.rs: configuring pins as TX/RX/CTS/RTS, split and join functions, UART HW flow control | |
uart0_split_flow-control.rs: test UART HW flow control on UART0 but uses split TX/RX for all operations | |
flow_control_debugging.rs: attempting to work out if we could unblock UART after errors (unsuccessfully) | |
I also tested without the CTS/RTS pins enabled, and that worked fine (HW flow control is currently always enabled) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![no_std] | |
#![no_main] | |
use cortex_m_rt::entry; | |
use panic_probe as _; | |
use defmt::*; | |
use defmt_rtt as _; | |
use rp2040_hal as hal; | |
use hal::pac; | |
use core::fmt::Write; | |
use embedded_time::fixed_point::FixedPoint; | |
use rp2040_hal::clocks::Clock; | |
#[link_section = ".boot2"] | |
#[used] | |
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080; | |
const XTAL_FREQ_HZ: u32 = 12_000_000u32; | |
#[entry] | |
fn main() -> ! { | |
let mut pac = pac::Peripherals::take().unwrap(); | |
let core = pac::CorePeripherals::take().unwrap(); | |
let mut watchdog = hal::Watchdog::new(pac.WATCHDOG); | |
let clocks = hal::clocks::init_clocks_and_plls( | |
XTAL_FREQ_HZ, | |
pac.XOSC, | |
pac.CLOCKS, | |
pac.PLL_SYS, | |
pac.PLL_USB, | |
&mut pac.RESETS, | |
&mut watchdog, | |
) | |
.ok() | |
.unwrap(); | |
let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().integer()); | |
let sio = hal::Sio::new(pac.SIO); | |
let pins = hal::gpio::Pins::new( | |
pac.IO_BANK0, | |
pac.PADS_BANK0, | |
sio.gpio_bank0, | |
&mut pac.RESETS, | |
); | |
let uart_pins = ( | |
// UART TX (characters sent from RP2040) on pin 1 (GPIO0) | |
pins.gpio0.into_mode::<hal::gpio::FunctionUart>(), | |
// UART RX (characters reveived by RP2040) on pin 2 (GPIO1) | |
pins.gpio1.into_mode::<hal::gpio::FunctionUart>(), | |
// UART CTS (Clear to send input pin, high when sending is okay) on pin 4 (GPIO2) | |
// This pin is expected to be observed by the transmitter | |
pins.gpio2.into_mode::<hal::gpio::FunctionUart>(), | |
// UART RTS (Request to send output pin, high when sending is okay) on pin 5 (GPIO3) | |
// This pin is expected to be driven by the receiver | |
pins.gpio3.into_mode::<hal::gpio::FunctionUart>(), | |
); | |
let uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS) | |
.enable( | |
hal::uart::common_configs::_9600_8_N_1, | |
clocks.peripheral_clock.into(), | |
) | |
.unwrap(); | |
println!("UART example"); | |
uart.write_full_blocking(b"UART example\r\n"); | |
let mut value = 0u32; | |
let mut read_buffer: [u8; 40] = [0; 40]; | |
let (rx, mut tx) = uart.split(); | |
loop { | |
println!("Writing {}", value); | |
writeln!(tx, "value: {:02}\r", value).unwrap(); | |
let read = rx.read_raw(&mut read_buffer); | |
match read { | |
Ok(bytes) => { | |
if bytes > 0 { | |
println!("Bytes received: {}", bytes); | |
let read_slice = &read_buffer[0..(bytes - 1)]; | |
println!("Received: {:a}", read_slice); | |
} | |
} | |
Err(_) => println!("Read error"), | |
} | |
delay.delay_ms(100); | |
value += 1 | |
} | |
} | |
// End of file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![no_std] | |
#![no_main] | |
use core::fmt::Write; | |
use cortex_m_rt::entry; | |
use defmt::*; | |
use defmt_rtt as _; | |
use embedded_time::fixed_point::FixedPoint; | |
use hal::pac; | |
use panic_probe as _; | |
use rp2040_hal as hal; | |
use rp2040_hal::clocks::Clock; | |
#[link_section = ".boot2"] | |
#[used] | |
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080; | |
const XTAL_FREQ_HZ: u32 = 12_000_000u32; | |
#[entry] | |
fn main() -> ! { | |
let mut pac = pac::Peripherals::take().unwrap(); | |
let core = pac::CorePeripherals::take().unwrap(); | |
let mut watchdog = hal::Watchdog::new(pac.WATCHDOG); | |
let clocks = hal::clocks::init_clocks_and_plls( | |
XTAL_FREQ_HZ, | |
pac.XOSC, | |
pac.CLOCKS, | |
pac.PLL_SYS, | |
pac.PLL_USB, | |
&mut pac.RESETS, | |
&mut watchdog, | |
) | |
.ok() | |
.unwrap(); | |
let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().integer()); | |
let sio = hal::Sio::new(pac.SIO); | |
let pins = hal::gpio::Pins::new( | |
pac.IO_BANK0, | |
pac.PADS_BANK0, | |
sio.gpio_bank0, | |
&mut pac.RESETS, | |
); | |
let uart_pins = ( | |
// UART TX (characters sent from RP2040) on pin 1 (GPIO0) | |
pins.gpio4.into_mode::<hal::gpio::FunctionUart>(), | |
// UART RX (characters reveived by RP2040) on pin 2 (GPIO1) | |
pins.gpio5.into_mode::<hal::gpio::FunctionUart>(), | |
// UART CTS (Clear to send input pin, high when sending is okay) on pin 4 (GPIO2) | |
// This pin is expected to be observed by the transmitter | |
pins.gpio6.into_mode::<hal::gpio::FunctionUart>(), | |
// UART RTS (Request to send output pin, high when sending is okay) on pin 5 (GPIO3) | |
// This pin is expected to be driven by the receiver | |
pins.gpio7.into_mode::<hal::gpio::FunctionUart>(), | |
); | |
let uart = hal::uart::UartPeripheral::new(pac.UART1, uart_pins, &mut pac.RESETS) | |
.enable( | |
hal::uart::common_configs::_9600_8_N_1, | |
clocks.peripheral_clock.into(), | |
) | |
.unwrap(); | |
println!("UART example"); | |
uart.write_full_blocking(b"UART example\r\n"); | |
let (rx, tx) = uart.split(); | |
let mut uart = hal::uart::UartPeripheral::join(rx, tx); | |
let mut value = 0u32; | |
let mut read_buffer: [u8; 40] = [0; 40]; | |
loop { | |
println!("Writing {}", value); | |
writeln!(uart, "value: {:02}\r", value).unwrap(); | |
let read = uart.read_raw(&mut read_buffer); | |
match read { | |
Ok(bytes) => { | |
if bytes > 0 { | |
println!("Bytes received: {}", bytes); | |
let read_slice = &read_buffer[0..(bytes - 1)]; | |
println!("Received: {:a}", read_slice); | |
} | |
} | |
Err(_) => println!("Read error"), | |
} | |
delay.delay_ms(100); | |
value += 1 | |
} | |
} | |
// End of file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment