Closed4

ZephyrRTOSを試す

gotoooogotoooo

開発環境

  • Windows10
  • VSCode
  • PlatformIO

※Windows10上にWSLでUbuntuをインストールし、その上でZepherの開発環境を構築する方法もある。
ただしUbuntuでビルドしたhexファイルを直接マイコンに書き込むことが難しいため、Windowsのみで完結する構成でトライすることにした。

使用ボード

  • NucleoF401RET6 Rev.C01 <= このRev.が後の罠の原因となる

参考:
https://jhalfmoon.com/dbc/2022/05/06/モダンosのお砂場45-zephyr、抽象度?高っ!gpioをトグルす/

https://qiita.com/yagshi/items/0e1f31036c9ef430a99a

https://docs.platformio.org/en/stable/frameworks/zephyr.html

gotoooogotoooo

準備

  1. VSCode インストール(筆者環境ではインストール済み)
  2. VSCode拡張機能としてPlatformIOインストール(筆者環境ではインストール済み)
  3. PlatformIOでNewProject

    ※初回のプロジェクト作成時にZephyr一式をダウンロードするため数十分待つ必要あり
gotoooogotoooo

Lチカ Blinky

※サンプルをビルドしてマイコンに書き込むだけと思いきや、NucleoのRev.が古いようでクロック設定をカスタマイズする必要があり、それに気づくのにかなり苦労した。

参考:https://github.com/zephyrproject-rtos/zephyr/issues/50440

Blinkyのサンプルコード一式は下記ディレクトリに格納されている。
※[User]は各PC環境によって異なる。

[C:\Users[User].platformio\packages\framework-zephyr\samples\basic\blinky\src]

  1. main.cの編集
main.c
main.c
/*
 * Copyright (c) 2016 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <stdio.h>
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>

/* 1000 msec = 1 sec */
#define SLEEP_TIME_MS   1000

/* The devicetree node identifier for the "led0" alias. */
#define LED0_NODE DT_ALIAS(led0)

/*
 * A build error on this line means your board is unsupported.
 * See the sample documentation for information on how to fix this.
 */
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);

int main(void)
{
	int ret;
	bool led_state = true;

	if (!gpio_is_ready_dt(&led)) {
		return 0;
	}

	ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
	if (ret < 0) {
		return 0;
	}

	while (1) {
		ret = gpio_pin_toggle_dt(&led);
		if (ret < 0) {
			return 0;
		}

		led_state = !led_state;
		printf("LED state: %s\n", led_state ? "ON" : "OFF");
		k_msleep(SLEEP_TIME_MS);
	}
	return 0;
}

  1. prj.confの編集
prj.conf
CONFIG_GPIO=y
  1. DeviceTreeのオーバーレイファイルを作成する
    ※NucleoボードのRevがC01の場合は必須。それ以外は多分不要。

作成した[nucleo_f401re.overlay]はプロジェクトフォルダにあるzephyrフォルダに収める

nucleo_f401re.overlay

&clk_lsi {
	status = "okay";
};

&clk_hse {
	status = "disabled";
};

&pll {
	div-m = <16>;
	clocks = <&clk_hsi>;
};

&rcc {
	clocks = <&pll>;
	clock-frequency = <DT_FREQ_M(84)>;
	ahb-prescaler = <1>;
	apb1-prescaler = <2>;
	apb2-prescaler = <1>;
};
  1. CMakeLists.txtを変更する
    ※NucleoボードのRevがC01の場合は必須。それ以外は多分デフォルトのままで問題なし。

set(DTC_OVERLAY_FILE "nucleo_f401re.overlay") を追記

CMakeList.txt
cmake_minimum_required(VERSION 3.13.1)
set(DTC_OVERLAY_FILE "nucleo_f401re.overlay")
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(nucleof401re_zephyr)

FILE(GLOB app_sources ../src/*.c*)
target_sources(app PRIVATE ${app_sources})

  1. ビルドする

  2. 書き込む

gotoooogotoooo

割り込みとかマルチタスクとか色々試したいことはあるが、「試す」の範疇を超えそうなため一旦クローズ。

このスクラップは1ヶ月前にクローズされました