Embedded Controller
This section covers how to get started developing and testing with the virtual Embedded Controller (EC).
What this is
ODP provides a fully virtual embedded controller modeled on a QEMU 32-bit RISC-V machine. This also includes, in addition to the virtual UART bus provided by QEMU, a virtual I2C bus and virtual GPIOs.
These virtual peripherals present a memory-mapped register interface to the EC, just like real hardware, but send data over a socket acting as the virtual wire.
Along with a host OS running inside an ARM virt or x64 Q35 QEMU instance, this enables a completely virtual end-to-end development environment. ARM supports ODP ACPI services and HID-over-I2C; Q35 supports direct Windows user-mode serial access to the EC.
What this is not
The intent of this virtual EC is to aid in the development and testing of application logic sitting on top of hardware.
For example, a properly designed thermal management system in Rust can be written largely independently of the underlying hardware (as in, it shouldn't necessarily depend on the architecture of the microcontroller, specific sensors, which bus they use, etc.).
Likewise, logic for communicating with the host over a transport such as HIDI2C shouldn't necessarily be dependent on details of the specific underlying I2C peripheral as those details are abstracted away through generic traits.
Thus, this virtual EC is NOT intended to emulate the specific hardware quirks or timing details of any specific platform. For such a use case, tools like Renode would be more appropriate.
How to use it
First, check out QEMU setup for the basics of getting started with QEMU.
ARM virt
Once you have the repo cloned and the devcontainer set up, you can verify host-to-EC communications by following these steps:
-
Run
make allto build UEFI firmware and secure services. -
Run
make run_ecwhich will build thedev-qemuplatform in themod/ecfolder (theodp-embedded-controllersubmodule) and start a headless QEMU instance running the firmware on the RISC-Vecmachine while displaying logs. -
In a separate terminal, run
make run_os QEMU_DISPLAY=gtkwhich will download our bundled WinVOS image containing all drivers and EC applications needed to interface with the EC, then boot it in an ARMvirtQEMU instance running in a display window (alternatively, if you prefer connecting remotely via VNC,make run_osalone will start a headless instance with no local display window). This QEMU instance will automatically connect to the EC QEMU instance via sockets for virtual bus communications. -
In the WinVOS terminal, run
cd C:\ectest. -
Run
ec-test-tui.exe --source acpi(which ultimately goes through the virtual serial/UART bus). If everything is working correctly, you should see the TUI app open with an overview of EC subsystems with a green*indicating successful communication (don't worry if the data seems nonsensical; that is because some subsystems currently provide more realistic mock data than others):
-
To verify virtual I2C/GPIO communication, run
ec-test-cli.exe --source windows rtc get-real-time(currently, only the time-alarm service supports HIDI2C), which goes through a custom Windows driver that ultimately talks HID over I2C. If successful, you should receive output that looks like:
x86_64 Q35
You can also talk to the EC from an x86_64 Q35 host, but currently only over the virtual UART bus. Unlike ARM, this exposes a COM port directly to Windows, so the apps use --source serial instead of --source acpi:
- Run
make all ARCH=x86_64to build the Q35 UEFI firmware. - Run
make run_ecto build and start the same RISC-V EC instance used with ARM. - In a separate terminal, run
make run_os ARCH=x86_64 QEMU_DISPLAY=gtkto boot WinVOS in a display window (alternatively, usemake run_os ARCH=x86_64for VNC). This connects the EC's virtual UART to COM3 in Windows. - In the WinVOS terminal, run
cd C:\ectest. - Run
ec-test-tui.exe --source serial --port COM3. If everything is working correctly, you should see the TUI app open with thermal, battery, and time-alarm data from the EC. - To test an individual service, run a CLI command such as
ec-test-cli.exe --source serial --port COM3 rtc get-real-time. Close the TUI first, since only one app can use the COM port at a time.
Virtual I2C/GPIO communication is not yet supported on Q35.
If everything is working, then you have successfully set up your environment and can begin developing on your own. The best way to start is by navigating to mod/ec/platform/dev-qemu/src to see the current, basic reference implementation provided by ODP and start tweaking it to suit your needs. Running make run_ec again will build and launch your changes, and you can continue using the test apps running in the host QEMU instance to test your changes.
While the nitty-gritty details of how this all works under the hood aren't required for development, if you're curious, continue reading below.
How it works
At its core is a modified QEMU binary which adds a few nice features:
- An emulated I2C target peripheral
- An emulated I2C controller peripheral
- An emulated GPIO peripheral
- A new RISC-V
ecmachine which wires up the I2C target and GPIO peripherals as memory-mapped registers - A modified ARM
virtmachine which additionally wires up the I2C controller and GPIO peripherals as memory-mapped registers
The important thing to note is that the peripherals expose a socket allowing two QEMU instances or any other external program to connect. This is what enables the QEMU ARM host machine to connect its virtual I2C controller peripheral to the QEMU EC machine's virtual I2C target peripheral. This mirrors how the existing virtual UART peripherals in QEMU work.
Q35 uses QEMU's existing ISA UART model and the EC UART socket, without changes to QEMU or the EC firmware. Its I2C/GPIO connections remain unsupported.
Fortunately, knowing the nitty-gritty of how this works isn't required if you just want to get started developing your EC application logic. For those who are curious or need to modify QEMU themselves, the relevant repos and details are included below:
- odp-qemu-builder: This is where ODP modifications to QEMU live as they are not yet upstreamed. The patch for I2C and GPIO emulation can be found here. As mentioned, this is responsible for implementing the new peripherals and wiring them up into the respective machines.
- qemu-riscv-rs: This repo contains a peripheral access crate (PAC) and Embassy hardware abstraction layer (HAL) targeting the QEMU
ecmachine. The PAC and HAL hide the specific memory-mapped registers of the virtual peripherals, so knowledge of them is not required for writing application code. However, if one needs to modify the register map presented by the QEMUecmachine, then the SVD file MUST be updated as well since this is the source of truth from which the PAC is generated and the HAL is built upon. - odp-embedded-controller: This contains reference EC implementations for several platforms supported by ODP, including, of course dev-qemu. This is the primary repo that EC firmware developers will need to concern themselves with as this is where application logic lives. Currently, the
dev-qemuplatform contains mock/virtual sensors, fans, batteries, and a real-time clock (RTC) to demonstrate the ODP embedded-services. Additionally, the MCTP/UART service and HIDI2C time-alarm service are implemented over the virtual bus peripherals to demonstrate communication with a host. - odp-platform-common: The TUI and CLI apps that interface with the EC from the host OS live here. The apps are designed so that the underlying "source" (or transport) is configurable at runtime depending on the platform (for example, direct serial/UART, ACPI, Windows class interface, etc.). These are great tools to test host-to-EC communications end-to-end.