blob: 96409b072fadc8676b2feb9f28388cca6eae6fd4 [file] [log] [blame]
Brian Silverman4aa83042018-01-05 12:47:31 -08001#ifndef MOTORS_USB_INTERRUPT_OUT_H_
2#define MOTORS_USB_INTERRUPT_OUT_H_
3
Brian Silverman4aa83042018-01-05 12:47:31 -08004#include <array>
5#include <string>
6
Philipp Schrader790cb542023-07-05 21:06:52 -07007#include "motors/usb/usb.h"
8#include "motors/util.h"
9
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080010namespace frc971::teensy {
Brian Silverman4aa83042018-01-05 12:47:31 -080011
12// A simple function that just has an interrupt out endpoint and exposes the
13// data received.
14class 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 Pleinesd99b1ee2024-02-02 20:56:44 -080060} // namespace frc971::teensy
Brian Silverman4aa83042018-01-05 12:47:31 -080061
62#endif // MOTORS_USB_INTERRUPT_OUT_H_