Lokey

MCUs

An MCU in Lokey represents the microcontroller your firmware runs on.

At the type level, an MCU is defined by implementing the Mcu trait, plus the McuCreator trait which initializes the MCU together with its storage backend.

Responsibilities

An MCU type defines the setup of a microcontroller:

  • Configuration type: The MCU-specific config type.
  • Initialization: How to create and initialize the MCU and its storage backend.
  • Background tasks: Optional MCU-level background work, typically running for the device lifetime.

These are split across two traits:

  • Mcu holds the configuration type and the background tasks.
  • McuCreator holds the initialization logic and the storage type.

MCU selection

The concrete MCU used by your firmware is chosen by the device type. In the Device implementation, the type Mcu = ... associated type binds that device to a specific Mcu implementation.

impl Device for MyDevice {
	type Mcu = Nrf;
	// ...
}

Example

use lokey::storage::{EmptyStorage, EmptyStorageConfig};
use lokey::{Address, AnyState, Context, Device, Mcu, McuCreator};

pub struct MyMcu;

impl Mcu for MyMcu {
	type Config = (); // Define the MCU-specific configuration type

	async fn run<D, S>(context: Context<D, S>)
	where
		D: Device<Mcu = Self>,
		S: AnyState,
	{
		// Run MCU background tasks here (if needed)
	}
}

impl McuCreator<EmptyStorageConfig> for MyMcu {
	type Storage = EmptyStorage;

	async fn create(
		mcu_config: Self::Config,
		address: Address,
		storage_config: EmptyStorageConfig,
	) -> (Self, Self::Storage) {
		// Perform MCU initialization
		let mcu = Self;

		// Create storage
		let storage = EmptyStorage;

		(mcu, storage)
	}
}

On this page