From 868d1375a5fac2c347d765d481bd0433debabccd Mon Sep 17 00:00:00 2001 From: Asherah Connor Date: Wed, 29 May 2024 21:46:39 +0300 Subject: [PATCH] WIP. --- .../hrzn/chryse/platform/ecp5/PullMode.scala | 7 ++ .../chryse/platform/ecp5/ULX3SPlatform.scala | 59 ++++++++++------- .../platform/ice40/IceBreakerPlatform.scala | 4 +- .../platform/resource/Disconnected.scala | 11 ++++ .../hrzn/chryse/platform/resource/LED.scala | 6 +- .../chryse/platform/resource/SPIOLED.scala | 65 +++++++++++++++++++ .../ee/hrzn/chryse/platform/SimPlatform.scala | 6 +- 7 files changed, 128 insertions(+), 30 deletions(-) create mode 100644 src/main/scala/ee/hrzn/chryse/platform/ecp5/PullMode.scala create mode 100644 src/main/scala/ee/hrzn/chryse/platform/resource/Disconnected.scala create mode 100644 src/main/scala/ee/hrzn/chryse/platform/resource/SPIOLED.scala diff --git a/src/main/scala/ee/hrzn/chryse/platform/ecp5/PullMode.scala b/src/main/scala/ee/hrzn/chryse/platform/ecp5/PullMode.scala new file mode 100644 index 0000000..602e5ec --- /dev/null +++ b/src/main/scala/ee/hrzn/chryse/platform/ecp5/PullMode.scala @@ -0,0 +1,7 @@ +package ee.hrzn.chryse.platform.ecp5 + +object PullMode { + val UP = "UP" + val DOWN = "DOWN" + val NONE = "NONE" +} diff --git a/src/main/scala/ee/hrzn/chryse/platform/ecp5/ULX3SPlatform.scala b/src/main/scala/ee/hrzn/chryse/platform/ecp5/ULX3SPlatform.scala index bf20e5c..01cec8b 100644 --- a/src/main/scala/ee/hrzn/chryse/platform/ecp5/ULX3SPlatform.scala +++ b/src/main/scala/ee/hrzn/chryse/platform/ecp5/ULX3SPlatform.scala @@ -6,6 +6,7 @@ import ee.hrzn.chryse.platform.PlatformBoard import ee.hrzn.chryse.platform.PlatformBoardResources import ee.hrzn.chryse.platform.resource.Button import ee.hrzn.chryse.platform.resource.ClockSource +import ee.hrzn.chryse.platform.resource.Connector import ee.hrzn.chryse.platform.resource.LED import ee.hrzn.chryse.platform.resource.ResourceData import ee.hrzn.chryse.platform.resource.SPIFlash @@ -44,44 +45,58 @@ class ULX3SPlatformResources extends PlatformBoardResources { val clock = ClockSource(25_000_000).onPin("G2") val program = - LED().inverted.onPin("M4").withAttributes("PULLMODE" -> "UP") + LED().onPin("M4").withAttributes("PULLMODE" -> PullMode.UP) - // TODO: also expose RTS, DTR. + // TODO: also expose RTS, DTR inputs. var uart = UART() .onPins(rx = "M1", tx = "L4") // TODO: either just unconditionally set this on, or only when uart.tx is // accessed. var uartTxEnable = ResourceData(Output(Bool())).onPin("L3") - // TODO - val led0 = LED().inverted.onPin("B2").withAttributes("DRIVE" -> 4) - val led1 = LED().inverted.onPin("C2").withAttributes("DRIVE" -> 4) - val led2 = LED().inverted.onPin("C1").withAttributes("DRIVE" -> 4) - val led3 = LED().inverted.onPin("D2").withAttributes("DRIVE" -> 4) - val led4 = LED().inverted.onPin("D1").withAttributes("DRIVE" -> 4) - val led5 = LED().inverted.onPin("E2").withAttributes("DRIVE" -> 4) - val led6 = LED().inverted.onPin("E1").withAttributes("DRIVE" -> 4) - val led7 = LED().inverted.onPin("H3").withAttributes("DRIVE" -> 4) -// val leds = -// resource -// .LEDs() -// .onPins("B2", "C2", "C1", "D2", "D1", "E2", "E1", "H3") -// .withAttributes("DRIVE" -> "4") + val leds = Connector( + LED().withAttributes("DRIVE" -> 4), + 0 -> "B2", + 1 -> "C2", + 2 -> "C1", + 3 -> "D2", + 4 -> "D1", + 5 -> "E2", + 6 -> "E1", + 7 -> "H3", + ) val spiFlash = SPIFlash() .onPins( csN = "R2", clock = USRMCLKPin, copi = "W2", cipo = "V2", wpN = "Y2", holdN = "W1", ) - .withAttributes("PULLMODE" -> "NONE", "DRIVE" -> "4") + .withAttributes("PULLMODE" -> PullMode.NONE, "DRIVE" -> 4) + + val buttonPwr = + Button().inverted.onPin("D6").withAttributes("PULLMODE" -> PullMode.UP) + val buttonFire0 = + Button().onPin("R1").withAttributes("PULLMODE" -> PullMode.DOWN) + val buttonFire1 = + Button().onPin("T1").withAttributes("PULLMODE" -> PullMode.DOWN) + val buttonLeft = + Button().onPin("U1").withAttributes("PULLMODE" -> PullMode.DOWN) + val buttonDown = + Button().onPin("V1").withAttributes("PULLMODE" -> PullMode.DOWN) + val buttonUp = + Button().onPin("R18").withAttributes("PULLMODE" -> PullMode.DOWN) + val buttonRight = + Button().onPin("H16").withAttributes("PULLMODE" -> PullMode.DOWN) + + // val oledBl = onPin("J4") + // val oledCs = onPin("N2") + // val oledDc = onPin("P1") + // val oledRes = onPin("P2") + // val oledCopi = onPin("P3") + // val oledClk = onPin("P4") - // TODO - val butt0 = Button().inverted.onPin("D6").withAttributes("PULLMODE" -> "UP") -// val buttons = // DIP switches - // UART // SD card - // SPI flash // SDRAM // ADC // TRRS diff --git a/src/main/scala/ee/hrzn/chryse/platform/ice40/IceBreakerPlatform.scala b/src/main/scala/ee/hrzn/chryse/platform/ice40/IceBreakerPlatform.scala index b0d2a5d..1649b46 100644 --- a/src/main/scala/ee/hrzn/chryse/platform/ice40/IceBreakerPlatform.scala +++ b/src/main/scala/ee/hrzn/chryse/platform/ice40/IceBreakerPlatform.scala @@ -37,8 +37,8 @@ class IceBreakerPlatformResources extends PlatformBoardResources { .onPins(rx = 6, tx = 9) .withAttributes("IO_STANDARD" -> IOStandard.LVTTL, "PULLUP" -> 1) - val ledg = LED().inverted.onPin(37) - val ledr = LED().inverted.onPin(11) + val ledg = LED().onPin(37) + val ledr = LED().onPin(11) var spiFlash = SPIFlash() diff --git a/src/main/scala/ee/hrzn/chryse/platform/resource/Disconnected.scala b/src/main/scala/ee/hrzn/chryse/platform/resource/Disconnected.scala new file mode 100644 index 0000000..09eef74 --- /dev/null +++ b/src/main/scala/ee/hrzn/chryse/platform/resource/Disconnected.scala @@ -0,0 +1,11 @@ +package ee.hrzn.chryse.platform.resource + +import ee.hrzn.chryse.platform.resource.Pin +import ee.hrzn.chryse.platform.resource.PinPlatform + +import scala.language.implicitConversions + +object Disconnected { + implicit def disconnected2Pin(disc: this.type): Pin = + PinPlatform(this) +} diff --git a/src/main/scala/ee/hrzn/chryse/platform/resource/LED.scala b/src/main/scala/ee/hrzn/chryse/platform/resource/LED.scala index 23f3f82..66d0cc6 100644 --- a/src/main/scala/ee/hrzn/chryse/platform/resource/LED.scala +++ b/src/main/scala/ee/hrzn/chryse/platform/resource/LED.scala @@ -2,9 +2,9 @@ package ee.hrzn.chryse.platform.resource import chisel3._ -class LED - extends ResourceData(Output(Bool())) - with ResourceDataUserInvertible {} +class LED extends ResourceData(Output(Bool())) { + _invert = true +} object LED { def apply() = new LED diff --git a/src/main/scala/ee/hrzn/chryse/platform/resource/SPIOLED.scala b/src/main/scala/ee/hrzn/chryse/platform/resource/SPIOLED.scala new file mode 100644 index 0000000..ef43fbd --- /dev/null +++ b/src/main/scala/ee/hrzn/chryse/platform/resource/SPIOLED.scala @@ -0,0 +1,65 @@ +package ee.hrzn.chryse.platform.resource + +import chisel3._ +import chisel3.experimental.Param + +class SPIOLED extends ResourceBase { + val cs = ResourceData(Output(Bool()), invert = true) + val clock = ResourceData(Output(Clock())) + val copi = ResourceData(Output(Bool())) + val cipo = ResourceData(Input(Bool())) + val wp = ResourceData(Output(Bool()), invert = true) + val hold = ResourceData(Output(Bool()), invert = true) + + def setName(name: String): Unit = { + cs.setName(s"${name}_cs") + clock.setName(s"${name}_clock") + copi.setName(s"${name}_copi") + cipo.setName(s"${name}_cipo") + wp.setName(s"${name}_wp") + hold.setName(s"${name}_hold") + } + + def withAttributes(attribs: (String, Param)*): this.type = { + cs.withAttributes(attribs: _*) + clock.withAttributes(attribs: _*) + copi.withAttributes(attribs: _*) + cipo.withAttributes(attribs: _*) + wp.withAttributes(attribs: _*) + hold.withAttributes(attribs: _*) + this + } + + def setDefaultAttributes(defaultAttributes: Map[String, Param]): Unit = { + cs.setDefaultAttributes(defaultAttributes) + clock.setDefaultAttributes(defaultAttributes) + copi.setDefaultAttributes(defaultAttributes) + cipo.setDefaultAttributes(defaultAttributes) + wp.setDefaultAttributes(defaultAttributes) + hold.setDefaultAttributes(defaultAttributes) + } + + def onPins( + csN: Pin, + clock: Pin, + copi: Pin, + cipo: Pin, + wpN: Pin, + holdN: Pin, + ): this.type = { + this.cs.onPin(csN) + this.clock.onPin(clock) + this.copi.onPin(copi) + this.cipo.onPin(cipo) + this.wp.onPin(wpN) + this.hold.onPin(holdN) + this + } + + def data: Seq[ResourceData[_ <: Data]] = + Seq(cs, clock, copi, cipo, wp, hold) +} + +object SPIOLED { + def apply() = new SPIOLED +} diff --git a/src/test/scala/ee/hrzn/chryse/platform/SimPlatform.scala b/src/test/scala/ee/hrzn/chryse/platform/SimPlatform.scala index 126610f..5eef887 100644 --- a/src/test/scala/ee/hrzn/chryse/platform/SimPlatform.scala +++ b/src/test/scala/ee/hrzn/chryse/platform/SimPlatform.scala @@ -42,9 +42,9 @@ class SimPlatformResources extends PlatformBoardResources { val uart = UART().onPins(rx = "C3", tx = "D4") - val ledg = LED().inverted.onPin("E5") - val ledr = LED().inverted.onPin("F6") - val led3 = LED().inverted.onPin("G7") + val ledg = LED().onPin("E5") + val ledr = LED().onPin("F6") + val led3 = LED().onPin("G7") val pmod = Connector( InOut(),