Skip to content

Commit

Permalink
introduce mem pool and heap
Browse files Browse the repository at this point in the history
  • Loading branch information
robamu committed May 21, 2024
1 parent e9b748e commit 1079445
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 16 deletions.
4 changes: 3 additions & 1 deletion embedded-examples/stm32h7-rtic/.cargo/def_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ rustflags = [
# This is needed if your flash or ram addresses are not aligned to 0x10000 in memory.x
# See https://github.com/rust-embedded/cortex-m-quickstart/pull/95
"-C", "link-arg=--nmagic",
# Can be useful for debugging.
# "-Clink-args=-Map=app.map"
]

[build]
Expand All @@ -24,4 +26,4 @@ rb = "run --bin"
rrb = "run --release --bin"

[env]
DEFMT_LOG = "info"
DEFMT_LOG = "info"
1 change: 1 addition & 0 deletions embedded-examples/stm32h7-rtic/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
/.cargo/config*
/.vscode
/app.map
93 changes: 85 additions & 8 deletions embedded-examples/stm32h7-rtic/Cargo.lock

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

5 changes: 3 additions & 2 deletions embedded-examples/stm32h7-rtic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ defmt-brtt = { version = "0.1", default-features = false, features = ["rtt"] }
panic-probe = { version = "0.3", features = ["print-defmt"] }
cortex-m-semihosting = "0.5.0"
stm32h7xx-hal = { version="0.16", features= ["stm32h743v", "ethernet"] }
embedded-alloc = "0.5"

[dependencies.smoltcp]
version = "0.11.0"
Expand All @@ -36,10 +37,10 @@ version = "1"
features = ["cortex-m-systick"]

[dependencies.satrs]
# path = "satrs"
path = "../../satrs"
version = "0.2"
default-features = false
features = ["defmt"]
features = ["defmt", "heapless"]

[dev-dependencies]
defmt-test = "0.3"
Expand Down
81 changes: 77 additions & 4 deletions embedded-examples/stm32h7-rtic/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
#![no_main]
#![no_std]
extern crate alloc;

use rtic::app;
use rtic_monotonics::systick::Systick;
use rtic_monotonics::Monotonic;
use satrs::pool::StaticHeaplessMemoryPool;
use satrs::static_subpool;
// global logger + panicking-behavior + memory layout
use satrs_stm32h7_nucleo_rtic as _;
use smoltcp::socket::{dhcpv4, udp}; // global logger + panicking-behavior + memory layout
use smoltcp::socket::{dhcpv4, udp};

use core::mem::MaybeUninit;
use embedded_alloc::Heap;
use smoltcp::iface::{Config, Interface, SocketHandle, SocketSet, SocketStorage};
use smoltcp::wire::{HardwareAddress, IpAddress, IpCidr};
use stm32h7xx_hal::ethernet;

const DEFAULT_BLINK_FREQ_MS: u32 = 1000;
const PORT: u16 = 7301;

/// Ethernet descriptor rings are a global singleton
#[link_section = ".sram3.eth"]
static mut DES_RING: MaybeUninit<ethernet::DesRing<4, 4>> = MaybeUninit::uninit();
const HEAP_SIZE: usize = 131_072;

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

// We place the memory pool buffers inside the larger AXISRAM.
pub const SUBPOOL_SMALL_NUM_BLOCKS: u16 = 32;
pub const SUBPOOL_SMALL_BLOCK_SIZE: usize = 32;
pub const SUBPOOL_MEDIUM_NUM_BLOCKS: u16 = 16;
pub const SUBPOOL_MEDIUM_BLOCK_SIZE: usize = 128;
pub const SUBPOOL_LARGE_NUM_BLOCKS: u16 = 8;
pub const SUBPOOL_LARGE_BLOCK_SIZE: usize = 2048;

// This data will be held by Net through a mutable reference
pub struct NetStorageStatic<'a> {
Expand Down Expand Up @@ -55,6 +69,7 @@ impl<'a> Net<'a> {
&mut ethdev,
smoltcp::time::Instant::from_millis((Systick::now() - Systick::ZERO).to_millis()),
);
// SAFETY: The RX and TX buffers are passed here and not used anywhere else.
let udp_rx_buffer =
smoltcp::socket::udp::PacketBuffer::new(unsafe { &mut UDP_RX_META[..] }, unsafe {
&mut UDP_RX[..]
Expand Down Expand Up @@ -251,6 +266,11 @@ mod app {
let rmii_txd1 = gpiob.pb13.into_alternate::<11>();

let mac_addr = smoltcp::wire::EthernetAddress::from_bytes(&MAC_ADDRESS);

/// Ethernet descriptor rings are a global singleton
#[link_section = ".sram3.eth"]
static mut DES_RING: MaybeUninit<ethernet::DesRing<4, 4>> = MaybeUninit::uninit();

let (eth_dma, eth_mac) = ethernet::new(
cx.device.ETHERNET_MAC,
cx.device.ETHERNET_MTL,
Expand Down Expand Up @@ -300,6 +320,59 @@ mod app {

let net = Net::new(store, eth_dma, mac_addr.into());

let mut heapless_pool: StaticHeaplessMemoryPool<3> = StaticHeaplessMemoryPool::new(true);
static_subpool!(
SUBPOOL_SMALL,
SUBPOOL_SMALL_SIZES,
SUBPOOL_SMALL_NUM_BLOCKS as usize,
SUBPOOL_SMALL_BLOCK_SIZE,
link_section = ".axisram"
);
static_subpool!(
SUBPOOL_MEDIUM,
SUBPOOL_MEDIUM_SIZES,
SUBPOOL_MEDIUM_NUM_BLOCKS as usize,
SUBPOOL_MEDIUM_BLOCK_SIZE,
link_section = ".axisram"
);
static_subpool!(
SUBPOOL_LARGE,
SUBPOOL_LARGE_SIZES,
SUBPOOL_LARGE_NUM_BLOCKS as usize,
SUBPOOL_LARGE_BLOCK_SIZE,
link_section = ".axisram"
);

heapless_pool
.grow(
unsafe { SUBPOOL_SMALL.assume_init_mut() },
unsafe { SUBPOOL_SMALL_SIZES.assume_init_mut() },
SUBPOOL_SMALL_NUM_BLOCKS,
false,
)
.expect("growing heapless memory pool failed");
heapless_pool
.grow(
unsafe { SUBPOOL_MEDIUM.assume_init_mut() },
unsafe { SUBPOOL_MEDIUM_SIZES.assume_init_mut() },
SUBPOOL_MEDIUM_NUM_BLOCKS,
false,
)
.expect("growing heapless memory pool failed");
heapless_pool
.grow(
unsafe { SUBPOOL_LARGE.assume_init_mut() },
unsafe { SUBPOOL_LARGE_SIZES.assume_init_mut() },
SUBPOOL_LARGE_NUM_BLOCKS,
false,
)
.expect("growing heapless memory pool failed");

// Set up global allocator. Use AXISRAM for the heap.
#[link_section = ".axisram"]
static mut HEAP_MEM: [MaybeUninit<u8>; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE];
unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) }

eth_link_check::spawn().expect("eth link check failed");
blink::spawn().expect("spawning blink task failed");
(
Expand Down
2 changes: 1 addition & 1 deletion embedded-examples/stm32h7-rtic/vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"chip": "STM32H743ZITx",
"coreConfigs": [
{
"programBinary": "${workspaceFolder}/target/thumbv7em-none-eabihf/debug/satrs-stm32h7-rtic",
"programBinary": "${workspaceFolder}/target/thumbv7em-none-eabihf/debug/satrs-stm32h7-nucleo-rtic",
"rttEnabled": true,
"svdFile": "STM32H743.svd"
}
Expand Down
8 changes: 8 additions & 0 deletions satrs/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,14 @@ pub mod heapless_mod {
static mut $sizes_list_name: core::mem::MaybeUninit<[usize; $num_blocks]> =
core::mem::MaybeUninit::new([satrs::pool::STORE_FREE; $num_blocks]);
};
($pool_name: ident, $sizes_list_name: ident, $num_blocks: expr, $block_size: expr, $meta_data: meta) => {
#[$meta_data]
static mut $pool_name: core::mem::MaybeUninit<[u8; $num_blocks * $block_size]> =
core::mem::MaybeUninit::new([0; $num_blocks * $block_size]);
#[$meta_data]
static mut $sizes_list_name: core::mem::MaybeUninit<[usize; $num_blocks]> =
core::mem::MaybeUninit::new([satrs::pool::STORE_FREE; $num_blocks]);
};
}

/// A static memory pool similar to [super::StaticMemoryPool] which does not reply on
Expand Down

0 comments on commit 1079445

Please sign in to comment.