package sifive.blocks.devices.spi
import Chisel._
-import sifive.blocks.devices.gpio.{GPIOPin, GPIOOutputPinCtrl, GPIOInputPinCtrl}
+import chisel3.experimental.{withClockAndReset}
+import sifive.blocks.devices.pinctrl.{PinCtrl, Pin}
+
+class SPISignals[T <: Data] (pingen: ()=> T, c: SPIParamsBase) extends SPIBundle(c) {
+
+ val sck = pingen()
+ val dq = Vec(4, pingen())
+ val cs = Vec(c.csWidth, pingen())
+
+ override def cloneType: this.type =
+ this.getClass.getConstructors.head.newInstance(pingen, c).asInstanceOf[this.type]
-class SPIPinsIO(c: SPIParamsBase) extends SPIBundle(c) {
- val sck = new GPIOPin
- val dq = Vec(4, new GPIOPin)
- val cs = Vec(c.csWidth, new GPIOPin)
}
-class SPIGPIOPort(c: SPIParamsBase, syncStages: Int = 0, driveStrength: Bool = Bool(false)) extends Module {
- val io = new SPIBundle(c) {
- val spi = new SPIPortIO(c).flip
- val pins = new SPIPinsIO(c)
- }
+class SPIPins[T <: Pin] (pingen: ()=> T, c: SPIParamsBase) extends SPISignals(pingen, c)
- GPIOOutputPinCtrl(io.pins.sck, io.spi.sck, ds = driveStrength)
-
- GPIOOutputPinCtrl(io.pins.dq, Bits(0, io.spi.dq.size))
- (io.pins.dq zip io.spi.dq).foreach {
- case (p, s) =>
- p.o.oval := s.o
- p.o.oe := s.oe
- p.o.ie := ~s.oe
- p.o.pue := Bool(true)
- p.o.ds := driveStrength
- s.i := ShiftRegister(p.i.ival, syncStages)
- }
+object SPIPinsFromPort {
+
+ def apply[T <: Pin](pins: SPISignals[T], spi: SPIPortIO, clock: Clock, reset: Bool,
+ syncStages: Int = 0, driveStrength: Bool = Bool(false)) {
+
+ withClockAndReset(clock, reset) {
+ pins.sck.outputPin(spi.sck, ds = driveStrength)
- GPIOOutputPinCtrl(io.pins.cs, io.spi.cs.asUInt)
- io.pins.cs.foreach(_.o.ds := driveStrength)
+ (pins.dq zip spi.dq).foreach {case (p, s) =>
+ p.outputPin(s.o, pue = Bool(true), ds = driveStrength)
+ p.o.oe := s.oe
+ p.o.ie := ~s.oe
+ s.i := ShiftRegister(p.i.ival, syncStages)
+ }
+
+ (pins.cs zip spi.cs) foreach { case (c, s) =>
+ c.outputPin(s, ds = driveStrength)
+ }
+ }
+ }
}