spi面积通过

This commit is contained in:
RuigeLee
2024-06-28 18:39:35 +08:00
parent b696723e23
commit e0bfd59539
2 changed files with 148 additions and 192 deletions

View File

@@ -13,16 +13,19 @@ trait BackBoardSpiSlv{ this: BackBoardSlvLocal =>
val csn = Wire(Bool())
val clk100M = Wire(Bool())
val localReg = Module(new MirrorSRAMSpi)
localReg.io.clockw := clock.asBool
localReg.io.clockr := clock.asBool
localReg.io.addrr := statusPage
localReg.io.enr := true.B
val spiRegDown = Module(new SpiSRAMDown)
val spiRegUp = Module(new SpiSRAMUp)
spiRegDown.io.clockr := clk100M
spiRegDown.io.clockw := clock.asBool
spiRegUp.io.clockr := clock.asBool
spiRegUp.io.clockw := clk100M
val localUpdate = RegInit(0.U(16.W))
val shiftSCK = withClockAndReset( clk100M.asClock, reset ){ ShiftRegisters(sck, 3, false.B, true.B) }
val shiftCSn = withClockAndReset( clk100M.asClock, reset ){ ShiftRegisters(csn, 3, true.B, true.B) }
val shiftMOSI = withClockAndReset( clk100M.asClock, reset ){ ShiftRegisters(mosi, 3) }
@@ -32,30 +35,60 @@ trait BackBoardSpiSlv{ this: BackBoardSlvLocal =>
val isSckNegedge = shiftSCK(2) & ~shiftSCK(1)
val exReg_100M = withClockAndReset( clk100M.asClock, reset ){ Reg(UInt((8*31).W)) }
val exReg_100M = withClockAndReset( clk100M.asClock, reset ){ Reg(UInt(8.W)) }
val exMask_100M = withClockAndReset( clk100M.asClock, reset ){ Reg(UInt(16.W)) }
val cmd_100M = withClockAndReset( clk100M.asClock, reset ){ Reg(UInt(8.W)) }
val pageSel = cmd_100M(7,4)
val spiCmd = cmd_100M(3,0)
val cnt_100M = withClockAndReset( clk100M.asClock, reset ){ Reg(UInt(4.W)) }
val bitSel = withClockAndReset( clk100M.asClock, reset ){ Reg(UInt(3.W)) }
val byteSel = withClockAndReset( clk100M.asClock, reset ){ Reg(UInt(5.W)) }
spiRegDown.io.addrr := Cat(pageSel, byteSel)
spiRegDown.io.enr := spiCmd === 0.U
spiRegUp.io.addrw := Cat(pageSel, byteSel - 1.U)
spiRegUp.io.dataw := exReg_100M
spiRegUp.io.enw := isSckNegedge & ~shiftCSn(1+1) & bitSel === 0.U & spiCmd === 1.U
withClockAndReset( clk100M.asClock, reset ){//100M
when( shiftCSn(2) & ~shiftCSn(1) ){
cnt_100M := 0.U
// misoReg := false.B
} .elsewhen( isSckPosedge & ~shiftCSn(1) & cnt_100M < 8.U ){
cmd_100M := Cat( cmd_100M(6,0), shiftMOSI(1) )
cnt_100M := cnt_100M + 1.U
} .elsewhen( isSckNegedge & ~shiftCSn(1) & cnt_100M === 8.U ){
exReg_100M := Mux( spiCmd.extract(1), Cat( localUpdate, 0.U((8*29).W) ), localReg.io.datar )
cnt_100M := cnt_100M + 1.U
} .elsewhen( isSckPosedge & ~shiftCSn(1) & cnt_100M === 9.U ){
// misoReg := exReg_100M.extract(exReg_100M.getWidth-1)
exReg_100M := Cat( exReg_100M(exReg_100M.getWidth-2 ,0), shiftMOSI(1) )
bitSel := 0.U
byteSel := 0.U
} .elsewhen( isSckPosedge & ~shiftCSn(1) ){
bitSel := bitSel + 1.U
when( bitSel === 7.U ){
byteSel := byteSel + 1.U
}
}
when( shiftCSn(2) & ~shiftCSn(1) ){
bitSel := 0.U
byteSel := 0.U
cmd_100M := 0.U
exReg_100M := 0.U
} .elsewhen( isSckPosedge & ~shiftCSn(1) & byteSel === 0.U ){
cmd_100M := Cat( cmd_100M(6,0), shiftMOSI(1) )
} .elsewhen( isSckNegedge & ~shiftCSn(1) & bitSel === 0.U ){
when( byteSel === 2.U & spiCmd === 2.U ){
exReg_100M := localUpdate(7,0)
} .elsewhen( byteSel === 3.U & spiCmd === 2.U ){
exReg_100M := localUpdate(15,8)
} .elsewhen( spiCmd === 0.U ){
exReg_100M := spiRegDown.io.datar
}
} .elsewhen( isSckPosedge & ~shiftCSn(1) & byteSel =/= 0.U ){
exReg_100M := Cat( exReg_100M(6 ,0), shiftMOSI(1) )
}
when( isSckNegedge & ~shiftCSn(1) & byteSel === 2.U & bitSel === 0.U & spiCmd === 3.U ){
exMask_100M := Cat( exReg_100M, exMask_100M(7,0) )
} .elsewhen( isSckNegedge & ~shiftCSn(1) & byteSel === 3.U & bitSel === 0.U & spiCmd === 3.U ){
exMask_100M := Cat( exMask_100M(15,8), exReg_100M )
}
}
}
@@ -83,42 +116,28 @@ with BackBoardSpiSlv{
val shiftCSn_10M = ShiftRegisters(csn, 3, true.B, true.B)
when( isCrcPass ){
localUpdate := localUpdate & ~( 1.U << downRegTemp(0)(3,0) )
} .elsewhen( ~shiftCSn_10M(2) & shiftCSn_10M(1) & spiCmd === 3.U ){
localUpdate := localUpdate & (~exReg_100M(15,0))
} .elsewhen( ~shiftCSn_10M(2) & shiftCSn_10M(1) ){
localUpdate := localUpdate & ~exMask_100M
}
spiRegDown.io.enw := io.lvdsSlv.downstream_rx_data_valid & downCnt =/= 0.U
spiRegDown.io.dataw := io.lvdsSlv.downstream_rx_data_out
spiRegDown.io.addrw := Cat( downRegTemp(0)(3,0), downCnt(4,0))
spiRegUp.io.enr := io.lvdsSlv.upstream_tx_data_ready
spiRegUp.io.addrr := Cat( statusPage, upCnt(4,0)+1.U )
io.lvdsSlv.upstream_tx_data_in :=
Mux(upCnt === 0.U | statusPage === 15.U, upReg(upCnt), spiRegUp.io.datar)
// for( i <- 0 until page ){
when( isCrcPass ){
localReg.io.addrw := downRegTemp(0)(3,0)
localReg.io.enw := true.B
localReg.io.dataw := Cat( (1 until 32).map{ i => downRegTemp(i) }.reverse )
} .elsewhen( ~shiftCSn_10M(2) & shiftCSn_10M(1) & spiCmd === 1.U ){
localReg.io.addrw := pageSel
localReg.io.enw := true.B
localReg.io.dataw := exReg_100M
} .otherwise{
localReg.io.addrw := DontCare
localReg.io.enw := false.B
localReg.io.dataw := DontCare
}
for( i <- 0 until 31 ) {
upReg(i+1) := localReg.io.datar((30-i)*8+7, (30-i)*8)
}
}