DMX512 send/recv library with STM32 slave support originally from http://mbed.org/users/okini3939/notebook/dmx512
Fork of DMX by
DMX.cpp@16:4eeb749806e5, 2015-08-20 (annotated)
- Committer:
- okini3939
- Date:
- Thu Aug 20 09:21:28 2015 +0000
- Revision:
- 16:4eeb749806e5
- Parent:
- 15:4ea4a31c7609
- Child:
- 17:c9198630ba05
fix LPC4088
Who changed what in which revision?
User | Revision | Line number | New 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 | 16:4eeb749806e5 | 22 | memset(data_tx, 0, sizeof(data_tx)); |
okini3939 | 16:4eeb749806e5 | 23 | memset(data_rx, 0, sizeof(data_rx)); |
okini3939 | 0:cbff6bf41542 | 24 | |
okini3939 | 5:72039cd4c874 | 25 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
okini3939 | 11:cb132e066057 | 26 | if (p_rx == P0_3) { |
okini3939 | 11:cb132e066057 | 27 | _uart = (LPC_UART_TypeDef*)LPC_UART0; |
okini3939 | 11:cb132e066057 | 28 | NVIC_SetPriority(UART0_IRQn, 1); |
okini3939 | 0:cbff6bf41542 | 29 | } else |
okini3939 | 0:cbff6bf41542 | 30 | if (p_rx == p14) { |
okini3939 | 8:d4a45bba41d2 | 31 | _uart = (LPC_UART_TypeDef*)LPC_UART1; |
okini3939 | 0:cbff6bf41542 | 32 | NVIC_SetPriority(UART1_IRQn, 1); |
okini3939 | 0:cbff6bf41542 | 33 | } else |
okini3939 | 0:cbff6bf41542 | 34 | if (p_rx == p27) { |
okini3939 | 8:d4a45bba41d2 | 35 | _uart = LPC_UART2; |
okini3939 | 0:cbff6bf41542 | 36 | NVIC_SetPriority(UART2_IRQn, 1); |
okini3939 | 11:cb132e066057 | 37 | } else |
okini3939 | 11:cb132e066057 | 38 | if (p_rx == p10) { |
okini3939 | 11:cb132e066057 | 39 | _uart = LPC_UART3; |
okini3939 | 11:cb132e066057 | 40 | NVIC_SetPriority(UART3_IRQn, 1); |
okini3939 | 0:cbff6bf41542 | 41 | } |
okini3939 | 13:9841af9ac344 | 42 | #elif defined(TARGET_LPC4088) |
okini3939 | 16:4eeb749806e5 | 43 | if (p_rx == p10 || p_rx == P0_26 || p_rx == P4_29) { |
okini3939 | 13:9841af9ac344 | 44 | _uart = LPC_UART3; |
okini3939 | 13:9841af9ac344 | 45 | NVIC_SetPriority(UART3_IRQn, 1); |
okini3939 | 13:9841af9ac344 | 46 | } else |
okini3939 | 13:9841af9ac344 | 47 | if (p_rx == p31) { |
okini3939 | 13:9841af9ac344 | 48 | _uart = (LPC_UART_TypeDef*)LPC_UART4; |
okini3939 | 13:9841af9ac344 | 49 | NVIC_SetPriority(UART4_IRQn, 1); |
okini3939 | 16:4eeb749806e5 | 50 | } else |
okini3939 | 16:4eeb749806e5 | 51 | if (p_rx == P0_3) { |
okini3939 | 16:4eeb749806e5 | 52 | _uart = LPC_UART0; |
okini3939 | 16:4eeb749806e5 | 53 | NVIC_SetPriority(UART0_IRQn, 1); |
okini3939 | 16:4eeb749806e5 | 54 | } else |
okini3939 | 16:4eeb749806e5 | 55 | if (p_rx == P0_16 || p_rx == P2_1 || p_rx == P3_17) { |
okini3939 | 16:4eeb749806e5 | 56 | _uart = (LPC_UART_TypeDef*)LPC_UART1; |
okini3939 | 16:4eeb749806e5 | 57 | NVIC_SetPriority(UART1_IRQn, 1); |
okini3939 | 16:4eeb749806e5 | 58 | } else |
okini3939 | 16:4eeb749806e5 | 59 | if (p_rx == P0_11 || p_rx == P2_9 || p_rx == P4_23) { |
okini3939 | 16:4eeb749806e5 | 60 | _uart = LPC_UART2; |
okini3939 | 16:4eeb749806e5 | 61 | NVIC_SetPriority(UART2_IRQn, 1); |
okini3939 | 13:9841af9ac344 | 62 | } |
okini3939 | 15:4ea4a31c7609 | 63 | #elif defined(TARGET_LPC11UXX) |
okini3939 | 5:72039cd4c874 | 64 | if (p_rx == p10) { |
okini3939 | 8:d4a45bba41d2 | 65 | _uart = LPC_USART; |
okini3939 | 5:72039cd4c874 | 66 | NVIC_SetPriority(UART_IRQn, 1); |
okini3939 | 5:72039cd4c874 | 67 | } |
stanly88 | 10:b748aab8404c | 68 | #elif defined(TARGET_LPC11XX) |
okini3939 | 11:cb132e066057 | 69 | // LPC1114 support by Stanly Chen |
stanly88 | 10:b748aab8404c | 70 | if (p_rx == P1_6) { |
okini3939 | 13:9841af9ac344 | 71 | _uart = (LPC_UART_TypeDef*)UART_0; |
stanly88 | 10:b748aab8404c | 72 | NVIC_SetPriority(UART_IRQn, 1); |
stanly88 | 10:b748aab8404c | 73 | } |
okini3939 | 5:72039cd4c874 | 74 | #endif |
okini3939 | 0:cbff6bf41542 | 75 | |
okini3939 | 5:72039cd4c874 | 76 | _dmx.baud(250000); |
okini3939 | 5:72039cd4c874 | 77 | _dmx.format(8, Serial::None, 2); |
okini3939 | 5:72039cd4c874 | 78 | _dmx.attach(this, &DMX::int_rx, Serial::RxIrq); |
okini3939 | 0:cbff6bf41542 | 79 | |
okini3939 | 4:dd0544c80096 | 80 | // timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BETWEEN); |
okini3939 | 0:cbff6bf41542 | 81 | } |
okini3939 | 0:cbff6bf41542 | 82 | |
okini3939 | 6:9e7b4eeac6ec | 83 | void DMX::put (int addr, int data) { |
okini3939 | 6:9e7b4eeac6ec | 84 | if (addr < 0 || addr >= DMX_SIZE) return; |
okini3939 | 6:9e7b4eeac6ec | 85 | data_tx[addr] = data; |
okini3939 | 0:cbff6bf41542 | 86 | } |
okini3939 | 0:cbff6bf41542 | 87 | |
okini3939 | 6:9e7b4eeac6ec | 88 | void DMX::put (unsigned char *buf, int addr, int len) { |
okini3939 | 6:9e7b4eeac6ec | 89 | if (addr < 0 || addr >= DMX_SIZE) return; |
okini3939 | 6:9e7b4eeac6ec | 90 | if (len > DMX_SIZE - addr) len = DMX_SIZE - addr; |
okini3939 | 6:9e7b4eeac6ec | 91 | memcpy(&data_tx[addr], buf, len); |
okini3939 | 6:9e7b4eeac6ec | 92 | } |
okini3939 | 6:9e7b4eeac6ec | 93 | |
okini3939 | 6:9e7b4eeac6ec | 94 | int DMX::get (int addr) { |
okini3939 | 6:9e7b4eeac6ec | 95 | if (addr < 0 || addr >= DMX_SIZE) return -1; |
okini3939 | 6:9e7b4eeac6ec | 96 | return data_rx[addr]; |
okini3939 | 6:9e7b4eeac6ec | 97 | } |
okini3939 | 6:9e7b4eeac6ec | 98 | |
okini3939 | 6:9e7b4eeac6ec | 99 | void DMX::get (unsigned char *buf, int addr, int len) { |
okini3939 | 6:9e7b4eeac6ec | 100 | if (addr < 0 || addr >= DMX_SIZE) return; |
okini3939 | 6:9e7b4eeac6ec | 101 | if (len > DMX_SIZE - addr) len = DMX_SIZE - addr; |
okini3939 | 6:9e7b4eeac6ec | 102 | memcpy(buf, &data_rx[addr], len); |
okini3939 | 0:cbff6bf41542 | 103 | } |
okini3939 | 0:cbff6bf41542 | 104 | |
okini3939 | 0:cbff6bf41542 | 105 | void DMX::int_timer () { |
okini3939 | 0:cbff6bf41542 | 106 | |
okini3939 | 0:cbff6bf41542 | 107 | switch (mode_tx) { |
okini3939 | 0:cbff6bf41542 | 108 | case DMX_MODE_BEGIN: |
okini3939 | 0:cbff6bf41542 | 109 | // Break Time |
okini3939 | 0:cbff6bf41542 | 110 | timeout01.detach(); |
okini3939 | 8:d4a45bba41d2 | 111 | _uart->LCR |= (1 << 6); |
okini3939 | 0:cbff6bf41542 | 112 | mode_tx = DMX_MODE_BREAK; |
okini3939 | 0:cbff6bf41542 | 113 | timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BREAK); |
okini3939 | 0:cbff6bf41542 | 114 | break; |
okini3939 | 0:cbff6bf41542 | 115 | |
okini3939 | 0:cbff6bf41542 | 116 | case DMX_MODE_BREAK: |
okini3939 | 0:cbff6bf41542 | 117 | // Mark After Break |
okini3939 | 0:cbff6bf41542 | 118 | timeout01.detach(); |
okini3939 | 8:d4a45bba41d2 | 119 | _uart->LCR &= ~(1 << 6); |
okini3939 | 0:cbff6bf41542 | 120 | mode_tx = DMX_MODE_MAB; |
okini3939 | 0:cbff6bf41542 | 121 | timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_MAB); |
okini3939 | 0:cbff6bf41542 | 122 | break; |
okini3939 | 0:cbff6bf41542 | 123 | |
okini3939 | 0:cbff6bf41542 | 124 | case DMX_MODE_MAB: |
okini3939 | 0:cbff6bf41542 | 125 | // Start code |
okini3939 | 0:cbff6bf41542 | 126 | timeout01.detach(); |
okini3939 | 0:cbff6bf41542 | 127 | addr_tx = 0; |
okini3939 | 0:cbff6bf41542 | 128 | mode_tx = DMX_MODE_DATA; |
okini3939 | 5:72039cd4c874 | 129 | _dmx.attach(this, &DMX::int_tx, Serial::TxIrq); |
okini3939 | 2:d7677060f8eb | 130 | #ifdef DMX_UART_DIRECT |
okini3939 | 9:e687f321c428 | 131 | while(!(_uart->LSR & (1<<5))); |
okini3939 | 9:e687f321c428 | 132 | _uart->THR = DMX_START_CODE; |
okini3939 | 2:d7677060f8eb | 133 | #else |
okini3939 | 9:e687f321c428 | 134 | _dmx.putc(DMX_START_CODE); |
okini3939 | 2:d7677060f8eb | 135 | #endif |
okini3939 | 0:cbff6bf41542 | 136 | break; |
okini3939 | 0:cbff6bf41542 | 137 | } |
okini3939 | 0:cbff6bf41542 | 138 | } |
okini3939 | 0:cbff6bf41542 | 139 | |
okini3939 | 0:cbff6bf41542 | 140 | void DMX::int_tx () { |
okini3939 | 0:cbff6bf41542 | 141 | // Data |
okini3939 | 0:cbff6bf41542 | 142 | if (mode_tx == DMX_MODE_DATA) { |
okini3939 | 0:cbff6bf41542 | 143 | if (addr_tx < DMX_SIZE) { |
okini3939 | 1:f0d988e15810 | 144 | #ifdef DMX_UART_DIRECT |
okini3939 | 8:d4a45bba41d2 | 145 | _uart->THR = (uint8_t)data_tx[addr_tx]; |
okini3939 | 0:cbff6bf41542 | 146 | #else |
okini3939 | 5:72039cd4c874 | 147 | _dmx.putc(data_tx[addr_tx]); |
okini3939 | 0:cbff6bf41542 | 148 | #endif |
okini3939 | 0:cbff6bf41542 | 149 | addr_tx ++; |
okini3939 | 0:cbff6bf41542 | 150 | } else { |
okini3939 | 5:72039cd4c874 | 151 | _dmx.attach(0, Serial::TxIrq); |
okini3939 | 0:cbff6bf41542 | 152 | mode_tx = DMX_MODE_BEGIN; |
okini3939 | 0:cbff6bf41542 | 153 | is_sent = 1; |
okini3939 | 0:cbff6bf41542 | 154 | timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BETWEEN); |
okini3939 | 0:cbff6bf41542 | 155 | } |
okini3939 | 0:cbff6bf41542 | 156 | } |
okini3939 | 0:cbff6bf41542 | 157 | } |
okini3939 | 0:cbff6bf41542 | 158 | |
okini3939 | 0:cbff6bf41542 | 159 | void DMX::int_rx () { |
okini3939 | 0:cbff6bf41542 | 160 | int flg, dat; |
okini3939 | 0:cbff6bf41542 | 161 | |
okini3939 | 8:d4a45bba41d2 | 162 | flg = _uart->LSR; |
okini3939 | 1:f0d988e15810 | 163 | #ifdef DMX_UART_DIRECT |
okini3939 | 8:d4a45bba41d2 | 164 | dat = _uart->RBR; |
okini3939 | 1:f0d988e15810 | 165 | #else |
okini3939 | 5:72039cd4c874 | 166 | dat = _dmx.getc(); |
okini3939 | 1:f0d988e15810 | 167 | #endif |
okini3939 | 0:cbff6bf41542 | 168 | |
okini3939 | 0:cbff6bf41542 | 169 | if (flg & ((1 << 7)|(1 << 3)|(1 << 4))) { |
okini3939 | 0:cbff6bf41542 | 170 | // Break Time |
okini3939 | 15:4ea4a31c7609 | 171 | if (addr_rx >= 24 && mode_rx == DMX_MODE_DATA) { |
okini3939 | 0:cbff6bf41542 | 172 | is_recived = 1; |
okini3939 | 0:cbff6bf41542 | 173 | } |
okini3939 | 0:cbff6bf41542 | 174 | mode_rx = DMX_MODE_BREAK; |
okini3939 | 0:cbff6bf41542 | 175 | return; |
okini3939 | 0:cbff6bf41542 | 176 | } |
okini3939 | 0:cbff6bf41542 | 177 | |
okini3939 | 0:cbff6bf41542 | 178 | if (mode_rx == DMX_MODE_BREAK) { |
okini3939 | 0:cbff6bf41542 | 179 | |
okini3939 | 0:cbff6bf41542 | 180 | // Start Code |
okini3939 | 9:e687f321c428 | 181 | if (dat == DMX_START_CODE) { |
okini3939 | 0:cbff6bf41542 | 182 | addr_rx = 0; |
okini3939 | 0:cbff6bf41542 | 183 | mode_rx = DMX_MODE_DATA; |
okini3939 | 0:cbff6bf41542 | 184 | } else { |
okini3939 | 0:cbff6bf41542 | 185 | mode_rx = DMX_MODE_ERROR; |
okini3939 | 0:cbff6bf41542 | 186 | } |
okini3939 | 0:cbff6bf41542 | 187 | |
okini3939 | 0:cbff6bf41542 | 188 | } else |
okini3939 | 0:cbff6bf41542 | 189 | if (mode_rx == DMX_MODE_DATA) { |
okini3939 | 0:cbff6bf41542 | 190 | |
okini3939 | 0:cbff6bf41542 | 191 | // Data |
okini3939 | 0:cbff6bf41542 | 192 | data_rx[addr_rx] = dat; |
okini3939 | 0:cbff6bf41542 | 193 | addr_rx ++; |
okini3939 | 0:cbff6bf41542 | 194 | |
okini3939 | 0:cbff6bf41542 | 195 | if (addr_rx >= DMX_SIZE) { |
okini3939 | 0:cbff6bf41542 | 196 | is_recived = 1; |
okini3939 | 0:cbff6bf41542 | 197 | mode_rx = DMX_MODE_BEGIN; |
okini3939 | 0:cbff6bf41542 | 198 | } |
okini3939 | 0:cbff6bf41542 | 199 | } |
okini3939 | 0:cbff6bf41542 | 200 | } |
okini3939 | 0:cbff6bf41542 | 201 | |
okini3939 | 4:dd0544c80096 | 202 | void DMX::start () { |
okini3939 | 4:dd0544c80096 | 203 | if (mode_tx == DMX_MODE_STOP) { |
okini3939 | 4:dd0544c80096 | 204 | mode_tx = DMX_MODE_BEGIN; |
okini3939 | 4:dd0544c80096 | 205 | is_sent = 0; |
okini3939 | 4:dd0544c80096 | 206 | timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BETWEEN); |
okini3939 | 4:dd0544c80096 | 207 | } |
okini3939 | 4:dd0544c80096 | 208 | } |
okini3939 | 4:dd0544c80096 | 209 | |
okini3939 | 4:dd0544c80096 | 210 | void DMX::stop () { |
okini3939 | 5:72039cd4c874 | 211 | _dmx.attach(0, Serial::TxIrq); |
okini3939 | 3:2eb66b4d99bd | 212 | timeout01.detach(); |
okini3939 | 4:dd0544c80096 | 213 | mode_tx = DMX_MODE_STOP; |
okini3939 | 3:2eb66b4d99bd | 214 | } |
okini3939 | 12:1f176eee2d28 | 215 | |
okini3939 | 12:1f176eee2d28 | 216 | void DMX::clear () { |
okini3939 | 12:1f176eee2d28 | 217 | int i; |
okini3939 | 12:1f176eee2d28 | 218 | |
okini3939 | 12:1f176eee2d28 | 219 | for (i = 0; i < DMX_SIZE; i ++) { |
okini3939 | 12:1f176eee2d28 | 220 | data_tx[i] = 0; |
okini3939 | 12:1f176eee2d28 | 221 | data_rx[i] = 0; |
okini3939 | 12:1f176eee2d28 | 222 | } |
okini3939 | 12:1f176eee2d28 | 223 | } |