From 256aca63b7c1fbe2d788d3a5c4db14b064424063 Mon Sep 17 00:00:00 2001 From: dxl <64101226@qq.com> Date: Mon, 12 Oct 2026 00:04:06 +0800 Subject: [PATCH] FPGA: Add source files specific to PM5. --- fpga/fpga_pm5_hf.v | 382 ++++++++++++++++++++++++++++++++++++++++++++ fpga/fpga_pm5_lf.v | 335 ++++++++++++++++++++++++++++++++++++++ fpga/fpga_pm5_top.v | 132 +++++++++++++++ fpga/gowin_rpll.v | 64 ++++++++ 4 files changed, 913 insertions(+) create mode 100644 fpga/fpga_pm5_hf.v create mode 100644 fpga/fpga_pm5_lf.v create mode 100644 fpga/fpga_pm5_top.v create mode 100644 fpga/gowin_rpll.v diff --git a/fpga/fpga_pm5_hf.v b/fpga/fpga_pm5_hf.v new file mode 100644 index 000000000..b6b37670a --- /dev/null +++ b/fpga/fpga_pm5_hf.v @@ -0,0 +1,382 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// +// The FPGA is responsible for interfacing between the A/D, the coil drivers, +// and the ARM. In the low-frequency modes it passes the data straight +// through, so that the ARM gets raw A/D samples over the SSP. In the high- +// frequency modes, the FPGA might perform some demodulation first, to +// reduce the amount of data that we must send to the ARM. +//----------------------------------------------------------------------------- + + +module fpga_hf( + input ck_1356meg, + input spck, + //input pck0, + input ncs, + input [7:0] adc_d, + //input cross_hi, + //input cross_lo, + input mosi, + input ssp_dout, + input ce, // 复位输入,常态高 + + output ssp_din, + output ssp_frame, + output ssp_clk, + output adc_clk, + //output adc_noe, + output miso, + output pwr_lo, + output pwr_hi, + output pwr_oe1, + output pwr_oe2, + //output pwr_oe3, + output pwr_oe4, + output dbg, + output reg [11:0] pwm_duty = 12'h800 +); + + +// In all modes, let the ADC's outputs be enabled. +//assign adc_noe = 1'b0; +assign miso = 1'bZ; +wire cross_hi = 1'b0; + +wire [2:0] major_mode; +assign major_mode = ce ? conf_word[8:6] : 3'b111; + +//----------------------------------------------------------------------------- +// The SPI receiver. This sets up the configuration word, which the rest of +// the logic looks at to determine how to connect the A/D and the coil +// drivers (i.e., which section gets it). Also assign some symbolic names +// to the configuration bits, for use below. +//----------------------------------------------------------------------------- + +// Receive 16bits of data from ARM here. +reg [15:0] shift_reg; + +//always @(posedge spck) if (~ncs) shift_reg <= {shift_reg[14:0], mosi}; +always @(posedge spck) begin + if(~ncs & ce) + shift_reg <= {shift_reg[14:0], mosi}; +end + +reg trace_enable; + +reg [5:0] hf_edge_detect_threshold = 6'd7; +reg [5:0] hf_edge_detect_threshold_high = 6'd20; + +reg [8:0] conf_word; + +initial +begin + hf_edge_detect_threshold <= 7; + hf_edge_detect_threshold_high <= 20; +end + +// We switch modes between transmitting to the 13.56 MHz tag and receiving +// from it, which means that we must make sure that we can do so without +// glitching, or else we will glitch the transmitted carrier. +always @(posedge ncs) +begin + if(ce) begin + // 4 bit command + case (shift_reg[15:12]) + `FPGA_CMD_SET_CONFREG: conf_word <= shift_reg[8:0]; + `FPGA_CMD_TRACE_ENABLE: trace_enable <= shift_reg[0]; + `FPGA_CMD_SET_EDGE_DETECT_THRESHOLD: + begin + hf_edge_detect_threshold <= shift_reg[5:0]; + hf_edge_detect_threshold_high <= shift_reg[11:6]; + end + `FPGA_CMD_SET_PWR_PWM_LOW_COUNT:pwm_duty <= shift_reg[11:0]; + endcase + end +end +//----------------------------------------------------------------------------- +// And then we instantiate the modules corresponding to each of the FPGA's +// major modes, and use muxes to connect the outputs of the active mode to +// the output pins. +//----------------------------------------------------------------------------- + +// HF reader +hi_reader hr( + .ck_1356meg (ck_1356meg), + .adc_d (adc_d), + .subcarrier_frequency (conf_word[5:4]), + .minor_mode (conf_word[3:0]), + .ssp_dout (ssp_dout), + + .ssp_din (mux0_ssp_din), + .ssp_frame (mux0_ssp_frame), + .ssp_clk (mux0_ssp_clk), + .adc_clk (mux0_adc_clk), + .pwr_lo (mux0_pwr_lo), + .pwr_hi (mux0_pwr_hi), + .pwr_oe1 (mux0_pwr_oe1), + .pwr_oe2 (mux0_pwr_oe2), + .pwr_oe3 (mux0_pwr_oe3), + .pwr_oe4 (mux0_pwr_oe4), + .debug (mux0_debug) +); + +// HF simulated tag +hi_simulate hs( + .ck_1356meg (ck_1356meg), + .adc_d (adc_d), + .mod_type (conf_word[3:0]), + .ssp_dout (ssp_dout), + + .ssp_din (mux1_ssp_din), + .ssp_frame (mux1_ssp_frame), + .ssp_clk (mux1_ssp_clk), + .adc_clk (mux1_adc_clk), + .pwr_lo (mux1_pwr_lo), + .pwr_hi (mux1_pwr_hi), + .pwr_oe1 (mux1_pwr_oe1), + .pwr_oe2 (mux1_pwr_oe2), + .pwr_oe3 (mux1_pwr_oe3), + .pwr_oe4 (mux1_pwr_oe4), + .debug (mux1_debug) +); + +// HF ISO14443-A +hi_iso14443a hisn( + .ck_1356meg (ck_1356meg), + .adc_d (adc_d), + .mod_type (conf_word[3:0]), + .ssp_dout (ssp_dout), + + .ssp_din (mux2_ssp_din), + .ssp_frame (mux2_ssp_frame), + .ssp_clk (mux2_ssp_clk), + .adc_clk (mux2_adc_clk), + .pwr_lo (mux2_pwr_lo), + .pwr_hi (mux2_pwr_hi), + .pwr_oe1 (mux2_pwr_oe1), + .pwr_oe2 (mux2_pwr_oe2), + .pwr_oe3 (mux2_pwr_oe3), + .pwr_oe4 (mux2_pwr_oe4), + .debug (mux2_debug), + .edge_detect_threshold ({5'b00000, hf_edge_detect_threshold}), + .edge_detect_threshold_high ({5'b00000, hf_edge_detect_threshold_high}) +); + +// HF sniff +hi_sniffer he( + .ck_1356meg (ck_1356meg), + .adc_d (adc_d), + + .ssp_din (mux3_ssp_din), + .ssp_frame (mux3_ssp_frame), + .ssp_clk (mux3_ssp_clk), + .adc_clk (mux3_adc_clk), + .pwr_lo (mux3_pwr_lo), + .pwr_hi (mux3_pwr_hi), + .pwr_oe1 (mux3_pwr_oe1), + .pwr_oe2 (mux3_pwr_oe2), + .pwr_oe3 (mux3_pwr_oe3), + .pwr_oe4 (mux3_pwr_oe4) +); + +// HF ISO18092 FeliCa +hi_flite hfl( + .ck_1356meg (ck_1356meg), + .adc_d (adc_d), + .mod_type (conf_word[3:0]), + .ssp_dout (ssp_dout), + + .ssp_din (mux4_ssp_din), + .ssp_frame (mux4_ssp_frame), + .ssp_clk (mux4_ssp_clk), + .adc_clk (mux4_adc_clk), + .pwr_lo (mux4_pwr_lo), + .pwr_hi (mux4_pwr_hi), + .pwr_oe1 (mux4_pwr_oe1), + .pwr_oe2 (mux4_pwr_oe2), + .pwr_oe3 (mux4_pwr_oe3), + .pwr_oe4 (mux4_pwr_oe4), + .debug (mux4_debug) +); + +// HF get trace + +hi_get_trace gt( + .ck_1356megb (ck_1356meg), + .adc_d (adc_d), + .trace_enable (trace_enable), + .major_mode (major_mode), + .ssp_din (mux5_ssp_din), + .ssp_frame (mux5_ssp_frame), + .ssp_clk (mux5_ssp_clk) +); + +// These assignments must agree with the defines in fpgaloader.h + +// Major modes High Frequency +// mux0 = HF reader +// mux1 = HF simulated tag +// mux2 = HF ISO14443-A +// mux3 = HF sniff +// mux4 = HF ISO18092 FeliCa +// mux5 = HF get trace +// mux6 = unused +// mux7 = FPGA_MAJOR_MODE_OFF + +mux8 mux_ssp_clk ( + .sel (major_mode), + .y (ssp_clk ), + .x0 (mux0_ssp_clk ), + .x1 (mux1_ssp_clk ), + .x2 (mux2_ssp_clk ), + .x3 (mux3_ssp_clk ), + .x4 (mux4_ssp_clk ), + .x5 (mux5_ssp_clk ), + .x6 (1'b0 ), + .x7 (1'b0 ) +); + +mux8 mux_ssp_din ( + .sel (major_mode), + .y (ssp_din ), + .x0 (mux0_ssp_din ), + .x1 (mux1_ssp_din ), + .x2 (mux2_ssp_din ), + .x3 (mux3_ssp_din ), + .x4 (mux4_ssp_din ), + .x5 (mux5_ssp_din ), + .x6 (1'b0 ), + .x7 (1'b0 ) +); + +mux8 mux_ssp_frame ( + .sel (major_mode), + .y (ssp_frame ), + .x0 (mux0_ssp_frame ), + .x1 (mux1_ssp_frame ), + .x2 (mux2_ssp_frame ), + .x3 (mux3_ssp_frame ), + .x4 (mux4_ssp_frame ), + .x5 (mux5_ssp_frame ), + .x6 (1'b0 ), + .x7 (1'b0 ) +); + +mux8 mux_pwr_oe1 ( + .sel (major_mode), + .y (pwr_oe1 ), + .x0 (mux0_pwr_oe1 ), + .x1 (mux1_pwr_oe1 ), + .x2 (mux2_pwr_oe1 ), + .x3 (mux3_pwr_oe1 ), + .x4 (mux4_pwr_oe1 ), + .x5 (1'b0 ), + .x6 (1'b0 ), + .x7 (1'b0 ) +); + +mux8 mux_pwr_oe2 ( + .sel (major_mode), + .y (pwr_oe2 ), + .x0 (mux0_pwr_oe2 ), + .x1 (mux1_pwr_oe2 ), + .x2 (mux2_pwr_oe2 ), + .x3 (mux3_pwr_oe2 ), + .x4 (mux4_pwr_oe2 ), + .x5 (1'b0 ), + .x6 (1'b0 ), + .x7 (1'b0 ) +); + +mux8 mux_pwr_oe3 ( + .sel (major_mode), + .y (pwr_oe3 ), + .x0 (mux0_pwr_oe3 ), + .x1 (mux1_pwr_oe3 ), + .x2 (mux2_pwr_oe3 ), + .x3 (mux3_pwr_oe3 ), + .x4 (mux4_pwr_oe3 ), + .x5 (1'b0 ), + .x6 (1'b0 ), + .x7 (1'b0 ) +); + +mux8 mux_pwr_oe4 ( + .sel (major_mode), + .y (pwr_oe4 ), + .x0 (mux0_pwr_oe4 ), + .x1 (mux1_pwr_oe4 ), + .x2 (mux2_pwr_oe4 ), + .x3 (mux3_pwr_oe4 ), + .x4 (mux4_pwr_oe4 ), + .x5 (1'b0 ), + .x6 (1'b0 ), + .x7 (1'b0 ) +); + +mux8 mux_pwr_lo ( + .sel (major_mode), + .y (pwr_lo ), + .x0 (mux0_pwr_lo ), + .x1 (mux1_pwr_lo ), + .x2 (mux2_pwr_lo ), + .x3 (mux3_pwr_lo ), + .x4 (mux4_pwr_lo ), + .x5 (1'b0 ), + .x6 (1'b0 ), + .x7 (1'b0 ) +); + +mux8 mux_pwr_hi ( + .sel (major_mode), + .y (pwr_hi ), + .x0 (mux0_pwr_hi ), + .x1 (mux1_pwr_hi ), + .x2 (mux2_pwr_hi ), + .x3 (mux3_pwr_hi ), + .x4 (mux4_pwr_hi ), + .x5 (1'b0 ), + .x6 (1'b0 ), + .x7 (1'b0 ) +); + +mux8 mux_adc_clk ( + .sel (major_mode), + .y (adc_clk ), + .x0 (mux0_adc_clk ), + .x1 (mux1_adc_clk ), + .x2 (mux2_adc_clk ), + .x3 (mux3_adc_clk ), + .x4 (mux4_adc_clk ), + .x5 (1'b0 ), + .x6 (1'b0 ), + .x7 (1'b0 ) +); + +mux8 mux_dbg ( + .sel (major_mode), + .y (dbg ), + .x0 (mux0_debug ), + .x1 (mux1_debug ), + .x2 (mux2_debug ), + .x3 (1'b0 ), + .x4 (mux4_debug ), + .x5 (1'b0 ), + .x6 (1'b0 ), + .x7 (1'b0 ) +); +endmodule diff --git a/fpga/fpga_pm5_lf.v b/fpga/fpga_pm5_lf.v new file mode 100644 index 000000000..77044d0f8 --- /dev/null +++ b/fpga/fpga_pm5_lf.v @@ -0,0 +1,335 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// +// The FPGA is responsible for interfacing between the A/D, the coil drivers, +// and the ARM. In the low-frequency modes it passes the data straight +// through, so that the ARM gets raw A/D samples over the SSP. In the high- +// frequency modes, the FPGA might perform some demodulation first, to +// reduce the amount of data that we must send to the ARM. +//----------------------------------------------------------------------------- + + +module fpga_lf( + //input ck_1356meg, + input spck, + input pck0, + input ncs, + input [7:0] adc_d, + input cross_lo, + input mosi, + input ssp_dout, + input ce, // 复位输入,常态高 + + output ssp_din, + output ssp_frame, + output ssp_clk, + output adc_clk, + output miso, + output pwr_lo, + output pwr_oe1, + output pwr_oe2, + output pwr_oe4, + output dbg, + output reg [11:0] pwm_duty = 12'h500 +); + +// In all modes, let the ADC's outputs be enabled. +//assign adc_noe = 1'b0; +assign miso = 1'bZ; + +wire [2:0] major_mode; +assign major_mode = ce ? conf_word[8:6] : 3'b111; + +//----------------------------------------------------------------------------- +// The SPI receiver. This sets up the configuration word, which the rest of +// the logic looks at to determine how to connect the A/D and the coil +// drivers (i.e., which section gets it). Also assign some symbolic names +// to the configuration bits, for use below. +//----------------------------------------------------------------------------- + +// Receive 16bits of data from ARM here. +reg [15:0] shift_reg; + +always @(posedge spck) begin + if(~ncs & ce) + shift_reg <= {shift_reg[14:0], mosi}; +end + +reg [7:0] lf_ed_threshold; + +// adjustable frequency clock +wire [7:0] pck_cnt; +wire pck_divclk; +reg [7:0] divisor; +clk_divider div_clk(pck0, divisor, pck_cnt, pck_divclk); + +reg [11:0] conf_word; + +// We switch modes between transmitting to the 13.56 MHz tag and receiving +// from it, which means that we must make sure that we can do so without +// glitching, or else we will glitch the transmitted carrier. +always @(posedge ncs) +begin + if(ce) begin + // 4 bit command + case (shift_reg[15:12]) + `FPGA_CMD_SET_CONFREG: + begin + // 12 bit data + conf_word <= shift_reg[11:0]; + if (shift_reg[8:6] == `FPGA_MAJOR_MODE_LF_EDGE_DETECT) lf_ed_threshold <= 127; // default threshold + end + + `FPGA_CMD_SET_DIVISOR: + divisor <= shift_reg[7:0]; // 8bits + + `FPGA_CMD_SET_EDGE_DETECT_THRESHOLD: + lf_ed_threshold <= shift_reg[7:0]; // 8 bits + `FPGA_CMD_SET_PWR_PWM_LOW_COUNT:pwm_duty <= shift_reg[11:0]; + endcase + end +end + +//----------------------------------------------------------------------------- +// And then we instantiate the modules corresponding to each of the FPGA's +// major modes, and use muxes to connect the outputs of the active mode to +// the output pins. +//----------------------------------------------------------------------------- + +// ############################################################################ + +// LF reader (generic) +lo_read lr( + .pck0 (pck0), + .pck_divclk (pck_divclk), + .pck_cnt (pck_cnt), + .adc_d (adc_d), + .lf_field (conf_word[0]), + + .ssp_din (mux0_ssp_din), + .ssp_frame (mux0_ssp_frame), + .ssp_clk (mux0_ssp_clk), + .adc_clk (mux0_adc_clk), + .pwr_lo (mux0_pwr_lo), + .pwr_oe1 (mux0_pwr_oe1), + .pwr_oe2 (mux0_pwr_oe2), + .pwr_oe3 (mux0_pwr_oe3), + .pwr_oe4 (mux0_pwr_oe4), + .debug (mux0_debug) +); + +// LF edge detect (generic) +lo_edge_detect le( + .pck0 (pck0), + .pck_divclk (pck_divclk), + .adc_d (adc_d), + .cross_lo (cross_lo), + .lf_field (conf_word[0]), + .lf_ed_toggle_mode (conf_word[1]), + .lf_ed_threshold (lf_ed_threshold), + .ssp_dout (ssp_dout), + + .ssp_frame (mux1_ssp_frame), + .ssp_clk (mux1_ssp_clk), + .adc_clk (mux1_adc_clk), + .pwr_lo (mux1_pwr_lo), + .pwr_oe1 (mux1_pwr_oe1), + .pwr_oe2 (mux1_pwr_oe2), + .pwr_oe3 (mux1_pwr_oe3), + .pwr_oe4 (mux1_pwr_oe4), + .debug (mux1_debug) +); + +// LF passthrough +lo_passthru lp( + .pck_divclk (pck_divclk), + .cross_lo (cross_lo), + .ssp_dout (ssp_dout), + + .ssp_din (mux2_ssp_din), + .adc_clk (mux2_adc_clk), + .pwr_lo (mux2_pwr_lo), + .pwr_oe1 (mux2_pwr_oe1), + .pwr_oe2 (mux2_pwr_oe2), + .pwr_oe3 (mux2_pwr_oe3), + .pwr_oe4 (mux2_pwr_oe4), + .debug (mux2_debug) +); + +// LF ADC (read/write) +lo_adc la( + .pck0 (pck0), + .adc_d (adc_d), + .divisor (divisor), + .lf_field (conf_word[0]), + .ssp_dout (ssp_dout), + + .ssp_din (mux3_ssp_din), + .ssp_frame (mux3_ssp_frame), + .ssp_clk (mux3_ssp_clk), + .adc_clk (mux3_adc_clk), + .pwr_lo (mux3_pwr_lo ), + .pwr_oe1 (mux3_pwr_oe1), + .pwr_oe2 (mux3_pwr_oe2), + .pwr_oe3 (mux3_pwr_oe3), + .pwr_oe4 (mux3_pwr_oe4), + .debug (mux3_debug) +); + +assign mux6_pwr_lo = 1'b1; +// 7 -- SPARE + +// These assignments must agree with the defines in fpgaloader.h +// Major modes Low Frequency +// mux0 = LF reader (generic) +// mux1 = LF edge detect (generic) +// mux2 = LF passthrough +// mux3 = LF ADC (read/write) +// mux4 = SPARE +// mux5 = SPARE +// mux6 = SPARE +// mux7 = FPGA_MAJOR_MODE_OFF + +mux8 mux_ssp_clk ( + .sel (major_mode), + .y (ssp_clk ), + .x0 (mux0_ssp_clk ), + .x1 (mux1_ssp_clk ), + .x2 (1'b0 ), + .x3 (mux3_ssp_clk ), + .x4 (1'b0 ), + .x5 (1'b0 ), + .x6 (1'b0 ), + .x7 (1'b0 ) +); + +mux8 mux_ssp_din ( + .sel (major_mode), + .y (ssp_din ), + .x0 (mux0_ssp_din ), + .x1 (1'b0 ), + .x2 (mux2_ssp_din ), + .x3 (mux3_ssp_din ), + .x4 (1'b0 ), + .x5 (1'b0 ), + .x6 (1'b0 ), + .x7 (1'b0 ) +); + +mux8 mux_ssp_frame ( + .sel (major_mode), + .y (ssp_frame ), + .x0 (mux0_ssp_frame ), + .x1 (mux1_ssp_frame ), + .x2 (1'b0 ), + .x3 (mux3_ssp_frame ), + .x4 (1'b0 ), + .x5 (1'b0 ), + .x6 (1'b0 ), + .x7 (1'b0 ) +); + +mux8 mux_pwr_oe1 ( + .sel (major_mode), + .y (pwr_oe1 ), + .x0 (mux0_pwr_oe1 ), + .x1 (mux1_pwr_oe1 ), + .x2 (mux2_pwr_oe1 ), + .x3 (mux3_pwr_oe1 ), + .x4 (1'b0 ), + .x5 (1'b0 ), + .x6 (1'b0 ), + .x7 (1'b0 ) +); + +mux8 mux_pwr_oe2 ( + .sel (major_mode), + .y (pwr_oe2 ), + .x0 (mux0_pwr_oe2 ), + .x1 (mux1_pwr_oe2 ), + .x2 (mux2_pwr_oe2 ), + .x3 (mux3_pwr_oe2 ), + .x4 (1'b0 ), + .x5 (1'b0 ), + .x6 (1'b0 ), + .x7 (1'b0 ) +); + +mux8 mux_pwr_oe3 ( + .sel (major_mode), + .y (pwr_oe3 ), + .x0 (mux0_pwr_oe3 ), + .x1 (mux1_pwr_oe3 ), + .x2 (mux2_pwr_oe3 ), + .x3 (mux3_pwr_oe3 ), + .x4 (1'b0 ), + .x5 (1'b0 ), + .x6 (1'b0 ), + .x7 (1'b0 ) +); + +mux8 mux_pwr_oe4 ( + .sel (major_mode), + .y (pwr_oe4 ), + .x0 (mux0_pwr_oe4 ), + .x1 (mux1_pwr_oe4 ), + .x2 (mux2_pwr_oe4 ), + .x3 (mux3_pwr_oe4 ), + .x4 (1'b0 ), + .x5 (1'b0 ), + .x6 (1'b0 ), + .x7 (1'b0 ) +); + +mux8 mux_pwr_lo ( + .sel (major_mode), + .y (pwr_lo ), + .x0 (mux0_pwr_lo ), + .x1 (mux1_pwr_lo ), + .x2 (mux2_pwr_lo ), + .x3 (mux3_pwr_lo ), + .x4 (1'b0 ), + .x5 (1'b0 ), + .x6 (mux6_pwr_lo ), + .x7 (1'b0 ) +); + +mux8 mux_adc_clk ( + .sel (major_mode), + .y (adc_clk ), + .x0 (mux0_adc_clk ), + .x1 (mux1_adc_clk ), + .x2 (mux2_adc_clk ), + .x3 (mux3_adc_clk ), + .x4 (1'b0 ), + .x5 (1'b0 ), + .x6 (1'b0 ), + .x7 (1'b0 ) +); + +mux8 mux_dbg ( + .sel (major_mode), + .y (dbg ), + .x0 (mux0_debug ), + .x1 (mux1_debug ), + .x2 (mux2_debug ), + .x3 (mux3_debug ), + .x4 (1'b0 ), + .x5 (1'b0 ), + .x6 (1'b0 ), + .x7 (1'b0 ) +); +endmodule diff --git a/fpga/fpga_pm5_top.v b/fpga/fpga_pm5_top.v new file mode 100644 index 000000000..a3b14e9fb --- /dev/null +++ b/fpga/fpga_pm5_top.v @@ -0,0 +1,132 @@ +module fpga_top ( + input ck_1356meg, + input ck_1356meg_b, // B is for close range wiring + input spi_sck, + input pck0, + input spi_ncs, + input [9:0] adc_d, + input cross_lo, + input spi_mosi, + input ssp_dout, + input module_sel, // 选择模块,1=HF,0=LF + + output ssp_din, + output ssp_frame, + output ssp_clk, + output adc_clk, + output spi_miso, + output pwr_lo, + output pwr_hi, + output pwr_oe1, + output pwr_oe2, + output pwr_oe4, + output dbg1, + output dbg2, + output dbgled, + + output clk_gate, //门控输出,控制切断外部13.56m + output pwr_pwm, //升压器输出,pwm控制升压电压 + output pan_en, //模拟供电使能 + output pdp_en //升压器使能 +); + + wire ce_lf = ~module_sel; //高启用,低重置 + wire ce_hf = module_sel; //高启用,低重置 + //-----------------------------------------GATE------------------------- + //门控使能信号同步到输入时钟域,避免亚稳态 + reg gate_en_sync1, gate_en_sync2; + always @(posedge ck_1356meg) begin + gate_en_sync1 <= gate_en; + gate_en_sync2 <= gate_en_sync1; + end + + assign clk_gate = ~gate_en_sync2;//这里取反是因为外部门控是低有效的 + //-----------------------------------------PWM-------------------------- + + Gowin_rPLL pwr_pwm_pll( + .clkout(pwr_pwm_pll_clko), //output clkout + .clkin(ck_1356meg) //input clkin + ); + + wire [11:0] pwm_duty_lf; // LF 模块的 PWM 占空比输出 + wire [11:0] pwm_duty_hf; // HF 模块的 PWM 占空比输出 + + wire [11:0] pwm_duty; // PWM占空比,实际上是高电平长度,和4096做比例 + reg [11:0] pwm_cnt= 12'h0; + + always @(posedge pwr_pwm_pll_clko) pwm_cnt <= pwm_cnt + 1'b1; //计数器 + assign pwr_pwm = (pwm_cnt > pwm_duty); //pwm输出 + + assign pan_en = 1'b1; + assign pdp_en = 1'b1; + + assign pwr_hi = 1'bZ; //使用外部门控时禁用PWRHI,由于是并联的,需要高阻 + assign dbgled = 1'b1; + assign dbg2 = 1'bZ; // 未使用 + + fpga_lf uut_lf ( + .pck0(pck0), + + .spck(spi_sck), + .ncs(spi_ncs), + + .adc_d(adc_d[9:2]), + .cross_lo(cross_lo), + .mosi(spi_mosi), + .ssp_dout(ssp_dout), + .ce(ce_lf), + + .ssp_din(ssp_din_lf), + .ssp_frame(ssp_frame_lf), + .ssp_clk(ssp_clk_lf), + .adc_clk(adc_clk_lf), + .miso(miso_lf), + .pwr_lo(pwr_lo_lf), + .pwr_oe1(pwr_oe1_lf), + .pwr_oe2(pwr_oe2_lf), + .pwr_oe4(pwr_oe4_lf), + .dbg(dbg_lf), + + .pwm_duty(pwm_duty_lf) + ); + + fpga_hf uut_hf ( + .ck_1356meg(ck_1356meg), + + .spck(spi_sck), + .ncs(spi_ncs), + + .adc_d(adc_d[9:2]), + .mosi(spi_mosi), + .ssp_dout(ssp_dout), + .ce(ce_hf), + + .ssp_din(ssp_din_hf), + .ssp_frame(ssp_frame_hf), + .ssp_clk(ssp_clk_hf), + .adc_clk(adc_clk_hf), + .miso(miso_hf), + .pwr_lo(pwr_lo_hf), + .pwr_hi(gate_en), + .pwr_oe1(pwr_oe1_hf), + .pwr_oe2(pwr_oe2_hf), + .pwr_oe4(pwr_oe4_hf), + .dbg(dbg_hf), + + .pwm_duty(pwm_duty_hf) + ); + + assign ssp_din = ~module_sel ? ssp_din_lf : ssp_din_hf; + assign ssp_frame = ~module_sel ? ssp_frame_lf : ssp_frame_hf; + assign ssp_clk = ~module_sel ? ssp_clk_lf : ssp_clk_hf; + assign adc_clk = ~module_sel ? adc_clk_lf : adc_clk_hf; + assign spi_miso = ~module_sel ? miso_lf : miso_hf; + assign pwr_lo = ~module_sel ? pwr_lo_lf : pwr_lo_hf; + //assign pwr_hi = ~module_sel ? pwr_hi_lf : pwr_hi_hf; + assign pwr_oe1 = ~module_sel ? pwr_oe1_lf : pwr_oe1_hf; + assign pwr_oe2 = ~module_sel ? pwr_oe2_lf : pwr_oe2_hf; + assign pwr_oe4 = ~module_sel ? pwr_oe4_lf : pwr_oe4_hf; + assign dbg1 = ~module_sel ? dbg_lf : dbg_hf; + assign pwm_duty = ~module_sel ? pwm_duty_lf : pwm_duty_hf; + +endmodule \ No newline at end of file diff --git a/fpga/gowin_rpll.v b/fpga/gowin_rpll.v new file mode 100644 index 000000000..00895111a --- /dev/null +++ b/fpga/gowin_rpll.v @@ -0,0 +1,64 @@ +//Copyright (C)2014-2023 Gowin Semiconductor Corporation. +//All rights reserved. +//File Title: IP file +//Tool Version: V1.9.9 (64-bit) +//Part Number: GW1N-UV4QN48C6/I5 +//Device: GW1N-4 +//Device Version: D +//Created Time: Mon Jul 6 18:07:08 2026 + +module Gowin_rPLL (clkout, clkin); + +output clkout; +input clkin; + +wire lock_o; +wire clkoutp_o; +wire clkoutd_o; +wire clkoutd3_o; +wire gw_gnd; + +assign gw_gnd = 1'b0; + +rPLL rpll_inst ( + .CLKOUT(clkout), + .LOCK(lock_o), + .CLKOUTP(clkoutp_o), + .CLKOUTD(clkoutd_o), + .CLKOUTD3(clkoutd3_o), + .RESET(gw_gnd), + .RESET_P(gw_gnd), + .CLKIN(clkin), + .CLKFB(gw_gnd), + .FBDSEL({gw_gnd,gw_gnd,gw_gnd,gw_gnd,gw_gnd,gw_gnd}), + .IDSEL({gw_gnd,gw_gnd,gw_gnd,gw_gnd,gw_gnd,gw_gnd}), + .ODSEL({gw_gnd,gw_gnd,gw_gnd,gw_gnd,gw_gnd,gw_gnd}), + .PSDA({gw_gnd,gw_gnd,gw_gnd,gw_gnd}), + .DUTYDA({gw_gnd,gw_gnd,gw_gnd,gw_gnd}), + .FDLY({gw_gnd,gw_gnd,gw_gnd,gw_gnd}) +); + +defparam rpll_inst.FCLKIN = "13.56"; +defparam rpll_inst.DYN_IDIV_SEL = "false"; +defparam rpll_inst.IDIV_SEL = 1; +defparam rpll_inst.DYN_FBDIV_SEL = "false"; +defparam rpll_inst.FBDIV_SEL = 58; +defparam rpll_inst.DYN_ODIV_SEL = "false"; +defparam rpll_inst.ODIV_SEL = 2; +defparam rpll_inst.PSDA_SEL = "0000"; +defparam rpll_inst.DYN_DA_EN = "false"; +defparam rpll_inst.DUTYDA_SEL = "1000"; +defparam rpll_inst.CLKOUT_FT_DIR = 1'b1; +defparam rpll_inst.CLKOUTP_FT_DIR = 1'b1; +defparam rpll_inst.CLKOUT_DLY_STEP = 0; +defparam rpll_inst.CLKOUTP_DLY_STEP = 0; +defparam rpll_inst.CLKFB_SEL = "internal"; +defparam rpll_inst.CLKOUT_BYPASS = "false"; +defparam rpll_inst.CLKOUTP_BYPASS = "false"; +defparam rpll_inst.CLKOUTD_BYPASS = "false"; +defparam rpll_inst.DYN_SDIV_SEL = 2; +defparam rpll_inst.CLKOUTD_SRC = "CLKOUT"; +defparam rpll_inst.CLKOUTD3_SRC = "CLKOUT"; +defparam rpll_inst.DEVICE = "GW1N-4D"; + +endmodule //Gowin_rPLL