Brian Silverman | eda63f3 | 2017-10-08 18:57:33 -0400 | [diff] [blame] | 1 | #include "motors/usb/cdc.h" |
| 2 | |
| 3 | #include <string.h> |
Brian Silverman | 69f96c2 | 2017-11-01 02:54:02 -0400 | [diff] [blame] | 4 | #include <stdint.h> |
| 5 | |
| 6 | #include "motors/core/time.h" |
Brian Silverman | eda63f3 | 2017-10-08 18:57:33 -0400 | [diff] [blame] | 7 | |
| 8 | #define CHECK(c) \ |
| 9 | do { \ |
| 10 | if (!(c)) { \ |
| 11 | while (true) { \ |
| 12 | for (int i = 0; i < 10000000; ++i) { \ |
| 13 | GPIOC_PSOR = 1 << 5; \ |
| 14 | } \ |
| 15 | for (int i = 0; i < 10000000; ++i) { \ |
| 16 | GPIOC_PCOR = 1 << 5; \ |
| 17 | } \ |
| 18 | } \ |
| 19 | } \ |
| 20 | } while (false) |
| 21 | |
| 22 | namespace frc971 { |
| 23 | namespace teensy { |
| 24 | namespace { |
| 25 | |
| 26 | // Aka the Communications Device Class code, the Communications Class code, |
| 27 | // and the Communications Interface Class code. |
| 28 | constexpr uint8_t communications_class() { return 0x02; } |
| 29 | constexpr uint8_t data_interface_class() { return 0x0A; } |
| 30 | |
| 31 | namespace cdc_descriptor_subtype { |
| 32 | constexpr uint8_t header() { return 0x00; } |
| 33 | constexpr uint8_t header_length() { return 5; } |
| 34 | constexpr uint8_t call_management() { return 0x01; } |
| 35 | constexpr uint8_t call_management_length() { return 5; } |
| 36 | constexpr uint8_t abstract_control_management() { return 0x02; } |
| 37 | constexpr uint8_t abstract_control_management_length() { return 4; } |
| 38 | constexpr uint8_t direct_line_management() { return 0x03; } |
| 39 | constexpr uint8_t telephone_ringer() { return 0x04; } |
| 40 | constexpr uint8_t telephone_call_etc() { return 0x05; } |
| 41 | // Can't just call this "union" because that's a keyword... |
| 42 | constexpr uint8_t union_function() { return 0x06; } |
| 43 | constexpr uint8_t union_length(int number_subordinates) { |
| 44 | return 4 + number_subordinates; |
| 45 | } |
| 46 | constexpr uint8_t country_selection() { return 0x07; } |
| 47 | constexpr uint8_t telephone_operational_modes() { return 0x08; } |
| 48 | constexpr uint8_t usb_terminal() { return 0x09; } |
| 49 | constexpr uint8_t network_channel() { return 0x0A; } |
| 50 | constexpr uint8_t protocol_unit() { return 0x0B; } |
| 51 | constexpr uint8_t extension_unit() { return 0x0C; } |
| 52 | constexpr uint8_t multichannel_management() { return 0x0D; } |
| 53 | constexpr uint8_t capi_control() { return 0x0E; } |
| 54 | constexpr uint8_t ethernet_networking() { return 0x0F; } |
| 55 | constexpr uint8_t atm_networking() { return 0x10; } |
| 56 | constexpr uint8_t wireless_handset_control() { return 0x11; } |
| 57 | constexpr uint8_t mobile_direct_line() { return 0x12; } |
| 58 | constexpr uint8_t mdlm_detail() { return 0x13; } |
| 59 | constexpr uint8_t device_management() { return 0x14; } |
| 60 | constexpr uint8_t obex() { return 0x15; } |
| 61 | constexpr uint8_t command_set() { return 0x16; } |
| 62 | constexpr uint8_t command_set_detail() { return 0x17; } |
| 63 | constexpr uint8_t telephone_control() { return 0x18; } |
| 64 | constexpr uint8_t obex_service() { return 0x19; } |
| 65 | constexpr uint8_t ncm() { return 0x1A; } |
| 66 | } // namespace cdc_descriptor_subtype |
| 67 | |
| 68 | namespace communications_subclass { |
| 69 | constexpr uint8_t direct_line_control_model() { return 0x01; } |
| 70 | constexpr uint8_t abstract_control_model() { return 0x02; } |
| 71 | constexpr uint8_t telephone_control_model() { return 0x03; } |
| 72 | constexpr uint8_t multichannel_control_model() { return 0x04; } |
| 73 | constexpr uint8_t capi_control_model() { return 0x05; } |
| 74 | constexpr uint8_t ethernet_networking_control_model() { return 0x06; } |
| 75 | constexpr uint8_t atm_networking_control_model() { return 0x07; } |
| 76 | constexpr uint8_t wireless_handset_control_model() { return 0x08; } |
| 77 | constexpr uint8_t device_management() { return 0x09; } |
| 78 | constexpr uint8_t mobile_direct_line_model() { return 0x0A; } |
| 79 | constexpr uint8_t obex() { return 0x0B; } |
| 80 | constexpr uint8_t ethernet_emulation_model() { return 0x0C; } |
| 81 | constexpr uint8_t network_control_model() { return 0x0D; } |
| 82 | } // namespace communications_subclass |
| 83 | |
| 84 | namespace cdc_class_requests { |
| 85 | constexpr uint8_t send_encapsulated_command() { return 0x00; } |
| 86 | constexpr uint8_t get_encapsulated_response() { return 0x01; } |
| 87 | constexpr uint8_t set_comm_feature() { return 0x02; } |
| 88 | constexpr uint8_t get_comm_feature() { return 0x03; } |
| 89 | constexpr uint8_t clear_comm_feature() { return 0x04; } |
| 90 | constexpr uint8_t set_aux_line_state() { return 0x10; } |
| 91 | constexpr uint8_t set_hook_state() { return 0x11; } |
| 92 | constexpr uint8_t pulse_setup() { return 0x12; } |
| 93 | constexpr uint8_t send_pulse() { return 0x13; } |
| 94 | constexpr uint8_t set_pulse_time() { return 0x14; } |
| 95 | constexpr uint8_t ring_aux_jack() { return 0x15; } |
| 96 | constexpr uint8_t set_line_coding() { return 0x20; } |
| 97 | constexpr uint8_t get_line_coding() { return 0x21; } |
| 98 | constexpr uint8_t set_control_line_state() { return 0x22; } |
| 99 | constexpr uint8_t send_break() { return 0x23; } |
| 100 | constexpr uint8_t set_ringer_parms() { return 0x30; } |
| 101 | constexpr uint8_t get_ringer_parms() { return 0x31; } |
| 102 | constexpr uint8_t set_operation_parms() { return 0x32; } |
| 103 | constexpr uint8_t get_operation_parms() { return 0x33; } |
| 104 | constexpr uint8_t set_line_parms() { return 0x34; } |
| 105 | constexpr uint8_t get_line_parms() { return 0x35; } |
| 106 | constexpr uint8_t dial_digits() { return 0x36; } |
| 107 | constexpr uint8_t set_unit_parameter() { return 0x37; } |
| 108 | constexpr uint8_t get_unit_parameter() { return 0x38; } |
| 109 | constexpr uint8_t clear_unit_parameter() { return 0x39; } |
| 110 | constexpr uint8_t get_profile() { return 0x3A; } |
| 111 | } // namespace cdc_class_requests |
| 112 | |
| 113 | } // namespace |
| 114 | |
| 115 | void AcmTty::Initialize() { |
| 116 | status_interface_ = AddInterface(); |
| 117 | data_interface_ = AddInterface(); |
| 118 | status_endpoint_ = AddEndpoint(); |
| 119 | data_tx_endpoint_ = AddEndpoint(); |
| 120 | data_rx_endpoint_ = AddEndpoint(); |
| 121 | |
Brian Silverman | 4aa8304 | 2018-01-05 12:47:31 -0800 | [diff] [blame] | 122 | CreateIadDescriptor( |
| 123 | /*first_interface=*/status_interface_, |
| 124 | /*interface_count=*/2, |
| 125 | /*function_class=*/communications_class(), |
| 126 | /*function_subclass=*/communications_subclass::abstract_control_model(), |
| 127 | /*function_protocol=*/0, "UsbTty"); |
Brian Silverman | eda63f3 | 2017-10-08 18:57:33 -0400 | [diff] [blame] | 128 | |
| 129 | { |
| 130 | const auto interface_descriptor = CreateDescriptor( |
| 131 | interface_descriptor_length(), UsbDescriptorType::kInterface); |
| 132 | interface_descriptor->AddByte(status_interface_); // bInterfaceNumber |
| 133 | interface_descriptor->AddByte(0); // bAlternateSetting |
| 134 | interface_descriptor->AddByte(1); // bNumEndpoints |
| 135 | interface_descriptor->AddByte(communications_class()); // bInterfaceClass |
| 136 | interface_descriptor->AddByte( |
| 137 | communications_subclass:: |
| 138 | abstract_control_model()); // bInterfaceSubClass |
| 139 | interface_descriptor->AddByte(0); // bInterfaceProtocol |
| 140 | interface_descriptor->AddByte( |
| 141 | device()->AddString("UsbTty.status")); // iInterface |
| 142 | } |
| 143 | |
| 144 | { |
| 145 | const auto cdc_header = |
| 146 | CreateDescriptor(cdc_descriptor_subtype::header_length(), |
| 147 | UsbClassDescriptorType::kInterface); |
| 148 | cdc_header->AddByte( |
| 149 | cdc_descriptor_subtype::header()); // bDescriptorSubtype |
| 150 | cdc_header->AddUint16(0x0110); // bcdCDC |
| 151 | } |
| 152 | |
| 153 | { |
| 154 | const auto call_management = |
| 155 | CreateDescriptor(cdc_descriptor_subtype::call_management_length(), |
| 156 | UsbClassDescriptorType::kInterface); |
| 157 | call_management->AddByte( |
| 158 | cdc_descriptor_subtype::call_management()); // bDescriptorSubtype |
| 159 | // We don't do call management. |
| 160 | call_management->AddByte(0); // bmCapabilities |
| 161 | call_management->AddByte(data_interface_); // bDataInterface |
| 162 | } |
| 163 | |
| 164 | { |
Brian Silverman | d930f28 | 2017-11-04 23:09:12 -0400 | [diff] [blame] | 165 | const auto abstract_control_management = CreateDescriptor( |
| 166 | cdc_descriptor_subtype::abstract_control_management_length(), |
| 167 | UsbClassDescriptorType::kInterface); |
Brian Silverman | eda63f3 | 2017-10-08 18:57:33 -0400 | [diff] [blame] | 168 | abstract_control_management->AddByte( |
Brian Silverman | d930f28 | 2017-11-04 23:09:12 -0400 | [diff] [blame] | 169 | cdc_descriptor_subtype:: |
| 170 | abstract_control_management()); // bDescriptorSubtype |
Brian Silverman | eda63f3 | 2017-10-08 18:57:33 -0400 | [diff] [blame] | 171 | // We support: |
| 172 | // line_coding and serial_state |
| 173 | // send_break |
| 174 | // We don't support: |
| 175 | // comm_feature |
| 176 | // network_notification |
| 177 | abstract_control_management->AddByte(6); // bmCapabilities |
| 178 | } |
| 179 | |
| 180 | { |
| 181 | const auto cdc_union_descriptor = |
| 182 | CreateDescriptor(cdc_descriptor_subtype::union_length(1), |
| 183 | UsbClassDescriptorType::kInterface); |
| 184 | cdc_union_descriptor->AddByte( |
| 185 | cdc_descriptor_subtype::union_function()); // bDescriptorSubtype |
| 186 | cdc_union_descriptor->AddByte(status_interface_); // bMasterInterface |
| 187 | cdc_union_descriptor->AddByte(data_interface_); // bSlaveInterface |
| 188 | } |
| 189 | |
| 190 | { |
| 191 | const auto endpoint_descriptor = CreateDescriptor( |
| 192 | endpoint_descriptor_length(), UsbDescriptorType::kEndpoint); |
| 193 | endpoint_descriptor->AddByte(status_endpoint_ | |
| 194 | m_endpoint_address_in()); // bEndpointAddress |
| 195 | endpoint_descriptor->AddByte( |
| 196 | m_endpoint_attributes_interrupt()); // bmAttributes |
Brian Silverman | d930f28 | 2017-11-04 23:09:12 -0400 | [diff] [blame] | 197 | endpoint_descriptor->AddUint16(kStatusMaxPacketSize); // wMaxPacketSize |
Brian Silverman | eda63f3 | 2017-10-08 18:57:33 -0400 | [diff] [blame] | 198 | // Set it to the max because we have nothing to send, so no point using bus |
| 199 | // bandwidth asking. |
| 200 | endpoint_descriptor->AddByte(255); // bInterval |
| 201 | } |
| 202 | |
| 203 | { |
| 204 | const auto interface_descriptor = CreateDescriptor( |
| 205 | interface_descriptor_length(), UsbDescriptorType::kInterface); |
| 206 | interface_descriptor->AddByte(data_interface_); // bInterfaceNumber |
| 207 | interface_descriptor->AddByte(0); // bAlternateSetting |
| 208 | interface_descriptor->AddByte(2); // bNumEndpoints |
| 209 | interface_descriptor->AddByte(data_interface_class()); // bInterfaceClass |
| 210 | interface_descriptor->AddByte(0); // bInterfaceSubClass |
| 211 | interface_descriptor->AddByte(0); // bInterfaceProtocol |
| 212 | interface_descriptor->AddByte( |
| 213 | device()->AddString("UsbTty.data")); // iInterface |
| 214 | } |
| 215 | |
| 216 | // Kernel seems to think tx and rx belong in the other order, but it deals |
| 217 | // with either one. Everybody's examples seem to use this order, and the |
| 218 | // kernel deals with it, so going to go with this. |
| 219 | { |
| 220 | const auto endpoint_descriptor = CreateDescriptor( |
| 221 | endpoint_descriptor_length(), UsbDescriptorType::kEndpoint); |
| 222 | endpoint_descriptor->AddByte(data_rx_endpoint_); // bEndpointAddress |
| 223 | endpoint_descriptor->AddByte(m_endpoint_attributes_bulk()); // bmAttributes |
Brian Silverman | d930f28 | 2017-11-04 23:09:12 -0400 | [diff] [blame] | 224 | endpoint_descriptor->AddUint16(kDataMaxPacketSize); // wMaxPacketSize |
Brian Silverman | eda63f3 | 2017-10-08 18:57:33 -0400 | [diff] [blame] | 225 | endpoint_descriptor->AddByte(1); // bInterval |
| 226 | } |
| 227 | |
| 228 | { |
| 229 | const auto endpoint_descriptor = CreateDescriptor( |
| 230 | endpoint_descriptor_length(), UsbDescriptorType::kEndpoint); |
| 231 | endpoint_descriptor->AddByte(data_tx_endpoint_ | |
| 232 | m_endpoint_address_in()); // bEndpointAddress |
| 233 | endpoint_descriptor->AddByte(m_endpoint_attributes_bulk()); // bmAttributes |
Brian Silverman | d930f28 | 2017-11-04 23:09:12 -0400 | [diff] [blame] | 234 | endpoint_descriptor->AddUint16(kDataMaxPacketSize); // wMaxPacketSize |
Brian Silverman | eda63f3 | 2017-10-08 18:57:33 -0400 | [diff] [blame] | 235 | endpoint_descriptor->AddByte(1); // bInterval |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // We deliberately don't implement SendEncapsulatedCommand and |
| 240 | // GetEncapsulatedResponse because there doesn't seem to be any reason for |
| 241 | // anybody to send those to us, despite them being "required". |
| 242 | UsbFunction::SetupResponse AcmTty::HandleEndpoint0SetupPacket( |
| 243 | const UsbDevice::SetupPacket &setup_packet) { |
| 244 | if (G_SETUP_REQUEST_TYPE_TYPE(setup_packet.request_type) != |
| 245 | SetupRequestType::kClass) { |
| 246 | return SetupResponse::kIgnored; |
| 247 | } |
| 248 | if (G_SETUP_REQUEST_TYPE_RECIPIENT(setup_packet.request_type) != |
| 249 | standard_setup_recipients::kInterface) { |
| 250 | return SetupResponse::kIgnored; |
| 251 | } |
| 252 | if (setup_packet.index != status_interface_) { |
| 253 | return SetupResponse::kIgnored; |
| 254 | } |
| 255 | const bool in = setup_packet.request_type & M_SETUP_REQUEST_TYPE_IN; |
| 256 | switch (setup_packet.request) { |
| 257 | case cdc_class_requests::set_line_coding(): |
| 258 | if (in || setup_packet.value != 0 || |
| 259 | setup_packet.length != sizeof(line_coding_)) { |
| 260 | return SetupResponse::kStall; |
| 261 | } |
| 262 | next_endpoint0_out_ = NextEndpoint0Out::kLineCoding; |
| 263 | return SetupResponse::kHandled; |
| 264 | |
| 265 | case cdc_class_requests::get_line_coding(): |
| 266 | if (!in || setup_packet.value != 0 || |
| 267 | setup_packet.length != sizeof(line_coding_)) { |
| 268 | return SetupResponse::kStall; |
| 269 | } |
| 270 | line_coding_to_send_ = line_coding_; |
| 271 | device()->QueueEndpoint0Data( |
| 272 | reinterpret_cast<const char *>(&line_coding_to_send_), |
| 273 | setup_packet.length); |
| 274 | return SetupResponse::kHandled; |
| 275 | |
| 276 | case cdc_class_requests::set_control_line_state(): |
| 277 | if (in || setup_packet.length != 0) { |
| 278 | return SetupResponse::kStall; |
| 279 | } |
| 280 | control_line_state_ = setup_packet.value; |
| 281 | device()->SendEmptyEndpoint0Packet(); |
| 282 | return SetupResponse::kHandled; |
| 283 | |
| 284 | case cdc_class_requests::send_break(): |
| 285 | if (in || setup_packet.length != 0) { |
| 286 | return SetupResponse::kStall; |
| 287 | } |
| 288 | // TODO(Brian): setup_packet.value is the length of the break in ms. |
| 289 | // 0xFFFF means keep sending break until receiving another one. |
| 290 | device()->SendEmptyEndpoint0Packet(); |
| 291 | return SetupResponse::kHandled; |
| 292 | } |
| 293 | return SetupResponse::kStall; |
| 294 | } |
| 295 | |
| 296 | UsbFunction::SetupResponse AcmTty::HandleEndpoint0OutPacket(void *data, |
| 297 | int data_length) { |
| 298 | switch (next_endpoint0_out_) { |
| 299 | case NextEndpoint0Out::kNone: |
| 300 | return SetupResponse::kIgnored; |
| 301 | |
| 302 | case NextEndpoint0Out::kLineCoding: |
Brian Silverman | 69f96c2 | 2017-11-01 02:54:02 -0400 | [diff] [blame] | 303 | next_endpoint0_out_ = NextEndpoint0Out::kNone; |
Brian Silverman | eda63f3 | 2017-10-08 18:57:33 -0400 | [diff] [blame] | 304 | if (data_length != sizeof(line_coding_)) { |
| 305 | return SetupResponse::kStall; |
| 306 | } |
| 307 | memcpy(&line_coding_, data, data_length); |
| 308 | device()->SendEmptyEndpoint0Packet(); |
Brian Silverman | 69f96c2 | 2017-11-01 02:54:02 -0400 | [diff] [blame] | 309 | // If we're supposed to reboot, then do it. |
| 310 | if (line_coding_.rate == UINT32_C(0x97101678)) { |
| 311 | // Delay for a bit so the empty IN packet gets back to the host. |
| 312 | delay(5); |
| 313 | __asm__ __volatile__("bkpt"); |
| 314 | } |
Brian Silverman | eda63f3 | 2017-10-08 18:57:33 -0400 | [diff] [blame] | 315 | return SetupResponse::kHandled; |
| 316 | |
| 317 | default: |
| 318 | return SetupResponse::kIgnored; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | void AcmTty::HandleOutFinished(int endpoint, BdtEntry *bdt_entry) { |
| 323 | if (endpoint == data_rx_endpoint_) { |
| 324 | const size_t data_size = G_USB_BD_BC(bdt_entry->buffer_descriptor); |
| 325 | CHECK(rx_queue_.Write(static_cast<char *>(bdt_entry->address), data_size) == |
| 326 | data_size); |
| 327 | dma_memory_barrier(); |
| 328 | |
| 329 | DisableInterrupts disable_interrupts; |
| 330 | // If we don't have space to handle it, don't return the entry so we'll |
| 331 | // ask the host to wait to transmit more data. |
| 332 | if (rx_queue_.space_available() >= kDataMaxPacketSize * 2) { |
| 333 | bdt_entry->buffer_descriptor = M_USB_BD_OWN | M_USB_BD_DTS | |
| 334 | V_USB_BD_BC(kDataMaxPacketSize) | |
| 335 | static_cast<uint32_t>(next_rx_toggle_); |
| 336 | next_rx_toggle_ = Data01Inverse(next_rx_toggle_); |
| 337 | } else { |
| 338 | if (first_rx_held_ == nullptr) { |
| 339 | first_rx_held_ = bdt_entry; |
| 340 | } else { |
| 341 | CHECK(second_rx_held_ == nullptr); |
| 342 | second_rx_held_ = bdt_entry; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | void AcmTty::HandleInFinished(int endpoint, BdtEntry * /*bdt_entry*/, |
| 349 | EvenOdd odd) { |
| 350 | if (endpoint == data_tx_endpoint_) { |
| 351 | DisableInterrupts disable_interrupts; |
| 352 | CHECK(odd == BufferStateToEmpty(tx_state_)); |
| 353 | tx_state_ = BufferStateAfterEmpty(tx_state_); |
| 354 | EnqueueTxData(disable_interrupts); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | void AcmTty::HandleConfigured(int endpoint) { |
| 359 | // TODO(Brian): Handle data already in the buffers correctly. |
| 360 | if (endpoint == status_endpoint_) { |
| 361 | device()->ConfigureEndpointFor(status_endpoint_, false, true, true); |
| 362 | } else if (endpoint == data_tx_endpoint_) { |
| 363 | device()->ConfigureEndpointFor(data_tx_endpoint_, false, true, true); |
Brian Silverman | eda63f3 | 2017-10-08 18:57:33 -0400 | [diff] [blame] | 364 | DisableInterrupts disable_interrupts; |
Brian Silverman | d930f28 | 2017-11-04 23:09:12 -0400 | [diff] [blame] | 365 | next_tx_toggle_ = Data01::kData0; |
Brian Silverman | eda63f3 | 2017-10-08 18:57:33 -0400 | [diff] [blame] | 366 | EnqueueTxData(disable_interrupts); |
| 367 | } else if (endpoint == data_rx_endpoint_) { |
| 368 | device()->ConfigureEndpointFor(data_rx_endpoint_, true, false, true); |
| 369 | next_rx_toggle_ = Data01::kData0; |
| 370 | device()->SetBdtEntry( |
| 371 | data_rx_endpoint_, Direction::kRx, EvenOdd::kEven, |
Brian Silverman | 64d0427 | 2017-11-26 22:26:39 -0500 | [diff] [blame] | 372 | {M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(rx_buffers_[0].size()), |
| 373 | rx_buffers_[0].data()}); |
Brian Silverman | eda63f3 | 2017-10-08 18:57:33 -0400 | [diff] [blame] | 374 | device()->SetBdtEntry( |
| 375 | data_rx_endpoint_, Direction::kRx, EvenOdd::kOdd, |
Brian Silverman | 64d0427 | 2017-11-26 22:26:39 -0500 | [diff] [blame] | 376 | {M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(rx_buffers_[1].size()) | |
Brian Silverman | eda63f3 | 2017-10-08 18:57:33 -0400 | [diff] [blame] | 377 | M_USB_BD_DATA1, |
Brian Silverman | 64d0427 | 2017-11-26 22:26:39 -0500 | [diff] [blame] | 378 | rx_buffers_[1].data()}); |
Brian Silverman | eda63f3 | 2017-10-08 18:57:33 -0400 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
| 382 | size_t AcmTty::Read(void *buffer, size_t buffer_size) { |
| 383 | const size_t r = rx_queue_.Read(static_cast<char *>(buffer), buffer_size); |
| 384 | |
| 385 | DisableInterrupts disable_interrupts; |
| 386 | if (rx_queue_.space_available() >= kDataMaxPacketSize * 2) { |
| 387 | if (first_rx_held_ != nullptr) { |
| 388 | first_rx_held_->buffer_descriptor = |
| 389 | M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(kDataMaxPacketSize) | |
| 390 | static_cast<uint32_t>(next_rx_toggle_); |
| 391 | next_rx_toggle_ = Data01Inverse(next_rx_toggle_); |
| 392 | } |
| 393 | if (second_rx_held_ != nullptr) { |
| 394 | second_rx_held_->buffer_descriptor = |
| 395 | M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(kDataMaxPacketSize) | |
| 396 | static_cast<uint32_t>(next_rx_toggle_); |
| 397 | next_rx_toggle_ = Data01Inverse(next_rx_toggle_); |
| 398 | } |
| 399 | first_rx_held_ = second_rx_held_ = nullptr; |
| 400 | } |
| 401 | |
| 402 | return r; |
| 403 | } |
| 404 | |
| 405 | size_t AcmTty::Write(const void *buffer, size_t buffer_size) { |
| 406 | const size_t r = |
| 407 | tx_queue_.Write(static_cast<const char *>(buffer), buffer_size); |
| 408 | DisableInterrupts disable_interrupts; |
| 409 | EnqueueTxData(disable_interrupts); |
| 410 | return r; |
| 411 | } |
| 412 | |
| 413 | // TODO(Brian): Could this critical section be broken up per buffer we fill? |
| 414 | void AcmTty::EnqueueTxData(const DisableInterrupts &) { |
| 415 | while (BufferStateHasEmpty(tx_state_) && !tx_queue_.empty()) { |
| 416 | const EvenOdd next_tx_odd = BufferStateToFill(tx_state_); |
| 417 | const size_t buffer_size = |
| 418 | tx_queue_.Read(tx_buffer_for(next_tx_odd), kDataMaxPacketSize); |
| 419 | CHECK(buffer_size > 0); |
| 420 | dma_memory_barrier(); |
| 421 | device()->SetBdtEntry( |
| 422 | data_tx_endpoint_, Direction::kTx, next_tx_odd, |
| 423 | {M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(buffer_size) | |
| 424 | static_cast<uint32_t>(next_tx_toggle_), |
| 425 | tx_buffer_for(next_tx_odd)}); |
| 426 | tx_state_ = BufferStateAfterFill(tx_state_); |
| 427 | next_tx_toggle_ = Data01Inverse(next_tx_toggle_); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | } // namespace teensy |
| 432 | } // namespace frc971 |