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

Use Amaranth SoC GPIO peripheral #20

Merged
merged 2 commits into from
Jul 2, 2024
Merged
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
20 changes: 10 additions & 10 deletions chipflow_lib/providers/board_ulx3s.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
from amaranth import *
from amaranth_boards.ulx3s import *
from amaranth.lib.cdc import ResetSynchronizer
from amaranth.lib import io
from amaranth.lib import io, wiring
from amaranth.lib.wiring import In

from amaranth_soc import gpio

from amaranth_orchard.memory.spimemio import QSPIPins
from amaranth_orchard.base.gpio import GPIOPins
from amaranth_orchard.io.uart import UARTPins
from amaranth_orchard.memory.hyperram import HyperRAMPins

Expand Down Expand Up @@ -47,29 +49,27 @@ def elaborate(self, platform):
return m


class LEDGPIOProvider(Elaboratable):
def __init__(self):
self.pins = GPIOPins(width=8)
class LEDGPIOProvider(wiring.Component):
pins: In(gpio.PinSignature()).array(8)

def elaborate(self, platform):
m = Module()
for i in range(8):
led = io.Buffer("o", platform.request("led", i, dir="-"))
m.submodules[f"led_{i}"] = led
m.d.comb += led.o.eq(self.pins.o[i])
m.d.comb += led.o.eq(self.pins[i].o)
return m


class ButtonGPIOProvider(Elaboratable):
def __init__(self):
self.pins = GPIOPins(width=2)
class ButtonGPIOProvider(wiring.Component):
pins: In(gpio.PinSignature()).array(2)

def elaborate(self, platform):
m = Module()
for i in range(2):
btn = io.Buffer("i", platform.request("button_fire", i, dir="-"))
m.submodules[f"btn_{i}"] = btn
m.d.comb += self.pins.i[i].eq(btn.i)
m.d.comb += self.pins[i].i.eq(btn.i)
return m


Expand Down
27 changes: 14 additions & 13 deletions chipflow_lib/providers/silicon.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

from amaranth import *
from amaranth.lib.cdc import FFSynchronizer
from amaranth.lib import wiring
from amaranth.lib.wiring import In

from amaranth_soc import gpio

from amaranth_orchard.memory.spimemio import QSPIPins
from amaranth_orchard.base.gpio import GPIOPins
from amaranth_orchard.io.uart import UARTPins
from amaranth_orchard.memory.hyperram import HyperRAMPins

Expand All @@ -29,34 +32,32 @@ def elaborate(self, platform):
return m


class LEDGPIOProvider(Elaboratable):
def __init__(self):
self.pins = GPIOPins(width=8)
class LEDGPIOProvider(wiring.Component):
pins: In(gpio.PinSignature()).array(8)

def elaborate(self, platform):
m = Module()
for index in range(8):
pin = platform.request(f"gpio_{index}")
m.d.comb += [
self.pins.i[index].eq(pin.i),
pin.o.eq(self.pins.o[index]),
pin.oe.eq(self.pins.oe[index])
self.pins[index].i.eq(pin.i),
pin.o.eq(self.pins[index].o),
pin.oe.eq(self.pins[index].oe),
]
return m


class ButtonGPIOProvider(Elaboratable):
def __init__(self):
self.pins = GPIOPins(width=2)
class ButtonGPIOProvider(wiring.Component):
pins: In(gpio.PinSignature()).array(2)

def elaborate(self, platform):
m = Module()
for index in range(2):
pin = platform.request(f"btn_{index}")
m.d.comb += [
self.pins.i[index].eq(pin.i),
pin.o.eq(self.pins.o[index]),
pin.oe.eq(self.pins.oe[index])
self.pins[index].i.eq(pin.i),
pin.o.eq(self.pins[index].o),
pin.oe.eq(self.pins[index].oe),
]
return m

Expand Down
18 changes: 10 additions & 8 deletions chipflow_lib/providers/sim.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# SPDX-License-Identifier: BSD-2-Clause

from amaranth import *
from amaranth.lib import wiring
from amaranth.lib.wiring import In

from amaranth_soc import gpio

from amaranth_orchard.memory.spimemio import QSPIPins
from amaranth_orchard.base.gpio import GPIOPins
from amaranth_orchard.io.uart import UARTPins
from amaranth_orchard.memory.hyperram import HyperRAMPins

Expand All @@ -16,21 +19,20 @@ def elaborate(self, platform):
return platform.add_model("spiflash_model", self.pins, edge_det=['clk_o', 'csn_o'])


class LEDGPIOProvider(Elaboratable):
def __init__(self):
self.pins = GPIOPins(width=8)
class LEDGPIOProvider(wiring.Component):
pins: In(gpio.PinSignature()).array(8)

def elaborate(self, platform):
return Module()


class ButtonGPIOProvider(Elaboratable):
def __init__(self):
self.pins = GPIOPins(width=2)
class ButtonGPIOProvider(wiring.Component):
pins: In(gpio.PinSignature()).array(2)

def elaborate(self, platform):
m = Module()
m.d.comb += self.pins.i.eq(platform.buttons)
for i in range(2):
m.d.comb += self.pins[i].i.eq(platform.buttons[i])
return m


Expand Down
43 changes: 39 additions & 4 deletions chipflow_lib/software/drivers/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,45 @@

#include <stdint.h>

typedef struct __attribute__((packed)) {
uint32_t out;
uint32_t oe;
uint32_t in;
typedef struct __attribute__((packed, aligned(2))) {
uint16_t mode;
uint8_t input;
uint8_t output;
uint16_t setclr;
} gpio_regs_t;

typedef enum {
#define _GPIO_PIN(n) \
GPIO_PIN ## n ## _INPUT_ONLY = (0 << 2 * (n)), \
GPIO_PIN ## n ## _PUSH_PULL = (1 << 2 * (n)), \
GPIO_PIN ## n ## _OPEN_DRAIN = (2 << 2 * (n)), \
GPIO_PIN ## n ## _ALTERNATE = (3 << 2 * (n))

_GPIO_PIN(0),
_GPIO_PIN(1),
_GPIO_PIN(2),
_GPIO_PIN(3),
_GPIO_PIN(4),
_GPIO_PIN(5),
_GPIO_PIN(6),
_GPIO_PIN(7),
#undef _GPIO_PIN
} gpio_mode_t;

typedef enum {
#define _GPIO_PIN(n) \
GPIO_PIN ## n ## _SET = (1 << 2 * (n)), \
GPIO_PIN ## n ## _CLEAR = (2 << 2 * (n))

_GPIO_PIN(0),
_GPIO_PIN(1),
_GPIO_PIN(2),
_GPIO_PIN(3),
_GPIO_PIN(4),
_GPIO_PIN(5),
_GPIO_PIN(6),
_GPIO_PIN(7),
#undef _GPIO_PIN
} gpio_setclr_t;

#endif
2 changes: 1 addition & 1 deletion chipflow_lib/software/drivers/plat_timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include <stdint.h>

typedef struct __attribute__((packed)) {
typedef struct __attribute__((packed, aligned(4))) {
uint32_t cnt_lo;
uint32_t cnt_hi;
uint32_t cmp_lo;
Expand Down
2 changes: 1 addition & 1 deletion chipflow_lib/software/drivers/soc_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include <stdint.h>

typedef struct __attribute__((packed)) {
typedef struct __attribute__((packed, aligned(4))) {
uint32_t type;
uint32_t version;
} soc_id_regs_t;
Expand Down
2 changes: 1 addition & 1 deletion chipflow_lib/software/drivers/spiflash.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#define WINBOND_ID 0x40
#define ISSI_ID 0x60

typedef struct __attribute__((packed)) {
typedef struct __attribute__((packed, aligned(4))) {
uint32_t ctrl;
} spiflash_regs_t;

Expand Down
2 changes: 1 addition & 1 deletion chipflow_lib/software/drivers/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include <stdint.h>

typedef struct __attribute__((packed)) {
typedef struct __attribute__((packed, aligned(4))) {
uint32_t tx_data;
uint32_t rx_data;
uint32_t tx_ready;
Expand Down
5 changes: 0 additions & 5 deletions chipflow_lib/software/soft_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,6 @@ def start(self):
blt a0, a1, loop_init_bss
end_init_bss:

# Update LEDs
li a0, 0xb1000000
li a1, 2
sw a1, 0(a0)

# call main
call main
loop:
Expand Down