DMX Library for STM devices

Dependents:   DISCO-F746NG_DMX-Test

Fork of DMX by Suga koubou

Committer:
Gottfried
Date:
Sat Dec 12 13:16:07 2015 +0000
Revision:
17:b87addd12d20
Parent:
16:4eeb749806e5
Child:
18:e5e5eca21b4a
working for STM32 port of DMX library; Library does'nt work by now

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
Gottfried 17:b87addd12d20 14 // pragma does'nt work?
Gottfried 17:b87addd12d20 15 #pragma GCC diagnostic ignored "-Wreorder"
okini3939 0:cbff6bf41542 16
Gottfried 17:b87addd12d20 17 DMX::DMX (PinName p_tx, PinName p_rx, unsigned int size) :
Gottfried 17:b87addd12d20 18 _dmx(p_tx, p_rx),
Gottfried 17:b87addd12d20 19 myDMXSize(size),
Gottfried 17:b87addd12d20 20 is_recived(0),
Gottfried 17:b87addd12d20 21 is_sent(0),
Gottfried 17:b87addd12d20 22 mode_tx(DMX_MODE_STOP),
Gottfried 17:b87addd12d20 23 mode_rx(DMX_MODE_BEGIN)
Gottfried 17:b87addd12d20 24 {
Gottfried 17:b87addd12d20 25 data_tx = new char[myDMXSize];
Gottfried 17:b87addd12d20 26 data_rx = new char[myDMXSize];
Gottfried 17:b87addd12d20 27
Gottfried 17:b87addd12d20 28 clear();
okini3939 0:cbff6bf41542 29
Gottfried 17:b87addd12d20 30 _uart = new DUMMY_UART_TypeDef;
Gottfried 17:b87addd12d20 31 // NVIC_SetPriority(UART_IRQn, 1);
okini3939 0:cbff6bf41542 32
Gottfried 17:b87addd12d20 33 _dmx.baud(250000);
Gottfried 17:b87addd12d20 34 _dmx.format(8, Serial::None, 2);
Gottfried 17:b87addd12d20 35 _dmx.attach(this, &DMX::int_rx, Serial::RxIrq);
Gottfried 17:b87addd12d20 36 }
Gottfried 17:b87addd12d20 37
Gottfried 17:b87addd12d20 38 DMX::~DMX()
Gottfried 17:b87addd12d20 39 {
Gottfried 17:b87addd12d20 40 delete _uart;
Gottfried 17:b87addd12d20 41 delete[] data_tx;
Gottfried 17:b87addd12d20 42 delete[] data_rx;
okini3939 0:cbff6bf41542 43 }
okini3939 0:cbff6bf41542 44
Gottfried 17:b87addd12d20 45 void DMX::put(int addr, int data)
Gottfried 17:b87addd12d20 46 {
Gottfried 17:b87addd12d20 47 if(addr < 0 || addr >= myDMXSize)
Gottfried 17:b87addd12d20 48 return;
Gottfried 17:b87addd12d20 49 data_tx[addr] = data;
okini3939 0:cbff6bf41542 50 }
okini3939 0:cbff6bf41542 51
Gottfried 17:b87addd12d20 52 void DMX::put(unsigned char *buf, int addr, int len)
Gottfried 17:b87addd12d20 53 {
Gottfried 17:b87addd12d20 54 if(!len)
Gottfried 17:b87addd12d20 55 len = myDMXSize;
Gottfried 17:b87addd12d20 56
Gottfried 17:b87addd12d20 57 if(addr < 0 || addr >= myDMXSize)
Gottfried 17:b87addd12d20 58 return;
Gottfried 17:b87addd12d20 59 if(len > myDMXSize - addr)
Gottfried 17:b87addd12d20 60 len = myDMXSize - addr;
Gottfried 17:b87addd12d20 61 memcpy(&data_tx[addr], buf, len);
okini3939 6:9e7b4eeac6ec 62 }
okini3939 6:9e7b4eeac6ec 63
Gottfried 17:b87addd12d20 64 int DMX::get(int addr)
Gottfried 17:b87addd12d20 65 {
Gottfried 17:b87addd12d20 66 if(addr < 0 || addr >= myDMXSize)
Gottfried 17:b87addd12d20 67 return -1;
Gottfried 17:b87addd12d20 68
Gottfried 17:b87addd12d20 69 return data_rx[addr];
okini3939 6:9e7b4eeac6ec 70 }
okini3939 6:9e7b4eeac6ec 71
Gottfried 17:b87addd12d20 72 void DMX::get(unsigned char *buf, int addr, int len)
Gottfried 17:b87addd12d20 73 {
Gottfried 17:b87addd12d20 74 if(!len)
Gottfried 17:b87addd12d20 75 len = myDMXSize;
Gottfried 17:b87addd12d20 76
Gottfried 17:b87addd12d20 77 if(addr < 0 || addr >= myDMXSize)
Gottfried 17:b87addd12d20 78 return;
Gottfried 17:b87addd12d20 79 if(len > myDMXSize - addr)
Gottfried 17:b87addd12d20 80 len = myDMXSize - addr;
Gottfried 17:b87addd12d20 81 memcpy(buf, &data_rx[addr], len);
okini3939 0:cbff6bf41542 82 }
okini3939 0:cbff6bf41542 83
Gottfried 17:b87addd12d20 84 void DMX::int_timer()
Gottfried 17:b87addd12d20 85 {
Gottfried 17:b87addd12d20 86 switch(mode_tx)
Gottfried 17:b87addd12d20 87 {
okini3939 0:cbff6bf41542 88 case DMX_MODE_BEGIN:
okini3939 0:cbff6bf41542 89 // Break Time
okini3939 0:cbff6bf41542 90 timeout01.detach();
okini3939 8:d4a45bba41d2 91 _uart->LCR |= (1 << 6);
okini3939 0:cbff6bf41542 92 mode_tx = DMX_MODE_BREAK;
okini3939 0:cbff6bf41542 93 timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BREAK);
okini3939 0:cbff6bf41542 94 break;
okini3939 0:cbff6bf41542 95
okini3939 0:cbff6bf41542 96 case DMX_MODE_BREAK:
okini3939 0:cbff6bf41542 97 // Mark After Break
okini3939 0:cbff6bf41542 98 timeout01.detach();
okini3939 8:d4a45bba41d2 99 _uart->LCR &= ~(1 << 6);
okini3939 0:cbff6bf41542 100 mode_tx = DMX_MODE_MAB;
okini3939 0:cbff6bf41542 101 timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_MAB);
okini3939 0:cbff6bf41542 102 break;
okini3939 0:cbff6bf41542 103
okini3939 0:cbff6bf41542 104 case DMX_MODE_MAB:
okini3939 0:cbff6bf41542 105 // Start code
okini3939 0:cbff6bf41542 106 timeout01.detach();
okini3939 0:cbff6bf41542 107 addr_tx = 0;
okini3939 0:cbff6bf41542 108 mode_tx = DMX_MODE_DATA;
okini3939 5:72039cd4c874 109 _dmx.attach(this, &DMX::int_tx, Serial::TxIrq);
okini3939 2:d7677060f8eb 110 #ifdef DMX_UART_DIRECT
okini3939 9:e687f321c428 111 while(!(_uart->LSR & (1<<5)));
okini3939 9:e687f321c428 112 _uart->THR = DMX_START_CODE;
okini3939 2:d7677060f8eb 113 #else
okini3939 9:e687f321c428 114 _dmx.putc(DMX_START_CODE);
okini3939 2:d7677060f8eb 115 #endif
okini3939 0:cbff6bf41542 116 break;
Gottfried 17:b87addd12d20 117 }
Gottfried 17:b87addd12d20 118 }
Gottfried 17:b87addd12d20 119
Gottfried 17:b87addd12d20 120 void DMX::int_tx()
Gottfried 17:b87addd12d20 121 {
Gottfried 17:b87addd12d20 122 // Data
Gottfried 17:b87addd12d20 123 if(mode_tx == DMX_MODE_DATA)
Gottfried 17:b87addd12d20 124 {
Gottfried 17:b87addd12d20 125 if(addr_tx < myDMXSize)
Gottfried 17:b87addd12d20 126 {
Gottfried 17:b87addd12d20 127 #ifdef DMX_UART_DIRECT
Gottfried 17:b87addd12d20 128 _uart->THR = (uint8_t)data_tx[addr_tx];
Gottfried 17:b87addd12d20 129 #else
Gottfried 17:b87addd12d20 130 _dmx.putc(data_tx[addr_tx]);
Gottfried 17:b87addd12d20 131 #endif
Gottfried 17:b87addd12d20 132 addr_tx ++;
okini3939 0:cbff6bf41542 133 }
Gottfried 17:b87addd12d20 134 else
Gottfried 17:b87addd12d20 135 {
Gottfried 17:b87addd12d20 136 _dmx.attach(0, Serial::TxIrq);
Gottfried 17:b87addd12d20 137 mode_tx = DMX_MODE_BEGIN;
Gottfried 17:b87addd12d20 138 is_sent = 1;
Gottfried 17:b87addd12d20 139 timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BETWEEN);
Gottfried 17:b87addd12d20 140 }
Gottfried 17:b87addd12d20 141 }
okini3939 0:cbff6bf41542 142 }
okini3939 0:cbff6bf41542 143
Gottfried 17:b87addd12d20 144 void DMX::int_rx()
Gottfried 17:b87addd12d20 145 {
Gottfried 17:b87addd12d20 146 int flg, dat;
Gottfried 17:b87addd12d20 147
Gottfried 17:b87addd12d20 148 flg = _uart->LSR;
okini3939 1:f0d988e15810 149 #ifdef DMX_UART_DIRECT
Gottfried 17:b87addd12d20 150 dat = _uart->RBR;
okini3939 0:cbff6bf41542 151 #else
Gottfried 17:b87addd12d20 152 dat = _dmx.getc();
okini3939 1:f0d988e15810 153 #endif
okini3939 0:cbff6bf41542 154
Gottfried 17:b87addd12d20 155 if (flg & ((1 << 7)|(1 << 3)|(1 << 4)))
Gottfried 17:b87addd12d20 156 {
Gottfried 17:b87addd12d20 157 // Break Time
Gottfried 17:b87addd12d20 158 if(addr_rx >= 24 && mode_rx == DMX_MODE_DATA)
Gottfried 17:b87addd12d20 159 {
Gottfried 17:b87addd12d20 160 is_recived = 1;
Gottfried 17:b87addd12d20 161 }
Gottfried 17:b87addd12d20 162 mode_rx = DMX_MODE_BREAK;
Gottfried 17:b87addd12d20 163 return;
Gottfried 17:b87addd12d20 164 }
Gottfried 17:b87addd12d20 165
Gottfried 17:b87addd12d20 166 if(mode_rx == DMX_MODE_BREAK)
Gottfried 17:b87addd12d20 167 {
Gottfried 17:b87addd12d20 168 // Start Code
Gottfried 17:b87addd12d20 169 if (dat == DMX_START_CODE)
Gottfried 17:b87addd12d20 170 {
Gottfried 17:b87addd12d20 171 addr_rx = 0;
Gottfried 17:b87addd12d20 172 mode_rx = DMX_MODE_DATA;
Gottfried 17:b87addd12d20 173 }
Gottfried 17:b87addd12d20 174 else
Gottfried 17:b87addd12d20 175 {
Gottfried 17:b87addd12d20 176 mode_rx = DMX_MODE_ERROR;
okini3939 0:cbff6bf41542 177 }
okini3939 0:cbff6bf41542 178
Gottfried 17:b87addd12d20 179 }
Gottfried 17:b87addd12d20 180 else if(mode_rx == DMX_MODE_DATA)
Gottfried 17:b87addd12d20 181 {
Gottfried 17:b87addd12d20 182 // Data
Gottfried 17:b87addd12d20 183 data_rx[addr_rx] = dat;
Gottfried 17:b87addd12d20 184 addr_rx ++;
okini3939 0:cbff6bf41542 185
Gottfried 17:b87addd12d20 186 if (addr_rx >= myDMXSize)
Gottfried 17:b87addd12d20 187 {
Gottfried 17:b87addd12d20 188 is_recived = 1;
Gottfried 17:b87addd12d20 189 mode_rx = DMX_MODE_BEGIN;
okini3939 0:cbff6bf41542 190 }
Gottfried 17:b87addd12d20 191 }
okini3939 0:cbff6bf41542 192 }
okini3939 0:cbff6bf41542 193
Gottfried 17:b87addd12d20 194 void DMX::start()
Gottfried 17:b87addd12d20 195 {
Gottfried 17:b87addd12d20 196 if (mode_tx == DMX_MODE_STOP)
Gottfried 17:b87addd12d20 197 {
Gottfried 17:b87addd12d20 198 mode_tx = DMX_MODE_BEGIN;
Gottfried 17:b87addd12d20 199 is_sent = 0;
Gottfried 17:b87addd12d20 200 timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BETWEEN);
Gottfried 17:b87addd12d20 201 }
okini3939 4:dd0544c80096 202 }
okini3939 4:dd0544c80096 203
Gottfried 17:b87addd12d20 204 void DMX::stop()
Gottfried 17:b87addd12d20 205 {
Gottfried 17:b87addd12d20 206 _dmx.attach(0, Serial::TxIrq);
Gottfried 17:b87addd12d20 207 timeout01.detach();
Gottfried 17:b87addd12d20 208 mode_tx = DMX_MODE_STOP;
okini3939 3:2eb66b4d99bd 209 }
okini3939 12:1f176eee2d28 210
Gottfried 17:b87addd12d20 211 void DMX::clear()
Gottfried 17:b87addd12d20 212 {
Gottfried 17:b87addd12d20 213 memset(data_tx, 0, myDMXSize);
Gottfried 17:b87addd12d20 214 memset(data_rx, 0, myDMXSize);
okini3939 12:1f176eee2d28 215 }