Multi Source Translation Content

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Multi Source Translation Content

ディスカッション

ソート順:
Using S32 Flash Tool for S32N5 This guide explains how to use the NXP S32 Flash Tool to program and run a prebuilt GrayVIP software image on the S32N5 reference design board. It covers the required hardware and software, board setup, flash programming, and serial console configuration. Step-by-step instructions help you verify successful boot and software execution, providing a starting point for evaluating GrayVIP on S32N5. S32N S32N5
記事全体を表示
Zephyrデビュー会 〜FRDM-MCXN947でつまずかず動かす90分〜 [本編] (日本語ブログ) この記事は,本編(0〜6章と付録A)と付録編(付録B〜G)の2つに分けて掲載しています.付録編には,コマンド例の一覧(PowerShell/bash・zsh),早く終わった方向けの課題,他のFRDMボードで試す方法,用語集,よくある質問などがあります. 目次 0. この記事は? 0.1 対象読者 0.2 この記事のゴール 0.3 使うもの 0.4 進め方と所要時間の目安 0.5 この記事の読み方 1. Zephyrとは? 1.1 Zephyrの概要と特長 1.2 なぜ今Zephyrなのか 1.3 FRDM-MCXN947について 2. 開発環境を触ってみる 2.1 環境の確認 2.2 Hello Worldサンプルを実行してみる 2.3 blinkyサンプル(Lチカ)を書き込む 2.4 サンプルコードの中身をちょっと覗いてみる 3. 温度センサーを動かしてみる 3.1 サンプルをビルド・書き込む 3.2 値を確認する 3.3 なぜ動いたのか? 4. HTTP Serverを動かしてみる 4.1 サンプルをビルド・書き込む 4.2 ブラウザからLEDを制御してみる 4.3 /uptimeエンドポイントを覗いてみる 5. HTTP Serverを改変して,温度センサーの値をブラウザで見る 5.1 何を追加したか 5.2 実際にブラウザで表示してみる 5.3 3章・4章の内容がどう組み合わさっているか 6. まとめ 6.1 今日体験したこと 6.2 自分でもう一度やってみるための情報源 6.3 AIをどう活用できるか 6.4 後片付け 付録A. うまくいかないときは A.1 まず確認すること A.2 PowerShell・westまわり A.3 ビルドのエラー A.4 書き込み(west flash)のエラー A.5 シリアルコンソールに何も出ない A.6 ネットワーク・ブラウザまわり A.7 どうしても直らないときのリセット手順 付録編(別の記事): 付録B. コマンド例一覧(PowerShell/bash・zsh) 付録C. やってみよう(早く終わった方へ) 付録D. 他のFRDMボードで試すには 付録E. 用語集 付録F. よくある質問 付録G. HTTP Serverサンプルの足回り 0. この記事は? この記事は,ハンズオン・セミナー「Zephyrデビュー会」の進行ガイドとして書かれたものです.実際のセミナーでは,この記事の手順に沿って一緒に手を動かしていきます. セミナーに参加していない方も,この記事だけを読みながら,同じ内容を自分の環境で再現できます.そのために,コマンド例や確認ポイントを省略せずに書いています.読み物としての解説記事というより,実際に「動くところまで」たどり着くための作業ガイドだと捉えてください. 0.1 対象読者 「Zephyrって聞いたことはあるけど触ったことはない」というZephyr OS初心者,これからZephyrを触ってみたい方に向けた記事です.組み込み開発の経験は問いません.C言語のコードを読んだことがある程度で十分です. 0.2 この記事のゴール NXPのFRDM-MCXN947ボードを使って,LEDの点滅から温度センサーの読み取り,さらにブラウザ経由でセンサー値を表示するところまでを,実際に手を動かして体験します.ゴールは「Zephyrで簡単にアプリが動かせる」という成功体験を持ち帰ってもらうことです.難しい設定やコーディングは,この記事の中では最小限に留めます. この記事では,あえてコマンドラインで作業します.NXPのMCUXpresso for VS Codeのように,ボタン操作でビルドや書き込みができる便利なツールもありますが,ここではZephyrが標準で用意している west コマンドを直接使います.特定のベンダーのツールに依存しない,ニュートラルな素のZephyrを体験してもらうためです.ここで体験する手順は,NXP以外のメーカーのボードでも,ボード名を変えるだけでそのまま使えます. 今日の流れは,図0.2-1のとおりです.2章で開発の手順に慣れ,3章と4章でそれぞれ別の機能を動かし,5章でその2つを組み合わせます. 図0.2-1:今日の流れ 0.3 使うもの ハードウェア FRDM-MCXN947評価ボード PC(Zephyr開発環境など下記ソフトウェアをインストールしておく) 温度センサー: P3T1755 I3C/I²Cバス対応,±0.5°C精度デジタル温度センサー (FRDM-MCXN947評価ボードに搭載) Ethernetケーブル ソフトウェア Zephyr開発環境(west(Zephyrのビルドや書き込みを行うコマンド),Zephyr SDK(コンパイラなどのツール一式),Python仮想環境を含む.公式のGetting Started Guideの手順で ~/zephyrproject にセットアップしたもの) LinkServer(NXPの書き込み・デバッグツール. west flash がFRDM-MCXN947への書き込みに使います) Microsoft Visual Studio Code(ソースコードや prj.conf (アプリケーションごとの設定ファイル)の表示・編集に使います) ターミナルソフト(本記事ではWindows用のTera Termを使って説明します.macOSやLinuxで試す場合は,CoolTermなど,シリアルポートに接続できるものを使ってください) ブラウザ Git(5章でサンプルコードを取得するのに使います) MCUXpresso for VSC(必須ではありません.*会場PCには入っていますが,本記事の手順では使いません) ※会場のPCには,これらがすべてセットアップ済みです. ご自身のPCで再現する方へ 会場PCと同じ状態にするには,上のソフトウェアをインストールしておく必要があります.特にLinkServerは見落としがちです.インストールされていない(またはPATHが通っていない)と,ビルドまではできても west flash で書き込めません.また,4章ではPCのEthernetポートを使うので,Ethernetポートのないノートパソコンでは,USB-Ethernetアダプタを用意してください. インストール方法は,次のどちらかを参考にしてください. 公式ガイド: Getting Started Guide(英語).この記事の前提( ~/zephyrproject にワークスペースとPython仮想環境 .venv を作る構成)と同じ手順です.LinkServerは,別にインストールしてください. NXPのツールを使った手軽な方法: [Zephyr® シリーズ] 第2回 はじめてのビルドと実機動作(NXP Tech Blog,日本語).MCUXpresso for VS CodeとMCUXpresso Installerで,Zephyrの開発環境とLinkServerをまとめてインストールできます.ただし,この方法ではPython仮想環境(Windowsでは C:\Users\<ユーザー名>\.mcuxpressotools\ の下)やZephyrのフォルダの場所が,この記事と異なります.コマンド例2.1-1などのフォルダの場所は,ご自身の環境に合わせて読み替えてください. 0.4 進め方と所要時間の目安 セミナーは90分です.おおよそ次の時間配分で進めます. 章 内容 目安 0〜1章 この記事について,Zephyrとは 10分 2章 開発環境の確認,Hello World,Lチカ 25分 3章 温度センサーを動かす 15分 4章 HTTP Serverを動かす 20分 5章 HTTP Serverで温度を表示する 15分 6章 まとめ 5分 【実際のタイムテーブルに合わせて調整】 遅れてしまったら 3章から5章は,それぞれ独立しています.前の章が終わっていなくても,新しいPowerShellでコマンド例2.1-1(会場PCでは zinit )を実行すれば,どの章からでも始められます.たとえば3章でつまずいたまま時間が来たら,3章はいったん置いて,4章の最初のコマンド例から進めてください.あとからこの記事を見ながら,自分のペースでやり直せます. 早く終わったら 付録C. やってみように,サンプルコードを少しだけ書き換えて試す課題を用意しています. 質問するときは 手が止まったら,遠慮なく手を挙げてください.会場のスタッフや,あとからこの記事を見ながらAIや知り合いに質問するときは,次の3つを伝えると,原因がすぐに分かります. どのコマンド例で止まったか(コードブロックの1行目にある # コマンド例3.1-2 のような番号) 最初に出たエラーの行(赤い文字や error を含む行.スクリーンショットでも構いません) コマンド例A.1-1の実行結果(今いるフォルダと,環境の設定状態が分かります) 0.5 この記事の読み方 2章から5章の各節は,次の順に並んでいます.会場では,まず手順を最後まで進め,「✔ ここまでできればOK」を確かめてから,解説を聞いてください. この節でやること(1行) 手順(コマンド例.長い節は「ステップ1」「ステップ2」…に分けています) 表示例(成功したときの画面や表示) ✔ ここまでできればOK(表示例と同じになったかを確かめる確認ポイント) うまくいかないときは(よくあるつまずきと対処) 解説・コラム 本文の中の囲みは,次のように使い分けています. 囲み 内容 いつ読むか 注意 手順を進めるうえで,必ず守ってほしいこと 手順の前に必ず読む 解説 今やった操作やコードの意味 手順が終わってから.会場では講師がその場で説明します コラム 背景知識や,関連する話題 余裕があるとき,またはあとで ご自身のPCで試す場合 会場PC以外で再現するときの補足 自分のPCで試すとき 本文に出てくる用語は,付録E. 用語集にまとめています. この記事をVS Codeで読む場合 会場では,この記事をVS Code(Visual Studio Code)の右側に表示して読みます.コードブロックのコマンドは,ドラッグで選んで Ctrl + C でコピーし,PowerShellに貼り付けます. 記事の表示(プレビュー)がある右側の領域は,ロックしてあります.そのため, code prj.conf のようにコマンドで開いたファイルは,必ず左側に開きます.右側の記事は,読んでいた位置のまま残ります. 右側の記事を閉じてしまったときは,次の手順で戻します. VS Codeの「File」→「Open File...」で, ~\seminar_text\article_draft.md を開きます. Ctrl + K を押してから V を押すと,記事のプレビューが右側に表示されます. 右側のプレビューのタブをクリックしてから, Ctrl + Shift + P でコマンドパレットを開き,「View: Lock Editor Group」を実行します. 左側に開いた article_draft.md のタブは,閉じて構いません(誤って書き換えないためです). ブラウザやNXPのブログで読んでいる場合は,この手順は必要ありません. ▲ 目次に戻る 1. Zephyrとは? 1.1 Zephyrの概要と特長 Zephyrは,Linux Foundation配下で開発が進むオープンソースのRTOS(リアルタイムOS)です.小規模なマイコンから,比較的リソースの大きなSoCまで,幅広いハードウェアを対象にしています. 対象範囲の広さは,NXP製品だけを見ても分かります.今回使うMCXシリーズのようなCortex-Mコアのマイコンから,i.MXシリーズのようなCortex-Aコアのアプリケーションプロセッサまで,同じZephyrという1つのOSでカバーしています.ボードごとにOSやビルド環境を使い分けなくて済むのは,実用上の大きなメリットです. 特長を一言でまとめると,次の2つの仕組みで,ハードウェアの構成とソフトウェアの機能を分けて管理できる点です. デバイスツリー: 「どの部品が,どこにつながっているか」というハードウェアの構成を書くファイル Kconfig: 「どの機能を使うか」というソフトウェアの機能のオン・オフを設定する仕組み この分離のおかげで,同じアプリケーションのコードを,複数のボードに展開しやすくなっています. 言い換えると,C言語などで書くアプリケーションのロジックは,「何のセンサーが」「どのバスに」つながっているかを直接知らなくてよい,という設計です.アプリケーションのコードは,デバイスツリーに付けた名前を使ってデバイスにアクセスするだけです.この分離を,2.4節と3.3節で実際のコードを見ながら体感します. 1.2 なぜ今Zephyrなのか 半導体メーカー各社が,既存製品ラインを含めZephyrへの対応を広げています.特定のベンダーに縛られないRTOSとして,業界内での存在感が増しているのが現状です. Zephyrプロジェクトは2016年にLinux Foundation配下でスタートし,およそ10年の歴史を持ちます.新しくて枯れていない技術ではありません. この間に積み重ねられてきたのは,CPUアーキテクチャやペリフェラルのドライバだけではありません.4章・5章で体験するTCP/IPスタックやHTTPサーバーといった,ネットワーク関連のミドルウェアも,Zephyr本体に標準で組み込まれた機能として育ってきています.マイコン向けOSというと,最小限のスケジューラだけを想像しがちです.こうした上位のミドルウェアまで,一貫してKconfigで有効にできる点も,Zephyrを選ぶ理由のひとつです. 製品に使うOSとして,セキュリティへの取り組みが整っていることも,Zephyrが注目される理由です.欧州では,ネットワークにつながる製品にサイバーセキュリティ対策を求める「サイバーレジリエンス法(CRA)」が施行され,2026年9月からは,悪用された脆弱性の報告がメーカーの義務になっています.Zephyrプロジェクトは,脆弱性への対応や長期サポートの体制を整え,こうした要求に応える準備を進めています(下のコラム). 「結局何がそんなによいのか」は,触ってみないと実感しにくい部分です.この記事では,実際に手を動かしながらその感触を確かめていきます. コラム: Zephyrのセキュリティへの取り組み 脆弱性に対応する専門チーム(PSIRT)があり,2017年からCVE番号を割り当てる機関(CNA)に登録されています.報告を受けてから,30日以内にメーカーへの通知と修正の公開を,最長90日で公表することを目標にしています. 製品メーカーは,脆弱性の通知(Vulnerability Alert Registry)に登録して,情報を受け取れます. 製品向けの長期サポート版(LTS)は,セキュリティ修正を含めて少なくとも5年間保守されます. west spdx で,製品に含まれるソフトウェアの一覧(SBOM)を作れます. 暗号機能はPSA Crypto(実装はMbed TLS)で提供されます.FRDM-MCXN947には,TrustZoneを使うTF-Mと組み合わせるボード指定( frdm_mcxn947/mcxn947/cpu0/ns )も用意されています. CRAとZephyrの関係は,Zephyrの公式ドキュメントのCyber Resilience Actのページにまとめられています.なお,このハンズオンでは手順を簡単にするため,暗号化しないプレーンHTTPを使っています(4.1節の解説). 1.3 FRDM-MCXN947について FRDM-MCXN947は,NXPのMCX N947マイコンを搭載した評価ボードです.150MHzで動作するCortex-M33のデュアルコア構成です.GPIO・I2C・I3C・SPI・Ethernetなどのペリフェラルを,ArduinoヘッダやmikroBusソケットなどのコネクタ経由で扱えます. 本記事では,ボード上の次の部品を使います(図1.3-1).以降の本文では,「図1.3-1の①」のように,この番号で場所を示します. 図1.3-1:FRDM-MCXN947で使う部品 番号 部品 使う場面 ① デバッグ用USBコネクタ(MCU-Link側) PCとの接続.書き込み( west flash )とシリアルコンソールの両方に使います.電源もここから供給されます.MCU-Linkは,ボードに載っている書き込み・デバッグ用の回路です ② RESETボタン プログラムを最初から実行し直したいとき(シリアルの出力を見逃したときなど) ③ RGB LED 2.3節のLチカ(赤く点滅),4.2節のブラウザからのLED制御(緑に点灯) ④ 温度センサー(P3T1755) 3章,5章 ⑤ Ethernetコネクタ(RJ45) 4章,5章でPCとEthernetケーブルで直結します 注意: ボードにはUSBコネクタが複数あります.①以外のUSBコネクタにつなぐと,書き込みもシリアルコンソールも使えません. ▲ 目次に戻る 2. 開発環境を触ってみる 2.1 環境の確認 この節では,PowerShellを開いて,Zephyrのコマンド(west)が使える状態になっていることを確認します. 会場のPCには,west・Zephyr SDKを含む開発環境を,公式のGetting Startedガイドの手順でセットアップしてあります.皆さんが環境構築で手間取る必要はありません.MCUXpresso for VSCも使える状態ですが,0.2節で書いたとおり,このセミナーではあえてコマンドラインで操作します. ステップ1: PowerShellを開く スタートメニューで「PowerShell」と入力し,「Windows PowerShell」を起動してください(図2.1-1).このとき,「管理者として実行」は選ばないでください.このセミナーの手順に管理者の権限は必要なく,管理者として起動すると,コマンドを間違えたときの影響がPC全体に及ぶおそれがあるためです.管理者として起動したかどうかは,ウィンドウのタブ(またはタイトル)の先頭に「管理者:」と付いているかで見分けられます. 図2.1-1:スタートメニューから「Windows PowerShell」を開く PowerShellは,起動のしかたによって最初にいるフォルダ(カレントディレクトリ)が違います.図2.1-2のように,管理者として起動していなくても PS C:\WINDOWS\system32> と表示されることがあります.ホームフォルダ( C:\Users\<ユーザー名> )で開くこともありますし,VSCodeのターミナルでは開いているフォルダになります.どこで開いても,次のコマンド例の最初の cd でZephyrのワークスペース( ~\zephyrproject )に移動するので問題ありません. ~ は自分のホームフォルダを表す記号です. 図2.1-2:起動したPowerShell( C:\WINDOWS\system32 で開いた例) ステップ2: westを使える状態にする westを使う前に,Zephyr用のPython仮想環境(Zephyrのツールをまとめて入れてある,独立したPythonの環境)を有効化し,Zephyr本体の場所を環境変数 ZEPHYR_BASE に設定します.次のコマンド例をコピーして,PowerShellに貼り付けて実行してください. 貼り付けるときは PowerShellのウィンドウ上で右クリックするか, Ctrl + V を押すと貼り付けられます. 最後の行が入力されたまま止まっていたら, Enter を押して実行します. 1行目の # コマンド例… の行も,そのまま貼り付けて構いません(PowerShellでは何もしない行です). そのほかの注意は,この節の最後の「コラム: コマンドをコピー&ペーストするときの注意」にまとめています. # コマンド例2.1-1 cd ~\zephyrproject .\.venv\Scripts\Activate.ps1 $env:ZEPHYR_BASE = "$HOME\zephyrproject\zephyr" west --version west topdir 実行すると,次のように west --version でバージョンが, west topdir でワークスペースの場所が表示されます(図2.1-3).プロンプト(コマンドの入力を待っている PS C:\...> の表示)の先頭には, (.venv) が付きます. (.venv) PS C:\Users\<ユーザー名>\zephyrproject> west --version West version: v1.5.0 (.venv) PS C:\Users\<ユーザー名>\zephyrproject> west topdir C:/Users/<ユーザー名>/zephyrproject west topdir の表示は,フォルダの区切りが \ ではなく / になりますが,同じ場所を指しています. 図2.1-3:コマンド例2.1-1の実行結果 ✔ ここまでできればOK:プロンプトの先頭に (.venv) が付き,westのバージョンとワークスペースの場所が表示された (ここから先,「実行結果の例」として載せている出力は,自分の画面と見比べるためのものです.コマンドとしてコピーしないでください.数値やバージョン番号は,環境によって異なります.) 注意: このPowerShellのウィンドウは閉じない 仮想環境の有効化や ZEPHYR_BASE の設定は,そのウィンドウの中だけで有効です.セミナーの間は,このウィンドウを開いたままにしておいてください.閉じてしまったときは,下の「PowerShellを開き直したときは」の手順で戻れます. うまくいかないときは .\.venv\Scripts\Activate.ps1 で「このシステムではスクリプトの実行が無効になっている」と赤字が出る → 下の解説の最後にある実行ポリシーの設定を行ってから,コマンド例2.1-1をもう一度最初から実行します. west が「認識されません」と出る → 仮想環境の有効化に失敗しています.プロンプトの先頭に (.venv) が付いているか確認してください(A.2). cd で「パスが存在しないため検出できません」と出る → ワークスペースが ~\zephyrproject 以外の場所にある可能性があります.ご自身の環境で試している場合は,ワークスペースの場所に読み替えてください. PowerShellを開き直したときは 新しいPowerShellで環境を設定し直してから,作業していたサンプルのフォルダに戻ります.会場のPCには,これを一度に行う zinit というコマンドを用意してあります. zinit に続けて,作業していたサンプルの名前を指定します.たとえば3章の途中なら,次のように実行します. # コマンド例2.1-2 zinit thermometer サンプル名は,途中まで入力して Tab キーを押すと補完されます.サンプル名を付けずに zinit だけを実行すると,コマンド例2.1-1を実行した直後と同じ状態( ~\zephyrproject にいる状態)になります. zinit はVSCodeのターミナルでも使えます. zinit がないPCでは,コマンド例2.1-1を実行してから, cd ~\handson\thermometer のようにサンプルのフォルダに移動します.コマンド例2.1-1を実行した直後は ~\zephyrproject にいるので,そのまま west build を実行しても,サンプルのビルドにはなりません. 解説: コマンド例2.1-1で何をしているか cd ~\zephyrproject — Zephyrのワークスペース(west・Zephyr本体・各種モジュールをまとめて置いてあるフォルダ)に移動します. .\.venv\Scripts\Activate.ps1 — Zephyr用に用意されたPython仮想環境を有効化します.west自体やビルドに使う各種Pythonツールが,この仮想環境にインストールされています.有効化に成功すると,プロンプトの先頭に (.venv) と表示されます. $env:ZEPHYR_BASE = "..." — Zephyr本体のフォルダを環境変数 ZEPHYR_BASE に設定します.westは,カレントディレクトリから上のフォルダをたどってワークスペースを探します.この後の章では,ワークスペースの外にある作業用フォルダ( ~\handson\... )でビルドします.そこからではワークスペースが見つからず, west build が「unknown command」になってしまいます. ZEPHYR_BASE を設定しておくと,westはそれを手がかりにワークスペースを見つけられます. west --version — westコマンド自体が正しく使える状態になっているかを,バージョン表示で確認します. west topdir — westが認識しているワークスペースの場所を表示します.ここでエラーになる場合は, ZEPHYR_BASE の設定を見直してください. Linux/macOSの解説記事では, source ~/zephyrproject/.venv/bin/activate や source zephyr/zephyr-env.sh という書き方をよく見かけます.これはbash/zsh用の書き方で,PowerShellではそのままでは動きません. zephyr-env.sh が主に行っているのは, ZEPHYR_BASE の設定です.そこでPowerShellでは,上のように $env:ZEPHYR_BASE で直接設定します.(Windows向けには zephyr-env.cmd も用意されていますが,これはコマンドプロンプト用です.PowerShellから実行しても,設定した環境変数はPowerShell側には残りません.) なお, Activate.ps1 の実行時に「スクリプトの実行が無効になっている」というエラーが出る場合は,PowerShellの実行ポリシーで止められています. Set-ExecutionPolicy -Scope CurrentUser RemoteSigned を一度実行してから,もう一度試してください(会場PCでは設定済みです). コラム: コマンドをコピー&ペーストするときの注意 複数行の貼り付けの確認ダイアログ: Windows Terminalでは,複数行を貼り付けると「複数行のテキストを貼り付けようとしています」という確認が出ることがあります.そのまま貼り付けを選んでください. 途中でエラーが出たら,そこで止まる: 複数行をまとめて貼り付けると,途中の行でエラーが出ても,後ろの行が続けて実行されてしまいます.赤い文字のエラーが出たら,それより後の結果は当てにせず,エラーが出た行の原因を先に解決してください.慣れないうちは,1行ずつコピーして実行するのが確実です. # コマンド例… の行は目印: 質問するときや,付録Aを参照するときに,この番号を使ってください. プロンプト( PS C:\Users\...> )はコピーしない: スクリーンショットや実行例に表示されているプロンプト部分は,コマンドの一部ではありません. うまくいかないときは,付録A. うまくいかないときはを参照してください.記事中のコマンド例は,付録B. コマンド例一覧にもまとめています.macOSやLinuxで試す方向けのbash・zsh版もそちらに載せています. コラム: ご自身のPCで zinit を使うには zinit は,PowerShellのプロファイル(PowerShellの起動時に自動で読み込まれるスクリプト)に登録した関数です.ご自身のPCでも,次のようにして同じものを用意できます. まず,次のコマンドでプロファイルをメモ帳で開きます(ファイルがなければ作ります). # コマンド例2.1-3 if (-not (Test-Path $PROFILE.CurrentUserAllHosts)) { New-Item -Force $PROFILE.CurrentUserAllHosts | Out-Null } notepad $PROFILE.CurrentUserAllHosts 開いたメモ帳に,次の内容を書き加えて保存します.これはPowerShellに貼り付けて実行するコマンドではなく,プロファイルに書く内容です. function zinit { param([string]$Sample) Set-Location ~\zephyrproject . .\.venv\Scripts\Activate.ps1 $env:ZEPHYR_BASE = "$HOME\zephyrproject\zephyr" west --version west topdir if ($Sample) { Set-Location ~\handson\$Sample } } 保存したら,PowerShellのウィンドウを開き直すと zinit が使えるようになります.プロファイルの実行にも,この節の解説で説明した実行ポリシーの設定( RemoteSigned )が必要です.なお,プロファイルに日本語を書く場合は,文字コードに注意が必要です(Windows PowerShellは,BOMなしのUTF-8のファイルに含まれる日本語を正しく読めません).上の内容は英数字だけなので,そのまま保存して構いません. ご自身のPCで試す場合: 環境のセットアップ 今回は会場PCに環境構築済みの状態で臨んでいただいていますが,ご自身のPCでも同じ環境を用意できます.インストール方法は,0.3節の「ご自身のPCで再現する方へ」を参照してください. 2.2 Hello Worldサンプルを実行してみる この節では, hello_world サンプルをビルドしてボードに書き込み,シリアルコンソールに文字が表示されることを確認します. これ以降,サンプルは zephyr/samples/ の中のものを直接編集せず,自分の作業用フォルダ( ~\handson )にコピーしてから使います(図2.2-1). zephyr/samples/ は元のサンプル集として手を加えずに残しておき,実験や書き換えは自分のフォルダの中だけで行います. 図2.2-1:サンプルを作業用フォルダにコピーして使う ステップ1: サンプルを作業用フォルダにコピーする hello_world サンプルを作業用フォルダにコピーし,そのフォルダに移動します. # コマンド例2.2-1 New-Item -ItemType Directory -Force ~\handson | Out-Null Copy-Item -Recurse ~\zephyrproject\zephyr\samples\hello_world ~\handson\hello_world cd ~\handson\hello_world 実行後,プロンプトが PS C:\Users\<ユーザー名>\handson\hello_world> になっていれば移動できています.念のため ls を実行すると, src ・ CMakeLists.txt ・ prj.conf などが並んでいるはずです. ステップ2: ビルドする 注意: 最後の . を忘れずに 行末の . (スペースとピリオド)は「カレントディレクトリにあるアプリケーションをビルドする」という意味です.コピーの際に切れやすいので,貼り付けた後に確認してください. -b の後ろのボード名も, / と // を含めて1文字でも違うとエラーになります.手で打ち直さず,コピーしてください. # コマンド例2.2-2 west build -p auto -b frdm_mcxn947/mcxn947/cpu0 . 最初のビルドは数分かかることがあります.プロンプトが戻ってくるまで待ってください. ビルドが成功すると,出力の最後に,次のようなFLASHやRAMの使用量の表が出ます. (前略) [xxx/xxx] Linking C executable zephyr\zephyr.elf Memory region Used Size Region Size %age Used FLASH: xxxxx B 2 MB x.xx% RAM: xxxx B 320 KB x.xx% SRAMX: 0 B 96 KB 0.00% IDT_LIST: 0 B 32 KB 0.00% Generated files: (後略) 【会場PCでの実際の出力に差し替え】 ✔ ここまでできればOK:出力の最後に,FLASHやRAMの使用量の表が出た.これで,マイコンに書き込むプログラムができました エラーの場合は,そのメッセージが表示されます.Zephyrのエラーメッセージは大量になることが多く,最初は追うのが大変です.まずは最初に出てくる error の行を探すのがコツです.メッセージを注意深く読むか,AIの力を借りて把握するとよいでしょう.よくあるエラーと対処法はA.3 ビルドのエラーにまとめています. ステップ3: 書き込みの準備をする 書き込みの前に,次の2つを準備します. (1) PCとボードをUSBケーブルでつなぐ 図1.3-1の①(デバッグ用USBコネクタ,MCU-Link側)とPCを,USBケーブルでつなぎます(図2.2-2).ボードのLEDが点灯して,電源が入ったことが分かります.充電専用のUSBケーブルでは通信できないので,データ通信に対応したケーブルを使ってください. 図2.2-2:USBケーブルを図1.3-1の①につなぐ (2) シリアルコンソールを開いておく hello_world は,起動直後に1回だけ文字列を出力して終わるプログラムです.書き込んだ後にシリアルコンソールを開いたのでは,出力を見逃してしまいます.そこで,書き込む前にシリアルコンソール(ボードの printf の出力などを,PCで表示する画面)を開いて接続しておきます.今回はターミナルソフトとしてTera Termを使います.次の「Tera Termの手順」1〜4で接続してください. Tera Termを起動する デスクトップの「Tera Term 5」のアイコンから起動します(図2.2-3).起動すると「Tera Term: 新しい接続」という画面が開きます. シリアルポートを選ぶ 「シリアル」を選び,「ポート」の一覧から,MCU-Link(図1.3-1の①)に対応するポートを選んで「OK」を押します(図2.2-4). 一覧には, COM3: MCU-Link VCom Port (COM3) のように COM +番号の名前で表示されます.番号はPCによって異なります. どれか分からない場合は,ボードのUSBケーブルを抜き差しして,そのとき一覧から消えたり現れたりするポートを選んでください(一覧は「新しい接続」の画面を開き直すと更新されます). ボーレート(シリアル通信の速度)を115200にする メニューの「設定」→「シリアルポート」を選ぶと,「Tera Term: その他の設定」という画面の「シリアルポート」タブが開きます.「スピード」を 115200 にして「OK」を押します(図2.2-5).(古いバージョンのTera Termでは,画面の名前やボタンが異なります.) 文字コードをUTF-8にする (会場のPCではこの設定は必要ありません) メニューの「設定」→「その他の設定」を開き,「エンコーディング」タブを選びます(手順3の画面のまま,タブを切り替えても構いません).受信の文字コードを UTF-8 にして「OK」を押します.3章で表示する °C の ° を正しく表示するための設定です. 図2.2-3:デスクトップのTera Termのアイコン 図2.2-4:「新しい接続」でシリアルポートを選ぶ 図2.2-5:「設定」→「シリアルポート」でスピードを115200にする 一度設定すれば,以降の章ではTera Termを開いたままで構いません.書き込みのたびに接続し直す必要はありません. ステップ4: 書き込む 準備ができたら,PowerShellに戻って次のコマンドで書き込みます. # コマンド例2.2-3 west flash 書き込みが終わるとマイコンがリセットされ,書き込んだプログラムが実行されます. シリアルコンソールに,次のような文字列が表示されます(図2.2-6). *** Booting Zephyr OS build v4.x.x *** Hello World! frdm_mcxn947/mcxn947/cpu0 1行目はZephyrが起動したときに必ず出すメッセージで, v4.x.x の部分はZephyrのバージョンです.2行目が hello_world のプログラムが出力した文字列です. 図2.2-6:Tera TermにHello Worldが表示された画面 ✔ ここまでできればOK:Tera Termに Hello World! frdm_mcxn947/mcxn947/cpu0 と表示された うまくいかないときは 書き込み前にシリアルコンソールを開き忘れて出力を見逃した → Tera Termが接続されている状態で,ボードのRESETボタン(図1.3-1の②)を押してください.プログラムが最初から実行し直され,もう一度文字列が表示されます. west flash で「プローブが見つからない(No probe found など)」というエラーが出る → USBケーブルが図1.3-1の①に挿さっているか確認します.抜き差ししてから,もう一度コマンド例2.2-3を実行します(A.4). 書き込みは成功したのに何も表示されない,文字化けする → Tera Termのポート,ボーレート,文字コードの設定を確認します(A.5). 解説: 作業用フォルダへのコピー(コマンド例2.2-1) 1行目の New-Item で,作業用フォルダ ~\handson を作っておきます(すでにある場合は何もしません.この行は最初の1回だけで大丈夫です). Copy-Item -Recurse で, zephyr\samples\ の中のサンプルをフォルダごと作業用フォルダにコピーし, cd でそのフォルダに移動しています.以降の章でも同じように,触りたいサンプルをまず作業用フォルダへコピーしてから作業を始めます. Copy-Item は,コピー先のフォルダがすでに存在すると,その中にサンプルのフォルダをもう1つ作ってしまいます(例: ~\handson\hello_world\hello_world ).コピーをやり直したいときは,先に Remove-Item -Recurse -Force ~\handson\hello_world で古いフォルダを消してから実行してください. 解説: -b ・ -p オプション(コマンド例2.2-2) -b に続く引数は,ビルド対象のボードの指定です.FRDM-MCXN947はCortex-M33のデュアルコア構成なので, frdm_mcxn947/mcxn947/cpu0 のように「ボード名/SoC名/コア名」まで指定します.FRDM-MCXA153のようなシングルコアのボードでは, frdm_mcxa153 というボード名だけで済みます. SoC名を省略して frdm_mcxn947//cpu0 と書くこともできます.FRDM-MCXN947のようにSoCが1つしかないボードでは,間のSoC名を省いてスラッシュだけを残しても一意に決まるためです.本記事でも,2.3節以降はこの省略形を使います. -p auto は --pristine=auto の省略形です.ビルドフォルダをクリーンな状態から作り直す(pristineビルドする)かどうかを,自動で判定します.詳しくは次の解説で説明します. 以降の章で登場する west build ・ west flash でも,これらのオプションの意味は同じです. 解説: ビルドでできたファイルの場所 ビルドが成功すると,カレントディレクトリに build/ フォルダが作られます.書き込みに使われるのは build/zephyr/zephyr.elf (デバッグ情報付きのELFファイル)で,この後実行する west flash も,このファイルをボードに書き込みます.そのほか,コンフィグの内容は build/zephyr/.config ,展開後のデバイスツリーは build/zephyr/zephyr.dts で確認できます. --pristine は,このビルドフォルダを作り直すかどうかを指定するオプションです. -p auto ( --pristine=auto )は,ソースやコンフィグの変更を検知して,必要なときだけビルドフォルダを作り直します.ビルドの様子がおかしいときなど,明示的にクリーンビルドしたい場合は -p always ( --pristine=always )を指定します. ご自身のPCで試す場合(Tera Term) Tera Termの手順3と4の設定は,「設定」→「設定の保存」で保存しておくと,次回からは手順2だけで接続できます. macOSやLinuxでは,Tera Termは使えません.CoolTermなどで,ポートに /dev/tty.usbmodemXXXX (macOS)や /dev/ttyACM0 (Linux)を選び,ボーレートを115200にして接続してください. hello_world のコードを見てみる hello_world の src/main.c は,次のように驚くほど短いコードです.VSCodeでこのファイルを開くには,PowerShellから code ~\handson\hello_world\src\main.c を実行します.(VSCodeの「Open Folder」でフォルダを開くと,記事を表示しているウィンドウの中身が切り替わってしまうので,会場では使わないでください.) #include int main(void) { printf("Hello World! %s\n", CONFIG_BOARD_TARGET); return 0; } これを見る限り,至って普通のC言語による Hello World コードです.強いて特別な点を挙げるなら, CONFIG_BOARD_TARGET というマクロがこのビルド環境の中で定義されているのだろう,という程度です.マイコンプログラムでよく見られるクロック設定やペリフェラルの初期化処理は,このコードのどこにも見当たりません. このコードには,「RTOSのコードっぽくなさ」もあります.タスク(スレッド)の生成も,スケジューラの起動もありません. main() から printf() を呼ぶだけで,PC向けのC言語プログラムとほとんど同じ見た目です. 多くのRTOSでは,「Hello World」のためにもタスクを明示的に作り, osKernelStart() のような呼び出しでスケジューラを起動する必要があります.Zephyrでは, main() 自体がすでに1つのスレッドとして動いています.その手前の初期化は,ビルドシステムとKconfigの設定に任されています.この「素のCプログラムに近い書き味」は,Zephyrがアプリケーション開発者に見せる顔の特徴のひとつです. 解説: main.c以外に必要なファイル Zephyrアプリケーションとして最低限必要なのは, src/main.c のほかに prj.conf と CMakeLists.txt の2つです. prj.conf は,そのアプリケーションで使うKconfigの機能を並べる設定ファイルです. hello_world では特に有効にしたい機能がないため,中身はほぼ空です. CMakeLists.txt は,ビルドの定義ファイルです. find_package(Zephyr ...) でZephyrのビルドシステムを読み込み, target_sources() でビルドするソースファイルを指定します. 実際に zephyr/samples/hello_world/ の中を見ても,ファイルは CMakeLists.txt ・ prj.conf ・ src/main.c (と説明用の README.rst )程度しかありません.Zephyrアプリケーションの最小構成は,この3点セットです. 2.3 blinkyサンプル(Lチカ)を書き込む この節では, blinky サンプルを書き込んで,ボードのLEDを点滅させます.手順はHello Worldと同じで,コピーするサンプルが変わるだけです.これ以降,ボードの指定には,2.2節の解説で触れた省略形 frdm_mcxn947//cpu0 を使います. # コマンド例2.3-1 Copy-Item -Recurse ~\zephyrproject\zephyr\samples\basic\blinky ~\handson\blinky cd ~\handson\blinky プロンプトが ...\handson\blinky> になったことを確認してから,ビルドします. 注意: ビルドと書き込みは分けて実行する west build と west flash を一度に貼り付けると,ビルドが失敗しても west flash が続けて実行されます.そうなると,最後に表示されるのは west flash 側のエラーなので,本当の原因(ビルドのエラー)が画面の上の方に流れてしまいます.ビルドが成功したのを確認してから書き込む,という順序を守ると,どこで失敗したのかがすぐ分かります. # コマンド例2.3-2 west build -p auto -b frdm_mcxn947//cpu0 . ビルドが成功したこと(最後にメモリ使用量の表が出ていること)を確認してから,書き込みます. # コマンド例2.3-3 west flash ボード上のRGB LED(図1.3-1の③)が,赤色で1秒ごとに点滅します(図2.3-1).Tera Termを接続したままにしておくと,点滅に合わせて次のような表示も流れてきます. 図2.3-1:blinkyでRGB LEDが赤く点滅する *** Booting Zephyr OS build v4.x.x *** LED state: OFF LED state: ON LED state: OFF LED state: ON ✔ ここまでできればOK:RGB LEDが,赤色で1秒ごとに点滅した うまくいかないときは プロンプトが ...\hello_world> のままで west build を実行してしまった → hello_world がもう一度ビルドされるだけで,害はありません.コマンド例2.3-1の cd からやり直してください. Copy-Item を2回実行してしまった → ~\handson\blinky\blinky という入れ子のフォルダができますが, ~\handson\blinky でのビルドには影響しません.気になる場合は,2.2節の解説の方法で消してからコピーし直してください. 2.4 サンプルコードの中身をちょっと覗いてみる この節では, blinky のコードを見ながら,LEDの場所がどこで決まっているのかを確かめます. blinky の src/main.c は,GPIOをトグル(出力を反転)するだけの短いコードです.全体を見てみましょう.VSCodeで開くには,PowerShellから code ~\handson\blinky\src\main.c を実行します. /* * Copyright (c) 2016 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include #include #include /* 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; } 上から順に,次のことをしています. SLEEP_TIME_MS で,点滅の間隔(1000ミリ秒=1秒)を決めています. DT_ALIAS(led0) と GPIO_DT_SPEC_GET() で, led0 という名前のLEDが,どのGPIOの何番ピンにつながっているかの情報( led )を取り出しています. main() では,まず gpio_is_ready_dt() でGPIOが使えるか確かめ, gpio_pin_configure_dt() でそのピンを出力に設定します. while (1) のループで, gpio_pin_toggle_dt() でLEDを反転し, printf() で状態を表示し, k_msleep() で1秒待つ,をくり返します. ここで注目したいのは,コードのどこにも「どのピンにLEDがつながっているか」が書かれていないことです.コードが指定しているのは, led0 というエイリアス(別名)だけです. led0 が実際にどのLEDを指すのかは,Zephyr本体側にあるボードのデバイスツリー(ハードウェアの構成を書いたファイル. zephyr/boards/nxp/frdm_mcxn947/frdm_mcxn947.dtsi )で,次のように定義されています. aliases { led0 = &red_led; ... }; red_led: led_3 { gpios = <&gpio0 10 GPIO_ACTIVE_LOW>; label = "Red LED"; }; (抜粋) 「 led0 はRGB LEDの赤(GPIO0の10番ピン)だ」という情報がボード側に書かれているので,アプリケーションは led0 と言うだけで済みます. blinky のフォルダには,FRDM-MCXN947向けの追加設定( boards/ フォルダに置くオーバーレイ.ボードのデバイスツリーに差分を重ねるファイルで,3.3節で説明します)もありません.ボード側の定義だけで,そのまま動いているわけです.同じ blinky が, led0 を定義しているどのボードでもそのまま動くのはこのためです. アプリケーション・デバイスツリー・Kconfig・ドライバの関係を図にすると,図2.4-1のようになります.Kconfigは,Zephyrの機能のオン・オフを設定する仕組みです(3章でもう一度触れます). 図2.4-1:blinkyでの,アプリケーション・デバイスツリー・Kconfig・ドライバの関係 コラム: MCXなのに「kinetis」? 図の compatible = "nxp,kinetis-gpio" にある kinetis は,NXPの以前のマイコンシリーズ(Kinetis)の名前です.MCXのGPIOはKinetisと同じ系統の回路なので,Zephyrでは新しい名前を作らず,同じ名前・同じドライバ( gpio_mcux.c )を使っています. ここで出てきた「デバイスツリー」は,3章で温度センサーの仕組みを説明するときにもう一度登場します.3章では,ボード側の定義だけでは足りない部分を,アプリケーション側のオーバーレイで追加する例を見ます. ▲ 目次に戻る 3. 温度センサーを動かしてみる 3.1 サンプルをビルド・書き込む この節では,ボードに載っている温度センサーを読む thermometer サンプルを書き込みます.2章と同じように,作業用フォルダにコピーしてからビルド・書き込みます. # コマンド例3.1-1 Copy-Item -Recurse ~\zephyrproject\zephyr\samples\sensor\thermometer ~\handson\thermometer cd ~\handson\thermometer # コマンド例3.1-2 west build -p auto -b frdm_mcxn947//cpu0 . ビルドが成功したら,Tera Termが接続されたままか確認してから書き込みます(閉じてしまった場合は,2.2節の手順で接続し直します). # コマンド例3.1-3 west flash 3.2 値を確認する シリアルコンソールに,1秒ごとに気温の値が表示されます(図3.2-1).温度センサー(図1.3-1の④)を指で触ると,少しずつ値が上がっていくのが分かります. *** Booting Zephyr OS build v4.x.x *** Thermometer Example (arm) Temperature device is 0x1000b23c, name is p3t1755@4800000236152a0090 Temperature is 25.9°C Temperature is 25.8°C Temperature is 25.8°C 最初の数行は,サンプルの起動メッセージです. name is p3t1755@4800000236152a0090 の行から,3.3節で説明するデバイスツリーの定義どおり,P3T1755が温度センサーとして使われていることが分かります.( 0x1000b23c の部分は,ビルドによって変わることがあります.) 図3.2-1:Tera Termに温度が表示された画面 ✔ ここまでできればOK:Tera Termに1秒ごとに温度が表示され,センサーを指で触ると値が上がった うまくいかないときは 何も表示されない → Tera Termのタイトルバーに COM3 - Tera Term VT のようにCOMポートが表示されているか(接続中か)を確認し,ボードのRESETボタンを押します(A.5). °C の部分が文字化けする → Tera Termの文字コード設定によるもので,動作には問題ありません.気になる場合は,2.2節のTera Termの手順4で受信の文字コードを UTF-8 にしてください. Device ... is not ready のようなメッセージが出る → センサーの初期化に失敗しています.別のサンプル( hello_world など)を書き込んでいないか, cd ~\handson\thermometer でこのサンプルのフォルダにいる状態でビルドしたかを確認し,コマンド例3.1-2からやり直します. 3.3 なぜ動いたのか? この節では,センサーの値がなぜ表示されたのかを, src/main.c と boards/*.overlay を見ながら種明かしします. main.c 側では,センサーデバイスを次のように取得しています. static const struct device *const temp_dev = DEVICE_DT_GET(DT_ALIAS(ambient_temp0)); この ambient_temp0 というエイリアスが,実際にどのハードウェア(センサーの品種・接続バス)を指すのかを決めているのが, boards/*.overlay です.オーバーレイは,ボード本体のデバイスツリーに,アプリケーションごとの差分を重ねるファイルです. 今回のFRDM-MCXN947では,温度センサーはI3Cバスにつながっています. boards/frdm_mcxn947_mcxn947_cpu0.overlay には,次のように定義されています(実際のファイルにはI3Cバスのクロック速度の指定なども含まれますが,ここでは要点を抜粋しています). / { aliases { ambient-temp0 = &p3t1755; }; }; &i3c1 { status = "okay"; p3t1755: p3t1755@4800000236152a0090 { compatible = "nxp,p3t1755"; reg = <0x48 0x0236 0x152a0090>; status = "okay"; }; }; やっていることは2つだけです. aliases ブロックで, ambient-temp0 を p3t1755 ノードに結び付ける i3c1 バスの下に, nxp,p3t1755 ドライバを使うセンサーのノードを1つ追加する main.c 側は DT_ALIAS(ambient_temp0) としか書いていません.このセンサーがI2Cではなく(より高速な)I3Cでつながっていることも,レジスタアドレスの詳細も,アプリケーションのコードは一切知りません. ここでの見どころは,「コードを一行も書かずに,デバイスツリーの定義を変えるだけで別のハードウェア構成に対応できる」ことです.たとえば,同じセンサーをI2C接続のボードに載せ替えたり,別の温度センサーIC(対応ドライバがある前提)に変えたりする場合を考えます.変更するのは基本的に boards/*.overlay の中だけで, main.c の DEVICE_DT_GET(DT_ALIAS(ambient_temp0)) という1行はそのまま使い回せます.この関係を図にすると,図3.3-1のようになります. 図3.3-1:thermometerでの,アプリケーション・デバイスツリー・Kconfig・ドライバの関係 解説: デバイスツリーの実体はどこにあるのか FRDM-MCXN947のボード自体が持つデバイスツリーの本体は,Zephyr本体側の zephyr/boards/nxp/frdm_mcxn947/frdm_mcxn947_mcxn947_cpu0.dts (および共通定義の frdm_mcxn947.dtsi )です.ここには,ボードに標準で載っているペリフェラル一式が定義されています. このボード本体のデバイスツリーファイルは,直接書き換えません.Zephyrでは,アプリケーション側に,ボード本体の定義へ追記・上書きするための差分ファイル(オーバーレイ, *.overlay )を置きます. thermometerでは samples/sensor/thermometer/boards/frdm_mcxn947_mcxn947_cpu0.overlay ,5章で使うリポジトリでは boards/frdm_mcxn947_mcxn947_cpu0.overlay です.本文で示した p3t1755 ノードの定義も,このオーバーレイの中身です.ボード本体の定義はそのままに,アプリケーションごとに必要な差分だけを重ねられるのが利点です. ビルドすると,ボード本体のデバイスツリーとアプリケーションのオーバーレイがマージされ,最終的なデバイスツリー全体が build/zephyr/zephyr.dts に書き出されます.このファイルを開くと, p3t1755 ノードがボード本体の定義に追加されていることを確認できます. OSを使わないベアメタル開発や,デバイスツリーの仕組みを持たない多くのRTOSでは,センサーのI2C/I3Cアドレスやレジスタ操作を,アプリケーションのコードの中に直接書くのが一般的です.そのため,センサーの型番やバスの配線が変わるたびに,アプリケーションのロジックを書き換える必要があります.Zephyrでは,「どの部品が」「どこに」つながっているかという情報をデバイスツリーという別の層に追い出しています.そのため,ボードが変わっても,アプリケーションのロジック(この場合は「気温を読んで表示する」という処理)そのものは触らずに済みます. 興味を持った方は,他のボード向けの overlay ファイルも見比べてみてください.同じ samples/sensor/thermometer でも,ボードごとにハードウェアの接続だけが違う overlay が用意されていることが分かるはずです. ▲ 目次に戻る 4. HTTP Serverを動かしてみる この章では,PCとボードをEthernetケーブルでつなぎ,ボード上で動くHTTP Serverにブラウザからアクセスします(図4.1-1).これまではボード単体で完結していましたが,ここから一歩進めます. 使うのは,Zephyr公式サンプルの samples/net/sockets/http_server です.そのままビルド・書き込みして,ブラウザからLEDを点灯/消灯したり,シンプルなエンドポイント( /uptime のような,URLのパスごとに用意された窓口)の応答を確認したりします.5章では,この仕組みを土台にして,温度センサーの値をブラウザで見られるように拡張します. FRDM-MCXN947でHTTP Serverサンプルを動かす手順自体は,単発のブログ記事としてすでに公開されています.PC側のネットワーク設定や,固定IPの代わりにDHCPを使う場合の手順など,より詳しい内容はそちらも参照してください. Zephyr HTTP ServerをFRDM-MCXN947で動かしてみた(NXP Tech Blog,日本語) 4.1 サンプルをビルド・書き込む この節では, http_server サンプルのIPアドレスを書き換えてから書き込み,PCから ping で応答があることを確認します.今回は,TLSを使わないプレーンHTTPと,固定IPで進めます. 図4.1-1:PCとボードの接続とIPアドレス(USBケーブルとEthernetケーブル) ステップ1: サンプルを作業用フォルダにコピーする # コマンド例4.1-1 Copy-Item -Recurse ~\zephyrproject\zephyr\samples\net\sockets\http_server ~\handson\http_server cd ~\handson\http_server ステップ2: prj.conf のIPアドレスを書き換える 次のコマンドを実行すると,VSCodeで prj.conf が開きます. # コマンド例4.1-2 code prj.conf VSCodeの上部に「Restricted Mode is intended for safe code browsing.」という帯が表示されることがあります(後の図4.1-2の上部にも写っています).これは,開いたファイルを安全のため機能を制限したモードで表示している,という案内です. prj.conf の編集・保存には影響しないので,そのまま進めて構いません. ボードに固定IPアドレスを持たせるため, CONFIG_NET_CONFIG_MY_IPV4_ADDR の行を書き換えます.ファイルの中ほどにある # Network address config の下を見てください.元のサンプルでは,次のようになっています. CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1" CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2" CONFIG_NET_CONFIG_MY_IPV4_GW="192.0.2.2" 1行目(ファイルの54行目)の " で囲まれた値を,次のように書き換えます.VSCodeでは,行番号が左端に表示されています(図4.1-2).Zephyrのバージョンによっては,行番号が少しずれることがあります. CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.168.1.100" 3行目の CONFIG_NET_CONFIG_MY_IPV4_GW は,Gateway(ルーター)のアドレスです.PCとボードを直結する今回の構成では使われないので,書き換えなくても構いません.書き換える場合は "192.168.1.1" にします(この節の最後の解説を参照).2行目の CONFIG_NET_CONFIG_PEER_IPV4_ADDR は似ていますが別の設定で,今回は書き換えません. 注意: この行はPowerShellに貼り付けない 上の CONFIG_... の行はコマンドではなく,VSCodeで開いた prj.conf ファイルの中身です.PowerShellに貼り付けると「認識されません」というエラーになります(ファイルには何も影響しないので,間違えて貼り付けても問題はありません). 元の行を書き換えてください.元の行を残したまま末尾に行を追加すると,同じ設定が2回書かれた状態になり,どちらが有効か分かりにくくなります. Gatewayも書き換える場合: PEER_IPV4_ADDR の行と MY_IPV4_GW の行は,元の値がどちらも "192.0.2.2" です.値で探すと PEER の行を書き換えてしまいやすいので,行の先頭の CONFIG_... の名前で確かめてください. 値を囲む " (ダブルクォーテーション)は消さないでください. 編集したら, Ctrl + S で保存します.VSCodeのタブのファイル名の横に ● が付いている間は,まだ保存されていません.保存し忘れると,元のIPアドレスのままビルドされてしまいます. 図4.1-2は, MY_IPV4_ADDR の行を書き換えて保存したところです( MY_IPV4_GW の行は元のままにしています). 図4.1-2: prj.conf の MY_IPV4_ADDR を書き換えて保存したところ 会場のPC側のIPアドレスは,あらかじめこのボードと同じ 192.168.1.0/24 のサブネットに収まるよう設定済みです.PC側の設定を変更する必要はありません. ステップ3: ビルドして書き込む prj.conf を保存したら,PowerShellのウィンドウに戻ってビルドします.VSCodeの画面のままコマンドを貼り付けないよう注意してください. # コマンド例4.1-3 west build -p auto -b frdm_mcxn947//cpu0 . ビルドが成功したら書き込みます. # コマンド例4.1-4 west flash 書き込みの途中で, INFO: MCU-Link firmware update 'check': ... is running a firmware version which is older than the included firmware version のような表示が出ることがあります.ボードのMCU-Linkのファームウェアが,LinkServerに付属しているものより古いという案内です.書き込みはそのまま行われるので,気にせず進めてください. ステップ4: Ethernetケーブルをつないで,応答を確認する 書き込みが終わったら,PCとボードをEthernetケーブルで直結します.ケーブルを挿すのは,ボードのEthernetコネクタ(図1.3-1の⑤)です(図4.1-3). 図4.1-3:EthernetケーブルをボードのEthernetコネクタに挿す ボードがネットワークを使えるようになるまで数秒かかります.少し待ってから,PowerShellで次のコマンドを実行してください. # コマンド例4.1-5 ping 192.168.1.100 次のように, 192.168.1.100 からの応答 と表示されます(図4.1-4). 192.168.1.100 に ping を送信しています 32 バイトのデータ: 192.168.1.100 からの応答: バイト数 =32 時間 <1ms TTL=64 192.168.1.100 からの応答: バイト数 =32 時間 <1ms TTL=64 192.168.1.100 からの応答: バイト数 =32 時間 <1ms TTL=64 192.168.1.100 からの応答: バイト数 =32 時間 <1ms TTL=64 192.168.1.100 の ping 統計: パケット数: 送信 = 4、受信 = 4、損失 = 0 (0% の損失)、 ラウンド トリップの概算時間 (ミリ秒): 最小 = 0ms、最大 = 0ms、平均 = 0ms 図4.1-4: ping の実行結果 ✔ ここまでできればOK: 192.168.1.100 からの応答 が4回表示された.PCとボードの間で通信できています うまくいかないときは 「要求がタイムアウトしました」と表示される → 次の順に確認します. Ethernetケーブルが,PCとボードの両方にしっかり挿さっているか.ボード側のコネクタのLEDが点灯しているか. prj.conf のIPアドレスが 192.168.1.100 になっていて,保存されているか.保存し直した場合は,コマンド例4.1-3からビルドと書き込みをやり直します. シリアルコンソール(Tera Term)に,ボードの起動メッセージやIPアドレスの表示が出ているか.何も出ていなければ,RESETボタンを押してみます. それでもだめなら,A.6を参照してください. 「宛先ホストに到達できません」と表示される → PC側のIPアドレスが 192.168.1.x になっていない可能性があります(A.6). 解説: なぜプレーンHTTPを使うのか プレーンHTTPは,通信内容が暗号化されないHTTP通信です.実運用では,盗聴や改ざんを防ぐため,TLSで暗号化されたHTTPSの利用が推奨されます.HTTPSを使うには,サーバー証明書や秘密鍵といった認証まわりのファイルを,あらかじめ用意する必要があります. Zephyrでも,こうした認証の仕組みや,通信を暗号化する仕組みはサポートされています.しかし今回のハンズオンでは,限られた時間の中で認証ファイルの準備を省き,HTTP Serverそのものの仕組みを体験することに集中するため,あえてプレーンHTTPを使います. 解説: Gatewayの設定について Gateway( CONFIG_NET_CONFIG_MY_IPV4_GW )は,ルーターを経由しない通信では本来必須ではありません.今回はPCとボードをEthernetケーブルで直結し,同じ 192.168.1.0/24 のサブネットに両方を置きます.通信はすべて同一リンク内のARPで解決され,Gatewayを経由するルーティングは発生しません.そのため今回は,元の値のままでも, "192.168.1.1" に書き換えても,同じように動きます. ボードをルーターにつないで別のネットワークと通信する場合は,そのルーターのアドレスに書き換える必要があります. 4.2 ブラウザからLEDを制御してみる この節では,ブラウザからボードのLEDを点灯/消灯します. 注意: アドレスは http:// から入力する ブラウザのアドレス欄には, http:// を省略せず http://192.168.1.100/ のように入力してください. 最近のブラウザは, http:// を省略したり古い履歴を選んだりすると,自動的に暗号化通信の https:// で接続しようとします.今回のボードはプレーンHTTPで動いているので, https:// では接続できません.「接続がプライベートではありません」「安全な接続をサポートしていません」のような画面が出た場合は,アドレス欄が https:// になっていないか確認してください. なお,ページが表示されたとき,アドレス欄の左に「保護されていない通信」と表示されます.暗号化していないプレーンHTTPで接続しているためで,今回はこれで正常です. ブラウザで http://192.168.1.100/ にアクセスすると,LED制御用のページが表示されます(図4.2-1). 図4.2-1:ブラウザで開いたLED制御ページ このページには,「LED Control」のほかに,「Uptime Counter」(ボードの起動からの経過時間.4.3節で見る /uptime の値)と,「Network Statistics」(ボードが送受信したデータ量などの統計)も表示されます. ページの「LED on」「LED off」ボタンを押すと,ボード上のRGB LED(図1.3-1の③)が緑色に点灯/消灯します(図4.2-2).2.3節のLチカでは同じRGB LEDが赤く点滅しましたが,今回は緑です. 図4.2-2:ブラウザの操作でRGB LEDが緑に光る ✔ ここまでできればOK:ボタンの操作で,RGB LEDが緑色に点灯/消灯した このとき,Tera Termには [00:00:25.684,000] ... のような行が次々と流れ, uart:~$ という表示も出ています.HTTP Serverサンプルが出しているログと,Zephyrのシェル(コマンド入力の受付)の表示で,異常ではありません. うまくいかないときは pingは通るのにブラウザで表示されない → アドレスが http:// になっているか確認します.会社のPCなどでプロキシが設定されている場合は,プロキシを経由しない設定が必要です(A.6). ページは表示されるがLEDが反応しない → 赤ではなく緑に光るので,色を確かめてください.それでも反応しなければ,ブラウザを再読み込み( F5 )して,もう一度ボタンを押してみます. コラム: ネットワーク機能の「重さ」について 一般的な組み込み開発では,HTTPサーバーを1つ動かすだけでも,多くの下準備が必要になりがちです.TCP/IPスタック(lwIPなど)の組み込みと設定,ソケットAPIの用意,HTTPパーサーの選定と実装,静的ファイルの配信方法の実装,といった作業です. 今回は,それらがすべて prj.conf の CONFIG_NET_* ・ CONFIG_HTTP_* 系のオプションを y にするだけで揃っています.アプリケーション側で書いたのは,各エンドポイントのハンドラ関数(リクエストが来たときに呼び出される関数)だけです.ネットワークスタックからHTTPサーバーまでを,1つのビルドシステムとKconfigで一貫して扱える.これが,Zephyrがマイコン向けRTOSでありながら,比較的大きなミドルウェア込みで語られる理由のひとつです. コラム: ZephyrのTCP/IPスタックはlwIPなのか 組み込み向けのTCP/IPスタックというと,lwIP(lightweight IP)を思い浮かべる方も多いかもしれません.しかしZephyrは,lwIPを外部から取り込むのではなく, subsys/net/ 以下に実装された独自のTCP/IPスタックを標準で使っています.IPv4/IPv6・TCP/UDP・ソケットAPIといった機能は,Zephyr本体に組み込まれています. prj.conf で有効にしている CONFIG_NET_IPV4 や CONFIG_NET_TCP , CONFIG_NET_SOCKETS といったオプションも,このスタックの機能を指しています.(Zephyrは,モジュールとして外部のネットワークスタックを組み合わせる構成にも対応していますが,本記事で使っているのは標準のスタックです.) 4.3 /uptimeエンドポイントを覗いてみる この節では,一番シンプルなエンドポイントの動きを確かめます. ブラウザで http://192.168.1.100/uptime にアクセスすると,ボードの起動からの経過時間が表示されます. ✔ ここまでできればOK:ブラウザに,ボードの起動からの経過時間が表示された この「GETリクエストに対して,値を計算してテキストで返す」というエンドポイントの作りを,次の5章でそのまま応用します. 解説: HTTPリクエストとレスポンス ブラウザで /uptime にアクセスすると,ブラウザからボードへ,「このパスの内容をください」という意味の GET リクエストが送られます. ボード側では,リクエストを受け取ったHTTP Serverが, /uptime に紐づけられたハンドラ関数(リクエストが来たときに呼び出される関数.ここでは uptime_handler )を呼び出します. ハンドラは起動からの経過時間を計算し,その値をレスポンスの本文(body)としてセットします. HTTP Serverは,この本文にステータス行( 200 OK など)やヘッダーを付け加え,HTTPレスポンスとしてブラウザに返します. ブラウザは,受け取ったレスポンスの本文を画面に表示します. 5章で追加する /temperature エンドポイントも,リクエストを受けて値を計算し,本文として返すという,まったく同じ流れで動いています. ▲ 目次に戻る 5. HTTP Serverを改変して,温度センサーの値をブラウザで見る 5.1 何を追加したか この節では,4章のHTTP Serverサンプルに何を追加したのかを説明します. 4.3節で見た /uptime エンドポイントと同じ作りで,温度を返すエンドポイントを2つ追加しました.変更は src/main.c のごく一部です. エンドポイント ハンドラ関数 返すもの /temperature temperature_handler 温度を表示するHTMLページ /temperature.json temperature_json_handler その時点の温度の数値だけ(JSON) ブラウザで開くのは /temperature のページです.このページには小さなJavaScriptが埋め込まれていて,1秒ごとに /temperature.json へ値を問い合わせ,表示を書き換えます(図5.1-1). /uptime の値がトップページで自動的に更新されていくのと同じ仕組みです. static int temperature_handler(struct http_client_ctx *client, enum http_transaction_status status, const struct http_request_ctx *request_ctx, struct http_response_ctx *response_ctx, void *user_data) { /* 3章の温度センサーと同じ sensor_sample_fetch_chan / sensor_channel_get の流れで気温を取得し,HTMLとして返す */ ... } static int temperature_json_handler(struct http_client_ctx *client, enum http_transaction_status status, const struct http_request_ctx *request_ctx, struct http_response_ctx *response_ctx, void *user_data) { /* 同じ方法で気温を取得し,数値だけを返す */ ... } (抜粋) 追加したのは,次の2つだけです.他の部分(LED制御, /uptime ,静的ページの配信)は,元のサンプルのまま変更していません. この2つのハンドラ関数(センサーの読み取り部分は,共通の関数にまとめています) それぞれを /temperature ・ /temperature.json というパスに紐づける, HTTP_RESOURCE_DEFINE() の2行 図5.1-1: /temperature のページが温度を表示し,1秒ごとに更新する流れ 5.2 実際にブラウザで表示してみる この節では,改変済みのコードを書き込み,ブラウザで温度を表示します. 改変済みのコードは,GitHubのリポジトリteddokano/handson_http_server_tempで公開しています.作業用フォルダにクローンして,これまでと同じ手順でビルド・書き込みします. # コマンド例5.2-1 cd ~\handson git clone https://github.com/teddokano/handson_http_server_temp cd handson_http_server_temp 【会場PCにはクローン済みにしておくか(会場でインターネット接続が使えるか)を確認.クローン済みの場合は cd 以降だけを示す】 このリポジトリの prj.conf は,ボードのIPアドレスが4章と同じ 192.168.1.100 になるよう設定済みです. prj.conf を書き換える必要はありません. # コマンド例5.2-2 west build -p auto -b frdm_mcxn947//cpu0 . ビルドが成功したら書き込みます.Ethernetケーブルは,4章でつないだままで構いません. # コマンド例5.2-3 west flash 書き込み後,数秒待ってから,ブラウザで http://192.168.1.100/temperature にアクセスすると,次のようなページが表示されます(図5.2-1). Hello FRDM-MCXN947 temp sensor: P3T1755 Temperature: 24.312°C ← Back 温度の値は1秒ごとに自動で更新されます.温度センサー(図1.3-1の④)を指で触って,値が上がっていくのを確かめてみてください. http://192.168.1.100/temperature.json を直接開くと,ページの裏で問い合わせている数値だけの応答( 24.312 のような値)も見られます. 図5.2-1:ブラウザに表示された温度 ✔ ここまでできればOK:ブラウザに温度が表示され,センサーを指で触ると値が上がっていった うまくいかないときは git clone で「already exists and is not an empty directory」と出る → すでにクローン済みです.そのまま cd handson_http_server_temp を実行して先に進んでください. git clone で「Could not resolve host」などと出る → PCがインターネットに接続されていません.Wi-Fiなど,インターネットに出られるネットワークに接続してから再実行してください. /temperature を開くと「404 Not Found」になる → 4章の元のサンプルが書き込まれたままになっています.プロンプトが ...\handson_http_server_temp> になっているか確認し,コマンド例5.2-2からやり直してください. ページが開けない → 4.1節・4.2節の「うまくいかないときは」と同じ手順で確認します. 5.3 3章・4章の内容がどう組み合わさっているか この章で作ったアプリケーションは,新しい仕組みを使っていません.2〜4章で1つずつ動かした部品を組み合わせただけです.どの部品をどこで見たのかを,表にまとめます. 5章で使っているもの 5章のどこにあるか どこで見たか 温度センサーをエイリアスで取得する main.c の DEVICE_DT_GET(DT_ALIAS(ambient_temp0)) 3.3節(thermometerと同じ1行) 温度センサーをデバイスツリーに追加する boards/frdm_mcxn947_mcxn947_cpu0.overlay の p3t1755 ノード 3.3節(thermometerと同じオーバーレイ) センサーの機能を有効にする prj.conf の CONFIG_SENSOR=y 3.3節の図3.3-1 温度を読み取る sensor_sample_fetch_chan() ・ sensor_channel_get() 3章(thermometer) パスとハンドラ関数を結び付ける HTTP_RESOURCE_DEFINE() で /temperature ・ /temperature.json を登録 4.3節( /uptime と同じ書き方) 値を計算してレスポンスで返す temperature_handler ・ temperature_json_handler 4.3節の解説( uptime_handler と同じ流れ) ネットワークとHTTP Serverの機能 prj.conf の CONFIG_NET_* ・ CONFIG_HTTP_* 4章(元のサンプルのまま) それぞれ単体で動かした部品を組み合わせると,「ブラウザからセンサー値を見る」というアプリケーションになります.新しく書いたコードは,2つのハンドラ関数と,それを登録する2行だけです. 解説: main.cとCMakeLists.txtが少し煩雑に見える理由 src/main.c を開くと, /temperature のハンドラ以外にもかなりの行数があります.これは,HTTP Serverサンプルが,トップページのHTMLやJavaScript,LED制御・ /uptime などの元からあるエンドポイントも,あわせて用意しているためです.これらは温度センサーの追加とは直接関係のない「HTTPサーバーとして動くための足回り」なので,今回は5.1節の2つのハンドラ関数だけに注目すれば十分です.足回りの仕組みは,付録Gで説明しています. ▲ 目次に戻る 6. まとめ 6.1 今日体験したこと Zephyrの開発環境(west・デバイスツリー・Kconfig)に触れた Lチカ,温度センサー読み取り,HTTP Serverをそれぞれ動かした 2つのサンプルを組み合わせて,ブラウザでセンサー値を見るアプリケーションを作った 6.2 自分でもう一度やってみるための情報源 Zephyr Project公式ドキュメント 今回使ったサンプルコードのリポジトリ (teddokano/handson_http_server_temp) このブログ記事 さらに役に立つ情報→ NXPのZephyr OS ~まとめページ~ (日本語ブログ) 6.3 AIをどう活用できるか Zephyrのサンプルコードを読んだり改変したりするとき,AIアシスタントに「このサンプルにこういう機能を足すには」と相談すると,既存コードのパターンに沿った改変案を出してもらえます.今回の /temperature エンドポイントも,既存の /uptime エンドポイントと同じ構造で追加しています.分からない箇所があれば,まず既存のサンプルコードの近い部分を探し,AIと一緒に読み解いてみることをおすすめします. もう一つよくあるのが, west build でエラーになったときです.Zephyrのビルドでは,Kconfigの警告からCMakeのエラー,リンカのエラーまで,一度に大量のメッセージが出ます.どこが本当の原因なのか,初めは分かりにくいことがあります. そんなときも,エラーメッセージ全体をそのままAIに貼り付けて,「これはどういうエラーですか」と聞いてみるところから始めるのがおすすめです.付録Gで触れた sections-rom.ld まわりのリンクエラーも,この記事を書く過程で,AIと一緒に原因を特定したものです. 質問するときに伝えるとよい情報は,0.4節の「質問するときは」にまとめています.AIに相談するときも同じです. 6.4 後片付け 会場で参加された方 ボードやケーブルは,抜かずにそのままにしておいてください.PCの片付けも不要です.会場スタッフの案内に従ってください. ご自身のPCで試した方 PowerShell(またはターミナル)は,そのまま閉じて構いません.仮想環境の有効化や ZEPHYR_BASE の設定は,ウィンドウを閉じれば消えます.閉じずに仮想環境だけを抜けたい場合は, deactivate を実行します. 4章でPCのEthernetを固定IPに設定した場合は,元に戻しておきましょう.A.6のコラムと同じ画面で,「IP割り当て」を「自動(DHCP)」に戻します.戻さないと,そのEthernetポートを社内LANや自宅のルーターにつないだときに,インターネットにつながらなくなります. ボードには,最後に書き込んだプログラム(5章の温度表示つきHTTP Server)が残ります.電源を入れるたびにそのプログラムが動きますが, west flash でいつでも別のプログラムに書き換えられます. 作業用フォルダ ~\handson は,残しておけば今日の続きをいつでも試せます.不要になったら,フォルダごと削除して構いません( ~\zephyrproject は消さないでください). ▲ 目次に戻る 付録A. うまくいかないときは この付録では,コマンドをコピー&ペーストして進める中でつまずきやすいポイントと,その回復方法をまとめています.本文の各節にある「うまくいかないときは」から参照しています. A.1 まず確認すること エラーが出たら,原因を探す前に次の3点を確認してください.ほとんどのトラブルは,このどれかで見つかります. プロンプトの先頭に (.venv) が付いているか 付いていなければ,仮想環境が有効になっていません.PowerShellを開き直した,別のウィンドウで作業している,といった場合に起こります.コマンド例2.1-1を実行し直すか,会場PCでは zinit <作業中のサンプル名> (コマンド例2.1-2)を実行します. 今いるフォルダは正しいか プロンプトの PS C:\Users\<ユーザー名>\handson\thermometer> のような部分が,今作業している節のサンプルのフォルダになっているか確認します.違っていたら,その節の最初のコマンド例にある cd を実行し直します. 最初に出たエラーはどれか エラーが出ると後続のメッセージが大量に出ます.画面を上にスクロールして,最初に赤字や error が出た行を探してください.複数行をまとめて貼り付けた場合は,どのコマンドで最初にエラーが出たかを確認します. 次のコマンドで,今の状態をまとめて確認できます. # コマンド例A.1-1 Get-Location $env:ZEPHYR_BASE west topdir 1行目に今いるフォルダ,2行目に C:\Users\<ユーザー名>\zephyrproject\zephyr ,3行目に C:/Users/<ユーザー名>/zephyrproject が表示されれば,環境は正しく設定されています.2行目が空なら,コマンド例2.1-1を実行し直します. A.2 PowerShell・westまわり 症状 原因 回復方法 west : 用語 'west' は,コマンドレット,関数,スクリプト ファイル,または操作可能なプログラムの名前として認識されません 仮想環境が有効になっていない zinit <作業中のサンプル名> を実行する( zinit がないPCでは,コマンド例2.1-1を実行し,作業中のフォルダに cd で戻る) west: unknown command "build" ZEPHYR_BASE が設定されていない(westがワークスペースを見つけられない) zinit <作業中のサンプル名> を実行する( zinit がないPCでは,コマンド例2.1-1を実行し,作業中のフォルダに cd で戻る) Activate.ps1 で「このシステムではスクリプトの実行が無効になっている」 PowerShellの実行ポリシー Set-ExecutionPolicy -Scope CurrentUser RemoteSigned を実行してから,コマンド例2.1-1をやり直す cd や Copy-Item で「パスが存在しないため検出できません」 パスの打ち間違い,またはコピー元・コピー先のフォルダが無い コマンドを手で打たずにコピーし直す. ~\handson が無い場合はコマンド例2.2-1の1行目を実行する コマンドを貼り付けたのに何も起きず,プロンプトの後ろにコマンドが残っている 最後の行の改行がコピーされていない Enter を押す 貼り付けたら >> というプロンプトが出て止まった " の閉じ忘れなど,コマンドが途中で切れている Ctrl + C で中断し,コマンドを丸ごとコピーし直す ping や west の結果ではなく,日本語の文章がエラーになっている 記事の本文や prj.conf の中身まで貼り付けてしまった コードブロックの中だけをコピーし直す.PowerShell上で実行されただけなので,ファイルなどへの影響はない A.3 ビルドのエラー 症状 原因 回復方法 Invalid BOARD , No board named ... -b のボード名の打ち間違い コマンド例をコピーし直す. frdm_mcxn947//cpu0 の // も含めて正確に source directory ... does not contain a CMakeLists.txt など サンプルのフォルダ以外( ~\handson や ~\zephyrproject など)でビルドした その節のコマンド例の cd を実行してからビルドし直す Build directory ... is for application ... のように,別のアプリのビルドフォルダだと言われる 以前のビルドの build フォルダが残っている -p auto を -p always に変えてビルドし直す(下記) 設定を変えたのに反映されない,原因不明のエラーが続く ビルドフォルダに古い情報が残っている -p always でクリーンビルドする(下記) prj.conf を書き換えた後にKconfigのエラーが出る 書き換えの際に " や = を消してしまった prj.conf を見直す.分からなくなったらA.7の手順でサンプルをコピーし直す クリーンビルドは次のコマンドで行います.ビルドフォルダを消して最初から作り直すので,時間はかかりますが確実です. # コマンド例A.3-1 west build -p always -b frdm_mcxn947//cpu0 . エラーメッセージが長すぎて画面で追えない場合は,次のようにしてファイルに保存できます.保存した build_log.txt をVSCodeで開いて error で検索したり,内容をAIに貼り付けて相談したりするのに便利です. # コマンド例A.3-2 west build -p auto -b frdm_mcxn947//cpu0 . 2>&1 | Out-File build_log.txt code build_log.txt ビルドが遅いとき(ご自身のPCで試している場合) WindowsでのZephyrのビルドは,同じPCのLinuxより遅くなりがちです.http_serverのような大きめのサンプルでは,1回のビルドに数分かかることもあります.次の3つで大きく改善します(会場PCは設定済みです). ccacheを入れる: 一度コンパイルした結果を覚えておき,同じものを2回目以降は再利用するツールです.インストールしておけば,Zephyrが自動で使います.同じサンプルを同じフォルダでビルドし直すときは, -p always でクリーンビルドした場合も含めて,コンパイルの大部分が省略されて速くなります(最初の1回は通常どおり時間がかかります).筆者の環境では,http_serverのクリーンビルドが約4分半から73秒になりました. Windows Defenderの除外設定: 「Windows セキュリティ」→「ウイルスと脅威の防止」→「設定の管理」→「除外」を開きます. ~\zephyrproject ・作業用フォルダ・Zephyr SDKのフォルダ・ccacheのキャッシュフォルダ(通常は %LOCALAPPDATA%\ccache )を,除外に追加します.Defender以外のウイルス対策ソフトを使っている場合は,そのソフトの設定で除外します.ビルドのたびに大量のファイルがスキャンされるのを防げます.会社のPCでは,情報システム部門の方針を確認してください. 電源設定: ノートPCはACアダプタをつなぎ,電源モードを「最適なパフォーマンス」にします. # コマンド例A.3-3 winget install --id Ccache.Ccache -e インストールしたらPowerShellを開き直し, zinit (またはコマンド例2.1-1)で環境を設定してから, -p always でビルドし直してください.ccacheを入れる前に作ったbuildフォルダは,ccacheを使わない設定のままになっているためです. ご自身のPCで試している場合 Windowsのユーザー名に日本語やスペースが含まれていると,環境によってはビルドツールがパスを正しく扱えずにエラーになることがあります.その場合は, ~\handson の代わりに C:\handson のような英数字だけの短いパスを作業用フォルダにしてみてください. A.4 書き込み(west flash)のエラー 症状 原因 回復方法 No probe found など,デバッグプローブが見つからないというエラー USBケーブルがボードのデバッグ用コネクタ(MCU-Link側)に挿さっていない.または充電専用のUSBケーブルを使っている 挿すコネクタを確認し,抜き差ししてから再実行.データ通信対応のケーブルを使う プローブが使用中,接続できないというエラー MCUXpresso for VSCなど,別のツールでデバッグ中 他のツールのデバッグセッションを終了してから再実行 west flash がビルドからやり直そうとしてエラーになる 直前のビルドが失敗している west flash ではなく,ビルドのエラーを先に解決する(A.3) 書き込みが途中で止まった,ボードが反応しない 一時的な通信エラー USBケーブルを抜いて数秒待ってから挿し直し, west flash を再実行 west flash は何度実行しても問題ありません.失敗したら,原因を直してから同じコマンドを再実行してください. A.5 シリアルコンソールに何も出ない 症状 原因 回復方法 Tera Termの「新しい接続」で「シリアル」が選べない,ボードのポートが出てこない USBケーブルが挿さっていない,または充電専用ケーブル ケーブルを確認してから,「ファイル」→「新しい接続」で画面を開き直す(開き直すとポート一覧が更新される) 「ポートが使用中」「シリアルポートを開けません」で接続できない 別のウィンドウのTera Termや,VSCodeのシリアルモニターなどが同じポートを開いている 他のウィンドウ・アプリでの接続を閉じてから接続し直す 全体が意味のない記号の羅列になる ボーレートが違う 「設定」→「シリアルポート」で115200に設定し直す 接続しているのに何も出ない 出力のタイミングを逃した ボードのRESETボタンを押す USBケーブルを抜き差ししたら表示が止まった 抜き差しでTera Termの接続が切れた 「ファイル」→「接続断」のあと,「ファイル」→「新しい接続」でポートを選び直す.COM番号が変わっていることがあるので注意 ° などの記号が文字化けする 受信の文字コードがUTF-8になっていない 2.2節のTera Termの手順4のとおり,受信の文字コードを UTF-8 にする A.6 ネットワーク・ブラウザまわり PC側のIPアドレスは,PowerShellで次のコマンドを実行して確認できます. # コマンド例A.6-1 ipconfig 「イーサネット アダプター」の欄の「IPv4 アドレス」を確認してください. 症状 原因 回復方法 IPv4アドレスが 169.254.x.x になっている PCのEthernetが固定IPに設定されていない PC側を 192.168.1.10 / 255.255.255.0 などに設定する(下記) IPv4アドレスが 192.168.1.100 になっている PCとボードのIPアドレスが重複している PC側を 192.168.1.100 以外(例: 192.168.1.10 )にする PCのWi-Fiも 192.168.1.x のネットワークにつながっている 通信がWi-Fi側に流れてしまい,ボードに届かない 実習の間だけWi-Fiを切断する pingは通るが,ブラウザで開けない https:// で接続しようとしている,またはプロキシ経由になっている アドレスを http://192.168.1.100/ と入力し直す.プロキシを使っている場合は, 192.168.1.100 をプロキシの例外に追加する prj.conf を直したのにIPアドレスが変わらない 保存していない,またはビルドし直していない VSCodeで Ctrl + S で保存し,コマンド例4.1-3からやり直す ご自身のPCで試している場合: PC側の固定IPの設定 Windowsの「設定」→「ネットワークとインターネット」→「イーサネット」を開き,「IP割り当て」の「編集」から「手動」を選びます.IPv4をオンにして,IPアドレスに 192.168.1.10 ,サブネットマスクに 255.255.255.0 を入力して保存します(ゲートウェイは空欄で構いません).詳しくは4章の冒頭で紹介したHTTP Serverのブログ記事も参照してください. 図A.6-1:Windowsの「設定」でEthernetを固定IPにする画面 A.7 どうしても直らないときのリセット手順 何が原因か分からなくなったときは,その節を最初からやり直すのが一番の近道です.作業用フォルダにコピーしたサンプルを消しても, ~\zephyrproject の中の元のサンプルは残っているので,何度でもやり直せます. PowerShellのウィンドウを閉じて,新しく開きます. コマンド例2.1-1(会場PCでは zinit )を実行し,環境を設定し直します. やり直したい節のサンプルのフォルダを削除します.たとえば4章をやり直す場合は次のようにします. # コマンド例A.7-1 cd ~\handson Remove-Item -Recurse -Force ~\handson\http_server フォルダ名の部分( http_server )は,やり直す節に合わせて hello_world ・ blinky ・ thermometer ・ handson_http_server_temp に読み替えてください. ~\handson や ~\zephyrproject そのものを消さないよう注意してください. その節の最初のコマンド例(コピーと cd )からやり直します. ボードの動作がおかしいときは,USBケーブルとEthernetケーブルを両方抜いて数秒待ち,挿し直してから west flash をやり直してください. ▲ 目次に戻る 変更履歴:【公開日】:初版 ========================= 本投稿の「Comment」欄にコメントをいただいても、現在返信に対応しておりません。お手数をおかけしますが、お問い合わせの際には「NXPへの技術質問 - 問い合わせ方法 (日本語ブログ)」をご参照ください。(既に弊社NXP代理店、もしくはNXPとお付き合いのある方は、直接担当者へご質問いただいてもかまいません。) この記事は,ハンズオン・セミナー「Zephyrデビュー会」の進行ガイドとして書かれたものです.実際のセミナーでは,この記事の手順に沿って一緒に手を動かしていきます. セミナーに参加していない方も,この記事だけを読みながら,同じ内容を自分の環境で再現できます.そのために,コマンド例や確認ポイントを省略せずに書いています.読み物としての解説記事というより,実際に「動くところまで」たどり着くための作業ガイドだと捉えてください. (作業時間:90分) ※Zephyr OSをインストールしている前提 日本語ブログ introduction Sensor Ethernet Interface MCX
記事全体を表示
Zephyr Debut Meeting ~90 Minutes to Get the FRDM-MCXN947 Working Without Stumbling~ [Appendix] (Japanese Blog) This article is an appendix to "Zephyr Debut Session ~90 Minutes to Get FRDM-MCXN947 Working Without Stumbling~". It provides commands for testing in a Linux environment, applications of the main content, a glossary, and more. (The main instructions (Chapters 0-6) and troubleshooting (Appendix A) are included in the main text .) table of contents Appendix B. List of command examples (PowerShell/bash/zsh) Chapter 2: Trying out the development environment Chapter 3: Trying out a temperature sensor Chapter 4: Trying out an HTTP Server Chapter 5: Modifying the HTTP Server to view temperature sensor values in a browser Appendix A: When things don't work Appendix C: Let's try it! Appendix C. Let's try it (for those who finish early) C.1 Changing the blinking interval of the LED. C.2 Changing the Hello World message C.3 Display the temperature in Fahrenheit as well. C.4 Changing the number of digits displayed for temperature in the browser Appendix D. To try with other FRDM boards D.1 Find the board name D.2 Notes for each sample Appendix E. Glossary Appendix F. Frequently Asked Questions Appendix G.HTTP Server Sample Framework Main story (another article): 0. What is this article about? 1. What is Zephyr? 2. Try out the development environment 3. Try operating the temperature sensor. 4. Try running the HTTP Server. 5. Modify the HTTP Server to view the temperature sensor values in a browser. 6. Summary Appendix A. When things don't work Appendix B. List of command examples (PowerShell/bash/zsh) The command examples that appear in this text are listed in numerical order. This text assumes you are using PowerShell on Windows, but we have also included the bash and zsh versions for those who are trying it on their macOS or Linux environment. It is assumed that you have ~/zephyrproject the environment setup as described in Section 0.3 of the official Getting Started Guide. Notes on trying this with bash or zsh Path separators : \ in PowerShell is / in bash and zsh. ~ represents the home folder, which is common to both. The command examples do not have numbered lines : macOS's standard zsh does not treat # entered interactively as a comment by default. Therefore, pasting a line with # コマンド例2.1-1 will result in the error "command not found: #". The bash and zsh versions of the code blocks do not include numbered lines; instead, the numbers are indicated by the headings. Virtual environments and ZEPHYR_BASE : Just like with PowerShell, both are only valid within that terminal. If you reopen the terminal, restart the command from example 2.1-1. The serial port name is /dev/tty.usbmodemXXXX on macOS, /dev/ttyACM0 on Linux, etc. ( Section 2.2 ). Differences in syntax between PowerShell and bash/zsh This list compiles only the differences in syntax between PowerShell and bash/zsh. It does not include elements that are the same in both, such as cd , west build , west flash , and git clone . Things to do PowerShell bash/zsh Examples of commands that appear path separator ~\handson ~/handson whole Activating the virtual environment .\.venv\Scripts\Activate.ps1 source .venv/bin/activate 2.1-1 ZEPHYR_BASE Settings $env:ZEPHYR_BASE = "..." source zephyr/zephyr-env.sh 2.1-1 Open the configuration file (profile). notepad $PROFILE.CurrentUserAllHosts code ~/.bashrc / code ~/.zshrc 2.1-3 Create a folder New-Item -ItemType Directory -Force mkdir -p 2.2-1 Copy the entire folder. Copy-Item -Recurse cp -r 2.2-1, 2.3-1, 3.1-1, 4.1-1 ping Stop after 4 times ping (It stops after 4 attempts from the start) ping -c 4 4.1-5 Show the current folder Get-Location pwd A.1-1 Display environment variables $env:ZEPHYR_BASE echo $ZEPHYR_BASE A.1-1 Save the log to a file. 2>&1 | Out-File build_log.txt > build_log.txt 2>&1 A.3-2 Install ccache winget install --id Ccache.Ccache -e brew install ccache (macOS) / sudo apt install ccache (Ubuntu) A.3-3 Check your PC's IP address ipconfig ifconfig (macOS) / ip addr (Linux) A.6-1 Delete folder Remove-Item -Recurse -Force rm -rf A.7-1 Chapter 2: Trying out the development environment Command Example 2.1-1 Environment Preparation and Verification ( Section 2.1 ) # コマンド例2.1-1 cd ~\zephyrproject .\.venv\Scripts\Activate.ps1 $env:ZEPHYR_BASE = "$HOME\zephyrproject\zephyr" west --version west topdir cd ~/zephyrproject source .venv/bin/activate source zephyr/zephyr-env.sh west --version west topdir In bash and zsh, zephyr-env.sh sets ZEPHYR_BASE . Writing export ZEPHYR_BASE=~/zephyrproject/zephyr directly has the same effect. Command Example 2.1-2: Setting up the environment and moving to the working folder all at once ( Section 2.1 ) # コマンド例2.1-2 zinit thermometer zinit thermometer To use zinit in bash or zsh, add the following function to the file opened in command example 2.1-3, save it, and then reopen the terminal (this is what you write to the file, not the command you paste into the terminal). zinit() { cd ~/zephyrproject || return source .venv/bin/activate source zephyr/zephyr-env.sh west --version west topdir if [ -n "$1" ]; then cd ~/handson/"$1"; fi } Command example 2.1-3 Open the profile (shell configuration file) ( Section 2.1 ) # コマンド例2.1-3 if (-not (Test-Path $PROFILE.CurrentUserAllHosts)) { New-Item -Force $PROFILE.CurrentUserAllHosts | Out-Null } notepad $PROFILE.CurrentUserAllHosts For bash: code ~/.bashrc For zsh (the default on macOS): code ~/.zshrc Command example 2.2-1: Copy hello_world to the working folder ( Section 2.2 ) # コマンド例2.2-1 New-Item -ItemType Directory -Force ~\handson | Out-Null Copy-Item -Recurse ~\zephyrproject\zephyr\samples\hello_world ~\handson\hello_world cd ~\handson\hello_world mkdir -p ~/handson cp -r ~/zephyrproject/zephyr/samples/hello_world ~/handson/hello_world cd ~/handson/hello_world cp -r Similar to Copy-Item , if the destination folder already exists, the contents will be copied into it in a nested manner. Command example 2.2-2 Building hello_world ( Section 2.2 ) # コマンド例2.2-2 west build -p auto -b frdm_mcxn947/mcxn947/cpu0 . west build -p auto -b frdm_mcxn947/mcxn947/cpu0 . Command example 2.2-3: Writing hello_world ( Section 2.2 ) # コマンド例2.2-3 west flash west flash In Linux, if USB device access permissions (udev rules) are not configured, the programming tool may not be able to find the debug probe. In that case, configure the udev rules as described in the installation instructions for the programming tool (such as LinkServer). Command example 2.3-1: Copy blinky to the working folder ( Section 2.3 ) # コマンド例2.3-1 Copy-Item -Recurse ~\zephyrproject\zephyr\samples\basic\blinky ~\handson\blinky cd ~\handson\blinky cp -r ~/zephyrproject/zephyr/samples/basic/blinky ~/handson/blinky cd ~/handson/blinky Command example 2.3-2 Building blinky ( Section 2.3 ) # コマンド例2.3-2 west build -p auto -b frdm_mcxn947//cpu0 . west build -p auto -b frdm_mcxn947//cpu0 . Command Example 2.3-3: Writing to blinky ( Section 2.3 ) # コマンド例2.3-3 west flash west flash Chapter 3: Trying out a temperature sensor Command example 3.1-1 Copy thermometer to the working folder ( Section 3.1 ) # コマンド例3.1-1 Copy-Item -Recurse ~\zephyrproject\zephyr\samples\sensor\thermometer ~\handson\thermometer cd ~\handson\thermometer cp -r ~/zephyrproject/zephyr/samples/sensor/thermometer ~/handson/thermometer cd ~/handson/thermometer Command example 3.1-2 Building thermometer ( Section 3.1 ) # コマンド例3.1-2 west build -p auto -b frdm_mcxn947//cpu0 . west build -p auto -b frdm_mcxn947//cpu0 . Command Example 3.1-3: Writing to the thermometer ( Section 3.1 ) # コマンド例3.1-3 west flash west flash Chapter 4: Trying out an HTTP Server Command example 4.1-1 Copy http_server to the working folder ( Section 4.1 ) # コマンド例4.1-1 Copy-Item -Recurse ~\zephyrproject\zephyr\samples\net\sockets\http_server ~\handson\http_server cd ~\handson\http_server cp -r ~/zephyrproject/zephyr/samples/net/sockets/http_server ~/handson/http_server cd ~/handson/http_server Command example 4.1-2 Open prj.conf in VS Code ( Section 4.1 ) # コマンド例4.1-2 code prj.conf code prj.conf If code results in "command not found" on macOS, open the command palette ( Cmd + Shift + P ) in VS Code, run "Shell Command: Install 'code' command in PATH", and then reopen Terminal. Command example 4.1-3 Building the http_server ( Section 4.1 ) # コマンド例4.1-3 west build -p auto -b frdm_mcxn947//cpu0 . west build -p auto -b frdm_mcxn947//cpu0 . Command example 4.1-4: Writing to http_server ( Section 4.1 ) # コマンド例4.1-4 west flash west flash Command example 4.1-5 Checking communication with the board ( Section 4.1 ) # コマンド例4.1-5 ping 192.168.1.100 ping -c 4 192.168.1.100 Unlike Windows, ping in macOS and Linux will continue sending until you stop it with Ctrl+C. -c 4 specifies that it will stop after sending 4 times. The response will be displayed as 64 bytes from 192.168.1.100: ... . Chapter 5: Modifying the HTTP Server to view temperature sensor values in a browser Command example 5.2-1 Obtain the modified repository ( Section 5.2 ) # コマンド例5.2-1 cd ~\handson git clone https://github.com/teddokano/handson_http_server_temp cd handson_http_server_temp cd ~/handson git clone https://github.com/teddokano/handson_http_server_temp cd handson_http_server_temp Command example 5.2-2 Building a modified version ( Section 5.2 ) # コマンド例5.2-2 west build -p auto -b frdm_mcxn947//cpu0 . west build -p auto -b frdm_mcxn947//cpu0 . Command Example 5.2-3 Writing a Modified Version ( Section 5.2 ) # コマンド例5.2-3 west flash west flash Appendix A: When things don't work Command Example A.1-1: Check the current status ( A.1 ) # コマンド例A.1-1 Get-Location $env:ZEPHYR_BASE west topdir pwd echo $ZEPHYR_BASE west topdir Command example A.3-1 Clean build ( A.3 ) # コマンド例A.3-1 west build -p always -b frdm_mcxn947//cpu0 . west build -p always -b frdm_mcxn947//cpu0 . Command example A.3-2: Save the build log to a file ( A.3 ) # コマンド例A.3-2 west build -p auto -b frdm_mcxn947//cpu0 . 2>&1 | Out-File build_log.txt code build_log.txt west build -p auto -b frdm_mcxn947//cpu0 . > build_log.txt 2>&1 code build_log.txt Command example A.3-3 Installing ccache ( A.3 ) # コマンド例A.3-3 winget install --id Ccache.Ccache -e For macOS (Homebrew): brew install ccache For Linux (Ubuntu): sudo apt install ccache Command example A.6-1Check the IP address of your PC ( A.6 ) # コマンド例A.6-1 ipconfig For macOS: ifconfig For Linux: ip addr On macOS, the IPv4 address is displayed on inet line of the interface, such as en0 , while on Linux, it is displayed on inet line, such as eth0 or enp... . Command example A.7-1 Delete the working sample and start over ( A.7 ) # コマンド例A.7-1 cd ~\handson Remove-Item -Recurse -Force ~\handson\http_server cd ~/handson rm -rf ~/handson/http_server rm -rf This will delete the item without confirmation and it will not remain in the Recycle Bin. If you mistype the folder name, a different folder may be deleted, so please make sure you haven't specified ~/handson or ~/zephyrproject itself before executing. Appendix C: Let's try it! Command example C.1-1: Open blinky's main.c ( C.1 ) # コマンド例C.1-1 cd ~\handson\blinky code src\main.c cd ~/handson/blinky code src/main.c Command example C.2-1: Open main.c in hello_world ( C.2 ) # コマンド例C.2-1 cd ~\handson\hello_world code src\main.c cd ~/handson/hello_world code src/main.c Command example C.3-1: Open main.c of thermometer ( C.3 ) # コマンド例C.3-1 cd ~\handson\thermometer code src\main.c cd ~/handson/thermometer code src/main.c Command example C.4-1 Open main.c of the modified HTTP Server ( C.4 ) # コマンド例C.4-1 cd ~\handson\handson_http_server_temp code src\main.c cd ~/handson/handson_http_server_temp code src/main.c Command example C.4-2 Revert the modified main.c to its original version ( C.4 ) # コマンド例C.4-2 git checkout -- src/main.c git checkout -- src/main.c Command Example D.1-1Display a list of FRDM-MCX boards that Zephyr supports ( D.1 ) # コマンド例D.1-1 west boards -n frdm_mcx west boards -n frdm_mcx ▲ Return to Table of Contents Appendix C. Let's try it (for those who finish early) These are assignments for those who finished the main instructions quickly or who want to play around with them a bit when they get home. You can try each of them by simply changing a few lines of sample code. After changing the code, be sure to save it by pressing Ctrl + S in VS Code, then build and flash the file. If you forget which code you rewrote, you can always revert to the original by copying the sample again using the procedure in A.7 . C.1 Changing the blinking interval of the LED. Let's try making the blinking of blinky that we moved in Section 2.3 faster. # コマンド例C.1-1 cd ~\handson\blinky code src\main.c main.c Find the following line within it. #define SLEEP_TIME_MS 1000 1000 This means "switch the LED every 1000 milliseconds = 1 second". Change this to 200 or something similar, save it, build with command example 2.3-2 , and write with command example 2.3-3 . If the blinking becomes faster, it's successful. C.2 Changing the Hello World message # コマンド例C.2-1 cd ~\handson\hello_world code src\main.c printf("Hello World! %s\n", CONFIG_BOARD_TARGET); Try replacing Hello World! part with your favorite string. After saving, build it using command example 2.2-2 while Tera Term is connected, and then write it using command example 2.2-3 . Japanese characters can also be displayed. See Tera in Section 2.2.If you set the character encoding for receiving messages in Tera Term to UTF-8 in step 4 of the Tera Term procedure, it should display correctly. If the characters are garbled, please check that setting. C.3 Display the temperature in Fahrenheit as well. In Chapter 3, thermometer , let's try displaying Fahrenheit (°F) in addition to Celsius (°C). # コマンド例C.3-1 cd ~\handson\thermometer code src\main.c main() Look for the following line, near the end of the file. printf("Temperature is %0.1lf°C\n", sensor_value_to_double(&value)); Hint : Fahrenheit can be calculated using the formula: Celsius × 9 ÷ 5 + 32. Example answer : Replace the line above with the following two lines. double c = sensor_value_to_double(&value); printf("Temperature is %0.1lf°C (%0.1lf°F)\n", c, c * 9.0 / 5.0 + 32.0); After saving, build using command example 3.1-2 and write using command example 3.1-3 . If it displays as Temperature is 24.3°C (75.7°F) , it was successful. C.4 Changing the number of digits displayed for temperature in the browser In the modified HTTP Server in Chapter 5, the temperature is displayed with three decimal places. Let's change this to one decimal place. # コマンド例C.4-1 cd ~\handson\handson_http_server_temp code src\main.c Hint : The following two places within main.c are involved in what is displayed on the screen. Try searching for them in VS Code ( Ctrl + F ). temperature_handler The format when the page is first displayed: "Temperature: %.3f°C" Inside the JavaScript that updates the display every second: t.toFixed(3) %.3f If you search for it, you'll find another instance within temperature_json_handler . This is used to pass a numerical value to the browser, so you don't need to change it this time. Example answer : Replace the two places above with "Temperature: %.1f°C" and t.toFixed(1) respectively. After saving, build with command example 5.2-2 , write with command example 5.2-3 , and reopen http://192.168.1.100/temperature in your browser. If you rewrite only one of them, the result will be that the number is displayed as three digits immediately after opening the page, and then becomes a single digit after one second (or vice versa). This shows that both the C language side, which returns the HTML, and the JavaScript side, which runs in the browser, are involved in the display. Since this folder was obtained using Git, you can revert it to its previous state using the following command. # コマンド例C.4-2 git checkout -- src/main.c ▲ Return to Table of Contents Appendix D. To try with other FRDM boards This article assumes the use of the FRDM-MCXN947, but Zephyr is also compatible with other NXP FRDM boards. Here are some tips for when you have a different board on hand. D.1 Find the board name west build The board names to be specified in -b can be listed using the following command. # コマンド例D.1-1 west boards -n frdm_mcx The main methods for specifying boards are as follows: board -b Name to specify FRDM-MCXN947 frdm_mcxn947//cpu0 (This article) FRDM-MCXN236 frdm_mcxn236 FRDM-MCXA153 frdm_mcxa153 FRDM-MCXA156 frdm_mcxa156 FRDM-MCXC444 frdm_mcxc444 Except for dual-core boards like the FRDM-MCXN947, you can specify the board by name alone (see Section 2.2 for details). [Confirm the board names in this table using the Zephyr version on the venue PC.] D.2 Notes for each sample hello_world : This will work out of the box on almost all boards. Serial port names and baud rates may vary depending on the board, so please check the documentation for your specific board. blinky : If the alias led0 is defined in the board's device tree, it will work as is ( Section 2.4 ). thermometer : A temperature sensor is mounted on the board, and an overlay is needed to point to that sensor as ambient-temp0 . Check if there is .overlay for the board you want to use in samples/sensor/thermometer/boards/ . If not, you will need to write your own overlay, referring to the overlays shown in section 3.3. http_server : This will only work on boards with Ethernet. Also, the programming tool used by west flash may vary depending on the board. Please check the supported programming tools on the page for each board in the official Zephyr documentation (search for the board name under "Supported Boards"). ▲ Return to Table of Contents Appendix E. Glossary This article provides a brief explanation of the terms used, in alphabetical order and then in Japanese syllabary order. term explanation ELF file The program file created during the build process, which is written to the microcontroller. It also includes debug information ( build/zephyr/zephyr.elf ). Gateway This is the address of the router, the device that acts as the exit point when communicating with another network. It is not used in this configuration where the PC and the board are directly connected. HTTP/HTTPS This is a set of rules for communication between a browser and a web server. HTTPS encrypts the communication, while this board uses unencrypted HTTP. IP address A number used to distinguish devices on a network. In this case, the board is 192.168.1.100 and the PC is 192.168.1.x . Kconfig This is the mechanism for turning Zephyr's functions on and off. You write it like this: prj.conf becomes CONFIG_xxx=y . LinkServer NXP's programming and debugging tool. west flash is used for programming the FRDM-MCXN947. MCU-Link This is the programming and debugging circuit on the FRDM board. When connected to a PC via USB, it can be used for programming and serial communication (virtual COM port). ping A command that sends a small amount of data to the other device and checks if a response is received to determine if the network is connected. Pristine build (clean build) Delete the build folder and rebuild from scratch. Specify with -p always prj.conf Kconfig configuration files for each application west Zephyr command-line tools. They handle Zephyr operations such as building ( west build ) and writing ( west flash ) all in one place. Zephyr SDK A complete set of tools, including compilers, used to build Zephyr. alias Aliases are used to assign a node in the device tree, such as led0 or ambient-temp0 . Applications use aliases to specify devices, so code doesn't need to be changed even when the board changes. endpoint In an HTTP server, "windows" like /uptime and /temperature are provided for each URL path. Each of these has a corresponding handler function. overlay A file ( *.overlay ) overlays the differences for each application onto the device tree of the board itself. write Transfer the built program to the microcontroller. This is done using west flash Virtual environment (Python virtual environment) This is a standalone Python environment containing all the necessary Python tools for Zephyr. When enabled, (.venv) will appear in the prompt. Current directory The current folder. In PowerShell, this is displayed in PS C:\Users\...> part of the prompt. subnet A set of IP addresses that can communicate directly. 192.168.1.0/24 represents a set of addresses that begin with 192.168.1. . Serial console This is a screen for displaying the board's printf output and other information on a PC. This article uses Tera Term. Device Tree A mechanism for describing the hardware configuration, specifically "which components are connected to where." This is defined for each board. Handler function A function that is called when a certain request or event occurs. build To create a program that can be written to a microcontroller from source code. prompt In PowerShell, the PS C:\Users\...> symbol indicates that the system is waiting for command input. baud rate Serial communication speed. This article uses 115200. workspace This is the folder where the west/Zephyr main program and various modules are all located. ~\zephyrproject ▲ Return to Table of Contents Appendix F. Frequently Asked Questions Q. Can Zephyr be used in commercial products? A. The Zephyr core is released under the Apache License 2.0 and can be used in commercial products. However, some modules (external libraries) used with Zephyr may be under different licenses, so when incorporating them into your product, please also check the licenses of the modules you are using. Q. How should I choose between MCUXpresso SDK and Zephyr? A. Both can be used with NXP microcontrollers. MCUXpresso SDK is a driver and sample package provided for NXP microcontrollers and is suitable for bare-metal and FreeRTOS development. Zephyr's strengths are its ability to develop using the same syntax and procedures across boards and vendors, and its rich middleware, including networking. You should choose based on your existing assets and whether you plan to expand to other microcontrollers in the future. Q. I used the command line today, but can I do the same with MCUXpresso for VSC? A. Yes. You can also build, write, and debug Zephyr applications from MCUXpresso for VSC. The reason we used the command line in this article is to let you experience a procedure that is common to all boards and vendors ( Section 2.1 ). Q. Is it possible to debug using breakpoints? A. Yes, you can. You can launch the debugger from the command line using west debug . If you prefer to use a GUI, MCUXpresso for VSC is the easiest option. Q. What version of Zephyr did you use today? A. [Enter the Zephyr version of the venue PC]. You can also check this in the *** Booting Zephyr OS build v4.x.x *** line displayed on the serial console when the board starts up. If the version is different, the contents and output of the sample may differ slightly from those in this article. Q. Can this be done on macOS or Linux instead of Windows? A. Yes, you can. Please refer to the official Getting Started Guide for environment setup. Instructions for writing commands in both bash and zsh are included in Appendix B. Q. How does the output of printf reach the PC? A. Characters output from the microcontroller's UART (serial communication) are sent to the PC via the MCU-Link on the board and then to the USB virtual COM port. Therefore, the serial console can also be used with the same USB cable used for programming. ▲ Return to Table of Contents Appendix G. HTTP Server Sample Framework Chapter 5's repository src/main.c and CMakeLists.txt contain a considerable amount of information that is not directly related to adding a temperature sensor. This is because the HTTP Server sample also provides all the data that it delivers as a web server (the HTML for the top page, JavaScript, and several pre-existing endpoints such as LED control, /uptime , and network statistics). This section explains the "foundation for running as an HTTP server." You don't need to read this if you are just following the steps in Chapter 5. Embedding HTML and JavaScript index.html main.js is compressed with gzip during the build process by generate_inc_file_for_target() within CMakeLists.txt . The compressed data becomes a file like index.html.gz.inc , which is then main.c through #include and embedded in the program. List of endpoints HTTP_RESOURCE_DEFINE() Each endpoint registered is placed in a dedicated section (such as http_resource_desc_test_http_service ) provided by the linker script sections-rom.ld . The HTTP server traverses this section at startup to create a list of endpoints. If the correspondence between sections-rom.ld and zephyr_linker_sources() on CMakeLists.txt side is broken, a symbol like _http_resource_desc_test_http_service_list_start cannot be found, resulting in a link error. ▲ Return to Table of Contents Change history: [Published date]: First edition ========================= We are currently unable to respond to comments left in the "Comment" section of this post. We apologize for the inconvenience, but please refer to "Technical Questions to NXP - How to Contact Us (Japanese Blog)" when making inquiries. (If you are already an NXP distributor or have a business relationship with NXP, you may contact your representative directly.) This article is an appendix to "Zephyr Debut Session ~90 Minutes to Get FRDM-MCXN947 Working Without Stumbling~". It provides commands for testing in a Linux environment, applications of the main content, a glossary, and more. (The main instructions (Chapters 0-6) and troubleshooting (Appendix A) are included in the main text .) Japanese Blog Interface MCX Sensor Ethernet
記事全体を表示
Zephyr 首秀会议 ~90 分钟内让 FRDM-MCXN947 顺利运行~ [附录](日文博客) 本文是《Zephyr 首秀教程 ~90 分钟内让 FRDM-MCXN947 顺利运行~》的附录。它提供了在 Linux 环境下进行测试的命令、主要内容的应用、术语表等。 (主要说明(第0-6章)和故障排除(附录A)包含在正文中。) 目录 附录 B. 命令示例列表(PowerShell/bash/zsh) 第二章:试用开发环境 第三章:测试温度传感器 第四章:试用HTTP服务器 第五章:修改HTTP服务器以在浏览器中查看温度传感器值 附录A:当事情进展不顺利时 附录 C:让我们试一试! 附录C. 让我们来试试(供提前完成的人参考) C.1 改变 LED 的闪烁间隔。 C.2 更改“Hello World”消息 C.3 同时以华氏度显示温度。 C.4 更改浏览器中显示的温度数字位数 附录 D. 尝试使用其他 FRDM 板 D.1 找出电路板名称 D.2 每个样本的注释 附录 E. 词汇表 附录 F. 常见问题解答 附录 G。HTTP 服务器示例框架 主要报道(另一篇文章): 0. 这篇文章是关于什么的? 1. Zephyr是什么? 2. 试用开发环境。 3. 尝试操作温度传感器。 4. 尝试运行 HTTP 服务器。 5. 修改 HTTP 服务器,以便在浏览器中查看温度传感器值。 6. 总结 附录A:当事情进展不顺利时 附录 B. 命令示例列表(PowerShell/bash/zsh) 本文中的命令示例按数字顺序排列。本文假设您在 Windows 系统上使用 PowerShell,但也提供了 bash 和 zsh 版本,供在 macOS 或 Linux 环境下使用的用户参考。本文假设您已按照官方入门指南0.3 节中的说明 ~/zephyrproject 环境配置。 关于使用 bash 或 zsh 尝试此操作的说明 路径分隔符:PowerShell 中的 \ 对应于 bash 和 zsh 中的 / 。 ~ 代表用户主文件夹,这在两者中都是通用的。 命令示例中没有行号:macOS 的标准 zsh 默认不会将交互式输入的 # 视为注释。因此,粘贴包含 # コマンド例2.1-1 行会导致“命令未找到:#”错误。bash 和 zsh 版本的代码块中没有行号;行号由标题指示。 虚拟环境和 ZEPHYR_BASE :与 PowerShell 一样,两者都仅在当前终端中有效。如果重新打开终端,请从示例 2.1-1 重新开始执行命令。 在 macOS 上,串口名称为 /dev/tty.usbmodemXXXX ,在 Linux 上为 /dev/ttyACM0 ,等等。(第 2.2 节) PowerShell 和 bash/zsh 的语法差异 此列表仅列出 PowerShell 和 bash/zsh 之间的语法差异。它不包括两者中相同的元素,例如 cd 、 west build 、 west flash 和 git clone 。 要做的事 PowerShell bash/zsh 出现的命令示例 路径分隔符 ~\handson ~/handson 所有的 激活虚拟环境 .\.venv\Scripts\Activate.ps1 source .venv/bin/activate 2.1-1 ZEPHYR_BASE 设置 $env:ZEPHYR_BASE = "..." source zephyr/zephyr-env.sh 2.1-1 打开配置文件(profile)。 notepad $PROFILE.CurrentUserAllHosts code ~/.bashrc / code ~/.zshrc 2.1-3 创建一个文件夹 New-Item -ItemType Directory -Force mkdir -p 2.2-1 复制整个文件夹。 Copy-Item -Recurse cp -r 2.2-1,2.3-1,3.1-1,4.1-1 ping 做4次后停止。 ping (尝试4次后停止) ping -c 4 4.1-5 显示当前文件夹 Get-Location pwd A.1-1 显示环境变量 $env:ZEPHYR_BASE echo $ZEPHYR_BASE A.1-1 将日志保存到文件中。 2>&1 | Out-File build_log.txt > build_log.txt 2>&1 A.3-2 安装 ccache winget install --id Ccache.Ccache -e brew install ccache (macOS) / sudo apt install ccache (Ubuntu) A.3-3 检查您电脑的 IP 地址 ipconfig ifconfig (macOS) / ip addr (Linux) A.6-1 删除文件夹 Remove-Item -Recurse -Force rm -rf A.7-1 第二章:试用开发环境 命令示例 2.1-1 环境准备和验证(第 2.1 节) # コマンド例2.1-1 cd ~\zephyrproject .\.venv\Scripts\Activate.ps1 $env:ZEPHYR_BASE = "$HOME\zephyrproject\zephyr" west --version west topdir cd ~/zephyrproject source .venv/bin/activate source zephyr/zephyr-env.sh west --version west topdir 在 bash 和 zsh 中, zephyr-env.sh 会设置 ZEPHYR_BASE 。直接写 export ZEPHYR_BASE=~/zephyrproject/zephyr 效果相同。 命令示例 2.1-2:一次性设置环境并移动到工作文件夹(第 2.1 节) # コマンド例2.1-2 zinit thermometer zinit thermometer 要在 bash 或 zsh 中使用 zinit ,请将以下函数添加到命令示例 2.1-3 中打开的文件中,保存它,然后重新打开终端(这是你要写入文件的内容,而不是粘贴到终端中的命令)。 zinit() { cd ~/zephyrproject || return source .venv/bin/activate source zephyr/zephyr-env.sh west --version west topdir if [ -n "$1" ]; then cd ~/handson/"$1"; fi } 命令示例 2.1-3 打开配置文件(shell 配置文件)(第 2.1 节) # コマンド例2.1-3 if (-not (Test-Path $PROFILE.CurrentUserAllHosts)) { New-Item -Force $PROFILE.CurrentUserAllHosts | Out-Null } notepad $PROFILE.CurrentUserAllHosts 对于 bash: code ~/.bashrc 对于 zsh(macOS 上的默认 shell): code ~/.zshrc 命令示例 2.2-1:将 hello_world 复制到工作文件夹(第 2.2 节) # コマンド例2.2-1 New-Item -ItemType Directory -Force ~\handson | Out-Null Copy-Item -Recurse ~\zephyrproject\zephyr\samples\hello_world ~\handson\hello_world cd ~\handson\hello_world mkdir -p ~/handson cp -r ~/zephyrproject/zephyr/samples/hello_world ~/handson/hello_world cd ~/handson/hello_world cp -r 与 Copy-Item 类似,如果目标文件夹已存在,则会将内容以嵌套方式复制到该文件夹内。 命令示例 2.2-2 构建 hello_world(第 2.2 节) # コマンド例2.2-2 west build -p auto -b frdm_mcxn947/mcxn947/cpu0 . west build -p auto -b frdm_mcxn947/mcxn947/cpu0 . 命令示例 2.2-3:编写 hello_world(第 2.2 节) # コマンド例2.2-3 west flash west flash 在 Linux 系统中,如果未配置 USB 设备访问权限(udev 规则),编程工具可能无法找到调试探针。在这种情况下,请按照编程工具(例如 LinkServer)的安装说明配置 udev 规则。 命令示例 2.3-1:将 blinky 复制到工作文件夹(第 2.3 节) # コマンド例2.3-1 Copy-Item -Recurse ~\zephyrproject\zephyr\samples\basic\blinky ~\handson\blinky cd ~\handson\blinky cp -r ~/zephyrproject/zephyr/samples/basic/blinky ~/handson/blinky cd ~/handson/blinky 命令示例 2.3-2 构建闪烁灯(第 2.3 节) # コマンド例2.3-2 west build -p auto -b frdm_mcxn947//cpu0 . west build -p auto -b frdm_mcxn947//cpu0 . 命令示例 2.3-3:向 blinky 写入数据(第 2.3 节) # コマンド例2.3-3 west flash west flash 第三章:测试温度传感器 命令示例 3.1-1 将温度计复制到工作文件夹(第 3.1 节) # コマンド例3.1-1 Copy-Item -Recurse ~\zephyrproject\zephyr\samples\sensor\thermometer ~\handson\thermometer cd ~\handson\thermometer cp -r ~/zephyrproject/zephyr/samples/sensor/thermometer ~/handson/thermometer cd ~/handson/thermometer 命令示例 3.1-2 构建温度计(第 3.1 节) # コマンド例3.1-2 west build -p auto -b frdm_mcxn947//cpu0 . west build -p auto -b frdm_mcxn947//cpu0 . 命令示例 3.1-3:写入温度计(第 3.1 节) # コマンド例3.1-3 west flash west flash 第四章:试用HTTP服务器 命令示例 4.1-1 将 http_server 复制到工作文件夹(第 4.1 节) # コマンド例4.1-1 Copy-Item -Recurse ~\zephyrproject\zephyr\samples\net\sockets\http_server ~\handson\http_server cd ~\handson\http_server cp -r ~/zephyrproject/zephyr/samples/net/sockets/http_server ~/handson/http_server cd ~/handson/http_server 命令示例 4.1-2 在 VS Code 中打开 prj.conf(第 4.1 节) # コマンド例4.1-2 code prj.conf code prj.conf 如果在 macOS 上执行 code 后出现“命令未找到”的提示,请在 VS Code 中打开命令面板( Cmd + Shift + P ),运行“Shell 命令:在 PATH 中安装 'code' 命令”,然后重新打开终端。 命令示例 4.1-3 构建 http_server(第 4.1 节) # コマンド例4.1-3 west build -p auto -b frdm_mcxn947//cpu0 . west build -p auto -b frdm_mcxn947//cpu0 . 命令示例 4.1-4:写入 http_server(第 4.1 节) # コマンド例4.1-4 west flash west flash 命令示例 4.1-5 检查与电路板的通信(第 4.1 节) # コマンド例4.1-5 ping 192.168.1.100 ping -c 4 192.168.1.100 与 Windows 不同,macOS 和 Linux 中的 ping 会持续发送消息,直到您使用 Ctrl+C 停止为止。 -c 4 表示发送 4 次后停止。响应将显示为 64 bytes from 192.168.1.100: ... 。 第五章:修改HTTP服务器以在浏览器中查看温度传感器值 命令示例 5.2-1 获取修改后的存储库(第 5.2 节) # コマンド例5.2-1 cd ~\handson git clone https://github.com/teddokano/handson_http_server_temp cd handson_http_server_temp cd ~/handson git clone https://github.com/teddokano/handson_http_server_temp cd handson_http_server_temp 命令示例 5.2-2 构建修改版本(第 5.2 节) # コマンド例5.2-2 west build -p auto -b frdm_mcxn947//cpu0 . west build -p auto -b frdm_mcxn947//cpu0 . 命令示例 5.2-3 编写修改版本(第 5.2 节) # コマンド例5.2-3 west flash west flash 附录A:当事情进展不顺利时 命令示例 A.1-1:检查当前状态( A.1 ) # コマンド例A.1-1 Get-Location $env:ZEPHYR_BASE west topdir pwd echo $ZEPHYR_BASE west topdir 命令示例 A.3-1 清理构建 ( A.3 ) # コマンド例A.3-1 west build -p always -b frdm_mcxn947//cpu0 . west build -p always -b frdm_mcxn947//cpu0 . 命令示例 A.3-2:将构建日志保存到文件( A.3 ) # コマンド例A.3-2 west build -p auto -b frdm_mcxn947//cpu0 . 2>&1 | Out-File build_log.txt code build_log.txt west build -p auto -b frdm_mcxn947//cpu0 . > build_log.txt 2>&1 code build_log.txt 命令示例 A.3-3 安装 ccache ( A.3 ) # コマンド例A.3-3 winget install --id Ccache.Ccache -e 适用于 macOS(Homebrew): brew install ccache 对于 Linux(Ubuntu): sudo apt install ccache 命令示例 A.6-1检查您电脑的 IP 地址( A.6 ) # コマンド例A.6-1 ipconfig 适用于 macOS: ifconfig 对于 Linux 系统: ip addr 在 macOS 上,IPv4 地址显示在接口的 inet 行,例如 en0 ;而在 Linux 上,它显示在 inet 行,例如 eth0 或 enp... 。 命令示例 A.7-1 删除工作样本并重新开始 ( A.7 ) # コマンド例A.7-1 cd ~\handson Remove-Item -Recurse -Force ~\handson\http_server cd ~/handson rm -rf ~/handson/http_server rm -rf 此操作将直接删除该项目,无需确认,且不会保留在回收站中。如果您输入的文件夹名称有误,可能会删除其他文件夹,因此请确保在执行操作前没有指定 ~/handson 或 ~/zephyrproject 本身。 附录 C:让我们试一试! 命令示例 C.1-1:打开 blinky 的 main.c( C.1 ) # コマンド例C.1-1 cd ~\handson\blinky code src\main.c cd ~/handson/blinky code src/main.c 命令示例 C.2-1:在 hello_world 中打开 main.c( C.2 ) # コマンド例C.2-1 cd ~\handson\hello_world code src\main.c cd ~/handson/hello_world code src/main.c 命令示例 C.3-1:打开温度计的 main.c 文件( C.3 ) # コマンド例C.3-1 cd ~\handson\thermometer code src\main.c cd ~/handson/thermometer code src/main.c 命令示例 C.4-1 打开修改后的 HTTP 服务器的 main.c 文件 ( C.4 ) # コマンド例C.4-1 cd ~\handson\handson_http_server_temp code src\main.c cd ~/handson/handson_http_server_temp code src/main.c 命令示例 C.4-2 将修改后的 main.c 恢复到其原始版本 ( C.4 ) # コマンド例C.4-2 git checkout -- src/main.c git checkout -- src/main.c 命令示例 D.1-1显示 Zephyr 支持的 FRDM-MCX 板列表( D.1 ) # コマンド例D.1-1 west boards -n frdm_mcx west boards -n frdm_mcx ▲ 返回目录 附录C. 让我们来试试(供提前完成的人参考) 这些练习是为那些快速完成主要教程或想回家后稍作尝试的人准备的。你可以通过修改示例代码中的几行来尝试每个练习。修改代码后,请务必在 VS Code 中按 Ctrl + S 保存,然后编译并烧录文件。 如果您忘记了您重写了哪个代码,您可以随时通过再次复制示例并使用A.7中的步骤来恢复到原始代码。 C.1 改变 LED 的闪烁间隔。 让我们尝试加快我们在 2.3 节中移动的 blinky 的闪烁速度。 # コマンド例C.1-1 cd ~\handson\blinky code src\main.c main.c 其中找出下列一行。 #define SLEEP_TIME_MS 1000 1000 这意味着“每 1000 毫秒(即 1 秒)切换一次 LED”。将其更改为 200 或类似值,保存,使用示例 2.3-2 中的命令进行编译,然后使用示例 2.3-3 中的命令进行写入。如果闪烁速度增加,则表示成功。 C.2 更改“Hello World”消息 # コマンド例C.2-1 cd ~\handson\hello_world code src\main.c printf("Hello World! %s\n", CONFIG_BOARD_TARGET); 尝试将 Hello World! 部分替换为您喜欢的字符串。保存后,在 Tera Term 连接的情况下,使用命令示例 2.2-2构建它,然后使用命令示例 2.2-3写入它。 也可以显示日文字符。请参阅 2.2 节中的 Tera。如果在 Tera Term 流程的第 4 步中将接收消息的字符编码设置为 UTF-8 ,则应该可以正确显示。如果字符显示混乱,请检查该设置。 C.3 同时以华氏度显示温度。 在第 3 章中, thermometer ,让我们尝试除了摄氏度 (°C) 之外,还显示华氏度 (°F)。 # コマンド例C.3-1 cd ~\handson\thermometer code src\main.c main() 在文件末尾附近查找以下行。 printf("Temperature is %0.1lf°C\n", sensor_value_to_double(&value)); 提示:华氏度可以用以下公式计算:摄氏度 × 9 ÷ 5 + 32。 示例答案:将上面的一行替换为以下两行。 double c = sensor_value_to_double(&value); printf("Temperature is %0.1lf°C (%0.1lf°F)\n", c, c * 9.0 / 5.0 + 32.0); 保存后,使用命令示例 3.1-2进行构建,并使用命令示例 3.1-3进行写入。如果显示为 Temperature is 24.3°C (75.7°F) ,则表示成功。 C.4 更改浏览器中显示的温度数字位数 在第五章修改后的HTTP服务器中,温度显示为小数点后三位。我们将其改为小数点后一位。 # コマンド例C.4-1 cd ~\handson\handson_http_server_temp code src\main.c 提示: main.c 中的以下两个位置与屏幕上显示的内容有关。尝试在 VS Code 中搜索它们( Ctrl + F )。 temperature_handler 页面首次显示时的格式: "Temperature: %.3f°C" 在每秒更新一次显示的 JavaScript 代码中: t.toFixed(3) %.3f 如果你搜索一下,你会发现 temperature_json_handler 中还有另一个实例。这是用来向浏览器传递数值的,所以这次你不需要修改它。 示例答案:将上述两处分别替换为 "Temperature: %.1f°C" 和 t.toFixed(1) 。保存后,使用命令示例 5.2-2构建,使用命令示例 5.2-3写入,然后在浏览器中重新打开 http://192.168.1.100/temperature 。 如果只重写其中一处代码,结果就是页面打开后,数字会立即显示为三位数,一秒后变成一位数(反之亦然)。这表明,返回 HTML 的 C 语言部分和在浏览器中运行的 JavaScript 部分都参与了显示过程。 由于此文件夹是使用 Git 获取的,因此您可以使用以下命令将其恢复到之前的状态。 # コマンド例C.4-2 git checkout -- src/main.c ▲ 返回目录 附录 D. 尝试使用其他 FRDM 板 本文以 FRDM-MCXN947 为例,但 Zephyr 也兼容其他 NXP FRDM 开发板。如果您使用的是其他开发板,以下是一些建议。 D.1 找出电路板名称 west build 可以使用以下命令列出 -b 中要指定的板卡名称。 # コマンド例D.1-1 west boards -n frdm_mcx 电路板规格的主要指定方法如下: 木板 -b 要指定的名称 FRDM-MCXN947 frdm_mcxn947//cpu0 (本文) FRDM-MCXN236 frdm_mcxn236 FRDM-MCXA153 frdm_mcxa153 FRDM-MCXA156 frdm_mcxa156 FRDM-MCXC444 frdm_mcxc444 除了像 FRDM-MCXN947 这样的双核板之外,您可以仅通过名称指定板(详情请参见第 2.2 节)。 【请使用场馆电脑上的 Zephyr 版本确认此表中的棋盘名称。】 D.2 每个样本的注释 hello_world :这个程序几乎可以在所有开发板上直接运行。串口名称和波特率可能因开发板而异,请查阅您所用开发板的相关文档。 闪烁:如果板的设备树中定义了别名 led0 ,它将按原样工作(第 2.4 节)。 温度计:电路板上安装了一个温度传感器,需要一个覆盖层指向该传感器,标记为 ambient-temp0 。检查 samples/sensor/thermometer/boards/ 中是否存在与您要使用的电路板对应的 .overlay 。如果没有,您需要参考 3.3 节中所示的覆盖层,自行编写覆盖层。 http_server :此功能仅适用于带有以太网接口的开发板。 此外, west flash 使用的编程工具可能因开发板而异。请在 Zephyr 官方文档中查看各开发板对应的支持编程工具列表(在“支持的开发板”下搜索开发板名称)。 ▲ 返回目录 附录 E. 词汇表 本文将按字母顺序和日语音节顺序对所使用的术语进行简要解释。 学期 说明 ELF 文件 在构建过程中创建的程序文件,会写入微控制器。它还包含调试信息( build/zephyr/zephyr.elf )。 网关 这是路由器的地址,路由器是与其他网络通信时的出口设备。在这种PC和主板直接连接的配置中,不使用路由器的地址。 HTTP/HTTPS 这是一套浏览器与网络服务器之间通信的规则。HTTPS 对通信进行加密,而本论坛使用的是未加密的 HTTP。 IP地址 用于区分网络上设备的编号。在本例中,主板的编号为 192.168.1.100 ,PC的编号为 192.168.1.x 。 Kconfig 这是开启和关闭 Zephyr 函数的机制。你可以这样写: prj.conf 变成 CONFIG_xxx=y 。 链接服务器 NXP 的编程和调试工具。 west flash 用于对 FRDM-MCXN947 进行编程。 MCU-Link 这是FRDM板上的编程和调试电路。通过USB连接到PC后,可用于编程和串行通信(虚拟COM端口)。 ping 向另一设备发送少量数据并检查是否收到响应以确定网络连接是否正常的命令。 全新构建(干净构建) 删除构建文件夹并从头开始重新构建。用 -p always prj.conf 每个应用程序的 Kconfig 配置文件 西方 Zephyr 命令行工具。它们在一个地方处理 Zephyr 操作,例如构建 ( west build ) 和写入 ( west flash )。 Zephyr SDK 一套完整的工具,包括编译器,用于构建 Zephyr。 别名 别名用于在设备树中指定节点,例如 led0 或 ambient-temp0 。应用程序使用别名来指定设备,因此即使电路板发生变化,也不需要更改代码。 端点 在HTTP服务器中,每个URL路径都会被赋予类似 /uptime 和 /temperature 这样的“窗口”。每个窗口都有一个对应的处理函数。 覆盖 文件( *.overlay )将每个应用程序的差异叠加到板本身的设备树上。 写 将编译好的程序传输到微控制器。这是通过 west flash 虚拟环境(Python虚拟环境) 这是一个独立的 Python 环境,其中包含 Zephyr 所需的所有 Python 工具。启用后,提示符中将显示 (.venv) 。 当前目录 当前文件夹。在 PowerShell 中,这显示在提示符的 PS C:\Users\...> 部分。 子网 一组可以直接通信的IP地址。 192.168.1.0/24 表示以 192.168.1. 开头的地址集合。 串口控制台 这是一个用于在电脑上显示开发板 printf 输出和其他信息的屏幕。本文使用Tera Term。 设备树 一种用于描述硬件配置的机制,具体来说,就是“哪些组件连接到哪里”。这是针对每个电路板单独定义的。 处理函数 当特定请求或事件发生时调用的函数。 构建 创建一个可以从源代码写入微控制器的程序。 迅速的 在 PowerShell 中, PS C:\Users\...> 符号表示系统正在等待命令输入。 波特率 串口通信速率。本文采用的是 115200 波特率。 工作区 这是 west/Zephyr 主程序和各种模块所在的文件夹。 ~\zephyrproject ▲ 返回目录 附录 F. 常见问题解答 问:Zephyr 可以用于商业产品吗? A. Zephyr 核心代码采用 Apache License 2.0 许可发布,可用于商业产品。但是,Zephyr 使用的某些模块(外部库)可能采用不同的许可协议,因此在将它们集成到您的产品中时,请务必同时检查您所使用模块的许可协议。 问:我应该如何在MCUXpresso SDK和Zephyr之间进行选择? A. 两者都可用于恩智浦(NXP)微控制器。MCUXpresso SDK 是恩智浦为微控制器提供的驱动程序和示例包,适用于裸机和 FreeRTOS 开发。Zephyr 的优势在于它能够使用相同的语法和流程在不同开发板和不同厂商的产品上进行开发,并且拥有丰富的中间件,包括网络功能。您应该根据现有资源以及未来是否计划扩展到其他微控制器来选择。 问:我今天使用了命令行,但我可以用 MCUXpresso 对 VSC 做同样的事情吗? 答:是的。您也可以使用 MCUXpresso for VSC 构建、编写和调试 Zephyr 应用程序。本文使用命令行是为了让您体验适用于所有开发板和厂商的通用流程(参见 2.1 节)。 问:是否可以使用断点进行调试? 答:是的,可以。您可以使用 west debug 从命令行启动调试器。如果您更喜欢使用图形用户界面 (GUI),MCUXpresso for VSC 是最简单的选择。 问:您今天使用的是哪个版本的Zephyr? A. [请输入场地 PC 的 Zephyr 版本]。您也可以在板卡启动时通过串口控制台显示的 *** Booting Zephyr OS build v4.x.x *** 行来查看。如果版本不同,示例的内容和输出可能与本文略有差异。 问:能否在 macOS 或 Linux 系统上完成此操作,而不是在 Windows 系统上? A. 可以。请参阅官方入门指南进行环境设置。附录 B中包含了 bash 和 zsh 命令的编写说明。 问:输出 printf 如何到达 PC? A. 微控制器的 UART(串行通信)输出的字符通过板载的 MCU-Link 发送到 PC,然后再发送到 USB 虚拟 COM 端口。因此,也可以使用同一根 USB 电缆进行编程,从而控制串行控制台。 ▲ 返回目录 附录 G。HTTP 服务器示例框架 第 5 章的存储库 src/main.c 和 CMakeLists.txt 包含大量与添加温度传感器没有直接关系的信息。这是因为 HTTP 服务器示例还提供了它作为 Web 服务器提供的所有数据(首页的 HTML、JavaScript 以及一些预先存在的端点,例如 LED 控制 /uptime 和网络统计信息)。本节解释“作为 HTTP 服务器运行的基础”。如果您只是按照第 5 章中的步骤操作,则无需阅读此部分。 嵌入 HTML 和 JavaScript index.html main.js 在构建过程中由 generate_inc_file_for_target() 在 CMakeLists.txt 内使用 gzip 进行压缩。压缩后的数据成为类似 index.html.gz.inc 文件,然后通过 main.c 到 #include 嵌入到程序中。 端点列表 HTTP_RESOURCE_DEFINE() 每个注册的端点都放置在链接器脚本 sections-rom.ld 提供的专用部分(例如 http_resource_desc_test_http_service )中。HTTP 服务器在启动时遍历此部分以创建端点列表。 如果 CMakeLists.txt 侧的 sections-rom.ld 和 zephyr_linker_sources() 之间的对应关系被打破,则找不到像 _http_resource_desc_test_http_service_list_start 这样的符号,从而导致链接错误。 ▲ 返回目录 变更历史:[发布日期]:第一版 ========================= 我们目前无法回复此帖“评论”区的评论。由此给您带来的不便,我们深表歉意。如有任何疑问,请参阅“NXP技术问题咨询 - 如何联系我们(日语博客)”。(如果您已经是NXP的分销商或与NXP有业务往来,您可以直接联系您的代表。) 本文是《Zephyr 首秀教程 ~90 分钟内让 FRDM-MCXN947 顺利运行~》的附录。它提供了在 Linux 环境下进行测试的命令、主要内容的应用、术语表等。 (主要说明(第0-6章)和故障排除(附录A)包含在正文中。) 日本博客 界面 MCX 传感器 以太网
記事全体を表示
Zephyrデビュー会 〜FRDM-MCXN947でつまずかず動かす90分〜 [付録編] (日本語ブログ) この記事は,「Zephyrデビュー会 〜FRDM-MCXN947でつまずかず動かす90分〜」の付録編です.Linuxの環境で試すためのコマンドや、本編の応用、用語集などを公開しています。 (手順の本文(0〜6章)と,うまくいかないときの対処(付録A)は,本編にあります.) 目次 付録B. コマンド例一覧(PowerShell/bash・zsh) 2章 開発環境を触ってみる 3章 温度センサーを動かしてみる 4章 HTTP Serverを動かしてみる 5章 HTTP Serverを改変して,温度センサーの値をブラウザで見る 付録A うまくいかないときは 付録C やってみよう 付録C. やってみよう(早く終わった方へ) C.1 Lチカの点滅間隔を変える C.2 Hello Worldのメッセージを変える C.3 温度を華氏でも表示する C.4 ブラウザに表示する温度の桁数を変える 付録D. 他のFRDMボードで試すには D.1 ボード名を調べる D.2 サンプルごとの注意 付録E. 用語集 付録F. よくある質問 付録G. HTTP Serverサンプルの足回り 本編(別の記事): 0. この記事は? 1. Zephyrとは? 2. 開発環境を触ってみる 3. 温度センサーを動かしてみる 4. HTTP Serverを動かしてみる 5. HTTP Serverを改変して,温度センサーの値をブラウザで見る 6. まとめ 付録A. うまくいかないときは 付録B. コマンド例一覧(PowerShell/bash・zsh) 本文に登場するコマンド例を,番号順にまとめました.本文はWindowsのPowerShellを前提にしていますが,ご自身のmacOSやLinuxの環境で試す方のために,bash・zsh用の書き方もあわせて載せています.環境構築は,0.3節で紹介した公式のGetting Started Guideの手順どおりに ~/zephyrproject へ済ませてある前提です. bash・zshで試す場合の注意 パスの区切り: PowerShellの \ は,bash・zshでは / になります. ~ がホームフォルダを表すのは共通です. コマンド例の番号の行は付けていません: macOS標準のzshは,初期設定では対話中に入力した # をコメントとして扱いません.そのため # コマンド例2.1-1 の行を貼り付けると「command not found: #」というエラーになります.bash・zsh版のコードブロックには番号の行を入れず,見出しで番号を示しています. 仮想環境と ZEPHYR_BASE : どちらもそのターミナルの中だけで有効なのは,PowerShellと同じです.ターミナルを開き直したら,コマンド例2.1-1から実行し直します. シリアルポートの名前: macOSでは /dev/tty.usbmodemXXXX ,Linuxでは /dev/ttyACM0 などになります(2.2節). PowerShellとbash・zshの書き方の違い PowerShellとbash・zshで書き方が違うところだけを,対にしてまとめました. cd や west build ・ west flash ・ git clone のように,両方で同じ書き方のものは載せていません. やること PowerShell bash・zsh 出てくるコマンド例 パスの区切り ~\handson ~/handson 全体 仮想環境の有効化 .\.venv\Scripts\Activate.ps1 source .venv/bin/activate 2.1-1 ZEPHYR_BASE の設定 $env:ZEPHYR_BASE = "..." source zephyr/zephyr-env.sh 2.1-1 設定ファイル(プロファイル)を開く notepad $PROFILE.CurrentUserAllHosts code ~/.bashrc / code ~/.zshrc 2.1-3 フォルダを作る New-Item -ItemType Directory -Force mkdir -p 2.2-1 フォルダごとコピーする Copy-Item -Recurse cp -r 2.2-1,2.3-1,3.1-1,4.1-1 ping を4回で止める ping (初めから4回で止まる) ping -c 4 4.1-5 今いるフォルダを表示する Get-Location pwd A.1-1 環境変数を表示する $env:ZEPHYR_BASE echo $ZEPHYR_BASE A.1-1 ログをファイルに保存する 2>&1 | Out-File build_log.txt > build_log.txt 2>&1 A.3-2 ccacheをインストールする winget install --id Ccache.Ccache -e brew install ccache (macOS)/ sudo apt install ccache (Ubuntu) A.3-3 PCのIPアドレスを確認する ipconfig ifconfig (macOS)/ ip addr (Linux) A.6-1 フォルダを消す Remove-Item -Recurse -Force rm -rf A.7-1 2章 開発環境を触ってみる コマンド例2.1-1 環境の準備と確認(2.1節) # コマンド例2.1-1 cd ~\zephyrproject .\.venv\Scripts\Activate.ps1 $env:ZEPHYR_BASE = "$HOME\zephyrproject\zephyr" west --version west topdir cd ~/zephyrproject source .venv/bin/activate source zephyr/zephyr-env.sh west --version west topdir bash・zshでは, zephyr-env.sh が ZEPHYR_BASE を設定してくれます. export ZEPHYR_BASE=~/zephyrproject/zephyr と直接書いても同じです. コマンド例2.1-2 環境の準備と作業用フォルダへの移動をまとめて行う(2.1節) # コマンド例2.1-2 zinit thermometer zinit thermometer bash・zshで zinit を使うには,コマンド例2.1-3で開いたファイルに,次の関数を書き加えて保存し,ターミナルを開き直します(ファイルに書く内容です.ターミナルに貼り付けるコマンドではありません). zinit() { cd ~/zephyrproject || return source .venv/bin/activate source zephyr/zephyr-env.sh west --version west topdir if [ -n "$1" ]; then cd ~/handson/"$1"; fi } コマンド例2.1-3 プロファイル(シェルの設定ファイル)を開く(2.1節) # コマンド例2.1-3 if (-not (Test-Path $PROFILE.CurrentUserAllHosts)) { New-Item -Force $PROFILE.CurrentUserAllHosts | Out-Null } notepad $PROFILE.CurrentUserAllHosts bashの場合: code ~/.bashrc zsh(macOSの標準)の場合: code ~/.zshrc コマンド例2.2-1 hello_worldを作業用フォルダにコピー(2.2節) # コマンド例2.2-1 New-Item -ItemType Directory -Force ~\handson | Out-Null Copy-Item -Recurse ~\zephyrproject\zephyr\samples\hello_world ~\handson\hello_world cd ~\handson\hello_world mkdir -p ~/handson cp -r ~/zephyrproject/zephyr/samples/hello_world ~/handson/hello_world cd ~/handson/hello_world cp -r も Copy-Item と同じく,コピー先のフォルダがすでにあると,その中に入れ子でコピーします. コマンド例2.2-2 hello_worldのビルド(2.2節) # コマンド例2.2-2 west build -p auto -b frdm_mcxn947/mcxn947/cpu0 . west build -p auto -b frdm_mcxn947/mcxn947/cpu0 . コマンド例2.2-3 hello_worldの書き込み(2.2節) # コマンド例2.2-3 west flash west flash Linuxでは,USBデバイスへのアクセス権の設定(udevルール)がないと,書き込みツールがデバッグプローブを見つけられないことがあります.その場合は,書き込みツール(LinkServerなど)のインストール手順にあるudevルールの設定を行ってください. コマンド例2.3-1 blinkyを作業用フォルダにコピー(2.3節) # コマンド例2.3-1 Copy-Item -Recurse ~\zephyrproject\zephyr\samples\basic\blinky ~\handson\blinky cd ~\handson\blinky cp -r ~/zephyrproject/zephyr/samples/basic/blinky ~/handson/blinky cd ~/handson/blinky コマンド例2.3-2 blinkyのビルド(2.3節) # コマンド例2.3-2 west build -p auto -b frdm_mcxn947//cpu0 . west build -p auto -b frdm_mcxn947//cpu0 . コマンド例2.3-3 blinkyの書き込み(2.3節) # コマンド例2.3-3 west flash west flash 3章 温度センサーを動かしてみる コマンド例3.1-1 thermometerを作業用フォルダにコピー(3.1節) # コマンド例3.1-1 Copy-Item -Recurse ~\zephyrproject\zephyr\samples\sensor\thermometer ~\handson\thermometer cd ~\handson\thermometer cp -r ~/zephyrproject/zephyr/samples/sensor/thermometer ~/handson/thermometer cd ~/handson/thermometer コマンド例3.1-2 thermometerのビルド(3.1節) # コマンド例3.1-2 west build -p auto -b frdm_mcxn947//cpu0 . west build -p auto -b frdm_mcxn947//cpu0 . コマンド例3.1-3 thermometerの書き込み(3.1節) # コマンド例3.1-3 west flash west flash 4章 HTTP Serverを動かしてみる コマンド例4.1-1 http_serverを作業用フォルダにコピー(4.1節) # コマンド例4.1-1 Copy-Item -Recurse ~\zephyrproject\zephyr\samples\net\sockets\http_server ~\handson\http_server cd ~\handson\http_server cp -r ~/zephyrproject/zephyr/samples/net/sockets/http_server ~/handson/http_server cd ~/handson/http_server コマンド例4.1-2 prj.confをVSCodeで開く(4.1節) # コマンド例4.1-2 code prj.conf code prj.conf macOSで code が「command not found」になる場合は,VSCodeでコマンドパレット( Cmd + Shift + P )を開き,「Shell Command: Install 'code' command in PATH」を実行してから,ターミナルを開き直してください. コマンド例4.1-3 http_serverのビルド(4.1節) # コマンド例4.1-3 west build -p auto -b frdm_mcxn947//cpu0 . west build -p auto -b frdm_mcxn947//cpu0 . コマンド例4.1-4 http_serverの書き込み(4.1節) # コマンド例4.1-4 west flash west flash コマンド例4.1-5 ボードとの疎通確認(4.1節) # コマンド例4.1-5 ping 192.168.1.100 ping -c 4 192.168.1.100 macOS・Linuxの ping は,WindowsとちがってCtrl+Cで止めるまで送り続けます. -c 4 は「4回送ったら終わる」という指定です.応答は 64 bytes from 192.168.1.100: ... のように表示されます. 5章 HTTP Serverを改変して,温度センサーの値をブラウザで見る コマンド例5.2-1 改変版のリポジトリを取得(5.2節) # コマンド例5.2-1 cd ~\handson git clone https://github.com/teddokano/handson_http_server_temp cd handson_http_server_temp cd ~/handson git clone https://github.com/teddokano/handson_http_server_temp cd handson_http_server_temp コマンド例5.2-2 改変版のビルド(5.2節) # コマンド例5.2-2 west build -p auto -b frdm_mcxn947//cpu0 . west build -p auto -b frdm_mcxn947//cpu0 . コマンド例5.2-3 改変版の書き込み(5.2節) # コマンド例5.2-3 west flash west flash 付録A うまくいかないときは コマンド例A.1-1 今の状態をまとめて確認(A.1) # コマンド例A.1-1 Get-Location $env:ZEPHYR_BASE west topdir pwd echo $ZEPHYR_BASE west topdir コマンド例A.3-1 クリーンビルド(A.3) # コマンド例A.3-1 west build -p always -b frdm_mcxn947//cpu0 . west build -p always -b frdm_mcxn947//cpu0 . コマンド例A.3-2 ビルドのログをファイルに保存(A.3) # コマンド例A.3-2 west build -p auto -b frdm_mcxn947//cpu0 . 2>&1 | Out-File build_log.txt code build_log.txt west build -p auto -b frdm_mcxn947//cpu0 . > build_log.txt 2>&1 code build_log.txt コマンド例A.3-3 ccacheのインストール(A.3) # コマンド例A.3-3 winget install --id Ccache.Ccache -e macOS(Homebrew)の場合: brew install ccache Linux(Ubuntu)の場合: sudo apt install ccache コマンド例A.6-1 PC側のIPアドレスを確認(A.6) # コマンド例A.6-1 ipconfig macOSの場合: ifconfig Linuxの場合: ip addr macOSでは en0 などのインターフェースの inet の行に,Linuxでは eth0 や enp... などの inet の行に,IPv4アドレスが表示されます. コマンド例A.7-1 作業用のサンプルを削除してやり直す(A.7) # コマンド例A.7-1 cd ~\handson Remove-Item -Recurse -Force ~\handson\http_server cd ~/handson rm -rf ~/handson/http_server rm -rf は確認なしで削除し,ごみ箱にも残りません.フォルダ名を打ち間違えると別のフォルダが消えてしまうので, ~/handson や ~/zephyrproject そのものを指定していないことを確認してから実行してください. 付録C やってみよう コマンド例C.1-1 blinkyのmain.cを開く(C.1) # コマンド例C.1-1 cd ~\handson\blinky code src\main.c cd ~/handson/blinky code src/main.c コマンド例C.2-1 hello_worldのmain.cを開く(C.2) # コマンド例C.2-1 cd ~\handson\hello_world code src\main.c cd ~/handson/hello_world code src/main.c コマンド例C.3-1 thermometerのmain.cを開く(C.3) # コマンド例C.3-1 cd ~\handson\thermometer code src\main.c cd ~/handson/thermometer code src/main.c コマンド例C.4-1 改変版HTTP Serverのmain.cを開く(C.4) # コマンド例C.4-1 cd ~\handson\handson_http_server_temp code src\main.c cd ~/handson/handson_http_server_temp code src/main.c コマンド例C.4-2 改変版のmain.cを元に戻す(C.4) # コマンド例C.4-2 git checkout -- src/main.c git checkout -- src/main.c コマンド例D.1-1 Zephyrが対応しているFRDM-MCXボードの一覧を表示(D.1) # コマンド例D.1-1 west boards -n frdm_mcx west boards -n frdm_mcx ▲ 目次に戻る 付録C. やってみよう(早く終わった方へ) 本文の手順が早く終わった方や,帰ってから少し遊んでみたい方向けの課題です.どれもサンプルコードを数行書き換えるだけで試せます.書き換えたら,必ずVSCodeで Ctrl + S を押して保存してから,ビルドと書き込みをしてください. 書き換えたコードが分からなくなったときは,A.7の手順でサンプルをコピーし直せば,いつでも元に戻せます. C.1 Lチカの点滅間隔を変える 2.3節で動かした blinky の点滅を速くしてみましょう. # コマンド例C.1-1 cd ~\handson\blinky code src\main.c main.c の中の次の行を探します. #define SLEEP_TIME_MS 1000 1000 は「1000ミリ秒=1秒ごとにLEDを切り替える」という意味です.これを 200 などに書き換えて保存し,コマンド例2.3-2でビルド,コマンド例2.3-3で書き込みます.点滅が速くなれば成功です. C.2 Hello Worldのメッセージを変える # コマンド例C.2-1 cd ~\handson\hello_world code src\main.c printf("Hello World! %s\n", CONFIG_BOARD_TARGET); の Hello World! の部分を,好きな文字列に書き換えてみましょう.保存したら,Tera Termを接続した状態でコマンド例2.2-2でビルド,コマンド例2.2-3で書き込みます. 日本語の文字列も表示できます.2.2節のTera Termの手順4でTera Termの受信の文字コードを UTF-8 にしていれば,正しく表示されます.文字化けする場合は,その設定を確認してください. C.3 温度を華氏でも表示する 3章の thermometer で,摂氏(°C)に加えて華氏(°F)も表示してみましょう. # コマンド例C.3-1 cd ~\handson\thermometer code src\main.c main() の最後のほうにある,次の行を探します. printf("Temperature is %0.1lf°C\n", sensor_value_to_double(&value)); ヒント: 華氏は「摂氏 × 9 ÷ 5 + 32」で計算できます. 答えの例: 上の1行を,次の2行に置き換えます. double c = sensor_value_to_double(&value); printf("Temperature is %0.1lf°C (%0.1lf°F)\n", c, c * 9.0 / 5.0 + 32.0); 保存したら,コマンド例3.1-2でビルド,コマンド例3.1-3で書き込みます. Temperature is 24.3°C (75.7°F) のように表示されれば成功です. C.4 ブラウザに表示する温度の桁数を変える 5章の改変版HTTP Serverでは,温度を小数点以下3桁で表示しています.これを1桁にしてみましょう. # コマンド例C.4-1 cd ~\handson\handson_http_server_temp code src\main.c ヒント: 画面の表示に関わっているのは, main.c の中の次の2か所です.VSCodeの検索( Ctrl + F )で探してみてください. temperature_handler の中の,ページを最初に表示するときの書式: "Temperature: %.3f°C" 1秒ごとに表示を書き換えるJavaScriptの中: t.toFixed(3) %.3f で検索すると, temperature_json_handler の中にももう1か所見つかります.こちらはブラウザに数値を渡すためのものなので,今回は変えなくて構いません. 答えの例: 上の2か所を,それぞれ "Temperature: %.1f°C" と t.toFixed(1) に書き換えます.保存したら,コマンド例5.2-2でビルド,コマンド例5.2-3で書き込み,ブラウザで http://192.168.1.100/temperature を開き直します. 片方だけ書き換えると,ページを開いた直後だけ3桁で,1秒後から1桁になる(またはその逆)という動きになります.HTMLを返すC言語側と,ブラウザで動くJavaScript側の両方が表示に関わっていることが分かります. このフォルダはGitで取得したものなので,次のコマンドで書き換える前の状態に戻せます. # コマンド例C.4-2 git checkout -- src/main.c ▲ 目次に戻る 付録D. 他のFRDMボードで試すには 本記事はFRDM-MCXN947を前提にしていますが,ZephyrはNXPの他のFRDMボードにも対応しています.手元に別のボードがある場合のヒントをまとめます. D.1 ボード名を調べる west build の -b に指定するボード名は,次のコマンドで一覧できます. # コマンド例D.1-1 west boards -n frdm_mcx 主なボードの指定方法は次のとおりです. ボード -b に指定する名前 FRDM-MCXN947 frdm_mcxn947//cpu0 (本記事) FRDM-MCXN236 frdm_mcxn236 FRDM-MCXA153 frdm_mcxa153 FRDM-MCXA156 frdm_mcxa156 FRDM-MCXC444 frdm_mcxc444 FRDM-MCXN947のようなデュアルコアのボード以外は,ボード名だけで指定できます(2.2節の解説). 【この表のボード名を,会場PCのZephyrバージョンで確認】 D.2 サンプルごとの注意 hello_world: ほぼすべてのボードでそのまま動きます.シリアルポートの名前やボーレートはボードによって異なることがあるので,各ボードのドキュメントを確認してください. blinky: ボードのデバイスツリーに led0 というエイリアスが定義されていれば,そのまま動きます(2.4節). thermometer: 温度センサーがボードに載っていて,そのセンサーを ambient-temp0 として指すオーバーレイが必要です. samples/sensor/thermometer/boards/ に,使いたいボード向けの .overlay があるか確認してください.無い場合は,3.3節で見たオーバーレイを参考に自分で書くことになります. http_server: Ethernetを持つボードでなければ動きません. また, west flash が使う書き込みツールは,ボードによって異なることがあります.Zephyr公式ドキュメントの各ボードのページ(「Supported Boards」からボード名で検索)で,対応している書き込みツールを確認してください. ▲ 目次に戻る 付録E. 用語集 本記事に出てくる用語を,アルファベット順,五十音順に簡単に説明します. 用語 説明 ELFファイル ビルドでできる,マイコンに書き込むプログラムのファイル.デバッグ情報も含みます( build/zephyr/zephyr.elf ) Gateway(ゲートウェイ) 別のネットワークと通信するときの出口になる機器(ルーター)のアドレス.PCとボードを直結する今回の構成では使われません HTTP/HTTPS ブラウザとWebサーバーがやりとりするための決まり.HTTPSは通信を暗号化したもので,今回のボードは暗号化しないHTTPで動いています IPアドレス ネットワーク上の機器を区別するための番号.今回はボードが 192.168.1.100 ,PCが 192.168.1.x です Kconfig Zephyrの機能のオン・オフを設定する仕組み. prj.conf に CONFIG_xxx=y のように書きます LinkServer NXPの書き込み・デバッグツール. west flash がFRDM-MCXN947への書き込みに使います MCU-Link FRDMボードに載っている,書き込み・デバッグ用の回路.PCとUSBでつなぐと,書き込みとシリアル通信(仮想COMポート)に使えます ping 相手の機器に小さなデータを送り,応答が返ってくるかでネットワークがつながっているかを確かめるコマンド pristineビルド(クリーンビルド) ビルドフォルダを消して,最初からビルドし直すこと. -p always で指定します prj.conf アプリケーションごとのKconfigの設定ファイル west Zephyrのコマンドラインツール.ビルド( west build )や書き込み( west flash )など,Zephyrの操作をまとめて扱います Zephyr SDK Zephyrのビルドに使うコンパイラなどのツール一式 エイリアス デバイスツリーで,ノードにつける別名. led0 や ambient-temp0 など.アプリケーションはエイリアスでデバイスを指定するので,ボードが変わってもコードを変えずに済みます エンドポイント HTTP Serverで, /uptime や /temperature のように,URLのパスごとに用意された「窓口」.それぞれにハンドラ関数が対応します オーバーレイ ボード本体のデバイスツリーに,アプリケーションごとの差分を重ねるファイル( *.overlay ) 書き込み ビルドしたプログラムをマイコンに転送すること. west flash で行います 仮想環境(Python仮想環境) Zephyr用のPythonツールをまとめて入れてある,独立したPython環境.有効化するとプロンプトに (.venv) が付きます カレントディレクトリ 今いるフォルダ.PowerShellでは,プロンプトの PS C:\Users\...> の部分に表示されます サブネット 直接通信できるIPアドレスのまとまり. 192.168.1.0/24 は, 192.168.1. で始まるアドレスのまとまりを表します シリアルコンソール ボードの printf の出力などを,PCで表示するための画面.本記事ではTera Termを使います デバイスツリー 「どの部品が,どこにつながっているか」というハードウェア構成を記述する仕組み.ボードごとに定義されています ハンドラ関数 あるリクエストやイベントが来たときに呼び出される関数 ビルド ソースコードから,マイコンに書き込めるプログラムを作ること プロンプト PowerShellで,コマンドの入力を待っている状態を示す表示( PS C:\Users\...> ) ボーレート シリアル通信の速度.本記事では115200 ワークスペース west・Zephyr本体・各種モジュールをまとめて置いてあるフォルダ.本記事では ~\zephyrproject ▲ 目次に戻る 付録F. よくある質問 Q. Zephyrは商用製品に使えますか? A. Zephyr本体はApache License 2.0で公開されており,商用製品にも使えます.ただし,Zephyrと一緒に使うモジュール(外部ライブラリ)には別のライセンスのものもあるので,製品に組み込む際は,使っているモジュールのライセンスもあわせて確認してください. Q. MCUXpresso SDKとZephyrは,どう使い分ければいいですか? A. NXPのマイコンでは,どちらも使えます.MCUXpresso SDKはNXPのマイコン向けに提供されるドライバやサンプルのパッケージで,ベアメタルやFreeRTOSでの開発に向いています.Zephyrは,ボードやベンダーをまたいで同じ書き方・同じ手順で開発できることや,ネットワークなどのミドルウェアが豊富なことが強みです.既存の資産や,将来ほかのマイコンにも展開するかどうかで選ぶとよいでしょう. Q. 今日はコマンドラインで操作しましたが,MCUXpresso for VSCでもできますか? A. はい.MCUXpresso for VSCからも,Zephyrアプリケーションのビルドや書き込み,デバッグができます.本記事でコマンドラインを使ったのは,どのボード・どのベンダーでも共通の手順を体験してもらうためです(2.1節). Q. ブレークポイントを使ったデバッグはできますか? A. できます.コマンドラインでは west debug でデバッガを起動できます.GUIで操作したい場合は,MCUXpresso for VSCを使うのが手軽です. Q. 今日使ったZephyrのバージョンは? A. 【会場PCのZephyrバージョンを記入】です.ボードの起動時にシリアルコンソールに表示される *** Booting Zephyr OS build v4.x.x *** の行でも確認できます.バージョンが違うと,サンプルの中身や出力が本記事と少し異なることがあります. Q. WindowsではなくmacOSやLinuxでもできますか? A. できます.環境構築は公式のGetting Started Guideを参照してください.コマンドの書き方は,付録Bにbash・zsh版をまとめています. Q. printf の出力は,どうやってPCに届いているのですか? A. マイコンのUART(シリアル通信)から出力された文字が,ボード上のMCU-Linkを経由して,USBの仮想COMポートとしてPCに届いています.そのため,書き込み用と同じUSBケーブル1本で,シリアルコンソールも使えます. ▲ 目次に戻る 付録G. HTTP Serverサンプルの足回り 5章のリポジトリの src/main.c や CMakeLists.txt には,温度センサーの追加とは直接関係のない部分がかなりあります.HTTP Serverサンプルが,Webサーバーとして配信するデータ一式(トップページのHTML,JavaScript,LED制御・ /uptime ・ネットワーク統計といった元からある複数のエンドポイント)も,あわせて用意しているためです.ここでは,その「HTTPサーバーとして動くための足回り」を説明します.5章の手順を進めるだけなら,読まなくて構いません. HTMLとJavaScriptの埋め込み index.html や main.js は, CMakeLists.txt の中の generate_inc_file_for_target() によって,ビルド時にgzipで圧縮されます.圧縮したデータは index.html.gz.inc のようなファイルになり, main.c から #include されて,プログラムの中に埋め込まれます. エンドポイントの一覧 HTTP_RESOURCE_DEFINE() で登録した各エンドポイントは, sections-rom.ld というリンカスクリプトが用意する専用のセクション( http_resource_desc_test_http_service など)に置かれます.HTTPサーバーは,起動時にこのセクションをたどって,エンドポイントの一覧を作ります. この sections-rom.ld と, CMakeLists.txt 側の zephyr_linker_sources() の対応が崩れると, _http_resource_desc_test_http_service_list_start のようなシンボルが見つからず,リンクエラーになります. ▲ 目次に戻る 変更履歴:【公開日】:初版 ========================= 本投稿の「Comment」欄にコメントをいただいても、現在返信に対応しておりません。お手数をおかけしますが、お問い合わせの際には「NXPへの技術質問 - 問い合わせ方法 (日本語ブログ)」をご参照ください。(既に弊社NXP代理店、もしくはNXPとお付き合いのある方は、直接担当者へご質問いただいてもかまいません。) この記事は,「Zephyrデビュー会 〜FRDM-MCXN947でつまずかず動かす90分〜」の付録編です.Linuxの環境で試すためのコマンドや、本編の応用、用語集などを公開しています。 (手順の本文(0〜6章)と,うまくいかないときの対処(付録A)は,本編にあります.) 日本語ブログ Interface MCX Sensor Ethernet
記事全体を表示
Zephyr Debut Session ~90 Minutes to Get the FRDM-MCXN947 Working Without Stumbling~ [Main Feature] (Japanese Blog) This article is divided into two parts: the main text (Chapters 0-6 and Appendix A) and the appendix section (Appendices B-G) . The appendix section includes a list of command examples (PowerShell/bash/zsh), assignments for those who finish early, instructions on how to try it on other FRDM boards, a glossary, and frequently asked questions. table of contents 0. What is this article about? 0.1 Target audience 0.2 The Goal of This Article 0.3 Things to use 0.4 Procedure and Estimated Time Required 0.5 How to read this article 1. What is Zephyr? 1.1 Overview and Features of Zephyr 1.2 Why Zephyr now? 1.3 About FRDM-MCXN947 2. Try out the development environment 2.1 Checking the Environment 2.2 Let's run the Hello World sample. 2.3 Write the blinky sample (LED blinking) 2.4 Let's take a peek inside the sample code 3. Try operating the temperature sensor. 3.1 Build and flash the sample 3.2 Check the value 3.3 Why did it move? 4. Try running the HTTP Server. 4.1 Build and flash the sample 4.2 Trying to control an LED from a browser 4.3 Take a look at the /uptime endpoint 5. Modify the HTTP Server to view the temperature sensor values in a browser. 5.1 What was added? 5.2 Let's actually view it in a browser 5.3 How are the contents of Chapters 3 and 4 combined? 6. Summary 6.1 Things I experienced today 6.2 Resources for trying it again yourself 6.3 How can AI be utilized? 6.4 Cleaning up Appendix A. When things don't work A.1 First, check the following A.2 PowerShell Westbound A.3 Build errors A.4 Writing (west flash) error A.5 Nothing appears on the serial console. A.6 Network and Browser-Related Features A.7 Reset procedure when the problem still cannot be fixed Appendix (separate article): Appendix B. List of command examples (PowerShell/bash/zsh) Appendix C. Let's try it (for those who finish early) Appendix D. To try with other FRDM boards Appendix E. Glossary Appendix F. Frequently Asked Questions Appendix G.HTTP Server Sample Framework 0. What is this article about? This article was written as a progress guide for the hands-on seminar " Zephyr Debut Session ." In the actual seminar, we will follow the steps outlined in this article. Even if you didn't attend the seminar, you can reproduce the same content in your own environment by reading this article alone. To that end, I've included command examples and verification points without omitting any. Think of this not as a general explanatory article, but rather as a practical guide to get you to the point where it actually works. 0.1 Target audience This article is for beginners to Zephyr OS who have heard of it but have never used it, and for those who want to try Zephyr. No prior experience in embedded development is required. Just having some experience reading C language code is sufficient. 0.2 The Goal of This Article Using the NXP FRDM-MCXN947 board, you will gain hands-on experience in everything from blinking LEDs and reading temperature sensor readings to displaying sensor values via a browser. The goal is for you to take away the successful experience of "easily running apps on Zephyr." Difficult settings and coding will be kept to a minimum in this article. In this article, we will deliberately work using the command line . While there are convenient tools like NXP's MCUXpresso for VS Code that allow you to build and flash with button operations, we will directly use the west command provided by Zephyr as standard. This is to allow you to experience a neutral, raw Zephyr that is not dependent on any particular vendor's tools. The procedure described here can be used with boards from manufacturers other than NXP, simply by changing the board name. Today's schedule is as shown in Figure 0.2-1. In Chapter 2, you will become familiar with the development procedure, in Chapters 3 and 4 you will run different functions, and in Chapter 5 you will combine the two. Figure 0.2-1: Today's Schedule 0.3 Things to use hardware FRDM-MCXN947 Evaluation Board PC (with the following software installed, such as the Zephyr development environment) Temperature sensor: P3T1755 I3C/I²C bus compatible, ±0.5°C accuracy digital temperature sensor(Installed on the FRDM-MCXN947 evaluation board) Ethernet cable software Zephyr development environment (including west (command for building and flashing Zephyr), Zephyr SDK (a complete set of tools including a compiler), and a Python virtual environment; set up in ~/zephyrproject according to the official Getting Started Guide instructions) LinkServer (NXP's programming and debugging tool. west flash is used for programming the FRDM-MCXN947.) Microsoft Visual Studio Code (used for viewing and editing source code and prj.conf (application-specific configuration files)) Terminal software (This article uses Tera Term for Windows for explanation. If you are trying this on macOS or Linux, please use software that can connect to a serial port, such as CoolTerm.) browser Git (We will use this to obtain sample code in Chapter 5) MCUXpresso for VSC ( Not required . *It is installed on the venue PCs, but will not be used in the procedure described in this article.) *All of these features are already set up on the PCs at the venue. For those who want to reproduce this on their own PC To replicate the same setup as the venue PCs, you need to install the software listed above. LinkServer , in particular, is often overlooked. If it's not installed (or not in your PATH), you might be able to build the project, but you won't be able to write it (it will show west flash ). Also, Chapter 4 uses the PC's Ethernet port, so if your laptop doesn't have an Ethernet port, please prepare a USB-Ethernet adapter. Please refer to one of the following instructions for installation. Official guide : Getting Started Guide (English). The procedure is the same as the premise of this article (configuration to create a workspace and Python virtual environment .venv in ~/zephyrproject ). LinkServer must be installed separately. An easy method using NXP tools : [Zephyr ® Series] Part 2: First Build and Operation on Actual Device (NXP Tech Blog, Japanese) . You can install the Zephyr development environment and LinkServer together using MCUXpresso for VS Code and the MCUXpresso Installer. However, the location of the Python virtual environment (under C:\Users\<ユーザー名>\.mcuxpressotools\ on Windows) and the Zephyr folder will differ from that described in this article. Please adjust the folder locations in command example 2.1-1, etc., to match your own environment. 0.4 Procedure and Estimated Time Required The seminar will be 90 minutes long. The schedule will be roughly as follows: chapter Content guideline Chapter 0-1 About this article, what is Zephyr? 10 minutes Chapter 2 Checking the development environment, Hello World, blinking an LED. 25 minutes Chapter 3 Move the temperature sensor 15 minutes Chapter 4 Running an HTTP Server 20 minutes Chapter 5 Displaying temperature on an HTTP server 15 minutes Chapter 6 summary 5 minutes [Adjust to match the actual timetable] If I'm late Chapters 3 through 5 are independent of each other. Even if you haven't finished the previous chapter, you can start from any chapter by running command example 2.1-1 ( zinit on the venue PC) in a new PowerShell window. For example, if you get stuck in Chapter 3 and time runs out, you can leave Chapter 3 aside for now and start from the first command example in Chapter 4. You can refer to this article later and start again at your own pace. If it finishes quickly Appendix C, "Let's Try It ," includes an exercise where you'll modify a sample code slightly and try it out. When asking a question If you get stuck, please don't hesitate to raise your hand. When asking questions to the venue staff, or to the AI or acquaintances later while looking at this article, providing the following three points will help us quickly find the cause. Which command example caused the code block to stop? (The number, like # コマンド例3.1-2 , is on the first line of the code block.) The line containing the first error (the line with red text or error ; a screenshot is also acceptable). Execution result of command example A.1-1 (You can see the current folder and the environment settings) 0.5 How to read this article The sections in Chapters 2 through 5 are arranged in the following order. At the venue, please first complete the procedure to the end and confirm that you have reached this point before listening to the explanation. What to do in this section (1 line) Instructions (Example commands. Longer sections are divided into "Step 1," "Step 2," etc.) Example display (screen and display when successful) ✔ If you can do this much, you're good to go (Check points to make sure it matches the example) When things don't go well (common stumbling blocks and how to deal with them) Commentary/Column The boxes within the text are used in the following ways: box Content When to read Note Things you must keep in mind when proceeding with the procedure Read this before proceeding. Explanation What do the operations and code I just performed mean? After the procedure is complete, the instructor will explain it on the spot at the venue. column Background knowledge and related topics When you have time, or later If you want to try it on your own PC Supplementary information for reproducing the results on a computer other than the venue's PC. When trying it on your own PC Terms appearing in the main text are listed in Appendix E, Glossary . If you are reading this article in VS Code At the venue, you will read this article displayed on the right side of VS Code (Visual Studio Code). Commands within code blocks can be selected by dragging, copied using Ctrl + C , and pasted into PowerShell. The right-hand area, where the article is displayed (preview), is locked . Therefore, files opened with a command like code prj.conf will always open on the left side. The article on the right will remain as you were reading it. If you have closed the article on the right, follow these steps to restore it. Open ~\seminar_text\article_draft.md using "File" → "Open File..." in VS Code. Ctrl Pressing + K followed by V will display a preview of the article on the right. Click the preview tab on the right, then open the command palette by pressing Ctrl + Shift + P , and execute "View: Lock Editor Group". You can close the article_draft.md tab that opened on the left (to prevent accidental overwriting). If you are reading this in a browser or on the NXP blog, you do not need to follow these steps. ▲ Return to Table of Contents 1. What is Zephyr? 1.1 Overview and Features of Zephyr Zephyr is an open-source RTOS (Real-Time Operating System) under development by the Linux Foundation. It targets a wide range of hardware, from small microcontrollers to relatively resource-intensive SoCs. The breadth of its coverage is evident even when looking only at NXP products. From Cortex-M core microcontrollers like the MCX series we'll be using, to Cortex-A core application processors like the i.MX series, it's all covered by the same Zephyr OS. Not having to use different OSs or build environments for each board is a significant practical advantage. In short, its key feature is that it allows for the separate management of hardware configuration and software functions through the following two mechanisms. Device tree : A file that describes the hardware configuration, showing "which components are connected to where." Kconfig : A mechanism for turning software features on or off, specifying "which features to use." This separation makes it easier to deploy the same application's code to multiple boards. In other words, the logic of an application written in C or another language doesn't need to directly know "which sensor" is connected to "which bus." The application code simply accesses the device using the name assigned to it in the device tree. You will experience this separation by looking at actual code in sections 2.4 and 3.3. 1.2 Why Zephyr now? Semiconductor manufacturers are expanding their support for Zephyr, including in their existing product lines. As an RTOS that is not tied to a specific vendor, its presence in the industry is currently growing. The Zephyr project started in 2016 under the Linux Foundation and has a history of approximately 10 years. It is not a new, immature technology. It's not just the CPU architecture and peripheral drivers that have been built up during this time. Network-related middleware, such as the TCP/IP stack and HTTP server that you'll experience in Chapters 4 and 5, has also grown into a standard feature built into the Zephyr core. When you think of an OS for microcontrollers, you tend to imagine only a minimal scheduler. The fact that you can consistently enable even this higher-level middleware with Kconfig is one of the reasons to choose Zephyr. Zephyr is attracting attention as an operating system for products because of its robust security measures. In Europe, the Cyber Resilience Act (CRA) has been enacted, requiring cybersecurity measures for network-connected products, and from September 2026, manufacturers will be obligated to report exploited vulnerabilities. The Zephyr project is preparing to meet these requirements by establishing a system for addressing vulnerabilities and providing long-term support (see column below). Ultimately, it's difficult to truly understand "what makes it so good" without actually experiencing it. In this article, we'll explore its feel by actually using it. Column: Zephyr's approach to security There is a dedicated team (PSIRT) to address vulnerabilities, and it has been registered with the CNA (Computer Security Information Agency) since 2017, which assigns CVE numbers. The goal is to notify manufacturers and release fixes within 30 days of receiving a report, and to make them public within a maximum of 90 days. Product manufacturers can register with the Vulnerability Alert Registry to receive information about vulnerabilities. Long-Term Support (LTS) versions of products are maintained for at least five years, including security fixes. west spdx This allows you to create a list of software included with the product (SBOM). The encryption function is provided by PSA Crypto (implemented as Mbed TLS). The FRDM-MCXN947 also has a board specification ( frdm_mcxn947/mcxn947/cpu0/ns ) for use with TF-M which uses TrustZone. The relationship between CRA and Zephyr is summarized on the Cyber Resilience Act page of the official Zephyr documentation. Note that this hands-on session uses unencrypted plain HTTP for simplicity (see section 4.1 for details). 1.3 About FRDM-MCXN947 The FRDM-MCXN947 is an evaluation board equipped with NXP's MCX N947 microcontroller. It features a dual-core Cortex-M33 configuration operating at 150MHz. Peripherals such as GPIO, I2C, I3C, SPI, and Ethernet can be accessed via connectors such as Arduino headers and mikroBus sockets. This article uses the following components on the board (Figure 1.3-1). In the following text, we will indicate their locations using these numbers, such as "① in Figure 1.3-1". Figure 1.3-1: Components used in FRDM-MCXN947 number parts When to use ① Debug USB connector (MCU-Link side) Connecting to a PC. Used for both programming ( west flash ) and serial console. Power is also supplied from here. The MCU-Link is the programming and debugging circuit on the board. ② RESET button When you want to restart the program from the beginning (for example, when you missed the serial output) ③ RGB LED Section 2.3: LED blinking (red blinking), Section 4.2: LED control from a browser (green light) ④ Temperature sensor (P3T1755) Chapter 3, Chapter 5 ⑤ Ethernet connector (RJ45) In chapters 4 and 5, you will connect the PC directly using an Ethernet cable. Note : The board has multiple USB connectors. Connecting to any USB connector other than ① will prevent you from writing to the board or using the serial console. ▲ Return to Table of Contents 2. Try out the development environment 2.1 Checking the Environment In this section, we will open PowerShell and verify that the Zephyr command (west) is available. The PCs at the venue are set up with a development environment including the west/Zephyr SDK, following the instructions in the official Getting Started guide. You won't need to worry about setting up the environment yourself. MCUXpresso for VSC is also available, but as mentioned in section 0.2, we will be using command line operation in this seminar. Step 1: Open PowerShell In the Start menu, type "PowerShell" and launch "Windows PowerShell" (Figure 2.1-1). Do not select "Run as administrator." Administrator privileges are not required for the procedures in this seminar, and running it as administrator could cause problems throughout your entire PC if you make a mistake with a command. You can tell if you are running it as administrator by whether "Administrator:" appears at the beginning of the window tab (or title). Figure 2.1-1: Opening "Windows PowerShell" from the Start menu The initial folder (current directory) of PowerShell varies depending on how it is launched. As shown in Figure 2.1-2, it may display PS C:\WINDOWS\system32> even if you are not running it as an administrator. It may open in your home folder ( C:\Users\<ユーザー名> ), or in the VSCode terminal, it will open in the currently selected folder. No matter where you open it, the first cd in the example command will take you to the Zephyr workspace ( ~\zephyrproject ), so there is no problem. ~ is a symbol that represents your home folder. Figure 2.1-2: Launched PowerShell (Example opened with C:\WINDOWS\system32 ) Step 2: Make west usable. Before using west, activate the Python virtual environment for Zephyr (an independent Python environment containing all of Zephyr's tools) and set the location of the Zephyr executable to environment variable ZEPHYR_BASE . Copy the following example command, paste it into PowerShell, and execute it. When pasting You can paste by right-clicking on the PowerShell window or by pressing Ctrl + V . If the program is stuck with the last line entered, press Enter to execute. You can also paste the first line, # コマンド例… , as is (it does nothing in PowerShell). Other important points are summarized in the "Column: Precautions when copying and pasting commands" at the end of this section. # コマンド例2.1-1 cd ~\zephyrproject .\.venv\Scripts\Activate.ps1 $env:ZEPHYR_BASE = "$HOME\zephyrproject\zephyr" west --version west topdir When executed, the version will be displayed in west --version and the workspace location in west topdir as shown below (Figure 2.1-3). The prompt (the PS C:\...> display waiting for command input) will be preceded by (.venv) . (.venv) PS C:\Users\<ユーザー名>\zephyrproject> west --version West version: v1.5.0 (.venv) PS C:\Users\<ユーザー名>\zephyrproject> west topdir C:/Users/<ユーザー名>/zephyrproject west topdir The display will show / instead of \ as the folder separator, but it points to the same location. Figure 2.1-3: Execution result of command example 2.1-1 ✔If you've reached this point, you're good to go: The prompt will start with (.venv) , and your West version and workspace location will be displayed. (The output shown below as an "example of execution result" is for comparison with your own screen; do not copy it as a command. Numerical values and version numbers will vary depending on your environment.) Note: Do not close this PowerShell window. Enabling the virtual environment and setting ZEPHYR_BASE are only effective within that window . Please keep this window open during the seminar. If you close it, you can return to the previous state by following the steps in "When you reopen PowerShell" below. When things don't go well .\.venv\Scripts\Activate.ps1 Then, a message in red text will appear saying, "Script execution is disabled on this system." → After configuring the execution policy as described at the end of the explanation below, run command example 2.1-1 again from the beginning. west If it says "Not recognized" → Virtual environment activation failed. Check if (.venv) is at the beginning of the prompt ( A.2 ). cd If you get the message "Cannot be found because the path does not exist," then your workspace may be located in a location other than ~\zephyrproject . If you are trying this in your own environment, please substitute the workspace location. When I reopened PowerShell After resetting the environment with the new PowerShell, return to the sample folder you were working in. The PCs at the venue have a command called zinit that does this all at once. Specify the name of the sample you were working in after zinit . For example, if you are in the middle of Chapter 3, execute it as follows: # コマンド例2.1-2 zinit thermometer The sample name will be completed by typing part of it and pressing the Tab key. If you execute only zinit without specifying a sample name, you will be in the same state as immediately after executing command example 2.1-1 (the state in ~\zephyrproject ). zinit can also be used in the VS Code terminal. zinit On PCs without this feature, execute command example 2.1-1, then move to the sample folder as shown in cd ~\handson\thermometer . Immediately after executing command example 2.1-1, you will be in ~\zephyrproject , so executing west build directly will not build the sample. Explanation: What is being done in command example 2.1-1? cd ~\zephyrproject — Navigate to the Zephyr workspace (the folder containing west, the Zephyr core, and various modules). .\.venv\Scripts\Activate.ps1 — This activates the Python virtual environment provided for Zephyr. West itself and various Python tools used for building are installed in this virtual environment. If activation is successful, (.venv) will be displayed at the beginning of the prompt. $env:ZEPHYR_BASE = "..." — Set the Zephyr main folder as environment variable ZEPHYR_BASE . west will search for the workspace by traversing the folders above the current directory. In the following chapters, we will build in a working folder outside the workspace ( ~\handson\... ). From there, the workspace cannot be found, and west build becomes "unknown command". Setting ZEPHYR_BASE allows west to use it as a clue to find the workspace. west --version — Check the version display to see if the `west` command itself is working correctly. west topdir — Displays the workspace locations recognized by west. If you encounter an error here, please review the ZEPHYR_BASE settings. In articles explaining Linux/macOS, you often see notations like source ~/zephyrproject/.venv/bin/activate and source zephyr/zephyr-env.sh . These are notations for bash/zsh and will not work as is in PowerShell. zephyr-env.sh The main function is setting ZEPHYR_BASE . Therefore, in PowerShell, you set it directly using $env:ZEPHYR_BASE as shown above. (There is also zephyr-env.cmd available for Windows, but this is for the command prompt. Even if you execute it from PowerShell, the environment variables you set will not remain in PowerShell.) If you encounter an error message stating "Script execution is disabled" when running Activate.ps1 , it means that the PowerShell execution policy is preventing it. Please run Set-ExecutionPolicy -Scope CurrentUser RemoteSigned once and then try again (this setting is already configured on the venue PCs). Column: Precautions when copying and pasting commands Confirmation dialog for pasting multiple lines : In Windows Terminal, when pasting multiple lines, a confirmation message may appear stating, "You are trying to paste multiple lines of text." Please select paste as is. If an error occurs midway, it will stop there : When pasting multiple lines at once, even if an error occurs in an intermediate line, the subsequent lines will continue to execute. If you see an error in red text, do not rely on the results after that point, and first resolve the cause of the error in the line where it occurred. Until you get used to it, it is safer to copy and execute one line at a time . The rows marked with # コマンド例… are markers : Please use these numbers when asking questions or referring to Appendix A. Do not copy the prompt ( PS C:\Users\...> ) : The prompt shown in screenshots and execution examples is not part of the command. If it doesn't work, please refer to Appendix A , "Troubleshooting." The command examples in this article are also compiled in Appendix B, "List of Command Examples ." Bash and zsh versions for macOS and Linux users are also included there. Column: How to use zinit on your PC zinit This is a function registered in your PowerShell profile (a script that is automatically loaded when PowerShell starts). You can create the same thing on your own PC as follows: First, open the profile in Notepad using the following command (create the file if it doesn't exist). # コマンド例2.1-3 if (-not (Test-Path $PROFILE.CurrentUserAllHosts)) { New-Item -Force $PROFILE.CurrentUserAllHosts | Out-Null } notepad $PROFILE.CurrentUserAllHosts Open Notepad, add the following content, and save it. This is not a command to paste into PowerShell and execute, but rather content to write to your profile . function zinit { param([string]$Sample) Set-Location ~\zephyrproject . .\.venv\Scripts\Activate.ps1 $env:ZEPHYR_BASE = "$HOME\zephyrproject\zephyr" west --version west topdir if ($Sample) { Set-Location ~\handson\$Sample } } Once saved, reopen the PowerShell window and zinit will become available. Running the profile also requires the execution policy settings ( RemoteSigned ) explained in this section. Note that if you write Japanese in the profile, you need to pay attention to the character encoding (Windows PowerShell cannot correctly read Japanese characters contained in UTF-8 files without a BOM). The above content only contains alphanumeric characters, so you can save it as is. If you want to try it on your own PC: Environment setup This time, the environment will be pre-configured on the venue's PCs, but you can also set up the same environment on your own PC. For installation instructions, please refer to Section 0.3 , "For those who want to reproduce the issue on their own PC." 2.2 Let's run the Hello World sample. In this section, we will build the hello_world sample, write it to the board, and verify that characters are displayed on the serial console. From this point onward, do not directly edit the samples in zephyr/samples/ , but copy them to your working folder ( ~\handson ) before using them (Figure 2.2-1). Leave zephyr/samples/ as the original sample collection without making any changes, and perform all experiments and modifications only within your own folder. Figure 2.2-1: Copy the sample to your working folder and use it. Step 1: Copy the sample to your working folder. hello_world Copy the sample to your working folder and then move into that folder. # コマンド例2.2-1 New-Item -ItemType Directory -Force ~\handson | Out-Null Copy-Item -Recurse ~\zephyrproject\zephyr\samples\hello_world ~\handson\hello_world cd ~\handson\hello_world After execution, if the prompt shows PS C:\Users\<ユーザー名>\handson\hello_world> , you have successfully moved. Just to be sure, if you execute ls , you should see src , CMakeLists.txt , prj.conf , etc. lined up. Step 2: Build Note: Don't forget the final . . (space and period) at the end of the line means "build the application in the current directory." It's easy to cut off when copying, so please check after pasting. The board name after -b , including / and // , will also result in an error if even one character is different. Please copy and paste instead of retyping manually. # コマンド例2.2-2 west build -p auto -b frdm_mcxn947/mcxn947/cpu0 . The initial build may take several minutes. Please wait until the prompt returns. If the build is successful, a table showing FLASH and RAM usage will appear at the end of the output. (前略) [xxx/xxx] Linking C executable zephyr\zephyr.elf Memory region Used Size Region Size %age Used FLASH: xxxxx B 2 MB x.xx% RAM: xxxx B 320 KB x.xx% SRAMX: 0 B 96 KB 0.00% IDT_LIST: 0 B 32 KB 0.00% Generated files: (後略) [Replaced with the actual output from the venue's PC] ✔If you've reached this point, you're good to go: At the end of the output, a table showing the usage of FLASH and RAM will appear. Now you have the program ready to write to the microcontroller. If an error occurs, the message will be displayed. Zephyr error messages are often numerous and can be difficult to track at first. The trick is to first look for the line that starts with error . You should either carefully read the message or use AI to understand it. Common errors and their solutions are summarized in A.3 Build Errors . Step 3: Prepare to write Before writing, prepare the following two things. (1) Connect the PC and the board with a USB cable. Connect ① (Debug USB connector, MCU-Link side) in Figure 1.3-1 to your PC using a USB cable (Figure 2.2-2). The LED on the board will light up, indicating that the power is on. Note that a USB cable designed for charging only will not support data communication; please use a cable that supports data communication. Figure 2.2-2: Connect the USB cable to ① in Figure 1.3-1. (2) Keep the serial console open. hello_world This program outputs a string only once immediately after startup and then terminates. If you open the serial console after writing the program, you will miss the output. Therefore, open the serial console (a screen that displays the output of the board's printf , etc., on your PC) and connect before writing the program . This time we will use Tera Term as the terminal software. Please connect using steps 1 to 4 of the following "Tera Term Procedure". To launch Tera Term, click the "Tera Term 5" icon on the desktop (Figure 2.2-3). Once launched, click "TeraA screen titled "Term: New Connection" will open. Select "Serial" to choose a serial port , then from the list of "Ports," select the port corresponding to MCU-Link (① in Figure 1.3-1) and press "OK" (Figure 2.2-4). The list displays names like COM3: MCU-Link VCom Port (COM3) , where COM + number is used. The number varies depending on the PC. If you're unsure which one to choose, unplug and replug the board's USB cable, and select the port that disappears or reappears in the list (the list will update when you reopen the "New Connection" screen). Set the baud rate (serial communication speed) to 115200. Select "Settings" → "Serial Port" from the menu, and the "Serial Port" tab of the "Tera Term: Other Settings" screen will open. Set "Speed" to 115200 and press "OK" (Figure 2.2-5). (Older versions of Tera) In Term, the screen names and buttons are different. Set the character encoding to UTF-8 (this setting is not necessary on the venue's PCs). Open "Settings" → "Other Settings" from the menu and select the " Encoding " tab (you can also switch tabs while remaining on the screen from step 3). Set the receiving character encoding to UTF-8 and press "OK". This setting is necessary to correctly display ° in °C as shown in Chapter 3. Figure 2.2-3: Tera Term icon on the desktop Figure 2.2-4: Select a serial port in "New Connection" Figure 2.2-5: Set the speed to 115200 in "Settings" → "Serial Port". Once you've set it up, you can keep Tera Term open in subsequent chapters. You don't need to reconnect every time you write. Step 4: Write Once you're ready, return to PowerShell and write the following command: # コマンド例2.2-3 west flash Once the writing process is complete, the microcontroller will reset and the written program will be executed. The following string will appear on the serial console (Figure 2.2-6): *** Booting Zephyr OS build v4.x.x *** Hello World! frdm_mcxn947/mcxn947/cpu0 The first line is the message that Zephyr always displays when it starts up, and the v4.x.x part is the Zephyr version. The second line is the string output by the program hello_world . Figure 2.2-6: Screenshot of "Hello World" displayed in Tera Term ✔If you can do this, you're good to go: Hello World! frdm_mcxn947/mcxn947/cpu0 should be displayed in Tera Term. When things don't go well I forgot to open the serial console before writing and missed the output → With Tera Term connected, press the RESET button on the board (② in Figure 1.3-1). The program will run again from the beginning, and the string will be displayed again. west flash If you get an error message such as "No probe found," check that the USB cable is plugged into port ① in Figure 1.3-1. Unplug and replug the cable, then run command example 2.2-3 again ( A.4 ). If the writing was successful but nothing is displayed or the characters are garbled, check the port, baud rate, and character encoding settings in Tera Term ( A.5 ). Explanation: Copying to a working folder (Command example 2.2-1) The first line New-Item creates a working folder ~\handson (do nothing if it already exists; this line only needs to be written the first time). Copy-Item -Recurse copies the sample folders from zephyr\samples\ to the working folder, and cd moves you into that folder. In subsequent chapters, you should continue to copy the sample you want to work with to the working folder before starting. Copy-Item If the destination folder already exists, this command will create another sample folder inside it (e.g., ~\handson\hello_world\hello_world ). If you want to redo the copy, first delete the old folder using Remove-Item -Recurse -Force ~\handson\hello_world before running the command again. Explanation: -b and -p options (command example 2.2-2) -b The argument that follows specifies the board to be built. Since the FRDM-MCXN947 has a dual-core Cortex-M33 configuration, you specify the "board name/SoC name/core name" as in frdm_mcxn947/mcxn947/cpu0 . For single-core boards like the FRDM-MCXA153, only the board name, frdm_mcxa153 , is needed. You can also omit the SoC name and write it as frdm_mcxn947//cpu0 . This is because, on boards with only one SoC, such as the FRDM-MCXN947, the SoC name can be omitted, leaving only a slash, and the board will still be uniquely identified. In this article, we will use this abbreviation from section 2.3 onwards. -p auto This is an abbreviation for --pristine=auto . It automatically determines whether to rebuild the build folder from a clean state (pristine build). More details will be explained in the following section. The meaning of these options remains the same for west build and west flash , which will appear in subsequent chapters. Explanation: Location of files created during the build process. If the build is successful, a folder build/ will be created in the current directory. build/zephyr/zephyr.elf (an ELF file with debug information) will be used for writing, and west flash , which will be executed next, will also write this file to the board. In addition, the configuration contents can be checked in build/zephyr/.config , and the device tree after extraction can be checked in build/zephyr/zephyr.dts . --pristine This option specifies whether to recreate this build folder. -p auto ( --pristine=auto ) will only recreate the build folder when necessary, based on changes to the source or configuration. Specify -p always ( --pristine=always ) if you want to explicitly perform a clean build, such as when the build process is abnormal. If you want to try it on your own PC (using Tera Term) If you save the settings from steps 3 and 4 in Tera Term by going to "Settings" → "Save Settings," you can connect using only step 2 from the next time onward. Tera Term cannot be used on macOS or Linux. Please use CoolTerm or a similar application, select port /dev/tty.usbmodemXXXX (macOS) or /dev/ttyACM0 (Linux), and connect with a baud rate of 115200. hello_world Let's take a look at the code. hello_world The src/main.c is an astonishingly short piece of code. To open this file in VS Code, run code ~\handson\hello_world\src\main.c from PowerShell. (Please do not use VS Code's "Open Folder" to open a folder, as this will switch the contents of the window displaying the article.) #include int main(void) { printf("Hello World! %s\n", CONFIG_BOARD_TARGET); return 0; } From what we can see, this is a perfectly ordinary C code Hello World . The only thing that seems special is that the macro CONFIG_BOARD_TARGET is probably defined within this build environment. Clock settings and peripheral initialization processes, which are commonly found in microcontroller programs, are nowhere to be found in this code. This code also has a "non-RTOS-like" quality. There's no task (thread) creation or scheduler startup. It simply calls main() through printf() , making it look almost identical to a C program for a PC. In many RTOSs, even for a simple "Hello World" program, you need to explicitly create a task and invoke the scheduler with a call like osKernelStart() . In Zephyr, main() itself already runs as a thread. The initialization before that is left to the build system and Kconfig settings. This "feel close to writing plain C programs" is one of the features that Zephyr presents to application developers. Explanation: Files required in addition to main.c The minimum requirements for a Zephyr application are src/main.c , as well as prj.conf and CMakeLists.txt . prj.conf This is a configuration file that lists the Kconfig features used by the application. hello_world means there are no features that you particularly want to enable, so it is almost empty. CMakeLists.txt This is the build definition file. find_package(Zephyr ...) loads the Zephyr build system, and target_sources() specifies the source files to build. In fact, if you look inside zephyr/samples/hello_world/ , you'll only find files like CMakeLists.txt , prj.conf , src/main.c (and README.rst for explanation). The minimum configuration for a Zephyr application is this set of three files. 2.3 Write the blinky sample (LED blinking) In this section, we will write the blinky sample to make the board's LED blink. The procedure is the same as for Hello World, only the sample we copy changes. From now on, we will use the abbreviation frdm_mcxn947//cpu0 mentioned in the explanation in section 2.2 to specify the board. # コマンド例2.3-1 Copy-Item -Recurse ~\zephyrproject\zephyr\samples\basic\blinky ~\handson\blinky cd ~\handson\blinky Make sure the prompt is ...\handson\blinky> , then build. Note: Build and write operations should be performed separately. west build If you paste west flash at the same time, west flash will continue to run even if the build fails. In that case, the error on west flash will be displayed last, so the real cause (the build error) will be pushed to the top of the screen. If you follow the order of confirming that the build was successful before writing, you will be able to see where the failure occurred immediately. # コマンド例2.3-2 west build -p auto -b frdm_mcxn947//cpu0 . After confirming that the build was successful (that the memory usage table appears at the end), write the changes. # コマンド例2.3-3 west flash The RGB LED on the board (③ in Figure 1.3-1) blinks red every second (Figure 2.3-1). If you leave Tera Term connected, the following message will also be displayed along with the blinking. Figure 2.3-1: RGB LED blinks red with blinky *** Booting Zephyr OS build v4.x.x *** LED state: OFF LED state: ON LED state: OFF LED state: ON ✔This is the correct result: The RGB LED blinks red every second. When things don't go well If you executed west build while the prompt was still ...\hello_world> , hello_world will simply be rebuilt, which is harmless. Please start again from cd in command example 2.3-1. Copy-Item If you run it twice, a nested folder called ~\handson\blinky\blinky will be created, but this will not affect the build in ~\handson\blinky . If this bothers you, delete it using the method explained in section 2.2 and then copy it again. 2.4 Let's take a peek inside the sample code In this section, we will examine the code for blinky to determine where the LED's position is determined. blinky src/main.c is a short piece of code that simply toggles (inverts the output of) a GPIO. Let's look at the whole thing. To open it in VS Code, run code ~\handson\blinky\src\main.c from PowerShell. /* * Copyright (c) 2016 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include #include #include /* 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; } Starting from the top, the following things are being done: SLEEP_TIME_MS Then, the blinking interval (1000 milliseconds = 1 second) is determined. DT_ALIAS(led0) The GPIO_DT_SPEC_GET() command retrieves information ( led ) indicating which GPIO pin the LED named led0 is connected to. main() First, we check if the GPIO is available using gpio_is_ready_dt() , and then we set that pin as an output using gpio_pin_configure_dt() . while (1) In this loop, the LED is inverted at gpio_pin_toggle_dt() , the status is displayed at printf() , and the program waits for 1 second at k_msleep() , and this process is repeated. What's noteworthy here is that nowhere in the code is it written which pin the LED is connected to. The code only specifies an alias (another name) led0 . Which LED led0 actually refers to is defined in the Zephyr board's device tree (a file describing the hardware configuration, zephyr/boards/nxp/frdm_mcxn947/frdm_mcxn947.dtsi ) as follows: aliases { led0 = &red_led; ... }; red_led: led_3 { gpios = <&gpio0 10 GPIO_ACTIVE_LOW>; label = "Red LED"; }; (excerpt) The board has information written to it that " led0 is the red RGB LED (pin 10 of GPIO0)," so the application only needs to say led0 . The blinky folder does not contain any additional settings for the FRDM-MCXN947 (overlays placed in the boards/ folder. These are files that overlay differences onto the board's device tree, and are explained in section 3.3). It works as is with just the definitions on the board. This is why the same blinky works as is on any board that defines led0 . The relationship between the application, device tree, Kconfig, and driver is illustrated in Figure 2.4-1. Kconfig is the mechanism for turning Zephyr's functions on and off (we will discuss this again in Chapter 3). Figure 2.4-1: Relationship between applications, device trees, Kconfig, and drivers in blinky. Column: MCX but called "kinetis"? In the diagram, kinetis within compatible = "nxp,kinetis-gpio" is the name of NXP's previous microcontroller series (Kinetis). Since the MCX's GPIO uses the same circuitry as Kinetis, Zephyr does not create a new name, but uses the same name and driver ( gpio_mcux.c ). The "device tree" introduced here will reappear in Chapter 3 when we explain the mechanism of temperature sensors. In Chapter 3, we will look at an example of adding parts that are not covered by the board-side definition using an application-side overlay. ▲ Return to Table of Contents 3. Try operating the temperature sensor. 3.1 Build and flash the sample In this section, we will write the thermometer sample code that reads the temperature sensor on the board. As in Chapter 2, copy it to the working folder before building and writing it. # コマンド例3.1-1 Copy-Item -Recurse ~\zephyrproject\zephyr\samples\sensor\thermometer ~\handson\thermometer cd ~\handson\thermometer # コマンド例3.1-2 west build -p auto -b frdm_mcxn947//cpu0 . Once the build is successful, make sure Tera Term is still connected before writing the files (if you have closed it, reconnect using the procedure in Section 2.2). # コマンド例3.1-3 west flash 3.2 Check the value The serial console displays the temperature value every second (Figure 3.2-1). If you touch the temperature sensor (④ in Figure 1.3-1 ) with your finger, you can see the value gradually increase. *** Booting Zephyr OS build v4.x.x *** Thermometer Example (arm) Temperature device is 0x1000b23c, name is p3t1755@4800000236152a0090 Temperature is 25.9°C Temperature is 25.8°C Temperature is 25.8°C The first few lines are sample startup messages. From the line marked name is p3t1755@4800000236152a0090 , we can see that the P3T1755 is being used as a temperature sensor, as defined in the device tree in Section 3.3. (The part marked 0x1000b23c may vary depending on the build.) Figure 3.2-1: Screenshot showing temperature in Tera Term ✔If you can do this much, you're good to go: The temperature should be displayed in Tera Term every second, and the value should increase when you touch the sensor with your finger. When things don't go well If nothing is displayed, check if the COM port is displayed as COM3 - Tera Term VT in the Tera Term title bar (indicating a connection), and then press the RESET button on the board ( A.5 ). °C The part may appear garbled → This is due to the character encoding settings in Tera Term, and does not affect its operation. If it bothers you, please set the receiving character encoding to UTF-8 in step 4 of Tera Term in section 2.2. Device ... is not ready If you see a message like this → Sensor initialization failed. Check if you have written another sample (such as hello_world ) or if you built it while in the folder for this sample using cd ~\handson\thermometer , and then start again from command example 3.1-2. 3.3 Why did it move? In this section, we will explain why the sensor values were displayed by looking at src/main.c and boards/*.overlay . main.c On the other side, the sensor device is acquired as follows: static const struct device *const temp_dev = DEVICE_DT_GET(DT_ALIAS(ambient_temp0)); boards/*.overlay determines which hardware (sensor type and connection bus) this alias ambient_temp0 actually refers to. The overlay is a file that overlays the differences for each application onto the device tree of the board itself. In the FRDM-MCXN947, the temperature sensor is connected to the I3C bus. boards/frdm_mcxn947_mcxn947_cpu0.overlay is defined as follows (the actual file also includes specifications for the I3C bus clock speed, but only the essentials are extracted here). / { aliases { ambient-temp0 = &p3t1755; }; }; &i3c1 { status = "okay"; p3t1755: p3t1755@4800000236152a0090 { compatible = "nxp,p3t1755"; reg = <0x48 0x0236 0x152a0090>; status = "okay"; }; }; There are only two things I'm doing. aliases In the block, connect ambient-temp0 to the p3t1755 node. i3c1 Below the bus, add a sensor node that uses the nxp,p3t1755 driver. main.c The side only says DT_ALIAS(ambient_temp0) . The application code has no idea that this sensor is connected via I3C (which is faster) instead of I2C, nor does it know the details of the register address. The key point here is that "you can support different hardware configurations simply by changing the definition of the device tree, without writing a single line of code." For example, consider the case where you move the same sensor to an I2C-connected board or change to a different temperature sensor IC (assuming a compatible driver is available). Basically, only the contents of boards/*.overlay need to be changed, and the line DEVICE_DT_GET(DT_ALIAS(ambient_temp0)) in main.c can be reused as is. This relationship can be illustrated as shown in Figure 3.3-1. Figure 3.3-1: Relationship between applications, device tree, Kconfig, and drivers in a thermometer. Explanation: Where does the device tree actually exist? The main component of the FRDM-MCXN947 board's device tree is zephyr/boards/nxp/frdm_mcxn947/frdm_mcxn947_mcxn947_cpu0.dts (and the common definition frdm_mcxn947.dtsi ) on the Zephyr board itself. This defines the complete set of peripherals that come standard on the board. The device tree file on the board itself is not directly modified. Zephyr places a differential file (overlay, *.overlay ) on the application side to append to or overwrite the definition on the board itself. In Thermometer, it's samples/sensor/thermometer/boards/frdm_mcxn947_mcxn947_cpu0.overlay , and in the repository used in Chapter 5, it's boards/frdm_mcxn947_mcxn947_cpu0.overlay . The definition of the p3t1755 node shown in the main text is also part of this overlay. The advantage is that you can overlay only the differences necessary for each application while keeping the definition of the board itself the same. When you build, the board's device tree and the application's overlay are merged, and the entire final device tree is written to build/zephyr/zephyr.dts . If you open this file, you can see that the p3t1755 node has been added to the board's definition. In bare-metal development without an OS, and in many RTOSs that lack a device tree mechanism, it's common to directly write sensor I2C/I3C addresses and register operations into the application code. As a result, the application logic needs to be rewritten every time the sensor model or bus wiring changes. Zephyr moves the information of "which component" and "where" it's connected to a separate layer called the device tree. Therefore, even if the board changes, the application logic itself (in this case, the process of "reading and displaying the temperature") does not need to be touched. If you're interested, try comparing the overlay files for other boards. You'll see that even though they're all samples/sensor/thermometer , each board has a different overlay with only the hardware connections differing. ▲ Return to Table of Contents 4. Try running the HTTP Server. In this chapter, we connect the PC and the board with an Ethernet cable and access the HTTP Server running on the board from a browser (Figure 4.1-1). Up until now, everything has been self-contained on the board alone, but from here we take it a step further. We will use the official Zephyr sample samples/net/sockets/http_server . Build and flash it as is, and you can turn the LED on/off from a browser and check the response of a simple endpoint (a window provided for each URL path, like /uptime ). In Chapter 5, we will use this mechanism as a foundation and extend it so that the temperature sensor values can be viewed in a browser. The procedure for running the HTTP Server sample on the FRDM-MCXN947 has already been published as a separate blog post. For more detailed information, such as PC network settings and procedures for using DHCP instead of a static IP address, please refer to that post. I tried running Zephyr HTTP Server on FRDM-MCXN947 (NXP Tech Blog, Japanese) 4.1 Build and flash the sample In this section, we will rewrite the IP address of the http_server sample and write it down, then verify that a response is received from the PC at ping . This time, we will proceed using plain HTTP without TLS and a fixed IP address. Figure 4.1-1: PC and board connection and IP address (USB cable and Ethernet cable) Step 1: Copy the sample to your working folder. # コマンド例4.1-1 Copy-Item -Recurse ~\zephyrproject\zephyr\samples\net\sockets\http_server ~\handson\http_server cd ~\handson\http_server Step 2: Rewrite the IP address of prj.conf Running the following command will open prj.conf in VS Code. # コマンド例4.1-2 code prj.conf You may see a banner at the top of VSCode that says "Restricted Mode is intended for safe code browsing." (This is also visible at the top of Figure 4.1-2 later). This is a notification that the opened file is being displayed in a mode with restricted functionality for safety reasons. It does not affect editing or saving prj.conf , so you can proceed as is. To give the board a fixed IP address, rewrite the line with CONFIG_NET_CONFIG_MY_IPV4_ADDR . Look below # Network address config in the middle of the file. In the original sample, it looks like this: CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1" CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2" CONFIG_NET_CONFIG_MY_IPV4_GW="192.0.2.2" Replace the value enclosed in " on the first line (line 54 of the file) with the following. In VS Code, the line number is displayed on the left edge (Figure 4.1-2). Depending on the Zephyr version, the line number may be slightly off. CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.168.1.100" CONFIG_NET_CONFIG_MY_IPV4_GW on the third line is the address of the Gateway (router). In this configuration, where the PC and the board are directly connected, it is not used, so you do not need to change it. If you do change it, change it to "192.168.1.1" (see the explanation at the end of this section). CONFIG_NET_CONFIG_PEER_IPV4_ADDR on the second line is similar but a different setting, so do not change it this time. Note: Do not paste this line into PowerShell. The line marked CONFIG_... above is not a command, but the content of the prj.conf file opened in VS Code. If you paste it into PowerShell, you will get an error saying "Not recognized" (it doesn't affect the file, so it's okay if you paste it by mistake). Please rewrite the original line. If you add a line to the end while leaving the original line intact, the same setting will be written twice, making it difficult to determine which one is valid. If you also need to rewrite the Gateway: The original value for both the PEER_IPV4_ADDR row and the MY_IPV4_GW row is "192.0.2.2" . Searching by value can easily lead to overwriting PEER row, so please check CONFIG_... name at the beginning of the row. Do not remove " (double quotes) surrounding the value. After editing, save with Ctrl + S . While ● is displayed next to the filename in the VSCode tab, it hasn't been saved yet. If you forget to save, the build will proceed with the original IP address. Figure 4.1-2 shows the result after rewriting and saving the MY_IPV4_ADDR row (the MY_IPV4_GW row remains unchanged). Figure 4.1-2: prj.conf has been rewritten to MY_IPV4_ADDR and saved. The IP addresses of the PCs at the venue have already been configured to fall within the same subnet 192.168.1.0/24 as this board. You do not need to change any settings on your PCs. Step 3: Build and flash prj.conf After saving, return to the PowerShell window and build. Be careful not to paste commands directly into the VSCode window. # コマンド例4.1-3 west build -p auto -b frdm_mcxn947//cpu0 . I will write the data once the build is successful. # コマンド例4.1-4 west flash During the writing process, you may see a message like INFO: MCU-Link firmware update 'check': ... is running a firmware version which is older than the included firmware version . This indicates that the MCU-Link firmware on the board is older than the one included with the LinkServer. The writing process will proceed as is, so please do not worry and continue. Step 4: Connect the Ethernet cable and check for a response. Once the programming is complete, connect the PC and the board directly with an Ethernet cable. Insert the cable into the board's Ethernet connector (⑤ in Figure 1.3-1 ) (Figure 4.1-3). Figure 4.1-3: Insert the Ethernet cable into the Ethernet connector on the board. It will take a few seconds for the board to gain network access. Please wait a moment, then run the following command in PowerShell. # コマンド例4.1-5 ping 192.168.1.100 It will be displayed as 192.168.1.100 からの応答 as shown below (Figure 4.1-4). 192.168.1.100 に ping を送信しています 32 バイトのデータ: 192.168.1.100 からの応答: バイト数 =32 時間 <1ms TTL=64 192.168.1.100 からの応答: バイト数 =32 時間 <1ms TTL=64 192.168.1.100 からの応答: バイト数 =32 時間 <1ms TTL=64 192.168.1.100 からの応答: バイト数 =32 時間 <1ms TTL=64 192.168.1.100 の ping 統計: パケット数: 送信 = 4、受信 = 4、損失 = 0 (0% の損失)、 ラウンド トリップの概算時間 (ミリ秒): 最小 = 0ms、最大 = 0ms、平均 = 0ms Figure 4.1-4: Execution result of ping ✔If you've reached this point, you're good to go: 192.168.1.100 からの応答 was displayed four times. Communication is working between the PC and the board. When things don't go well If you see the message "Request timed out," check the following in order: Ensure the Ethernet cable is securely plugged into both the PC and the board. Also, check if the LED on the board's connector is lit. prj.conf Check if the IP address is set to 192.168.1.100 and saved correctly . If you have saved it again, rebuild and write the file again starting from command example 4.1-3. Check if the board's startup message or IP address is displayed on the serial console (Tera Term). If nothing is displayed, try pressing the RESET button. If that still doesn't work, please refer to A.6 . If you see the message "Destination host unreachable," it's possible that the PC's IP address is not set to 192.168.1.x ( A.6 ). Explanation: Why use plain HTTP? Plain HTTP is HTTP communication where the content of the communication is not encrypted. In practical use, it is recommended to use HTTPS, which is encrypted with TLS, to prevent eavesdropping and tampering. To use HTTPS, you need to prepare authentication-related files such as server certificates and private keys in advance. Zephyr also supports these authentication mechanisms and communication encryption mechanisms. However, in this hands-on session, to avoid preparing authentication files within the limited time, and to focus on experiencing the HTTP Server mechanism itself, we will deliberately use plain HTTP. Explanation: About Gateway Settings The Gateway ( CONFIG_NET_CONFIG_MY_IPV4_GW ) is not strictly necessary for communication that does not go through a router. In this case, the PC and the board are directly connected with an Ethernet cable, and both are placed on the same subnet 192.168.1.0/24 . All communication is resolved by ARP within the same link, and routing through the Gateway does not occur. Therefore, in this case, it will work the same whether you leave the original value or change it to "192.168.1.1" . If you connect the board to a router to communicate with another network, you will need to rewrite the router's address. 4.2 Trying to control an LED from a browser In this section, you can turn the board's LEDs on and off from your browser. Note: Enter the address starting with http:// Please enter the address in your browser's address bar as http://192.168.1.100/ , without omitting http:// . Modern browsers automatically attempt to connect using encrypted communication https:// if you omit http:// or select an older history entry. This board runs on plain HTTP, so you cannot connect using https:// . If you see a message like "Your connection is not private" or "This site does not support secure connections," please check if the address bar is set to https:// . Please note that when the page loads, "Not secure" will be displayed to the left of the address bar. This is because you are connecting via plain HTTP without encryption, and this is normal in this case. Accessing http://192.168.1.100/ in your browser will display a page for controlling the LEDs (Figure 4.2-1). Figure 4.2-1: LED control page opened in a browser In addition to "LED Control," this page also displays "Uptime Counter" (the elapsed time since the board was started, the value of /uptime as seen in Section 4.3) and "Network Statistics" (statistics such as the amount of data sent and received by the board). Pressing the "LED on" or "LED off" buttons on the page will turn the RGB LED on the board (③ in Figure 1.3-1 ) green on/off (Figure 4.2-2). The same RGB LED will be used in the LED blinking example in Section 2.3. The LED was blinking red before, but this time it's green. Figure 4.2-2: RGB LED lights up green when the browser is operated. ✔This is the correct result: The RGB LED turns green on/off when you press the button. At this time, lines like [00:00:25.684,000] ... are displayed one after another in Tera Term, and uart:~$ is also displayed. This is not an abnormality, as it is the log output by the HTTP Server sample and the display of the Zephyr shell (which accepts command input). When things don't go well Ping works but the page doesn't display in the browser → Check if the address is http:// . If a proxy is set up on your company PC, you need to configure it to bypass the proxy ( A.6 ). The page displays but the LED doesn't react → It should light up green, not red, so please check the color. If it still doesn't react, refresh your browser ( F5 ) and try pressing the button again. Column: On the "heaviness" of network functions In typical embedded development, even running a single HTTP server often requires a lot of preparation. This includes tasks such as embedding and configuring the TCP/IP stack (e.g., lwIP), preparing a socket API, selecting and implementing an HTTP parser, and implementing a method for serving static files. In this case, all of those options are simply changed from prj.conf to CONFIG_NET_* and CONFIG_HTTP_* to y . The only things written on the application side are the handler functions for each endpoint (functions that are called when a request arrives). The network stack and HTTP server can be handled consistently with a single build system and Kconfig. This is one of the reasons why Zephyr, despite being an RTOS for microcontrollers, is often discussed in the context of its relatively large amount of middleware. Column: Is Zephyr's TCP/IP stack lwIP? When it comes to TCP/IP stacks for embedded systems, many people might think of lwIP (lightweight IP). However, Zephyr doesn't import lwIP from an external source; instead, it uses its own proprietary TCP/IP stack implemented under subsys/net/ as standard. Features such as IPv4/IPv6, TCP/UDP, and socket APIs are built into the Zephyr core. prj.conf The options CONFIG_NET_IPV4 , CONFIG_NET_TCP , and CONFIG_NET_SOCKETS that you enable also refer to the functionality of this stack. (Zephyr also supports configurations that combine external network stacks as modules, but this article uses the standard stack.) 4.3 Take a look at the /uptime endpoint In this section, we will examine the behavior of the simplest endpoint. Accessing http://192.168.1.100/uptime in your browser will display the elapsed time since the board was launched. ✔If you've reached this point, you're good to go: The browser should now display the elapsed time since the board was launched. We will apply this endpoint structure, which "calculates a value in response to a GET request and returns it as text," directly to the next chapter, Chapter 5. Explanation: HTTP Requests and Responses When you access /uptime in your browser, a GET request is sent from the browser to the board, meaning "Please provide the contents of this path." On the board side, the HTTP Server that receives the request calls the handler function associated with /uptime (the function that is called when a request arrives; in this case, uptime_handler ). The handler calculates the elapsed time since the program was launched and sets that value as the response body. The HTTP server adds a status line (such as 200 OK ) and headers to this body and returns it to the browser as an HTTP response. The browser displays the body of the received response on the screen. The /temperature endpoint, which will be added in Chapter 5, operates in exactly the same way: it receives a request, calculates a value, and returns it as the message body. ▲ Return to Table of Contents 5. Modify the HTTP Server to view the temperature sensor values in a browser. 5.1 What was added? This section explains what has been added to the HTTP Server sample in Chapter 4. We've added two more endpoints that return temperature, using the same structure as the /uptime endpoint seen in Section 4.3. The changes are only minor parts of src/main.c . endpoint Handler function things to return /temperature temperature_handler HTML page displaying temperature /temperature.json temperature_json_handler Only the temperature value at that time (JSON) The page that opens in your browser is page /temperature . This page has a small piece of JavaScript embedded in it that queries /temperature.json for a value every second and updates the display (Figure 5.1-1). This is the same mechanism that automatically updates the value of /uptime on the top page. static int temperature_handler(struct http_client_ctx *client, enum http_transaction_status status, const struct http_request_ctx *request_ctx, struct http_response_ctx *response_ctx, void *user_data) { /* 3章の温度センサーと同じ sensor_sample_fetch_chan / sensor_channel_get の流れで気温を取得し,HTMLとして返す */ ... } static int temperature_json_handler(struct http_client_ctx *client, enum http_transaction_status status, const struct http_request_ctx *request_ctx, struct http_response_ctx *response_ctx, void *user_data) { /* 同じ方法で気温を取得し,数値だけを返す */ ... } (excerpt) I only added the following two things. The other parts (LED control, /uptime , static page delivery) remain unchanged from the original sample. These two handler functions (the sensor reading part is combined into a common function) Each is associated with the paths /temperature and /temperature.json , in the two lines HTTP_RESOURCE_DEFINE() Figure 5.1-1: Flowchart showing how page /temperature displays the temperature and updates every second. 5.2 Let's actually view it in a browser In this section, we will write the modified code and display the temperature in the browser. The modified code is available in the GitHub repository teddokano/handson_http_server_temp . Clone it to your working folder and build and flash it using the same procedure as before. # コマンド例5.2-1 cd ~\handson git clone https://github.com/teddokano/handson_http_server_temp cd handson_http_server_temp [Confirm whether the venue PC has been cloned (i.e., whether internet access is available at the venue). If it has been cloned, only cd and above will be shown.] prj.conf in this repository is already configured so that the board's IP address is the same as 192.168.1.100 in Chapter 4. There is no need to rewrite prj.conf . # コマンド例5.2-2 west build -p auto -b frdm_mcxn947//cpu0 . The program will be written once the build is successful. You can leave the Ethernet cable connected as it was in Chapter 4. # コマンド例5.2-3 west flash After writing the data, wait a few seconds, then access http://192.168.1.100/temperature in your browser, and the following page will be displayed (Figure 5.2-1). Hello FRDM-MCXN947 temp sensor: P3T1755 Temperature: 24.312°C ← Back The temperature value is automatically updated every second. Try touching the temperature sensor (④ in Figure 1.3-1 ) with your finger and see the value increase. If you open http://192.168.1.100/temperature.json directly, you can also see the numerical response (a value like 24.312 ) that is being queried on the back of the page. Figure 5.2-1: Temperature displayed in the browser ✔If you can do this, you're good to go: The temperature is displayed in the browser, and the value increases when you touch the sensor with your finger. When things don't go well git clone If you get the message "already exists and is not an empty directory" → It has already been cloned. Just proceed by executing cd handson_http_server_temp . git clone If you see a message like "Could not resolve host," it means your PC is not connected to the internet. Please connect to a network that can access the internet, such as Wi-Fi, and try again. /temperature Opening it results in "404 Not Found" → The original sample from Chapter 4 is still written there. Check that the prompt is ...\handson_http_server_temp> and start again from command example 5.2-2. If the page won't open, follow the same steps as in Sections 4.1 and 4.2, "When it doesn't work." 5.3 How are the contents of Chapters 3 and 4 combined? The application created in this chapter does not use any new mechanisms. It is simply a combination of the components that were operated one by one in chapters 2 through 4. A table summarizing which component was used and where it was used is provided. Things used in Chapter 5 Where in Chapter 5 is it? Where did you see it? Obtain temperature sensor data using an alias. main.c of DEVICE_DT_GET(DT_ALIAS(ambient_temp0)) Section 3.3 (same line as thermometer) Add a temperature sensor to the device tree. boards/frdm_mcxn947_mcxn947_cpu0.overlay p3t1755 node Section 3.3 (Same overlay as thermometer) Enable the sensor function prj.conf of CONFIG_SENSOR=y Figure 3.3-1 in Section 3.3 Read the temperature sensor_sample_fetch_chan() ・ sensor_channel_get() Chapter 3 (thermometer) Link the path and the handler function. HTTP_RESOURCE_DEFINE() Register /temperature and /temperature.json Section 4.3 (Same notation as /uptime ) Calculate the value and return it in the response. temperature_handler ・ temperature_json_handler Explanation of Section 4.3 (Same flow as uptime_handler ) Network and HTTP Server Functionality prj.conf of CONFIG_NET_* ・ CONFIG_HTTP_* Chapter 4 (as in the original sample) By combining the components that were operated individually, we get an application that allows you to "view sensor values from a browser." The only new code we wrote was two handler functions and two lines of code to register them. Explanation: Why main.c and CMakeLists.txt look a bit complicated src/main.c When you open it, you'll find quite a few lines of code besides the /temperature handler. This is because the HTTP Server sample also provides the original endpoints such as the HTML and JavaScript for the top page, and the LED control /uptime . These are the "foundation for running as an HTTP server" and are not directly related to adding the temperature sensor, so for now, it's sufficient to focus only on the two handler functions in Section 5.1. The mechanism of this foundation is explained in Appendix G. ▲ Return to Table of Contents 6. Summary 6.1 Things I experienced today I got a feel for the Zephyr development environment (west, device tree, Kconfig). I ran an LED blinking program, a temperature sensor reading program, and an HTTP server. I combined two samples to create an application that allows you to view sensor values in a browser. 6.2 Resources for trying it again yourself Zephyr Project Official Documentation The repository for the sample code used this time is (teddokano/handson_http_server_temp) This blog post For more helpful information, see → NXP's Zephyr OS - Summary Page (Japanese Blog) 6.3 How can AI be utilized? When reading or modifying Zephyr's sample code, you can ask the AI assistant, "How can I add this functionality to this sample?" and it will suggest modifications that follow patterns in existing code. /temperature endpoint in this example was added with the same structure as the existing /uptime endpoint. If you encounter any parts you don't understand, we recommend first finding a similar section in the existing sample code and trying to decipher it together with the AI. Another common issue is when an error occurs with west build . During a Zephyr build, a large number of messages appear at once, ranging from Kconfig warnings to CMake errors and linker errors. Initially, it can be difficult to pinpoint the real cause of the problem. In such cases, it's recommended to start by pasting the entire error message into the AI and asking, "What kind of error is this?" The link error around sections-rom.ld mentioned in Appendix G was also identified with the help of the AI during the process of writing this article. The information you should provide when asking a question is summarized in Section 0.4 , "When Asking a Question." The same applies when consulting with an AI. 6.4 Cleaning up Those who attended the event Please leave the boards and cables as they are. There is no need to put away your PC. Please follow the instructions of the venue staff. Those who have tried it on their own PC You can close PowerShell (or Terminal) as is. The virtual environment activation and ZEPHYR_BASE settings will disappear when you close the window. If you want to exit the virtual environment without closing the window, execute deactivate . If you set your PC's Ethernet to a static IP address in Chapter 4, you should revert it back to its original setting. On the same screen as in column A.6 , change "IP assignment" back to "Automatic (DHCP)". If you don't, you won't be able to connect to the internet when you connect that Ethernet port to your company LAN or home router. The board retains the last program written to it (the HTTP Server with temperature display from Chapter 5). This program runs every time the power is turned on, but it can be overwritten with a different program at any time using west flash . Keep the working folder ~\handson so you can continue where you left off today. You can delete the entire folder when you no longer need it (but do not delete ~\zephyrproject ). ▲ Return to Table of Contents Appendix A. When things don't work This appendix summarizes common pitfalls when copying and pasting commands, and how to resolve them. Refer to the "What to do if it doesn't work" sections in each part of the main text. A.1 First, check the following If you encounter an error, please check the following three points before trying to find the cause. Most problems can be found by checking one of these. If the prompt does not start with (.venv) or , the virtual environment is not enabled. This can happen if you have reopened PowerShell or are working in another window. Re-execute command example 2.1-1, or on the venue PC, execute zinit <作業中のサンプル名> ( command example 2.1-2 ). Check if you are in the correct folder . The part of the prompt that looks like PS C:\Users\<ユーザー名>\handson\thermometer> should be the folder of the sample in the section you are currently working on. If not, re-execute cd in the first command example in that section. Which error appeared first? When an error occurs, a large number of messages will appear afterward. Scroll up the screen and find the line where the first red text or error appeared. If you pasted multiple lines together, check which command caused the first error. You can check the current status all at once with the following command. # コマンド例A.1-1 Get-Location $env:ZEPHYR_BASE west topdir If the first line displays your current folder, the second line displays C:\Users\<ユーザー名>\zephyrproject\zephyr , and the third line displays C:/Users/<ユーザー名>/zephyrproject , then your environment is set up correctly. If the second line is empty, run command example 2.1-1 again. A.2 PowerShell Westbound Symptoms cause Recovery method west : 用語 'west' は,コマンドレット,関数,スクリプト ファイル,または操作可能なプログラムの名前として認識されません The virtual environment is not enabled. zinit <作業中のサンプル名> Execute the following (If your PC does not have zinit , execute command example 2.1-1 and return to the current working folder with cd ). west: unknown command "build" ZEPHYR_BASE This setting is not configured (west cannot find the workspace). zinit <作業中のサンプル名> Execute the following (If your PC does not have zinit , execute command example 2.1-1 and return to the current working folder with cd ). Activate.ps1 And "Script execution is disabled on this system." PowerShell execution policy Set-ExecutionPolicy -Scope CurrentUser RemoteSigned After executing this, repeat command example 2.1-1. cd The error message is "Cannot be detected because the path does not exist" (e.g., Copy-Item . Incorrect path entry, or missing source and destination folders. Copy the command again instead of typing it manually. If ~\handson is not present, execute the first line of command example 2.2-1. I pasted the command, but nothing happened, and the command remained after the prompt. The newline character at the end of the line was not copied. Enter Press After pasting, a prompt saying >> appeared and the program stopped. " Commands are cut off midway, such as forgetting to close a window. Ctrl Interrupt with + C and copy the entire command again. ping The error is not in the result of west , but in the Japanese text. I accidentally pasted the entire article text and even the contents of prj.conf This only copies the contents of the code block. Since it's executed within PowerShell, it doesn't affect any files. A.3 Build errors Symptoms cause Recovery method Invalid BOARD , No board named ... -b Typo in the board name Copy the command example again, including // in frdm_mcxn947//cpu0 exactly. source directory ... does not contain a CMakeLists.txt etc. Built in a folder other than the sample folder (e.g., ~\handson or ~\zephyrproject ). Execute the command cd in the example command for that section, then rebuild. Build directory ... is for application ... It is said to be the build folder of another app. The build folder from the previous build remains. -p auto Change -p always and rebuild (see below). The settings I changed aren't being reflected, and I keep getting unexplained errors. Old information remains in the build folder. -p always Then perform a clean build (see below). prj.conf After rewriting it, a Kconfig error occurs. I accidentally deleted " and = when rewriting. prj.conf Review it. If you get confused, copy the sample again using the procedure in A.7. A clean build is performed using the following command. It will delete the build folder and recreate it from scratch, so it will take time but is reliable. # コマンド例A.3-1 west build -p always -b frdm_mcxn947//cpu0 . If the error message is too long to read on the screen, you can save it to a file as follows. This is useful for opening the saved file build_log.txt in VS Code and searching for error , or for pasting the content into an AI for consultation. # コマンド例A.3-2 west build -p auto -b frdm_mcxn947//cpu0 . 2>&1 | Out-File build_log.txt code build_log.txt When the build is slow (if you are trying it on your own PC) Building Zephyr on Windows tends to be slower than building it on Linux on the same PC. For larger samples like http_server, a single build can take several minutes. The following three steps will significantly improve performance (the venue PCs are already configured). Install ccache : This tool remembers the results of a compilation and reuses them for subsequent builds. Once installed, Zephyr will use it automatically. When rebuilding the same sample in the same folder, most of the compilation is skipped, making it faster (the first build will still take the usual time). In my environment, a clean build of http_server went from about -p always minutes to 73 seconds. Windows Defender Exclusion Settings : Open "Windows Security" → "Virus & threat protection" → "Manage settings" → "Exclusions". Add ~\zephyrproject , working folder, Zephyr SDK folder, and ccache cache folder (usually %LOCALAPPDATA%\ccache ) to the exclusions. If you are using antivirus software other than Defender, exclude them in its settings. This will prevent a large number of files from being scanned with each build. If you are using a company PC, please check the IT department's policy. Power settings : Connect the AC adapter to your laptop and set the power mode to "Optimal Performance". # コマンド例A.3-3 winget install --id Ccache.Ccache -e After installation, reopen PowerShell, configure the environment using zinit (or command example 2.1-1), and then rebuild using -p always . This is because the build folder created before installing ccache still has the settings configured not to use ccache. If you are trying it on your own PC If your Windows username contains Japanese characters or spaces, the build tool may not be able to handle the path correctly in some environments, resulting in an error. In that case, try using a short path with only alphanumeric characters, such as C:\handson instead of ~\handson , for your working folder. A.4 Writing (west flash) error Symptoms cause Recovery method No probe found Errors such as "debug probe not found" The USB cable is not plugged into the board's debug connector (MCU-Link side), or a USB cable designed for charging only is being used. Check the connector you're plugging in, unplug it, plug it back in, and try again. Use a data communication compatible cable. The probe is in use and cannot connect. Debugging with another tool, such as MCUXpresso for VSC. End the debugging session for the other tool and then run it again. west flash It tries to restart the build and gets an error. The previous build failed. west flash Instead, resolve build errors first ( A.3 ) The writing process stopped midway, the board is unresponsive. Temporary communication error Unplug the USB cable, wait a few seconds, then plug it back in and run west flash again. west flash You can run this command multiple times without any problems. If it fails, fix the cause and then run the same command again. A.5 Nothing appears on the serial console. Symptoms cause Recovery method In Tera Term's "New Connection" menu, I can't select "Serial," and the board's port doesn't appear. The USB cable is not plugged in, or it is a charging-only cable. Check the cable, then reopen the screen by going to "File" → "New Connection" (the port list will be updated when you reopen it). Unable to connect due to "Port in use" or "Serial port not open". Another window, such as Tera Term or the VS Code serial monitor, is using the same port. Close any other windowed applications or applications that are connected, then reconnect. The whole thing becomes a meaningless string of symbols. The baud rates are different. Go to "Settings" → "Serial Port" and change it back to 115200. Nothing is displayed even though it's connected. I missed the timing for the output. Press the RESET button on the board. The display stopped working after I unplugged and replugged the USB cable. The Tera Term connection was lost when I plugged it back in and out. After going to "File" → "Disconnect," select "File" → "New Connection" and choose the port again. Note that the COM port number may have changed. ° Symbols such as these may appear as garbled characters. The received character encoding is not UTF-8. As per step 4 of Tera Term in Section 2.2 , set the receiving character encoding to UTF-8 A.6 Network and Browser-Related Features You can check your PC's IP address by running the following command in PowerShell. # コマンド例A.6-1 ipconfig Please check the "IPv4 Address" in the "Ethernet Adapter" section. Symptoms cause Recovery method The IPv4 address is 169.254.x.x The PC's Ethernet is not configured with a static IP address. Set the PC side to 192.168.1.10 / 255.255.255.0 etc. (see below) The IPv4 address is 192.168.1.100 The IP addresses of the PC and the board are overlapping. Set the PC side to something other than 192.168.1.100 (e.g., 192.168.1.10 ). The PC's Wi-Fi is also connected to network 192.168.1.x The communication is routed to the Wi-Fi network and doesn't reach the board. Disconnect Wi-Fi only during the practical training. Ping works, but it won't open in the browser. https:// You are trying to connect via a proxy, or through a proxy. Re-enter the address as http://192.168.1.100/ . If you are using a proxy, add 192.168.1.100 to the proxy exceptions. prj.conf I fixed it, but my IP address hasn't changed. Not saved, or not rebuilt. Save in VS Code with Ctrl + S and start again from command example 4.1-3. If you are trying this on your own PC: Set a static IP address on your PC. Open Windows "Settings" → "Network & Internet" → "Ethernet," and select "Manual" from "Edit" under "IP Assignment." Turn on IPv4, enter 192.168.1.10 for the IP address and 255.255.255.0 for the subnet mask, and save (you can leave the gateway field blank). For more details, please refer to the HTTP Server blog post introduced at the beginning of Chapter 4. Figure A.6-1: Screenshot of setting a static IP address for Ethernet in Windows "Settings". A.7 Reset procedure when the problem still cannot be fixed When you're unsure what's causing the problem, the quickest solution is to start the section over from the beginning. Even if you delete the sample you copied to your working folder, the original sample in ~\zephyrproject remains, so you can try again as many times as you like. Close the PowerShell window and open a new one. Execute command example 2.1-1 ( zinit on the venue PC) to reconfigure the environment. Delete the sample folder for the section you want to redo. For example, to redo Chapter 4, do the following: # コマンド例A.7-1 cd ~\handson Remove-Item -Recurse -Force ~\handson\http_server Replace the folder name ( http_server ) with hello_world , blinky , thermometer , or handson_http_server_temp depending on the section you are redoing. Be careful not to delete ~\handson or ~\zephyrproject themselves. Let's start again from the first command example in that section (copy and cd ). If the board is not working properly, unplug both the USB cable and the Ethernet cable, wait a few seconds, plug them back in, and then try west flash again. ▲ Return to Table of Contents Change history: [Published date]: First edition ========================= We are currently unable to respond to comments left in the "Comment" section of this post. We apologize for the inconvenience, but please refer to "Technical Questions to NXP - How to Contact Us (Japanese Blog)" when making inquiries. (If you are already an NXP distributor or have a business relationship with NXP, you may contact your representative directly.) This article was written as a progress guide for the hands-on seminar " Zephyr Debut Session ." In the actual seminar, we will follow the steps outlined in this article. Even if you didn't attend the seminar, you can reproduce the same content in your own environment by reading this article alone. To that end, I've included command examples and verification points without omitting any. Think of this not as a general explanatory article, but rather as a practical guide to get you to the point where it actually works. (Estimated time: 90 minutes) *Assumes Zephyr OS is installed. Japanese Blog Introduction Sensor Ethernet Interface MCX
記事全体を表示
Zephyr 首秀 ~90 分钟内让 FRDM-MCXN947 顺利运行~ [主打](日文博客) 本文分为两部分:正文(第0-6章和附录A)和附录部分(附录B-G) 。附录部分包括命令示例列表(PowerShell/bash/zsh)、提前完成作业的学生的练习、如何在其他FRDM板上进行测试的说明、术语表以及常见问题解答。 目录 0. 这篇文章是关于什么的? 0.1 目标受众 0.2 本文的目标 0.3 可用物品 0.4 流程及预计所需时间 0.5 如何阅读这篇文章 1. Zephyr是什么? 1.1 Zephyr 的概述和特点 1.2 为什么现在选择 Zephyr? 1.3 关于 FRDM-MCXN947 2. 试用开发环境。 2.1 检查环境 2.2 让我们运行 Hello World 示例。 2.3 编写闪烁示例(LED 闪烁) 2.4 让我们来看一下示例代码。 3. 尝试操作温度传感器。 3.1 构建并烧录样机 3.2 检查值 3.3 它为什么会移动? 4. 尝试运行 HTTP 服务器。 4.1 构建并烧录样机 4.2 尝试通过浏览器控制 LED 4.3 查看 /uptime 端点 5. 修改 HTTP 服务器,以便在浏览器中查看温度传感器值。 5.1 新增了哪些内容? 5.2 让我们实际在浏览器中查看一下 5.3 第 3 章和第 4 章的内容是如何结合起来的? 6. 总结 6.1 我今天经历的事 6.2 可供您再次尝试的资源 6.3 如何利用人工智能? 6.4 清理工作 附录A:当事情进展不顺利时 A.1 首先,检查以下内容 A.2 PowerShell 西行 A.3 构建错误 A.4 书写(西闪)错误 A.5 串口控制台上没有任何显示。 A.6 网络和浏览器相关功能 A.7 当问题仍然无法解决时,应执行重置程序 附录(单独文章): 附录 B. 命令示例列表(PowerShell/bash/zsh) 附录C. 让我们来试试(供提前完成的人参考) 附录 D. 尝试使用其他 FRDM 板 附录 E. 词汇表 附录 F. 常见问题解答 附录 G。HTTP 服务器示例框架 0. 这篇文章是关于什么的? 本文是为“ Zephyr 入门课程”实践研讨会编写的进度指南。在实际研讨会中,我们将按照本文概述的步骤进行操作。 即使您没有参加研讨会,也可以仅通过阅读本文在自己的环境中复现相同的内容。为此,我包含了命令示例和验证要点,没有任何遗漏。请将本文视为一份实用指南,而非一篇泛泛的解释性文章,它能帮助您最终实现目标。 0.1 目标受众 本文面向对 Zephyr OS 略有耳闻但从未实际使用过的初学者,或希望尝试 Zephyr 的用户。无需任何嵌入式开发经验,只需具备一定的 C 语言代码阅读能力即可。 0.2 本文的目标 使用 NXP FRDM-MCXN947 开发板,您将获得从控制 LED 闪烁、读取温度传感器读数到通过浏览器显示传感器数值等各种实践经验。目标是让您掌握“在 Zephyr 上轻松运行应用程序”的成功经验。本文将尽量减少复杂的设置和编码工作。 本文将特意使用命令行进行操作。虽然有一些便捷的工具,例如 NXP 的 MCUXpresso for VS Code,允许您通过按钮操作进行构建和烧录,但我们将直接使用 Zephyr 默认提供的 west 命令。这样做是为了让您体验到 Zephyr 的原始、中立,不依赖于任何特定厂商的工具。本文所述的步骤也适用于其他厂商的开发板,只需更改开发板名称即可。 今天的课程安排如图 0.2-1 所示。在第 2 章中,你将熟悉开发流程;在第 3 章和第 4 章中,你将运行不同的功能;在第 5 章中,你将把两者结合起来。 图 0.2-1:今日日程安排 0.3 可用物品 硬件 FRDM-MCXN947 评估板 PC(已安装以下软件,例如 Zephyr 开发环境) 温度传感器:P3T1755,兼容I3C/I²C总线,精度±0.5°C的数字温度传感器(安装在 FRDM-MCXN947 评估板上) 以太网线 软件 Zephyr 开发环境(包括 west(用于构建和烧录 Zephyr 的命令)、Zephyr SDK(包含编译器在内的一整套工具)和 Python 虚拟环境;根据官方入门指南的说明在 ~/zephyrproject 中设置) LinkServer (NXP 的编程和调试工具。 west flash 用于对 FRDM-MCXN947 进行编程。) Microsoft Visual Studio Code(用于查看和编辑源代码和 prj.conf (应用程序特定的配置文件)) 终端软件(本文以 Windows 版Tera Term 为例进行说明。如果您在 macOS 或 Linux 系统上尝试此操作,请使用可以连接到串口的软件,例如 CoolTerm。) 浏览器 Git(我们将在第五章中使用它来获取示例代码) VSC 的 MCUXpresso(非必需。*它已安装在场地电脑上,但本文所述步骤中不会用到。) *场馆内的电脑已经设置好了所有这些功能。 对于那些想在自己的电脑上重现此问题的人来说 要复现与场地电脑相同的设置,您需要安装上面列出的软件。特别是LinkServer ,它经常被忽略。如果它没有安装(或不在您的 PATH 环境变量中),您可能可以构建项目,但无法写入(会显示 west flash )。此外,第 4 章使用了电脑的以太网端口,因此如果您的笔记本电脑没有以太网端口,请准备一个 USB 转以太网适配器。 请参考以下安装说明之一。 官方指南:入门指南(英文)。操作步骤与本文的前提相同(在 ~/zephyrproject 中配置创建工作区和 Python 虚拟环境 .venv )。LinkServer 需要单独安装。 使用 NXP 工具的简易方法: [Zephyr ®系列] 第二部分:首次构建及在实际设备上的操作(NXP 技术博客,日语) 。您可以使用 MCUXpresso for VS Code 和 MCUXpresso 安装程序同时安装 Zephyr 开发环境和 LinkServer。但是,Python 虚拟环境(在 Windows 系统中位于 C:\Users\<ユーザー名>\.mcuxpressotools\ 下)和 Zephyr 文件夹的位置可能与本文所述有所不同。请根据您的实际环境调整命令示例 2.1-1 等中的文件夹位置。 0.4 流程及预计所需时间 研讨会时长为90分钟。大致日程安排如下: 章 内容 指导方针 第0-1章 本文介绍一下,Zephyr是什么? 10分钟 第二章 检查开发环境,显示“Hello World”,LED 灯闪烁。 25分钟 第三章 移动温度传感器 15分钟 第四章 运行 HTTP 服务器 20分钟 第五章 在 HTTP 服务器上显示温度 15分钟 第六章 总结 5分钟 [根据实际时间表进行调整] 如果我迟到了 第 3 章至第 5 章彼此独立。即使您尚未完成前一章,也可以通过在新 PowerShell 窗口中运行命令示例 2.1-1(在会议电脑上为 zinit )从任意章节开始。例如,如果您在第 3 章遇到困难且时间不够,可以暂时搁置第 3 章,直接从第 4 章的第一个命令示例开始。您可以稍后参考本文,并按照自己的节奏继续学习。 如果它很快结束 附录 C,“让我们试一试”,其中包含一个练习,您将稍微修改示例代码并进行尝试。 提问时 如果您遇到任何问题,请不要犹豫举手。当您向场馆工作人员、AI或其他相关人员咨询本文内容时,提供以下三点信息将有助于我们快速找到问题所在。 是哪个命令示例导致代码块停止运行? (该数字,例如 # コマンド例3.1-2 ,位于代码块的第一行。) 包含第一个错误的行(带有红色文本或 error 行;屏幕截图也可以接受)。 命令示例 A.1-1的执行结果(可以看到当前文件夹和环境设置) 0.5 如何阅读这篇文章 第二章至第五章的各部分按以下顺序排列。请先完成所有步骤,确认已完成至此步骤后再听讲解。 本部分需要完成的操作(1 行) 说明(示例命令。较长的章节将分为“步骤 1”、“步骤 2”等。) 示例显示(成功时的屏幕和显示) ✔如果你能做到这些,就可以开始了(检查要点,确保与示例相符) 当事情进展不顺利时(常见的绊脚石及应对方法) 评论/专栏 文本中的方框有以下几种用途: 盒子 内容 何时阅读 笔记 在进行该手术时,您必须牢记以下事项。 请先阅读此内容再继续。 解释 我刚才执行的操作和代码是什么意思? 操作完成后,指导员会在现场进行讲解。 柱子 背景知识及相关主题 当你有时间的时候,或者稍后 如果你想在自己的电脑上尝试一下。 补充信息,用于在除会场电脑以外的计算机上重现结果。 在您自己的电脑上尝试时 正文中出现的术语列于附录 E 词汇表中。 如果您正在使用 VS Code 阅读本文。 在活动现场,您将在 VS Code(Visual Studio Code)右侧阅读本文。代码块中的命令可以通过拖动选择,使用 Ctrl + C 复制,然后粘贴到 PowerShell 中。 右侧区域(文章预览区域)已被锁定。因此,使用类似 code prj.conf 命令打开的文件将始终在左侧打开。右侧的文章将保持您之前的阅读状态。 如果您关闭了右侧的文章,请按照以下步骤恢复它。 在 VS Code 中使用“文件”→“打开文件...”打开 ~\seminar_text\article_draft.md 。 Ctrl 按 + K 再按 V 将在右侧显示文章预览。 单击右侧的预览选项卡,然后按 Ctrl + Shift + P 打开命令面板,并执行“视图:锁定编辑器组”。 您可以关闭左侧打开的 article_draft.md 标签页(以防止意外覆盖)。 如果您正在浏览器或 NXP 博客上阅读本文,则无需按照这些步骤操作。 ▲ 返回目录 1. Zephyr是什么? 1.1 Zephyr 的概述和特点 Zephyr 是由 Linux 基金会开发的开源实时操作系统 (RTOS)。它面向广泛的硬件,从小型微控制器到资源密集型片上系统 (SoC)。 即使只看恩智浦的产品,其覆盖范围之广也显而易见。从我们将要使用的MCX系列等Cortex-M内核微控制器,到i.MX系列等Cortex-A内核应用处理器,Zephyr操作系统都能完美兼容。无需为每块电路板使用不同的操作系统或构建环境,这无疑是一项显著的实用优势。 简而言之,它的主要特点是允许通过以下两种机制分别管理硬件配置和软件功能。 设备树:一个描述硬件配置的文件,显示“哪些组件连接到哪里”。 Kconfig :一种用于打开或关闭软件功能的机制,指定“要使用哪些功能”。 这种分离使得将同一应用程序的代码部署到多个开发板上变得更加容易。 换句话说,用 C 或其他语言编写的应用程序逻辑无需直接知道“哪个传感器”连接到“哪个总线”。应用程序代码只需使用设备树中分配给设备的名称来访问设备即可。您可以通过查看 2.4 节和 3.3 节中的实际代码来体验这种分离。 1.2 为什么现在选择 Zephyr? 半导体制造商正在扩大对 Zephyr 的支持,包括将其集成到现有产品线中。作为一款不依赖于特定供应商的实时操作系统,Zephyr 目前在业界的影响力正在不断增长。 Zephyr 项目于 2016 年在 Linux 基金会的支持下启动,至今已有约 10 年的历史。它并非一项新兴的、不成熟的技术。 这段时间以来,Zephyr 的发展不仅仅体现在 CPU 架构和外设驱动程序上。网络相关的中间件,例如您将在第 4 章和第 5 章中看到的 TCP/IP 协议栈和 HTTP 服务器,也已发展成为 Zephyr 内核的标准功能。提到微控制器操作系统,人们往往只会想到一个最基本的调度器。而 Zephyr 的优势之一就在于,您可以使用 Kconfig 轻松启用这些更高级别的中间件。 Zephyr 操作系统凭借其强大的安全措施,正受到众多产品的关注。在欧洲,《网络弹性法案》(CRA) 已正式生效,该法案要求联网产品必须具备网络安全措施,并且自 2026 年 9 月起,制造商有义务报告已被利用的漏洞。Zephyr 项目正致力于满足这些要求,为此,项目建立了一套漏洞修复系统并提供长期支持(详见下文)。 归根结底,如果不亲身体验,很难真正理解“它为何如此出色”。在本文中,我们将通过实际使用来探索它的感受。 专栏:Zephyr 的安全方法 公司设立了专门的漏洞应对团队(PSIRT),该团队自 2017 年起已在计算机安全信息局 (CSI) 注册,CSI 负责分配 CVE 编号。其目标是在收到漏洞报告后 30 天内通知制造商并发布修复程序,并在最多 90 天内公开这些修复程序。 产品制造商可以向漏洞警报注册表注册,以接收有关漏洞的信息。 产品的长期支持 (LTS) 版本至少维护五年,包括安全修复。 west spdx 这样就可以创建产品附带软件的列表(SBOM)。 加密功能由 PSA Crypto 提供(以 Mbed TLS 实现)。FRDM-MCXN947 还具有与 TF-M 配合使用的板级规格( frdm_mcxn947/mcxn947/cpu0/ns ),TF-M 使用 TrustZone。 《网络弹性法案》 (CRA) 与 Zephyr 之间的关系概述于 Zephyr 官方文档的“网络弹性法案”页面。请注意,为简化操作,本次实践环节使用未加密的纯 HTTP 协议(详情请参见4.1 节)。 1.3 关于 FRDM-MCXN947 FRDM-MCXN947 是一款评估板,搭载了恩智浦半导体 (NXP) 的 MCX N947 微控制器。它采用双核 Cortex-M33 配置,运行频率为 150MHz。GPIO、I2C、I3C、SPI 和以太网等外设可通过 Arduino 接头和 mikroBus 插座等连接器进行访问。 本文使用了电路板上的以下组件(图 1.3-1)。在下文中,我们将使用这些数字来表示它们的位置,例如图 1.3-1 中的“①”。 图 1.3-1:FRDM-MCXN947 中使用的组件 数字 部分 何时使用 ① 调试 USB 连接器(MCU-Link 侧) 这是与PC的连接接口。它用于编程( west flash )和串口控制台访问。电源也从这里供应。MCU-Link是板上的编程和调试电路。 ② 重置按钮 当您想要从头开始重新启动程序时(例如,当您错过了串口输出时) ③ RGB LED 第 2.3 节:LED 闪烁(红色闪烁),第 4.2 节:通过浏览器控制 LED(绿色亮起) ④ 温度传感器(P3T1755) 第三章,第五章 ⑤ 以太网连接器(RJ45) 在第 4 章和第 5 章中,您将使用以太网线直接连接 PC。 注意:该开发板有多个 USB 接口。连接到除①以外的任何 USB 接口都将导致无法向开发板写入数据或使用串口控制台。 ▲ 返回目录 2. 试用开发环境 2.1 检查环境 在本节中,我们将打开 PowerShell 并验证 Zephyr 命令(west)是否可用。 会场的电脑已按照官方入门指南中的说明,安装了包含 west/Zephyr SDK 的开发环境。您无需自行配置开发环境。MCUXpresso for VSC 也可用,但如 0.2 节所述,本次研讨会将使用命令行操作。 步骤 1:打开 PowerShell 在“开始”菜单中,键入“PowerShell”并启动“Windows PowerShell”(图 2.1-1)。请勿选择“以管理员身份运行”。本研讨会中的步骤不需要管理员权限,而且如果您在命令输入错误时以管理员身份运行,可能会导致整个电脑出现问题。您可以通过窗口选项卡(或标题)开头是否显示“Administrator:”来判断是否以管理员身份运行。 图 2.1-1:从“开始”菜单打开“Windows PowerShell” PowerShell 的初始文件夹(当前目录)取决于其启动方式。如图 2.1-2 所示,即使您并非以管理员身份运行,它也可能显示 PS C:\WINDOWS\system32> 。它可能在您的主文件夹 ( C:\Users\<ユーザー名> ) 中打开,或者在 VSCode 终端中打开,则会在当前选定的文件夹中打开。无论您在哪里打开它,示例命令中的第一个 cd 都会将您带到 Zephyr 工作区 ( ~\zephyrproject ),因此没有问题。 ~ 是代表您的主文件夹的符号。 图 2.1-2:启动 PowerShell(示例以 C:\WINDOWS\system32 打开) 第二步:使西部地区可用。 在使用 west 之前,请激活 Zephyr 的 Python 虚拟环境(一个包含 Zephyr 所有工具的独立 Python 环境),并将 Zephyr 可执行文件的位置设置为环境变量 ZEPHYR_BASE 。复制以下示例命令,将其粘贴到 PowerShell 中并执行。 粘贴时 您可以通过右键单击 PowerShell 窗口或按 Ctrl + V 来粘贴。 如果程序卡在输入的最后一行,请按 Enter 执行。 您也可以直接粘贴第一行 # コマンド例… (它在 PowerShell 中不会执行任何操作)。 本节末尾的“专栏:复制和粘贴命令时的注意事项”总结了其他要点。 # コマンド例2.1-1 cd ~\zephyrproject .\.venv\Scripts\Activate.ps1 $env:ZEPHYR_BASE = "$HOME\zephyrproject\zephyr" west --version west topdir 执行时,版本信息将显示在 west --version 中,工作区位置将显示在 west topdir 中,如下图所示(图 2.1-3)。提示符(等待命令输入的 PS C:\...> 显示)前面将显示 (.venv) 。 (.venv) PS C:\Users\<ユーザー名>\zephyrproject> west --version West version: v1.5.0 (.venv) PS C:\Users\<ユーザー名>\zephyrproject> west topdir C:/Users/<ユーザー名>/zephyrproject west topdir 显示屏将显示 / 而不是 \ 作为文件夹分隔符,但它指向相同的位置。 图 2.1-3:命令示例 2.1-1 的执行结果 ✔如果您已到达此步骤,即可继续:提示将以 (.venv) 开头,并显示您的 West 版本和工作区位置。 (以下输出仅为“执行结果示例”,供您与屏幕截图进行比较;请勿将其复制为命令。数值和版本号会因您的环境而异。) 注意:请勿关闭此 PowerShell 窗口。 启用虚拟环境和设置 ZEPHYR_BASE 仅在当前窗口内有效。请在研讨会期间保持此窗口打开。如果您关闭此窗口,可以按照下文“重新打开 PowerShell 时”中的步骤恢复到之前的状态。 当事情进展不顺利时 .\.venv\Scripts\Activate.ps1 然后,屏幕上会出现一条红色文字消息,提示“此系统上已禁用脚本执行。” → 按照下文末尾的说明配置执行策略后,请从头开始再次运行命令示例 2.1-1。 west 如果显示“未识别”→虚拟环境激活失败。检查提示符开头是否为 (.venv) ( A.2 )。 cd 如果您收到“找不到,因为路径不存在”的消息,则您的工作区可能位于 ~\zephyrproject 以外的位置。如果您在自己的环境中尝试此操作,请替换为工作区位置。 当我重新打开 PowerShell 时 使用新的 PowerShell 重置环境后,返回到您之前工作的示例文件夹。场地内的电脑有一个名为 zinit 的命令,可以一次性完成此操作。在 zinit 之后指定您之前工作的示例名称。例如,如果您正在学习第 3 章,请按如下方式执行: # コマンド例2.1-2 zinit thermometer 输入部分示例名称并按 Tab 键即可自动补全。如果您仅执行 zinit 而不指定示例名称,则会进入与执行命令示例 2.1-1 后相同的状态(即 ~\zephyrproject 中的状态)。 zinit 也可在 VS Code 终端中使用。 zinit 在没有此功能的电脑上,请执行命令示例 2.1-1,然后移动到示例文件夹,如 cd ~\handson\thermometer 所示。执行命令示例 2.1-1 后,您将立即进入 ~\zephyrproject ,因此直接执行 west build 将无法构建示例。 说明:命令示例 2.1-1 中执行的是什么操作? cd ~\zephyrproject — 导航到 Zephyr 工作区(包含 west、Zephyr 核心和各种模块的文件夹)。 .\.venv\Scripts\Activate.ps1 — 此操作会激活为 Zephyr 提供的 Python 虚拟环境。West 本身以及用于构建的各种 Python 工具都将安装在此虚拟环境中。如果激活成功,提示符开头将显示 (.venv) 。 $env:ZEPHYR_BASE = "..." — 将 Zephyr 主文件夹设置为环境变量 ZEPHYR_BASE 。west 将遍历当前目录的上一级文件夹来搜索工作区。在接下来的章节中,我们将在工作区之外的一个工作文件夹 ( ~\handson\... ) 中构建。这样一来,就找不到工作区, west build 会显示为“未知命令”。设置 ZEPHYR_BASE 可以让 west 将其作为线索来查找工作区。 west --version — 检查版本显示,看看 `west` 命令本身是否正常工作。 west topdir — 显示 west 识别的工作区位置。如果在此处遇到错误,请检查 ZEPHYR_BASE 设置。 在讲解 Linux/macOS 的文章中,你经常会看到像 source ~/zephyrproject/.venv/bin/activate 和 source zephyr/zephyr-env.sh 这样的符号。这些符号是 bash/zsh 的,不能直接在 PowerShell 中使用。 zephyr-env.sh 主要功能是设置 ZEPHYR_BASE 。因此,在 PowerShell 中,您可以像上面那样直接使用 $env:ZEPHYR_BASE 进行设置。(Windows 系统也有 zephyr-env.cmd ,但它是用于命令提示符的。即使您从 PowerShell 执行它,您设置的环境变量也不会保留在 PowerShell 中。) 如果在运行 Activate.ps1 时遇到“脚本执行已禁用”的错误消息,则表示 PowerShell 执行策略阻止了该脚本的执行。请运行 Set-ExecutionPolicy -Scope CurrentUser RemoteSigned 一次,然后重试(此设置已在会场计算机上配置)。 栏目:复制和粘贴命令时的注意事项 粘贴多行文本的确认对话框:在 Windows 终端中,粘贴多行文本时,可能会出现一条确认消息,提示“您正在尝试粘贴多行文本”。请选择“按原样粘贴”。 如果在执行过程中出现错误,程序会在该处停止:一次性粘贴多行代码时,即使中间某行出现错误,后续行也会继续执行。如果看到红色错误提示,请不要依赖该行之后的执行结果,而应首先解决出错行的问题。在您熟练掌握之前,建议每次复制并执行一行代码,这样更安全。 标有 # コマンド例… 行是标记:提问或参考附录 A 时,请使用这些数字。 不要复制提示符( PS C:\Users\...> ) :屏幕截图和执行示例中显示的提示符不是命令的一部分。 如果遇到问题,请参阅附录 A “故障排除”。本文中的命令示例也汇总在附录 B“命令示例列表”中。该列表还包含了适用于 macOS 和 Linux 用户的 Bash 和 zsh 版本。 专栏:如何在电脑上使用 zinit zinit 这是一个注册到 PowerShell配置文件中的函数(PowerShell 启动时自动加载的脚本)。您可以在自己的电脑上按如下方式创建相同的内容: 首先,使用以下命令在记事本中打开配置文件(如果文件不存在,请创建该文件)。 # コマンド例2.1-3 if (-not (Test-Path $PROFILE.CurrentUserAllHosts)) { New-Item -Force $PROFILE.CurrentUserAllHosts | Out-Null } notepad $PROFILE.CurrentUserAllHosts 打开记事本,添加以下内容并保存。这不是要粘贴到 PowerShell 中执行的命令,而是要写入您个人资料的内容。 function zinit { param([string]$Sample) Set-Location ~\zephyrproject . .\.venv\Scripts\Activate.ps1 $env:ZEPHYR_BASE = "$HOME\zephyrproject\zephyr" west --version west topdir if ($Sample) { Set-Location ~\handson\$Sample } } 保存后,重新打开 PowerShell 窗口, zinit 即可使用。运行此配置文件还需要本节中解释的执行策略设置 ( RemoteSigned )。请注意,如果在配置文件中输入日语,则需要注意字符编码(Windows PowerShell 无法正确读取没有 BOM 的 UTF-8 文件中的日语字符)。以上内容仅包含字母数字字符,因此您可以直接保存。 如果您想在自己的电脑上尝试:环境设置 这次,场地提供的电脑上会预先配置好环境,但您也可以在自己的电脑上设置相同的环境。安装说明请参见第 0.3 节“对于想要在自己的电脑上重现问题的用户”。 2.2 让我们运行 Hello World 示例。 在本节中,我们将构建 hello_world 样本,将其写入电路板,并验证字符是否显示在串行控制台上。 从现在开始,不要直接编辑 zephyr/samples/ 中的样本,而是在使用前将其复制到您的工作文件夹 ( ~\handson ) 中(图 2.2-1)。保持 zephyr/samples/ 作为原始样本集不变,所有实验和修改都应在您自己的文件夹中进行。 图 2.2-1:将示例复制到您的工作文件夹并使用它。 步骤 1:将示例复制到您的工作文件夹。 hello_world 将示例文件复制到您的工作文件夹,然后进入该文件夹。 # コマンド例2.2-1 New-Item -ItemType Directory -Force ~\handson | Out-Null Copy-Item -Recurse ~\zephyrproject\zephyr\samples\hello_world ~\handson\hello_world cd ~\handson\hello_world 执行后,如果提示符显示 PS C:\Users\<ユーザー名>\handson\hello_world> ,则表示移动成功。为了确保万无一失,如果您执行 ls ,则应该看到 src 、 CMakeLists.txt 、 prj.conf 等依次排列。 步骤 2:构建 注意:不要忘记最后的 . 行尾的 . (空格加句点)表示“在当前目录下构建应用程序”。复制时很容易被截断,所以请在粘贴后仔细检查。 -b 之后的板名,包括 / 和 // ,即使只有一个字符不同也会导致错误。请复制粘贴,不要手动重新输入。 # コマンド例2.2-2 west build -p auto -b frdm_mcxn947/mcxn947/cpu0 . 首次构建可能需要几分钟时间。请耐心等待提示再次出现。 如果构建成功,输出末尾将显示一个表格,显示 FLASH 和 RAM 使用情况。 (前略) [xxx/xxx] Linking C executable zephyr\zephyr.elf Memory region Used Size Region Size %age Used FLASH: xxxxx B 2 MB x.xx% RAM: xxxx B 320 KB x.xx% SRAMX: 0 B 96 KB 0.00% IDT_LIST: 0 B 32 KB 0.00% Generated files: (後略) [已替换为场馆电脑的实际输出结果] ✔如果您已完成以上步骤,即可继续:输出结果末尾将显示一个表格,其中列出了 FLASH 和 RAM 的使用情况。现在,您的程序已准备好写入微控制器。 如果发生错误,将会显示错误信息。Zephyr 的错误信息通常很多,一开始可能难以追踪。诀窍是先查找以 error 开头的行。您应该仔细阅读错误信息,或者使用 AI 来理解它。常见错误及其解决方案总结在A.3 构建错误中。 步骤三:准备写作 写作之前,请准备以下两样东西。 (1)用 USB 电缆连接 PC 和开发板。 使用 USB 数据线(图 2.2-2)将图 1.3-1 中的 ①(调试 USB 接口,MCU-Link 端)连接到您的电脑。板上的 LED 指示灯将亮起,表示电源已接通。请注意,仅用于充电的 USB 数据线不支持数据通信;请使用支持数据通信的数据线。 图 2.2-2:将 USB 电缆连接到图 1.3-1 中的 ①。 (2)保持串口控制台打开。 hello_world 该程序启动后仅输出一次字符串,然后终止。如果在编写程序后再打开串口控制台,将会错过此次输出。因此,请在编写程序前打开串口控制台(在您的电脑上显示开发板输出的窗口,例如 printf 等)并连接。本次我们将使用Tera Term作为终端软件。请按照以下“Tera Term 使用指南”中的步骤 1 至 4 进行连接。 要启动 Tera Term, 请单击桌面上的“Tera Term 5”图标(图 2.2-3)。启动后,单击“Tera”。将打开一个名为“终端:新连接”的屏幕。 选择“串口” 选择串口 ,然后从“端口”列表中选择与 MCU-Link 对应的端口(图 1.3-1 中的 ①),然后按“确定”(图 2.2-4)。 列表中显示的名称类似于 COM3: MCU-Link VCom Port (COM3) ,其中 COM + 数字。该数字因电脑而异。 如果您不确定该选择哪个端口,请拔下并重新插入主板的 USB 电缆,然后选择列表中消失或重新出现的端口(当您重新打开“新连接”屏幕时,列表将更新)。 将波特率(串口通信速度)设置为 115200。 从菜单中选择“设置”→“串口”,将打开“Tera Term:其他设置”屏幕的“串口”选项卡。将“速度”设置为 115200 ,然后按“确定”(图 2.2-5)。(旧版 Tera)在 Term 中,屏幕名称和按钮都不同。 将字符编码设置为 UTF-8 (场馆的电脑不需要进行此设置)。 从菜单中打开“设置”→“其他设置”,然后选择“编码”选项卡(您也可以在步骤 3 的屏幕上切换选项卡)。将接收字符编码设置为 UTF-8 ,然后按“确定”。此设置对于正确显示 °C 中的 ° 至关重要,如第 3 章所示。 图 2.2-3:桌面上的 Tera Term 图标 图 2.2-4:在“新建连接”中选择串口 图 2.2-5:在“设置”→“串口”中将速度设置为 115200。 设置完成后,你可以在后续章节中保持 Tera Term 的打开状态,无需每次写作时都重新连接。 第四步:写 准备就绪后,返回 PowerShell 并输入以下命令: # コマンド例2.2-3 west flash 写入过程完成后,微控制器将复位,写入的程序将被执行。 串口控制台上将显示以下字符串(图 2.2-6): *** Booting Zephyr OS build v4.x.x *** Hello World! frdm_mcxn947/mcxn947/cpu0 第一行是 Zephyr 启动时始终显示的消息, v4.x.x 部分是 Zephyr 版本。第二行是程序 hello_world 输出的字符串。 图 2.2-6:Tera Term 中显示的“Hello World”屏幕截图 ✔如果您能做到这一点,那就万事俱备了: Hello World! frdm_mcxn947/mcxn947/cpu0 应该显示在 Tera Term 中。 当事情进展不顺利时 我忘记在写入前打开串口控制台,错过了输出结果 → 连接 Tera Term 后,按下板上的 RESET 按钮(图 1.3-1 中的 ②)。程序将从头开始重新运行,字符串将再次显示。 west flash 如果收到“未找到探针”之类的错误消息,请检查 USB 电缆是否已插入图 1.3-1 中的端口 ①。拔下电缆并重新插入,然后再次运行命令示例 2.2-3( A.4 )。 如果写入成功,但未显示任何内容或字符乱码,请检查 Tera Term 中的端口、波特率和字符编码设置( A.5 )。 说明:复制到工作文件夹(命令示例 2.2-1) 第一行代码 New-Item 创建工作文件夹 ~\handson (如果该文件夹已存在,则无需执行任何操作;此行代码只需在第一次编写时执行)。 Copy-Item -Recurse 将示例文件夹从 zephyr\samples\ 复制到工作文件夹, cd 将您切换到该文件夹。在后续章节中,您应该在开始之前将要使用的示例复制到工作文件夹。 Copy-Item 如果目标文件夹已存在,此命令将在该文件夹内创建一个新的示例文件夹(例如, ~\handson\hello_world\hello_world )。如果要重新复制,请先使用 Remove-Item -Recurse -Force ~\handson\hello_world 删除旧文件夹,然后再运行该命令。 说明: -b 和 -p 选项(命令示例 2.2-2) -b 接下来的参数指定要构建的开发板。由于 FRDM-MCXN947 采用双核 Cortex-M33 配置,因此需要指定“开发板名称/SoC 名称/核心名称”,如 frdm_mcxn947/mcxn947/cpu0 所示。对于像 FRDM-MCXA153 这样的单核开发板,则只需要指定开发板名称,如 frdm_mcxa153 所示。 您也可以省略 SoC 名称,将其写成 frdm_mcxn947//cpu0 。这是因为,对于只有一个 SoC 的电路板(例如 FRDM-MCXN947),可以省略 SoC 名称,只留下一个斜杠,电路板仍然可以被唯一标识。本文从 2.3 节开始将使用此缩写。 -p auto 这是 --pristine=auto 的缩写。它会自动判断是否从全新状态(原始构建)重建构建文件夹。更多详情将在下一节中解释。 这些选项的含义对于 west build 和 west flash 仍然相同,它们将在后续章节中出现。 说明:构建过程中创建的文件位置。 如果构建成功,将在当前目录下创建文件夹 build/ 。 build/zephyr/zephyr.elf (包含调试信息的 ELF 文件)将用于写入,接下来执行的 west flash 也将把该文件写入开发板。此外,可以在 build/zephyr/.config 中查看配置内容,并在 build/zephyr/zephyr.dts 中查看提取后的设备树。 --pristine 此选项指定是否重新创建此构建文件夹。 -p auto ( --pristine=auto )仅在必要时(基于源代码或配置的更改)重新创建构建文件夹。如果您希望显式执行全新构建(例如,当构建过程异常时),请指定 -p always ( --pristine=always )。 如果你想在自己的电脑上尝试一下(使用 Tera Term) 如果您通过进入 Tera Term 的“设置”→“保存设置”来保存步骤 3 和 4 中的设置,那么从下次开始,您只需使用步骤 2 即可连接。 Tera Term 无法在 macOS 或 Linux 上使用。请使用 CoolTerm 或类似应用程序,选择端口 /dev/tty.usbmodemXXXX (macOS)或 /dev/ttyACM0 (Linux),并以 115200 的波特率连接。 hello_world 我们来看一下代码。 hello_world src/main.c 是一段非常短的代码。要在 VS Code 中打开此文件,请从 PowerShell 运行 code ~\handson\hello_world\src\main.c 。(请勿使用 VS Code 的“打开文件夹”功能打开文件夹,因为这会切换显示文章的窗口内容。) #include int main(void) { printf("Hello World! %s\n", CONFIG_BOARD_TARGET); return 0; } 从我们目前所见,这是一段非常普通的 C 代码 Hello World 。唯一特殊之处在于,宏 CONFIG_BOARD_TARGET 可能定义于此构建环境中。通常微控制器程序中常见的时钟设置和外设初始化过程,在这段代码中却找不到。 这段代码也具有“非实时操作系统”的特征。它没有创建任务(线程),也没有启动调度器。它只是简单地调用 main() 到 printf() ,这使得它看起来几乎与 PC 上的 C 程序完全相同。 在许多实时操作系统 (RTOS) 中,即使是简单的“Hello World”程序,也需要显式地创建一个任务,并通过类似 osKernelStart() 的调用来启动调度器。而在 Zephyr 中, main() 本身就已经作为一个线程运行。之前的初始化工作则由构建系统和 Kconfig 设置完成。这种“接近编写纯 C 程序的体验”是 Zephyr 为应用程序开发者提供的特性之一。 说明:除 main.c 外,还需要其他文件 Zephyr 应用程序的最低要求是 src/main.c ,以及 prj.conf 和 CMakeLists.txt 。 prj.conf 这是一个配置文件,列出了应用程序使用的 Kconfig 功能。 hello_world 表示您没有特别想要启用的功能,因此它几乎是空的。 CMakeLists.txt 这是构建定义文件。 find_package(Zephyr ...) 加载 Zephyr 构建系统, target_sources() 指定要构建的源文件。 实际上,如果您查看目录 zephyr/samples/hello_world/ ,只会找到像 CMakeLists.txt 、 prj.conf 、 src/main.c 这样的文件(以及用于解释的 README.rst )。Zephyr 应用程序的最小配置就是这三个文件。 2.3 编写闪烁示例(LED 闪烁) 在本节中,我们将编写 blinky 示例程序,使开发板上的 LED 闪烁。步骤与“Hello World”程序相同,只是复制的示例程序有所不同。从现在开始,我们将使用 2.2 节中提到的缩写 frdm_mcxn947//cpu0 来指代开发板。 # コマンド例2.3-1 Copy-Item -Recurse ~\zephyrproject\zephyr\samples\basic\blinky ~\handson\blinky cd ~\handson\blinky 确保提示符为 ...\handson\blinky> ,然后构建。 注意:构建和写入操作应分开执行。 west build 如果同时粘贴 west flash ,即使构建失败, west flash 也会继续运行。在这种情况下, west flash 中的错误信息会最后显示,因此真正的原因(构建错误)会被推到屏幕顶部。如果按照确认构建成功后的顺序进行操作,然后再写入,就能立即看到失败发生的位置。 # コマンド例2.3-2 west build -p auto -b frdm_mcxn947//cpu0 . 确认构建成功(内存使用情况表出现在最后)后,写入更改。 # コマンド例2.3-3 west flash 板上的 RGB LED(图 1.3-1 中的 ③)每秒闪烁一次红色(图 2.3-1)。如果 Tera Term 保持连接状态,则在闪烁的同时还会显示以下信息。 图 2.3-1:RGB LED 闪烁红色 *** Booting Zephyr OS build v4.x.x *** LED state: OFF LED state: ON LED state: OFF LED state: ON ✔这是正确的结果: RGB LED 每秒闪烁一次红色。 当事情进展不顺利时 如果在提示符仍为 ...\hello_world> 时执行了 west build ,则 hello_world 将被重新构建,这不会造成任何损害。请从命令示例 2.3-1 中的 cd 重新开始。 Copy-Item 如果运行两次,将会创建一个名为 ~\handson\blinky\blinky 的嵌套文件夹,但这不会影响 ~\handson\blinky 中的构建。如果您对此感到困扰,请使用 2.2 节中解释的方法将其删除,然后再复制一次。 2.4 让我们来看一下示例代码。 在本节中,我们将检查 blinky 的代码,以确定 LED 的位置是如何确定的。 blinky src/main.c 是一小段代码,它简单地切换(反转)一个 GPIO 的输出。我们来看一下完整的代码。要在 VS Code 中打开它,请从 PowerShell 运行 code ~\handson\blinky\src\main.c 。 /* * Copyright (c) 2016 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include #include #include /* 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; } 从上到下,正在进行以下工作: SLEEP_TIME_MS 然后确定闪烁间隔(1000 毫秒 = 1 秒)。 DT_ALIAS(led0) GPIO_DT_SPEC_GET() 命令检索信息( led ),指示名为 led0 LED 连接到哪个 GPIO 引脚。 main() 首先,我们使用 gpio_is_ready_dt() 检查 GPIO 是否可用,然后我们使用 gpio_pin_configure_dt() 将该引脚设置为输出。 while (1) 在这个循环中, gpio_pin_toggle_dt() 时 LED 反转, printf() 时显示状态, k_msleep() 时程序等待 1 秒,然后重复此过程。 值得注意的是,代码中没有任何地方写明 LED 连接到哪个引脚。代码只指定了一个别名(另一个名称) led0 。LED led0 实际指的是哪个 LED,这在 Zephyr 开发板的设备树(一个描述硬件配置的文件, zephyr/boards/nxp/frdm_mcxn947/frdm_mcxn947.dtsi )中定义如下: aliases { led0 = &red_led; ... }; red_led: led_3 { gpios = <&gpio0 10 GPIO_ACTIVE_LOW>; label = "Red LED"; }; (摘抄) 电路板上已写入信息“ led0 是红色 RGB LED(GPIO0 的引脚 10)”,因此应用程序只需指定 led0 即可。 blinky 文件夹不包含 FRDM-MCXN947 的任何其他设置(覆盖文件位于 boards/ 文件夹中。这些文件会将差异覆盖到电路板的设备树上,详见 3.3 节)。仅使用电路板上的定义即可正常工作。这就是为什么相同的 blinky 在任何定义了 led0 电路板上都能正常工作的原因。 图 2.4-1 展示了应用程序、设备树、 Kconfig和驱动程序之间的关系。Kconfig 是开启和关闭 Zephyr 功能的机制(我们将在第 3 章中再次讨论这一点)。 图 2.4-1:blinky 中应用程序、设备树、Kconfig 和驱动程序之间的关系。 列:MCX,但被称为“kinetis”? 在图中, compatible = "nxp,kinetis-gpio" 内的 kinetis 是恩智浦 (NXP) 上一代微控制器系列(Kinetis)的名称。由于 MCX 的 GPIO 与 Kinetis 使用相同的电路,Zephyr 没有创建新名称,而是沿用了相同的名称和驱动程序 ( gpio_mcux.c )。 这里介绍的“设备树”将在第3章解释温度传感器的工作原理时再次出现。在第3章中,我们将通过一个示例来探讨如何使用应用层覆盖来添加板级定义中未涵盖的元件。 ▲ 返回目录 3. 尝试操作温度传感器。 3.1 构建并烧录样机 在本节中,我们将编写读取板上温度传感器的 thermometer 示例代码。与第2章一样,在编译和编写之前,请将其复制到工作文件夹。 # コマンド例3.1-1 Copy-Item -Recurse ~\zephyrproject\zephyr\samples\sensor\thermometer ~\handson\thermometer cd ~\handson\thermometer # コマンド例3.1-2 west build -p auto -b frdm_mcxn947//cpu0 . 构建成功后,请确保 Tera Term 仍然连接,然后再写入文件(如果您已关闭它,请使用第 2.2 节中的步骤重新连接)。 # コマンド例3.1-3 west flash 3.2 检查值 串口控制台每秒显示一次温度值(图 3.2-1)。如果用手指触摸温度传感器(图 1.3-1中的 ④),可以看到该值逐渐增加。 *** Booting Zephyr OS build v4.x.x *** Thermometer Example (arm) Temperature device is 0x1000b23c, name is p3t1755@4800000236152a0090 Temperature is 25.9°C Temperature is 25.8°C Temperature is 25.8°C 前几行是示例启动信息。从标记为 name is p3t1755@4800000236152a0090 行可以看出,P3T1755 被用作温度传感器,如第 3.3 节设备树中所定义。(标记为 0x1000b23c 部分可能因版本而异。) 图 3.2-1:Tera Term 中温度的屏幕截图 ✔如果你能做到以下几点,那就没问题了:温度应该每秒在 Tera Term 中显示一次,并且当你用手指触摸传感器时,该值应该增加。 当事情进展不顺利时 如果没有任何显示,请检查 Tera Term 标题栏中 COM 端口是否显示为 COM3 - Tera Term VT (表示已连接),然后按下板上的 RESET 按钮 ( A.5 )。 °C 这部分内容可能显示乱码 → 这是由于 Tera Term 中的字符编码设置所致,并不影响其运行。如果您对此感到困扰,请在 Tera Term 2.2 节的步骤 4 中将接收字符编码设置为 UTF-8 。 Device ... is not ready 如果您看到类似这样的消息 → 传感器初始化失败。请检查您是否编写了另一个示例(例如 hello_world ),或者是否在本示例的文件夹中使用 cd ~\handson\thermometer 构建了它,然后从命令示例 3.1-2 重新开始。 3.3 它为什么会移动? 在本节中,我们将解释为什么通过查看 src/main.c 和 boards/*.overlay 来显示传感器值。 main.c 另一方面,传感器设备的获取方式如下: static const struct device *const temp_dev = DEVICE_DT_GET(DT_ALIAS(ambient_temp0)); boards/*.overlay 决定了别名 ambient_temp0 实际指的是哪个硬件(传感器类型和连接总线)。覆盖文件将每个应用程序的差异叠加到电路板本身的设备树上。 在 FRDM-MCXN947 中,温度传感器连接到 I3C 总线。 boards/frdm_mcxn947_mcxn947_cpu0.overlay 定义如下(实际文件还包含 I3C 总线时钟速度的规范,但这里只提取了必要的部分)。 / { aliases { ambient-temp0 = &p3t1755; }; }; &i3c1 { status = "okay"; p3t1755: p3t1755@4800000236152a0090 { compatible = "nxp,p3t1755"; reg = <0x48 0x0236 0x152a0090>; status = "okay"; }; }; 我只做两件事。 aliases 在块中,将 ambient-temp0 连接到 p3t1755 节点。 i3c1 在总线下方,添加一个使用 nxp,p3t1755 驱动程序的传感器节点。 main.c 侧面只显示 DT_ALIAS(ambient_temp0) 。应用程序代码并不知道该传感器是通过I3C(速度更快)而不是I2C连接的,也不知道寄存器地址的详细信息。 关键在于“只需更改设备树的定义,即可支持不同的硬件配置,而无需编写任何代码”。例如,考虑将同一传感器移至 I2C 连接的电路板,或更换为不同的温度传感器 IC(假设有兼容的驱动程序)。基本上,只需更改 boards/*.overlay 的内容, main.c 中的 DEVICE_DT_GET(DT_ALIAS(ambient_temp0)) 行即可直接重用。这种关系如图 3.3-1 所示。 图 3.3-1:温度计中应用程序、设备树、Kconfig 和驱动程序之间的关系。 解释:设备树实际存在于哪里? FRDM-MCXN947 板设备树的主要组成部分是 Zephyr 板上的 zephyr/boards/nxp/frdm_mcxn947/frdm_mcxn947_mcxn947_cpu0.dts (以及通用定义 frdm_mcxn947.dtsi )。它定义了板上所有标准外设的完整集合。 板载设备树文件本身不会被直接修改。Zephyr 会在应用程序端放置一个差异文件(overlay, *.overlay ) ,用于追加或覆盖板载设备树文件的定义。 在 Thermometer 中,它是 samples/sensor/thermometer/boards/frdm_mcxn947_mcxn947_cpu0.overlay ,而在第五章使用的存储库中,它是 boards/frdm_mcxn947_mcxn947_cpu0.overlay 。正文中显示的 p3t1755 节点的定义也是此叠加层的一部分。其优点在于,您可以仅叠加每个应用程序所需的差异,同时保持电路板本身的定义不变。 构建时,开发板的设备树和应用程序的覆盖层会合并,最终的完整设备树会写入 build/zephyr/zephyr.dts 。打开此文件,可以看到 p3t1755 节点已添加到开发板的定义中。 在没有操作系统的裸机开发中,以及在许多缺乏设备树机制的实时操作系统(RTOS)中,通常会将传感器的 I2C/I3C 地址和寄存器操作直接写入应用程序代码。因此,每次传感器型号或总线接线发生变化时,都需要重写应用程序逻辑。Zephyr 将“哪个组件”及其“连接位置”的信息移至一个名为设备树的独立层。因此,即使电路板发生变化,应用程序逻辑本身(在本例中为“读取和显示温度”的过程)也无需修改。 如果您感兴趣,可以尝试比较其他电路板的 overlay 文件。您会发现,尽管它们都是 samples/sensor/thermometer ,但每个电路板都有一个不同的 overlay 区别仅在于硬件连接。 ▲ 返回目录 4. 尝试运行 HTTP 服务器。 在本章中,我们将使用以太网线连接PC和开发板,并通过浏览器访问运行在开发板上的HTTP服务器(图4.1-1)。到目前为止,所有功能都独立运行在开发板上,但接下来我们将更进一步。 我们将使用官方的 Zephyr 示例 samples/net/sockets/http_server 。直接编译并烧录即可,您可以通过浏览器控制 LED 的开关,并检查简单端点的响应(每个 URL 路径都会提供一个窗口,例如 /uptime )。在第 5 章中,我们将以此机制为基础进行扩展,使其能够在浏览器中查看温度传感器的值。 关于在 FRDM-MCXN947 上运行 HTTP 服务器示例的步骤已单独发布在一篇博文中。如需了解更多详细信息,例如 PC 网络设置以及如何使用 DHCP 而非静态 IP 地址,请参阅该博文。 我尝试在 FRDM-MCXN947 上运行 Zephyr HTTP 服务器(NXP 技术博客,日语) 4.1 构建并烧录样机 在本节中,我们将重写样本 http_server 的 IP 地址并记录下来,然后验证是否从位于 ping PC 收到响应。这次,我们将使用不使用 TLS 的纯 HTTP 协议,并使用固定的 IP 地址。 图 4.1-1:PC 和主板连接及 IP 地址(USB 线缆和以太网线缆) 步骤 1:将示例复制到您的工作文件夹。 # コマンド例4.1-1 Copy-Item -Recurse ~\zephyrproject\zephyr\samples\net\sockets\http_server ~\handson\http_server cd ~\handson\http_server 步骤 2:重写 prj.conf 的 IP 地址 运行以下命令将在 VS Code 中打开 prj.conf 。 # コマンド例4.1-2 code prj.conf 您可能会在 VSCode 顶部看到一条横幅,上面写着“受限模式旨在安全地浏览代码。”(稍后在图 4.1-2 的顶部也可以看到此横幅)。这是一条通知,表明出于安全考虑,当前打开的文件以功能受限的模式显示。它不会影响编辑或保存 prj.conf ,因此您可以继续操作。 要为开发板分配固定 IP 地址,请重写包含 CONFIG_NET_CONFIG_MY_IPV4_ADDR 行。查看文件中间 # Network address config 下方的内容。在原始示例中,它看起来像这样: CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1" CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2" CONFIG_NET_CONFIG_MY_IPV4_GW="192.0.2.2" 将第一行(文件第 54 行)中 " 内的值替换为以下内容。在 VS Code 中,行号显示在左侧(图 4.1-2)。根据 Zephyr 版本不同,行号可能略有偏差。 CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.168.1.100" 第三行的 CONFIG_NET_CONFIG_MY_IPV4_GW 是网关(路由器)的地址。在本配置中,由于 PC 和主板直接连接,因此无需更改此地址。如果需要更改,请将其更改为 "192.168.1.1" (请参阅本节末尾的说明)。第二行的 CONFIG_NET_CONFIG_PEER_IPV4_ADDR 与之类似,但设置不同,因此这次无需更改。 注意:请勿将此行粘贴到 PowerShell 中。 上面标记为 CONFIG_... 行不是命令,而是在 VS Code 中打开的 prj.conf 文件的内容。如果您将其粘贴到 PowerShell 中,将会收到“无法识别”的错误提示(这不会影响文件,所以即使您误粘贴也没关系)。 请重写原行。如果在保留原行不变的情况下在末尾添加一行,则相同的设置将被写入两次,难以确定哪个设置有效。 如果您还需要重写网关: PEER_IPV4_ADDR 行和 MY_IPV4_GW 行的原始值均为 "192.0.2.2" 。按值搜索很容易导致覆盖 PEER 行,因此请检查行开头的 CONFIG_... 名称。 不要移除值周围的 " (双引号)。 编辑完成后,请使用 Ctrl + S 保存。虽然 VSCode 标签页中文件名旁边会显示 ● ,但文件尚未保存。如果您忘记保存,构建过程将使用原始 IP 地址。 图 4.1-2 显示了重写并保存 MY_IPV4_ADDR 行后的结果( MY_IPV4_GW 行保持不变)。 图 4.1-2: prj.conf 已重写为 MY_IPV4_ADDR 并保存。 场地内所有电脑的IP地址已配置为与本主板位于同一子网 192.168.1.0/24 内。您无需更改电脑上的任何设置。 步骤 3:构建和刷写 prj.conf 保存后,返回 PowerShell 窗口并进行构建。注意不要将命令直接粘贴到 VSCode 窗口中。 # コマンド例4.1-3 west build -p auto -b frdm_mcxn947//cpu0 . 构建成功后,我会写入数据。 # コマンド例4.1-4 west flash 写入过程中,您可能会看到类似 INFO: MCU-Link firmware update 'check': ... is running a firmware version which is older than the included firmware version 消息。这表示板上的 MCU-Link 固件版本低于 LinkServer 自带的固件版本。写入过程将照常进行,请放心继续。 步骤 4:连接以太网线并检查响应。 编程完成后,使用以太网线将 PC 和电路板直接连接。将电缆插入电路板的以太网连接器(图 1.3-1中的⑤)(图 4.1-3)。 图 4.1-3:将以太网电缆插入板上的以太网连接器。 开发板需要几秒钟才能连接到网络。请稍等片刻,然后在 PowerShell 中运行以下命令。 # コマンド例4.1-5 ping 192.168.1.100 它将显示为 192.168.1.100 からの応答 ,如下图所示(图 4.1-4)。 192.168.1.100 に ping を送信しています 32 バイトのデータ: 192.168.1.100 からの応答: バイト数 =32 時間 <1ms TTL=64 192.168.1.100 からの応答: バイト数 =32 時間 <1ms TTL=64 192.168.1.100 からの応答: バイト数 =32 時間 <1ms TTL=64 192.168.1.100 からの応答: バイト数 =32 時間 <1ms TTL=64 192.168.1.100 の ping 統計: パケット数: 送信 = 4、受信 = 4、損失 = 0 (0% の損失)、 ラウンド トリップの概算時間 (ミリ秒): 最小 = 0ms、最大 = 0ms、平均 = 0ms 图 4.1-4: ping 的执行结果 ✔如果您已看到此处,则一切正常: 192.168.1.100 からの応答 已显示四次。PC 和主板之间的通信正常。 当事情进展不顺利时 如果看到“请求超时”的消息,请按顺序检查以下内容: 请确保以太网线已牢固地插入电脑和主板。另外,请检查主板连接器上的 LED 指示灯是否亮起。 prj.conf 检查 IP 地址是否已设置为 192.168.1.100 并正确保存。如果已重新保存,请从命令示例 4.1-3 开始重新构建并写入文件。 检查串口控制台(Tera Term)上是否显示开发板的启动信息或 IP 地址。如果未显示任何内容,请尝试按下 RESET 按钮。 如果仍然无法解决问题,请参阅A.6 。 如果您看到“目标主机不可达”的消息,则可能是 PC 的 IP 地址未设置为 192.168.1.x ( A.6 )。 解释:为什么要使用纯 HTTP? 纯HTTP是指通信内容未加密的HTTP通信。实际应用中,建议使用HTTPS,它采用TLS加密,以防止窃听和篡改。使用HTTPS需要预先准备好身份验证相关的文件,例如服务器证书和私钥。 Zephyr 也支持这些身份验证机制和通信加密机制。但是,为了避免在有限的时间内准备身份验证文件,并专注于体验 HTTP 服务器机制本身,本次实践环节我们将特意使用纯 HTTP。 说明:关于网关设置 对于不经过路由器的通信,网关( CONFIG_NET_CONFIG_MY_IPV4_GW )并非绝对必要。在这种情况下,PC 和主板通过以太网线直接连接,并且位于同一子网 192.168.1.0/24 中。所有通信均通过同一链路内的 ARP 协议进行解析,无需经过网关。因此,在这种情况下,无论您保留原始值还是将其更改为 "192.168.1.1" ,结果都相同。 如果将电路板连接到路由器以与其他网络通信,则需要重写路由器的地址。 4.2 尝试通过浏览器控制 LED 在本部分,您可以通过浏览器打开和关闭电路板上的 LED 灯。 注意:请输入以 http:// 请在浏览器地址栏中输入地址 http://192.168.1.100/ ,不要省略 http:// 。 如果您省略 http:// 或选择较早的历史记录,现代浏览器会自动尝试使用加密通信 https:// 进行连接。本论坛使用普通的 HTTP 协议,因此您无法使用 https:// 进行连接。如果您看到类似“您的连接不是私密的”或“此网站不支持安全连接”的消息,请检查地址栏是否设置为 https:// 。 请注意,页面加载时,地址栏左侧会显示“不安全”字样。这是因为您正在使用未加密的纯HTTP连接,这种情况属于正常现象。 在浏览器中访问 http://192.168.1.100/ 将显示一个用于控制 LED 的页面(图 4.2-1)。 图 4.2-1:在浏览器中打开的 LED 控制页面 除了“LED 控制”之外,此页面还显示“运行时间计数器”(自电路板启动以来经过的时间,如第 4.3 节中看到的值为 /uptime )和“网络统计信息”(电路板发送和接收的数据量等统计信息)。 按下页面上的“LED开”或“LED关”按钮,即可使电路板上的RGB LED(图1.3-1中的③)亮起/熄灭(图4.2-2)。2.3节中的LED闪烁示例也将使用同一个RGB LED。之前LED灯闪烁的是红色,但这次是绿色。 图 4.2-2:浏览器运行时,RGB LED 会亮起绿色。 ✔这是正确的结果:按下按钮时,RGB LED 灯会亮起/熄灭。 此时,Tera Term 中会连续显示类似 [00:00:25.684,000] ... 的行,还会显示 uart:~$ 。这并非异常,而是 HTTP 服务器示例的日志输出以及 Zephyr shell(接受命令输入)的显示内容。 当事情进展不顺利时 Ping 命令可以正常工作,但页面在浏览器中无法显示 → 检查地址是否为 http:// 。如果您的公司电脑上设置了代理,则需要将其配置为绕过代理( A.6 )。 页面显示正常,但 LED 灯没有反应 → 它应该亮起绿色,而不是红色,请检查颜色。如果仍然没有反应,请刷新浏览器( F5 )并再次尝试按下按钮。 专栏:论网络功能的“沉重性” 在典型的嵌入式开发中,即使运行一个简单的 HTTP 服务器通常也需要大量的准备工作。这包括嵌入和配置 TCP/IP 协议栈(例如 lwIP)、准备套接字 API、选择和实现 HTTP 解析器以及实现静态文件服务方法等任务。 在这种情况下,所有这些选项都简单地从 prj.conf 改为 CONFIG_NET_* ,从 CONFIG_HTTP_* 改为 y 。应用程序端唯一需要编写的是每个端点的处理函数(即请求到达时调用的函数)。网络协议栈和 HTTP 服务器可以通过单一的构建系统和 Kconfig 进行统一管理。这也是 Zephyr 虽然是用于微控制器的实时操作系统 (RTOS),但经常被讨论其相对大量的中间件的原因之一。 专栏:Zephyr 的 TCP/IP 协议栈是 lwIP 吗? 提到嵌入式系统的 TCP/IP 协议栈,很多人可能会想到 lwIP(轻量级 IP)。然而,Zephyr 并没有从外部导入 lwIP;相反,它使用自己基于 subsys/net/ 实现的专有 TCP/IP 协议栈作为标准。Zephyr 内核内置了 IPv4/IPv6、TCP/UDP 和套接字 API 等功能。 prj.conf 您启用的选项 CONFIG_NET_IPV4 、 CONFIG_NET_TCP 和 CONFIG_NET_SOCKETS 也与此协议栈的功能相关。(Zephyr 也支持将外部网络协议栈组合成模块的配置,但本文使用的是标准协议栈。) 4.3 查看 /uptime 端点 在本节中,我们将研究最简单的端点的行为。 在浏览器中访问 http://192.168.1.100/uptime 将显示自棋盘启动以来经过的时间。 ✔如果您已到达此步骤,则一切就绪:浏览器现在应该显示自启动以来经过的时间。 我们将在下一章(第 5 章)中直接应用这种端点结构,即“计算响应 GET 请求的值并将其作为文本返回”。 说明:HTTP 请求和响应 当您在浏览器中访问 /uptime 时,浏览器会向板子发送 GET 请求,意思是“请提供此路径的内容”。 在电路板端,接收请求的 HTTP 服务器调用与 /uptime 关联的处理函数(当请求到达时调用的函数;在本例中为 uptime_handler )。 处理程序计算自程序启动以来经过的时间,并将该值设置为响应正文。 HTTP 服务器会将状态行(例如 200 OK )和标头添加到此正文中,并将其作为 HTTP 响应返回给浏览器。 浏览器会在屏幕上显示收到的响应正文。 将在第五章中添加的 /temperature 端点以完全相同的方式运行:它接收请求,计算一个值,并将其作为消息体返回。 ▲ 返回目录 5. 修改 HTTP 服务器,以便在浏览器中查看温度传感器值。 5.1 新增了哪些内容? 本节解释了第 4 章中 HTTP 服务器示例新增的内容。 我们添加了两个返回温度的端点,使用与第 4.3 节中看到的 /uptime 端点相同的结构。这些变化只是 src/main.c 的一小部分。 端点 处理函数 需要归还的物品 /temperature temperature_handler 显示温度的HTML页面 /temperature.json temperature_json_handler 仅提供当时的温度值(JSON格式) 浏览器中打开的页面是页面 /temperature 。该页面嵌入了一小段 JavaScript 代码,每秒向 /temperature.json 查询一个值并更新显示(图 5.1-1)。这与自动更新首页 /uptime 值的机制相同。 static int temperature_handler(struct http_client_ctx *client, enum http_transaction_status status, const struct http_request_ctx *request_ctx, struct http_response_ctx *response_ctx, void *user_data) { /* 3章の温度センサーと同じ sensor_sample_fetch_chan / sensor_channel_get の流れで気温を取得し,HTMLとして返す */ ... } static int temperature_json_handler(struct http_client_ctx *client, enum http_transaction_status status, const struct http_request_ctx *request_ctx, struct http_response_ctx *response_ctx, void *user_data) { /* 同じ方法で気温を取得し,数値だけを返す */ ... } (摘抄) 我只添加了以下两项。其他部分(LED 控制、 /uptime 、静态页面交付)与原始示例保持一致。 这两个处理函数(传感器读取部分合并到一个公共函数中) 每条线都与路径 /temperature 和 /temperature.json 相关联,位于两条线 HTTP_RESOURCE_DEFINE() 图 5.1-1:流程图显示页面 /temperature 如何显示温度并每秒更新一次。 5.2 让我们实际在浏览器中查看一下 在本节中,我们将编写修改后的代码,并在浏览器中显示温度。 修改后的代码可在 GitHub 仓库teddokano/handson_http_server_temp中找到。将其克隆到您的工作文件夹,然后使用与之前相同的步骤进行构建和烧录。 # コマンド例5.2-1 cd ~\handson git clone https://github.com/teddokano/handson_http_server_temp cd handson_http_server_temp [确认场地电脑是否已克隆(即场地是否可连接互联网)。如果已克隆,则仅显示 cd 及以上值。] 此存储库中的 prj.conf 已配置为使板的 IP 地址与第 4 章中的 192.168.1.100 相同。无需重写 prj.conf 。 # コマンド例5.2-2 west build -p auto -b frdm_mcxn947//cpu0 . 程序将在构建成功后编写。您可以像第四章中那样保持以太网线连接。 # コマンド例5.2-3 west flash 写入数据后,等待几秒钟,然后在浏览器中访问 http://192.168.1.100/temperature ,将显示以下页面(图 5.2-1)。 Hello FRDM-MCXN947 temp sensor: P3T1755 Temperature: 24.312°C ← Back 温度值每秒自动更新一次。尝试用手指触摸温度传感器(图 1.3-1中的 ④),观察数值变化。如果直接打开 http://192.168.1.100/temperature.json ,还可以在页面背面看到查询到的数值响应(例如 24.312 )。 图 5.2-1:浏览器中显示的温度 ✔如果你能做到这一点,那就没问题了:温度会显示在浏览器中,当你用手指触摸传感器时,数值会增加。 当事情进展不顺利时 git clone 如果收到“目录已存在且非空”的消息 → 说明目录已被克隆。只需执行 cd handson_http_server_temp 即可。 git clone 如果您看到类似“无法解析主机”的消息,则表示您的电脑未连接到互联网。请连接到可以访问互联网的网络(例如 Wi-Fi),然后重试。 /temperature 打开后显示“404 Not Found”→ 第 4 章的原始示例仍然保留在那里。请检查提示符是否为 ...\handson_http_server_temp> ,然后从命令示例 5.2-2 重新开始。 如果页面无法打开,请按照第 4.1 节和第 4.2 节“当它不起作用时”中的相同步骤进行操作。 5.3 第 3 章和第 4 章的内容是如何结合起来的? 本章创建的应用程序没有使用任何新的机制。它只是我们在第 2 章到第 4 章中逐一使用的各个组件的组合。我们将在表格中总结每个组件的出现位置。 第五章中使用的物品 第五章的哪个位置? 你在哪里看到的? 使用别名获取温度传感器数据。 main.c 的 DEVICE_DT_GET(DT_ALIAS(ambient_temp0)) 第 3.3 节(与温度计同一行) 在设备树中添加温度传感器。 boards/frdm_mcxn947_mcxn947_cpu0.overlay p3t1755 节点 第 3.3 节(与温度计相同的叠加层) 启用传感器功能 prj.conf 的 CONFIG_SENSOR=y 第 3.3 节中的图 3.3-1 读取温度 sensor_sample_fetch_chan() ・ sensor_channel_get() 第三章(温度计) 将路径和处理函数关联起来。 HTTP_RESOURCE_DEFINE() 注册 /temperature 和 /temperature.json 第 4.3 节(与 /uptime 符号相同) 计算该值并将其作为响应返回。 temperature_handler ・ temperature_json_handler 第 4.3 节的解释(与 uptime_handler 相同流程) 网络和HTTP服务器功能 prj.conf 的 CONFIG_NET_* ・ CONFIG_HTTP_* 第四章(与原例相同) 通过将各个独立运行的组件组合起来,我们得到了一个允许用户“通过浏览器查看传感器数值”的应用程序。我们编写的新代码只有两个处理函数和两行用于注册它们的代码。 解释:为什么 main.c 和 CMakeLists.txt 看起来有点复杂 src/main.c 打开后,除了 /temperature 处理程序之外,您还会发现很多行代码。这是因为 HTTP 服务器示例还提供了原始端点,例如首页的 HTML 和 JavaScript,以及 LED 控制 /uptime 。这些是“作为 HTTP 服务器运行的基础”,与添加温度传感器没有直接关系,因此目前只需关注 5.1 节中的两个处理程序函数即可。此基础的机制在附录 G中有详细说明。 ▲ 返回目录 6. 总结 6.1 我今天经历的事 我熟悉了 Zephyr 开发环境(west、设备树、Kconfig)。 我运行了一个 LED 闪烁程序、一个温度传感器读取程序和一个 HTTP 服务器。 我将两个示例结合起来,创建了一个应用程序,允许您在浏览器中查看传感器值。 6.2 可供您再次尝试的资源 Zephyr 项目官方文档 本次使用的示例代码仓库是 (teddokano/handson_http_server_temp) 这篇博文 更多实用信息,请参阅 → NXP Zephyr OS - 概要页面(日语博客) 6.3 如何利用人工智能? 阅读或修改 Zephyr 的示例代码时,您可以询问 AI 助手“如何将此功能添加到此示例中?”,它会根据现有代码的模式提出修改建议。本示例中的 /temperature 端点就是按照与现有 /uptime 端点相同的结构添加的。如果您遇到任何不理解的部分,我们建议您首先在现有示例代码中找到类似的部分,然后尝试与 AI 一起解读。 另一个常见问题是当 west build 出现错误时。在 Zephyr 构建过程中,会同时出现大量消息,包括 Kconfig 警告、CMake 错误和链接器错误等。最初,很难确定问题的真正原因。 在这种情况下,建议先将整个错误信息粘贴到人工智能中,然后询问“这是什么类型的错误?”。在撰写本文的过程中,附录 G中提到的 sections-rom.ld 附近的链接错误也是借助人工智能识别出来的。 提问时应提供的信息已在第 0.4 节“提问时”中进行了总结。这同样适用于咨询人工智能的情况。 6.4 清理工作 参加此次活动的人 请勿移动电路板和线缆。无需收起您的电脑。请听从场馆工作人员的指示。 那些在自己的电脑上试过的人 您可以直接关闭 PowerShell(或终端)。关闭窗口后,虚拟环境激活和 ZEPHYR_BASE 设置将会消失。如果您想在不关闭窗口的情况下退出虚拟环境,请执行 deactivate 。 如果您在第 4 章中将电脑的以太网端口设置为静态 IP 地址,则应将其恢复为原始设置。在与A.6列相同的屏幕上,将“IP 地址分配”改回“自动 (DHCP)”。否则,当您将该以太网端口连接到公司局域网或家用路由器时,将无法连接到互联网。 电路板会保留最后写入的程序(第 5 章中的带温度显示的 HTTP 服务器)。该程序每次通电时都会运行,但可以随时使用 west flash 将其覆盖为其他程序。 保留工作文件夹 ~\handson ,以便您可以从今天中断的地方继续工作。当您不再需要该文件夹时,可以将其删除(但不要删除 ~\zephyrproject )。 ▲ 返回目录 附录A:当事情进展不顺利时 本附录总结了复制粘贴命令时常见的陷阱以及解决方法。请参阅正文各部分中的“如果出现问题该怎么办”章节。 A.1 首先,检查以下内容 如果遇到错误,请在查找原因之前检查以下三点。大多数问题都可以通过检查其中一项来解决。 如果 提示符不是以 (.venv) 或 开头 ,则虚拟环境未启用。如果您重新打开了 PowerShell 或在另一个窗口中工作,则可能会发生这种情况。请重新执行命令示例 2.1-1,或者在活动电脑上执行 zinit <作業中のサンプル名> ( 命令示例 2.1-2 )。 检查您是否在正确的文件夹中。 提示符中类似 PS C:\Users\<ユーザー名>\handson\thermometer> 的部分应该是您当前正在处理的示例所在文件夹。如果不是,请重新执行该部分第一个命令示例中的 cd 。 哪个错误最先出现? 发生错误时,随后会出现大量消息。向上滚动屏幕,找到第一个出现红色文本或 error 的行。如果您粘贴了多行,请检查是哪个命令导致了第一个错误。 您可以使用以下命令一次性查看当前状态。 # コマンド例A.1-1 Get-Location $env:ZEPHYR_BASE west topdir 如果第一行显示当前文件夹,第二行显示 C:\Users\<ユーザー名>\zephyrproject\zephyr ,第三行显示 C:/Users/<ユーザー名>/zephyrproject ,则说明环境已正确设置。如果第二行空,请再次运行命令示例 2.1-1。 A.2 PowerShell 西行 症状 原因 回收方法 west : 用語 'west' は,コマンドレット,関数,スクリプト ファイル,または操作可能なプログラムの名前として認識されません 虚拟环境未启用。 zinit <作業中のサンプル名> 执行以下操作(如果您的电脑没有 zinit ,请执行命令示例 2.1-1 并返回到当前工作文件夹 cd )。 west: unknown command "build" ZEPHYR_BASE 此设置未配置(west 找不到工作区)。 zinit <作業中のサンプル名> 执行以下操作(如果您的电脑没有 zinit ,请执行命令示例 2.1-1 并返回到当前工作文件夹 cd )。 Activate.ps1 “此系统已禁用脚本执行。” PowerShell 执行策略 Set-ExecutionPolicy -Scope CurrentUser RemoteSigned 执行此操作后,重复命令示例 2.1-1。 cd 错误信息为“无法检测到,因为路径不存在”(例如, Copy-Item 。 路径输入错误,或缺少源文件夹和目标文件夹。 复制命令,不要手动输入。如果 ~\handson 不存在,则执行示例 2.2-1 中的第一行命令。 我粘贴了命令,但没有任何反应,命令仍然显示在提示符后面。 行尾的换行符没有被复制。 Enter 按 粘贴后,出现提示 >> ,程序停止运行。 " 命令执行到一半就被中断了,例如忘记关窗户。 Ctrl 使用 + C 中断并重新复制整个命令。 ping 错误不在于 west 的结果,而在于日文文本。 我不小心粘贴了整篇文章的内容,甚至包括 prj.conf 这只会复制代码块的内容。由于它是在 PowerShell 中执行的,因此不会影响任何文件。 A.3 构建错误 症状 原因 回收方法 Invalid BOARD , No board named ... -b 板名拼写错误 再次复制命令示例,务必将 frdm_mcxn947//cpu0 中的 // 完全复制。 source directory ... does not contain a CMakeLists.txt ETC。 在示例文件夹以外的文件夹中构建(例如, ~\handson 或 ~\zephyrproject )。 执行该部分示例命令中的命令 cd ,然后重新构建。 Build directory ... is for application ... 据说这是另一个应用程序的构建文件夹。 上一次构建的 build 文件夹仍然存在。 -p auto 更改 -p always 并重新构建(见下文)。 我更改的设置没有生效,而且我一直收到不明原因的错误。 旧信息仍保留在构建文件夹中。 -p always 然后执行干净构建(见下文)。 prj.conf 重写后出现 Kconfig 错误。 重写时我不小心删除了 " 和 = 。 prj.conf 请仔细阅读。如果遇到任何困惑,请按照A.7 中的步骤重新复制示例。 使用以下命令执行全新构建。此操作会删除构建文件夹并从头开始重新创建,因此需要一些时间,但很可靠。 # コマンド例A.3-1 west build -p always -b frdm_mcxn947//cpu0 . 如果错误信息过长无法在屏幕上完整显示,您可以按如下方式将其保存到文件中。这有助于在 VS Code 中打开保存的文件 build_log.txt 并搜索 error ,或者将内容粘贴到 AI 中进行查询。 # コマンド例A.3-2 west build -p auto -b frdm_mcxn947//cpu0 . 2>&1 | Out-File build_log.txt code build_log.txt 如果构建速度很慢(如果您在自己的电脑上尝试构建), 在同一台电脑上,Windows 系统下构建 Zephyr 通常比 Linux 系统下构建要慢。对于像 http_server 这样的大型示例,单次构建可能需要几分钟。以下三个步骤将显著提升性能(场地电脑已配置完毕)。 安装 ccache :此工具会记住编译结果,并在后续构建中重复使用。安装后,Zephyr 将自动使用它。在同一文件夹中重新构建同一示例时,大部分编译过程将被跳过,从而加快速度(首次构建仍需正常时间)。在我的环境中,http_server 的全新构建时间从大约 -p always 分钟缩短至 73 秒。 Windows Defender 排除设置:打开“Windows 安全中心”→“病毒和威胁防护”→“管理设置”→“排除项”。将 ~\zephyrproject 、工作文件夹、Zephyr SDK 文件夹和 ccache 缓存文件夹(通常为 %LOCALAPPDATA%\ccache )添加到排除项中。如果您使用除 Windows Defender 之外的其他防病毒软件,请在其设置中将其排除。这将防止每次构建时扫描大量文件。如果您使用的是公司电脑,请查看 IT 部门的相关政策。 电源设置:将交流适配器连接到笔记本电脑,并将电源模式设置为“最佳性能”。 # コマンド例A.3-3 winget install --id Ccache.Ccache -e 安装完成后,重新打开 PowerShell,使用 zinit (或命令示例 2.1-1)配置环境,然后使用 -p always 重新构建。这是因为在安装 ccache 之前创建的构建文件夹仍然保留着不使用 ccache 的设置。 如果您是在自己的电脑上尝试, 如果您的 Windows 用户名包含日文字符或空格,构建工具在某些环境下可能无法正确处理路径,从而导致错误。在这种情况下,请尝试使用仅包含字母数字字符的短路径,例如使用 C:\handson 而不是 ~\handson 作为工作文件夹路径。 A.4 书写(西闪)错误 症状 原因 回收方法 No probe found 诸如“未找到调试探针”之类的错误 USB 连接线未插入电路板的调试连接器(MCU-Link 侧),或者使用了仅用于充电的 USB 连接线。 检查一下你插入的连接器,拔下来,再插回去,然后重试。使用与数据通信兼容的电缆。 探头正在使用中,无法连接。 使用其他工具进行调试,例如 MCUXpresso for VSC。 结束另一个工具的调试会话,然后重新运行它。 west flash 它尝试重新启动构建,但出现错误。 之前的构建失败了。 west flash 首先,解决构建错误( A.3 )。 写作过程进行到一半就停止了,电脑没有反应。 临时通信错误 拔下 USB 连接线,等待几秒钟,然后重新插上,再次运行 west flash 。 west flash 您可以多次运行此命令而不会出现任何问题。如果运行失败,请修复原因,然后再次运行相同的命令。 A.5 串口控制台上没有任何显示。 症状 原因 回收方法 在 Tera Term 的“新建连接”菜单中,我无法选择“串口”,并且板子的端口也没有出现。 USB 数据线未插入,或者它只是一根仅用于充电的数据线。 检查电缆,然后通过“文件”→“新建连接”重新打开屏幕(重新打开时端口列表将会更新)。 由于“端口正在使用”或“串口未打开”,无法连接。 另一个窗口,例如 Tera Term 或 VS Code 串口监视器,正在使用同一个端口。 关闭所有其他窗口应用程序或已连接的应用程序,然后重新连接。 整个事物变成了一串毫无意义的符号。 波特率不同。 进入“设置”→“串口”,将其改回 115200。 即使已连接,屏幕上也没有任何显示。 我错过了输出的时机。 按下电路板上的复位按钮。 拔下USB线再重新插上后,显示屏就停止工作了。 当我重新插拔 Tera Term 时,连接断开了。 依次点击“文件”→“断开连接”后,选择“文件”→“新建连接”,然后再次选择端口。请注意,COM端口号可能已更改。 ° 这些符号可能会显示为乱码。 接收到的字符编码不是 UTF-8。 根据第 2.2 节Tera Term 的步骤 4,将接收字符编码设置为 UTF-8 A.6 网络和浏览器相关功能 您可以通过在 PowerShell 中运行以下命令来检查您电脑的 IP 地址。 # コマンド例A.6-1 ipconfig 请检查“以太网适配器”部分中的“IPv4 地址”。 症状 原因 回收方法 IPv4 地址为 169.254.x.x 该电脑的以太网接口未配置静态IP地址。 将电脑端设置为 192.168.1.10 / 255.255.255.0 等(见下文) IPv4 地址为 192.168.1.100 PC 和主板的 IP 地址重叠。 将 PC 端设置为除 192.168.1.100 以外的值(例如, 192.168.1.10 )。 电脑的 Wi-Fi 也连接到网络 192.168.1.x 通信被路由到 Wi-Fi 网络,但无法到达电路板。 仅在实际操作训练期间断开 Wi-Fi。 Ping 命令可以正常工作,但无法在浏览器中打开。 https:// 您尝试通过代理服务器连接。 请将地址重新输入为 http://192.168.1.100/ 。如果您使用代理,请将 192.168.1.100 添加到代理例外列表中。 prj.conf 问题已解决,但我的IP地址没有改变。 未保存,或未重建。 在 VS Code 中保存 Ctrl + S ,然后从命令示例 4.1-3 重新开始。 如果您要在自己的电脑上尝试此操作:请在您的电脑上设置静态 IP 地址。 打开 Windows“设置”→“网络和 Internet”→“以太网”,然后在“IP 地址分配”下的“编辑”中选择“手动”。启用 IPv4,IP 地址输入 192.168.1.10 ,子网掩码输入 255.255.255.0 ,然后保存(网关字段可以留空)。更多详情,请参阅第四章开头介绍的 HTTP 服务器博客文章。 图 A.6-1:在 Windows“设置”中为以太网设置静态 IP 地址的屏幕截图。 A.7 当问题仍然无法解决时,应执行重置程序 如果您不确定问题所在,最快的解决方法是从头开始重新编写该部分。即使您删除了复制到工作文件夹的示例, ~\zephyrproject 中的原始示例仍然保留,因此您可以根据需要多次尝试。 关闭 PowerShell 窗口,然后打开一个新的窗口。 执行命令示例 2.1-1(在场地 PC 上输入 zinit )以重新配置环境。 删除要重做章节的示例文件夹。例如,要重做第 4 章,请执行以下操作: # コマンド例A.7-1 cd ~\handson Remove-Item -Recurse -Force ~\handson\http_server 将文件夹名称( http_server )替换为 hello_world 、 blinky 、 thermometer 或 handson_http_server_temp 以匹配您要重做的部分。注意不要删除 ~\handson 或 ~\zephyrproject 本身。 让我们从该部分的第一个命令示例(复制和 cd )重新开始。 如果电路板无法正常工作,请拔下 USB 电缆和以太网电缆,等待几秒钟,再重新插上,然后再次尝试 west flash 。 ▲ 返回目录 变更历史:[发布日期]:第一版 ========================= 我们目前无法回复此帖“评论”区的评论。由此给您带来的不便,我们深表歉意。如有任何疑问,请参阅“NXP技术问题咨询 - 如何联系我们(日语博客)”。(如果您已经是NXP的分销商或与NXP有业务往来,您可以直接联系您的代表。) 本文是为“ Zephyr 入门课程”实践研讨会编写的进度指南。在实际研讨会中,我们将按照本文概述的步骤进行操作。 即使您没有参加研讨会,也可以仅通过阅读本文在自己的环境中复现相同的内容。为此,我包含了命令示例和验证要点,没有任何遗漏。请将本文视为一份实用指南,而非一篇泛泛的解释性文章,它能帮助您最终实现目标。 (预计时间:90 分钟)*假设已安装 Zephyr 操作系统。 日本博客 介绍 传感器 以太网 界面 MCX
記事全体を表示
NXPのモーター制御 ~まとめページ~ (日本語ブログ) NXPのモーター制御に関わる記事をまとめたページです。 MCXマイコンをベースに、モーター制御の基礎から、実際の動かし方まで丁寧に解説しています。   NXPマイコン「MCX」を使用したモーター制御 順次公開予定 カテゴリ 記事 基礎編 【基礎編①】開発環境をまるっと解説!これ読めば迷わない! 【基礎編②】永久磁石モータの仕組みと制御 【基礎編③】永久磁石同期モータの仕組みと制御方法 【基礎編④】実践!ベクトル制御の仕組みをブロック図で見てみよう! 実践編 VSCode環境 (FRDM-MCXA153を使用) IDE環境 (FRDM-MCXA156を使用) TBD 【実践編①】永久磁石同期モータの仕組みと制御方法 TBD 【実践編②】永久磁石同期モータの仕組みと制御方法 TBD - NXPでは、モーター制御を行うためのソフトウェアを上記のMCX A156以外にも展開しており、様々なマイコンでモーター制御を行うことができます。 以下のサイトにモーター制御ができるマイコンがまとまっているので、ご参照ください。 モータ制御向けのMCUXpresso SDK モーター制御に関連するコンテンツの要望や改善点などございましたら、以下よりお気軽にお問い合わせください。 NXPジャパン 技術ブログ(日本語) コンテンツ要望・改善アンケート – フォーム​に記入する =========================​ 本投稿の「Comment」欄にコメントをいただいても、現在返信に対応しておりません。​ お手数をおかけしますが、お問い合わせの際には「NXPへの技術質問 - 問い合わせ方法 (日本語ブログ)」をご参照ください。​ (既に弊社NXP代理店、もしくはNXPとお付き合いのある方は、直接担当者へご質問いただいてもかまいません。)​ NXPのモーター制御に関わる記事をまとめたページです。 MCXマイコンをベースに、モーター制御の基礎から、実際の動かし方まで丁寧に解説しています。 是非、ブックマークを! MCUXpresso MCUXpresso IDE MCUXpresso SDK MCX Motor Control SW | Downloads 日本語ブログ
記事全体を表示
NXP Motor Control - Summary Page - (Japanese blog) This page compiles articles related to NXP's motor control. Based on the MCX microcontroller, this book provides a detailed explanation of everything from the basics of motor control to how to actually operate it.   Motor control using NXP microcontroller " MCX " Planned release category article Basics [Basics Part 1] A complete explanation of the development environment! Read this and you won't get lost! [Basics Part 2] Mechanism and control of permanent magnet motors [Basics Part 3] Mechanism and control method of permanent magnet synchronous motor [Basics Part 4] Practice! Let's see how vector control works using a block diagram! Practical Edition VSCode environment (Using FRDM-MCXA153 ) IDE environment (Using FRDM-MCXA156 ) TBD [Practical Part 1] Mechanism and control method of permanent magnet synchronous motor TBD [Practical Part 2] Mechanism and control method of permanent magnet synchronous motor TBD - NXP offers motor control software for more than just the MCX A156 mentioned above, allowing motor control with a variety of microcontrollers. The following website lists microcontrollers capable of motor control; please refer to it. MCUXpresso SDK for Motor Control If you have any requests or suggestions for improvements regarding motor control-related content, please feel free to contact us using the information below. NXP Japan Technical Blog (Japanese) Content Request/Improvement Survey – Fill out the form =========================== We are currently unable to respond to comments in the " Comment " section of this post . We apologize for the inconvenience, but when making inquiries, please refer to " How to contact NXP with technical questions ( Japanese blog ) " . (If you are already an NXP distributor or have a relationship with NXP , you may contact the person in charge directly. ) This page compiles articles related to NXP's motor control. Based on the MCX microcontroller, this book provides a detailed explanation of everything from the basics of motor control to how to actually operate it. Please bookmark it! MCUXpresso MCUXpresso IDE MCUXpresso SDK MCX Motor Control SW | Downloads Japanese blog
記事全体を表示
NXP电机控制 - 概要页面 - (日语博客) 本页面汇总了与恩智浦半导体电机控制相关的文章。 本书以MCX微控制器为基础,详细解释了从电机控制基础知识到实际操作方法的所有内容。   使用NXP微控制器“ MCX ”的电机控制 计划发布 类别 文章 基础知识 【基础知识(一)】开发环境的完整讲解!阅读本文,您就不会迷路! 【基础知识(二)】永磁电机的机理与控制 【基础知识(三)】永磁同步电机的机理及控制方法 【基础知识 第4部分】练习!让我们用框图来了解矢量控制的工作原理! 实用版 VSCode 环境 (使用FRDM-MCXA153 ) IDE环境 (使用FRDM-MCXA156 ) 待定 【实践部分1】永磁同步电机的机理及控制方法 待定 【实践部分2】永磁同步电机的机理及控制方法 待定 - NXP 提供的电机控制软件不仅限于上面提到的 MCX A156,还支持使用各种微控制器进行电机控制。 以下网站列出了能够进行电机控制的微控制器;请参考该网站。 MCUXpresso 电机控制 SDK 如果您对运动控制相关内容有任何改进要求或建议,请随时使用以下信息与我们联系。 NXP日本技术博客(日语)内容请求/改进调查——请填写表格 =========================== 我们目前无法 回复 此帖子“ 评论”部分的评论。 对于由此造成的不便,我们深表歉意。如有任何疑问, 请 参考“ 如何就 技术问题 联系 NXP ( 日语 博客 ) ” 。 (如果您已经是 恩智浦的 分销商或 与 恩智浦 有业务往来 ,您可以直接联系负责人。 ) 本页面汇总了与恩智浦半导体电机控制相关的文章。 本书以MCX微控制器为基础,详细解释了从电机控制基础知识到实际操作方法的所有内容。 请收藏! MCUXpresso MCUXpresso IDE MCUXpresso SDK MCX 电机控制 SW | 下载 日本博客
記事全体を表示
S32k348 HSE PLL CONFIGURATION how can i get some example codes about how to actually write HSE_CLK_MODE_AND_GSKT_CTRL in UTEST DCF Register,and can i add DCF configuration code directly blow HSE installtion code where blow : if(FALSE == checkHseFwFeatureFlagEnabled()) { /* unlock UTEST data flash sector */ PFLASH_Unlock (PFLASH_BL5, PFLASH_SS0, PFLASH_S0); /* Write in UtestSector using main interface */ status = FLASH_Write ((uint32_t*)UTEST_BASE_ADDRESS, hseFwFeatureFlagEnabledValue, sizeof(hseFwFeatureFlagEnabledValue)); (void)status; // Dummy read to remove warning } thanks alot. Re: S32k348 HSE PLL CONFIGURATION Yes, this method can be used for this purpose the same as shown. I can only offer TRACE32 script I am using for this: ; Program UTEST do ~~\demo\arm\flash\s32k3.cmm prepareonly flash.Program 3. /OTP ;put lines generated by S32K344 DCF configurator data.set 0x1B000780 %QUAD 0x0010000460000101 ;dcf_client_utest_misc flash.Program off Mentioned DCF configurator you may find here: https://community.nxp.com/t5/S32K-Knowledge-Base/S32K344-DCF-Configurator/ta-p/1986243
記事全体を表示
S32K312データフラッシュ – リセットによってプログラム動作が中断されたページの動作 こんにちは、 S32K312(HDQFP100)とRTD 4.4 4.0.0を使用しています。P20およびVector MICROSAR Fee(Fee_30_FlexNor)は、NXP FLSドライバの上に搭載され、データフラッシュでの不揮発性データ保存が可能です。 Vector社は、フラッシュハードウェアの動作に依存する料金に関する問題を報告しました。現在、この現象はルネサスRH850で発生することが確認されている。S32K312が同じ挙動を示すことができるかどうかを確認したいと思います。 シナリオ: データフラッシュ内のページプログラム操作が、意図しないリセットまたは電源喪失によって中断された。 その結果、ページは未定義の状態、つまり完全にプログラムされていない状態になり、適切に消去もされていない状態になる。 その後、このページの空白チェックまたは読み取りは、消去済みとして報告されます(空白チェックのしきい値以下のすべてのセル)。 しかし、後日このページをプログラムしようとすると、実際には完全に消去されていないため失敗する。 質問: S32K312データフラッシュに対する中断されたプログラム操作によって、空白チェックや読み取りが消去されたと報告する状態にして、そのページに対するその後のプログラム操作が失敗する可能性はありますか? このように部分的にプログラムされたページが読み取られた場合、ECC(シングルビット補正またはマルチビットエラー/バスフォールト)によって確実に検出されますか?それともECCエラーなしで消去データとして返されることは CAN ありますか? 推奨される方法はありますか(例:マージン読み取りまたは特定のブランクチェックモード)を使用して、部分的にプログラムされたページを確実に検出できますか? Re: S32K312 Data Flash – behavior of a page whose program operation was interrupted by reset こんにちは、 @S32K-3xx さん。 プログラム操作が中断されると、影響を受けるデータフレーズの内容が保証できません。例えば、データがECCチェックサムと一致しなくなる可能性があります。 しかし、1から0にプログラムされるはずの細胞の中には、わずかにしかプログラムされないものもある。しきい値電圧は定称読み取りしきい値を下回るため、依然として1として読み出されます。したがって、通常の読み取りや白紙のチェックでは、そのフレーズが消去されたと報告される可能性があります。 「このページを後からプログラムしようとすると、完全に消去されていないため失敗する」という記述は正しくありません。後続のプログラム操作では、そのフレーズを再度プログラムし、追加のセルを1から0に変更する可能性がある。ただし、現在の書き込みが対象としていないセルについては、以前に影響を受けたセルは部分的にプログラムされた状態のままになる。 Q: S32K312データフラッシュに対する中断されたプログラム操作によって、空白チェックや読み取りでは消去されたと報告される状態に、その後のプログラム操作が失敗する状態になることはあり得ますか? A: プログラム操作が中断されると、通常の読み取りチェックや空白チェックで消去されたと報告される状態になることがあります。しかし、後続のプログラム操作が失敗するという記述は、一般的には正しくありません。その後の操作により、追加の細胞が1から0にプログラムされる可能性があり、以前に影響を受けた細胞はわずかにプログラムされたままになる可能性がある。 Q: このような部分的にプログラムされたページが読み取られた場合、ECC(シングルビット訂正または多ビットエラー/バスフォールト)によって確実に検出されますか?それともECCエラーなしで消去データとして返すことはできますか? A: ECCでは確実に検出できません。わずかにプログラムされた細胞が名目上の読み取り閾値を下回っている場合、それらは依然として1として解釈される可能性があります。したがって、データとECCビットは消去されたECC整合値として読み取れ、フレーズはECCエラーなしに消去データとして返すことができます。中断された操作によって無効なデータ/ECCコードワードが生成された場合、後続の読み取りでECCエラーが発生する可能性がありますが、ECCエラーが発生しないからといって、ページがクリーンであるとは証明されません。 Q: 推奨される方法はありますか(例:マージン読み取りまたは特定のブランクチェックモード)を使用して、部分的にプログラムされたページを確実に検出できますか? A:はい。消去されたユーザーレベルの余白読み取りが使用可能です(C40_IP_MARGIN_LEVEL_ERASE)。 BR、ダニエル
記事全体を表示
LPC2387/LPC1768 BSDLファイル 境界スキャンテストを作成するためのLPC2387/LPC1768のbsdlファイルはどこで見つけられますか? LPC17xx lpc2000 Re: LPC2387/LPC1768 BSDL file BSDLファイルは公式サイトからは入手できません。リクエストについては、NXP専属FAEにお問い合わせください。
記事全体を表示
i.MX8X DMAコントロール(コプロセッサによる) 私のグループは以前、i.MX7を古いチップ(VF61)の代替候補として検討していました。しかし、コプロセッサにはDMA制御のサポートがなかったため、その設定はやめました。i.MX8Xにも同様の問題が発生するかどうか、ご存知の方はいらっしゃいますか? i.MX 8ファミリ | i.MX 8QuadMax (8QM) | 8QuadPlus Re: i.MX8X DMA Control From Coprocessor i.MX 8Xファミリには、i.MX7で遭遇したようなDMAコプロセッサの制限は**ありません**。重要な違いは以下のとおりです。 i.MX8Xデバイスには、専用のCortex-M4Fコアに加え、Tensilica HiFi 4 DSPとCortex-A35アプリケーションコアが搭載されています。さらに重要なのは、i.MX8XのDMAアーキテクチャが、Cortex-A35コアとCortex-M4Fの両方から完全にアクセス・制御可能な適切なSDMA(Smart DMA)コントローラを基盤としており、リソース分離はリソースドメインコントローラ(RDC)によって管理されていることです[^1]。 対照的に、i.MX7のコプロセッサ(Cortex-M4)はメインDMAエンジンを直接制御できなかったため、チームはそれをやめざるを得ませんでした。i.MX8Xでは、M4コアがSDMA経由でDMA転送を独立して設定・トリガーでき、RDCではドメインごとにDMAチャネルをパーティション分割できるため、A35コアとM4が互いに干渉しないようにできます[^2]。 PCIe に関しては、i.MX8 ファミリーは Root Complex モードと Endpoint モードの両方で DMA をサポートしており、NXP は i.MX8M Plus および i.MX8MM 上で DMA 転送が PCIe 上で動作することを確認しています。参照コードは Linux カーネルで入手可能です(DW PCIe コントローラドライバーの「imx6_pcie_local_dma_start」を参照)[^3][^4]。 ですので、i.MX8XをVF61の代替として検討しているなら、DMA面では十分に準備が整っているはずです——ただし、BSP/カーネル版がSDMAドライバと、目的のメモリ/ペリフェラルパーティションのRDC構成をサポートしていることを確認してください。
記事全体を表示
_FMSTR_StartRec() 会导致竞争条件,具体取决于编译器优化情况。 您好,NXP团队,   freemaster_rec.c 中的 _FMSTR_StartRec() 函数存在问题:   static FMSTR_U8 _FMSTR_StartRec(FMSTR_LP_REC recorder) { ... /* initialize write pointer */ recorder->writePtr = recorder->buffAddr; /* initialize time divisor */ recorder->timeDivCtr = 0U; /* run now in virgin cycle */ recorder->flags.flg.isRunning = 1U; recorder->flags.flg.hasData = 0U; recorder->flags.flg.isVirginCycle = 1U; recorder->flags.flg.isStopping = 0U; return FMSTR_STS_OK; } 虽然乍一看似乎是正确的,但编译器可能会重新排列存储顺序。这 导致竞争条件和对地址 0x0000'0000 的写入访问。   为什么会发生这种情况? FMSTR_Recorder() 由优先级更高的任务调用。如果检测到 isRunning == 1,则将其记录到 writePtr 中的地址。 在源代码中,writePtr 是在 isRunning 之前设置的,因此它应该始终有效。 然而,编译器允许将writePtr 存储移到 isRunning 存储之后。使用 GCC 15.2.1 和 -O2 优化选项,我观察到了这一点:所有标志存储都在最前面,而 writePtr 存储是最后一个。 如果 FMSTR_Recorder() 抢占了运行 FMSTR_Poll() 的(优先级较低的)上下文 在这个窗口中,writePtr 仍然为 0,记录器写入地址 0,MPU 引发 MemManage 故障。   请您调查一下这个问题,并更新代码,使其也能与……兼容。 激进的编译器优化?例如,在……之前设置一个明确的障碍 isRunning 存储可以独立于编译器修复此问题。   FreeMASTER 驱动程序 3.0.11,GCC 15.2.1,-O2。   顺祝商祺!
記事全体を表示
FRDM-i.MX93はM33準備完了以降に起動できません。LinuxとWindowsでUUU SDPSの起動がタイムアウトします。 こんにちは、NXPサポート様 FRDM-i.MX93ボードの復旧についてご協力をお願いします。ボードは、SPLブートの初期段階で「M33 prepare ok」の直後に一貫して停止し、BL31または完全なU-Bootに進みません。UUUリカバリもSDPSの起動中にタイムアウトで失敗します。 役員の詳細: ボード: FRDM-i.MX93 シリアルログに表示されるSoC:0xa1009300 シリアルログに表示されたLC: 0x2040010 PMIC: PCA9451A DDR: 3733MTS 典型的なシリアル出力: U-Boot SPL 2024.04+gde16f4f1722+p0(2024年9月2日 10:44:35 +0000) SOC: 0xa1009300 LC: 0x2040010 PMIC: PCA9451A PMIC: オーバードライブ電圧モード DDR: 3733MTS DDR: 3733MTS M33準備OK 再構築された2025 SPLでも、同じ停止ポイントが発生します。 U-Boot SPL 2025.04 (2026年4月26日 16:21:54 +0000) PMIC: PCA9451A PMIC: オーバードライブ電圧モード DDR: 3733MTS DDR: 3733MTS M33準備OK 使用したハードウェア構成: P1 = 外部電源、45W USB-Cウォールアダプターでテスト済み P16 = デバッグ用シリアルコンソール P13 = microSDカードスロット P2 = UUU / シリアルダウンローダーモード用のUSB-C接続 PCのUSB電源ではなく、壁のコンセント用アダプターから電源を供給するテストも行いました。行動に変化は見られなかった。 テスト対象のホストシステム: Linux Mint / Ubuntu ホスト Windowsホスト UUUのバージョンをテストしました: ううう 1.5.141 ううう 1.5.243 主な問題は、ボードがSPLに到達し、PMICとDDRを初期化して「M33 prepare ok」と出力した後、何も起こらないことです。「Normal Boot」、「Trying to boot from BOOTROM」、「NOTICE: BL31」、または完全な U-Boot に到達しません。これは、SDカードとeMMCの両方から起動した場合に発生します。 USBシリアルダウンローダーモードでは、ボードはUUUによって検出されます。 sudo ./uuu-lsusb コネクテッド Known USB Devices パスチッププロビデオPID Bcdバージョン 5:2 MX93 SDPS: 0x1FC9 0x014E 0x0001 しかし、SDPSの起動中にUUUが失敗します。使用されたコマンドは次のとおりです。 sudo ./uuu-V -b emmc_all imx-boot-imx93frdm-sd.bin-flash_singlebootimx-image-full-imx93frdm.rootfs.wic.zst Linuxでは、以下のエラーが発生します。 開始コマンド:SDPS: boot -scanterm -f imx-boot-imx93frdm-sd.bin-flash_singleboot-scanlimited 0x800000 HID(W)エラー:LIBUSB_ERROR_TIMEOUT Windowsでは、以下のエラーが発生します。 開始コマンド:SDPS: boot -scanterm -f .\imx-boot-imx93frdm-sd.bin-flash_singleboot -scanlimited 0x800000 14% HID(W)エラー: LIBUSB_ERROR_TIMEOUT (-7) これはLinuxとWindowsの両方でテストされ、同じ結果が得られました。 テスト対象画像: NXP公式FRDM-i.MX93 Rev 4.0デモイメージパッケージをテストしました。 LF_v6.6.36-2.1.0_images_FRDM_4.0_IMX93 ブートイメージのハッシュ値は次のとおりです。 7aba6102e5ec64add632cd6667e77fa3f6886fd72c314e4c01f2964c0fc56a5f imx-boot-imx93frdm-sd.bin-flash_singleboot 私は、同じブートイメージハッシュを使用するimx93frdm用の独自のYoctoイメージもテストしました。 SDカードからの起動選択が機能することを確認しました。SDカードが挿入されていないSDブートモードでは、シリアル出力はありません。SDカードを挿入したSDブートモードでは、SPLは起動して「M33 prepare ok」で停止します。SDカードブートスイッチは正常に動作しているようです。 また、公式のNXP .wicファイルも確認しました。イメージには、想定される32 KiB / 0x8000オフセットにブートイメージが含まれています。使用したコマンド: WIC=nxp.wic BOOT=imx-boot-imx93frdm-sd.bin-flash_singleboot xxd -l 64 -s $((32*1024)) "$WIC" xxd -l 64 -s 0 "$BOOT" cmp -n "$(stat -c%s "$BOOT")" -i $((32*1024)):0 "$WIC" "$BOOT" && echo "NXP WICには32Kにブートイメージが含まれています" || echo "NXP WICには32Kにブートイメージは含まれていません" 結果: NXP WICには32Kのブートイメージが含まれています つまり、SDカードイメージにはブートコンテナが正しく含まれているようです。 2024.04 SPLイメージだけが問題の原因ではないことを確認するため、Flexbuild/U-Bootを使用してより新しいブートイメージを作成しました。構築されたイメージ内のSPLは以下を示します。 U-Boot SPL 2025.04 (2026年4月26日 16:21:54 +0000) NXP FRDM-IMX93 私は以下の方法で、この新しい flash.bin ファイルを SD カードの 32 KiB オフセットに書き込みました。 sudo dd if=flash-imx93frdm-2025.bin of=/dev/sdX bs=1K seek=32 conv=fsync 同期 その後、ボードは新しいSPLバナーを印刷し、新しいSDブートイメージが実行されていることを確認した。 U-Boot SPL 2025.04 (2026年4月26日 16:21:54 +0000) PMIC: PCA9451A PMIC: オーバードライブ電圧モード DDR: 3733MTS DDR: 3733MTS M33準備OK しかし、それでも同じ箇所で停止し、BL31/完全なU-Bootまでは進みませんでした。 eMMCの状態: 当初、eMMCはLinuxログイン画面まで起動したが、ルートファイルシステムに/bin/shが存在しなかったため、rootログインができなかった。復旧試行中に、eMMCは.wicファイルを使用してSD Linuxから書き換えられた。画像。その後、eMMCブートも「M33 prepare ok」の後に停止します。しかし、同じ停止ポイントは、公式のNXPイメージを使用したSDブートと、再構築された2025 SPLを使用した場合にも発生するため、現在の問題はLinux/rootfsよりも前の段階にあるようです。 私が除外されたと考えること: シリアルポートが間違っています:シリアル接続は正常に動作し、SPL出力が表示されます。 PCの電源不良:45Wの外部ACアダプターを使用してテストしました。 SDカードブートスイッチの設定が間違っています:SDカードが入っていない状態でSDブートモードにすると、何も出力されません。 SDイメージにブートイメージがありません:公式NXP WICの0x8000 / 32 KiBにブートイメージが存在することが確認されています。 Linux/rootfsの問題:BL31/完全なU-Boot/Linuxの前に障害が発生します。 UUUのホストOSの問題:UUU SDPSの起動タイムアウトがLinuxとWindowsの両方で発生します。 古い2024 SPLだけが問題で、再構築された2025.04 SPLも「M33 prepare ok」の後に停止します。 これがFRDM-i.MX93の既知の早期起動問題かどうか、確認にご協力いただけますでしょうか? Re: FRDM-i.MX93 cannot boot past M33 prepare ok; UUU SDPS boot times out on Linux and Windows 弊社がリリースしたBSPバージョンをご利用ですか? i.MXアプリケーション・プロセッサ向け組み込みLinux | NXP Semiconductors どのバージョンのBSPを使用していますか?また、どのバージョンを選択していますか? 労働者の休暇から戻り次第、当社のボードでテストしてみます。来週の水曜日にオフィスに戻り、テストを実施してから、テスト結果をご報告します。 素敵な一日をお過ごしください よろしくお願いいたします。 リタ Re: FRDM-i.MX93 cannot boot past M33 prepare ok; UUU SDPS boot times out on Linux and Windows 私はLF_v6.6.36-2.1.0_images_FRDM_4.0_IMX93を使用しています。画像 Re: FRDM-i.MX93 cannot boot past M33 prepare ok; UUU SDPS boot times out on Linux and Windows オフィスに戻りましたので、ボードでテストを行い、結果をお知らせします。 Re: FRDM-i.MX93 cannot boot past M33 prepare ok; UUU SDPS boot times out on Linux and Windows 解決できましたか? Re: FRDM-i.MX93 cannot boot past M33 prepare ok; UUU SDPS boot times out on Linux and Windows @Rita_Wang 同じ問題が発生しています... 私のimx-image-full-imx93frdm.rootfs-20260705225501.wic.zst scarthgapビルドでは、BL31が起動を開始したのを見たことがありません。 Re: FRDM-i.MX93 cannot boot past M33 prepare ok; UUU SDPS boot times out on Linux and Windows こんにちは、私も現在同じ問題を抱えています。解決策は見つかりましたか? Re: FRDM-i.MX93 cannot boot past M33 prepare ok; UUU SDPS boot times out on Linux and Windows 私も同じ問題に直面しています。imx93がSDカードから起動できるようにするため、あるいはUUUが正常に動作するようにするために必要なアップデートはありますか? Re: FRDM-i.MX93 cannot boot past M33 prepare ok; UUU SDPS boot times out on Linux and Windows 他のFRDM-IMX93ボードと比較して、このボード上のメモリチップに違いがあることに気づきました。これが問題解決の手がかりになるかもしれません。 正常に動作する基板にはミクロン製の部品が使われており、故障した基板には見覚えのないメーカーの部品が使われている。   IMG_7007.jpeg   Re: FRDM-i.MX93 cannot boot past M33 prepare ok; UUU SDPS boot times out on Linux and Windows 私も同じ問題を抱えています。 これは最優先事項としてマークされるべきです。なぜなら、唯一正常に動作するイメージは、工場出荷時にeMMCに搭載されているものだけだからです!もしそれを再フラッシュしたら、解決策が見つかるまでFRDM-IMX93は起動不能な状態になってしまうでしょう。これはDDRメモリのタイミングに関係しているのではないかと疑っています。 私も「M33 prepare ok」で止まってしまいます。これはDDRの設定/タイミングの問題を示しています。私のボードにも、上記の@SynchronicITさんの投稿と同じ「ノーネーム」DDR IC(メーカーロゴに「J」が付いているもの)が搭載されています。 なぜこれらのボードはeMMCで動作イメージを付けて出荷できるのに、NXPのどの画像も動作しないのでしょうか? 工場出荷時に搭載されているeMMC経由で起動した場合(これは正常に動作します)、u-bootのバージョンは以下のとおりです。 U-Boot SPL 2025.04-g99518e6b6f20(2026年2月2日 05:52:54 +0000) 一方、NXPからの最新ダウンロード(LF_v6.6.36-2.1.0_images_FRDM_4.0_IMX93.zip)では、「imx-boot-imx93frdm-sd.bin-flash_singleboot」ファイルはU-Bootのバージョンです。 U-Boot SPL 2024.04+gde16f4f1722+p0(2024年9月2日 10:44:35 +0000) 以下は、正常に動作する工場出荷時イメージのeMMC、および動作しないSDカードに書き込まれたイメージとUUUにアップロードされたu-bootからの完全な出力です。 * 動作確認済み(工場出荷時設定のeMMC搭載) * U-Boot SPL 2025.04-g99518e6b6f20(2026年2月2日 05:52:54 +0000) PMIC: PCA9451A PMIC: オーバードライブ電圧モード DDR: 3733MTS DRAM 2CS_2GB DRAMが一致しました M33準備OK 通常起動 BOOTROMから起動しようとしています ブートステージ:プライマリブート 画像オフセット 0x8000、ページサイズ 0x200、IVT オフセット 0x0 ROM_APIを使用して0x57800からイメージをロードします 通知:TRDC初期化完了 お知らせ:BL31:v2.12.0(リリース):lf-6.18.2-1.0.0 お知らせ:BL31:製造日時:2026年2月10日 07:53:18 U-Boot 2025.04-g99518e6b6f20(2026年2月2日 05:52:54 +0000) リセットステータス: POR CPU:NXP i.MX93(52) Rev1.2 A55、1700 MHz CPU:インダストリアル温度グレード(-40°Cから105°C)、24°Cで対応 モデル:NXP FRDM-IMX93 DRAM:2 GiB ボード:V1.0(ADC2:684、ADC3:271) TCPC:ベンダーID [0x1fc9]、製品ID [0x5110]、Addr [I2C2 0x52] 無駄。CC1でのPower3.0 PDO 0:タイプ0、5000 mV、3000 mA [E] PDO 1:タイプ0、9000 mV、3000 mA [] PDO 2:タイプ0、12000 mV、3000 mA [] PDO 3:タイプ0、15000 mV、3000 mA [] PDO 4:タイプ0、20000 mV、3250 mA [] PDO 5:タイプ3、未定義 PDO 4を要請:20000 mV、750 mA ソース受理リクエスト PDソース準備完了! tcpc_pd_receive_message:ALERTレジスタのポーリング、TCPC_ALERT_RX_STATUSビット失敗、ret = -62 TCPC:ベンダーID [0x1fc9]、製品ID [0x5110]、Addr [I2C2 0x50] コア:229デバイス、32 uクラス、devicetree:別々 MMC: FSL_SDHC: 0, FSL_SDHC: 1 MMCからの読み込み環境...MMC(0)から読み上げています... *** 警告 - CRCが悪い、デフォルト環境を使用しています ビデオリンクの設定に失敗しました 掲載: シリアル 出力: シリアル エラー: シリアル ビルド情報: - ELEファームウェアバージョン2.0.5-7a34cee パーティション#0に切り替える、OK MMC0(パート0)は電流装置です UID: 4a7ff07fa81b46d8b2b59146dfa5af84 フラッシュターゲットはMMC:0です ネット:eth0: ethernet@42890000、eth1: ethernet@428a0000 [プライム] 速攻:通常 通常起動 自動起動を停止するには、任意のキーを押してください: 0 u-boot=> *動作しません* U-Boot SPL 2024.04+gde16f4f1722+p0(2024年9月2日 10:44:35 +0000) SOC: 0xa1009300 LC: 0x2040010 PMIC: PCA9451A PMIC: オーバードライブ電圧モード DDR: 3733MTS DDR: 3733MTS M33準備OK - 下がる - Re: FRDM-i.MX93 cannot boot past M33 prepare ok; UUU SDPS boot times out on Linux and Windows 私も同じ問題を抱えています。uuu.exe を介してイメージをフラッシュしようとしましたが、同じ「HID(W): LIBUSB_ERROR_TIMEOUT (-7)」エラーが出力されました。私のマザーボードに搭載されているメモリチップも、Micron製ではなく「J」ブランドのものです。 Re: FRDM-i.MX93 cannot boot past M33 prepare ok; UUU SDPS boot times out on Linux and Windows DDR部品の具体的な情報を含むデータポイントをもう一つ追加します。というのも、交換されたメモリがこの問題の鍵となると思うのですが、正確な部品番号がまだ投稿されていないからです。 **ボード:** FRDM-IMX93、SCH-94611 REV B2(ボードラベルDRQ30063390) **SoC:** i.MX93(52) Rev1.2,SOC 0xa1009300、LC 0x2040010 **PMIC:** PCA9451A **DDR:** 回路図およびユーザーガイドにはLPDDR4x Micron MT53E1G16D1FW-046と明記されています。私の基板に実際に取り付けられているチップには、**JSL4BAG167ZAMF**と刻印されています。Micron製ではありません。これはこのThreadの他の方々の報告と一致しています(故障している基板には「J」ブランドのDDRと、動作する基板ではMicronが比較的)。 **失敗はDDRのトレーニング・検証時に発生しており、M33/ELEではありません。**正常に動作する工場出荷時のeMMCブートと、失敗するパブリックBSPブートを比較すると、次の点が明らかになります。 動作中 (工場出荷時の eMMC、U-Boot SPL 2025.04、BL31 lf-6.18.2-1.0.0): 「`」 DDR: 3733MTS DRAM 2CS_2GB DRAMが一致しました M33準備OK 通常起動 ... 「`」 失敗しました (LF_v6.6.36-2.1.0一般公開、U-Boot SPL 2024.04 2024年9月2日): 「`」 DDR: 3733MTS DDR: 3733MTS M33準備OK - 下がる - 「`」 正常に動作するブートローダーには「found DRAM ... DRAM matched」という行が存在し、不具合のあるブートローダーには存在しない。ボードは、DDR検証が完了する直前、つまりBL31/通常起動の直前のまさにその時点で停止します。これは6.6.36のようです。DDRの構成/タイミングが、交換されたDDR部品と一致していないことが原因であり、下流側の何かに問題があるわけではありません。 動作するeMMCブートローダーは、6.6.36パッケージに付属しているものよりも新しいELEファームウェアバージョン2.0.5-7a34ceeも報告しており、修正がDDRのタイミングとELEバージョンの両方に依存している可能性があることに注意してください。 **私が除外できたこと**、サイクルを節約するために: - ビルド:私自身のYocto imx93frdmビルドは、純正パッケージ(sha256 7aba6102e5ec64add632cd6667e77fa3f6886fd72c314e4c01f2964c0fc56a5f)と同一バイトのimx-bootを生成するため、これはビルドの問題ではありません。 - SD フラッシュ: ブート コンテナが 32 KiB (0020 0287 マジック) で検証され、有効な MBR (55aa) とブートローダー領域に 2 つの FIT マジックが確認されました。 - ブートスイッチ:カードなしのSDモードではシリアル出力はなし;カードを使うとSPLが動作するため、USDHC2/SDの選択は正しいです。 - 電源:複数のUSB-C電源で同じ結果が得られました。 - ホスト/USB:UUU SDPSの起動がHID(W: LIBUSB_ERROR_TIMEOUT)で2つの異なるLinuxホストで失敗します。 - ハードウェア自体は良好です。ボードは工場出荷時のeMMCイメージをLinuxに起動するため、DDRは正しいブートローダーで学習可能です。 **質問:** 1.非Micron (JSL4BAG167ZAMF) DDRを搭載したREV B2ボード用の修正済みDDR構成は、現在公開されているBSP(例:)で入手できますか?6.6.52-2.2.0 または 6.12.x リリースでしょうか?それとも、現在 eMMC に搭載されている新しいブートローダー (lf-6.18.2) のみでしょうか? 2. もしまだ公開されていない場合、更新されたFRDM-IMX93のDDRタイミングヘッダー(またはlf-6.18.2 FRDMブートローダー)を公開して、SO、これらのボードでSDブートが動作するようにすることは可能でしょうか? もしそれが問題の絞り込みに役立つのであれば、このボード上で診断を実行したり、候補となるブートローダーやタイミング設定をテストしたりしても構いません。 ありがとう、 jjudk Re: FRDM-i.MX93 cannot boot past M33 prepare ok; UUU SDPS boot times out on Linux and Windows 同じ基板とDDRの組み合わせを持つ他の方の参考になればと思い、このThreadで動作する解決策を続けてお知らせします。 概要:この失敗は公開版LF6.6.36-2.1.0におけるDDRトレーニングの問題です。代替品(Micron製ではない)LPDDR4Xを搭載した新しいFRDM-IMX93ボード上のBSP。**LF6.18.2 (Whinlatter) BSP** に移行すると問題が解決し、ボードが DDR をトレーニングして SD から正常に起動します。 ボード/DDR(参考用): - FRDM-IMX93、SCH-94611 REV B2 回路図ではMicron MT53E1G16D1FWが指定されていますが、実際に取り付けられているチップはJSL4BAG167ZAMF(Micron製以外の代替品)と表示されています。 根本原因を確認できたのは、工場出荷時のeMMCイメージ(正常に起動する)が新しいブートローダーであるU-Boot SPL 2025.04を使用していることだった。BL31 lf-6.18.2、カーネル6.18.2 — そのSPLは`M33 prepare ok`の前に`found DRAM 2CS_2GB DRAM matched`と出力します。公開LF6.6.36SPL (U-Boot 2024.04)`DRAM matched` と表示されず、`M33 prepare ok` でハングアップします。つまり6.6.36DDR構成ではこの置換メモリはトレーニングされませんが、6.18.2構成ではトレーニングされます。 作業手順 — LF6.18.2 BSPをビルドして起動する: mkdir imx-bsp-6.18.2 && cd imx-bsp-6.18.2 リポ init -u https://github.com/nxp-imx/imx-manifest-b imx-linux-whinlatter -m imx-6.18.2-1.0.0.xml リポジトリ同期 DISTRO=fsl-imx-xwayland MACHINE=imx93-11x11-lpddr4x-frdm ソースソース/meta-imx/tools/imx-setup-release.sh -b build-frdm Bitbake imx-image-core   マシン名は `imx93-11x11-lpddr4x-frdm` であることに注意してください(これはこの BSP に固有のもので、6.6.36 とは異なり、別途 meta-imx-frdm レイヤーは必要ありません)。 その後、得られた「.wic.zst」をSDにフラッシュし(解凍とddをカードデバイス全体にフラッシュ)、起動スイッチをSDに設定(SW1 = 1 1 0 0)して起動します。REV B2 / JSL DDRボードでの結果: DDR: 3733MTS DRAM 2CS_2GB DRAMが一致しました M33準備OK 通常起動 ... NXP FRDM-IMX93 ログイン: `free -h`を実行すると、2GBすべてが学習済みで利用可能であることが確認できます。 NXPへの質問:更新されたFRDM DDR構成をLF6.6.36-2.1.0にバックポートする予定はありますか?6.6.36 を使い続ける必要がある人向けのブランチですか?バージョン6.18.2に移行できる場合は、上記の手順で問題ありません。 これが誰かのデバッグ作業の手間を省くのに役立てば幸いです。 jjudk Re: FRDM-i.MX93 cannot boot past M33 prepare ok; UUU SDPS boot times out on Linux and Windows こんにちは、リタさん。 この件について何か進展はありますか?私たちはこの問題で行き詰まっています。 Re: FRDM-i.MX93 cannot boot past M33 prepare ok; UUU SDPS boot times out on Linux and Windows デモイメージ LF_v6.6.36-2.1.0_images_FRDM_4.0_IMX93 を使用して、当社の 93FRDM REV B バージョンボードでテストを行いました。 UUU uuuツールリリースuuu_1.5.243を使用する SDカードとeMMCの両方で試しましたが、ダウンロードと起動は成功し、発生したエラーは再現されませんでした。 .\uuu.exe -b emmc_all imx-boot-imx93frdm-sd.bin-flash_singlebootimx-image-full-imx93frdm.rootfs.wic.zst Rita_Wang_0-1786613213510.png Rita_Wang_1-1786613222942.png Rita_Wang_2-1786613231468.png Re: FRDM-i.MX93 cannot boot past M33 prepare ok; UUU SDPS boot times out on Linux and Windows どうやら問題はREV B2基板にあるようです。REV B2で試してみてください Re: FRDM-i.MX93 cannot boot past M33 prepare ok; UUU SDPS boot times out on Linux and Windows やあみんな。 @Rita_Wangこれは「改訂」の問題ではありません。私の机の上には、同じリビジョンだがLP DDRチップが異なる2枚の基板がある。 しかし、あなたのチームからの発言をそのまま引用すると次のようになります。 LFU-956 imx93_frdm:i.MX93 FRDM に 2CS 2GB DRAM サポートを追加する FRDM-IMX93はMicron MT53E1G16D1FW 1CS DRAMを使用していますが、これは既に生産終了しています。JSC JSL4BAG167ZAMF 2CS DRAMに交換します。そして1G DRAMのサポートは終了します。 i.MX93 FRDM上で2CS 2GB DRAMサポートを追加し、1CSおよび2CS DRAMベースのFRDM-IMX93ボードの両方をサポートします。 それは、添付画像にある2つのチップのタイミングパラメータが異なるためです。@jjudkさんが述べたように、これを機能させる唯一の方法は、ファームウェアを新しいバージョンにアップデートすることです。 私の場合は、古いものを使い続けなければなりませんでした。そのため、必要なパッチだけを抜き出しました(こちらをご覧ください: https://github.com/nxp-imx/uboot-imx/commit/4c35a6086aedca2f6220382920242ad81ae372f6 )。 もしまだ問題がある方がいれば、完成品をお渡しできます。 -ガブリエレ Re: FRDM-i.MX93 cannot boot past M33 prepare ok; UUU SDPS boot times out on Linux and Windows こんにちは、 私はJSC LPDDR4Xメモリ(JSL4BAG167ZAMF)を搭載した同じFRDM-i.MX93 REV B2ボードを持っています。 公式のLF6.6.36-2.1.0 SDカードイメージ( .wic.zstルートファイルシステムイメージとそれに対応するimx-bootバイナリを含む)をテストしました。また、フラッシュされたイメージを検査し、ブートローダーが期待どおりに書き込まれていることを確認しました。しかし、シリアルコンソールは常にM33 prepare okで停止し、通常のブートには到達しません。 これは古いファームウェアに欠けていた2CS/2GB DDRサポートとLF6.18.2に更新されたサポートが含まれていることに関連していると理解しています。 既成イメージがあるとおっしゃっていたので、このボード用の対応するブートローダー付きのテスト済みSDカード.wic.zstイメージをぜひ共有してもらえますか?Yocto BSP全体を再構築せずに、直接検証したいです。 よろしくお願いします。 Re: FRDM-i.MX93 cannot boot past M33 prepare ok; UUU SDPS boot times out on Linux and Windows はい。今、不在です。動作する.wic.zstファイルは月曜日に添付します。 Re: FRDM-i.MX93 cannot boot past M33 prepare ok; UUU SDPS boot times out on Linux and Windows こんにちは。月曜日に共有していただいた、事前に構築済みのイメージについて、確認させてください。都合のいいときに共有してもらえますか? サポートありがとうございます。
記事全体を表示
MIMXRT1180-EVK - 安全配置工具擦除 FlashSPI1 卡住 你好, 我在将镜像加载到 MIMXRT1180-EVK 时遇到问题。IDE - 最新版 VS Code,SDK 最新版(安装于 2026 年 11 月 24 日),安全配置工具 26.09 在将任何(经过多次测试的)示例导入 VS Code 或 MCUX IDE 后,无论使用何种配置,我都已配置安全配置工具以使用正确的 MCU(通信已验证 - 测试通过)和正确的内存(默认 U12 W25Q128JWSIQ - 测试通过)。 高效密码学标准(SEC)工具已成功生成镜像的版本(所有设置均为默认值,源文件为 .elx)。文件),但“写入图像”按钮操作总是导致 30 分钟以上的待处理事件: blhost -t 240000 -p COM16,57600 -j -- flash-image --base-offset 0x38001000 "imagepath_cm7.simg" erase 0 即使超过 30 分钟,此事件也只能通过用户交互来终止。 我尝试过全新安装工具,也尝试过多个示例,但都无济于事。 板上可以直接调试与 RAM 连接的程序。 请看图片。 Re: MIMXRT1180-EVK - Secure provisioning tool Erase FlashSPI1 A stuck 如果按照 i.MX RT1180 参考手册第 10 版(2026 年 5 月 14 日)中的表 4. 系统内存映射 (CM7) 使用 0x38000000 或 0x28000000,则可以使用安全配置工具擦除和编程设备。(启动尚未成功) Re: MIMXRT1180-EVK - Secure provisioning tool Erase FlashSPI1 A stuck 示例似乎将 m_flash_start 设置为 0x28800000 而不是 Flash 编程器检查所宣传的 0x38000000。 SDK mimxrt1180_evk 26.09.00 Re: MIMXRT1180-EVK - Secure provisioning tool Erase FlashSPI1 A stuck 嗨@TrenzT 高效密码学标准(SEC)工具中的内存检查不会验证写入或擦除操作,因此结果可能无效。 能否在命令行界面 (CLI) 中使用 -v 或 -vv 或 -vvv 参数运行有问题的 blhost 命令,以便记录操作详情?如果失败,能否附上日志文件? blhost -vv -t 240000 -p COM16,57600 -j -- flash-image --base-offset 0x38001000 "imagepath_cm7.simg" erase 0 操作时间很大程度上取决于应用程序的大小。如果你的应用程序比较大,能否先尝试一些小的例子,比如像“Hello World”这样的示例项目?或许你已经成功了,因为从你的第二张截图来看,似乎已经成功了。请与我们联系:哪些图片通过了审核,哪些图片审核失败了? 您还可以尝试主菜单 > 工具 > 闪存编程器来检查对闪存的访问权限。 关于图像地址,0x2800_000 和 0x3800_0000 都是 SPI NOR 闪存的地址。第一个别名不安全,第二个别名安全。在配置过程中,只能使用安全别名,但应用程序可以构建为 0x2800_xxxx。我看不出地址有什么问题。
記事全体を表示
NTB0104 VCC(A) = 1.2 V で 30 Mbps 信号で:このアプリケーションは適しているのでしょうか? こんにちは、 私は論理レベル変換のためにNTB0104を評価しています。VCC(A) = 1.2Vでの動作について懸念があり、この部品が私のアプリケーションに適しているか確認したいです アプリケーション: - VCC(A) = 1.2 V、許容範囲±3% - VCC(B) = 1.8V から 3.3V、許容差±3%)、最終ユーザーが設定する可変電圧 - 信号:プッシュプル出力、30 Mbps - 周囲温度:-40°Cから+85°Cまで、インダストリアル全温度範囲 私の懸念事項: 1. 表6では最小VCC(A)として1.2Vが示されていますが、電源許容値やリップルに対するマージンはありません。 2. 表9は、VCC(A) = 1.4 V~3.6 Vの場合のみVOHとVOLを規定しています。1.2 Vの場合、表7には25 °Cにおける代表的な値のみが示されている。 3. 表11および12は、VCC(A) = 1.5 V ± 0.1 Vからの保証されたタイミングのみを示しています。1.2Vの場合、表10には25℃における代表的な値(70Mbps~80Mbps)のみが記載されています。表11および表12の最低保証データレートは40 Mbpsで、VCC(A) = 1.5 Vです。30 Mbpsは典型的な1.2 Vより低いですが、保証値が見つかりません。 質問: 1.NTB0104はVCC(A) = 1.2 Vで、-40°Cから+85°Cまでの全温度範囲で完全にサポートされますか? 2. NXPはVOH、VOL、tpd、tt、および最大データレートに対してVCC(A) = 1.2 Vの保証(最小/最大)値を供給できますか? 3. VCC(A) = 1.2 V での 30 Mbps はサポートされている CASE ですか?そうでない場合、推奨される最大データ転送速度はどれくらいですか? 4. VCC(A) = 1.2 V の場合、どの程度の電源電圧許容範囲が認められますか?例えば、1.14 V (1.2 V − 5 %) での動作は許容されますか? 5.もしNTB0104が適さない場合、このアプリケーションにおすすめの他の製品は何でしょうか? よろしくお願いします。 Re: NTB0104 at VCC(A) = 1.2 V with a 30 Mbps signal: is this application suitable? ジユエさん、こんにちは。 NTB0104はVCC(A) = 1.2Vで動作可能ですが、1.2Vは推奨される最小動作電圧です。公称1.2 Vの電源で±3%の許容差があると、推奨動作範囲外となる1.164 Vまで低下することがあります。1.14Vでの動作も同様に保証されません。 1.2Vの場合、VOH、VOL、および動的性能については、25℃における代表値のみを提供します。AポートのVOH/VOLの保証範囲は1.4Vから始まり、タイミングとデータレートの保証範囲は1.5V±0.1Vから始まる。 現在、製品チームにVCC(A) = 1.2 Vでの動作に関する追加の特性評価データや保証限界が利用可能なかどうか確認しています。フィードバックが入り次第、このThreadを更新します。 BRs、トーマス
記事全体を表示
Looking for infos about a NXP keyboard Hi I'm looking for technical infos about the showed item, first of all the item name and/or id code LPC1768FBD100 seems to be the embedded processor, it looks like NXP produced the whole keyboard, I'm looking for infos about connecting it and communication protocol Re: Looking for infos about a NXP keyboard Hi @Laverdanny  The unit appears to be a CNC/machine control panel. However, the picture is not sufficient to identify the exact part number or interface. LPC1768FBD100 is the MCU used in the unit and supports multiple interfaces, so the communication protocol cannot be determined from the MCU alone. BR Harry
記事全体を表示
未找到程序“make” 我将一个最初在 Kinetis Design Studio 中创建的项目导入到 MCUXpresso 中时,遇到了一个错误,提示在 PATH 中找不到程序“make”。该项目是针对 KE06 微控制器的。这是IDE的问题还是项目的问题?我该如何解决? Re: Program "make" not found 你好@JJanecek12 , 感谢你的帖子。 错误“在 PATH 中找不到程序‘make’”表示 MCUXpresso IDE 在构建项目时找不到 make 工具。 由于该项目最初是用 Kinetis Design Studio 创建的,然后导入到 MCUXpresso IDE 中,因此这很可能与项目配置有关。 首先,请在 MCUXpresso IDE 中创建一个新的 KE06 项目或导入一个类似“hello world”的示例演示,然后尝试构建它。 如果新项目构建成功,MCUXpresso IDE 安装应该也没问题。在这种情况下,请查看以下旧项目: 项目 > 属性 > C/C++ 版本 > 环境 请检查 PATH 变量是否已定义或被覆盖。您可以参考已解决:回复:MCUXpresso 错误:在 PATH 中找不到程序“make” - NXP 社区。 如果可以的话,我建议在 MCUXpresso IDE 中创建一个新的 KE06 项目,然后将源文件和必要的设置从旧的 KDS 项目移动到新项目中。这样可以避免因旧的 KDS 项目设置而导致的兼容性问题。 您可能对从 Kinetis Design Studio 迁移到 MCUXpresso IDE感兴趣。 如果新的 KE06 项目也报告相同的“make”错误,请告知您的 MCUXpresso IDE 版本并提供完整的构建日志。我可以帮忙进一步核实。 BR 塞莱斯特
記事全体を表示