DMX512 send/recv library with STM32 slave support originally from http://mbed.org/users/okini3939/notebook/dmx512

Fork of DMX by Suga koubou

Committer:
okini3939
Date:
Mon Sep 23 09:15:57 2013 +0000
Revision:
12:1f176eee2d28
Parent:
11:cb132e066057
Child:
13:9841af9ac344
add clear()

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okini3939 0:cbff6bf41542 1 /*
okini3939 0:cbff6bf41542 2 * DMX512 send/recv library
okini3939 6:9e7b4eeac6ec 3 * Copyright (c) 2013 Hiroshi Suga
okini3939 0:cbff6bf41542 4 * Released under the MIT License: http://mbed.org/license/mit
okini3939 0:cbff6bf41542 5 */
okini3939 0:cbff6bf41542 6
okini3939 7:16d6874076dd 7 /** @file
okini3939 0:cbff6bf41542 8 * @brief DMX512 send/recv
okini3939 0:cbff6bf41542 9 */
okini3939 0:cbff6bf41542 10
okini3939 0:cbff6bf41542 11 #include "mbed.h"
okini3939 0:cbff6bf41542 12 #include "DMX.h"
okini3939 0:cbff6bf41542 13
okini3939 5:72039cd4c874 14 DMX::DMX (PinName p_tx, PinName p_rx) : _dmx(p_tx, p_rx) {
okini3939 0:cbff6bf41542 15
okini3939 12:1f176eee2d28 16 clear();
okini3939 4:dd0544c80096 17 // mode_tx = DMX_MODE_BEGIN;
okini3939 4:dd0544c80096 18 mode_tx = DMX_MODE_STOP;
okini3939 0:cbff6bf41542 19 mode_rx = DMX_MODE_BEGIN;
okini3939 0:cbff6bf41542 20 is_recived = 0;
okini3939 0:cbff6bf41542 21 is_sent = 0;
okini3939 0:cbff6bf41542 22
okini3939 5:72039cd4c874 23 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
okini3939 11:cb132e066057 24 if (p_rx == P0_3) {
okini3939 11:cb132e066057 25 _uart = (LPC_UART_TypeDef*)LPC_UART0;
okini3939 11:cb132e066057 26 NVIC_SetPriority(UART0_IRQn, 1);
okini3939 0:cbff6bf41542 27 } else
okini3939 0:cbff6bf41542 28 if (p_rx == p14) {
okini3939 8:d4a45bba41d2 29 _uart = (LPC_UART_TypeDef*)LPC_UART1;
okini3939 0:cbff6bf41542 30 NVIC_SetPriority(UART1_IRQn, 1);
okini3939 0:cbff6bf41542 31 } else
okini3939 0:cbff6bf41542 32 if (p_rx == p27) {
okini3939 8:d4a45bba41d2 33 _uart = LPC_UART2;
okini3939 0:cbff6bf41542 34 NVIC_SetPriority(UART2_IRQn, 1);
okini3939 11:cb132e066057 35 } else
okini3939 11:cb132e066057 36 if (p_rx == p10) {
okini3939 11:cb132e066057 37 _uart = LPC_UART3;
okini3939 11:cb132e066057 38 NVIC_SetPriority(UART3_IRQn, 1);
okini3939 0:cbff6bf41542 39 }
okini3939 5:72039cd4c874 40 #elif defined(TARGET_LPC11U24)
okini3939 5:72039cd4c874 41 if (p_rx == p10) {
okini3939 8:d4a45bba41d2 42 _uart = LPC_USART;
okini3939 5:72039cd4c874 43 NVIC_SetPriority(UART_IRQn, 1);
okini3939 5:72039cd4c874 44 }
stanly88 10:b748aab8404c 45 #elif defined(TARGET_LPC11XX)
okini3939 11:cb132e066057 46 // LPC1114 support by Stanly Chen
stanly88 10:b748aab8404c 47 if (p_rx == P1_6) {
stanly88 10:b748aab8404c 48 _uart = (LPC_UART_TypeDef*) UART_0;
stanly88 10:b748aab8404c 49 NVIC_SetPriority(UART_IRQn, 1);
stanly88 10:b748aab8404c 50 }
okini3939 5:72039cd4c874 51 #endif
okini3939 0:cbff6bf41542 52
okini3939 5:72039cd4c874 53 _dmx.baud(250000);
okini3939 5:72039cd4c874 54 _dmx.format(8, Serial::None, 2);
okini3939 5:72039cd4c874 55 _dmx.attach(this, &DMX::int_rx, Serial::RxIrq);
okini3939 0:cbff6bf41542 56
okini3939 4:dd0544c80096 57 // timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BETWEEN);
okini3939 0:cbff6bf41542 58 }
okini3939 0:cbff6bf41542 59
okini3939 6:9e7b4eeac6ec 60 void DMX::put (int addr, int data) {
okini3939 6:9e7b4eeac6ec 61 if (addr < 0 || addr >= DMX_SIZE) return;
okini3939 6:9e7b4eeac6ec 62 data_tx[addr] = data;
okini3939 0:cbff6bf41542 63 }
okini3939 0:cbff6bf41542 64
okini3939 6:9e7b4eeac6ec 65 void DMX::put (unsigned char *buf, int addr, int len) {
okini3939 6:9e7b4eeac6ec 66 if (addr < 0 || addr >= DMX_SIZE) return;
okini3939 6:9e7b4eeac6ec 67 if (len > DMX_SIZE - addr) len = DMX_SIZE - addr;
okini3939 6:9e7b4eeac6ec 68 memcpy(&data_tx[addr], buf, len);
okini3939 6:9e7b4eeac6ec 69 }
okini3939 6:9e7b4eeac6ec 70
okini3939 6:9e7b4eeac6ec 71 int DMX::get (int addr) {
okini3939 6:9e7b4eeac6ec 72 if (addr < 0 || addr >= DMX_SIZE) return -1;
okini3939 6:9e7b4eeac6ec 73 return data_rx[addr];
okini3939 6:9e7b4eeac6ec 74 }
okini3939 6:9e7b4eeac6ec 75
okini3939 6:9e7b4eeac6ec 76 void DMX::get (unsigned char *buf, int addr, int len) {
okini3939 6:9e7b4eeac6ec 77 if (addr < 0 || addr >= DMX_SIZE) return;
okini3939 6:9e7b4eeac6ec 78 if (len > DMX_SIZE - addr) len = DMX_SIZE - addr;
okini3939 6:9e7b4eeac6ec 79 memcpy(buf, &data_rx[addr], len);
okini3939 0:cbff6bf41542 80 }
okini3939 0:cbff6bf41542 81
okini3939 0:cbff6bf41542 82 void DMX::int_timer () {
okini3939 0:cbff6bf41542 83
okini3939 0:cbff6bf41542 84 switch (mode_tx) {
okini3939 0:cbff6bf41542 85 case DMX_MODE_BEGIN:
okini3939 0:cbff6bf41542 86 // Break Time
okini3939 0:cbff6bf41542 87 timeout01.detach();
okini3939 8:d4a45bba41d2 88 _uart->LCR |= (1 << 6);
okini3939 0:cbff6bf41542 89 mode_tx = DMX_MODE_BREAK;
okini3939 0:cbff6bf41542 90 timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BREAK);
okini3939 0:cbff6bf41542 91 break;
okini3939 0:cbff6bf41542 92
okini3939 0:cbff6bf41542 93 case DMX_MODE_BREAK:
okini3939 0:cbff6bf41542 94 // Mark After Break
okini3939 0:cbff6bf41542 95 timeout01.detach();
okini3939 8:d4a45bba41d2 96 _uart->LCR &= ~(1 << 6);
okini3939 0:cbff6bf41542 97 mode_tx = DMX_MODE_MAB;
okini3939 0:cbff6bf41542 98 timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_MAB);
okini3939 0:cbff6bf41542 99 break;
okini3939 0:cbff6bf41542 100
okini3939 0:cbff6bf41542 101 case DMX_MODE_MAB:
okini3939 0:cbff6bf41542 102 // Start code
okini3939 0:cbff6bf41542 103 timeout01.detach();
okini3939 0:cbff6bf41542 104 addr_tx = 0;
okini3939 0:cbff6bf41542 105 mode_tx = DMX_MODE_DATA;
okini3939 5:72039cd4c874 106 _dmx.attach(this, &DMX::int_tx, Serial::TxIrq);
okini3939 2:d7677060f8eb 107 #ifdef DMX_UART_DIRECT
okini3939 9:e687f321c428 108 while(!(_uart->LSR & (1<<5)));
okini3939 9:e687f321c428 109 _uart->THR = DMX_START_CODE;
okini3939 2:d7677060f8eb 110 #else
okini3939 9:e687f321c428 111 _dmx.putc(DMX_START_CODE);
okini3939 2:d7677060f8eb 112 #endif
okini3939 0:cbff6bf41542 113 break;
okini3939 0:cbff6bf41542 114 }
okini3939 0:cbff6bf41542 115 }
okini3939 0:cbff6bf41542 116
okini3939 0:cbff6bf41542 117 void DMX::int_tx () {
okini3939 0:cbff6bf41542 118 // Data
okini3939 0:cbff6bf41542 119 if (mode_tx == DMX_MODE_DATA) {
okini3939 0:cbff6bf41542 120 if (addr_tx < DMX_SIZE) {
okini3939 1:f0d988e15810 121 #ifdef DMX_UART_DIRECT
okini3939 8:d4a45bba41d2 122 _uart->THR = (uint8_t)data_tx[addr_tx];
okini3939 0:cbff6bf41542 123 #else
okini3939 5:72039cd4c874 124 _dmx.putc(data_tx[addr_tx]);
okini3939 0:cbff6bf41542 125 #endif
okini3939 0:cbff6bf41542 126 addr_tx ++;
okini3939 0:cbff6bf41542 127 } else {
okini3939 5:72039cd4c874 128 _dmx.attach(0, Serial::TxIrq);
okini3939 0:cbff6bf41542 129 mode_tx = DMX_MODE_BEGIN;
okini3939 0:cbff6bf41542 130 is_sent = 1;
okini3939 0:cbff6bf41542 131 timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BETWEEN);
okini3939 0:cbff6bf41542 132 }
okini3939 0:cbff6bf41542 133 }
okini3939 0:cbff6bf41542 134 }
okini3939 0:cbff6bf41542 135
okini3939 0:cbff6bf41542 136 void DMX::int_rx () {
okini3939 0:cbff6bf41542 137 int flg, dat;
okini3939 0:cbff6bf41542 138
okini3939 8:d4a45bba41d2 139 flg = _uart->LSR;
okini3939 1:f0d988e15810 140 #ifdef DMX_UART_DIRECT
okini3939 8:d4a45bba41d2 141 dat = _uart->RBR;
okini3939 1:f0d988e15810 142 #else
okini3939 5:72039cd4c874 143 dat = _dmx.getc();
okini3939 1:f0d988e15810 144 #endif
okini3939 0:cbff6bf41542 145
okini3939 0:cbff6bf41542 146 if (flg & ((1 << 7)|(1 << 3)|(1 << 4))) {
okini3939 0:cbff6bf41542 147 // Break Time
okini3939 8:d4a45bba41d2 148 if (addr_rx >= 24) {
okini3939 0:cbff6bf41542 149 is_recived = 1;
okini3939 0:cbff6bf41542 150 }
okini3939 0:cbff6bf41542 151 mode_rx = DMX_MODE_BREAK;
okini3939 0:cbff6bf41542 152 return;
okini3939 0:cbff6bf41542 153 }
okini3939 0:cbff6bf41542 154
okini3939 0:cbff6bf41542 155 if (mode_rx == DMX_MODE_BREAK) {
okini3939 0:cbff6bf41542 156
okini3939 0:cbff6bf41542 157 // Start Code
okini3939 9:e687f321c428 158 if (dat == DMX_START_CODE) {
okini3939 0:cbff6bf41542 159 addr_rx = 0;
okini3939 0:cbff6bf41542 160 mode_rx = DMX_MODE_DATA;
okini3939 0:cbff6bf41542 161 } else {
okini3939 0:cbff6bf41542 162 mode_rx = DMX_MODE_ERROR;
okini3939 0:cbff6bf41542 163 }
okini3939 0:cbff6bf41542 164
okini3939 0:cbff6bf41542 165 } else
okini3939 0:cbff6bf41542 166 if (mode_rx == DMX_MODE_DATA) {
okini3939 0:cbff6bf41542 167
okini3939 0:cbff6bf41542 168 // Data
okini3939 0:cbff6bf41542 169 data_rx[addr_rx] = dat;
okini3939 0:cbff6bf41542 170 addr_rx ++;
okini3939 0:cbff6bf41542 171
okini3939 0:cbff6bf41542 172 if (addr_rx >= DMX_SIZE) {
okini3939 0:cbff6bf41542 173 is_recived = 1;
okini3939 0:cbff6bf41542 174 mode_rx = DMX_MODE_BEGIN;
okini3939 0:cbff6bf41542 175 }
okini3939 0:cbff6bf41542 176 }
okini3939 0:cbff6bf41542 177 }
okini3939 0:cbff6bf41542 178
okini3939 4:dd0544c80096 179 void DMX::start () {
okini3939 4:dd0544c80096 180 if (mode_tx == DMX_MODE_STOP) {
okini3939 4:dd0544c80096 181 mode_tx = DMX_MODE_BEGIN;
okini3939 4:dd0544c80096 182 is_sent = 0;
okini3939 4:dd0544c80096 183 timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BETWEEN);
okini3939 4:dd0544c80096 184 }
okini3939 4:dd0544c80096 185 }
okini3939 4:dd0544c80096 186
okini3939 4:dd0544c80096 187 void DMX::stop () {
okini3939 5:72039cd4c874 188 _dmx.attach(0, Serial::TxIrq);
okini3939 3:2eb66b4d99bd 189 timeout01.detach();
okini3939 4:dd0544c80096 190 mode_tx = DMX_MODE_STOP;
okini3939 3:2eb66b4d99bd 191 }
okini3939 12:1f176eee2d28 192
okini3939 12:1f176eee2d28 193 void DMX::clear () {
okini3939 12:1f176eee2d28 194 int i;
okini3939 12:1f176eee2d28 195
okini3939 12:1f176eee2d28 196 for (i = 0; i < DMX_SIZE; i ++) {
okini3939 12:1f176eee2d28 197 data_tx[i] = 0;
okini3939 12:1f176eee2d28 198 data_rx[i] = 0;
okini3939 12:1f176eee2d28 199 }
okini3939 12:1f176eee2d28 200 }