I am trying to build a setup with an Arduino Mega, having a DMX output (using DMXSimple library), and a MIDI library (to be done).

I was wondering, if I get into problems. According to This question, where Majenko's answer mentions that FastLED holds interrupts, I wonder if I can get into problems if I try to read MIDI data from 3 serial ports while sending DMX.

Probably FastLED (for led strips) and DMX (250 kbps) have some similarity. DMXSimple works with just one data line (no UART), but MIDI is using UART (31.25 kbps), 3 UARTS.

My fear is that everything works separately (I tried it before, long time ago with MIDI), but when I try to combine it, I get timing problems.


Yes, it does disable interrupts when sending a byte:

#if defined(__AVR__)
void dmxSendByte(volatile uint8_t value)
  uint8_t bitCount, delCount;
  __asm__ volatile (
    "cli\n"                            <<==== DISABLE INTERRUPTS
    "ld __tmp_reg__,%a[dmxPort]\n"
    "and __tmp_reg__,%[outMask]\n"
    "st %a[dmxPort],__tmp_reg__\n"
    "ldi %[bitCount],11\n" // 11 bit intervals per transmitted byte
    "rjmp bitLoop%=\n"     // Delay 2 clock cycles. 
    "ldi %[delCount],%[delCountVal]\n"
    "dec %[delCount]\n"
    "brne delLoop%=\n"
    "ld __tmp_reg__,%a[dmxPort]\n"
    "and __tmp_reg__,%[outMask]\n"
    "ror %[value]\n"
    "brcc sendzero%=\n"
    "or __tmp_reg__,%[outBit]\n"
    "st %a[dmxPort],__tmp_reg__\n"
    "dec %[bitCount]\n"
    "brne bitLoop%=\n"
    "sei\n"                             <<==== ENABLE INTERRUPTS
      [bitCount] "=&d" (bitCount),
      [delCount] "=&d" (delCount)
      [dmxPort] "e" (dmxPort),
      [outMask] "r" (~dmxBit),
      [outBit] "r" (dmxBit),
      [delCountVal] "M" (F_CPU/1000000-3),
      [value] "r" (value)

Also that routine is run from within a timer interrupt routine. On AVR with no interrupt priorities even without disabling interrupts it would still be blocking all other software from running.

  • Thanks for answering so fast, do you think it could give problems to MIDI input? 3 serial ports, 31 kbps? Or is this so slow, the MIDI will be handled between calls to dmxSendByte of this library? – Michel Keijzers Jul 23 '19 at 18:55
  • 1
    It's not the baud rate that is a problem, but how rapidly bytes arrive. There has to be time to put the incoming byte into the RX buffer before the next byte arrives, and DMXSimple will end up creating windows where that can happen. If you have two bytes arriving between windows then one will be lost. – Majenko Jul 23 '19 at 19:02
  • Ok (I hope because I use hardware UARTs, the RX buffer will be filled before this problem happens. Anyway, I will do some testing of 'simultaneous' MIDI and DMX together. Thanks for the insights. – Michel Keijzers Jul 23 '19 at 19:17
  • 1
    There is (IIRC) only one byte of hardware RX buffer. It has to be emptied by the RX interrupt before the next byte arrives. – Majenko Jul 23 '19 at 19:19
  • Ok ... that means thus 32 us, but that seems longer than the time for executing those 20 instructions ... so it could be ok :-) – Michel Keijzers Jul 23 '19 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.