mirror of
https://github.com/italicsjenga/rp-hal-boards.git
synced 2024-12-23 20:51:31 +11:00
Implement conversion from Clock to Hertz using reference
Implementing `impl From<SystemClock> for Hertz` is a footgun, as SystemClock is not Copy, so the automatic conversion consumes the owned clock. This is visible in the example i2c.rs: ``` let mut i2c = hal::I2C::i2c1( pac.I2C1, sda_pin, scl_pin, // Try `not_an_scl_pin` here 400.kHz(), &mut pac.RESETS, clocks.peripheral_clock, ); ``` If the user wants to use both `i2c0` and `i2c1` at the same time, copying from this example won't work: ``` error[E0382]: use of moved value: `clocks.peripheral_clock` --> rp2040-hal/examples/i2c.rs:106:9 | 97 | clocks.peripheral_clock, | ----------------------- value moved here ... 106 | clocks.peripheral_clock, | ^^^^^^^^^^^^^^^^^^^^^^^ value used here after move | = note: move occurs because `clocks.peripheral_clock` has type `PeripheralClock`, which does not implement the `Copy` trait ``` As getting the frequency from a clock doesn't really need ownership, changing it to `impl From<&SystemClock> for Hertz` is both more logical and provides better usability. This is, however, a breaking change: Code relying on this trait implementation needs to be changed by adding a `&`.
This commit is contained in:
parent
69c2dd2c2b
commit
f8984a9eac
|
@ -134,7 +134,7 @@ fn main() -> ! {
|
|||
scl_pin,
|
||||
400.kHz(),
|
||||
&mut pac.RESETS,
|
||||
clocks.peripheral_clock,
|
||||
&clocks.peripheral_clock,
|
||||
);
|
||||
|
||||
// Create the I²C display interface:
|
||||
|
|
|
@ -98,7 +98,7 @@ fn main() -> ! {
|
|||
let mut uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS)
|
||||
.enable(
|
||||
hal::uart::common_configs::_115200_8_N_1,
|
||||
clocks.peripheral_clock.into(),
|
||||
clocks.peripheral_clock.freq(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
|
|
|
@ -138,7 +138,7 @@ fn main() -> ! {
|
|||
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(),
|
||||
clocks.peripheral_clock.freq(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
|
|
|
@ -122,7 +122,7 @@ fn main() -> ! {
|
|||
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(),
|
||||
clocks.peripheral_clock.freq(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
|
|
|
@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
### Changed
|
||||
|
||||
- Update embedded-hal alpha support to version 1.0.0-alpha.8
|
||||
- Implement `From<&SomeClock> for Hertz` instead of `From<SomeClock> for Hertz`
|
||||
for the clocks in `rp2040_hal::clocks`.
|
||||
|
||||
## [0.5.0] - 2022-06-13
|
||||
|
||||
|
|
|
@ -94,7 +94,7 @@ fn main() -> ! {
|
|||
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(),
|
||||
clocks.peripheral_clock.freq(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
|
|
|
@ -89,7 +89,7 @@ fn main() -> ! {
|
|||
scl_pin, // Try `not_an_scl_pin` here
|
||||
400.kHz(),
|
||||
&mut pac.RESETS,
|
||||
clocks.peripheral_clock,
|
||||
&clocks.peripheral_clock,
|
||||
);
|
||||
|
||||
// Write three bytes to the I²C device with 7-bit address 0x2C
|
||||
|
|
|
@ -25,6 +25,7 @@ use hal::pac;
|
|||
|
||||
// Some traits we need
|
||||
use core::fmt::Write;
|
||||
use hal::Clock;
|
||||
|
||||
/// The linker will place this boot block at the start of our program image. We
|
||||
/// need this to help the ROM bootloader get our code up and running.
|
||||
|
@ -89,7 +90,7 @@ fn main() -> ! {
|
|||
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(),
|
||||
clocks.peripheral_clock.freq(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
|
|
|
@ -417,9 +417,9 @@ macro_rules! base_clock {
|
|||
|
||||
impl Sealed for $name {}
|
||||
|
||||
impl From<$name> for Hertz
|
||||
impl From<&$name> for Hertz
|
||||
{
|
||||
fn from(value: $name) -> Hertz {
|
||||
fn from(value: &$name) -> Hertz {
|
||||
value.frequency
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
//!
|
||||
//! See [examples/uart.rs](https://github.com/rp-rs/rp-hal/tree/main/rp2040-hal/examples/uart.rs) for a more complete example
|
||||
//! ```no_run
|
||||
//! use rp2040_hal::{clocks::init_clocks_and_plls, gpio::{Pins, FunctionUart}, pac, sio::Sio, uart::{self, UartPeripheral}, watchdog::Watchdog};
|
||||
//! use rp2040_hal::{Clock, clocks::init_clocks_and_plls, gpio::{Pins, FunctionUart}, pac, sio::Sio, uart::{self, UartPeripheral}, watchdog::Watchdog};
|
||||
//!
|
||||
//! const XOSC_CRYSTAL_FREQ: u32 = 12_000_000; // Typically found in BSP crates
|
||||
//!
|
||||
|
@ -25,7 +25,7 @@
|
|||
//! let uart = UartPeripheral::new(peripherals.UART0, pins, &mut peripherals.RESETS)
|
||||
//! .enable(
|
||||
//! uart::common_configs::_9600_8_N_1,
|
||||
//! clocks.peripheral_clock.into(),
|
||||
//! clocks.peripheral_clock.freq(),
|
||||
//! ).unwrap();
|
||||
//!
|
||||
//! uart.write_full_blocking(b"Hello World!\r\n");
|
||||
|
|
Loading…
Reference in a new issue