Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to embedded-hal 1.0 #2

Merged
merged 10 commits into from
Sep 30, 2024
Merged
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ rustflags = [
target = "thumbv6m-none-eabi"

[env]
DEFMT_LOG = "debug"
DEFMT_LOG = "info"
28 changes: 14 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,35 @@ bench = false
[dependencies]
cortex-m = "0.7"
cortex-m-rt = "0.7"
embedded-hal = { version = "0.2", features = ["unproven"] }
embedded-sdmmc = { version = "0.6", default-features = false, features = [
critical-section = "1.1"
embedded-hal = { version = "1.0", features = ["defmt-03"] }
embedded-sdmmc = { version = "0.8", default-features = false, features = [
"defmt-log",
] }
shared-bus = { version = "0.3", features = ["cortex-m"] }
embedded-hal-bus = { version = "0.1.0", features = ["defmt-03"] }
defmt = "0.3"
defmt-rtt = "0.4"
panic-probe = { version = "0.3", features = ["print-defmt"] }
micromath = { version = "2.1", features = ["vector"] }
mpu6050 = { git = "https://github.com/michaelkamprath/mpu6050.git", branch = "micromath", features = [
mpu6050 = { git = "https://github.com/michaelkamprath/mpu6050.git", branch = "embedded-hal-upgrade", features = [
"defmt",
] }
embedded-alloc = "0.6"
adafruit-lcd-backpack = { version = "0.1", features = ["defmt"] }
i2c-character-display = { version = "0.1", features = ["defmt"] }
ini_core = "0.2"

# We're using a Pico by default on this template
rp-pico = "0.8"
rp-pico = "0.9"

# but you can use any BSP. Uncomment this to use the pro_micro_rp2040 BSP instead
# sparkfun-pro-micro-rp2040 = "0.7"

# If you're not going to use a Board Support Package you'll need these:
rp2040-hal = { version = "0.9", features = [
"rt",
"critical-section-impl",
"defmt",
] }
# rp2040-hal = { version = "0.10", features = [
# "rt",
# "critical-section-impl",
# "defmt",
# ] }
# rp2040-boot2 = "0.3"

# cargo build/run
Expand Down Expand Up @@ -94,8 +95,7 @@ incremental = false
lto = 'fat'
opt-level = 3

# [patch."https://github.com/michaelkamprath/mpu6050.git"]
[patch."https://github.com/michaelkamprath/mpu6050.git"]
# mpu6050 = { path = "../mpu6050" }

# [patch."https://github.com/michaelkamprath/adafruit-lcd-backpack-rust.git"]
# adafruit-lcd-backpack = { path = "../adafruit-lcd-backpack-rust" }
[patch.crates-io]
74 changes: 20 additions & 54 deletions src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ use alloc::{
};
use defmt::{debug, info, warn};
use embedded_hal::{
blocking::{
delay::{DelayMs, DelayUs},
i2c::{Write as I2cWrite, WriteRead},
},
digital::v2::{InputPin, OutputPin},
PwmPin,
delay::DelayNs,
digital::{InputPin, OutputPin},
i2c::I2c,
pwm::SetDutyCycle,
spi::SpiDevice,
};
use micromath::F32Ext;

Expand All @@ -28,25 +27,16 @@ pub struct Driver<
INA2: OutputPin,
INB1: OutputPin,
INB2: OutputPin,
ENA: PwmPin<Duty = u16>,
ENB: PwmPin<Duty = u16>,
ENA: SetDutyCycle,
ENB: SetDutyCycle,
BUTT1: InputPin,
BUTT2: InputPin,
TWI,
TWI_ERR,
SPI,
CS: OutputPin,
DELAY,
TWI: I2c,
SPI_DEV: SpiDevice<u8>,
DELAY: DelayNs + Clone,
LED1: OutputPin,
> where
TWI: I2cWrite<Error = TWI_ERR> + WriteRead<Error = TWI_ERR>,
TWI_ERR: defmt::Format,
SPI: embedded_hal::blocking::spi::Transfer<u8> + embedded_hal::blocking::spi::Write<u8>,
<SPI as embedded_hal::blocking::spi::Transfer<u8>>::Error: core::fmt::Debug,
<SPI as embedded_hal::blocking::spi::Write<u8>>::Error: core::fmt::Debug,
DELAY: DelayMs<u16> + DelayUs<u16> + DelayMs<u8> + DelayUs<u8> + Copy,
{
robot: Robot<'a, INA1, INA2, INB1, INB2, ENA, ENB, BUTT1, BUTT2, TWI, TWI_ERR, SPI, CS, DELAY>,
> {
robot: Robot<'a, INA1, INA2, INB1, INB2, ENA, ENB, BUTT1, BUTT2, TWI, SPI_DEV, DELAY>,
delay: DELAY,
led1: LED1,
selected_path: Option<String>,
Expand All @@ -58,42 +48,18 @@ impl<
INA2: OutputPin,
INB1: OutputPin,
INB2: OutputPin,
ENA: PwmPin<Duty = u16>,
ENB: PwmPin<Duty = u16>,
ENA: SetDutyCycle,
ENB: SetDutyCycle,
BUTT1: InputPin,
BUTT2: InputPin,
TWI,
TWI_ERR,
SPI,
CS: OutputPin,
DELAY,
TWI: I2c,
SPI_DEV: SpiDevice<u8>,
DELAY: DelayNs + Clone,
LED1: OutputPin,
> Driver<'a, INA1, INA2, INB1, INB2, ENA, ENB, BUTT1, BUTT2, TWI, TWI_ERR, SPI, CS, DELAY, LED1>
where
TWI: I2cWrite<Error = TWI_ERR> + WriteRead<Error = TWI_ERR>,
TWI_ERR: defmt::Format,
SPI: embedded_hal::blocking::spi::Transfer<u8> + embedded_hal::blocking::spi::Write<u8>,
<SPI as embedded_hal::blocking::spi::Transfer<u8>>::Error: core::fmt::Debug,
<SPI as embedded_hal::blocking::spi::Write<u8>>::Error: core::fmt::Debug,
DELAY: DelayMs<u16> + DelayUs<u16> + DelayMs<u8> + DelayUs<u8> + Copy,
> Driver<'a, INA1, INA2, INB1, INB2, ENA, ENB, BUTT1, BUTT2, TWI, SPI_DEV, DELAY, LED1>
{
pub fn new(
robot: Robot<
'a,
INA1,
INA2,
INB1,
INB2,
ENA,
ENB,
BUTT1,
BUTT2,
TWI,
TWI_ERR,
SPI,
CS,
DELAY,
>,
robot: Robot<'a, INA1, INA2, INB1, INB2, ENA, ENB, BUTT1, BUTT2, TWI, SPI_DEV, DELAY>,
delay: DELAY,
led1_pin: LED1,
) -> Self {
Expand All @@ -111,7 +77,7 @@ where
let start_millis = millis();
while millis() - start_millis < ms {
self.robot.handle_loop();
self.delay.delay_us(500_u16);
self.delay.delay_us(500);
}
}

Expand Down
100 changes: 51 additions & 49 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,36 @@ mod model;
mod robot;
mod system;

use bsp::{
entry,
hal::{fugit::HertzU32, gpio},
};
use core::cell::RefCell;
use defmt::{error, info, panic};
use defmt_rtt as _;
use panic_probe as _;
use rp2040_hal::{
gpio::{
bank0::{Gpio0, Gpio2, Gpio3, Gpio4, Gpio5},
FunctionI2c, FunctionSpi, Pin, PullDown,
},
pac::{I2C0, SPI0},
};

// 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 sparkfun_pro_micro_rp2040 as bsp;

use crate::robot::Robot;
use bsp::{
entry,
hal::fugit::HertzU32,
hal::{
clocks::{init_clocks_and_plls, Clock},
gpio::{FunctionI2C, Pin, PullUp},
pac,
pwm::Slices,
sio::Sio,
watchdog::Watchdog,
},
};
use driver::Driver;
use system::millis::init_millis;
extern crate alloc;

use embedded_alloc::TlsfHeap as Heap;

#[global_allocator]
static HEAP: Heap = Heap::empty();

use bsp::hal::{
clocks::{init_clocks_and_plls, Clock},
pac,
pwm::Slices,
sio::Sio,
watchdog::Watchdog,
};

use driver::Driver;
use robot::Robot;
use system::millis::init_millis;

#[entry]
fn main() -> ! {
// Initialize the allocator BEFORE you use it
Expand Down Expand Up @@ -100,35 +91,43 @@ fn main() -> ! {
channel_a.output_to(pins.gpio8);
channel_b.output_to(pins.gpio9);

// Configure two pins as being I²C, not GPIO
let sda_pin: Pin<_, FunctionI2C, PullUp> = pins.gpio4.reconfigure();
let scl_pin: Pin<_, FunctionI2C, PullUp> = pins.gpio5.reconfigure();
// set up I2C
let i2c = bsp::hal::I2C::new_controller(
pac.I2C0,
pins.gpio4.into_function::<gpio::FunctionI2c>(),
pins.gpio5.into_function::<gpio::FunctionI2c>(),
sda_pin,
scl_pin,
HertzU32::from_raw(400_000),
&mut pac.RESETS,
clocks.system_clock.freq(),
);
let i2c_manager: &'static _ = shared_bus::new_cortexm!(rp2040_hal::I2C<I2C0, (Pin<Gpio4, FunctionI2c, PullDown>, Pin<Gpio5, FunctionI2c, PullDown>)> = i2c).unwrap();
let i2c_ref_cell = RefCell::new(i2c);
let i2c_mutex = critical_section::Mutex::new(i2c_ref_cell);

// set up SPI
#[allow(clippy::type_complexity)]
let spi: rp_pico::hal::Spi<
rp_pico::hal::spi::Disabled,
SPI0,
(
rp_pico::hal::gpio::Pin<Gpio3, FunctionSpi, PullDown>,
rp_pico::hal::gpio::Pin<Gpio0, FunctionSpi, PullDown>,
rp_pico::hal::gpio::Pin<Gpio2, FunctionSpi, PullDown>,
),
> = bsp::hal::Spi::new(
pac.SPI0,
(
pins.gpio3.into_function::<gpio::FunctionSpi>(),
pins.gpio0.into_function::<gpio::FunctionSpi>(),
pins.gpio2.into_function::<gpio::FunctionSpi>(),
),
);
// let spi: rp_pico::hal::Spi<
// rp_pico::hal::spi::Disabled,
// SPI0,
// (
// rp_pico::hal::gpio::Pin<Gpio3, FunctionSpi, PullDown>,
// rp_pico::hal::gpio::Pin<Gpio0, FunctionSpi, PullDown>,
// rp_pico::hal::gpio::Pin<Gpio2, FunctionSpi, PullDown>,
// ),
// > = bsp::hal::Spi::new(
// pac.SPI0,
// (
// pins.gpio3.into_function::<gpio::FunctionSpi>(),
// pins.gpio0.into_function::<gpio::FunctionSpi>(),
// pins.gpio2.into_function::<gpio::FunctionSpi>(),
// ),
// );
let spi_mosi = pins.gpio3.into_function::<bsp::hal::gpio::FunctionSpi>();
let spi_miso = pins.gpio0.into_function::<bsp::hal::gpio::FunctionSpi>();
let spi_sclk = pins.gpio2.into_function::<bsp::hal::gpio::FunctionSpi>();
let spi = bsp::hal::spi::Spi::<_, _, _, 8>::new(pac.SPI0, (spi_mosi, spi_miso, spi_sclk));

// Exchange the uninitialised SPI driver for an initialised one
let spi = spi.init(
Expand All @@ -139,16 +138,19 @@ fn main() -> ! {
);

let sd = crate::robot::file_storage::FileStorage::new(
spi,
pins.gpio1.into_push_pull_output(),
crate::robot::file_storage::sd_card_spi_device::SDCardSPIDevice::new(
spi,
pins.gpio1.into_push_pull_output(),
timer,
),
timer,
);

// If SD card is successfully initialized, we can increase the SPI speed
match sd.spi(|spi| {
spi.set_baudrate(
spi.bus.set_baudrate(
clocks.peripheral_clock.freq(),
HertzU32::from_raw(20_000_000),
HertzU32::from_raw(16_000_000),
)
}) {
Some(speed) => {
Expand All @@ -168,7 +170,7 @@ fn main() -> ! {
channel_b,
pins.gpio14.into_pull_up_input(),
pins.gpio15.into_pull_up_input(),
i2c_manager,
&i2c_mutex,
pins.gpio21.into_pull_up_input(),
pins.gpio20.into_pull_up_input(),
sd,
Expand Down
Loading
Loading