Brian Silverman | 4aa8304 | 2018-01-05 12:47:31 -0800 | [diff] [blame] | 1 | #ifndef MOTORS_USB_INTERRUPT_OUT_H_ |
| 2 | #define MOTORS_USB_INTERRUPT_OUT_H_ |
| 3 | |
Brian Silverman | 4aa8304 | 2018-01-05 12:47:31 -0800 | [diff] [blame] | 4 | #include <array> |
| 5 | #include <string> |
| 6 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 7 | #include "motors/usb/usb.h" |
| 8 | #include "motors/util.h" |
| 9 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 10 | namespace frc971::teensy { |
Brian Silverman | 4aa8304 | 2018-01-05 12:47:31 -0800 | [diff] [blame] | 11 | |
| 12 | // A simple function that just has an interrupt out endpoint and exposes the |
| 13 | // data received. |
| 14 | class InterruptOut final : public UsbFunction { |
| 15 | public: |
| 16 | static constexpr size_t kSize = 64; |
| 17 | |
| 18 | InterruptOut(UsbDevice *device, const ::std::string &name) |
| 19 | : UsbFunction(device), name_(name) {} |
| 20 | ~InterruptOut() override = default; |
| 21 | |
| 22 | // Copies the next packet into buffer. |
| 23 | // buffer must have kSize of space available. |
| 24 | // Returns the size of the packet, or -1 if there isn't one. |
| 25 | int ReceiveData(char *buffer); |
| 26 | |
| 27 | private: |
| 28 | void Initialize() override; |
| 29 | |
| 30 | void HandleOutFinished(int endpoint, BdtEntry *bdt_entry) override; |
| 31 | void HandleConfigured(int endpoint) override; |
| 32 | void HandleReset() override { |
| 33 | device()->SetBdtEntry(endpoint_, Direction::kRx, EvenOdd::kEven, |
| 34 | {0, nullptr}); |
| 35 | device()->SetBdtEntry(endpoint_, Direction::kRx, EvenOdd::kOdd, |
| 36 | {0, nullptr}); |
| 37 | } |
| 38 | |
| 39 | ::std::string MicrosoftExtendedCompatibleId() override { |
| 40 | ::std::string result = "WINUSB"; |
| 41 | result.resize(16); |
| 42 | return result; |
| 43 | } |
| 44 | |
| 45 | ::std::array<::std::array<char, kSize>, 2> buffers_; |
| 46 | |
| 47 | int interface_; |
| 48 | int endpoint_; |
| 49 | |
| 50 | // These are BdtEntries which we're holding onto until the data is copied out. |
| 51 | // This also has the advantage of avoiding any more data being sent until |
| 52 | // we're ready. |
| 53 | // They are only manipulated with interrupts disabled. |
| 54 | BdtEntry *first_rx_held_ = nullptr, *second_rx_held_ = nullptr; |
| 55 | Data01 next_rx_toggle_; |
| 56 | |
| 57 | const ::std::string name_; |
| 58 | }; |
| 59 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 60 | } // namespace frc971::teensy |
Brian Silverman | 4aa8304 | 2018-01-05 12:47:31 -0800 | [diff] [blame] | 61 | |
| 62 | #endif // MOTORS_USB_INTERRUPT_OUT_H_ |