Skip to content

doc/guides: add guide for porting new MCUs#22173

Open
fjrdev wants to merge 11 commits intoRIOT-OS:masterfrom
fjrdev:master
Open

doc/guides: add guide for porting new MCUs#22173
fjrdev wants to merge 11 commits intoRIOT-OS:masterfrom
fjrdev:master

Conversation

@fjrdev
Copy link
Copy Markdown

@fjrdev fjrdev commented Apr 2, 2026

Contribution description

This PR features a markdown file describing the process of porting new CPUs into the RIOT ecosystem, since there is currently only a porting guide for boards but not for CPUs. The guide first gives a general overview of the process and then dives into to process of creating the file-structure for a new CPU. A description + code examples are given for the required files. Furthermore, a roadmap is given for the implementing the most important peripherals. This guide was created during the port of the Infineon PSOC-6, which is a new CPU for RIOT. A PR for this will follow in a bit.

This PR adds a new Markdown guide at doc/guides/advanced_tutorials/RIOT_CPU_PORTING.md that documents the process of porting a new CPU to RIOT.

At the moment, RIOT provides guidance for board porting, but there is no CPU porting guide. This contribution is intended to fill that gap by describing the overall CPU porting workflow, the expected file structure, the role of the main CPU-specific files and high-level descriptions for implementing core peripherals (GPIO, UART, Timer, SPI and I2C).

This guide was written during the ongoing port of the Infineon PSoC-6 CPU family to RIOT. The corresponding CPU port itself will be submitted in a separate PR in the near future. It is intended to make future CPU ports easier to structure and understand.

Scope:

  • documentation only
  • no functional code changes

Testing procedure

Issues/PRs references

None

Declaration of AI-Tools / LLMs usage:

AI-Tools / LLMs that were used are:

  • none

@github-actions github-actions Bot added the Area: doc Area: Documentation label Apr 2, 2026
@crasbe crasbe added Type: new feature The issue requests / The PR implemements a new feature for RIOT CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR CI: skip compile test If set, CI server will run only non-compile jobs, but no compile jobs or their dependent jobs labels Apr 3, 2026
@crasbe
Copy link
Copy Markdown
Contributor

crasbe commented Apr 3, 2026

There is a compile error when trying to build the documentation:

make doc-starlight
...

11:06:27 [content] Syncing content
[InvalidContentEntryDataError] docs → advanced_tutorials/porting_cpus data does not match collection schema.

  **: [
  {
    "expected": "string",
    "code": "invalid_type",
    "path": [
      "title"
    ],
    "message": "title: Required"
  }
]

  Hint:
    See https://docs.astro.build/en/guides/content-collections/ for more information on content schemas.
  Error reference:
    https://docs.astro.build/en/reference/errors/invalid-content-entry-data-error/
  Location:
    /home/cbuec/RIOTstuff/riot-vanilla/RIOT/doc/guides/advanced_tutorials/porting_cpus.md:0:0
  Stack trace:
    at getEntryData (/home/cbuec/RIOTstuff/riot-vanilla/RIOT/doc/starlight/node_modules/astro/dist/content/utils.js:123:16)
    at async eval (/home/cbuec/RIOTstuff/riot-vanilla/RIOT/doc/starlight/node_modules/astro/dist/content/loaders/glob.js:211:13)

Copy link
Copy Markdown
Contributor

@crasbe crasbe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding this much needed guide!

The remarks about rearranging the Typical Responsibilities lists is valid for all files, I did not want to add "Same" comments for every occurance.

Once the documentation is buildable, we can perhaps think about using actual Subheadings for the Example: etc "headings" that are currently just in text style.

- documentation and tests that make the port maintainable.

This document focuses on the CPU side. For the board side, also refer to the
existing RIOT guide on porting boards.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if you could add a link to said guide.

Comment thread doc/guides/advanced_tutorials/porting_cpus.md Outdated
└── cpu.c
```

Not every subdirectory is mandatory, but most non-trivial ports will need at
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could add an asterisk to the mandatory or non-mandatory files?

Comment on lines +74 to +79
Typical responsibilities:

- define `MODULE = cpu`,
- include `$(RIOTBASE)/Makefile.base`,
- optionally add subdirectories such as `periph/`, `vectors/`, or a common CPU
family directory.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a bit redundant because that's exactly what's in the code below. I'd probably remove it tbh.

Comment on lines +96 to +99
Typical responsibilities:

- pull in common CPU-family dependency files,
- select required helper modules
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Comment on lines +164 to +172
`cpu.c` contains `cpu_init()`, which performs the minimum hardware and runtime
initialization needed before RIOT starts normal execution.

Typical responsibilities:

- call generic core initialization for the architecture,
- set interrupt priorities and interrupt grouping if required,
- configure clocks or clock assumptions used by the CPU port,
- perform any CPU-local setup that must happen before peripherals are used.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same.

corresponding peripheral implementation.

:::note
Refer to the Technical Reference Manual of the target CPU for details on implementing the boot process.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Refer to the Technical Reference Manual of the target CPU for details on implementing the boot process.
Refer to the Technical Reference Manual of the target CPU for details on
implementing the boot process.

Long line.

### Reuse common code

If the MCU belongs to an already supported architecture or family, reuse the
existing common code where possible. For this case, a base directory (e.g. `cpu/<CPU_FAM>_common`) should be created that contains all shared/common configurations. This drastically reduces maintenance and lowers the
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long line.


### Refer to the manual of the target CPU

Many CPUs are shipped with detailed manuals containing register-level information about peripherals, clock trees, the boot sequence, interrupt handling, and more. This information should be read, understood, and then mapped to the RIOT framework.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long line.

Comment thread doc/guides/advanced_tutorials/porting_cpus.mdx
@crasbe crasbe changed the title add guide for porting new MCUs doc/guides: add guide for porting new MCUs Apr 3, 2026
@riot-ci
Copy link
Copy Markdown

riot-ci commented Apr 3, 2026

Murdock results

✔️ PASSED

f6c6376 doc: resolved build issues

Success Failures Total Runtime
1 0 1 03m:54s

Artifacts

@github-actions github-actions Bot added Area: tests Area: tests and testing framework Area: build system Area: Build system Area: drivers Area: Device drivers Area: boards Area: Board ports Area: cpu Area: CPU/MCU ports Area: Kconfig Area: Kconfig integration labels Apr 7, 2026
@crasbe
Copy link
Copy Markdown
Contributor

crasbe commented Apr 7, 2026

I think the last two commits were not meant to be added here? 👀

@fjrdev
Copy link
Copy Markdown
Author

fjrdev commented Apr 7, 2026

I think the last two commits were not meant to be added here? 👀

Oh sorry. you are right! Will fix it + your suggestions asap!

@github-actions github-actions Bot removed Area: tests Area: tests and testing framework Area: build system Area: Build system Area: drivers Area: Device drivers Area: boards Area: Board ports Area: cpu Area: CPU/MCU ports Area: Kconfig Area: Kconfig integration labels Apr 7, 2026
@crasbe
Copy link
Copy Markdown
Contributor

crasbe commented Apr 7, 2026

To avoid this kind of issue, it is usually a good idea to create a separate branch for a pull request and not use the master branch. Every change to your fork's master branch is automatically reflected to the PR.

Unfortunately it is not possible to change the branch after opening the PR AFAIK.

Comment thread doc/guides/advanced_tutorials/porting_cpus.md Outdated
@fjrdev
Copy link
Copy Markdown
Author

fjrdev commented Apr 7, 2026

@crasbe Thanks a lot for reviewing! I have added your feedback in the latest version. I will add subheading to the corresponding sections. Can we use # levels here as well?

@crasbe
Copy link
Copy Markdown
Contributor

crasbe commented Apr 7, 2026

@AnnsAnns: as our recently graduated CPU-porting expert, would you like to take a look at this too? 👁️👁️

@crasbe
Copy link
Copy Markdown
Contributor

crasbe commented Apr 7, 2026

You'll have to add the guide to the Astro configuration file, otherwise it won't show up in the nagivation side bar:

diff --git a/doc/starlight/astro.config.ts b/doc/starlight/astro.config.ts
index 1d6bf1f3d4..06a48f592b 100644
--- a/doc/starlight/astro.config.ts
+++ b/doc/starlight/astro.config.ts
@@ -142,6 +142,7 @@ export default defineConfig({
                 "advanced_tutorials/unittests",
                 "advanced_tutorials/device_drivers",
                 "advanced_tutorials/porting_boards",
+                "advanced_tutorials/porting_cpus",
                 "advanced_tutorials/event_queue",
               ],
             },

@AnnsAnns
Copy link
Copy Markdown
Member

AnnsAnns commented Apr 7, 2026

For sure will take a look, I was really happy to hear that somebody was working on this when crasbe mentioned this as I wanted to also do something like this, thank you for this PR 🫡

Copy link
Copy Markdown
Contributor

@crasbe crasbe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that the documentation can be built in Starlight, I gave this a more thorough review. Nothing major, just some minor improvements here and there :)

A practical CPU porting workflow usually looks like this:

1. create the CPU directory and build-system files,
2. add startup support and a working `cpu_init()`,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
2. add startup support and a working `cpu_init()`,
2. add startup support and a working `cpu_init()` function,


## Build System Files

### `Makefile`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the Code formatting, our current style file does not respect the sublevel headings:

Image

@AnnsAnns @LasseRosenow is that something that you'd want to fix in the style file or should the use of code formatting just be avoided for sublevel headlines?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd personally say just avoid formatting in titles

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd personally say just avoid formatting in titles

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd personally say just avoid formatting in titles

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'd personally say just avoid formatting in titles? Did I understand that correctly? 🤪

Copy link
Copy Markdown
Member

@AnnsAnns AnnsAnns Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most normal Github behavior, how is this the default git forge but it can't handle weird internet connections, I was legit just in an ICE on train wifi 😭

### `Makefile`

The top-level CPU `Makefile` defines the CPU module and may include common
subdirectories that are shared with other CPUs.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
subdirectories that are shared with other CPUs.
subdirectories that are shared with other CPUs. `$(RIOTCPU)` is the `cpu/` subdirectory
of the RIOT basefolder `$(RIOTBASE)`.


This file declares what the CPU provides to the rest of RIOT by defining the
CPU architecture, including common family feature files as well as defining
the implemented peripherals with `FEATURES_PROVIDED`.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
the implemented peripherals with `FEATURES_PROVIDED`.
the implemented peripherals with `FEATURES_PROVIDED` in alphabetical order.


### `Makefile.include`

This file contains low-level build configuration for the CPU.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This file contains low-level build configuration for the CPU.
This file contains low-level build configuration and additional compiler and
linker search paths for the CPU.

Comment on lines +501 to +510
### Generic high-level shape

```c
spi_init(bus);
spi_init_cs(bus, cs);

spi_acquire(bus, cs, SPI_MODE_0, SPI_CLK_1MHZ);
spi_transfer_bytes(bus, cs, false, tx_buf, rx_buf, len);
spi_release(bus);
```
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be application/device driver level though.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it too specific? Thought this would be nice as an addition to the bullet points above this snippet

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps you could specify that this is not part of the CPU code but just given as an example to clarify how the functions will be called on the application/driver level.


- boot to `cpu_init()`,
- confirm the vector table works,
- get `examples/basic/hello-world` running over UART,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably include something like "light up an LED at different stages of the code if you don't observe output on the UART". That often helped me in the past.

### Reuse common code

If the MCU belongs to an already supported architecture or family, reuse the
existing common code where possible. For this case, a base directory
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
existing common code where possible. For this case, a base directory
existing common code where possible. In this case, a base directory

existing common code where possible. For this case, a base directory
(e.g. `cpu/<CPU_FAM>_common`) should be created that contains all shared/common
configurations. This drastically reduces maintenance and lowers the risk of
subtle startup or interrupt bugs.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
subtle startup or interrupt bugs.
subtle startup or interrupt bugs, as well as code divergation issues in the future.

Correct handling of repeated starts, ACK/NACK conditions, and bus-busy recovery
is more important than adding optional optimizations early.

## General Recommendations
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this chapter wouldn't have a better home further on top. Currently this is a repetition of many things that were already explained in greater detail 🤔

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put this chapter at the end to wrap-up the sections explaining the different directories/files. Moving it at the top would put the mentioning of some concepts/files above the explanation wouldn't it? maybe im missing your point

Copy link
Copy Markdown
Member

@AnnsAnns AnnsAnns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either the wifi is too spotty or github is having problems so I'll have to halt my review for now since I'm getting a lot of errors, so far this was a nice read though 👍

Comment thread doc/guides/advanced_tutorials/porting_cpus.md Outdated
Comment thread doc/guides/advanced_tutorials/porting_cpus.md Outdated
Comment thread doc/guides/advanced_tutorials/porting_cpus.md Outdated

## Build System Files

### `Makefile`
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd personally say just avoid formatting in titles


## Build System Files

### `Makefile`
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd personally say just avoid formatting in titles

Comment thread doc/guides/advanced_tutorials/porting_cpus.md Outdated
Comment thread doc/guides/advanced_tutorials/porting_cpus.md Outdated
Comment thread doc/guides/advanced_tutorials/porting_cpus.md Outdated
Comment thread doc/guides/advanced_tutorials/porting_cpus.md Outdated
Comment thread doc/guides/advanced_tutorials/porting_cpus.md Outdated
Comment thread doc/starlight/astro.config.ts Outdated
@fjrdev
Copy link
Copy Markdown
Author

fjrdev commented Apr 20, 2026

@crasbe @AnnsAnns Thanks a lot for the helpful feedback and your time! I have included your suggestions in the guide :)

Copy link
Copy Markdown
Member

@AnnsAnns AnnsAnns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read through it again, looks good for the most part, just one minor nitpick from my side left

Comment thread doc/guides/advanced_tutorials/porting_cpus.md Outdated
Comment thread doc/guides/advanced_tutorials/porting_cpus.mdx
Comment on lines +35 to +54
{% filetree %}
- cpu/foo
- include
- cpu_conf.h
- periph_cpu.h
- vendor*
- ldscripts*
- periph*
- gpio.c
- i2c.c
- spi.c
- …
- vectors
- vectors_<model>.c
- Makefile
- Makefile.dep*
- Makefile.features*
- Makefile.include
- cpu.c
{% /filetree %}
Copy link
Copy Markdown
Member

@AnnsAnns AnnsAnns Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{% filetree %}
- cpu/foo
- include
- cpu_conf.h
- periph_cpu.h
- vendor*
- ldscripts*
- periph*
- gpio.c
- i2c.c
- spi.c
- …
- vectors
- vectors_<model>.c
- Makefile
- Makefile.dep*
- Makefile.features*
- Makefile.include
- cpu.c
{% /filetree %}
<FileTree>
- cpu/foo
- include
- cpu_conf.h
- periph_cpu.h
- vendor*
- ldscripts*
- periph*
- gpio.c
- i2c.c
- spi.c
- …
- vectors
- vectors_<model>.c
- Makefile
- Makefile.dep*
- Makefile.features*
- Makefile.include
- cpu.c
</FileTree>

That syntax is for Markdoc, we use MDX. To use this component rename the file to "porting_cpus.mdx" and also import import { FileTree } from '@astrojs/starlight/components'; at the top of the file. See: https://guide.riot-os.org/misc/how_to_doc/ and https://starlight.astro.build/components/file-tree/ for the component itself

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pointing this out! I updated to mdx in the last commit.

Copy link
Copy Markdown
Contributor

@crasbe crasbe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You know that you can run make doc-starlight locally to check if it works, right? 👀

- architecture-common code such as `cpu/cortexm_common/` when porting
Cortex-M-based MCUs
- merged RIOT PRs adding CPU support, for reference and pattern matching:
<https://github.com/RIOT-OS/RIOT/pulls?q=is%3Apr+is%3Amerged+%22cpu%2F%22+%22boards%2F%22>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<https://github.com/RIOT-OS/RIOT/pulls?q=is%3Apr+is%3Amerged+%22cpu%2F%22+%22boards%2F%22>
[see GitHub](https://github.com/RIOT-OS/RIOT/pulls?q=is%3Apr+is%3Amerged+%22cpu%2F%22+%22boards%2F%22)

To fix the build error:

11:07:49 [ERROR] Unexpected character `/` (U+002F) before local name, expected a character that can start a name, such as a letter, `$`, or `_` (note: to create a link in MDX, use `[text](url)`)
  Stack trace:
    at /home/cbuec/RIOTstuff/riot-vanillaice/RIOT/doc/guides/advanced_tutorials/porting_cpus.mdx:650:10
    [...] See full stack trace in the browser, or rerun with --verbose.

description: Guide on how to port new CPU families to RIOT-OS
---

import { FileTree } from '@astrojs/starlight/components';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { FileTree } from '@astrojs/starlight/components';
import FileTree from '@components/FileTree.astro';

To fix the next occurring issue:

11:11:12 [ERROR] Cannot find module '@astrojs/starlight/components' imported from '/home/cbuec/RIOTstuff/riot-vanillaice/RIOT/doc/guides/advanced_tutorials/porting_cpus.mdx'
  Stack trace:
    at fetchModule (file:///home/cbuec/RIOTstuff/riot-vanillaice/RIOT/doc/starlight/node_modules/vite/dist/node/chunks/config.js:33980:34)
    [...] See full stack trace in the browser, or rerun with --verbose.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just saw that I recommended this line. Sorry for suggesting something incorrect 🫨

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it was @AnnsAnns, not me 👀

- spi.c
- …
- vectors
- vectors_<model>.c
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- vectors_<model>.c
- vectors_\<model\>.c

To fix the next error:

11:10:05 [ERROR] Expected a closing tag for `<model>` (50:15-50:22) before the end of `paragraph`
  Stack trace:
    at /home/cbuec/RIOTstuff/riot-vanillaice/RIOT/doc/guides/advanced_tutorials/porting_cpus.mdx:50:7
    [...] See full stack trace in the browser, or rerun with --verbose.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestions! I have resolved the build issues in the newest commit

@AnnsAnns
Copy link
Copy Markdown
Member

I'd recommend looking at https://guide.riot-os.org/misc/how_to_doc/#how-to-run-the-site-locally if something is not working, otherwise feel free to reach out for help

@fjrdev
Copy link
Copy Markdown
Author

fjrdev commented May 4, 2026

You know that you can run make doc-starlight locally to check if it works, right? 👀

Didn't know that, thanks for pointing out! :D

Copy link
Copy Markdown
Contributor

@crasbe crasbe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this is good to go now. Thank you!

@AnnsAnns what do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: doc Area: Documentation CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR CI: skip compile test If set, CI server will run only non-compile jobs, but no compile jobs or their dependent jobs Type: new feature The issue requests / The PR implemements a new feature for RIOT

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants