Skip to content

Commit 7dd8c25

Browse files
committed
gateware.usb2.endpoints: use amaranth.lib.streams for isochronous endpoints
1 parent e9adbaa commit 7dd8c25

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-19
lines changed

luna/gateware/stream/future.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#
2+
# This file is part of LUNA.
3+
#
4+
# Copyright (c) 2025 Great Scott Gadgets <info@greatscottgadgets.com>
5+
# SPDX-License-Identifier: BSD-3-Clause
6+
7+
""" Core stream definitions for supporting native Amaranth 0.5 streams. """
8+
9+
from amaranth import *
10+
from amaranth.lib import data
11+
12+
class Packet(data.StructLayout):
13+
def __init__(self, data_layout, first=True, last=True):
14+
sig = (first and { "first": unsigned(1) } or {}) \
15+
| (last and { "last": unsigned(1) } or {})
16+
super().__init__(sig | {
17+
"data": data_layout
18+
})

luna/gateware/usb/usb2/endpoints/isochronous_in.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
interfaces to hosts via isochronous pipes.
1010
"""
1111

12-
from amaranth import Elaboratable, Module, Signal, unsigned
13-
# TODO from amaranth.lib import stream
12+
from amaranth import *
13+
from amaranth.lib import stream
1414

15-
from ..endpoint import EndpointInterface
16-
from ...stream import StreamInterface # TODO
15+
from ..endpoint import EndpointInterface
1716

1817

1918
class USBIsochronousStreamInEndpoint(Elaboratable):
@@ -69,10 +68,7 @@ def __init__(self, *, endpoint_number, max_packet_size):
6968
# I/O Port
7069
#
7170
self.interface = EndpointInterface()
72-
# TODO self.stream = stream.Interface(
73-
# stream.Signature(unsigned(8))
74-
# )
75-
self.stream = StreamInterface()
71+
self.stream = stream.Interface(stream.Signature(unsigned(8)))
7672
self.data_requested = Signal()
7773
self.frame_finished = Signal()
7874

luna/gateware/usb/usb2/endpoints/isochronous_out.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
interfaces to hosts via isochronous pipes.
1111
"""
1212

13-
from amaranth import Elaboratable, Module, Signal
13+
from amaranth import *
14+
from amaranth.lib import stream, wiring
15+
from amaranth.lib .wiring import In, Out
1416

15-
from ..endpoint import EndpointInterface
16-
from ...stream import StreamInterface, USBOutStreamBoundaryDetector
17-
from ....memory import TransactionalizedFIFO
17+
from ..endpoint import EndpointInterface
18+
from ...stream import USBOutStreamBoundaryDetector
19+
from ....stream.future import Packet
20+
from ....memory import TransactionalizedFIFO
1821

1922

2023
class USBIsochronousStreamOutEndpoint(Elaboratable):
@@ -52,9 +55,15 @@ def __init__(self, *, endpoint_number, max_packet_size, buffer_size=None):
5255
#
5356
# I/O port
5457
#
55-
self.stream = StreamInterface()
58+
self.stream = stream.Interface(
59+
stream.Signature(
60+
Packet(unsigned(8))
61+
)
62+
)
5663
self.interface = EndpointInterface()
5764

65+
self.busy = Signal()
66+
self.byteclk = Signal()
5867

5968
def elaborate(self, platform):
6069
m = Module()
@@ -125,17 +134,17 @@ def elaborate(self, platform):
125134

126135
# Our stream data always comes directly out of the FIFO; and is valid
127136
# whenever our FIFO actually has data for us to read.
128-
stream.valid .eq(~fifo.empty),
129-
stream.payload .eq(fifo.read_data[0:8]),
137+
stream.valid .eq(~fifo.empty),
138+
stream.p.data .eq(fifo.read_data[0:8]),
130139

131140
# Our `last` bit comes directly from the FIFO; and we know a `first` bit immediately
132141
# follows a `last` one.
133-
stream.last .eq(fifo.read_data[8]),
134-
stream.first .eq(fifo.read_data[9]),
142+
stream.p.last .eq(fifo.read_data[8]),
143+
stream.p.first .eq(fifo.read_data[9]),
135144

136145
# Move to the next byte in the FIFO whenever our stream is advaced.
137-
fifo.read_en .eq(stream.ready),
138-
fifo.read_commit .eq(1)
146+
fifo.read_en .eq(stream.ready),
147+
fifo.read_commit .eq(1)
139148
]
140149

141150
# Count bytes in packet.
@@ -155,4 +164,11 @@ def elaborate(self, platform):
155164
m.d.usb += overflow.eq(0)
156165
m.d.usb += rx_cnt.eq(0)
157166

167+
# Debug
168+
last_cnt = Signal.like(rx_cnt)
169+
m.d.comb += self.busy.eq(rx_cnt != 0)
170+
m.d.usb += last_cnt.eq(rx_cnt)
171+
with m.If(last_cnt != rx_cnt):
172+
m.d.usb += self.byteclk.eq(~self.byteclk)
173+
158174
return m

0 commit comments

Comments
 (0)