rp2040_adt7310/
main.rs

1#![no_std]
2#![no_main]
3
4use rp2040_hal as hal;
5
6use hal::{
7    fugit::RateExtU32,
8    gpio::PinState,
9    spi::Spi,
10    uart::{DataBits, StopBits, UartConfig, UartPeripheral},
11    Clock,
12};
13
14use embedded_hal::{delay::DelayNs, digital::OutputPin, spi::MODE_3};
15use embedded_hal_0_2_x::blocking::spi::{Transfer as SpiTransfer, Write as SpiWrite};
16use embedded_io::Write;
17
18#[cfg(not(feature = "defmt"))]
19#[panic_handler]
20fn panic(_: &core::panic::PanicInfo) -> ! {
21    loop {}
22}
23
24#[cfg(feature = "defmt")]
25use {defmt_rtt as _, panic_probe as _};
26
27#[link_section = ".boot2"]
28#[no_mangle]
29#[used]
30static BOOT2_FIRMWARE: [u8; 256] = rp2040_boot2::BOOT_LOADER_GD25Q64CS;
31
32const XOSC_CRYSTAL_FREQ: u32 = 12_000_000;
33
34#[hal::entry]
35fn main() -> ! {
36    let mut pac = hal::pac::Peripherals::take().unwrap();
37    let _core = hal::pac::CorePeripherals::take().unwrap();
38
39    let mut watchdog = hal::watchdog::Watchdog::new(pac.WATCHDOG);
40
41    let clocks = hal::clocks::init_clocks_and_plls(
42        XOSC_CRYSTAL_FREQ,
43        pac.XOSC,
44        pac.CLOCKS,
45        pac.PLL_SYS,
46        pac.PLL_USB,
47        &mut pac.RESETS,
48        &mut watchdog,
49    )
50    .unwrap();
51
52    let mut timer = hal::Timer::new(pac.TIMER, &mut pac.RESETS, &clocks);
53
54    let sio = hal::Sio::new(pac.SIO);
55    let pins = hal::gpio::Pins::new(
56        pac.IO_BANK0,
57        pac.PADS_BANK0,
58        sio.gpio_bank0,
59        &mut pac.RESETS,
60    );
61
62    let pins_uart = (pins.gpio0.into_function(), pins.gpio1.into_function());
63    let mut uart = UartPeripheral::new(pac.UART0, pins_uart, &mut pac.RESETS)
64        .enable(
65            UartConfig::new(115_200.Hz(), DataBits::Eight, None, StopBits::One),
66            clocks.peripheral_clock.freq(),
67        )
68        .unwrap();
69
70    let mut adt7310_csn = pins.gpio6.into_push_pull_output_in_state(PinState::High);
71
72    let pins_spi = (
73        pins.gpio19.into_function(),
74        pins.gpio20.into_function(),
75        pins.gpio18.into_function(),
76    );
77    let mut spi = Spi::<_, _, _, 8>::new(pac.SPI0, pins_spi).init(
78        &mut pac.RESETS,
79        clocks.peripheral_clock.freq(),
80        5.MHz(),
81        MODE_3,
82    );
83
84    // serial interface reset
85    {
86        adt7310_csn.set_low().unwrap();
87        spi.write(&[0xff_u8; 4]).unwrap();
88        adt7310_csn.set_high().unwrap();
89        timer.delay_us(1);
90    }
91
92    // レジスタ読み出し
93    //                    ┌───────── 1: read, 0: write
94    //                    │   ┌───── address
95    //                    │   │ ┌─── 1: continuos read (temperature value register only)
96    //    spi.write(&[0b0_0_001_0_00, ...]).unwrap();
97    //
98    // let mut buf = [0b0_1_010_0_00, 0xff, ...];
99    // spi.transfer(buf.as_mut_slice()).unwrap();
100
101    loop {
102        adt7310_csn.set_low().unwrap();
103
104        #[allow(clippy::unusual_byte_groupings)]
105        //                  ┌───── configuration register
106        //                  │         ┌───── Resolution (16-bit)
107        //                  │         │  ┌───── Operation mode (One shot)
108        spi.write(&[0b0_0_001_0_00, 0b1_01_0_0_0_00]).unwrap();
109
110        timer.delay_ms(240);
111
112        #[allow(clippy::unusual_byte_groupings)]
113        //                     ┌───── temperature value register
114        let mut buf = [0b0_1_010_0_00, 0, 0];
115        spi.transfer(buf.as_mut_slice()).unwrap();
116
117        let temp = i16::from_be_bytes([buf[1], buf[2]]);
118        let temp_int = temp / 128;
119        let temp_frac = 78125_u32 * (temp % 128).unsigned_abs() as u32;
120        write!(uart, "\r\ntemp = {temp_int:3}.{temp_frac:07} ({temp:#06x})").unwrap();
121
122        adt7310_csn.set_high().unwrap();
123
124        timer.delay_ms(760);
125    }
126}