summaryrefslogtreecommitdiff
path: root/src/arch/arduino-nano/driver/dmx.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/arch/arduino-nano/driver/dmx.cc')
-rw-r--r--src/arch/arduino-nano/driver/dmx.cc52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/arch/arduino-nano/driver/dmx.cc b/src/arch/arduino-nano/driver/dmx.cc
new file mode 100644
index 0000000..20a7332
--- /dev/null
+++ b/src/arch/arduino-nano/driver/dmx.cc
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2022 Daniel Friesel
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+#include <avr/io.h>
+#include "arch.h"
+#include "driver/dmx.h"
+#include "driver/gpio.h"
+
+#define BAUD 250000UL
+#include <util/setbaud.h>
+
+void DMX::setup()
+{
+ UBRR0H = UBRRH_VALUE;
+ UBRR0L = UBRRL_VALUE;
+
+#if USE_2X
+ UCSR0A |= _BV(U2X0);
+#else
+ UCSR0A &= ~_BV(U2X0);
+#endif
+
+ UCSR0B |= _BV(TXEN0);
+ UCSR0C = _BV(USBS0) | _BV(UCSZ01) | _BV(UCSZ00); // 8 bits
+}
+
+void DMX::write()
+{
+ // Disable UART for reset and mark signals
+ UCSR0B &= ~_BV(TXEN0);
+ gpio.output(GPIO::pd1, 0);
+ arch.delay_us(88); // break
+ gpio.output(GPIO::pd1, 1);
+ arch.delay_us(8);
+ UCSR0B |= _BV(TXEN0); // causes line to go high
+ for (uint8_t i = 0; i < 16; i++) {
+ while (!(UCSR0A & _BV(UDRE0)));
+ UDR0 = frames[i];
+ }
+ for (uint8_t i = 0; i < 241; i++) {
+ while (!(UCSR0A & _BV(UDRE0)));
+ UDR0 = 0;
+ }
+ for (uint8_t i = 0; i < 255; i++) {
+ while (!(UCSR0A & _BV(UDRE0)));
+ UDR0 = 0;
+ }
+}
+
+DMX dmx;