From 22ca7e7c34f2ee2c9cda0c634c99ff951cad067d Mon Sep 17 00:00:00 2001 From: Asherah Connor Date: Wed, 22 May 2024 13:41:27 +0300 Subject: [PATCH] UART: refactor into one Resource. --- .../platform/ice40/IceBreakerPlatform.scala | 3 +-- .../chryse/platform/resource/Resource.scala | 1 - .../hrzn/chryse/platform/resource/UART.scala | 25 +++++++++++++++++++ .../chryse/platform/resource/UARTRX.scala | 9 ------- .../chryse/platform/resource/UARTTX.scala | 9 ------- .../chryse/platform/BoardResourcesSpec.scala | 14 +++++------ 6 files changed, 32 insertions(+), 29 deletions(-) create mode 100644 src/main/scala/ee/hrzn/chryse/platform/resource/UART.scala delete mode 100644 src/main/scala/ee/hrzn/chryse/platform/resource/UARTRX.scala delete mode 100644 src/main/scala/ee/hrzn/chryse/platform/resource/UARTTX.scala 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 0d81480..1011d97 100644 --- a/src/main/scala/ee/hrzn/chryse/platform/ice40/IceBreakerPlatform.scala +++ b/src/main/scala/ee/hrzn/chryse/platform/ice40/IceBreakerPlatform.scala @@ -28,8 +28,7 @@ class IceBreakerResources extends BoardResources { val ubtn = resource.Button().inverted.onPin(10) - val uart_tx = resource.UARTTX().onPin(9) - val uart_rx = resource.UARTRX().onPin(6) + val uart = resource.UART().onPins(rx = 6, tx = 9) val ledg = resource.LED().inverted.onPin(37) val ledr = resource.LED().inverted.onPin(11) diff --git a/src/main/scala/ee/hrzn/chryse/platform/resource/Resource.scala b/src/main/scala/ee/hrzn/chryse/platform/resource/Resource.scala index 00b208c..239f73d 100644 --- a/src/main/scala/ee/hrzn/chryse/platform/resource/Resource.scala +++ b/src/main/scala/ee/hrzn/chryse/platform/resource/Resource.scala @@ -7,7 +7,6 @@ import scala.collection.mutable.ArrayBuffer trait Resource { def setName(name: String): Unit - def onPin(id: Pin): this.type def bases(): List[Base[_ <: Data]] } diff --git a/src/main/scala/ee/hrzn/chryse/platform/resource/UART.scala b/src/main/scala/ee/hrzn/chryse/platform/resource/UART.scala new file mode 100644 index 0000000..6de1a67 --- /dev/null +++ b/src/main/scala/ee/hrzn/chryse/platform/resource/UART.scala @@ -0,0 +1,25 @@ +package ee.hrzn.chryse.platform.resource + +import chisel3._ + +class UART extends Resource { + val rx = new Base[Bool](Input(Bool())) {} + val tx = new Base[Bool](Output(Bool())) {} + + def setName(name: String): Unit = { + rx.setName(s"${name}_rx") + tx.setName(s"${name}_tx") + } + + def onPins(rx: Pin, tx: Pin): this.type = { + this.rx.onPin(rx) + this.tx.onPin(tx) + this + } + + def bases(): List[Base[_ <: Data]] = List(rx, tx) +} + +object UART { + def apply() = new UART +} diff --git a/src/main/scala/ee/hrzn/chryse/platform/resource/UARTRX.scala b/src/main/scala/ee/hrzn/chryse/platform/resource/UARTRX.scala deleted file mode 100644 index 75b562b..0000000 --- a/src/main/scala/ee/hrzn/chryse/platform/resource/UARTRX.scala +++ /dev/null @@ -1,9 +0,0 @@ -package ee.hrzn.chryse.platform.resource - -import chisel3._ - -class UARTRX extends Base[Bool](Input(Bool())) {} - -object UARTRX { - def apply() = new UARTRX -} diff --git a/src/main/scala/ee/hrzn/chryse/platform/resource/UARTTX.scala b/src/main/scala/ee/hrzn/chryse/platform/resource/UARTTX.scala deleted file mode 100644 index 4eb6294..0000000 --- a/src/main/scala/ee/hrzn/chryse/platform/resource/UARTTX.scala +++ /dev/null @@ -1,9 +0,0 @@ -package ee.hrzn.chryse.platform.resource - -import chisel3._ - -class UARTTX extends Base[Bool](Output(Bool())) {} - -object UARTTX { - def apply() = new UARTTX -} diff --git a/src/test/scala/ee/hrzn/chryse/platform/BoardResourcesSpec.scala b/src/test/scala/ee/hrzn/chryse/platform/BoardResourcesSpec.scala index 57113d4..713ad94 100644 --- a/src/test/scala/ee/hrzn/chryse/platform/BoardResourcesSpec.scala +++ b/src/test/scala/ee/hrzn/chryse/platform/BoardResourcesSpec.scala @@ -105,10 +105,8 @@ class BoardResourcesSpec extends AnyFlatSpec with Matchers { "\\s+".r .replaceAllIn(rtl, " ") should include( "module chrysetop( " + - "input clock, ubtn, " + - "output uart_tx, " + - "input uart_rx, " + - "output ledr, pmod1a1, " + + "input clock, ubtn, uart_rx, " + + "output uart_tx, ledr, pmod1a1, " + "input pmod1a2, " + "output pmod1b1, " + "input pmod1b2 " + @@ -120,14 +118,14 @@ class BoardResourcesSpec extends AnyFlatSpec with Matchers { class DetectionTop(platform: Platform) extends Module { val plat = platform.asInstanceOf[IceBreakerPlatform] plat.resources.ledg := plat.resources.ubtn - plat.resources.uart_tx := plat.resources.uart_rx + plat.resources.uart.tx := plat.resources.uart.rx } class InversionTop(platform: Platform) extends Module { val plat = platform.asInstanceOf[IceBreakerPlatform] // User button is inverted. // UART isn't inverted. - plat.resources.uart_tx := plat.resources.ubtn + plat.resources.uart.tx := plat.resources.ubtn // LED is inverted. plat.resources.ledg := plat.resources.ubtn } @@ -135,8 +133,8 @@ class InversionTop(platform: Platform) extends Module { class InOutTop(platform: Platform) extends Module { val plat = platform.asInstanceOf[IceBreakerPlatform] // Treat pmod1a1 as output, 1a2 as input. - plat.resources.pmod1a1.o := plat.resources.uart_rx - plat.resources.uart_tx := plat.resources.pmod1a2.i + plat.resources.pmod1a1.o := plat.resources.uart.rx + plat.resources.uart.tx := plat.resources.pmod1a2.i // Do the same with 1b1 and 1b2, but use inverted inputs/outputs. plat.resources.pmod1b1.o := plat.resources.ubtn