2021-10-23 22:45:13 +11:00
|
|
|
//! # Pico I2C PIO Example
|
|
|
|
//!
|
|
|
|
//! Reads the temperature from an LM75B
|
|
|
|
//!
|
|
|
|
//! This read over I2C the temerature from an LM75B temperature sensor wired on pins 20 and 21
|
|
|
|
//! using the PIO peripheral as an I2C bus controller.
|
|
|
|
//! The pins used for the I2C can be remapped to any other pin available to the PIO0 peripheral.
|
|
|
|
//!
|
2022-04-18 20:49:41 +10:00
|
|
|
//! See the `Cargo.toml` file for Copyright and license details.
|
2021-10-23 22:45:13 +11:00
|
|
|
|
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
|
|
|
|
// The trait used by formatting macros like write! and writeln!
|
|
|
|
use core::fmt::Write as FmtWrite;
|
|
|
|
|
|
|
|
// The macro for our start-up function
|
2022-04-10 21:54:56 +10:00
|
|
|
use rp_pico::entry;
|
2021-10-23 22:45:13 +11:00
|
|
|
|
|
|
|
// I2C HAL traits & Types.
|
|
|
|
use embedded_hal::blocking::i2c::{Operation, Read, Transactional, Write};
|
|
|
|
|
|
|
|
// Time handling traits
|
|
|
|
use embedded_time::rate::*;
|
|
|
|
|
|
|
|
// Ensure we halt the program on panic (if we don't mention this crate it won't
|
|
|
|
// be linked)
|
|
|
|
use panic_halt as _;
|
|
|
|
|
|
|
|
// Pull in any important traits
|
2021-12-23 22:18:52 +11:00
|
|
|
use rp_pico::hal::prelude::*;
|
2021-10-23 22:45:13 +11:00
|
|
|
|
|
|
|
// A shorter alias for the Peripheral Access Crate, which provides low-level
|
|
|
|
// register access
|
2021-12-23 22:18:52 +11:00
|
|
|
use rp_pico::hal::pac;
|
2021-10-23 22:45:13 +11:00
|
|
|
|
|
|
|
// A shorter alias for the Hardware Abstraction Layer, which provides
|
|
|
|
// higher-level drivers.
|
2021-12-23 22:18:52 +11:00
|
|
|
use rp_pico::hal;
|
2021-10-23 22:45:13 +11:00
|
|
|
|
|
|
|
/// Prints the temperature received from the sensor
|
|
|
|
fn print_temperature(serial: &mut impl FmtWrite, temp: [u8; 2]) {
|
|
|
|
let temp_i16 = i16::from_be_bytes(temp) >> 5;
|
|
|
|
let temp_f32 = f32::from(temp_i16) * 0.125;
|
|
|
|
|
|
|
|
// Write formatted output but ignore any error.
|
|
|
|
let _ = writeln!(serial, "Temperature: {:0.2}°C", temp_f32);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Entry point to our bare-metal application.
|
|
|
|
///
|
|
|
|
/// The `#[entry]` macro ensures the Cortex-M start-up code calls this function
|
|
|
|
/// as soon as all global variables are initialised.
|
|
|
|
///
|
|
|
|
/// The function configures the RP2040 peripherals, reads the temperature from
|
|
|
|
/// the attached LM75B using PIO0.
|
|
|
|
#[entry]
|
|
|
|
fn main() -> ! {
|
|
|
|
// Grab our singleton objects
|
|
|
|
let mut pac = pac::Peripherals::take().unwrap();
|
|
|
|
|
|
|
|
// Set up the watchdog driver - needed by the clock setup code
|
2021-12-04 17:30:47 +11:00
|
|
|
let mut watchdog = hal::Watchdog::new(pac.WATCHDOG);
|
2021-10-23 22:45:13 +11:00
|
|
|
|
|
|
|
// Configure the clocks
|
|
|
|
//
|
|
|
|
// The default is to generate a 125 MHz system clock
|
|
|
|
let clocks = hal::clocks::init_clocks_and_plls(
|
2021-12-23 22:18:52 +11:00
|
|
|
rp_pico::XOSC_CRYSTAL_FREQ,
|
2021-10-23 22:45:13 +11:00
|
|
|
pac.XOSC,
|
|
|
|
pac.CLOCKS,
|
|
|
|
pac.PLL_SYS,
|
|
|
|
pac.PLL_USB,
|
|
|
|
&mut pac.RESETS,
|
|
|
|
&mut watchdog,
|
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// The single-cycle I/O block controls our GPIO pins
|
2021-12-04 15:52:46 +11:00
|
|
|
let sio = hal::Sio::new(pac.SIO);
|
2021-10-23 22:45:13 +11:00
|
|
|
|
|
|
|
// Set the pins up according to their function on this particular board
|
2021-12-23 22:18:52 +11:00
|
|
|
let pins = rp_pico::Pins::new(
|
2021-10-23 22:45:13 +11:00
|
|
|
pac.IO_BANK0,
|
|
|
|
pac.PADS_BANK0,
|
|
|
|
sio.gpio_bank0,
|
|
|
|
&mut pac.RESETS,
|
|
|
|
);
|
|
|
|
|
2021-11-27 23:46:29 +11:00
|
|
|
let uart_pins = (
|
|
|
|
// UART TX (characters sent from RP2040) on pin 1 (GPIO0)
|
|
|
|
pins.gpio0.into_mode::<hal::gpio::FunctionUart>(),
|
2021-12-27 06:29:14 +11:00
|
|
|
// UART RX (characters received by RP2040) on pin 2 (GPIO1)
|
2021-11-27 23:46:29 +11:00
|
|
|
pins.gpio1.into_mode::<hal::gpio::FunctionUart>(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS)
|
2021-12-03 08:33:21 +11:00
|
|
|
.enable(
|
|
|
|
hal::uart::common_configs::_115200_8_N_1,
|
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 `&`.
2022-07-23 05:31:10 +10:00
|
|
|
clocks.peripheral_clock.freq(),
|
2021-12-03 08:33:21 +11:00
|
|
|
)
|
|
|
|
.unwrap();
|
2021-10-23 22:45:13 +11:00
|
|
|
|
|
|
|
let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);
|
|
|
|
|
2021-12-05 23:17:34 +11:00
|
|
|
let mut i2c_pio = i2c_pio::I2C::new(
|
2021-10-23 22:45:13 +11:00
|
|
|
&mut pio,
|
|
|
|
pins.gpio20,
|
|
|
|
pins.gpio21,
|
|
|
|
sm0,
|
|
|
|
100_000.Hz(),
|
|
|
|
clocks.system_clock.freq(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut temp = [0; 2];
|
|
|
|
i2c_pio
|
|
|
|
.read(0x48u8, &mut temp)
|
|
|
|
.expect("Failed to read from the peripheral");
|
|
|
|
print_temperature(&mut uart, temp);
|
|
|
|
|
|
|
|
i2c_pio
|
|
|
|
.write(0x48u8, &[0])
|
|
|
|
.expect("Failed to write to the peripheral");
|
|
|
|
|
|
|
|
let mut temp = [0; 2];
|
|
|
|
i2c_pio
|
|
|
|
.read(0x48u8, &mut temp)
|
|
|
|
.expect("Failed to read from the peripheral");
|
|
|
|
print_temperature(&mut uart, temp);
|
|
|
|
|
|
|
|
let mut config = [0];
|
|
|
|
let mut thyst = [0; 2];
|
|
|
|
let mut tos = [0; 2];
|
|
|
|
let mut temp = [0; 2];
|
|
|
|
let mut operations = [
|
|
|
|
Operation::Write(&[1]),
|
|
|
|
Operation::Read(&mut config),
|
|
|
|
Operation::Write(&[2]),
|
|
|
|
Operation::Read(&mut thyst),
|
|
|
|
Operation::Write(&[3]),
|
|
|
|
Operation::Read(&mut tos),
|
|
|
|
Operation::Write(&[0]),
|
|
|
|
Operation::Read(&mut temp),
|
|
|
|
];
|
|
|
|
i2c_pio
|
|
|
|
.exec(0x48u8, &mut operations)
|
|
|
|
.expect("Failed to run all operations");
|
|
|
|
print_temperature(&mut uart, temp);
|
|
|
|
|
|
|
|
loop {
|
|
|
|
cortex_m::asm::nop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// End of file
|