Skip to content

Commit

Permalink
Fix warnings (#76)
Browse files Browse the repository at this point in the history
* Fix all warnings

* Fix all warnings in examples

* Format

* Fix more warnings

* Resolve comments
  • Loading branch information
usbalbin authored Nov 29, 2023
1 parent a92f938 commit d96a466
Show file tree
Hide file tree
Showing 18 changed files with 64 additions and 59 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ optional = true
[dev-dependencies]
cortex-m-rt = "0.7.2"
defmt-rtt = "0.4.0"
cortex-m-rtic = "0.5.8"
cortex-m-rtic = "1.1.4"
cortex-m-semihosting = "0.3.5"
panic-probe = { version = "0.3.0", features = ["print-defmt"] }
panic-semihosting = "0.5.3"
Expand Down Expand Up @@ -101,3 +101,7 @@ debug = false
codegen-units = 1
incremental = false
lto = true

[[example]]
name = "flash_with_rtic"
required-features = ["stm32g474"]
2 changes: 1 addition & 1 deletion examples/can-echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn main() -> ! {

loop {
if let Ok(rxheader) = block!(can.receive0(&mut buffer)) {
block!(can.transmit(rxheader.unwrap().to_tx_header(None), &mut buffer)).unwrap();
block!(can.transmit(rxheader.unwrap().to_tx_header(None), &buffer)).unwrap();
}
}
}
2 changes: 0 additions & 2 deletions examples/flash_with_rtic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ mod app {

const LOG_LEVEL: log::LevelFilter = log::LevelFilter::Info;

use panic_halt as _; // you can put a breakpoint on `rust_begin_unwind` to catch panics

// Resources shared between tasks
#[shared]
struct Shared {}
Expand Down
2 changes: 1 addition & 1 deletion examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use utils::logger::println;

#[entry]
fn main() -> ! {
let _ = println!("Hello, STM32G4!");
println!("Hello, STM32G4!");

#[allow(clippy::empty_loop)]
loop {}
Expand Down
6 changes: 3 additions & 3 deletions examples/i2c-bme680.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ fn main() -> ! {
.with_run_gas(true)
.build();

let profile_dur = dev.get_profile_dur(&settings.0).unwrap();
let _profile_dur = dev.get_profile_dur(&settings.0).unwrap();
dev.set_sensor_settings(&mut delayer, settings).unwrap();
dev.set_sensor_mode(&mut delayer, PowerMode::ForcedMode)
.unwrap();
let sensor_settings = dev.get_sensor_settings(settings.1);
let _sensor_settings = dev.get_sensor_settings(settings.1);

loop {
delay.delay_ms(500u32);
let power_mode = dev.get_sensor_mode();
let _power_mode = dev.get_sensor_mode();
dev.set_sensor_mode(&mut delayer, PowerMode::ForcedMode)
.unwrap();
let (data, _state) = dev.get_sensor_data(&mut delayer).unwrap();
Expand Down
8 changes: 5 additions & 3 deletions examples/opamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn main() -> ! {
);

// Configure op with pa7 as non-inverting input and set gain to x4
let mut opamp2 = opamp2.pga(
let opamp2 = opamp2.pga(
pa7,
PgaModeInternal::gain(NonInvertingGain::Gain4),
Option::<PA6<Analog>>::None, // Do not route output to any external pin, use internal AD instead
Expand All @@ -72,7 +72,7 @@ fn main() -> ! {
loop {
// Here we can sample the output of opamp2 as if it was a regular AD pin
let sample = adc.convert(
&mut opamp2,
&opamp2,
stm32g4xx_hal::adc::config::SampleTime::Cycles_640_5,
);

Expand All @@ -87,6 +87,8 @@ fn main() -> ! {
let (_opamp1, _pa1, _mode, _some_pa2) = _opamp1.disable();
let (_opamp2, _pa7, _mode, _none) = opamp2.disable();

loop {}
loop {
delay.delay_ms(100);
}
}
}
6 changes: 5 additions & 1 deletion examples/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ use hal::prelude::*;
use hal::stm32;
use hal::time::RateExtU32;
use stm32g4xx_hal as hal;
mod utils;
extern crate cortex_m_rt as rt;

#[macro_use]
mod utils;

#[entry]
fn main() -> ! {
utils::logger::init();

let dp = stm32::Peripherals::take().expect("cannot take peripherals");
let mut rcc = dp.RCC.constrain();
let gpioa = dp.GPIOA.split(&mut rcc);
Expand Down
20 changes: 9 additions & 11 deletions examples/spi-sd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,30 @@

extern crate embedded_sdmmc;

use fugit::RateExtU32;
use hal::gpio::gpiob::PB14;
use hal::gpio::gpiob::PB15;
use hal::gpio::gpiof::PF8;
use hal::gpio::gpiof::PF9;
use hal::gpio::Alternate;
use hal::gpio::AF5;
use hal::prelude::*;
use hal::rcc::Config;
use hal::spi;
use hal::stm32;

use hal::stm32::Peripherals;
use hal::time::RateExtU32;
use hal::timer::Timer;
use stm32g4xx_hal as hal;

use embedded_sdmmc::{
Block, BlockCount, BlockDevice, BlockIdx, Controller, Error, Mode, TimeSource, Timestamp,
VolumeIdx,
};
use embedded_sdmmc::{TimeSource, Timestamp};

use cortex_m_rt::entry;
use log::info;

#[macro_use]
mod utils;

#[entry]
fn main() -> ! {
utils::logger::init();

let dp = Peripherals::take().unwrap();
let rcc = dp.RCC.constrain();
let mut rcc = rcc.freeze(Config::hsi());
Expand All @@ -49,7 +45,7 @@ fn main() -> ! {
let miso: PB14<Alternate<AF5>> = gpiob.pb14.into_alternate();
let mosi: PB15<Alternate<AF5>> = gpiob.pb15.into_alternate();

let mut spi = dp
let spi = dp
.SPI2
.spi((sck, miso, mosi), spi::MODE_0, 400.kHz(), &mut rcc);

Expand All @@ -70,6 +66,8 @@ fn main() -> ! {

let mut cont = embedded_sdmmc::Controller::new(embedded_sdmmc::SdMmcSpi::new(spi, cs), Clock);

cont.device().init();
cont.device().init().unwrap();

#[allow(clippy::empty_loop)]
loop {}
}
8 changes: 7 additions & 1 deletion examples/utils/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ cfg_if::cfg_if! {
pub use defmt::println as println;
} else {
pub use log::{info, trace, warn, debug, error};
pub use cortex_m_semihosting::hprintln as println;

#[allow(unused_macros)]
macro_rules! println {
($($tt:tt)*) => {
$crate::cortex_m_semihosting::hprintln!($($tt,)*).unwrap();
};
}
}
}

Expand Down
8 changes: 1 addition & 7 deletions src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ pub mod config {
}

/// Sets the input type per channel
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, Default)]
pub struct DifferentialSelection(pub(crate) u32);
impl DifferentialSelection {
/// Set pin to Single-Ended or Differential
Expand Down Expand Up @@ -614,12 +614,6 @@ pub mod config {
}
}

impl Default for DifferentialSelection {
fn default() -> Self {
DifferentialSelection(0)
}
}

/// Configuration for the adc.
/// There are some additional parameters on the adc peripheral that can be
/// added here when needed but this covers several basic usecases.
Expand Down
1 change: 1 addition & 0 deletions src/can.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod sealed {
}

/// Select an FDCAN Clock Source
#[allow(clippy::upper_case_acronyms)]
#[allow(dead_code)]
enum FdCanClockSource {
/// Select HSE as the FDCAN clock source
Expand Down
8 changes: 2 additions & 6 deletions src/dma/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,18 @@ use super::Bits;
/// the same software priority level, the stream with the lower number takes
/// priority over the stream with the higher number. For example, Stream 2
/// takes priority over Stream 4.
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, Default)]
pub enum Priority {
/// Low priority.
Low,
/// Medium priority.
#[default]
Medium,
/// High priority.
High,
/// Very high priority.
VeryHigh,
}
impl Default for Priority {
fn default() -> Self {
Priority::Medium
}
}

impl Bits<u8> for Priority {
fn bits(self) -> u8 {
Expand Down
1 change: 1 addition & 0 deletions src/dma/mux.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[allow(clippy::upper_case_acronyms)]
pub enum DmaMuxResources {
DMAMUXReqG0 = 1,
DMAMUXReqG1 = 2,
Expand Down
4 changes: 2 additions & 2 deletions src/dma/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ where
{
/// Return the number of elements available to read
pub fn elements_available(&mut self) -> usize {
let blen = unsafe { self.transfer.buf.static_write_buffer().1 } as usize;
let blen = unsafe { self.transfer.buf.static_write_buffer().1 };
let ndtr = STREAM::get_number_of_transfers() as usize;
let pos_at = self.r_pos;

Expand Down Expand Up @@ -341,7 +341,7 @@ where
&mut self,
dat: &mut [<PERIPHERAL as TargetAddress<PeripheralToMemory>>::MemSize],
) -> usize {
let blen = unsafe { self.transfer.buf.static_write_buffer().1 } as usize;
let blen = unsafe { self.transfer.buf.static_write_buffer().1 };
let pos = self.r_pos;
let read = dat.len();

Expand Down
18 changes: 10 additions & 8 deletions src/flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub struct FlashWriter<'a, const SECTOR_SZ_KB: u32> {
verify: bool,
}
impl<'a, const SECTOR_SZ_KB: u32> FlashWriter<'a, SECTOR_SZ_KB> {
#[allow(unused)]
fn unlock_options(&mut self) -> Result<()> {
// Check if flash is busy
while self.flash.sr.sr().read().bsy().bit_is_set() {}
Expand Down Expand Up @@ -142,12 +143,12 @@ impl<'a, const SECTOR_SZ_KB: u32> FlashWriter<'a, SECTOR_SZ_KB> {
if offset
.checked_add(length as u32)
.ok_or(Error::LengthTooLong)?
> self.flash_sz.kbytes() as u32
> self.flash_sz.kbytes()
{
return Err(Error::LengthTooLong);
}

if force_padding == false && length % 8 != 0 {
if !force_padding && length % 8 != 0 {
return Err(Error::ArrayMustBeDivisibleBy8);
}

Expand All @@ -164,7 +165,7 @@ impl<'a, const SECTOR_SZ_KB: u32> FlashWriter<'a, SECTOR_SZ_KB> {
// Set Page Erase
self.flash.cr.cr().modify(|_, w| w.per().set_bit());

let page = start_offset / (SECTOR_SZ_KB as u32);
let page = start_offset / SECTOR_SZ_KB;

// Write address bits
// NOTE(unsafe) This sets the page address in the Address Register.
Expand Down Expand Up @@ -206,7 +207,7 @@ impl<'a, const SECTOR_SZ_KB: u32> FlashWriter<'a, SECTOR_SZ_KB> {
let size = SECTOR_SZ_KB;
let start = start_offset & !(size - 1);
for idx in (start..start + size).step_by(2) {
let write_address = (FLASH_START + idx as u32) as *const u16;
let write_address = (FLASH_START + idx) as *const u16;
let verify: u16 = unsafe { core::ptr::read_volatile(write_address) };
if verify != 0xFFFF {
return Err(Error::VerifyError);
Expand Down Expand Up @@ -237,7 +238,7 @@ impl<'a, const SECTOR_SZ_KB: u32> FlashWriter<'a, SECTOR_SZ_KB> {
pub fn read(&self, offset: u32, length: usize) -> Result<&[u8]> {
self.valid_address(offset)?;

if offset + length as u32 > self.flash_sz.kbytes() as u32 {
if offset + length as u32 > self.flash_sz.kbytes() {
return Err(Error::LengthTooLong);
}

Expand Down Expand Up @@ -281,9 +282,7 @@ impl<'a, const SECTOR_SZ_KB: u32> FlashWriter<'a, SECTOR_SZ_KB> {
// Check if there is enough data to make 2 words, if there isn't, pad the data with 0xFF
if idx + 8 > data.len() {
let mut tmp_buffer = [255u8; 8];
for jdx in idx..data.len() {
tmp_buffer[jdx] = data[idx + jdx];
}
tmp_buffer[idx..data.len()].copy_from_slice(&data[(idx + idx)..(data.len() + idx)]);
let tmp_dword = u64::from_le_bytes(tmp_buffer);
word1 = tmp_dword as u32;
word2 = (tmp_dword >> 32) as u32;
Expand Down Expand Up @@ -392,6 +391,7 @@ pub struct Parts {
pub(crate) cr: CR,

/// Opaque ECCR register
#[allow(unused)]
pub(crate) eccr: ECCR,

/// Opaque KEYR register
Expand All @@ -410,9 +410,11 @@ pub struct Parts {
pub(crate) _pcrop1er: PCROP1ER,

/// Opaque PDKEYR register
#[allow(unused)]
pub(crate) pdkeyr: PDKEYR,

/// Opaque SEC1R register
#[allow(unused)]
pub(crate) sec1r: SEC1R,

/// Opaque SR register
Expand Down
2 changes: 0 additions & 2 deletions src/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,8 +1080,6 @@ fn calculate_deadtime(base_freq: Hertz, deadtime: NanoSecond) -> (u8, u8) {
let deadtime_ticks = deadtime_ticks as u64 * 429497;
let deadtime_ticks = (deadtime_ticks >> 32) as u32;

let deadtime_ticks = deadtime_ticks as u32;

// Choose CR1 CKD divider of 1, 2, or 4 to determine tDTS
let (deadtime_ticks, ckd) = match deadtime_ticks {
t if t <= 1008 => (deadtime_ticks, 1),
Expand Down
Loading

0 comments on commit d96a466

Please sign in to comment.