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