Adapt a C library to arduino

Hi!
For an earlier project I’ve written a library for receiving DMX with an AVR. Unfortunately it’s written in C.
Is there any chance of adapting those code snippets for the use with an Arduino? They are working well and it would be great to use them with my Arduino!

/********************************************************************
 * dmx_recv.h
 *
 * AVR GCC / ATtiny2313 (8MHz)
 * DMX512 receive
 *******************************************************************/

#include <avr/io.h>
#include <avr/interrupt.h>

#define DMX_CH_BASE 0
#define DMX_BUF_SIZE 24
#define DMX_SYNC 3 // 3:8MHz, 7:16MHz, 9:20MHz

void wait_dmx ();
unsigned char *init_dmx (int, int);
void set_dmx_channel(int);
/********************************************************************
 * dmx_recv.c
 *
 * AVR GCC / ATtiny2313 (8MHz)
 * DMX512 Receive
 *******************************************************************/

#include "dmx_recv.h"

static volatile unsigned char dmx_buf[DMX_BUF_SIZE];
static volatile int dmx_ch, dmx_size, dmx_addr;
static volatile char dmx_mode;


// complete receive?
void wait_dmx () {
	while (dmx_mode != 4);
	dmx_mode = 0;
}

// DMX receive, USART interrupt
ISR(SIG_USART0_RECV) {
    register unsigned char flg, dat;

    flg = UCSRA;
    dat = UDR;

    if (flg & 0x0c) {
        // data over run, parity error

        dmx_mode = 99; // data error
        while (UCSRA & 0x80) { dat = UDR; } // buffer clear
        return;

    } else
    if (flg & 0x10) {
        // Frame error, detect BreakTime

        dmx_mode = 1;
        return;

    }

    if (dmx_mode == 1) {
        // Start Code

        if (dat == 0) {
            // Start Code = 0
            dmx_addr = 0;
            dmx_mode = 2;
        } else {
            // Start Code error
            dmx_mode = 99;
        }

    } else
    if (dmx_mode == 2) {
        // DMX base address

        if (dmx_addr == dmx_ch) {
            dmx_addr = 0;
            dmx_mode = 3;
        } else {
            dmx_addr ++;
        }
    }

    if (dmx_mode == 3) {
        // recieve the data

        dmx_buf[dmx_addr] = dat;
        dmx_addr ++;

        if (dmx_addr >= dmx_size) {
            // complete receive
            dmx_mode = 4;
        }
    }

}

// initialize USART
unsigned char *init_dmx (int ch, int size) {
    int i;

    // buffer clear
    dmx_mode = 0;
    dmx_addr = 0;
	dmx_ch = ch;
	dmx_size = size;
	if (size > DMX_BUF_SIZE) {
		dmx_size = DMX_BUF_SIZE;
	}
    for (i = 0; i < dmx_size; i ++) {
        dmx_buf[i] = 0;
    }

    // set USART
    UBRRH = 0;
    UBRRL = DMX_SYNC; // 250kbps
    UCSRA = _BV(U2X); // U2X
    UCSRC = _BV(USBS) | _BV(UCSZ1) | _BV(UCSZ0); // stop code 2bitAdata 8bit
    UCSRB = _BV(RXCIE) | _BV(RXEN); // receive, interrupt

    return (unsigned char *)&dmx_buf;
}

void set_dmx_channel(int ch)
{ dmx_ch=ch;
  dmx_mode=0;
}

Sebi: Unfortunately it's written in C.

Why is that unfortunate? That's what Arduino uses. Your library comments mention AVR GCC, which is the same compiler used by Arduino.

Yeah… you’re right! Sometimes it’s too easy :wink:

I’ve tried to throw both files together and remove recursive includes.
All I get is “F:\Apps\arduino-1.0-windows\arduino-1.0\libraries\dmxrecv/dmx_recv.h:30: error: expected constructor, destructor, or type conversion before ‘(’ token”
Line 30 is “ISR(SIG_USART0_RECV) {”

Okay, maxbe not so easy :wink:
I’ve got some experience in C and some in the Arduino-IDE, but I’ve never tried to build my own library, so I’m a little stuck.

/********************************************************************
 * dmx_recv.h
 *
 * AVR GCC / ATtiny2313 (8MHz)
 * DMX512 recieve
 *******************************************************************/

//#include <avr/io.h>
//#include <avr/interrupt.h>

#define DMX_CH_BASE 0
#define DMX_BUF_SIZE 24
#define DMX_SYNC 3 // 3:8MHz, 7:16MHz, 9:20MHz

void wait_dmx ();
unsigned char *init_dmx (int, int);
void set_dmx_channel(int);
static volatile unsigned char dmx_buf[DMX_BUF_SIZE];
static volatile int dmx_ch, dmx_size, dmx_addr;
static volatile char dmx_mode;


// complete receive?
void wait_dmx () {
	while (dmx_mode != 4);
	dmx_mode = 0;
}

// DMX receive, USART interrupt
ISR(SIG_USART0_RECV) {
    register unsigned char flg, dat;

    flg = UCSRA;
    dat = UDR;

    if (flg & 0x0c) {
        // data over run, parity error

        dmx_mode = 99; // data error
        while (UCSRA & 0x80) { dat = UDR; } // buffer clear
        return;

    } else
    if (flg & 0x10) {
        // Frame error, detect BreakTime

        dmx_mode = 1;
        return;

    }

    if (dmx_mode == 1) {
        // Start Code

        if (dat == 0) {
            // Start Code = 0
            dmx_addr = 0;
            dmx_mode = 2;
        } else {
            // Start Code error
            dmx_mode = 99;
        }

    } else
    if (dmx_mode == 2) {
        // DMX base address

        if (dmx_addr == dmx_ch) {
            dmx_addr = 0;
            dmx_mode = 3;
        } else {
            dmx_addr ++;
        }
    }

    if (dmx_mode == 3) {
        // recieve the data

        dmx_buf[dmx_addr] = dat;
        dmx_addr ++;

        if (dmx_addr >= dmx_size) {
            // complete receive
            dmx_mode = 4;
        }
    }

}

// initialize USART
unsigned char *init_dmx (int ch, int size) {
    int i;

    // buffer clear
    dmx_mode = 0;
    dmx_addr = 0;
	dmx_ch = ch;
	dmx_size = size;
	if (size > DMX_BUF_SIZE) {
		dmx_size = DMX_BUF_SIZE;
	}
    for (i = 0; i < dmx_size; i ++) {
        dmx_buf[i] = 0;
    }

    // set USART
    UBRRH = 0;
    UBRRL = DMX_SYNC; // 250kbps
    UCSRA = _BV(U2X); // U2X
    UCSRC = _BV(USBS) | _BV(UCSZ1) | _BV(UCSZ0); // stop code 2bitAdata 8bit
    UCSRB = _BV(RXCIE) | _BV(RXEN); // receive, interrupt

    return (unsigned char *)&dmx_buf;
}

void set_dmx_channel(int ch)
{ dmx_ch=ch;
  dmx_mode=0;
}

You have created an interrupt for the USART, but Arduino uses that. The Rx and Tx pins are used with the Serial functions: http://arduino.cc/en/Reference/Serial They are also used by the boot section to download the firmware into the Arduino.

So you might want to change your code, and use the Arduino functions like: Serial.println("Hello World"); If you don't use the Serial functions at all, perhaps you can use your own ISR, but I'm not sure about that.