DMX Library for STM devices
Dependents: DISCO-F746NG_DMX-Test
Fork of DMX by
DMX.cpp@8:d4a45bba41d2, 2013-03-11 (annotated)
- Committer:
- okini3939
- Date:
- Mon Mar 11 04:02:26 2013 +0000
- Revision:
- 8:d4a45bba41d2
- Parent:
- 7:16d6874076dd
- Child:
- 9:e687f321c428
fix uart direct
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 | 0:cbff6bf41542 | 28 | if (p_rx == p10) { |
okini3939 | 8:d4a45bba41d2 | 29 | _uart = LPC_UART3; |
okini3939 | 0:cbff6bf41542 | 30 | NVIC_SetPriority(UART3_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 | 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 | } |
okini3939 | 5:72039cd4c874 | 45 | #endif |
okini3939 | 0:cbff6bf41542 | 46 | |
okini3939 | 5:72039cd4c874 | 47 | _dmx.baud(250000); |
okini3939 | 5:72039cd4c874 | 48 | _dmx.format(8, Serial::None, 2); |
okini3939 | 5:72039cd4c874 | 49 | _dmx.attach(this, &DMX::int_rx, Serial::RxIrq); |
okini3939 | 0:cbff6bf41542 | 50 | |
okini3939 | 4:dd0544c80096 | 51 | // timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BETWEEN); |
okini3939 | 0:cbff6bf41542 | 52 | } |
okini3939 | 0:cbff6bf41542 | 53 | |
okini3939 | 6:9e7b4eeac6ec | 54 | void DMX::put (int addr, int data) { |
okini3939 | 6:9e7b4eeac6ec | 55 | if (addr < 0 || addr >= DMX_SIZE) return; |
okini3939 | 6:9e7b4eeac6ec | 56 | data_tx[addr] = data; |
okini3939 | 0:cbff6bf41542 | 57 | } |
okini3939 | 0:cbff6bf41542 | 58 | |
okini3939 | 6:9e7b4eeac6ec | 59 | void DMX::put (unsigned char *buf, int addr, int len) { |
okini3939 | 6:9e7b4eeac6ec | 60 | if (addr < 0 || addr >= DMX_SIZE) return; |
okini3939 | 6:9e7b4eeac6ec | 61 | if (len > DMX_SIZE - addr) len = DMX_SIZE - addr; |
okini3939 | 6:9e7b4eeac6ec | 62 | memcpy(&data_tx[addr], buf, len); |
okini3939 | 6:9e7b4eeac6ec | 63 | } |
okini3939 | 6:9e7b4eeac6ec | 64 | |
okini3939 | 6:9e7b4eeac6ec | 65 | int DMX::get (int addr) { |
okini3939 | 6:9e7b4eeac6ec | 66 | if (addr < 0 || addr >= DMX_SIZE) return -1; |
okini3939 | 6:9e7b4eeac6ec | 67 | return data_rx[addr]; |
okini3939 | 6:9e7b4eeac6ec | 68 | } |
okini3939 | 6:9e7b4eeac6ec | 69 | |
okini3939 | 6:9e7b4eeac6ec | 70 | void DMX::get (unsigned char *buf, int addr, int len) { |
okini3939 | 6:9e7b4eeac6ec | 71 | if (addr < 0 || addr >= DMX_SIZE) return; |
okini3939 | 6:9e7b4eeac6ec | 72 | if (len > DMX_SIZE - addr) len = DMX_SIZE - addr; |
okini3939 | 6:9e7b4eeac6ec | 73 | memcpy(buf, &data_rx[addr], len); |
okini3939 | 0:cbff6bf41542 | 74 | } |
okini3939 | 0:cbff6bf41542 | 75 | |
okini3939 | 0:cbff6bf41542 | 76 | void DMX::int_timer () { |
okini3939 | 0:cbff6bf41542 | 77 | |
okini3939 | 0:cbff6bf41542 | 78 | switch (mode_tx) { |
okini3939 | 0:cbff6bf41542 | 79 | case DMX_MODE_BEGIN: |
okini3939 | 0:cbff6bf41542 | 80 | // Break Time |
okini3939 | 0:cbff6bf41542 | 81 | timeout01.detach(); |
okini3939 | 8:d4a45bba41d2 | 82 | _uart->LCR |= (1 << 6); |
okini3939 | 0:cbff6bf41542 | 83 | mode_tx = DMX_MODE_BREAK; |
okini3939 | 0:cbff6bf41542 | 84 | timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BREAK); |
okini3939 | 0:cbff6bf41542 | 85 | break; |
okini3939 | 0:cbff6bf41542 | 86 | |
okini3939 | 0:cbff6bf41542 | 87 | case DMX_MODE_BREAK: |
okini3939 | 0:cbff6bf41542 | 88 | // Mark After Break |
okini3939 | 0:cbff6bf41542 | 89 | timeout01.detach(); |
okini3939 | 8:d4a45bba41d2 | 90 | _uart->LCR &= ~(1 << 6); |
okini3939 | 0:cbff6bf41542 | 91 | mode_tx = DMX_MODE_MAB; |
okini3939 | 0:cbff6bf41542 | 92 | timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_MAB); |
okini3939 | 0:cbff6bf41542 | 93 | break; |
okini3939 | 0:cbff6bf41542 | 94 | |
okini3939 | 0:cbff6bf41542 | 95 | case DMX_MODE_MAB: |
okini3939 | 0:cbff6bf41542 | 96 | // Start code |
okini3939 | 0:cbff6bf41542 | 97 | timeout01.detach(); |
okini3939 | 0:cbff6bf41542 | 98 | addr_tx = 0; |
okini3939 | 0:cbff6bf41542 | 99 | mode_tx = DMX_MODE_DATA; |
okini3939 | 5:72039cd4c874 | 100 | _dmx.attach(this, &DMX::int_tx, Serial::TxIrq); |
okini3939 | 2:d7677060f8eb | 101 | #ifdef DMX_UART_DIRECT |
okini3939 | 8:d4a45bba41d2 | 102 | _uart->THR = 0; |
okini3939 | 2:d7677060f8eb | 103 | #else |
okini3939 | 5:72039cd4c874 | 104 | _dmx.putc(0); |
okini3939 | 2:d7677060f8eb | 105 | #endif |
okini3939 | 0:cbff6bf41542 | 106 | break; |
okini3939 | 0:cbff6bf41542 | 107 | } |
okini3939 | 0:cbff6bf41542 | 108 | } |
okini3939 | 0:cbff6bf41542 | 109 | |
okini3939 | 0:cbff6bf41542 | 110 | void DMX::int_tx () { |
okini3939 | 0:cbff6bf41542 | 111 | // Data |
okini3939 | 0:cbff6bf41542 | 112 | if (mode_tx == DMX_MODE_DATA) { |
okini3939 | 0:cbff6bf41542 | 113 | if (addr_tx < DMX_SIZE) { |
okini3939 | 1:f0d988e15810 | 114 | #ifdef DMX_UART_DIRECT |
okini3939 | 8:d4a45bba41d2 | 115 | _uart->THR = (uint8_t)data_tx[addr_tx]; |
okini3939 | 0:cbff6bf41542 | 116 | #else |
okini3939 | 5:72039cd4c874 | 117 | _dmx.putc(data_tx[addr_tx]); |
okini3939 | 0:cbff6bf41542 | 118 | #endif |
okini3939 | 0:cbff6bf41542 | 119 | addr_tx ++; |
okini3939 | 0:cbff6bf41542 | 120 | } else { |
okini3939 | 5:72039cd4c874 | 121 | _dmx.attach(0, Serial::TxIrq); |
okini3939 | 0:cbff6bf41542 | 122 | mode_tx = DMX_MODE_BEGIN; |
okini3939 | 0:cbff6bf41542 | 123 | is_sent = 1; |
okini3939 | 0:cbff6bf41542 | 124 | timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BETWEEN); |
okini3939 | 0:cbff6bf41542 | 125 | } |
okini3939 | 0:cbff6bf41542 | 126 | } |
okini3939 | 0:cbff6bf41542 | 127 | } |
okini3939 | 0:cbff6bf41542 | 128 | |
okini3939 | 0:cbff6bf41542 | 129 | void DMX::int_rx () { |
okini3939 | 0:cbff6bf41542 | 130 | int flg, dat; |
okini3939 | 0:cbff6bf41542 | 131 | |
okini3939 | 8:d4a45bba41d2 | 132 | flg = _uart->LSR; |
okini3939 | 1:f0d988e15810 | 133 | #ifdef DMX_UART_DIRECT |
okini3939 | 8:d4a45bba41d2 | 134 | dat = _uart->RBR; |
okini3939 | 1:f0d988e15810 | 135 | #else |
okini3939 | 5:72039cd4c874 | 136 | dat = _dmx.getc(); |
okini3939 | 1:f0d988e15810 | 137 | #endif |
okini3939 | 0:cbff6bf41542 | 138 | |
okini3939 | 0:cbff6bf41542 | 139 | if (flg & ((1 << 7)|(1 << 3)|(1 << 4))) { |
okini3939 | 0:cbff6bf41542 | 140 | // Break Time |
okini3939 | 8:d4a45bba41d2 | 141 | if (addr_rx >= 24) { |
okini3939 | 0:cbff6bf41542 | 142 | is_recived = 1; |
okini3939 | 0:cbff6bf41542 | 143 | } |
okini3939 | 0:cbff6bf41542 | 144 | mode_rx = DMX_MODE_BREAK; |
okini3939 | 0:cbff6bf41542 | 145 | return; |
okini3939 | 0:cbff6bf41542 | 146 | } |
okini3939 | 0:cbff6bf41542 | 147 | |
okini3939 | 0:cbff6bf41542 | 148 | if (mode_rx == DMX_MODE_BREAK) { |
okini3939 | 0:cbff6bf41542 | 149 | |
okini3939 | 0:cbff6bf41542 | 150 | // Start Code |
okini3939 | 0:cbff6bf41542 | 151 | if (dat == 0) { |
okini3939 | 0:cbff6bf41542 | 152 | addr_rx = 0; |
okini3939 | 0:cbff6bf41542 | 153 | mode_rx = DMX_MODE_DATA; |
okini3939 | 0:cbff6bf41542 | 154 | } else { |
okini3939 | 0:cbff6bf41542 | 155 | mode_rx = DMX_MODE_ERROR; |
okini3939 | 0:cbff6bf41542 | 156 | } |
okini3939 | 0:cbff6bf41542 | 157 | |
okini3939 | 0:cbff6bf41542 | 158 | } else |
okini3939 | 0:cbff6bf41542 | 159 | if (mode_rx == DMX_MODE_DATA) { |
okini3939 | 0:cbff6bf41542 | 160 | |
okini3939 | 0:cbff6bf41542 | 161 | // Data |
okini3939 | 0:cbff6bf41542 | 162 | data_rx[addr_rx] = dat; |
okini3939 | 0:cbff6bf41542 | 163 | addr_rx ++; |
okini3939 | 0:cbff6bf41542 | 164 | |
okini3939 | 0:cbff6bf41542 | 165 | if (addr_rx >= DMX_SIZE) { |
okini3939 | 0:cbff6bf41542 | 166 | is_recived = 1; |
okini3939 | 0:cbff6bf41542 | 167 | mode_rx = DMX_MODE_BEGIN; |
okini3939 | 0:cbff6bf41542 | 168 | } |
okini3939 | 0:cbff6bf41542 | 169 | } |
okini3939 | 0:cbff6bf41542 | 170 | } |
okini3939 | 0:cbff6bf41542 | 171 | |
okini3939 | 4:dd0544c80096 | 172 | void DMX::start () { |
okini3939 | 4:dd0544c80096 | 173 | if (mode_tx == DMX_MODE_STOP) { |
okini3939 | 4:dd0544c80096 | 174 | mode_tx = DMX_MODE_BEGIN; |
okini3939 | 4:dd0544c80096 | 175 | is_sent = 0; |
okini3939 | 4:dd0544c80096 | 176 | timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BETWEEN); |
okini3939 | 4:dd0544c80096 | 177 | } |
okini3939 | 4:dd0544c80096 | 178 | } |
okini3939 | 4:dd0544c80096 | 179 | |
okini3939 | 4:dd0544c80096 | 180 | void DMX::stop () { |
okini3939 | 5:72039cd4c874 | 181 | _dmx.attach(0, Serial::TxIrq); |
okini3939 | 3:2eb66b4d99bd | 182 | timeout01.detach(); |
okini3939 | 4:dd0544c80096 | 183 | mode_tx = DMX_MODE_STOP; |
okini3939 | 3:2eb66b4d99bd | 184 | } |