Skip to content

Commit

Permalink
spi: a bit of refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
schoeberl committed Nov 28, 2024
1 parent 6dcaea4 commit b3f88df
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
12 changes: 11 additions & 1 deletion src/main/scala/spi/SerialSpiTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package spi

import com.fazecast.jSerialComm._

import chiseltest._

/**
* A simple test for SPI communication.
* @param id (Adx, Flash, SRAM)
Expand Down Expand Up @@ -228,7 +230,7 @@ class SerialSpiTest(id: Int, portName: String = "/dev/tty.usbserial-210292B40860
readStatusRegister()
}

def echoPins(sck: Int, mosi: Int, ncs: Int) = {
def echoPinsOuter(sck: Int, mosi: Int, ncs: Int) = {
// println(s"ncs: $ncs, mosi: $mosi, sck: $sck")
val bits = (ncs << 2) | (mosi << 1) | sck
val s = "w4" + (bits + '0').toChar + "4\r"
Expand All @@ -240,6 +242,14 @@ class SerialSpiTest(id: Int, portName: String = "/dev/tty.usbserial-210292B40860
// println("bit: " + bit)
bit
}

def echoPins(spi: SpiIO) = {
val sck = spi.sclk.peekInt().toInt
val mosi = spi.mosi.peekInt().toInt
val ncs = spi.ncs.peekInt().toInt
val bit = echoPinsOuter(sck, mosi, ncs)
spi.miso.poke(bit)
}
}

object SerialSpiTest extends App {
Expand Down
13 changes: 8 additions & 5 deletions src/test/scala/spi/FlashTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class FlashTest() extends AnyFlatSpec with ChiselScalatestTester {

it should "test the flash" in {

var spi: SerialSpiTest = null
var spiDriver: SerialSpiTest = null
try {
spi = new SerialSpiTest(1)
spiDriver = new SerialSpiTest(1)
} catch {
case e: Exception => {
println("Serial port not available")
Expand All @@ -23,19 +23,22 @@ class FlashTest() extends AnyFlatSpec with ChiselScalatestTester {

test(new SpiMaster) { c =>

/*
def echoPins() = {
val sck = c.spi.sclk.peekInt().toInt
val mosi = c.spi.mosi.peekInt().toInt
val ncs = c.spi.ncs.peekInt().toInt
val bit = spi.echoPins(sck, mosi, ncs)
val bit = spiDriver.echoPinsOuter(sck, mosi, ncs)
c.spi.miso.poke(bit)
}
*/
def readWord(addr: Int) = {
c.io.readData.ready.poke(true.B)

c.io.readAddr.valid.poke(true.B)
c.io.readAddr.bits.poke(addr.U)
echoPins()
spiDriver.echoPins(c.spi)
c.clock.step()
/*
while(!c.io.readAddr.ready.peekBoolean()) {
Expand All @@ -49,7 +52,7 @@ class FlashTest() extends AnyFlatSpec with ChiselScalatestTester {
var ready = false
var v = 0
while(!ready) {
echoPins()
spiDriver.echoPins(c.spi)
c.clock.step()
if (c.io.readData.valid.peekBoolean()) {
ready = true
Expand Down

0 comments on commit b3f88df

Please sign in to comment.