Compare commits
No commits in common. "910010ba22437efe5777e95ddba0793c96b4d6d7" and "dda09ca009e8a6d9db86e78ddeb76221b6acd3ca" have entirely different histories.
910010ba22
...
dda09ca009
|
@ -7,10 +7,7 @@ license = "MIT OR Apache-2.0"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
cortex-m = {version = "0.7"}
|
cortex-m = {version = "0.7"}
|
||||||
cortex-m-rt = "0.7"
|
cortex-m-rt = "0.7"
|
||||||
cortex-m-rtic = "1.1.4"
|
|
||||||
embedded-hal = { version = "0.2.5", features = ["unproven"] }
|
embedded-hal = { version = "0.2.5", features = ["unproven"] }
|
||||||
embedded-alloc = "0.5.1"
|
|
||||||
rp2040-monotonic = "1.3.0"
|
|
||||||
|
|
||||||
defmt = "0.3"
|
defmt = "0.3"
|
||||||
defmt-rtt = "0.4"
|
defmt-rtt = "0.4"
|
||||||
|
@ -20,6 +17,7 @@ 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]
|
||||||
|
|
260
src/main.rs
260
src/main.rs
|
@ -1,45 +1,34 @@
|
||||||
#![no_std]
|
#![no_std]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
#[rtic::app(
|
|
||||||
device = rp_pico::hal::pac,
|
|
||||||
dispatchers = [TIMER_IRQ_1]
|
|
||||||
)]
|
|
||||||
mod app {
|
|
||||||
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::{
|
use embedded_hal::digital::v2::OutputPin;
|
||||||
digital::v2::{OutputPin, ToggleableOutputPin},
|
use embedded_hal::spi::MODE_0;
|
||||||
spi::MODE_0,
|
|
||||||
};
|
|
||||||
use hd44780::{bus::FourBitBus, HD44780};
|
|
||||||
use panic_probe as _;
|
use panic_probe as _;
|
||||||
|
|
||||||
use rp2040_monotonic::{fugit::Duration, Rp2040Monotonic};
|
// Provide an alias for our BSP so we can switch targets quickly.
|
||||||
|
// Uncomment the BSP you included in Cargo.toml, the rest of the code does not need to change.
|
||||||
|
use rp_pico as bsp;
|
||||||
|
|
||||||
use rp_pico::{
|
use bsp::hal::{
|
||||||
hal::{
|
|
||||||
clocks::{init_clocks_and_plls, Clock},
|
clocks::{init_clocks_and_plls, Clock},
|
||||||
fugit::RateExtU32,
|
fugit::RateExtU32,
|
||||||
gpio::{
|
pac,
|
||||||
self,
|
|
||||||
bank0::{
|
|
||||||
Gpio0, Gpio1, Gpio14, Gpio16, Gpio17, Gpio18, Gpio19, Gpio2, Gpio20, Gpio21,
|
|
||||||
Gpio25, Gpio3, Gpio4, Gpio5,
|
|
||||||
},
|
|
||||||
FunctionSio, FunctionSpi, FunctionUart, Pin, PullDown, SioOutput,
|
|
||||||
},
|
|
||||||
sio::Sio,
|
sio::Sio,
|
||||||
spi::{self, Spi},
|
spi::Spi,
|
||||||
uart::{self, UartConfig, UartPeripheral},
|
|
||||||
watchdog::Watchdog,
|
watchdog::Watchdog,
|
||||||
},
|
|
||||||
pac::{SPI0, UART0},
|
|
||||||
Pins,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use hd44780_driver as hd44780;
|
use hd44780_driver as hd44780;
|
||||||
|
@ -48,94 +37,32 @@ mod app {
|
||||||
#[global_allocator]
|
#[global_allocator]
|
||||||
static HEAP: Heap = Heap::empty();
|
static HEAP: Heap = Heap::empty();
|
||||||
|
|
||||||
const MONO_NUM: u32 = 1;
|
fn write_lcd<T: hd44780_driver::bus::DataBus>(
|
||||||
const MONO_DENOM: u32 = 1000000;
|
lcd: &mut hd44780::HD44780<T>,
|
||||||
const ONE_SEC_TICKS: u64 = 1000000;
|
delay: &mut Delay,
|
||||||
|
string: &str,
|
||||||
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>,
|
|
||||||
>,
|
|
||||||
>;
|
|
||||||
|
|
||||||
#[monotonic(binds = TIMER_IRQ_0, default = true)]
|
|
||||||
type Rp2040Mono = Rp2040Monotonic;
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_lcd(lcd: &mut Lcd, delay: &mut Delay, string: &str) {
|
|
||||||
lcd.reset(delay).unwrap();
|
lcd.reset(delay).unwrap();
|
||||||
lcd.clear(delay).unwrap();
|
lcd.clear(delay).unwrap();
|
||||||
lcd.write_str(string, delay).unwrap();
|
lcd.write_str(string, delay).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[shared]
|
#[entry]
|
||||||
struct Shared {
|
fn main() -> ! {
|
||||||
delay: Delay,
|
let mut pac = pac::Peripherals::take().unwrap();
|
||||||
}
|
let core = pac::CorePeripherals::take().unwrap();
|
||||||
|
let mut watchdog = Watchdog::new(pac.WATCHDOG);
|
||||||
#[local]
|
let sio = Sio::new(pac.SIO);
|
||||||
struct Local {
|
|
||||||
led: Led,
|
|
||||||
thermocouple: Thermocouple,
|
|
||||||
lcd: Lcd,
|
|
||||||
external_led: ExternalLed,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[init]
|
|
||||||
fn init(mut ctx: init::Context) -> (Shared, Local, init::Monotonics) {
|
|
||||||
let mut watchdog = Watchdog::new(ctx.device.WATCHDOG);
|
|
||||||
let sio = Sio::new(ctx.device.SIO);
|
|
||||||
|
|
||||||
// External high-speed crystal on the pico board is 12Mhz
|
// External high-speed crystal on the pico board is 12Mhz
|
||||||
let external_xtal_freq_hz = 12_000_000u32;
|
let external_xtal_freq_hz = 12_000_000u32;
|
||||||
let clocks = init_clocks_and_plls(
|
let clocks = init_clocks_and_plls(
|
||||||
external_xtal_freq_hz,
|
external_xtal_freq_hz,
|
||||||
ctx.device.XOSC,
|
pac.XOSC,
|
||||||
ctx.device.CLOCKS,
|
pac.CLOCKS,
|
||||||
ctx.device.PLL_SYS,
|
pac.PLL_SYS,
|
||||||
ctx.device.PLL_USB,
|
pac.PLL_USB,
|
||||||
&mut ctx.device.RESETS,
|
&mut pac.RESETS,
|
||||||
&mut watchdog,
|
&mut watchdog,
|
||||||
)
|
)
|
||||||
.ok()
|
.ok()
|
||||||
|
@ -149,27 +76,23 @@ mod app {
|
||||||
unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
|
unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut delay =
|
let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz());
|
||||||
cortex_m::delay::Delay::new(ctx.core.SYST, clocks.system_clock.freq().to_Hz());
|
|
||||||
|
|
||||||
let pins = Pins::new(
|
let pins = bsp::Pins::new(
|
||||||
ctx.device.IO_BANK0,
|
pac.IO_BANK0,
|
||||||
ctx.device.PADS_BANK0,
|
pac.PADS_BANK0,
|
||||||
sio.gpio_bank0,
|
sio.gpio_bank0,
|
||||||
&mut ctx.device.RESETS,
|
&mut pac.RESETS,
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut uart: Uart = UartPeripheral::new(
|
let uart_pins = (pins.gpio0.into_function(), pins.gpio1.into_function());
|
||||||
ctx.device.UART0,
|
let mut uart = UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS)
|
||||||
(pins.gpio0.into_function(), pins.gpio1.into_function()),
|
|
||||||
&mut ctx.device.RESETS,
|
|
||||||
)
|
|
||||||
.enable(
|
.enable(
|
||||||
UartConfig::new(
|
UartConfig::new(
|
||||||
9600u32.Hz(),
|
9600u32.Hz(),
|
||||||
rp_pico::hal::uart::DataBits::Eight,
|
bsp::hal::uart::DataBits::Eight,
|
||||||
None,
|
None,
|
||||||
rp_pico::hal::uart::StopBits::One,
|
bsp::hal::uart::StopBits::One,
|
||||||
),
|
),
|
||||||
clocks.peripheral_clock.freq(),
|
clocks.peripheral_clock.freq(),
|
||||||
)
|
)
|
||||||
|
@ -178,36 +101,29 @@ mod app {
|
||||||
defmt::info!("Program start");
|
defmt::info!("Program start");
|
||||||
writeln!(uart, "Program start\r",).unwrap();
|
writeln!(uart, "Program start\r",).unwrap();
|
||||||
|
|
||||||
let mut led_pin: Led = pins.led.into_push_pull_output();
|
let mut led_pin = pins.led.into_push_pull_output();
|
||||||
let mut external_led_pin: ExternalLed = pins.gpio14.into_push_pull_output();
|
let mut external_led_pin = pins.gpio14.into_push_pull_output();
|
||||||
|
|
||||||
//cs
|
|
||||||
let thermometer_spi_device = ctx.device.SPI0;
|
|
||||||
let thermocouple = Thermocouple {
|
|
||||||
cs: pins
|
|
||||||
.gpio5
|
|
||||||
.into_push_pull_output_in_state(gpio::PinState::Low),
|
|
||||||
|
|
||||||
spi: Spi::<_, _, _, 8>::new(
|
|
||||||
thermometer_spi_device,
|
|
||||||
(
|
|
||||||
//mosi
|
|
||||||
pins.gpio3.into_function::<gpio::FunctionSpi>(),
|
|
||||||
//miso/do
|
|
||||||
pins.gpio4.into_function::<gpio::FunctionSpi>(),
|
|
||||||
//clk
|
//clk
|
||||||
pins.gpio2.into_function::<gpio::FunctionSpi>(),
|
let thermometer_spi_sck = pins.gpio2.into_function::<gpio::FunctionSpi>();
|
||||||
),
|
//MOSI, I think unused?
|
||||||
)
|
let thermometer_spi_tx = pins.gpio3.into_function::<gpio::FunctionSpi>();
|
||||||
.init(
|
//do, or MISO.
|
||||||
&mut ctx.device.RESETS,
|
let thermometer_spi_rx = pins.gpio4.into_function::<gpio::FunctionSpi>();
|
||||||
|
//cs
|
||||||
|
let mut thermometer_spi_csn = pins
|
||||||
|
.gpio5
|
||||||
|
.into_push_pull_output_in_state(gpio::PinState::Low);
|
||||||
|
|
||||||
|
let thermometer_spi_device = pac.SPI0;
|
||||||
|
let spi_pin_layout = (thermometer_spi_tx, thermometer_spi_rx, thermometer_spi_sck);
|
||||||
|
let mut spi = Spi::<_, _, _, 8>::new(thermometer_spi_device, spi_pin_layout).init(
|
||||||
|
&mut pac.RESETS,
|
||||||
125_000_000u32.Hz(),
|
125_000_000u32.Hz(),
|
||||||
4u32.MHz(),
|
4u32.MHz(),
|
||||||
MODE_0,
|
MODE_0,
|
||||||
),
|
);
|
||||||
};
|
|
||||||
|
|
||||||
let mut lcd: Lcd = hd44780::HD44780::new_4bit(
|
let mut 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
|
||||||
|
@ -218,54 +134,20 @@ mod app {
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let mono = Rp2040Mono::new(ctx.device.TIMER);
|
|
||||||
|
|
||||||
write_lcd(&mut lcd, &mut delay, "Starting");
|
write_lcd(&mut lcd, &mut delay, "Starting");
|
||||||
|
loop {
|
||||||
(
|
led_pin.set_high().unwrap();
|
||||||
Shared { delay },
|
external_led_pin.set_low().unwrap();
|
||||||
Local {
|
delay.delay_ms(500);
|
||||||
led: led_pin,
|
led_pin.set_low().unwrap();
|
||||||
thermocouple,
|
external_led_pin.set_high().unwrap();
|
||||||
lcd,
|
delay.delay_ms(500);
|
||||||
external_led: external_led_pin,
|
match spi.read_thermocouple(&mut thermometer_spi_csn, Unit::Celsius) {
|
||||||
},
|
Ok(v) => {
|
||||||
init::Monotonics(mono),
|
writeln!(uart, "Current: {} \r", v).unwrap();
|
||||||
)
|
write_lcd(&mut lcd, &mut delay, &format!("{:02.2} C", v))
|
||||||
|
|
||||||
// loop {
|
|
||||||
// led_pin.set_high().unwrap();
|
|
||||||
// external_led_pin.set_low().unwrap();
|
|
||||||
// delay.delay_ms(500);
|
|
||||||
// led_pin.set_low().unwrap();
|
|
||||||
// external_led_pin.set_high().unwrap();
|
|
||||||
// delay.delay_ms(500);
|
|
||||||
// match thermocouple.read_temp() {
|
|
||||||
// Ok(v) => {
|
|
||||||
// writeln!(uart, "Current: {} \r", v).unwrap();
|
|
||||||
// write_lcd(&mut lcd, &mut delay, &format!("{:02.2} C", v))
|
|
||||||
// }
|
|
||||||
// Err(e) => defmt::error!("error reading temp {}", defmt::Debug2Format(&e)),
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
#[task(local = [led])]
|
Err(e) => defmt::error!("error reading temp {}", defmt::Debug2Format(&e)),
|
||||||
fn heartbeat(ctx: heartbeat::Context) {
|
};
|
||||||
// Flicker the built-in LED
|
|
||||||
ctx.local.led.toggle().unwrap();
|
|
||||||
|
|
||||||
// Re-spawn this task after 1 second
|
|
||||||
let one_second = Duration::<u64, MONO_NUM, MONO_DENOM>::from_ticks(ONE_SEC_TICKS);
|
|
||||||
heartbeat::spawn_after(one_second).unwrap();
|
|
||||||
}
|
|
||||||
#[task(local = [external_led])]
|
|
||||||
fn second_heartbeat(ctx: second_heartbeat::Context) {
|
|
||||||
// Flicker the built-in LED
|
|
||||||
ctx.local.external_led.toggle().unwrap();
|
|
||||||
|
|
||||||
// Re-spawn this task after 1 second
|
|
||||||
let one_and_one_half_second =
|
|
||||||
Duration::<u64, MONO_NUM, MONO_DENOM>::from_ticks((ONE_SEC_TICKS as f32 * 1.5) as u64);
|
|
||||||
heartbeat::spawn_after(one_and_one_half_second).unwrap();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue