FortySevenEffects / arduino_midi_library

MIDI for Arduino
MIT License
1.56k stars 253 forks source link

MIDI Output Only #176

Closed tomcarlotan closed 3 years ago

tomcarlotan commented 3 years ago

Its not apparent in the resources, but is there a possible setting in the library to just make use of the MIDI output functionality and leave the RX pin free for other uses in the Arduino? Thank you for your work!

franky47 commented 3 years ago

There are two cases:

  1. Use only the TX pin (MIDI output only, RX is free)
  2. Use only the RX pin (MIDI input only, TX is free)

Unfortunately, the serial port is tied both ways on AVR boards. There is a single baud rate (transmission speed) setting for both TX and RX, so unless your other transmission can run at the MIDI rate of 31250 bps, you can't really use the other half of the serial line.

If you can use 31250bps however, it's possible:

In both cases, MIDI.begin will setup the whole Serial port for both receiving and sending messages at 31250 bps.

tomcarlotan commented 3 years ago

My use case is that of 1, I will only use MIDI output functionality and wont use the input. I plan to use RX pin as digital inputs or as an INT pin for the USB host Shield. Will there be conflicts when doing this? Actually I've done this and observed no problems with my project, the USB Host shield works at sending USB-MIDI even if I used the RX pin as the INT pin.

franky47 commented 3 years ago

Are you using a hardware serial output in addition to the USB MIDI ?

I guess you can try and see what happens if you configure your RX pin after the call to MIDI.begin (the hardware serial one), it's possible that the RX circuitry will still try and interpret pin toggles as data bits, but I'm not sure.

tomcarlotan commented 3 years ago

Yes, Im still using the Hardware Serial along with the USB-HOST as a MIDI Output as well. On top I'm also using the MIDIUSB Library to communicate MIDI via USB to a PC. I observe no problem so far, being able to send on both hardware serial and usb-host, and both send and receive on the built in USB on the ATMEGA32u4 board. I was just wondering if there is a safer way of leaving the RX Pin unused in your library.

franky47 commented 3 years ago

Got it. From what I can read in the AVR user manual, there is a way to clear the RX bit in a register to disable the serial receiver, you might want to check that out before setting the pin up for other tasks. On the 32u4 it looks to be UCSR1B &= ~(1 << RXEN1) (see page 222 of the user guide for that chip for the register and bit description).

The order of operations would then be:

MIDI.begin(); // on the hardware serial port
UCSR1B &= ~(1 << RXEN1); // clear the RX bit
// setup the RX pin as you like
tomcarlotan commented 3 years ago

Thank you very much! Will look into your recommendation :)

jnettome commented 3 years ago

Thanks @franky47 ! It help me as well.