Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gateware.architecture.adv: add ApolloAdvertiser #215

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions luna/gateware/architecture/adv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#
# This file is part of LUNA.
#
# Copyright (c) 2023 Great Scott Gadgets <[email protected]>
# SPDX-License-Identifier: BSD-3-Clause

""" Controllers for communicating with Apollo through the FPGA_ADV pin """

from amaranth import Elaboratable, Module, Signal, Mux
from amaranth_stdio.serial import AsyncSerialTX

from luna.gateware.usb.usb2.request import USBRequestHandler
from usb_protocol.types import USBRequestType


class ApolloAdvertiser(Elaboratable):
""" Gateware that implements a periodic announcement to Apollo using the FPGA_ADV pin.

Currently it is used to tell Apollo that the gateware wants to use the CONTROL port.
Apollo will keep the port switch connected to the FPGA after a reset as long as this
message is received periodically.
Once the port is lost, Apollo will ignore further messages until a specific vendor
request is called.

I/O ports:
I: stop -- Advertisement messages are stopped if this line is asserted.
"""
def __init__(self):
self.stop = Signal()

def default_request_handler(self):
return ApolloAdvertiserRequestHandler(self.stop)

def elaborate(self, platform):
m = Module()

clk_freq = platform.DEFAULT_CLOCK_FREQUENCIES_MHZ["sync"] * 1e6

# Communication is done with a serial transmitter (unidirectional)
baudrate = 9600
divisor = int(clk_freq // baudrate)
fpga_adv = AsyncSerialTX(divisor=divisor, data_bits=8, parity="even")
m.submodules += fpga_adv

# Counter with 50ms period
period = int(clk_freq * 50e-3)
timer = Signal(range(period))
m.d.sync += timer.eq(Mux(timer == period-1, 0, timer+1))

# Trigger announcement when the counter overflows
m.d.comb += [
fpga_adv.data .eq(ord('A')),
fpga_adv.ack .eq((timer == 0) & ~self.stop),
]

# Drive the FPGA_ADV pin with the serial transmitter
m.d.comb += platform.request("int").o.eq(fpga_adv.o)

return m


class ApolloAdvertiserRequestHandler(USBRequestHandler):
""" Request handler for ApolloAdvertiser.

Implements default vendor requests related to ApolloAdvertiser.
"""
REQUEST_APOLLO_ADV_STOP = 0xF0

def __init__(self, stop_pin=None):
super().__init__()
self.stop_pin = stop_pin

def elaborate(self, platform):
m = Module()

interface = self.interface
setup = self.interface.setup

#
# Vendor request handlers.

with m.If(setup.type == USBRequestType.VENDOR):
with m.Switch(setup.request):

with m.Case(self.REQUEST_APOLLO_ADV_STOP):

# Once the receive is complete, respond with an ACK.
with m.If(interface.rx_ready_for_response):
m.d.comb += interface.handshakes_out.ack.eq(1)

# If we reach the status stage, send a ZLP.
with m.If(interface.status_requested):
m.d.comb += self.send_zlp()
m.d.usb += self.stop_pin.eq(1)

with m.Case():

#
# Stall unhandled requests.
#
with m.If(interface.status_requested | interface.data_requested):
m.d.comb += interface.handshakes_out.stall.eq(1)

return m
6 changes: 6 additions & 0 deletions luna/gateware/platform/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ def toolchain_program(self, products, name):
with debugger.jtag as jtag:
programmer = ECP5_JTAGProgrammer(jtag)
programmer.configure(bitstream)

# Let the LUNA gateware take over in devices with shared USB port
debugger.honor_fpga_adv()


def _ensure_unconfigured(self, debugger):
Expand Down Expand Up @@ -141,6 +144,9 @@ def toolchain_flash(self, products, name="top"):

debugger.soft_reset()

# Let the LUNA gateware take over in devices with shared USB port
debugger.honor_fpga_adv()


def toolchain_erase(self):
""" Erases the LUNA board's flash. """
Expand Down
Loading