📚 RL78G14 VS Code開発環境の構築を試してみましょう。　LM_1_Code

#### QQQ :bow_tone1:３Q! 
##### [【初心者から】組み込み開発_RL78G14篇　LC1](https://qiita.com/zhb/items/a8749effa03b8dd31b0e)   　:arrow_heading_up:

# VSCode試し理由
- Cursorはe2Studio IDEより軽い
- AIプラグインよりコーディング効率化
:::note info
ソースコードの自動生成およびシミュレーターの設計は、e² Studio IDEに任せます。
:::

# 環境準備
- e2 Studio + LLVM for RL78 环境
    - 编译 & デバッグ环境
    C:\ProgramData\LLVM for Renesas RL78 17.0.1.202406\bin
    - GNU make環境
C:\Renesas\e2_studio\eclipse\plugins\com.renesas.ide.exttools.gnumake.win32.x86_64_4.3.0.v20220121-1024\mk

- VSCode + Renesasプラグイン
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3659233/4ae1158e-89ea-4b2a-a068-4ccd3dfd0190.png)

- CMakeダウンロード
 Portable、インストール不要
https://cmake.org/download/
**Binary distributions:**
| Windows x64 ZIP | [cmake-4.0.2-windows-x86_64.zip](https://github.com/Kitware/CMake/releases/download/v4.0.2/cmake-4.0.2-windows-x86_64.zip) |

- Ninja ダウンロード
 Portable、インストール不要
 https://github.com/ninja-build/ninja/releases
 [ninja-win.zip](https://github.com/ninja-build/ninja/releases/download/v1.12.1/ninja-win.zip)   269 KBMay 11, 2024

- CMake & Ninjaの置き場
　C:\cmake\bin
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3659233/d6aabaa3-14eb-4e31-847a-8ceffc742f24.png)

- システムパスへ追加 
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3659233/ddca8f21-2eaf-4eb8-ab30-493212a2a95d.png)


:::note warn
CS+ IDEでの CC_RL または CA_CX コンパイラ環境は使用できません！
:::

# e² studioプロジェクトを構築
省略、下記リンクをご参照ください。
:point_right:　[LED点消灯を10分で制御させる](https://qiita.com/zhb/items/b2d6463c66d6026c7057)

# VSCodeプロジェクトの変換／作成
- 「Ctrl+Shift+P」より「Create LLVM for RL78 project files」メニューを選択
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3659233/15f17e35-b2d4-4aad-86f5-06520894383f.png)
- 二つファイル（CMakeLists.txtとcross.cmake）を生成
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3659233/1061ad95-b31b-430b-9c86-587f9090a0df.png)

# 不要な .c や .s ファイルを削除
（旧プロジェクトのエントリポイントである main 関数付きの廃棄ファイルも含む）
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3659233/876e8649-4ea1-48d8-b4fe-baad07c75c95.png)

# CMakeLists.txt を編集
Makefile群を生成するため
- プロジェクト名を記入（例：QQQ）
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3659233/1fc53ee3-2630-4132-9869-b9e7d13c280d.png)
- 起動アセンブラファイル（start.S）箇所変更伴い、パス変更対応（📁名変更：generate -> src）
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3659233/9b8cf8e4-7348-4aa6-9c37-9b8905dcd897.png)

# クロスコンパイル設定
cross.cmakeを編集
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3659233/a9c00cb7-99da-4fad-9437-f215acc1f032.png)

# Debug環境を構築
  launch.json の生成と編集
 - ✅simulator（シミュレーター環境でのデバッグ）
 - hardware （Lite版ハードウェア回路でのデバッグ）
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3659233/babc5a15-88f0-43df-8247-cfbadf1fbc1a.png)

![{651D708B-793C-4B62-812B-F204582A2274}.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3659233/20733ca4-0d6f-4c10-a91c-64f6259006d7.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3659233/4ac7f7d9-a279-4cc5-afa0-1f5ebb0ff614.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3659233/18f5f53b-572b-4c02-940a-b09879494791.png)

```json
{
	// IntelliSense を使用して利用可能な属性を学べます。
	// 既存の属性の説明をホバーして表示します。
	// 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387
	//e2-server-gdb -g 
	//SIMULATOR -usecurityIdSize 10 
	//-t R5F104PL -uConnectionTimeout= 30 
	//-umFreq= 2.00 -usFreq= 32.768 
	//-uresetOnReload= 1 
	//-udisplaySimGUI= 1 
	// -uSimulatorSetting= C:\git\vscode\LLVM\QQQ/QQQ Simulator Debug.simpj 
	// -uRelayBreak= 0 -l 
	// -uCore= SINGLE_CORE|enabled|1|main 
	// -uSyncMode= async 
	// -uFirstGDB= main 
	// --gdbVersion= 12.1
	"version": "0.2.0",
	"configurations": [
		// Renesas GDB Simulator configuration
		// This configuration is used for debugging with the Renesas GDB simulator.
		{
			"type": "renesas-simulator",
			"request": "launch",
			"name": "Renesas GDB Simulator Debugging",
			"target": {
				"deviceFamily": "RL78",
				"device": "R5F104PL",
				"debuggerType": "SIMULATOR",
				"serverParameters": [
					"-usecurityIdSize",
					"10",
					"-t",
					"R5F104PL",
					"-uConnectionTimeout",
					"30",
					"-umFreq",
					"2.00",
					"-usFreq",
					"32.768",
					"-uresetOnReload",
					"1",
					"-udisplaySimGUI",
					"1",
					"-uSimulatorSetting",
					"${workspaceFolder}/QQQ Debug.simpj",
					"-uRelayBreak",
					"0",
					"-l",
					"-uCore",
					"SINGLE_CORE|enabled|1|main",
					"-uSyncMode",
					"async",
					"-uFirstGDB",
					"main"
				],
			}
		},
		// Renesas E2Lite emulator configuration
		// This configuration is used for debugging with the Renesas E2Lite emulator.
		{
			"type": "renesas-hardware",
			"request": "launch",
			"name": "RL78G14 E2Lite emulator",
			"target": {
				"deviceFamily": "RL78",
				"device": "R5F104PL",
				"debuggerType": "E2LITE",
				"serverParameters": [
					"-t",
					"R5F104PL",
					"-uConnectionTimeout=",
					"30",
					"-umFreq=",
					"0",
					"-usFreq=",
					"0",
					"-umClock=",
					"1",
					"-w",
					"0",
					"-usupplyVoltage=",
					"0",
					"-ucommMethod=",
					"0",
					"-usecurityID=",
					"00000000000000000000",
					"-usecurityIdSize",
					"10",
					"-upermitFlash=",
					"1",
					"-uuseWideVoltageMode=",
					"0",
					"-ueraseRom=",
					"1",
					"-uresetOnReload=",
					"1",
					"-ustopTimerEmu=",
					"0",
					"-ustopSerialEmu=",
					"0",
					"-umaskInternalResetSignal=",
					"0",
					"-umaskTargetResetSignal=",
					"0",
					"-n",
					"0",
					"-uverifyOnWritingMemory=",
					"1",
					"-uAllowRRMDMM=",
					"0",
					"-uRelayBreak=",
					"0",
					"-l",
					"-uCore=SINGLE_CORE|enabled|1|main",
					"-uSyncMode=",
					"async",
					"-uFirstGDB=",
					"main",
					"--gdbVersion=",
					"12.1"
				],
				"gdbPath": "C:\\Renesas\\e2studio\\2023-10\\RLSimGDB\\bin\\rl78g14_e2lite_gdb.exe",
			]
		}
	}
]
}
```
:::note warn
↑↑↑↑↑　「"${workspaceFolder}/QQQ Debug.simpj",」行にプロジェクト名を更新するべき。例：PROJECT_XXX Debug.simpj
これはe2Studio IDE環境より自動生成したシミュレーター用ファイルです。
:::

# CMake操作の実行
（CMake: キャッシュを削除して再構成）
![{8EC925C9-1056-4FAB-949D-91165794FE23}.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3659233/7853c494-7fec-412a-82df-ff6804bcc118.png)


```console
[main] プロジェクトを構成しています: QQQ 
[proc] コマンドを実行しています: C:\cmake\bin\cmake.EXE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE "-DCMAKE_C_COMPILER:FILEPATH=C:\ProgramData\LLVM for Renesas RL78 17.0.1.202406\bin\clang.exe" "-DCMAKE_CXX_COMPILER:FILEPATH=C:\ProgramData\LLVM for Renesas RL78 17.0.1.202406\bin\clang++.exe" -DCMAKE_TOOLCHAIN_FILE=c:/QQQ/QQQ/cross.cmake --no-warn-unused-cli -SC:/QQQ/QQQ -Bc:/QQQ/QQQ/build -G Ninja
[cmake] Not searching for unused variables given on the command line.
[cmake] -- The C compiler identification is Clang 17.0.1
[cmake] -- The CXX compiler identification is Clang 17.0.1
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - done
[cmake] -- Check for working C compiler: C:/ProgramData/LLVM for Renesas RL78 17.0.1.202406/bin/clang.exe - skipped
[cmake] -- Detecting C compile features
[cmake] -- Detecting C compile features - done
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Check for working CXX compiler: C:/ProgramData/LLVM for Renesas RL78 17.0.1.202406/bin/clang.exe - skipped
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] -- The ASM compiler identification is Clang with GNU-like command-line
[cmake] -- Found assembler: C:/ProgramData/LLVM for Renesas RL78 17.0.1.202406/bin/clang.exe
[cmake] CMake Warning (dev) at CMakeLists.txt:22 (add_custom_command):
[cmake]   Exactly one of PRE_BUILD, PRE_LINK, or POST_BUILD must be given.  Assuming
[cmake]   POST_BUILD to preserve backward compatibility.
[cmake] 
[cmake]   Policy CMP0175 is not set: add_custom_command() rejects invalid arguments.
[cmake]   Run "cmake --help-policy CMP0175" for policy details.  Use the cmake_policy
[cmake]   command to set the policy and suppress this warning.
[cmake] This warning is for project developers.  Use -Wno-dev to suppress it.
[cmake] 
[cmake] -- Configuring done (5.0s)
[cmake] -- Generating done (0.2s)
[cmake] -- Build files have been written to: C:/QQQ/QQQ/build

```
# ビルド開始
 コンパイル／リンクを実行し、.elf／.bin ファイルを生成
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3659233/cf41a8ea-b38c-49f4-89de-6382636a50bb.png)



# デバッグ ＆ 実行 
- 実行ボタン「Renesas GDB Simulator Debugging」を押してデバッグ開始
![{DDA1C964-3022-4C19-B3E3-BF2D760D2569}.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3659233/69607eb7-0c62-4906-8761-b9c975917940.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3659233/9e1ab8a4-3105-42fb-bd1d-5140656f5f95.png)
:::note warn
コード生成後は、必ず再度 CMake 操作（キャッシュ削除 → 再構成）を行ってください。
:::

:::note alert
 残留課題：
     ・ 変な浮動演算　※ 原因不明、しかし、e2Studio環境で算出ＯＫ！
    　　例：uint32_t ulSetParam = (uint32_t) (19999 + 1) * (uint32_t) duty / 100 - 1;　→最終ulDuty → 超大きいな値
:::


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3659233/5445f045-2839-44bc-ba93-533e164b33c1.png)

:interrobang:なぜ、同じClangコンパイラなのに違いが出るのでしょうか？
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3659233/7c4b6207-6575-4e8a-ac32-bfa438aadbdd.png)

##### [【初心者から】組み込み一体開発_RL78G14篇_LC01](https://qiita.com/zhb/items/a8749effa03b8dd31b0e)   　:arrow_heading_up:

##### QQQ :bow_tone1:３Q!