PlatformIOでRA4M1の開発環境を構築

## 1. はじめに
Arduino-Uno R4で使われているRA4M1の開発環境は、Arduino-IDE以外にもVSCode+PlatformIOを使うことができます。

## 2. プロジェクトの作成
PlatfromIOのHomeから **New Project** をクリックします。

![pio3.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/193997/c82f01e1-e6b3-0c8e-cc37-510924602587.png)

**Board:** をクリックし、ボードのリストを表示します。

![pio4.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/193997/da2db4a2-ffac-c866-8c76-6c31b2b4b6e1.png)


ボードのリストから、 **Arduino Uno R4 Minima** を選びます。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/193997/eeca57d2-f9b0-4aa2-a00b-84b1ea073d53.png)

Frameworkには **Arduino** を選択し、Nameにプロジェクト名を入れ、 **Finish** をクリックします。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/193997/d968cb46-ed59-9788-f8df-897844e94791.png)

完了までに少々時間がかかります。完了後プロジェクトのディレクトリには、以下のようなplatformio.iniが生成されます。

```:platformio.ini
; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:uno_r4_minima]
platform = renesas-ra
board = uno_r4_minima
framework = arduino
```

## 3. テストプログラムのビルドとアップロード
Fspタイマを使ったLEDを点滅させるプログラムです。
```cpp:main.cpp
#include <Arduino.h>
#include <FspTimer.h>

static constexpr uint32_t led_on_ms = 100;
static constexpr uint32_t led_off_ms = 400; 

static FspTimer fsp_timer;

static bool is_led_on;

static void
timer_callback([[maybe_unused]]timer_callback_args_t *arg)
{
  static uint32_t cnt = 0;

  cnt++;
  if (is_led_on) {
    if (cnt > led_on_ms) {
      digitalWrite(LED_BUILTIN, LOW);   // turn the LED off
      is_led_on = false;
      cnt = 0;
    }
  } else {
    if (cnt > led_off_ms) {
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on
      is_led_on = true;
      cnt = 0;
    }
  }
}

void
setup()
{
  while (!Serial) {
    ;
  }
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on
  is_led_on = true;

  uint8_t timer_type;
  int8_t timer_ch = FspTimer::get_available_timer(timer_type);
  if (timer_ch < 0) {
    Serial.println("get_available_timer() failed.");
    return;
  }
  fsp_timer.begin(TIMER_MODE_PERIODIC, timer_type, static_cast<uint8_t>(timer_ch), 1000.0, 25.0, timer_callback, nullptr);
  fsp_timer.setup_overflow_irq();
  fsp_timer.open();
  fsp_timer.start();
  Serial.println("started.");
}

void
loop()
{
  __WFI(); 
}
```
## 4. 動作確認

Upload and Monitorを実行してみます。ターミナルに以下のメッセージが表示され、LEDが点滅することが確認できました。
```
Processing uno_r4_minima (platform: renesas-ra; board: uno_r4_minima; framework: arduino)
-------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/renesas-ra/uno_r4_minima.html
PLATFORM: Renesas RA (0.0.2) > Arduino Uno R4 Minima
HARDWARE: RA4M1 48MHz, 32KB RAM, 256KB Flash
DEBUG: Current (cmsis-dap) External (cmsis-dap, jlink)
PACKAGES: 
 - framework-arduinorenesas-uno @ 1.0.1 
 - tool-dfuutil-arduino @ 1.11.0 
 - toolchain-gccarmnoneeabi @ 1.100301.220327 (10.3.1)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 233 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Checking size .pio\build\uno_r4_minima\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]   7.7% (used 2528 bytes from 32768 bytes)
Flash: [=         ]  13.0% (used 34208 bytes from 262144 bytes)
Configuring upload protocol...
AVAILABLE: cmsis-dap, dfu, jlink
CURRENT: upload_protocol = dfu
Uploading .pio\build\uno_r4_minima\firmware.bin
dfu-util 0.11-arduino4

Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.
Copyright 2010-2021 Tormod Volden and Stefan Schmidt
This program is Free Software and has ABSOLUTELY NO WARRANTY
Please report bugs to http://sourceforge.net/p/dfu-util/tickets/

Opening DFU capable USB device...
Device ID 2341:0069
Run-Time device DFU version 0101
Claiming USB DFU (Run-Time) Interface...
Setting Alternate Interface zero...
Determining device status...
DFU state(0) = appIDLE, status(0) = No error condition is present
Device really in Run-Time Mode, send DFU detach request...
Device will detach and reattach...
Opening DFU USB Device...
Claiming USB DFU Interface...
Setting Alternate Interface #0 ...
Determining device status...
DFU state(2) = dfuIDLE, status(0) = No error condition is present
DFU mode device DFU version 0101
Device returned transfer size 64
Copying data from PC to DFU device

Download        [                         ]   0%            0 bytes
Download        [=                        ]   4%         1408 bytes
Download        [==                       ]   8%         2752 bytes
Download        [===                      ]  12%         4160 bytes
Download        [====                     ]  16%         5504 bytes
Download        [=====                    ]  20%         6912 bytes
Download        [======                   ]  24%         8256 bytes
Download        [=======                  ]  28%         9600 bytes
Download        [========                 ]  32%        11008 bytes
Download        [=========                ]  36%        12352 bytes
Download        [==========               ]  40%        13760 bytes
Download        [===========              ]  44%        15104 bytes
Download        [============             ]  48%        16512 bytes
Download        [=============            ]  52%        17856 bytes
Download        [==============           ]  56%        19200 bytes
Download        [===============          ]  60%        20608 bytes
Download        [================         ]  64%        21952 bytes
Download        [================         ]  65%        22528 bytes
Download        [=================        ]  68%        23360 bytes
Download        [==================       ]  72%        24704 bytes
Download        [===================      ]  76%        26048 bytes
Download        [====================     ]  80%        27456 bytes
Download        [=====================    ]  84%        28800 bytes
Download        [======================   ]  88%        30208 bytes
Download        [=======================  ]  92%        31552 bytes
Download        [======================== ]  96%        32960 bytes
Download        [=========================] 100%        34208 bytes
Download done.
DFU state(7) = dfuMANIFEST, status(0) = No error condition is present
DFU state(2) = dfuIDLE, status(0) = No error condition is present
Done!
--- Terminal on COM14 | 9600 8-N-1
--- Available filters and text transformations: colorize, debug, default, direct, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at https://bit.ly/pio-monitor-filters
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
started.
```