1

I'm having some conflicts between the core HardwareSerial.cpp and an external DMX library.

For DMX I'm using the Four Universes DMX 512 Library. Using the library standalone, it all works perfectly. But when combining it with other libraries, I'm getting the error:

core.a(HardwareSerial.cpp.o): In function '__vector_36':
C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/HardwareSerial.cpp:147: multiple definition of '__vector_36'
Dmx\lib_dmx.cpp.o:C:\Program Files (x86)\Arduino\libraries\Dmx/lib_dmx.cpp:206: first defined here

The DMX library uses the UART hardware to generate the DMX signals. It supports four universes on the Arduino Mega since that has four UART hardware parts. Now, I'd like to only use the second hardware part, so the UART1. In the DMX library this is easily adjusted in the .h-file, by commenting out the non-used serial ports.

The line from lib_dmx.cpp that is conflicting is this line:

#if defined(USE_UART1)
ISR (SIG_USART1_RECV)
{
  ArduinoDmx1.Process_ISR_RX(1);
}
#endif

The line from hardwareSerial.cpp that is conflicting is in the ISR here:

#if defined(USART1_RX_vect)
  void serialEvent1() __attribute__((weak));
  void serialEvent1() {}
  #define serialEvent1_implemented
  ISR(USART1_RX_vect)
  {
    if (bit_is_clear(UCSR1A, UPE1)) {
      unsigned char c = UDR1;
      store_char(c, &rx_buffer1);
    } else {
      unsigned char c = UDR1;
    };
  }
#endif

I have been having issues with this for a very long time and don't have a solution at hand yet. It's also a but unclear to me how the interrupts on Arduinos are arranged. I've also included the .ino I'm using currently. I'm also using another Ethernet Library than the default, one that includes multicast UDP (found here).

lib_dmx.h

> /***************************************************************************
> *
> * Title          : Arduino DMX512 library. 4 input/output universes.
> * Version        : v 0.3 beta
> * Last updated   : 07.07.2012
> * Target         : Arduino mega 2560, Arduino mega 1280, Arduino nano (1 universe)  
> * Author         : Toni Merino - merino.toni at gmail.com
> * Web            : www.deskontrol.net/blog
> *
> * Based on ATmega8515 Dmx library written by Hendrik Hoelscher, www.hoelscher-hi.de
> 
> ;***************************************************************************/
> #ifndef __INC_DMX_H
> #define __INC_DMX_H
> 
> #include <avr/io.h>
> #include <stdint.h>
> #include <avr/interrupt.h>
> #include <util/delay.h>
> #if ARDUINO >= 100   #include "Arduino.h"
> #else    #include "WProgram.h"
> #endif
> 
> //#define        USE_INTERBYTE_DELAY     // rare cases of equipment
> non full DMX-512 compliant, need this
> 
> // *** comment UARTs not used *** //#define        USE_UART0
> #define        USE_UART1 //#define        USE_UART2 //#define        USE_UART3
> 
> // New DMX modes *** EXPERIMENTAL ***
> #define        DMX512            (0)    // DMX-512 (250 kbaud - 512 channels) Standard USITT DMX-512
> #define        DMX1024           (1)    // DMX-1024 (500 kbaud - 1024 channels) Completely non standard - TESTED ok
> #define        DMX2048           (2)    // DMX-2048 (1000 kbaud - 2048 channels) called by manufacturers DMX1000K, DMX 4x or DMX 1M ???
> 
> // DMX-512  (250 kbaud - 512 channels) Standard USITT DMX-512
> #define        IBG_512           (10)                      // interbyte gap [us]
> #define        DMX_512           ((F_CPU/(250000*8))-1)    // 250 kbaud
> #define        BREAK_512         ( F_CPU/(100000*8))       // 90.9 kbaud
> 
> // DMX-1024 (500 kbaud - 1024 channels) Completely non standard
> #define        IBG_1024          (5)                       // interbyte gap [us]
> #define        DMX_1024          ((F_CPU/(500000*8))-1)    // 500 kbaud
> #define        BREAK_1024        ( F_CPU/(200000*8))       // 181.8 kbaud
> 
> // DMX-2048 (1000 kbaud - 2048 channels) Non standard, but used by
> manufacturers as DMX1000K or DMX-4x or DMX 1M ???
> #define        IBG_2048          (2)                       // interbyte gap [us] + nop's to reach 2.5 uS
> #define        DMX_2048          ((F_CPU/(1000000*8))-1)   // 1000 kbaud
> #define        BREAK_2048        ( F_CPU/(400000*8))       // 363.6 kbaud
> 
> // Inline assembly: do nothing for one clock cycle.
> #define        nop()             __asm__ __volatile__("nop")
> 
> #ifdef __cplusplus extern "C" {
> #endif   #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
>     #if defined(USE_UART0)
>       void SIG_USART0_RECV  (void) __attribute__((__always_inline__));
>       void SIG_USART0_TRANS (void) __attribute__((__always_inline__));
>     #endif
>     #if defined(USE_UART1)
>       void SIG_USART1_RECV  (void) __attribute__((__always_inline__));
>       void SIG_USART1_TRANS (void) __attribute__((__always_inline__));
>     #endif
>     #if defined(USE_UART2)  
>       void SIG_USART2_RECV  (void) __attribute__((__always_inline__));
>       void SIG_USART2_TRANS (void) __attribute__((__always_inline__));
>     #endif
>     #if defined(USE_UART3)  
>       void SIG_USART3_RECV  (void) __attribute__((__always_inline__));
>       void SIG_USART3_TRANS (void) __attribute__((__always_inline__));
>     #endif   #elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
>     #if defined(USE_UART0)
>       void USART_RX_vect    (void) __attribute__((__always_inline__));
>       void USART_TX_vect    (void) __attribute__((__always_inline__));
>     #endif   #endif
> #ifdef __cplusplus };
> #endif
> 
> class CArduinoDmx  {    #if defined(__AVR_ATmega1280__) ||
> defined(__AVR_ATmega2560__)
>     #if defined(USE_UART0)
>       friend void SIG_USART0_RECV  (void);
>       friend void SIG_USART0_TRANS (void);
>     #endif
>     #if defined(USE_UART1)
>       friend void SIG_USART1_RECV  (void);
>       friend void SIG_USART1_TRANS (void);
>     #endif
>     #if defined(USE_UART2)  
>       friend void SIG_USART2_RECV  (void);
>       friend void SIG_USART2_TRANS (void);
>     #endif
>     #if defined(USE_UART3)  
>       friend void SIG_USART3_RECV  (void);
>       friend void SIG_USART3_TRANS (void);
>     #endif   #elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
>     #if defined(USE_UART0)
>       friend void USART_RX_vect    (void);
>       friend void USART_TX_vect    (void);
>     #endif   #endif    public:    enum {IDLE, BREAK, STARTB, STARTADR};     // RX DMX states    enum {TXBREAK, TXSTARTB, TXDATA};  
> // TX DMX states
>       volatile uint8_t    *RxBuffer;            // array of RX DMX values    volatile uint8_t    *TxBuffer;            // array of TX DMX
> values
> 
> private:    uint8_t     gRxState;    uint8_t    *gRxPnt;    uint8_t   
> IndicatorCount;    uint8_t     USARTstate;            uint8_t     RxByte;   
> uint8_t     RxState;    uint8_t     mUART;    uint8_t     gTxState;   
> uint16_t    RxCount;    uint16_t    gCurTxCh;         uint16_t   
> rx_channels;                  // rx channels number    uint16_t   
> tx_channels;                  // tx channels number    uint16_t   
> rx_address;                   // rx start address    uint16_t   
> tx_address;                   // tx start address    int8_t     
> rx_led;                       // rx indicator led pin    int8_t     
> tx_led;                       // tx indicator led pin    int8_t     
> control_pin;                  // max485 input/output selection pin   
> uint8_t     dmx_mode;                     // Standard USITT DMX512 =
> 0, non standard DMX1024 = 1, non standard DMX2048 (DMX1000K) = 2   
> uint8_t     speed_dmx;    uint8_t     speed_break;    uint16_t   
> CurTxCh;    uint8_t     TxState;    uint8_t    *RxPnt;
>    
> #if defined(USE_INTERBYTE_DELAY)       void        delay_gap          ();
> #endif
> 
> public:    void        stop_dmx           ();    void        set_speed
> (uint8_t mode);    void        set_control_pin    (int8_t  pin)       
> { control_pin     = pin;      }    void        init_rx           
> (uint8_t mode);  // Standard USITT DMX512 = 0, non standard DMX1024 =
> 1, non standard DMX2048 (DMX1000K) = 2    void        set_rx_address  
> (uint16_t address)   { rx_address      = address;  }    void       
> set_rx_channels    (uint16_t channels)  { rx_channels     = channels;
> }    void        init_tx            (uint8_t mode);  // Standard USITT
> DMX512 = 0, non standard DMX1024 = 1, non standard DMX2048 (DMX1000K)
> = 2    void        set_tx_address     (uint16_t address)   { tx_address      = address;  }    void        set_tx_channels   
> (uint16_t channels)  { tx_channels     = channels; }
> 
>    void        attachTXInterrupt  (void (*isr)(uint8_t uart))      {
> TXisrCallback   = isr; }   // register the user TX callback    void   
> attachRXInterrupt  (void (*isr)(uint8_t uart))      { RXisrCallback  
> = isr; }   // register the user RX callback
>        //void         Process_ISR_RX(uint8_t rx_isr_number);
> 
>    void        (*TXisrCallback)   (uint8_t uart);    void       
> (*RXisrCallback)   (uint8_t uart);
> 
>    inline void Process_ISR_RX     (uint8_t  rx_isr_number);    inline
> void Process_ISR_TX     (uint8_t  tx_isr_number);    public:   
> CArduinoDmx                    (uint8_t uart)       { rx_address     
> = 1; 
>                                                          rx_channels     = 8;
>                                                          tx_address      = 1; 
>                                                          tx_channels     = 8;
>                                                          mUART           = uart; }   };
> 
> #if defined(USE_UART0)   extern CArduinoDmx ArduinoDmx0;
> #endif
> #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)   #if defined(USE_UART1)
>     extern CArduinoDmx ArduinoDmx1;   #endif   #if defined(USE_UART2)
>     extern CArduinoDmx ArduinoDmx2;   #endif   #if defined(USE_UART3)
>     extern CArduinoDmx ArduinoDmx3;   #endif
> #endif
> 
> #endif

lib_dmx.cpp

/***************************************************************************
*
* Title          : Arduino DMX512 library. 4 input/output universes.
* Version        : v 0.3 beta
* Last updated   : 07.07.2012
* Target         : Arduino mega 2560, Arduino mega 1280, Arduino nano (1 universe)  
* Author         : Toni Merino - merino.toni at gmail.com
* Web            : www.deskontrol.net/blog
*
* Based on ATmega8515 Dmx library written by Hendrik Hoelscher, www.hoelscher-hi.de

;***************************************************************************/
#include "lib_dmx.h"
#include <SPI.h>

#if defined(USE_UART0)
  CArduinoDmx ArduinoDmx0(0);
#endif
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  #if defined(USE_UART1)
    CArduinoDmx ArduinoDmx1(1);
  #endif
  #if defined(USE_UART2)
    CArduinoDmx ArduinoDmx2(2);
  #endif
  #if defined(USE_UART3)
    CArduinoDmx ArduinoDmx3(3);
  #endif
#endif

// *************** DMX Transmision Initialisation ****************
void CArduinoDmx::init_tx(uint8_t mode)
{
  cli();          //disable interrupts
  stop_dmx();                         //stop uart
  dmx_mode = mode;
  set_speed(dmx_mode);

  if(control_pin != -1)
  {
    pinMode(control_pin,OUTPUT);        // max485 I/O control
    digitalWrite(control_pin, HIGH);    // set 485 as output
  }

  if(mUART == 0)
  {
    pinMode(1, OUTPUT);
    UBRR0H   = 0;
    UBRR0L   = speed_dmx;  
    UCSR0A  |= (1<<U2X0);
    UCSR0C  |= (3<<UCSZ00)|(1<<USBS0);
    UCSR0B  |= (1<<TXEN0) |(1<<TXCIE0);
    UDR0     = 0;                                     //start USART 0
  }
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  else if(mUART == 1)
  {
    pinMode(18, OUTPUT); 
    UBRR1H   = 0;
    UBRR1L   = speed_dmx;   
    UCSR1A  |= (1<<U2X1);
    UCSR1C  |= (3<<UCSZ10)|(1<<USBS1);
    UCSR1B  |= (1<<TXEN1) |(1<<TXCIE1);
    UDR1     = 0;                                     //start USART 1
  }
  else if(mUART == 2)
  {
    pinMode(16, OUTPUT); 
    UBRR2H   = 0;
    UBRR2L   = speed_dmx;   
    UCSR2A  |= (1<<U2X2);
    UCSR2C  |= (3<<UCSZ20)|(1<<USBS2);
    UCSR2B  |= (1<<TXEN2) |(1<<TXCIE2);
    UDR2     = 0;                                     //start USART 2
  }
  else if(mUART == 3)
  {
    pinMode(14, OUTPUT); 
    UBRR3H   = 0;
    UBRR3L   = speed_dmx;    
    UCSR3A  |= (1<<U2X3);
    UCSR3C  |= (3<<UCSZ30)|(1<<USBS3);
    UCSR3B  |= (1<<TXEN3) |(1<<TXCIE3);
    UDR3     = 0;                                     //start USART 3
  }
#endif

  gTxState = BREAK;                                     // start with break
  TxBuffer = (uint8_t*)malloc(tx_channels);     // allocate mem for buffer
  memset((uint8_t*)TxBuffer, 0, tx_channels);   // fill buffer with 0's
  sei();          //enable interrupts
}

// ************************ DMX Stop ***************************
void CArduinoDmx::stop_dmx()
{
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  if(mUART == 0)
  {
    UCSR0B &= ~((1<<RXCIE0) | (1<<TXCIE0) | (1<<RXEN0) | (1<<TXEN0));
  }
  else if(mUART == 1)
  {
    UCSR1B &= ~((1<<RXCIE1) | (1<<TXCIE1) | (1<<RXEN1) | (1<<TXEN1));
  }
  else if(mUART == 2)
  {
    UCSR2B &= ~((1<<RXCIE2) | (1<<TXCIE2) | (1<<RXEN2) | (1<<TXEN2));
  }
  else if(mUART == 3)
  {
    UCSR3B &= ~((1<<RXCIE3) | (1<<TXCIE3) | (1<<RXEN3) | (1<<TXEN3));
  }
#elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
  if(mUART == 0)
  {
    UCSR0B &= ~((1<<RXCIE0) | (1<<TXCIE0) | (1<<RXEN0) | (1<<TXEN0));
  }
#endif
}

// *************** DMX Reception Initialisation ****************
void CArduinoDmx::init_rx(uint8_t mode)
{
  cli();          //disable interrupts
  stop_dmx();
  dmx_mode = mode;
  set_speed(dmx_mode);

  if(control_pin != -1)
  {
    pinMode(control_pin,OUTPUT);        //max485 I/O control
    digitalWrite(control_pin, LOW);     //set 485 as input
  }

  if(mUART == 0)
  {
    pinMode(0, INPUT); 
    UBRR0H   = 0;
    UBRR0L   = speed_dmx;
    UCSR0A  |= (1<<U2X0);
    UCSR0C  |= (3<<UCSZ00)|(1<<USBS0);
    UCSR0B  |= (1<<RXEN0) |(1<<RXCIE0);
  }
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  else if(mUART == 1)
  {
    pinMode(19, INPUT); 
    UBRR1H   = 0;
    UBRR1L   = speed_dmx;
    UCSR1A  |= (1<<U2X1);
    UCSR1C  |= (3<<UCSZ10)|(1<<USBS1);
    UCSR1B  |= (1<<RXEN1) |(1<<RXCIE1);
  }
  else if(mUART == 2)
  {
    pinMode(17, INPUT); 
    UBRR2H   = 0;
    UBRR2L   = speed_dmx;
    UCSR2A  |= (1<<U2X2); 
    UCSR2C  |= (3<<UCSZ20)|(1<<USBS2);
    UCSR2B  |= (1<<RXEN2) |(1<<RXCIE2);
  }
  else if(mUART == 3)
  {
    pinMode(15, INPUT); 
    UBRR3H   = 0;
    UBRR3L   = speed_dmx; 
    UCSR3A  |= (1<<U2X3);
    UCSR3C  |= (3<<UCSZ30)|(1<<USBS3);
    UCSR3B  |= (1<<RXEN3) |(1<<RXCIE3);
  }
#endif

  gRxState = IDLE;
  RxBuffer = (uint8_t*)malloc(rx_channels);   // allocate mem for buffer
  memset((uint8_t*)RxBuffer, 0, rx_channels); // fill buffer with 0's
  sei();          //enable interrupts
}

// *************** DMX Reception ISR ****************
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  #if defined(USE_UART0)
    ISR (SIG_USART0_RECV)
    {
      ArduinoDmx0.Process_ISR_RX(0);
    }
  #endif
  #if defined(USE_UART1)
    ISR (SIG_USART1_RECV)
    {
      ArduinoDmx1.Process_ISR_RX(1);
    }
  #endif
  #if defined(USE_UART2)
    ISR (SIG_USART2_RECV)
    {
      ArduinoDmx2.Process_ISR_RX(2);
    }
  #endif
  #if defined(USE_UART3)
    ISR (SIG_USART3_RECV)
    {
      ArduinoDmx3.Process_ISR_RX(3);
    }
  #endif
#elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
  #if defined(USE_UART0)
    ISR (USART_RX_vect)
    {
        ArduinoDmx0.Process_ISR_RX(0);
    } 
  #endif
#endif

void CArduinoDmx::Process_ISR_RX(uint8_t rx_isr_number)
{
  if(rx_isr_number == 0)
  {
    USARTstate = UCSR0A;                  //get state
    RxByte     = UDR0;                //get data
    RxState    = gRxState;                    //just get once from SRAM!!!
    if (USARTstate &(1<<FE0))         //check for break
    {                   
      UCSR0A  &= ~(1<<FE0);                 //reset flag
      RxCount  = rx_address;                  //reset frame counter
      gRxState = BREAK;
    }
  }
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)

  else if(rx_isr_number == 1)
  {
    USARTstate = UCSR1A;                  //get state
    RxByte     = UDR1;                //get data
    RxState    = gRxState;                    //just get once from SRAM!!!
    if (USARTstate &(1<<FE1))         //check for break
    {                   
      UCSR1A  &= ~(1<<FE1);                 //reset flag
      RxCount  = rx_address;                  //reset frame counter
      gRxState = BREAK;
    }
  }
  else if(rx_isr_number == 2)
  {
    USARTstate = UCSR2A;                  //get state
    RxByte     = UDR2;                //get data
    RxState    = gRxState;                    //just get once from SRAM!!!
    if (USARTstate &(1<<FE2))         //check for break
    {                   
      UCSR2A  &= ~(1<<FE2);                 //reset flag
      RxCount  = rx_address;                  //reset frame counter
      gRxState = BREAK;
    }
  }
  else if(rx_isr_number == 3)
  {
    USARTstate = UCSR3A;                  //get state
    RxByte     = UDR3;                //get data
    RxState    = gRxState;                    //just get once from SRAM!!!
    if (USARTstate &(1<<FE3))         //check for break
    {                   
      UCSR3A  &= ~(1<<FE3);                 //reset flag
      RxCount  = rx_address;                  //reset frame counter
      gRxState = BREAK;
    }
  }
#endif

  if (RxState == BREAK)
  {
    if (RxByte == 0) 
    {
      gRxState = STARTB;                          //normal start code detected
      gRxPnt   = ((uint8_t*)RxBuffer + 1);
    }
    else 
      gRxState = IDLE;
  }
  else if (RxState == STARTB)
  {
    if (--RxCount == 0)                         //start address reached?
    {
      gRxState   = STARTADR;
      RxBuffer[0]= RxByte;
    }
  }
  else if (RxState == STARTADR)
  {
    RxPnt  = gRxPnt;
    *RxPnt = RxByte;
    if (++RxPnt >= (RxBuffer + rx_channels))    //all ch received?
    {
      gRxState= IDLE;
      if (*RXisrCallback) RXisrCallback(mUART);   // fire callback for read data
    }
    else 
    {
      gRxPnt = RxPnt;
    }
  }                         
}

// *************** DMX Transmision ISR ****************
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  #if defined(USE_UART0)
    ISR(SIG_USART0_TRANS)
    {
      ArduinoDmx0.Process_ISR_TX(0);
    }
  #endif
  #if defined(USE_UART1)
    ISR(SIG_USART1_TRANS)
    {
      ArduinoDmx1.Process_ISR_TX(1);
    }
  #endif
  #if defined(USE_UART2)
    ISR(SIG_USART2_TRANS)
    {
      ArduinoDmx2.Process_ISR_TX(2);
    }
  #endif
  #if defined(USE_UART3)
    ISR(SIG_USART3_TRANS)
    {
      ArduinoDmx3.Process_ISR_TX(3);
    }
  #endif
#elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
  #if defined(USE_UART0)
    ISR(USART_TX_vect)
    {
      ArduinoDmx0.Process_ISR_TX(0);
    }
  #endif
#endif


void CArduinoDmx::Process_ISR_TX(uint8_t tx_isr_number)
{
  TxState = gTxState;

  if(tx_isr_number == 0)
  {
    if (TxState == TXBREAK) //BREAK + MAB
    {
      UBRR0H   = 0;
      UBRR0L   = speed_break;
      UDR0     = 0;                                 //send break
      gTxState = TXSTARTB;
    }
    else if (TxState == TXSTARTB)
    {
      UBRR0H   = 0;
      UBRR0L   = speed_dmx;
      UDR0     = 0;                                 //send start byte
      gTxState = TXDATA;
      gCurTxCh = 0;
    }
    else
    {
      #if defined(USE_INTERBYTE_DELAY)
        delay_gap();
      #endif    
      CurTxCh = gCurTxCh;
      UDR0 = TxBuffer[CurTxCh++];               //send data
      if (CurTxCh == tx_channels)
      {
        if (*TXisrCallback) TXisrCallback(0); // fire callback for update data
        gTxState = TXBREAK;   // new break if all ch sent
      }
      else 
      {
        gCurTxCh = CurTxCh;
      }
    }
  }
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)

  else if(tx_isr_number == 1)
  {
    if (TxState == TXBREAK)
    {
      UBRR1H   = 0;
      UBRR1L   = speed_break;
      UDR1     = 0;                                 //send break
      gTxState = TXSTARTB;
    }
    else if (TxState == TXSTARTB)
    {
      UBRR1H   = 0;
      UBRR1L   = speed_dmx;
      UDR1     = 0;                                 //send start byte
      gTxState = TXDATA;
      gCurTxCh = 0;
    }
    else
    {
      #if defined(USE_INTERBYTE_DELAY)
        delay_gap();
      #endif    
      CurTxCh = gCurTxCh;
      UDR1 = TxBuffer[CurTxCh++];               //send data
      if (CurTxCh == tx_channels)
      {
        if (*TXisrCallback) TXisrCallback(1); // fire callback for update data
        gTxState = TXBREAK;   // new break if all ch sent
      }
      else 
      {
        gCurTxCh = CurTxCh;
      }
    }
  }
  else if(tx_isr_number == 2)
  {
    if (TxState == TXBREAK)
    {
      UBRR2H   = 0;
      UBRR2L   = speed_break;
      UDR2     = 0;                                 //send break
      gTxState = TXSTARTB;
    }
    else if (TxState == TXSTARTB)
    {
      UBRR2H   = 0;
      UBRR2L   = speed_dmx;
      UDR2     = 0;                                 //send start byte
      gTxState = TXDATA;
      gCurTxCh = 0;
    }
    else
    {
      #if defined(USE_INTERBYTE_DELAY)
        delay_gap();
      #endif   
      CurTxCh = gCurTxCh;
      UDR2 = TxBuffer[CurTxCh++];               //send data
      if (CurTxCh == tx_channels)
      {
        if (*TXisrCallback) TXisrCallback(2); // fire callback for update data
        gTxState = TXBREAK;   // new break if all ch sent
      }
      else 
      {
        gCurTxCh = CurTxCh;
      }
    }
  }
  else if(tx_isr_number == 3)
  {
    if (TxState == TXBREAK)
    {
      UBRR3H   = 0;
      UBRR3L   = speed_break;
      UDR3     = 0;                                 //send break
      gTxState = TXSTARTB;
    }
    else if (TxState == TXSTARTB)
    {
      UBRR3H   = 0;
      UBRR3L   = speed_dmx;
      UDR3     = 0;                                 //send start byte
      gTxState = TXDATA;
      gCurTxCh = 0;
    }
    else
    {
      #if defined(USE_INTERBYTE_DELAY)
        delay_gap();
      #endif
      CurTxCh = gCurTxCh;
      UDR3 = TxBuffer[CurTxCh++];               //send data
      if (CurTxCh == tx_channels)
      {
        if (*TXisrCallback) TXisrCallback(3); // fire callback for update data
        gTxState = TXBREAK;   // new break if all ch sent
      }
      else 
      {
        gCurTxCh = CurTxCh;
      }
    }
  }
#endif
}

void CArduinoDmx::set_speed(uint8_t mode)
{
  if(mode == 0)
  {
    speed_dmx   = DMX_512;    // DMX-512  (250 kbaud - 512 channels) Standard USITT DMX-512
    speed_break = BREAK_512;
  }
  else if(mode == 1)
  {
    speed_dmx   = DMX_1024;   // DMX-1024 (500 kbaud - 1024 channels) Completely non standard, but usefull ;)
    speed_break = BREAK_1024;
  }
  else if(mode == 2)
  {
    speed_dmx   = DMX_2048;   // DMX-2048 (1000 kbaud - 2048 channels) Used by manufacturers as DMX1000K, DMX-4x or DMX-1M ???
    speed_break = BREAK_2048;
  }  
}

#if defined(USE_INTERBYTE_DELAY)

void CArduinoDmx::delay_gap() // rare cases of equipment non full DMX-512 compliant, need this
{
  if(dmx_mode == 0)
  {
    _delay_us(IBG_512);
  }
  else if(dmx_mode == 1)
  {
    _delay_us(IBG_1024);
  }
  else if(dmx_mode == 2)
  {
    _delay_us(IBG_2048);
  }
}
#endif

myFile.ino

/*-----------------------------------------------------------------------------
        include files
------------------------------------------------------------------------------*/
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <lib_dmx.h>

#define DMX512  (0)

byte mac[] = { 0xDD, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10,0,0,2); // TODO: assign ip address from DHCP
unsigned int multicastPort = 8888;
IPAddress multicastIp(239,0,0,57);

char packetBuffer[UDP_TX_PACKET_MAX_SIZE];

EthernetUDP Udp;

int sensorValue = 0;
int ledState = 0;
int DMXPin = 0;

void setup() {

  // DMX
  ArduinoDmx1.set_control_pin(DMXPin);
  ArduinoDmx1.set_rx_channels(1);
  ArduinoDmx1.set_tx_channels(5);
  ArduinoDmx1.init_tx(DMX512);

  Ethernet.begin(mac,ip);
  Udp.beginMulti( multicastIp, multicastPort );
  Serial.begin(9600);

}

void loop() {
  int packetSize = Udp.parsePacket();
  if(packetSize){
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i =0; i < 4; i++){
      Serial.print(remote[i], DEC);
      if (i < 3){
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer[0]);

    ArduinoDmx1.TxBuffer[0] = (int)packetBuffer;
  }
}

2 Answers 2

1

It seems that, from the discussion on the page you link to, the author of the DMX Library himself is not sure how to get this to run.

The easiest solution, it seems to me, is to modify HardwareSerial.cpp in your Arduino core to no longer use that interrupt vector.

1
  • I was hoping to prevent editing of core libraries, but I guess there's no other option... Thanks :)
    – jdepypere
    Commented May 1, 2014 at 19:06
3

You use the Serial.begin(9600) and other Serial functions in your sketch. Therefore the HardwareSerial library is included. Just avoid the Serial stuff in your sketch, it won't work anyway, because the DMX uses the same serial port.

Have a look to "Don't use the Arduino Serial implementation" in http://www.mathertel.de/Arduino/DMXSerial.aspx The Four Universes DMX 512 Library works the same way as the DMXSerial library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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