-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDMX.cpp
314 lines (266 loc) · 7.56 KB
/
DMX.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* DMX512, RDM send/recv library
* Copyright (c) 2021,2017 Hiroshi Suga
* Released under the MIT License: http://mbed.org/license/mit
*/
/** @file
* @brief DMX512 send/recv
*/
#include "DMX.h"
#include "SERCOM.h"
#include "SAMD51_TC.h"
DMX *DMX::_inst;
//void UART_IRQHandler ();
DMX::DMX () {
_inst = this;
_uart = new SERCOM(SERCOM_DEV);
// mode_tx = DMX_MODE_BEGIN;
mode_tx = DMX_MODE_STOP;
mode_rx = DMX_MODE_BEGIN;
is_received = 0;
is_sent = 0;
clear();
time_break = DMX_TIME_BREAK;
time_mab = DMX_TIME_MAB;
time_mbb = DMX_TIME_MBB;
}
void DMX::init () {
XMIT_INIT;
XMIT_RX;
XMIT2_INIT;
XMIT2_TX;
pinPeripheral(uc_pinRX, g_APinDescription[uc_pinRX].ulPinType);
pinPeripheral(uc_pinTX, g_APinDescription[uc_pinTX].ulPinType);
_uart->initUART(UART_INT_CLOCK, SAMPLE_RATE_x16, 250000);
_uart->initFrame(UART_CHAR_SIZE_8_BITS, LSB_FIRST, SERCOM_NO_PARITY, SERCOM_STOP_BITS_2);
_uart->initPads(uc_padTX, uc_padRX);
_uart->enableUART();
_dmx_attach(1, 0); // (this, &DMX::int_rx, Serial::RxIrq);
NVIC_SetPriority(SERCOM3_0_IRQn, 1);
NVIC_EnableIRQ(SERCOM3_0_IRQn);
// timeout01.attach_us(this, &DMX::int_timer, DMX_TIME_BETWEEN);
}
void DMX::put (int addr, int data) {
if (addr < 0 || addr >= DMX_SIZE) return;
data_tx[addr] = data;
}
void DMX::put (const unsigned char *buf, int addr, int len) {
if (addr < 0 || addr >= DMX_SIZE) return;
if (len > DMX_SIZE - addr) len = DMX_SIZE - addr;
memcpy(&data_tx[addr], buf, len);
}
int DMX::get (int addr) {
if (addr < 0 || addr >= DMX_SIZE) return -1;
return data_rx[addr];
}
void DMX::get (unsigned char *buf, int addr, int len) {
if (addr < 0 || addr >= DMX_SIZE) return;
if (len > DMX_SIZE - addr) len = DMX_SIZE - addr;
memcpy(buf, &data_rx[addr], len);
}
void DMX::int_timer () {
switch (mode_tx) {
case DMX_MODE_BEGIN:
// Break Time
timeout01_detach();
while(SERCOM_DEV->USART.SYNCBUSY.bit.ENABLE);
SERCOM_DEV->USART.CTRLA.bit.ENABLE = 0;
SERCOM_DEV->USART.CTRLA.reg |= SERCOM_USART_CTRLA_TXINV; // break on
SERCOM_DEV->USART.CTRLA.bit.ENABLE = 1;
while(SERCOM_DEV->USART.SYNCBUSY.bit.ENABLE); // is enabled
mode_tx = DMX_MODE_BREAK;
timeout01_attach_us(1, time_break); // (this, &DMX::int_timer, time_break);
break;
case DMX_MODE_BREAK:
// Mark After Break
timeout01_detach();
while(SERCOM_DEV->USART.SYNCBUSY.bit.ENABLE);
SERCOM_DEV->USART.CTRLA.bit.ENABLE = 0;
SERCOM_DEV->USART.CTRLA.reg &= ~SERCOM_USART_CTRLA_TXINV; // break off
SERCOM_DEV->USART.CTRLA.bit.ENABLE = 1;
while(SERCOM_DEV->USART.SYNCBUSY.bit.ENABLE); // is enabled
mode_tx = DMX_MODE_MAB;
timeout01_attach_us(1, time_mab); // (this, &DMX::int_timer, time_mab);
break;
case DMX_MODE_MAB:
// Start code
timeout01_detach();
addr_tx = 0;
mode_tx = DMX_MODE_DATA;
#ifdef DMX_UART_DIRECT
while (! SERCOM_DEV->USART.INTFLAG.bit.DRE); // is empty
SERCOM_DEV->USART.DATA.reg = DMX_START_CODE;
#else
_dmx_putc(DMX_START_CODE);
#endif
_dmx_attach(1, 1); // (this, &DMX::int_tx, Serial::TxIrq);
break;
}
}
void DMX::int_tx () {
// Data
if (mode_tx == DMX_MODE_DATA) {
if (addr_tx < DMX_SIZE) {
// DMX data
#ifdef DMX_UART_DIRECT
SERCOM_DEV->USART.DATA.reg = (uint8_t)data_tx[addr_tx];
#else
_dmx_putc(data_tx[addr_tx]);
#endif
addr_tx ++;
} else {
_dmx_attach(0, 1); // (0, Serial::TxIrq); // disable
mode_tx = DMX_MODE_BEGIN;
is_sent = 1;
timeout01_attach_us(1, time_mbb);
}
}
}
void DMX::int_rx () {
uint32_t flag, stat, dat;
flag = SERCOM_DEV->USART.INTFLAG.reg;
stat = SERCOM_DEV->USART.STATUS.reg;
dat = SERCOM_DEV->USART.DATA.reg;
if ((flag & (SERCOM_USART_INTFLAG_ERROR|SERCOM_USART_INTFLAG_RXBRK)) && (stat & (SERCOM_USART_STATUS_FERR))) {
// Break Time
SERCOM_DEV->USART.INTFLAG.reg = SERCOM_USART_INTFLAG_ERROR|SERCOM_USART_INTFLAG_RXBRK;
SERCOM_DEV->USART.STATUS.reg = SERCOM_USART_STATUS_FERR;
if (is_rdm_received) {
return;
} else
if (addr_rx >= 24 && mode_rx == DMX_MODE_DATA) {
is_received = 1;
}
if (dat == 0) {
mode_rx = DMX_MODE_BREAK;
} else {
mode_rx = DMX_MODE_ERROR;
}
return;
}
if (mode_rx == DMX_MODE_BREAK) {
// Start Code
if (dat == DMX_START_CODE) {
addr_rx = 0;
mode_rx = DMX_MODE_DATA;
} else {
mode_rx = DMX_MODE_ERROR;
}
} else
if (mode_rx == DMX_MODE_DATA) {
// Data
data_rx[addr_rx] = dat;
addr_rx ++;
if (addr_rx >= DMX_SIZE) {
is_received = 1;
mode_rx = DMX_MODE_BEGIN;
}
}
}
void DMX::start () {
if (mode_tx == DMX_MODE_STOP) {
mode_tx = DMX_MODE_BEGIN;
is_sent = 0;
timeout01_attach_us(1, time_mbb); // &DMX::int_timer, time_mbb);
}
}
void DMX::stop () {
_dmx_attach(0, 1); // (0, Serial::TxIrq);
timeout01_detach();
mode_tx = DMX_MODE_STOP;
}
void DMX::clear () {
memset(data_tx, 0, sizeof(data_tx));
memset(data_rx, 0, sizeof(data_rx));
}
int DMX::isReceived (){
int r = is_received;
is_received = 0;
return r;
}
int DMX::isSent () {
int r = is_sent;
is_sent = 0;
return r;
}
unsigned char *DMX::getRxBuffer () {
return data_rx;
}
unsigned char *DMX::getTxBuffer () {
return data_tx;
}
int DMX::setTimingParameters (int breaktime, int mab, int mbb) {
if (breaktime < 88 || breaktime > 1000000) return -1;
if (mab < 8 || mab > 1000000) return -1;
if (mbb < 0 || mbb > 1000000) return -1;
time_break = breaktime;
time_mab = mab;
time_mbb = mbb;
return 0;
}
void isrUart () {
int flg = SERCOM_DEV->USART.INTFLAG.reg;
if (flg & (SERCOM_USART_INTFLAG_ERROR | SERCOM_USART_INTFLAG_RXBRK | SERCOM_USART_INTFLAG_RXC)) {
if (DMX::_inst->callback[0] == 1) {
DMX::_inst->int_rx();
} else
if (DMX::_inst->callback[0] == 2) {
}
}
if (flg & SERCOM_USART_INTFLAG_DRE) {
if (DMX::_inst->callback[1] == 1) {
DMX::_inst->int_tx();
}
}
}
extern "C" {
void SERCOM3_0_Handler () {
isrUart();
}
void SERCOM3_1_Handler() {
isrUart();
}
void SERCOM3_2_Handler() {
isrUart();
}
void SERCOM3_3_Handler() {
isrUart();
}
} // extern
void DMX::_dmx_putc (int dat) {
SERCOM_DEV->USART.DATA.reg = dat;
}
int DMX::_dmx_getc () {
return SERCOM_DEV->USART.DATA.reg;
}
void DMX::_dmx_attach (int func, int type) {
callback[type] = func;
if (type == 0) {
if (func) {
SERCOM_DEV->USART.INTENSET.reg = (SERCOM_USART_INTENSET_RXC | SERCOM_USART_INTENSET_RXBRK | SERCOM_USART_INTENSET_ERROR); // interrupt RX
} else {
SERCOM_DEV->USART.INTENCLR.reg = (SERCOM_USART_INTENSET_RXC | SERCOM_USART_INTENSET_RXBRK | SERCOM_USART_INTENSET_ERROR);
}
} else {
if (func) {
SERCOM_DEV->USART.INTENSET.reg = (SERCOM_USART_INTENSET_DRE); // interrupt TX
} else {
SERCOM_DEV->USART.INTENCLR.reg = (SERCOM_USART_INTENSET_DRE);
}
}
}
void isrTimerDmx (void) {
TimerTC3.stop();
if (DMX::_inst->callback[2] == 1) {
DMX::_inst->int_timer();
}
}
void DMX::timeout01_attach_us (int func, int us) {
callback[2] = func;
TimerTC3.initialize(us);
TimerTC3.attachInterrupt(isrTimerDmx);
}
void DMX::timeout01_detach () {
callback[2] = 0;
TimerTC3.stop();
}