|
| 1 | +from amaranth import * |
| 2 | +from usb_protocol.types import USBRequestType, USBRequestRecipient |
| 3 | + |
| 4 | +from luna.gateware.usb.usb2.request import USBRequestHandler |
| 5 | +from luna.gateware.usb.stream import USBInStreamInterface |
| 6 | +from luna.gateware.stream.generator import StreamSerializer |
| 7 | + |
| 8 | +class ACMRequestHandler(USBRequestHandler): |
| 9 | + def __init__(self, if_num): |
| 10 | + super().__init__() |
| 11 | + |
| 12 | + self.if_num = if_num |
| 13 | + |
| 14 | + self.new_request = Signal() |
| 15 | + self.request_done = Signal() |
| 16 | + |
| 17 | + def handle_set_line_coding(self, m): |
| 18 | + with m.If(self.interface.rx_ready_for_response): |
| 19 | + m.d.comb += self.interface.handshakes_out.ack.eq(1) |
| 20 | + |
| 21 | + with m.If(self.interface.status_requested): |
| 22 | + m.d.comb += self.send_zlp() |
| 23 | + m.d.comb += self.request_done.eq(1) |
| 24 | + |
| 25 | + def handle_unhandled(self, m): |
| 26 | + interface = self.interface |
| 27 | + |
| 28 | + with m.If(interface.rx_ready_for_response | interface.data_requested | interface.status_requested): |
| 29 | + m.d.comb += [ |
| 30 | + interface.handshakes_out.stall.eq(1), |
| 31 | + self.request_done.eq(1), |
| 32 | + ] |
| 33 | + |
| 34 | + def transition(self, m): |
| 35 | + setup = self.interface.setup |
| 36 | + |
| 37 | + with m.If(self.request_done): |
| 38 | + m.next = 'IDLE' |
| 39 | + |
| 40 | + targeting_if = (setup.recipient == USBRequestRecipient.INTERFACE) & (setup.index == self.if_num) |
| 41 | + |
| 42 | + with m.If(setup.received & targeting_if): |
| 43 | + m.next = 'DISPATCH' |
| 44 | + |
| 45 | + def elaborate(self, platform): |
| 46 | + m = Module() |
| 47 | + |
| 48 | + m.submodules.transmitter = self.transmitter = StreamSerializer(data_length = 6, domain = 'usb', stream_type=USBInStreamInterface) |
| 49 | + |
| 50 | + interface = self.interface |
| 51 | + setup = self.interface.setup |
| 52 | + |
| 53 | + m.d.usb += self.new_request.eq(0) |
| 54 | + |
| 55 | + with m.FSM(domain = 'usb'): |
| 56 | + with m.State('IDLE'): |
| 57 | + self.transition(m) |
| 58 | + |
| 59 | + with m.State('DISPATCH'): |
| 60 | + m.d.usb += self.new_request.eq(1) |
| 61 | + |
| 62 | + m.next = 'UNHANDLED' |
| 63 | + |
| 64 | + with m.If(setup.type == USBRequestType.CLASS): |
| 65 | + with m.Switch(setup.request): |
| 66 | + with m.Case(0x20): # SET_LINE_CODING |
| 67 | + m.next = 'SET_LINE_CODING' |
| 68 | + |
| 69 | + with m.State('SET_LINE_CODING'): |
| 70 | + self.handle_set_line_coding(m) |
| 71 | + self.transition(m) |
| 72 | + |
| 73 | + with m.State('UNHANDLED'): |
| 74 | + self.handle_unhandled(m) |
| 75 | + self.transition(m) |
| 76 | + |
| 77 | + return m |
0 commit comments