DMX512 library test http://mbed.org/users/okini3939/notebook/dmx512

Dependencies:   DMX mbed

Files at this revision

API Documentation at this revision

Comitter:
okini3939
Date:
Tue May 03 12:13:42 2011 +0000
Child:
1:25b5d99157c4
Commit message:

Changed in this revision

DMX/DMX.cpp Show annotated file Show diff for this revision Revisions of this file
DMX/DMX.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DMX/DMX.cpp	Tue May 03 12:13:42 2011 +0000
@@ -0,0 +1,138 @@
+/*
+ * DMX512 send/recv library
+ * Copyright (c) 2011 Hiroshi Suga
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
+
+/** @file DMX.cpp
+ * @brief DMX512 send/recv
+ */
+
+#include "mbed.h"
+#include "DMX.h"
+
+DMX::DMX (PinName p_tx, PinName p_rx) : dmx(p_tx, p_rx) {
+    int i;
+
+    for (i = 0; i < DMX_SIZE; i ++) {
+        data_tx[i] = 0;
+        data_rx[i] = 0;
+    }
+    mode_tx = DMX_MODE_BEGIN;
+    mode_rx = DMX_MODE_BEGIN;
+
+    dmx.baud(250000);
+    dmx.format(8, Serial::None, 2);
+    dmx.attach(this, &DMX::int_rx, Serial::RxIrq);
+
+    ticker01.attach_us(this, &DMX::int_timer, 100); // 10kHz
+    
+    if (p_tx == p9) {
+      uart_lcr = &LPC_UART3->LCR;
+    } else
+    if (p_tx == p13) {
+      uart_lcr = &LPC_UART1->LCR;
+    } else
+    if (p_tx == p28) {
+      uart_lcr = &LPC_UART2->LCR;
+    }
+    if (p_rx == p10) {
+      uart_lsr = &LPC_UART3->LSR;
+    } else
+    if (p_rx == p14) {
+      uart_lsr = &LPC_UART1->LSR;
+    } else
+    if (p_rx == p27) {
+      uart_lsr = &LPC_UART2->LSR;
+    }
+}
+
+void DMX::put (int addr, int data) {
+    data_tx[addr] = data;
+}
+
+int DMX::get (int addr) {
+    return data_rx[addr];
+}
+
+void DMX::int_timer () {
+
+    switch (mode_tx) {
+    case DMX_MODE_BEGIN:
+        // Break Time
+        *uart_lcr |= (1 << 6);
+        addr_tx = 0;
+        mode_tx = DMX_MODE_BREAK;
+        break;
+
+    case DMX_MODE_BREAK:
+        addr_tx ++;
+        if (addr_tx >= DMX_TIME_BREAK) {
+            // Mark After Break
+            *uart_lcr &= ~(1 << 6);
+            addr_tx = 0;
+            mode_tx = DMX_MODE_MAB;
+        }
+        break;
+
+    case DMX_MODE_MAB:
+        addr_tx ++;
+        if (addr_tx >= DMX_TIME_MAB) {
+            // Start code
+            addr_tx = 0;
+            mode_tx = DMX_MODE_DATA;
+            dmx.attach(this, &DMX::int_tx, Serial::TxIrq);
+            dmx.putc(0);
+        }
+        break;
+    }
+}
+
+void DMX::int_tx () {
+    // Data
+    if (mode_tx == DMX_MODE_DATA) {
+        dmx.putc(data_tx[addr_tx]);
+        addr_tx ++;
+        
+        if (addr_tx >= DMX_SIZE) {
+            dmx.attach(0, Serial::TxIrq);
+            mode_tx = DMX_MODE_BEGIN;
+        }
+    }
+}
+
+void DMX::int_rx () {
+    int flg, dat;
+
+    flg = *uart_lsr;
+    dat = dmx.getc();
+
+    if (flg & (1 << 3) || flg & (1 << 4)) {
+        // Break Time
+        mode_rx = DMX_MODE_BREAK;
+        return;
+    }
+
+    if (mode_rx == DMX_MODE_BREAK) {
+
+        // Start Code
+        if (dat == 0) {
+            addr_rx = 0;
+            mode_rx = DMX_MODE_DATA;
+        } else {
+            mode_rx = DMX_MODE_ERROR;
+        }
+
+    } else
+    if (mode_rx == DMX_MODE_DATA) {
+
+        // Data
+        data_rx[addr_rx] = dat;
+        addr_rx ++;
+        
+        if (addr_rx >= DMX_SIZE) {
+            mode_rx = DMX_MODE_BEGIN;
+        }
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DMX/DMX.h	Tue May 03 12:13:42 2011 +0000
@@ -0,0 +1,73 @@
+/*
+ * DMX512 send/recv library
+ * Copyright (c) 2011 Hiroshi Suga
+ * Released under the MIT License: http://mbed.org/license/mit
+ */
+
+/** @file DMX.h
+ * @brief DMX512 send/recv
+ */
+ 
+#ifndef DMX_H
+#define DMX_H
+
+#include "mbed.h"
+
+#define DMX_SIZE 512
+#define DMX_TIME_BREAK 2 // 200us
+#define DMX_TIME_MAB 1 // 100us
+
+enum DMX_MODE {
+    DMX_MODE_BEGIN,
+    DMX_MODE_START,
+    DMX_MODE_BREAK,
+    DMX_MODE_MAB,
+    DMX_MODE_DATA,
+    DMX_MODE_ERROR,
+};
+
+/** brief DMX512 class (sender/client)
+ */
+class DMX {
+public:
+    /** init DMX class
+     * @param p_tx TX serial port (p9, p13, p28)
+     * @param p_rx RX serial port (p10, p14, p27)
+     */
+    DMX (PinName p_tx, PinName p_rx); 
+
+    /** Send the message
+     * @param addr DMX data address (0-511)
+     * @param data DMX data (0-255)
+     */
+    void put (int addr, int data);
+
+    /** Send the message
+     * @param addr DMX data address (0-511)
+     * @return DMX data (0-255)
+     */
+    int get (int addr);
+
+protected:
+
+    void int_timer ();
+    void int_tx ();
+    void int_rx ();
+//    void int_tx (MODSERIAL_IRQ_INFO *q);
+//    void int_rx (MODSERIAL_IRQ_INFO *q);
+
+    Serial dmx;
+//    MODSERIAL dmx;
+    Ticker ticker01;
+    __IO uint8_t *uart_lcr;
+    __I  uint8_t *uart_lsr;
+    volatile DMX_MODE mode_tx, mode_rx;
+    volatile int addr_tx, addr_rx;
+    unsigned char data_tx[512];
+    unsigned char data_rx[512];
+
+private:
+
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue May 03 12:13:42 2011 +0000
@@ -0,0 +1,35 @@
+#include "mbed.h"
+#include "DMX.h"
+
+DMX dmx(p9, p10);
+//DMX dmx(p13, p14);
+//DMX dmx(p28, p27);
+
+DigitalOut myled(LED1);
+Serial pc(USBTX, USBRX);
+
+int main() {
+    int i, j;
+
+    j = 0;
+    while(1) {
+        myled = 1;
+        wait(0.2);
+        myled = 0;
+        wait(0.2);
+
+        j = j + 10;
+        if (j >= 256) j = 0;
+        for (i = 0; i < DMX_SIZE; i ++) {
+            dmx.put(i, j);
+        }
+
+        pc.printf("tx: %d\r\n", j);
+        
+        pc.printf("rx: ", dmx.get(0));
+        for (i = 0; i < 16; i ++) {
+            pc.printf("%d ", dmx.get(i));
+        }
+        pc.printf("\r\n");
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue May 03 12:13:42 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912