2025-05-08 17:43:10 +08:00
|
|
|
|
package BACK
|
|
|
|
|
|
|
|
|
|
|
|
import chisel3._
|
|
|
|
|
|
import chisel3.util._
|
|
|
|
|
|
|
2025-05-13 18:03:22 +08:00
|
|
|
|
class QSPIInterfaceIO extends Bundle{
|
|
|
|
|
|
val sck = Input(Bool())
|
|
|
|
|
|
val mosi = Input(UInt(4.W))
|
|
|
|
|
|
val miso = Output(UInt(4.W))
|
|
|
|
|
|
val isDirIn = Output(Bool())
|
|
|
|
|
|
val csn = Input(Bool())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SSPIInterfaceIO extends Bundle{
|
2025-05-08 17:43:10 +08:00
|
|
|
|
val sck = Input(Bool())
|
|
|
|
|
|
val mosi = Input(Bool())
|
|
|
|
|
|
val miso = Output(Bool())
|
|
|
|
|
|
val csn = Input(Bool())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-15 18:40:01 +08:00
|
|
|
|
class RegAccessInterfaceIO extends Bundle{
|
2025-05-21 10:28:36 +08:00
|
|
|
|
val addrw = Output(UInt(16.W))
|
|
|
|
|
|
val addrr = Output(UInt(16.W))
|
2025-05-15 18:40:01 +08:00
|
|
|
|
val dataw = Output( UInt(8.W) )
|
|
|
|
|
|
val datar = Input( UInt(8.W) )
|
|
|
|
|
|
val isEnW = Output(Bool())
|
|
|
|
|
|
val isEnR = Output(Bool())
|
|
|
|
|
|
}
|
2025-05-08 17:43:10 +08:00
|
|
|
|
|
2025-05-13 18:03:22 +08:00
|
|
|
|
abstract class SpiInterfaceBase extends Module{
|
2025-05-15 18:40:01 +08:00
|
|
|
|
val io = IO(new RegAccessInterfaceIO)
|
2025-05-08 17:43:10 +08:00
|
|
|
|
|
2025-05-13 18:03:22 +08:00
|
|
|
|
val sck = Wire(Bool())
|
|
|
|
|
|
val csn = Wire(Bool())
|
2025-05-08 17:43:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-05-13 18:03:22 +08:00
|
|
|
|
//数据信号相对SCK的稳定应该由外部主机保证,因此认为SCK采样时刻,数据都是稳定正确的
|
2025-05-08 17:43:10 +08:00
|
|
|
|
val shiftSCK = ShiftRegisters(sck, 3, false.B, true.B)
|
|
|
|
|
|
val shiftCSn = ShiftRegisters(csn, 3, true.B, true.B)
|
2025-05-13 18:03:22 +08:00
|
|
|
|
|
2025-05-08 17:43:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val isSckPosedge = ~shiftSCK(2) & shiftSCK(1)
|
|
|
|
|
|
val isSckPosedgeNext = ShiftRegisters(isSckPosedge, 2, false.B, true.B)
|
|
|
|
|
|
|
2025-05-19 14:13:47 +08:00
|
|
|
|
val isSckNegedge = shiftSCK(2) & ~shiftSCK(1) //确定下降沿放数据到数据线上
|
2025-05-08 17:43:10 +08:00
|
|
|
|
|
2025-05-19 14:13:47 +08:00
|
|
|
|
val exRego = Reg(UInt(8.W))
|
|
|
|
|
|
val exRegi = Reg(UInt(8.W))
|
2025-05-08 17:43:10 +08:00
|
|
|
|
|
2025-05-13 18:03:22 +08:00
|
|
|
|
|
2025-05-08 17:43:10 +08:00
|
|
|
|
|
2025-05-21 10:28:36 +08:00
|
|
|
|
val addrw = Reg(UInt(16.W))
|
|
|
|
|
|
val addrr = Reg(UInt(16.W))
|
2025-05-08 17:43:10 +08:00
|
|
|
|
val spiCmd = Reg(UInt(8.W))
|
|
|
|
|
|
|
|
|
|
|
|
val isSpiWrite = spiCmd === 0.U
|
|
|
|
|
|
val isSpiRead = spiCmd === 1.U
|
|
|
|
|
|
|
|
|
|
|
|
val bitSel = Reg(UInt(3.W))
|
2025-05-12 19:52:30 +08:00
|
|
|
|
val byteSel = Reg(UInt(12.W))
|
2025-05-08 17:43:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-05-17 12:20:07 +08:00
|
|
|
|
io.isEnR :=
|
2025-05-19 14:13:47 +08:00
|
|
|
|
(isSckPosedgeNext(0) & ~shiftCSn(1) & bitSel === 0.U & exRegi === 1.U & byteSel === 3.U ) |
|
2025-05-17 12:20:07 +08:00
|
|
|
|
(isSckPosedgeNext(0) & ~shiftCSn(1) & bitSel === 0.U & isSpiRead & byteSel >= 4.U)
|
2025-05-14 16:26:20 +08:00
|
|
|
|
io.isEnW := isSckPosedgeNext(0) & ~shiftCSn(1) & bitSel === 0.U & isSpiWrite & byteSel >= 4.U
|
2025-05-19 14:13:47 +08:00
|
|
|
|
io.dataw := exRegi
|
2025-05-21 10:28:36 +08:00
|
|
|
|
io.addrr := Mux( isSpiRead, addrr + 1.U, addrr )
|
|
|
|
|
|
io.addrw := addrw
|
|
|
|
|
|
// io.addrSel :=
|
|
|
|
|
|
// Mux( isSpiRead, addrSel + 1.U, addrSel )//覆盖三种情况
|
2025-05-22 10:16:00 +08:00
|
|
|
|
// val readData = RegEnable(io.datar, io.isEnR)
|
2025-05-17 12:20:07 +08:00
|
|
|
|
|
2025-05-08 17:43:10 +08:00
|
|
|
|
|
|
|
|
|
|
when( shiftCSn(2) & ~shiftCSn(1) ){
|
|
|
|
|
|
byteSel := 0.U
|
|
|
|
|
|
} .elsewhen( isSckPosedge & ~shiftCSn(1) ){
|
2025-05-13 18:03:22 +08:00
|
|
|
|
when( bitSel.andR ){
|
2025-05-08 17:43:10 +08:00
|
|
|
|
byteSel := byteSel + 1.U
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-13 18:03:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-05-08 17:43:10 +08:00
|
|
|
|
when( shiftCSn(2) & ~shiftCSn(1) ){ //CS下跳
|
2025-05-21 10:28:36 +08:00
|
|
|
|
addrw := 0.U
|
2025-05-14 16:26:20 +08:00
|
|
|
|
} .elsewhen(isSckPosedgeNext(0) & ~shiftCSn(1) & bitSel === 0.U & byteSel === 1.U){ //CS为低,且SCK上跳的第0byte
|
2025-05-21 10:28:36 +08:00
|
|
|
|
addrw := exRegi
|
2025-05-14 16:26:20 +08:00
|
|
|
|
} .elsewhen(isSckPosedgeNext(0) & ~shiftCSn(1) & bitSel === 0.U & byteSel === 2.U) {//CS为低,且SCK上跳的第0byte
|
2025-05-21 10:28:36 +08:00
|
|
|
|
addrw := Cat(exRegi, addrw(7,0))
|
2025-05-12 19:52:30 +08:00
|
|
|
|
} .otherwise{ // byteSel > 1.U
|
2025-05-14 16:26:20 +08:00
|
|
|
|
when( isSckPosedgeNext(1) & byteSel > 3.U & bitSel === 0.U ){
|
2025-05-21 10:28:36 +08:00
|
|
|
|
addrw := addrw + 1.U
|
2025-05-08 17:43:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-21 10:28:36 +08:00
|
|
|
|
when( shiftCSn(2) & ~shiftCSn(1) ){ //CS下跳
|
|
|
|
|
|
addrr := 0.U
|
|
|
|
|
|
} .elsewhen(isSckPosedgeNext(0) & ~shiftCSn(1) & bitSel === 0.U & byteSel === 1.U){ //CS为低,且SCK上跳的第0byte
|
|
|
|
|
|
addrr := exRegi
|
|
|
|
|
|
} .elsewhen(isSckPosedgeNext(0) & ~shiftCSn(1) & bitSel === 0.U & byteSel === 2.U) {//CS为低,且SCK上跳的第0byte
|
|
|
|
|
|
addrr := Cat(exRegi, addrr(7,0))
|
|
|
|
|
|
} .otherwise{ // byteSel > 1.U
|
|
|
|
|
|
when( isSckPosedgeNext(1) & byteSel > 3.U & bitSel === 0.U ){
|
|
|
|
|
|
addrr := addrr + 1.U
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-05-12 19:52:30 +08:00
|
|
|
|
|
2025-05-08 17:43:10 +08:00
|
|
|
|
when( shiftCSn(2) & ~shiftCSn(1) ){ //CS下跳
|
|
|
|
|
|
spiCmd := "hFF".U
|
2025-05-14 16:26:20 +08:00
|
|
|
|
} .elsewhen( isSckPosedgeNext(0) & ~shiftCSn(1) & byteSel === 3.U & bitSel === 0.U ){ //CS为低,且SCK上跳的第0byte
|
2025-05-19 14:13:47 +08:00
|
|
|
|
spiCmd := exRegi
|
2025-05-08 17:43:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-13 18:03:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class SSpiInterface extends SpiInterfaceBase{
|
|
|
|
|
|
val spi = IO(new SSPIInterfaceIO)
|
|
|
|
|
|
|
|
|
|
|
|
sck := spi.sck
|
|
|
|
|
|
csn := spi.csn
|
|
|
|
|
|
|
2025-05-19 14:13:47 +08:00
|
|
|
|
spi.miso := exRego.extract(7)
|
2025-05-13 18:03:22 +08:00
|
|
|
|
|
|
|
|
|
|
when( shiftCSn(2) & ~shiftCSn(1) ){
|
|
|
|
|
|
bitSel := 0.U
|
|
|
|
|
|
} .elsewhen( isSckPosedge & ~shiftCSn(1) ){
|
|
|
|
|
|
bitSel := bitSel + 1.U
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
val shiftMOSI = ShiftRegisters(spi.mosi, 3)
|
|
|
|
|
|
|
2025-05-19 14:13:47 +08:00
|
|
|
|
//输入看SCK上升沿
|
|
|
|
|
|
when( isSckPosedge & ~shiftCSn(1) ){
|
|
|
|
|
|
when( bitSel === 0.U ){
|
|
|
|
|
|
exRegi := Cat( 0.U, shiftMOSI(1) )
|
|
|
|
|
|
}.otherwise{
|
|
|
|
|
|
exRegi := Cat( exRegi(6 ,0), shiftMOSI(1) )
|
|
|
|
|
|
}
|
2025-05-08 17:43:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-19 14:13:47 +08:00
|
|
|
|
//输出在SCK下降沿准备数据
|
|
|
|
|
|
when( shiftCSn(2) & ~shiftCSn(1) ){ //CS下跳
|
|
|
|
|
|
exRego := 0.U //第一个byte 地址0
|
|
|
|
|
|
} .elsewhen( isSckNegedge & ~shiftCSn(1) ){
|
|
|
|
|
|
when( bitSel === 0.U ){
|
|
|
|
|
|
when((byteSel === 1.U || byteSel === 2.U) ){
|
|
|
|
|
|
exRego := 0.U //地址1,cmd
|
|
|
|
|
|
} .elsewhen( isSpiRead ){
|
2025-05-22 10:16:00 +08:00
|
|
|
|
exRego := io.datar
|
2025-05-19 14:13:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
}.otherwise{
|
|
|
|
|
|
exRego := exRego << 1
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-05-08 17:43:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-13 18:03:22 +08:00
|
|
|
|
class QSpiInterface extends SpiInterfaceBase{
|
|
|
|
|
|
val qspi = IO(new QSPIInterfaceIO)
|
2025-05-08 17:43:10 +08:00
|
|
|
|
|
2025-05-13 18:03:22 +08:00
|
|
|
|
sck := qspi.sck
|
|
|
|
|
|
csn := qspi.csn
|
2025-05-19 14:13:47 +08:00
|
|
|
|
qspi.miso := exRego(7, 4)
|
2025-05-20 14:53:21 +08:00
|
|
|
|
qspi.isDirIn := ~isSpiRead
|
2025-05-08 17:43:10 +08:00
|
|
|
|
|
2025-05-13 18:03:22 +08:00
|
|
|
|
when( shiftCSn(2) & ~shiftCSn(1) ){
|
|
|
|
|
|
bitSel := 0.U
|
|
|
|
|
|
} .elsewhen( isSckPosedge & ~shiftCSn(1) ){
|
|
|
|
|
|
bitSel := ~bitSel
|
|
|
|
|
|
}
|
2025-05-08 17:43:10 +08:00
|
|
|
|
|
2025-05-13 18:03:22 +08:00
|
|
|
|
val shiftMOSI = ShiftRegisters(qspi.mosi, 3)
|
2025-05-08 17:43:10 +08:00
|
|
|
|
|
2025-05-19 14:13:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//输入看SCK上升沿
|
|
|
|
|
|
when( isSckPosedge & ~shiftCSn(1) ){
|
|
|
|
|
|
when( bitSel === 0.U ){
|
|
|
|
|
|
exRegi := Cat( 0.U, shiftMOSI(1) )
|
|
|
|
|
|
}.otherwise{
|
|
|
|
|
|
exRegi := Cat( exRegi(3 ,0), shiftMOSI(1) )
|
|
|
|
|
|
}
|
2025-05-13 18:03:22 +08:00
|
|
|
|
}
|
2025-05-08 17:43:10 +08:00
|
|
|
|
|
2025-05-19 14:13:47 +08:00
|
|
|
|
//输出在SCK下降沿准备数据
|
|
|
|
|
|
when( shiftCSn(2) & ~shiftCSn(1) ){ //CS下跳
|
|
|
|
|
|
exRego := 0.U //第一个byte 地址0
|
|
|
|
|
|
} .elsewhen( isSckNegedge & ~shiftCSn(1) ){
|
|
|
|
|
|
when( bitSel === 0.U ){
|
|
|
|
|
|
when((byteSel === 1.U || byteSel === 2.U) ){
|
|
|
|
|
|
exRego := 0.U //地址1,cmd
|
|
|
|
|
|
} .elsewhen( isSpiRead ){
|
2025-05-22 10:16:00 +08:00
|
|
|
|
exRego := io.datar
|
2025-05-19 14:13:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
}.otherwise{
|
|
|
|
|
|
exRego := exRego << 4
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-05-08 17:43:10 +08:00
|
|
|
|
|
2025-05-13 18:03:22 +08:00
|
|
|
|
}
|
2025-05-08 17:43:10 +08:00
|
|
|
|
|
|
|
|
|
|
|