0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

この M5Stack Unit Synth は UARTインタフェースでアクセスしますが、
CP2102チップのUSBシリアルでは、まったく鳴らすことができませんでした。
しかし、CH340CチップのUSBシリアルであれば、問題なく鳴らすことができす。

原因は、CP2102では 31250bpsで通信できないことにありました。
31250bpsを指定しても、CP2102は 29種類の固定UARTボーレートへの丸め込みが行われ、38400bpsになってしまうそうです(誤差23%)。
300, 600, 1200, 1800, 2400, 4000, 4800, 7200, 9600, 14400, 16000, 19200, 28800, 38400, 51200, 56000, 57600, 64000, 76800, 115200, 128000, 153600, 230400, 250000, 256000, 460800, 500000, 576000, 921600

CH340xではクロックの整数分周により、誤差0%で 31250bps通信できる。

USBシリアル変換チップなら、なんでも使えると考えていたので、原因箇所の特定に時間を要しました。

MIDI play
import serial
import time

PORT = "/dev/cu.usbserial-120"
BAUD = 31250

CHANNEL = 0
NOTE_ON = 0x90 | CHANNEL
NOTE_OFF = 0x80 | CHANNEL
PROGRAM_CHANGE = 0xC0 | CHANNEL

# C major scale-ish 4-bar melody (note, duration_beats)
MELODY = [
    (60, 1), (62, 1), (64, 1), (65, 1),   # bar 1: C D E F
    (67, 1), (65, 1), (64, 1), (62, 1),   # bar 2: G F E D
    (60, 2), (64, 2),                     # bar 3: C E (half notes)
    (65, 1), (67, 1), (69, 1), (72, 1),   # bar 4: F G A C(high)
]

BPM = 100
BEAT_SEC = 60.0 / BPM
VELOCITY = 100
PROGRAM = 0  # 0 = Acoustic Grand Piano


def main():
    with serial.Serial(PORT, BAUD) as ser:
        # allow time in case opening the port reset the board (DTR toggle)
        time.sleep(2.0)
        ser.write(bytes([PROGRAM_CHANGE, PROGRAM]))
        time.sleep(0.05)

        for note, beats in MELODY:
            ser.write(bytes([NOTE_ON, note, VELOCITY]))
            time.sleep(BEAT_SEC * beats * 0.9)
            ser.write(bytes([NOTE_OFF, note, 0]))
            time.sleep(BEAT_SEC * beats * 0.1)

        print("Done.")


if __name__ == "__main__":
    main()

もし、31250bpsで通信できるUSBシリアルがない場合は、ESP32、RP2040 や ATmega32U4 を使い UARTブリッジ することにより、M5Stack Unit Synthを 115200bpsでアクセスできるようになります。
115200bpsであれば、おそらく どんなシリアルチップでも使うことが できるはずです。

Arduino
// USB-Serial (115200bps) <-> Hardware UART (31250bps) bridge
// for ESP32 , RP2040 (Raspberry Pi Pico) and ATmega32U4

#define MIDI_BAUD 31250

// TX -> Unit Synth UART_RX (Grove yellow)
// RX <- Unit Synth UART_TX, unused by Unit Synth

#if defined(ARDUINO_ARCH_ESP32)
    #define MIDI_SERIAL Serial2
    #define UNIT_SYNTH_TXD 32
    #define UNIT_SYNTH_RXD 33
#elif defined(ARDUINO_ARCH_RP2040)
    #define MIDI_SERIAL Serial1
    #define UNIT_SYNTH_TX_PIN 4
    #define UNIT_SYNTH_RX_PIN 5
#elif defined(__AVR_ATmega32U4__)
    // Leonardo/Micro/Pro Micro: Serial1 TX = pin 1, RX = pin 0
    #define MIDI_SERIAL Serial1
#else
    #error "Unsupported platform: add pin definitions for your board"
#endif

void setup() {
    Serial.begin(115200);  // USB CDC to Mac

#if defined(ARDUINO_ARCH_ESP32)
    MIDI_SERIAL.begin(MIDI_BAUD, SERIAL_8N1, UNIT_SYNTH_RXD, UNIT_SYNTH_TXD);
#elif defined(ARDUINO_ARCH_RP2040)
    MIDI_SERIAL.setTX(UNIT_SYNTH_TX_PIN);
    MIDI_SERIAL.setRX(UNIT_SYNTH_RX_PIN);
    MIDI_SERIAL.begin(MIDI_BAUD, SERIAL_8N1);
#elif defined(__AVR_ATmega32U4__)
    MIDI_SERIAL.begin(MIDI_BAUD);
#endif
}

void loop() {
    while (Serial.available()) {
        MIDI_SERIAL.write(Serial.read());
    }
}

ちなみに、市販のUSB-MIDIインターフェースで使われるチップの定番は、FTDI系(FT232x)のようです。



以上

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?