M5Stamp-C3 Arduino Install
2021/12/11
初版
M5Stamp-C3 Arduino Install
M5Stamp-C3 Arduino Install
概要
Arduino-IDEで「M5Stamp C3 Mate」を動かす。本ボードは簡単にいうと従来のESP32のCPUをRISC-V(32bits)CPUに置き換えたものになる。
ホスト環境は、ubuntu20.04とする。
なお、Arduino-IDEの最新版がインストール済みのものとする。
また、platformioでの使用する際のplatformio.iniについても記載する。
linuxのArduino-IDEのインストールについては以下を参照のこと。
・Install the Arduino Software (IDE) on Linux
本記事は、linux版Arduino-IDEについて記述したものであるが、 ほとんど、そのままwindows版でも適応可能である。
USB serial ドライバーの設定
windows10については特別に何かをインストールする必要はなかった。 linuxの場合、udevの設定が必要になるが、platformioでの設定を以下のように流用する。
udev登録
以下を実行して、udevのrulesを登録する:
curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core/master/scripts/99-platformio-udev.rules | sudo tee /etc/udev/rules.d/99-platformio-udev.rules
sudo udevadm control --reload-rules
sudo usermod -a -G dialout $USER
sudo usermod -a -G plugdev $USER
ボード情報(*.json)のインストール
Arduino-IDEを起動して以下を実行する:
1.[ファイル]/[環境設定]を選択する
2.画面の「追加のボードマネージャーのURL」に以下を入力(追加)する
https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/arduino/package_m5stack_index.json
3.入力後、[OK]をクリックする
4.[ツール]/[ボード]/[ボードマネージャー]を選択する
5.検索欄に「M5」と入力する
6.検索結果から「M5STACK」を選び、[インストール]をクリックする
7.インストール後、[閉じる]をクリックする
8.[ツール]/[ボード]/[M5Stack Arduino]/[STAMP-C3]を選択する
以上で、ボードとして[STAMP-C3]が選択されたことになる。
# ASCIITable.inoのようにUSB serialを動かす場合 # ツール設定を以下にする: USB CDC On Boot: "Disable"
なお、シリアルポートは通常「/dev/ttyACM0」になる。
以下、実際に動かしたスケッチを挙げる:
ASCIITableM.ino
/*
ASCII table
Prints out byte values in all possible formats:
* as raw binary values
* as ASCII-encoded decimal, hex, octal, and binary values
For more on ASCII, see http://www.asciitable.com and http://en.wikipedia.org/wiki/ASCII
The circuit: No external hardware needed.
created 2006
by Nicholas Zambetti
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
<http://www.zambetti.com>
*/
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
// while (!Serial) {
// ; // wait for serial port to connect. Needed for Leonardo only
// }
// prints title with ending line break
Serial.println("ASCII Table ~ Character Map");
Serial.println("ASCII Table ~ Character Map");
Serial.println("ASCII Table ~ Character Map");
Serial.println("ASCII Table ~ Character Map");
Serial.println("ASCII Table ~ Character Map");
Serial.println("ASCII Table ~ Character Map");
Serial.println("ASCII Table ~ Character Map");
Serial.println("ASCII Table ~ Character Map");
Serial.println("ASCII Table ~ Character Map");
Serial.println("ASCII Table ~ Character Map");
Serial.println("ASCII Table ~ Character Map");
Serial.println("ASCII Table ~ Character Map");
Serial.println("ASCII Table ~ Character Map");
Serial.println("ASCII Table ~ Character Map");
Serial.println("ASCII Table ~ Character Map");
Serial.println("ASCII Table ~ Character Map");
}
// first visible ASCIIcharacter '!' is number 33:
int thisByte = 33;
// you can also write ASCII characters in single quotes.
// for example. '!' is the same as 33, so you could also use this:
//int thisByte = '!';
void loop() {
// prints value unaltered, i.e. the raw binary version of the
// byte. The serial monitor interprets all bytes as
// ASCII, so 33, the first number, will show up as '!'
Serial.write(thisByte);
Serial.print(", dec: ");
// prints value as string as an ASCII-encoded decimal (base 10).
// Decimal is the default format for Serial.print() and Serial.println(),
// so no modifier is needed:
Serial.print(thisByte);
// But you can declare the modifier for decimal if you want to.
//this also works if you uncomment it:
// Serial.print(thisByte, DEC);
Serial.print(", hex: ");
// prints value as string in hexadecimal (base 16):
Serial.print(thisByte, HEX);
Serial.print(", oct: ");
// prints value as string in octal (base 8);
Serial.print(thisByte, OCT);
Serial.print(", bin: ");
// prints value as string in binary (base 2)
// also prints ending line break:
Serial.println(thisByte, BIN);
// if printed last visible character '~' or 126, stop:
if(thisByte == 126) { // you could also use if (thisByte == '~') {
// This loop loops forever and does nothing
while(true) {
continue;
}
}
// go on to the next character
thisByte++;
}
Arduinoの標準サンプルでserialのbpsのみを変更したものになる。
platformioの設定
platformio.iniの設定だけでは自動的にインストールできないものが あるので以下の手順で、予めインストールしておく。
# userの top directory
mkdir tools
cd tools
# ダウンロード
wget https://github.com/espressif/crosstool-NG/releases/download/esp-2021r2/riscv32-esp-elf-gcc8_4_0-esp-2021r2-linux-amd64.tar.gz
# 解凍
tar -xzvf riscv32-esp-elf-gcc8_4_0-esp-2021r2-linux-amd64.tar.gz
# package.jsonのコピー
cd ~/tools/riscv32-esp-elf
cp ~/.platformio/packages/toolchain-riscv-esp/package.json .
plaformio.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:esp32c3]
platform = espressif32
platform_packages =
toolchain-riscv-esp@file:///home/<USER>/tools/riscv32-esp-elf
framework-arduinoespressif32@https://github.com/espressif/arduino-esp32.git#master
platformio/tool-esptoolpy
framework = arduino
board = esp32dev
board_build.mcu = esp32c3
board_build.partitions = huge_app.csv
board_build.variant = esp32c3
board_build.f_cpu = 160000000L
board_build.f_flash = 80000000L
board_build.flash_mode = dio
board_build.arduino.ldscript = esp32c3_out.ld
build_unflags =
-DARDUINO_ESP32_DEV
-DARDUINO_VARIANT="esp32"
build_flags =
-DARDUINO_ESP32C3_DEV
-DARDUINO_VARIANT="esp32c3"
lib_deps = adafruit/Adafruit NeoPixel@^1.10.0
monitor_speed = 115200
monitor_filters = time
上のplatformio.iniは「This is an example PlatformIO project for M5Stamp C3」のものをベースにして、そのままでは動作しないので若干変更したものになる。 「<USER>」の部分は、自分の環境に合わせて変更すること。
sample code for platfomio
b3NTP.ino
// select board
////#define WIO_TERMINAL
////#define ESP8266
////#define ESP32
////#define M5ATOM
//------------------
// NTP Client for Wio-Terminal/ESP8266/ESP32
#ifdef M5ATOM
#include "M5Atom.h"
#define ESP32
#endif
#ifdef WIO_TERMAL
//#include <AtWiFi.h>
#include <rpcWiFi.h>
#endif
#ifdef ESP8266
#include <ESP8266WiFi.h>
#endif
#ifdef ESP32
#include <WiFi.h>
#endif
#include <time.h>
#define WIFI_SSID "your_wifi_ssid"
#define WIFI_PASSWORD "your_wifi_password"
void setup() {
Serial.begin(115200);
delay(100);
Serial.print("\r\n\nReset:\r\n");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while(WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(500);
}
Serial.println();
Serial.printf("Connected, IP address: ");
Serial.println(WiFi.localIP());
configTzTime("JST-9", "ntp.nict.jp", "ntp.jst.mfeed.ad.jp"); // 2.7.0以降, esp32コンパチ
}
void loop() {
time_t t;
struct tm *tm;
static const char *wd[7] = {"Sun","Mon","Tue","Wed","Thr","Fri","Sat"};
t = time(NULL);
tm = localtime(&t);
/*****
Serial.printf("ESP8266/Arduino ver%s : %04d/%02d/%02d(%s) %02d:%02d:%02d\n",
__STR(ARDUINO_ESP8266_GIT_DESC),
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
wd[tm->tm_wday],
tm->tm_hour, tm->tm_min, tm->tm_sec);
****/
Serial.printf("Arduino NTP: %04d/%02d/%02d(%s) %02d:%02d:%02d\r\n",
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
wd[tm->tm_wday],
tm->tm_hour, tm->tm_min, tm->tm_sec);
delay(1000);
}
以下はwifi環境に応じて変更すること:
#define WIFI_SSID "your_wifi_ssid"
#define WIFI_PASSWORD "your_wifi_password"
本ソースは、WIO_TERMINAL、ESP8266、ESP32、M5ATOMで共通になっており、platformio.iniの内容でターゲットを切り替えることができる。
platformioのビルドと書き込み
# ライブラリのインストール(1回目のみ)
pio run -t clean
# ボードをPCに接続する
pio run -t upload
実行時のシリアル出力(/dev/ttyACM0):
Build:Feb 7 2021
rst:0xf (BROWNOUT_RST),boot:0xc (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fcd6100,len:0x3f0
load:0x403ce000,len:0x6a8
load:0x403d0000,len:0x2398
SHA-256 comparison failed:
Calculated: 071318b1de86cde7f1aec94a59e4cb69b9476060b25c8688588480d5e8f36d1c
Expected: aeb43c2bf68a09f09f2b1d7c8bb79c7727cf0a2b827a76540c98a24fcafdd49a
Attempting to boot anyway...
entry 0x403ce000
Reset:
........
Connected, IP address: 192.168.1.15
Arduino NTP: 1970/01/01(Thr) 09:00:04
Arduino NTP: 1970/01/01(Thr) 09:00:05
Arduino NTP: 1970/01/01(Thr) 09:00:06
Arduino NTP: 1970/01/01(Thr) 09:00:07
Arduino NTP: 1970/01/01(Thr) 09:00:08
Arduino NTP: 2021/12/11(Sat) 16:55:59 ← ここでNTPによって時刻が同期している
Arduino NTP: 2021/12/11(Sat) 16:56:00
Arduino NTP: 2021/12/11(Sat) 16:56:01
sample code #2 for platformio
main.ino
#include <Adafruit_NeoPixel.h>
#define BUTTON 3
#define LED 2
#define NUMPIXELS 1
Adafruit_NeoPixel pixels(NUMPIXELS, LED, NEO_GRB + NEO_KHZ800);
int lastState = HIGH;
int currentState;
int buttonCount = 1;
void setup() {
Serial.begin(115200);
pixels.begin();
pinMode(BUTTON, INPUT_PULLUP);
}
int high = 255;
int mid = 128;
int low = 0;
void setLed() {
pixels.clear();
switch (buttonCount) {
case 1:
pixels.setPixelColor(0, pixels.Color(high, low, low));
break;
case 2:
pixels.setPixelColor(0, pixels.Color(high, mid, mid));
break;
case 3:
pixels.setPixelColor(0, pixels.Color(low, high, low));
break;
case 4:
pixels.setPixelColor(0, pixels.Color(mid, high, mid));
break;
case 5:
pixels.setPixelColor(0, pixels.Color(low, low, high));
break;
case 6:
pixels.setPixelColor(0, pixels.Color(mid, mid, high));
break;
default:
break;
}
pixels.show();
}
void loop() {
currentState = digitalRead(BUTTON);
if(lastState == LOW && currentState == HIGH) {
Serial.println("Button Pressed!");
buttonCount++;
if (buttonCount > 6) {
buttonCount = 1;
}
setLed();
}
lastState = currentState;
}
本サンプルは「This is an example PlatformIO project for M5Stamp C3」のものになる。
動作としては、ボタンを押すたびにLEDの色が変化する。
またボタンを押すたびに「Button Pressed!」がシリアル出力される。
参考情報
M5Stamp-C3 docs:
STAMP-C3
ESP32C3 Technical Reference Manual
ESP32C3 Series - Datasheet
Arduino-IDE関連:
Arduino IDEのインストールと設定 (Windows, Mac, Linux対応)
M5Stamp C3 Mateを試してみました【Arduino使用】
platformio関連:
arduinoフレームワーク用platformio.ini集
This is an example PlatformIO project for M5Stamp C3
その他: スルーホール用テストワイヤ TT-200 (10本入)
以上
| 固定リンク
「PlatformIO」カテゴリの記事
- NuEVI/NuRADのビルド(2022.08.18)
- Wio_ExtFlashLoad(WriteSampleMenu.ino)スケッチをplatformioでビルドする(2022.02.03)
- uncannyeyesスケッチをplatformioでビルドする(2022.01.31)
- LovyanGFX-Display ライブラリを使用したスケッチをplatformioでビルドする(2022.01.30)
- Wio-Terminal/M5Core2のWiFiAnallyzer(2022.01.24)
「RISC-V」カテゴリの記事
- M5Stamp-C3 Arduino Install(2021.12.12)
- Wio_Lite_RISC-VボードでWiFiを動かす(その4:MQTT)(2020.06.29)
- Wio_Lite_RISC-VボードでWiFiを動かす(その3:OSC)(2020.06.23)
- Wio_Lite_RISC-VボードでWiFiを動かす(その2:STARWARS,REST-API)(2020.06.21)
- Longan-NanoでLCDとSerialを使う(framework:Arduino版)(2020.05.19)
「Arduino」カテゴリの記事
- platformioのために新しいユーザーを設定する(2022.02.06)
- MAKER_PI_RP2040でI2Cを使う(Arduino編)(2022.01.09)
- M5Stamp-PICO Arduino Install(2021.12.12)
- M5Stamp-C3 Arduino Install(2021.12.12)
- Arduino-CLIのインストール(2021.05.19)
「M5Stamp-C3」カテゴリの記事
- M5Stamp-C3 Arduino Install(2021.12.12)
この記事へのコメントは終了しました。
コメント