Skip to content

Commit

Permalink
refactor(keymap): remove storage config for initializing keymap
Browse files Browse the repository at this point in the history
Signed-off-by: Haobo Gu <[email protected]>
  • Loading branch information
HaoboGu committed Jan 23, 2024
1 parent e130753 commit da493a0
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 76 deletions.
29 changes: 9 additions & 20 deletions boards/rp2040/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ mod keymap;
mod macros;
mod vial;

use crate::keymap::{COL, NUM_LAYER, ROW};
use core::cell::RefCell;
use defmt::*;
use defmt_rtt as _;
use embassy_executor::Spawner;
use embassy_rp::{
bind_interrupts,
Expand All @@ -16,21 +19,15 @@ use embassy_rp::{
peripherals::{self, USB},
usb::{Driver, InterruptHandler},
};

use defmt::*;
use defmt_rtt as _;
use panic_probe as _;
use rmk::{eeprom::EepromStorageConfig, initialize_keyboard_and_run, keymap::KeyMap};
use rmk::{initialize_keyboard_and_run, keymap::KeyMap};
use static_cell::StaticCell;

use crate::keymap::{COL, NUM_LAYER, ROW};
use vial::{VIAL_KEYBOARD_DEF, VIAL_KEYBOARD_ID};

bind_interrupts!(struct Irqs {
USBCTRL_IRQ => InterruptHandler<USB>;
});

// static SUSPENDED: AtomicBool = AtomicBool::new(false);
const FLASH_SECTOR_15_ADDR: u32 = 15 * 8192;
const EEPROM_SIZE: usize = 128;
const FLASH_SIZE: usize = 2 * 1024 * 1024;

Expand All @@ -46,6 +43,8 @@ async fn main(_spawner: Spawner) {
// Pin config
let (input_pins, output_pins) = config_matrix_pins_rp!(peripherals: p, input: [PIN_6, PIN_7, PIN_8, PIN_9], output: [PIN_19, PIN_20, PIN_21]);

// Use internal flash to emulate eeprom
let flash = Flash::<_, Blocking, FLASH_SIZE>::new_blocking(p.FLASH);
// Keymap + eeprom config
static MY_KEYMAP: StaticCell<
RefCell<
Expand All @@ -58,19 +57,9 @@ async fn main(_spawner: Spawner) {
>,
>,
> = StaticCell::new();
let eeprom_storage_config = EepromStorageConfig {
start_addr: FLASH_SECTOR_15_ADDR,
storage_size: 8192, // uses 8KB for eeprom
page_size: 32,
};
// Use internal flash to emulate eeprom
// let f = Flash::new_blocking(p.FLASH);
let flash = Flash::<_, Blocking, FLASH_SIZE>::new_blocking(p.FLASH);
// let mut flash = Flash::new_blocking(p.FLASH);
let keymap = MY_KEYMAP.init(RefCell::new(KeyMap::new(
crate::keymap::KEYMAP,
Some(flash),
eeprom_storage_config,
None,
)));

Expand All @@ -89,8 +78,8 @@ async fn main(_spawner: Spawner) {
input_pins,
output_pins,
keymap,
&vial::VIAL_KEYBOARD_ID,
&vial::VIAL_KEYBOARD_DEF,
&VIAL_KEYBOARD_ID,
&VIAL_KEYBOARD_DEF,
)
.await;
}
9 changes: 1 addition & 8 deletions boards/stm32f4/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# cargo-features = ["per-package-target"]

[package]
name = "rmk-stm32f4"
version = "0.1.2"
Expand All @@ -11,8 +9,6 @@ readme = "../../README.md"
edition = "2021"
license = "MIT OR Apache-2.0"

# forced-target = "thumbv7em-none-eabihf"

[dependencies]
rmk = { version = "0.1.2", path = "../../rmk", features = [
"eeprom",
Expand All @@ -21,7 +17,6 @@ rmk = { version = "0.1.2", path = "../../rmk", features = [
cortex-m = { version = "0.7.7", features = ['critical-section-single-core'] }
cortex-m-rt = "0.7.3"
embassy-time = { version = "0.3", git = "https://github.com/embassy-rs/embassy", features = [
# "tick-hz-1_000_000",
"tick-hz-32_768",
"defmt",
] }
Expand All @@ -38,8 +33,6 @@ embassy-executor = { version = "0.5.0", git = "https://github.com/embassy-rs/emb
"executor-thread",
"integrated-timers",
] }
rtt-target = "0.5.0"
log = "0.4.19"
packed_struct = { version = "0.10.1", default-features = false }
embedded-storage = { version = "0.3" }
static_cell = { version = "2" }
Expand All @@ -52,4 +45,4 @@ panic-probe = { version = "0.3", features = ["print-defmt"] }
[[bin]]
name = "rmk-stm32f4"
test = false
bench = false
bench = false
25 changes: 10 additions & 15 deletions boards/stm32f4/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ use embassy_stm32::{
Config,
};
use panic_probe as _;
use rmk::{eeprom::EepromStorageConfig, initialize_keyboard_and_run, keymap::KeyMap};
use rmk::{initialize_keyboard_and_run, keymap::KeyMap};
use static_cell::StaticCell;
use vial::{VIAL_KEYBOARD_DEF, VIAL_KEYBOARD_ID};

use crate::keymap::{COL, NUM_LAYER, ROW};

bind_interrupts!(struct Irqs {
OTG_FS => InterruptHandler<USB_OTG_FS>;
});

const FLASH_SECTOR_7_ADDR: u32 = 0x60000;
const EEPROM_SIZE: usize = 128;

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
info!("start!");
info!("Rmk start!");
// RCC config
let config = Config::default();

Expand All @@ -56,25 +56,19 @@ async fn main(_spawner: Spawner) {
// Pin config
let (input_pins, output_pins) = config_matrix_pins_stm32!(peripherals: p, input: [PD9, PD8, PB13, PB12], output: [PE13, PE14, PE15]);

// Use internal flash to emulate eeprom
let f = Flash::new_blocking(p.FLASH);
// Keymap + eeprom config
static MY_KEYMAP: StaticCell<
RefCell<KeyMap<Flash<'_, Blocking>, EEPROM_SIZE, ROW, COL, NUM_LAYER>>,
> = StaticCell::new();
let eeprom_storage_config = EepromStorageConfig {
start_addr: FLASH_SECTOR_7_ADDR,
storage_size: 0x20000, // uses last sector, 128KB for eeprom
page_size: 8,
};
// Use internal flash to emulate eeprom
let f = Flash::new_blocking(p.FLASH);
let keymap = MY_KEYMAP.init(RefCell::new(KeyMap::new(
crate::keymap::KEYMAP,
Some(f),
eeprom_storage_config,
None,
)));

// Initialize all utilities: keyboard, usb and keymap
// Start serving
initialize_keyboard_and_run::<
Driver<'_, USB_OTG_FS>,
Input<'_, AnyPin>,
Expand All @@ -89,7 +83,8 @@ async fn main(_spawner: Spawner) {
input_pins,
output_pins,
keymap,
&vial::VIAL_KEYBOARD_ID,
&vial::VIAL_KEYBOARD_DEF,
).await;
&VIAL_KEYBOARD_ID,
&VIAL_KEYBOARD_DEF,
)
.await;
}
22 changes: 12 additions & 10 deletions boards/stm32h7/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# cargo-features = ["per-package-target"]

[package]
name = "rmk-stm32h7"
version = "0.1.2"
Expand All @@ -11,16 +9,13 @@ readme = "../../README.md"
edition = "2021"
license = "MIT OR Apache-2.0"

# forced-target = "thumbv7em-none-eabihf"

[dependencies]
rmk = { version = "0.1.2", path = "../../rmk", features = [
"eeprom",
"col2row",
] }
cortex-m = { version = "0.7.7", features = ['critical-section-single-core'] }
cortex-m-rt = "0.7.3"
panic-rtt-target = { version = "0.1.2", features = ["cortex-m"] }
embassy-time = { version = "0.3", git = "https://github.com/embassy-rs/embassy", features = [
# "tick-hz-1_000_000",
"tick-hz-32_768",
Expand All @@ -36,16 +31,23 @@ embassy-executor = { version = "0.5.0", git = "https://github.com/embassy-rs/emb
"executor-thread",
"integrated-timers",
] }
rtt-target = "0.5.0"
log = "0.4.19"
packed_struct = { version = "0.10.1", default-features = false }
embedded-storage = { version = "0.3" }
static_cell = { version = "2" }

# rtt-target = "0.5.0"
# panic-rtt-target = { version = "0.1.2", features = ["cortex-m"] }
# log = "0.4.19"

# defmt deps
# defmt = "0.3.5"
# defmt-rtt = "0.4.0"
# panic-probe = "0.3.1"
defmt = "0.3"
defmt-rtt = "0.4"
panic-probe = { version = "0.3", features = ["print-defmt"] }

[build-dependencies]
xz2 = "0.1.7"
json = "0.12"
const-gen = "1.6"

[[bin]]
name = "rmk-stm32h7"
Expand Down
37 changes: 16 additions & 21 deletions boards/stm32h7/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
#[macro_use]
mod macros;
mod keymap;
#[macro_use]
pub mod rtt_logger;
mod vial;
// #[macro_use]
// pub mod rtt_logger;
// use log::info;

use crate::keymap::{COL, NUM_LAYER, ROW};
use core::cell::RefCell;
use defmt::*;
use defmt_rtt as _;
use embassy_executor::Spawner;
use embassy_stm32::{
bind_interrupts,
Expand All @@ -19,25 +23,22 @@ use embassy_stm32::{
usb_otg::{Driver, InterruptHandler},
Config,
};
use log::info;
use panic_rtt_target as _;
use rmk::{eeprom::EepromStorageConfig, initialize_keyboard_and_run, keymap::KeyMap};
use panic_probe as _;
use rmk::{initialize_keyboard_and_run, keymap::KeyMap};
use static_cell::StaticCell;

use crate::keymap::{COL, NUM_LAYER, ROW};
use vial::{VIAL_KEYBOARD_DEF, VIAL_KEYBOARD_ID};

bind_interrupts!(struct Irqs {
OTG_HS => InterruptHandler<USB_OTG_HS>;
});

const FLASH_SECTOR_15_ADDR: u32 = 15 * 8192;
const EEPROM_SIZE: usize = 128;

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
if cfg!(debug_assertions) {
rtt_logger::init(log::LevelFilter::Info);
}
// if cfg!(debug_assertions) {
// rtt_logger::init(log::LevelFilter::Info);
// }
info!("Rmk start!");
// RCC config
let mut config = Config::default();
Expand Down Expand Up @@ -90,21 +91,15 @@ async fn main(_spawner: Spawner) {
// Pin config
let (input_pins, output_pins) = config_matrix_pins_stm32!(peripherals: p, input: [PD9, PD8, PB13, PB12], output: [PE13, PE14, PE15]);

// Use internal flash to emulate eeprom
let f = Flash::new_blocking(p.FLASH);
// Keymap + eeprom config
static MY_KEYMAP: StaticCell<
RefCell<KeyMap<Flash<'_, Blocking>, EEPROM_SIZE, ROW, COL, NUM_LAYER>>,
> = StaticCell::new();
let eeprom_storage_config = EepromStorageConfig {
start_addr: FLASH_SECTOR_15_ADDR,
storage_size: 8192, // uses 8KB for eeprom
page_size: 32,
};
// Use internal flash to emulate eeprom
let f = Flash::new_blocking(p.FLASH);
let keymap = MY_KEYMAP.init(RefCell::new(KeyMap::new(
crate::keymap::KEYMAP,
Some(f),
eeprom_storage_config,
None,
)));

Expand All @@ -123,8 +118,8 @@ async fn main(_spawner: Spawner) {
input_pins,
output_pins,
keymap,
&vial::VIAL_KEYBOARD_ID,
&vial::VIAL_KEYBOARD_DEF,
&VIAL_KEYBOARD_ID,
&VIAL_KEYBOARD_DEF,
)
.await;
}
9 changes: 7 additions & 2 deletions rmk/src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ impl<
pub fn new(
mut action_map: [[[KeyAction; COL]; ROW]; NUM_LAYER],
storage: Option<F>,
eeprom_storage_config: EepromStorageConfig,
eeconfig: Option<Eeconfig>,
) -> KeyMap<F, EEPROM_SIZE, ROW, COL, NUM_LAYER> {
// Initialize the allocator at the very beginning of the initialization of the keymap
Expand All @@ -63,14 +62,20 @@ impl<
// 1KB heap size
const HEAP_SIZE: usize = 512;
// Check page_size and heap size
assert!((eeprom_storage_config.page_size as usize) < HEAP_SIZE);
assert!(F::WRITE_SIZE < HEAP_SIZE);
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }
}

// Initialize eeprom, if success, re-load keymap from it
let eeprom = match storage {
Some(s) => {
// TODO: Refactor Eeprom so that we don't need this anymore
let eeprom_storage_config = EepromStorageConfig {
start_addr: (F::ERASE_SIZE * (s.capacity() / F::ERASE_SIZE - 1)) as u32,
storage_size: F::ERASE_SIZE as u32,
page_size: F::WRITE_SIZE as u32,
};
let e = Eeprom::new(s, eeprom_storage_config, eeconfig, &mut action_map);
// If eeprom is initialized, read keymap from it.
match e {
Expand Down

0 comments on commit da493a0

Please sign in to comment.