DMX interface (DMX in/out, Art-Net in/out, DMX patch) http://mbed.org/users/okini3939/notebook/dmx-platform/

Dependencies:   ChaNFSSD EthernetNetIf mbed ConfigFile ChaNFS DmxArtNet

Committer:
okini3939
Date:
Thu Mar 01 01:40:07 2012 +0000
Revision:
0:41b699bbda83

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okini3939 0:41b699bbda83 1 /*
okini3939 0:41b699bbda83 2 * NEC 950MHz RF module
okini3939 0:41b699bbda83 3 * H001-000003-001
okini3939 0:41b699bbda83 4 */
okini3939 0:41b699bbda83 5
okini3939 0:41b699bbda83 6 #include "mbed.h"
okini3939 0:41b699bbda83 7 #include "NEC950MHz.h"
okini3939 0:41b699bbda83 8 #include "dbg.h"
okini3939 0:41b699bbda83 9 #include <string.h>
okini3939 0:41b699bbda83 10
okini3939 0:41b699bbda83 11 #define RXD p10
okini3939 0:41b699bbda83 12 #define TXD p9
okini3939 0:41b699bbda83 13 #define RST p12
okini3939 0:41b699bbda83 14
okini3939 0:41b699bbda83 15
okini3939 0:41b699bbda83 16 // host to network short
okini3939 0:41b699bbda83 17 #define htons( x ) ( (( (x) << 8 ) & 0xFF00) | (( (x) >> 8 ) & 0x00FF) )
okini3939 0:41b699bbda83 18 #define ntohs( x ) htons(x)
okini3939 0:41b699bbda83 19 // host to network long
okini3939 0:41b699bbda83 20 #define htonl( x ) ( (( (x) << 24 ) & 0xFF000000) \
okini3939 0:41b699bbda83 21 | (( (x) << 8 ) & 0x00FF0000) \
okini3939 0:41b699bbda83 22 | (( (x) >> 8 ) & 0x0000FF00) \
okini3939 0:41b699bbda83 23 | (( (x) >> 24 ) & 0x000000FF) )
okini3939 0:41b699bbda83 24 #define ntohl( x ) htonl(x)
okini3939 0:41b699bbda83 25
okini3939 0:41b699bbda83 26
okini3939 0:41b699bbda83 27 static Serial rf(TXD, RXD);
okini3939 0:41b699bbda83 28 static DigitalOut rst(RST);
okini3939 0:41b699bbda83 29
okini3939 0:41b699bbda83 30 int send_rf (int msgid, unsigned long dest, char *param, int len) {
okini3939 0:41b699bbda83 31 int i;
okini3939 0:41b699bbda83 32 static int msgno = 0;
okini3939 0:41b699bbda83 33 struct ifMessage ifmsg;
okini3939 0:41b699bbda83 34 unsigned char *buf = (unsigned char *)&ifmsg;
okini3939 0:41b699bbda83 35
okini3939 0:41b699bbda83 36 if (len > 240) len = 240;
okini3939 0:41b699bbda83 37 msgno ++;
okini3939 0:41b699bbda83 38 ifmsg.start = htons(0x0f5a);
okini3939 0:41b699bbda83 39 ifmsg.length = 13 + len;
okini3939 0:41b699bbda83 40 ifmsg.msgid = msgid;
okini3939 0:41b699bbda83 41 ifmsg.msgno = msgno;
okini3939 0:41b699bbda83 42 ifmsg.dstid = htonl(dest);
okini3939 0:41b699bbda83 43 ifmsg.srcid = htonl(0xffffffff);
okini3939 0:41b699bbda83 44 memcpy(ifmsg.parameter, param, len);
okini3939 0:41b699bbda83 45 DBG("send_rf %d / %d\r\n", ifmsg.length, len);
okini3939 0:41b699bbda83 46
okini3939 0:41b699bbda83 47 for (i = 0; i < ifmsg.length; i ++) {
okini3939 0:41b699bbda83 48 rf.putc(buf[i]);
okini3939 0:41b699bbda83 49 }
okini3939 0:41b699bbda83 50
okini3939 0:41b699bbda83 51 return ifmsg.msgno;
okini3939 0:41b699bbda83 52 }
okini3939 0:41b699bbda83 53
okini3939 0:41b699bbda83 54 int read_rf (struct ifMessage *ifmsg) {
okini3939 0:41b699bbda83 55 Timer timeout;
okini3939 0:41b699bbda83 56 int i = 0, len = 0;
okini3939 0:41b699bbda83 57 unsigned char buf[sizeof(struct ifMessage)];
okini3939 0:41b699bbda83 58
okini3939 0:41b699bbda83 59 timeout.start();
okini3939 0:41b699bbda83 60 while (timeout.read_ms() < TIMEOUT) {
okini3939 0:41b699bbda83 61 if (rf.readable()) {
okini3939 0:41b699bbda83 62 buf[i] = rf.getc();
okini3939 0:41b699bbda83 63 if (i == 0 && buf[i] == 0x0f) {
okini3939 0:41b699bbda83 64 i ++;
okini3939 0:41b699bbda83 65 } else
okini3939 0:41b699bbda83 66 if (i == 1 && buf[i] == 0x5a) {
okini3939 0:41b699bbda83 67 i ++;
okini3939 0:41b699bbda83 68 } else
okini3939 0:41b699bbda83 69 if (i == 2) {
okini3939 0:41b699bbda83 70 len = buf[i];
okini3939 0:41b699bbda83 71 i ++;
okini3939 0:41b699bbda83 72 } else
okini3939 0:41b699bbda83 73 if (i >= 3) {
okini3939 0:41b699bbda83 74 i ++;
okini3939 0:41b699bbda83 75 if (i >= len || i >= sizeof(buf)) break;
okini3939 0:41b699bbda83 76 }
okini3939 0:41b699bbda83 77 timeout.reset();
okini3939 0:41b699bbda83 78 }
okini3939 0:41b699bbda83 79 }
okini3939 0:41b699bbda83 80 timeout.stop();
okini3939 0:41b699bbda83 81
okini3939 0:41b699bbda83 82 if (len) {
okini3939 0:41b699bbda83 83 DBG("read_rf %d / %d\r\n", i, len);
okini3939 0:41b699bbda83 84 memcpy(ifmsg, buf, len);
okini3939 0:41b699bbda83 85 }
okini3939 0:41b699bbda83 86 return len;
okini3939 0:41b699bbda83 87 }
okini3939 0:41b699bbda83 88
okini3939 0:41b699bbda83 89 int init_rf (int ch) {
okini3939 0:41b699bbda83 90 int no;
okini3939 0:41b699bbda83 91 struct ifMessage ifmsg;
okini3939 0:41b699bbda83 92 // high power, 100kbps, retry 0
okini3939 0:41b699bbda83 93 char rfconf[] = {POWER_HIGH, ch, BAUD_100k, 1, 0, UART_115200, 1, 0xff, 0xff,
okini3939 0:41b699bbda83 94 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
okini3939 0:41b699bbda83 95 0x00, 0x00, 0x83, 0x41};
okini3939 0:41b699bbda83 96
okini3939 0:41b699bbda83 97 rf.baud(38400);
okini3939 0:41b699bbda83 98
okini3939 0:41b699bbda83 99 rst = 0; // reset
okini3939 0:41b699bbda83 100 wait_ms(50);
okini3939 0:41b699bbda83 101 rst = 1;
okini3939 0:41b699bbda83 102 wait_ms(100);
okini3939 0:41b699bbda83 103
okini3939 0:41b699bbda83 104 no = send_rf(MSGID_WRITE_CONFIG, 0xffffffff, rfconf, sizeof(rfconf));
okini3939 0:41b699bbda83 105 DBG("rfconf %d\r\n", no);
okini3939 0:41b699bbda83 106 // responce
okini3939 0:41b699bbda83 107 if (! read_rf(&ifmsg) || ifmsg.msgid != MSGID_ACK || ifmsg.msgno != no) {
okini3939 0:41b699bbda83 108 return -1;
okini3939 0:41b699bbda83 109 }
okini3939 0:41b699bbda83 110
okini3939 0:41b699bbda83 111 rf.baud(115200);
okini3939 0:41b699bbda83 112
okini3939 0:41b699bbda83 113 return 0;
okini3939 0:41b699bbda83 114 }