blob: 9284ecd0fb0b839c20d3cce4611a9094f4d99212 [file] [log] [blame]
Brian Silverman4787a6e2018-10-06 16:00:54 -07001#include "motors/print/usb.h"
2
3#include <atomic>
4
5#include "motors/core/kinetis.h"
6
7namespace frc971 {
8namespace motors {
9namespace {
10
11::std::atomic<teensy::AcmTty *> global_stdout{nullptr};
12
13} // namespace
14
15::std::unique_ptr<PrintingImplementation> CreatePrinting(
16 const PrintingParameters &parameters) {
17 if (parameters.dedicated_usb) {
18 return ::std::unique_ptr<PrintingImplementation>(
19 new DedicatedUsbPrinting());
20 }
21 if (parameters.stdout_tty != nullptr) {
22 return ::std::unique_ptr<PrintingImplementation>(
23 new UsbPrinting(parameters.stdout_tty, parameters.debug_tty));
24 }
25 return ::std::unique_ptr<PrintingImplementation>(new NopPrinting());
26}
27
28extern "C" int _write(const int /*file*/, char *const ptr, const int len) {
29 teensy::AcmTty *const tty = global_stdout.load(::std::memory_order_acquire);
30 if (tty != nullptr) {
31 return tty->Write(ptr, len);
32 }
33 return len;
34}
35
36UsbPrinting::UsbPrinting(teensy::AcmTty *stdout_tty, teensy::AcmTty *debug_tty)
37 : stdout_tty_(stdout_tty), debug_tty_(debug_tty) {}
38
39UsbPrinting::~UsbPrinting() {
40 global_stdout.store(nullptr, ::std::memory_order_release);
41}
42
43void UsbPrinting::Initialize() {
44 global_stdout.store(stdout_tty_, ::std::memory_order_release);
45}
46
47DedicatedUsbPrinting::DedicatedUsbPrinting()
48 : usb_device_{0, 0x16c0, 0x0490},
49 stdout_tty_{&usb_device_},
50 debug_tty_{&usb_device_} {
51 usb_device_.SetManufacturer("FRC 971 Spartan Robotics");
52 usb_device_.SetProduct("FET12v2");
53 NVIC_SET_SANE_PRIORITY(IRQ_USBOTG, 0x7);
54}
55
56DedicatedUsbPrinting::~DedicatedUsbPrinting() {
57 global_stdout.store(nullptr, ::std::memory_order_release);
58}
59
60void DedicatedUsbPrinting::Initialize() {
61 usb_device_.Initialize();
62 global_stdout.store(&stdout_tty_, ::std::memory_order_release);
63}
64
65} // namespace motors
66} // namespace frc971