|
2 | 2 | #define KLIB_CYPRESS_MB9BF560L_USB_HPP |
3 | 3 |
|
4 | 4 | #include <span> |
| 5 | +#include <atomic> |
5 | 6 |
|
6 | 7 | #include <klib/klib.hpp> |
7 | 8 | #include <klib/math.hpp> |
@@ -132,6 +133,9 @@ namespace klib::core::mb9bf560l::io { |
132 | 133 | // create a instance of the helper |
133 | 134 | static inline auto helper = irq_helper(); |
134 | 135 |
|
| 136 | + // flag if the last request was a setup packet |
| 137 | + static inline std::atomic<bool> setup_packet_received = false; |
| 138 | + |
135 | 139 | /** |
136 | 140 | * @brief Convert a mode to the correct raw value for the usb hardware |
137 | 141 | * |
@@ -651,10 +655,14 @@ namespace klib::core::mb9bf560l::io { |
651 | 655 | if (const uint16_t ep_status = Usb::port->EP0OS; ep_status & (0x1 << 10)) [[likely]] { |
652 | 656 | // check if we have a setup packet |
653 | 657 | if (status & (0x1 << 1)) [[likely]] { |
| 658 | + setup_packet_received = true; |
| 659 | + |
654 | 660 | // we have a setup packet |
655 | 661 | setup_packet_irq(); |
656 | 662 | } |
657 | 663 | else if (ep_status & (0x1 << 14)) { |
| 664 | + setup_packet_received = false; |
| 665 | + |
658 | 666 | // get the amount of bytes we received |
659 | 667 | const uint32_t count = get_endpoint_byte_count(0, klib::usb::usb::endpoint_mode::out); |
660 | 668 |
|
@@ -974,6 +982,17 @@ namespace klib::core::mb9bf560l::io { |
974 | 982 | // This is handled in hardware. No need to send a ack again. In some |
975 | 983 | // cases the hardware does not send a ack. For example the SET_IDLE. |
976 | 984 | // We send this manually when we detect it. |
| 985 | + if (endpoint == 0 && setup_packet_received.load()) { |
| 986 | + // for endpoint 0 setup the hardware sends a ack automatically. Ignore these |
| 987 | + return; |
| 988 | + } |
| 989 | + |
| 990 | + // get the endpoint status register |
| 991 | + volatile uint16_t *const ep_status = get_endpoint_status(endpoint, mode); |
| 992 | + |
| 993 | + // clear the drq flag to mark we have written/read the data to read the |
| 994 | + // zlp or write the zlp |
| 995 | + (*ep_status) &= (~(0x1 << 10)); |
977 | 996 | } |
978 | 997 |
|
979 | 998 | /** |
|
0 commit comments