Skip to content

Commit

Permalink
nrf: various cleanups
Browse files Browse the repository at this point in the history
* remove unused imports
* remove unused (led)-members
* fix unused variables
* fix various minor warnings
  • Loading branch information
daringer committed Jul 26, 2022
1 parent 8459399 commit 35dac8d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 59 deletions.
8 changes: 4 additions & 4 deletions runners/embedded/src/bin/app-nrf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ mod app {

#[cfg(feature = "board-nk3am")]
let ui = ERL::soc::board::init_ui(board_gpio.rgb_led,
ctx.device.PWM0, ctx.device.TIMER1,
ctx.device.PWM1, ctx.device.TIMER2,
ctx.device.PWM2, ctx.device.TIMER3,
ctx.device.PWM0,
ctx.device.PWM1,
ctx.device.PWM2,
board_gpio.touch.unwrap()
);

Expand Down Expand Up @@ -246,7 +246,7 @@ mod app {

#[task(priority = 5, binds = POWER_CLOCK, local = [power])]
fn power_handler(ctx: power_handler::Context) {
let mut power = ctx.local.power;
let power = ctx.local.power;

trace!("irq PWR {:x} {:x} {:x}",
power.mainregstatus.read().bits(),
Expand Down
58 changes: 18 additions & 40 deletions runners/embedded/src/soc_nrf52840/board_nk3am.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use nrf52840_pac::{
Peripherals, CorePeripherals
};
use nrf52840_hal::{
gpio::{p0, p1, Level, Pin, Output, PushPull},
gpiote::Gpiote,
prelude::{OutputPin, InputPin},
prelude::InputPin,
pwm::Pwm,
timer::Timer,
pac, pwm, timer, spim,
pac, pwm, spim,
};

pub const BOARD_NAME: &'static str = "NK3AM";
Expand All @@ -33,9 +29,6 @@ pub struct RgbLed {
pwm_red: Pwm<pac::PWM0>,
pwm_green: Pwm<pac::PWM1>,
pwm_blue: Pwm<pac::PWM2>,
timer_red: Timer<pac::TIMER1>,
timer_green: Timer<pac::TIMER2>,
timer_blue: Timer<pac::TIMER3>,
}

pub struct HardwareButtons {
Expand Down Expand Up @@ -63,7 +56,7 @@ impl Press for HardwareButtons {

for idx in 0..need_ticks+1 {
match floating.is_low() {
Err(e) => { trace!("is_pressed: err!"); },
Err(_e) => { trace!("is_pressed: err!"); },
Ok(v) => match v {
true => {
ticks = idx;
Expand All @@ -86,29 +79,28 @@ impl Press for HardwareButtons {

impl RgbLed {

pub fn init_led<T: pwm::Instance, S: timer::Instance>(
pub fn init_led<T: pwm::Instance>(
led: OutPin,
raw_pwm: T,
raw_timer: S,
channel: pwm::Channel)
-> (Pwm<T>, Timer<S>) {
-> Pwm<T> {

let pwm = Pwm::new(raw_pwm);
pwm.set_output_pin(channel, led);

//pwm.set_period(500u32.hz());
//debug!("max duty: {:?}", pwm.max_duty());
pwm.set_max_duty(255);
(pwm, Timer::new(raw_timer))
pwm

}

pub fn set_led(
&mut self,
color: Color,
channel: pwm::Channel,
intensity: u8) {

intensity: u8
) {
match color {
Color::Red => {
let duty: u16 = ((intensity as f32 / 255.0) * self.pwm_red.max_duty() as f32) as u16;
Expand All @@ -123,34 +115,28 @@ impl RgbLed {
self.pwm_blue.set_duty_on(channel, duty as u16);
},
}
//}
}
}

impl RgbLed {
pub fn new (
leds: [Option<OutPin>; 3],
pwm_red: pac::PWM0,
timer_red: pac::TIMER1,
pwm_green: pac::PWM1,
timer_green: pac::TIMER2,
pwm_blue: pac::PWM2,
timer_blue: pac::TIMER3,
) -> RgbLed {

let [mut red, mut green, mut blue] = leds;
let [red, green, blue] = leds;

let (red_pwm_obj, red_timer_obj) =
RgbLed::init_led(red.unwrap(), pwm_red, timer_red, pwm::Channel::C0);
let (green_pwm_obj, green_timer_obj) =
RgbLed::init_led(green.unwrap(), pwm_green, timer_green, pwm::Channel::C1);
let (blue_pwm_obj, blue_timer_obj) =
RgbLed::init_led(blue.unwrap(), pwm_blue, timer_blue, pwm::Channel::C2);
let red_pwm_obj =
RgbLed::init_led(red.unwrap(), pwm_red, pwm::Channel::C0);
let green_pwm_obj =
RgbLed::init_led(green.unwrap(), pwm_green, pwm::Channel::C1);
let blue_pwm_obj =
RgbLed::init_led(blue.unwrap(), pwm_blue, pwm::Channel::C2);

Self {
pwm_red: red_pwm_obj, pwm_green: green_pwm_obj, pwm_blue: blue_pwm_obj,
timer_red: red_timer_obj, timer_green: green_timer_obj, timer_blue: blue_timer_obj

pwm_red: red_pwm_obj, pwm_green: green_pwm_obj, pwm_blue: blue_pwm_obj
}

}
Expand All @@ -173,20 +159,12 @@ impl rgb_led::RgbLed for RgbLed {
pub fn init_ui(leds: [Option<OutPin>; 3],

pwm_red: pac::PWM0,
timer_red: pac::TIMER1,
pwm_green: pac::PWM1,
timer_green: pac::TIMER2,
pwm_blue: pac::PWM2,
timer_blue: pac::TIMER3,
touch: OutPin
) -> TrussedUI {

let rgb = RgbLed::new(
leds,
pwm_red, timer_red,
pwm_green, timer_green,
pwm_blue, timer_blue,
);
let rgb = RgbLed::new(leds, pwm_red, pwm_green, pwm_blue);

let buttons = HardwareButtons {
touch_button: Some(touch),
Expand All @@ -201,7 +179,7 @@ pub fn init_ui(leds: [Option<OutPin>; 3],
ui
}

pub fn init_pins(gpiote: &Gpiote, gpio_p0: p0::Parts, gpio_p1: p1::Parts) -> BoardGPIO {
pub fn init_pins(_gpiote: &Gpiote, gpio_p0: p0::Parts, gpio_p1: p1::Parts) -> BoardGPIO {

/* touch sensor */
let touch = gpio_p0.p0_04.into_push_pull_output(Level::High).degrade();
Expand Down
10 changes: 5 additions & 5 deletions runners/embedded/src/soc_nrf52840/flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ impl littlefs2::driver::Storage for FlashStorage {
}

fn erase(&mut self, off: usize, len: usize) -> Result<usize, littlefs2::io::Error> {
const real_block_size: u32 = 4096;
const REAL_BLOCK_SIZE: u32 = 4096;
trace!("IFe {:x} {:x}", off, len);

let real_off_remainer: u32 = (off as u32) % real_block_size;
let real_off: u32 = ((off as u32) - real_off_remainer);
let mut buf: [u8; real_block_size as usize] = [0x00; real_block_size as usize];
let real_off_remainer: u32 = (off as u32) % REAL_BLOCK_SIZE;
let real_off: u32 = (off as u32) - real_off_remainer;
let mut buf: [u8; REAL_BLOCK_SIZE as usize] = [0x00; REAL_BLOCK_SIZE as usize];
self.nvmc.read(real_off as u32, &mut buf);

/* nrf52840_hal has nvmc.erase(from, to) */
//let res = self.nvmc.erase(off as u32, (off+len) as u32);

let res = self.nvmc.erase(real_off as u32, (real_off + real_block_size) as u32);
let res = self.nvmc.erase(real_off as u32, (real_off + REAL_BLOCK_SIZE) as u32);

trace!("IFex {:x} {:x} {:x}", real_off, (off - (real_off as usize)), ((real_off_remainer as usize) + len) );
self.nvmc.write(real_off as u32, &buf[0..(off - (real_off as usize))]);
Expand Down
9 changes: 3 additions & 6 deletions runners/embedded/src/soc_nrf52840/trussed_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ use crate::traits::{

};
use trussed::platform::{
ui, reboot, consent,
ui, consent,
};
use nrf52840_hal::Timer;

use crate::runtime::UserPresenceStatus;

use nrf52840_hal::prelude::_embedded_hal_blocking_delay_DelayMs;


// translated from https://stackoverflow.com/a/2284929/2490057
fn sin(x: f32) -> f32
Expand Down Expand Up @@ -95,7 +92,7 @@ RGB: RgbLed,

let mut counter: u8 = 0;
let mut is_pressed = false;
const threshold: u8 = 3;
let threshold: u8 = 1;

let start_time = self.uptime().as_millis();
let timeout_at = start_time + 28_000u128;
Expand All @@ -115,7 +112,7 @@ RGB: RgbLed,
continue;
}

if let Some(mut button) = self.buttons.as_mut() {
if let Some(button) = self.buttons.as_mut() {
UserPresenceStatus::set_waiting(true);
is_pressed = button.is_pressed(Button::A);
UserPresenceStatus::set_waiting(false);
Expand Down
4 changes: 0 additions & 4 deletions runners/embedded/src/soc_nrf52840/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use littlefs2::const_ram_storage;
use nrf52840_hal::{
gpio::{Pin, Input, Output, PushPull, PullDown, PullUp},
spim,
Expand All @@ -8,9 +7,6 @@ use nrf52840_hal::{
usbd::{Usbd, UsbPeripheral},
};
use nrf52840_pac;

use trussed::types::{LfsStorage, LfsResult};

use crate::soc::types::pac::SCB;


Expand Down

0 comments on commit 35dac8d

Please sign in to comment.