made all types, broke out some functionality.
This commit is contained in:
		
							parent
							
								
									dda09ca009
								
							
						
					
					
						commit
						eb0bda3300
					
				
					 2 changed files with 110 additions and 55 deletions
				
			
		| 
						 | 
					@ -8,6 +8,7 @@ license = "MIT OR Apache-2.0"
 | 
				
			||||||
cortex-m = {version = "0.7"}
 | 
					cortex-m = {version = "0.7"}
 | 
				
			||||||
cortex-m-rt = "0.7"
 | 
					cortex-m-rt = "0.7"
 | 
				
			||||||
embedded-hal = { version = "0.2.5", features = ["unproven"] }
 | 
					embedded-hal = { version = "0.2.5", features = ["unproven"] }
 | 
				
			||||||
 | 
					embedded-alloc = "0.5.1"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
defmt = "0.3"
 | 
					defmt = "0.3"
 | 
				
			||||||
defmt-rtt = "0.4"
 | 
					defmt-rtt = "0.4"
 | 
				
			||||||
| 
						 | 
					@ -17,7 +18,6 @@ panic-probe = { version = "0.3", features = ["print-defmt"] }
 | 
				
			||||||
rp-pico = "0.8"
 | 
					rp-pico = "0.8"
 | 
				
			||||||
max31855 = "0.1.0"
 | 
					max31855 = "0.1.0"
 | 
				
			||||||
hd44780-driver = "0.4.0"
 | 
					hd44780-driver = "0.4.0"
 | 
				
			||||||
embedded-alloc = "0.5.1"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
# cargo build/run
 | 
					# cargo build/run
 | 
				
			||||||
[profile.dev]
 | 
					[profile.dev]
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										163
									
								
								src/main.rs
									
										
									
									
									
								
							
							
						
						
									
										163
									
								
								src/main.rs
									
										
									
									
									
								
							| 
						 | 
					@ -3,32 +3,35 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
extern crate alloc;
 | 
					extern crate alloc;
 | 
				
			||||||
use alloc::format;
 | 
					use alloc::format;
 | 
				
			||||||
use bsp::{
 | 
					 | 
				
			||||||
    entry,
 | 
					 | 
				
			||||||
    hal::{
 | 
					 | 
				
			||||||
        gpio,
 | 
					 | 
				
			||||||
        uart::{UartConfig, UartPeripheral},
 | 
					 | 
				
			||||||
    },
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
use core::fmt::Write;
 | 
					use core::fmt::Write;
 | 
				
			||||||
use cortex_m::delay::Delay;
 | 
					use cortex_m::delay::Delay;
 | 
				
			||||||
use defmt_rtt as _;
 | 
					use defmt_rtt as _;
 | 
				
			||||||
use embedded_alloc::Heap;
 | 
					use embedded_alloc::Heap;
 | 
				
			||||||
use embedded_hal::digital::v2::OutputPin;
 | 
					use embedded_hal::{digital::v2::OutputPin, spi::MODE_0};
 | 
				
			||||||
use embedded_hal::spi::MODE_0;
 | 
					use hd44780::{bus::FourBitBus, HD44780};
 | 
				
			||||||
use panic_probe as _;
 | 
					use panic_probe as _;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Provide an alias for our BSP so we can switch targets quickly.
 | 
					use rp_pico::{
 | 
				
			||||||
// Uncomment the BSP you included in Cargo.toml, the rest of the code does not need to change.
 | 
					    entry,
 | 
				
			||||||
use rp_pico as bsp;
 | 
					    hal::{
 | 
				
			||||||
 | 
					        clocks::{init_clocks_and_plls, Clock},
 | 
				
			||||||
use bsp::hal::{
 | 
					        fugit::RateExtU32,
 | 
				
			||||||
    clocks::{init_clocks_and_plls, Clock},
 | 
					        gpio::{
 | 
				
			||||||
    fugit::RateExtU32,
 | 
					            self,
 | 
				
			||||||
    pac,
 | 
					            bank0::{
 | 
				
			||||||
    sio::Sio,
 | 
					                Gpio0, Gpio1, Gpio14, Gpio16, Gpio17, Gpio18, Gpio19, Gpio2, Gpio20, Gpio21,
 | 
				
			||||||
    spi::Spi,
 | 
					                Gpio25, Gpio3, Gpio4, Gpio5,
 | 
				
			||||||
    watchdog::Watchdog,
 | 
					            },
 | 
				
			||||||
 | 
					            FunctionSio, FunctionSpi, FunctionUart, Pin, PullDown, SioOutput,
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        pac,
 | 
				
			||||||
 | 
					        sio::Sio,
 | 
				
			||||||
 | 
					        spi::{self, Spi},
 | 
				
			||||||
 | 
					        uart::{self, UartConfig, UartPeripheral},
 | 
				
			||||||
 | 
					        watchdog::Watchdog,
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    pac::{SPI0, UART0},
 | 
				
			||||||
 | 
					    Pins,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use hd44780_driver as hd44780;
 | 
					use hd44780_driver as hd44780;
 | 
				
			||||||
| 
						 | 
					@ -47,6 +50,53 @@ fn write_lcd<T: hd44780_driver::bus::DataBus>(
 | 
				
			||||||
    lcd.write_str(string, delay).unwrap();
 | 
					    lcd.write_str(string, delay).unwrap();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Uart = UartPeripheral<
 | 
				
			||||||
 | 
					    uart::Enabled,
 | 
				
			||||||
 | 
					    UART0,
 | 
				
			||||||
 | 
					    (
 | 
				
			||||||
 | 
					        Pin<Gpio0, FunctionUart, PullDown>,
 | 
				
			||||||
 | 
					        Pin<Gpio1, FunctionUart, PullDown>,
 | 
				
			||||||
 | 
					    ),
 | 
				
			||||||
 | 
					>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Led = Pin<Gpio25, FunctionSio<SioOutput>, PullDown>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type ExternalLed = Pin<Gpio14, FunctionSio<SioOutput>, PullDown>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type ThermocoupleSpi = Spi<
 | 
				
			||||||
 | 
					    spi::Enabled,
 | 
				
			||||||
 | 
					    SPI0,
 | 
				
			||||||
 | 
					    (
 | 
				
			||||||
 | 
					        Pin<Gpio3, FunctionSpi, PullDown>,
 | 
				
			||||||
 | 
					        Pin<Gpio4, FunctionSpi, PullDown>,
 | 
				
			||||||
 | 
					        Pin<Gpio2, FunctionSpi, PullDown>,
 | 
				
			||||||
 | 
					    ),
 | 
				
			||||||
 | 
					>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type Lcd = HD44780<
 | 
				
			||||||
 | 
					    FourBitBus<
 | 
				
			||||||
 | 
					        Pin<Gpio16, FunctionSio<SioOutput>, PullDown>,
 | 
				
			||||||
 | 
					        Pin<Gpio17, FunctionSio<SioOutput>, PullDown>,
 | 
				
			||||||
 | 
					        Pin<Gpio18, FunctionSio<SioOutput>, PullDown>,
 | 
				
			||||||
 | 
					        Pin<Gpio19, FunctionSio<SioOutput>, PullDown>,
 | 
				
			||||||
 | 
					        Pin<Gpio20, FunctionSio<SioOutput>, PullDown>,
 | 
				
			||||||
 | 
					        Pin<Gpio21, FunctionSio<SioOutput>, PullDown>,
 | 
				
			||||||
 | 
					    >,
 | 
				
			||||||
 | 
					>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct Thermocouple {
 | 
				
			||||||
 | 
					    cs: Pin<Gpio5, FunctionSio<SioOutput>, PullDown>,
 | 
				
			||||||
 | 
					    spi: ThermocoupleSpi,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl Thermocouple {
 | 
				
			||||||
 | 
					    fn read_temp(
 | 
				
			||||||
 | 
					        &mut self,
 | 
				
			||||||
 | 
					    ) -> Result<f32, max31855::Error<core::convert::Infallible, core::convert::Infallible>> {
 | 
				
			||||||
 | 
					        self.spi.read_thermocouple(&mut self.cs, Unit::Celsius)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[entry]
 | 
					#[entry]
 | 
				
			||||||
fn main() -> ! {
 | 
					fn main() -> ! {
 | 
				
			||||||
    let mut pac = pac::Peripherals::take().unwrap();
 | 
					    let mut pac = pac::Peripherals::take().unwrap();
 | 
				
			||||||
| 
						 | 
					@ -78,52 +128,57 @@ fn main() -> ! {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz());
 | 
					    let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let pins = bsp::Pins::new(
 | 
					    let pins = Pins::new(
 | 
				
			||||||
        pac.IO_BANK0,
 | 
					        pac.IO_BANK0,
 | 
				
			||||||
        pac.PADS_BANK0,
 | 
					        pac.PADS_BANK0,
 | 
				
			||||||
        sio.gpio_bank0,
 | 
					        sio.gpio_bank0,
 | 
				
			||||||
        &mut pac.RESETS,
 | 
					        &mut pac.RESETS,
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let uart_pins = (pins.gpio0.into_function(), pins.gpio1.into_function());
 | 
					    let mut uart: Uart = UartPeripheral::new(
 | 
				
			||||||
    let mut uart = UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS)
 | 
					        pac.UART0,
 | 
				
			||||||
        .enable(
 | 
					        (pins.gpio0.into_function(), pins.gpio1.into_function()),
 | 
				
			||||||
            UartConfig::new(
 | 
					        &mut pac.RESETS,
 | 
				
			||||||
                9600u32.Hz(),
 | 
					    )
 | 
				
			||||||
                bsp::hal::uart::DataBits::Eight,
 | 
					    .enable(
 | 
				
			||||||
                None,
 | 
					        UartConfig::new(
 | 
				
			||||||
                bsp::hal::uart::StopBits::One,
 | 
					            9600u32.Hz(),
 | 
				
			||||||
            ),
 | 
					            rp_pico::hal::uart::DataBits::Eight,
 | 
				
			||||||
            clocks.peripheral_clock.freq(),
 | 
					            None,
 | 
				
			||||||
        )
 | 
					            rp_pico::hal::uart::StopBits::One,
 | 
				
			||||||
        .unwrap();
 | 
					        ),
 | 
				
			||||||
 | 
					        clocks.peripheral_clock.freq(),
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					    .unwrap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    defmt::info!("Program start");
 | 
					    defmt::info!("Program start");
 | 
				
			||||||
    writeln!(uart, "Program start\r",).unwrap();
 | 
					    writeln!(uart, "Program start\r",).unwrap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let mut led_pin = pins.led.into_push_pull_output();
 | 
					    let mut led_pin: Led = pins.led.into_push_pull_output();
 | 
				
			||||||
    let mut external_led_pin = pins.gpio14.into_push_pull_output();
 | 
					    let mut external_led_pin: ExternalLed = pins.gpio14.into_push_pull_output();
 | 
				
			||||||
    //clk
 | 
					
 | 
				
			||||||
    let thermometer_spi_sck = pins.gpio2.into_function::<gpio::FunctionSpi>();
 | 
					 | 
				
			||||||
    //MOSI, I think unused?
 | 
					 | 
				
			||||||
    let thermometer_spi_tx = pins.gpio3.into_function::<gpio::FunctionSpi>();
 | 
					 | 
				
			||||||
    //do, or MISO.
 | 
					 | 
				
			||||||
    let thermometer_spi_rx = pins.gpio4.into_function::<gpio::FunctionSpi>();
 | 
					 | 
				
			||||||
    //cs
 | 
					    //cs
 | 
				
			||||||
    let mut thermometer_spi_csn = pins
 | 
					 | 
				
			||||||
        .gpio5
 | 
					 | 
				
			||||||
        .into_push_pull_output_in_state(gpio::PinState::Low);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    let thermometer_spi_device = pac.SPI0;
 | 
					    let thermometer_spi_device = pac.SPI0;
 | 
				
			||||||
    let spi_pin_layout = (thermometer_spi_tx, thermometer_spi_rx, thermometer_spi_sck);
 | 
					    let mut thermocouple = Thermocouple {
 | 
				
			||||||
    let mut spi = Spi::<_, _, _, 8>::new(thermometer_spi_device, spi_pin_layout).init(
 | 
					        cs: pins
 | 
				
			||||||
        &mut pac.RESETS,
 | 
					            .gpio5
 | 
				
			||||||
        125_000_000u32.Hz(),
 | 
					            .into_push_pull_output_in_state(gpio::PinState::Low),
 | 
				
			||||||
        4u32.MHz(),
 | 
					 | 
				
			||||||
        MODE_0,
 | 
					 | 
				
			||||||
    );
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let mut lcd = hd44780::HD44780::new_4bit(
 | 
					        spi: Spi::<_, _, _, 8>::new(
 | 
				
			||||||
 | 
					            thermometer_spi_device,
 | 
				
			||||||
 | 
					            (
 | 
				
			||||||
 | 
					                //mosi
 | 
				
			||||||
 | 
					                pins.gpio3.into_function::<gpio::FunctionSpi>(),
 | 
				
			||||||
 | 
					                //miso/do
 | 
				
			||||||
 | 
					                pins.gpio4.into_function::<gpio::FunctionSpi>(),
 | 
				
			||||||
 | 
					                //clk
 | 
				
			||||||
 | 
					                pins.gpio2.into_function::<gpio::FunctionSpi>(),
 | 
				
			||||||
 | 
					            ),
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					        .init(&mut pac.RESETS, 125_000_000u32.Hz(), 4u32.MHz(), MODE_0),
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let mut lcd: Lcd = hd44780::HD44780::new_4bit(
 | 
				
			||||||
        pins.gpio16.into_push_pull_output(), //rs
 | 
					        pins.gpio16.into_push_pull_output(), //rs
 | 
				
			||||||
        pins.gpio17.into_push_pull_output(), //enable
 | 
					        pins.gpio17.into_push_pull_output(), //enable
 | 
				
			||||||
        pins.gpio18.into_push_pull_output(), //d4
 | 
					        pins.gpio18.into_push_pull_output(), //d4
 | 
				
			||||||
| 
						 | 
					@ -142,7 +197,7 @@ fn main() -> ! {
 | 
				
			||||||
        led_pin.set_low().unwrap();
 | 
					        led_pin.set_low().unwrap();
 | 
				
			||||||
        external_led_pin.set_high().unwrap();
 | 
					        external_led_pin.set_high().unwrap();
 | 
				
			||||||
        delay.delay_ms(500);
 | 
					        delay.delay_ms(500);
 | 
				
			||||||
        match spi.read_thermocouple(&mut thermometer_spi_csn, Unit::Celsius) {
 | 
					        match thermocouple.read_temp() {
 | 
				
			||||||
            Ok(v) => {
 | 
					            Ok(v) => {
 | 
				
			||||||
                writeln!(uart, "Current: {} \r", v).unwrap();
 | 
					                writeln!(uart, "Current: {} \r", v).unwrap();
 | 
				
			||||||
                write_lcd(&mut lcd, &mut delay, &format!("{:02.2} C", v))
 | 
					                write_lcd(&mut lcd, &mut delay, &format!("{:02.2} C", v))
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue