0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

I2C-Uart変換基板のテスト

Last updated at Posted at 2025-06-09

概要

マイコンのUARTポートが埋まってしまった場合にI2C経由でUART通信できる基板が販売されています。
普段使っているESP32系マイコンで使えるか試してみました。

結果

一部ライブラリの修正を行うことで無事に利用することができました。
文字列(String)で送受信ができるよう関数化し普段の使い勝手と同じになるようしています。

<テスト環境>
 M5AtomLite ⇔ i2c-uart変換基板 ⇔ USBSerialコンバーター ⇔ PC(仮想COM)
image.png

環境など

 マイコン:ATOM Lite
 エディタ:VisualStudioCode(PlatformIO IDE)
 フレームワーク:Aruduino
利用ライブラリ:https://github.com/meerstern/I2C_UART_Converter.git
 変換基板:
image.png

 購入先
 https://www.switch-science.com/products/6027?_pos=1&_sid=fc129606c&_ss=r

ライブラリの修正

ライブラリはArduino用のため、ESP32で利用にあたり'SC16IS750.cpp'内の下記
2点の修正を行いました。

 ①Wire定義
 デフォルトではESP32はWire1が選択されてしまうため、Wireを利用するための修正

 <修正前>
 #ifdef AVR
 #define WIRE Wire
 #else // Arduino Due
 #define WIRE Wire1
 #endif

 <修正後>
 #ifndef WIRE
  #if defined(AVR) || defined(ESP32)
   #define WIRE Wire
  #else
  #define WIRE Wire1
  #endif
 #endif

②write()関数の修正
 定義では戻り値の指定があるが、戻り値が無いため実行時にエラーとなるため修正
 <修正前>
 size_t SC16IS750::write(uint8_t val)
 {
  WriteByte(val);
 }

 <修正後>
 size_t SC16IS750::write(uint8_t val)
 {
  WriteByte(val);
  return 1;// ←戻り値を指定
 }

ソースコード 

#include <Arduino.h>
#include <M5Unified.h>
#include <SC16IS750.h>
#include <Wire.h>

SC16IS750 i2cuart = SC16IS750(SC16IS750_PROTOCOL_I2C,SC16IS750_ADDRESS_BB);

// i2c-uart 送信処理
void send_i2cuart(SC16IS750& uart,String msg){
  const char* message = msg.c_str();
  // 文字列を1文字ずつ送信
  for (size_t i = 0; i < strlen(message); i++) {
    uart.write(message[i]);
  }
}

// i2c-uart 受信処理
String rev_i2cuart(SC16IS750& uart, unsigned long timeout_ms = 1000) {
  String received = "";
  unsigned long startTime = millis();

  while (millis() - startTime < timeout_ms) {
    if (uart.available()) {
      char c = uart.read();
      if (c == '\n') {
        break;  // 改行で受信完了
      }
      received += c;
      startTime = millis();  // データが来たらタイマーをリセット
    }
  }
  return received;
}

void setup() 
{
  delay(7000);
  M5.begin(); 
  Serial.begin(9600);
  i2cuart.begin(9600);
  Serial.println("Init OK");
  if (i2cuart.ping()!=1) {
      Serial.println("device not found");
      while(1);
  } else {
      Serial.println("device found");
  }
  Serial.println("start serial communication");
};

void loop() 
{
  // i2c-uart受信処理
  if(i2cuart.available()){
    String rev_str = rev_i2cuart(i2cuart);
    Serial.println(rev_str);
  }

  // USBシリアル受信処理
  if(Serial.available()>0){    
    String rev_str_usb = Serial.readStringUntil('\n'); //終端文字までを取得
    send_i2cuart(i2cuart,rev_str_usb+"\n");
  }
};

plathomeio.ini設定

[env:m5stack-atom]
platform = espressif32
board = m5stack-atom
framework = arduino
lib_deps =
m5stack/M5Unified
https://github.com/meerstern/I2C_UART_Converter.git

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?