Skip to content

Commit

Permalink
feat(core): add heap, use it for eeprom write buf
Browse files Browse the repository at this point in the history
Signed-off-by: Haobo Gu <[email protected]>
  • Loading branch information
HaoboGu committed Oct 27, 2023
1 parent ae07f60 commit d38d161
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rmk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ usb-device = "0.2.9"
usbd-hid = "0.6.1"
packed_struct = { version = "0.10.1", default-features = false }
byteorder = { version = "1.4", default-features = false }
embedded-alloc = "0.5.0"

[features]
default = ["col2row"]
Expand Down
7 changes: 5 additions & 2 deletions rmk/src/eeprom.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
pub mod eeconfig;
pub mod eekeymap;

extern crate alloc;

use self::eeconfig::EEPROM_MAGIC;
use crate::{action::KeyAction, keymap::KeyMapConfig};
use alloc::vec;
use core::sync::atomic::{AtomicBool, Ordering::SeqCst};
use embedded_storage::nor_flash::NorFlash;
use log::{debug, error, info, warn};
Expand Down Expand Up @@ -65,6 +68,7 @@ pub struct Eeprom<F: NorFlash, const EEPROM_SIZE: usize> {
/// EEPROM_SIZE should be at least 1008(keymap) + 15(eeconfig) + 100(macro)
keymap_config: KeyMapConfig,
}

impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
pub fn new<const ROW: usize, const COL: usize, const NUM_LAYER: usize>(
storage: F,
Expand Down Expand Up @@ -223,8 +227,7 @@ impl<F: NorFlash, const EEPROM_SIZE: usize> Eeprom<F, EEPROM_SIZE> {
}

let bytes = record.to_bytes();
// TODO: Use a buf whose length equals storage_write_size
let mut buf = [0xFF_u8; 16];
let mut buf = vec![0xFF_u8; self.storage_config.page_size as usize];
buf[..bytes.len()].copy_from_slice(&bytes);
debug!(
"EEPROM write storage at 0x{:X}: {:02X?} ",
Expand Down
15 changes: 15 additions & 0 deletions rmk/src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ use crate::{
via::{descriptor::ViaReport, process::process_via_packet},
};
use core::convert::Infallible;
use embedded_alloc::Heap;
use embedded_hal::digital::v2::{InputPin, OutputPin};
use embedded_storage::nor_flash::NorFlash;
use log::debug;
use rtic_monotonics::systick::*;
use usb_device::class_prelude::UsbBus;
use usbd_hid::descriptor::{KeyboardReport, MediaKeyboardReport, SystemControlReport};

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

pub struct Keyboard<
In: InputPin,
Out: OutputPin,
Expand Down Expand Up @@ -75,6 +79,17 @@ impl<
eeprom_storage_config: EepromStorageConfig,
mut keymap: [[[KeyAction; COL]; ROW]; NUM_LAYER],
) -> Self {
// Initialize the allocator at the very beginning of the initialization of the keyboard
{
use core::mem::MaybeUninit;
// 1KB heap size
const HEAP_SIZE: usize = 1024;
// Check page_size and heap size
assert!((eeprom_storage_config.page_size as usize) < 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) }
}

let eeprom = match storage {
Some(s) => {
let e = Eeprom::new(s, eeprom_storage_config, &keymap);
Expand Down

0 comments on commit d38d161

Please sign in to comment.