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