Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
WangXuan95 committed Mar 31, 2022
1 parent 5677747 commit 7c5ef65
Show file tree
Hide file tree
Showing 30 changed files with 501 additions and 287 deletions.
5 changes: 1 addition & 4 deletions FPGA/foc.qsf
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ set_global_assignment -name DEVICE EP4CE15E22I7
set_global_assignment -name TOP_LEVEL_ENTITY top
set_global_assignment -name ORIGINAL_QUARTUS_VERSION 13.1
set_global_assignment -name PROJECT_CREATION_TIME_DATE "17:37:36 FEBRUARY 05, 2021"
set_global_assignment -name LAST_QUARTUS_VERSION 13.1
set_global_assignment -name LAST_QUARTUS_VERSION "18.1.0 Standard Edition"
set_global_assignment -name PROJECT_OUTPUT_DIRECTORY output_files
set_global_assignment -name MIN_CORE_JUNCTION_TEMP "-40"
set_global_assignment -name MAX_CORE_JUNCTION_TEMP 100
Expand All @@ -74,10 +74,7 @@ set_global_assignment -name PARTITION_FITTER_PRESERVATION_LEVEL PLACEMENT_AND_RO
set_global_assignment -name PARTITION_COLOR 16764057 -section_id Top
set_global_assignment -name STRATIX_DEVICE_IO_STANDARD "2.5 V"
set_global_assignment -name SYSTEMVERILOG_FILE ../RTL/top.sv
set_global_assignment -name VERILOG_FILE ../RTL/pll.v
set_global_assignment -name SYSTEMVERILOG_FILE ../RTL/uart/uart_monitor.sv
set_global_assignment -name SYSTEMVERILOG_FILE ../RTL/uart/uart_tx.sv
set_global_assignment -name SYSTEMVERILOG_FILE ../RTL/uart/itoa.sv
set_global_assignment -name SYSTEMVERILOG_FILE ../RTL/sensors/as5600_read.sv
set_global_assignment -name SYSTEMVERILOG_FILE ../RTL/sensors/i2c_register_read.sv
set_global_assignment -name SYSTEMVERILOG_FILE ../RTL/sensors/adc_ad7928.sv
Expand Down
232 changes: 165 additions & 67 deletions README.md

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion RTL/foc/cartesian2polar.sv
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
`timescale 1 ns/1 ns

// 模块: cartesian2polar
// Type : synthesizable
// Standard: SystemVerilog 2005 (IEEE1800-2005)
// 功能: 把直角坐标系 (x,y) 转换为极坐标系

module cartesian2polar #(
parameter ATTENUAION = 0
Expand Down
6 changes: 5 additions & 1 deletion RTL/foc/clark_tr.sv
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
`timescale 1 ns/1 ns

// clark_tr
// Type : synthesizable
// Standard: SystemVerilog 2005 (IEEE1800-2005)
// 功能: clark 变换

module clark_tr(
input wire rstn,
Expand Down
6 changes: 4 additions & 2 deletions RTL/foc/foc_top.sv
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
`timescale 1 ns/1 ns

// 模块: foc_top
// Type : synthesizable, IP's top
// Standard: SystemVerilog 2005 (IEEE1800-2005)
// 功能:FOC 算法(仅包含电流环) + SVPWM
// 参数:详见下方注释
// 输入输出:详见下方注释

module foc_top #(
// ----------------------------------------------- 模块参数 ---------------------------------------------------------------------------------------------------------------------------------------------------
parameter INIT_CYCLES = 16777216, // 决定了初始化步骤粘多少个时钟(clk)周期,取值范围为1~4294967294。该值不能太短,因为要留足够的时间让转子回归电角度=0。例如若时钟(clk)频率为 36.864MHz,INIT_CYCLES=16777216,则初始化时间为 16777216/36864000=0.45 秒
parameter INIT_CYCLES = 16777216, // 决定了初始化步骤占多少个时钟(clk)周期,取值范围为1~4294967294。该值不能太短,因为要留足够的时间让转子回归电角度=0。例如若时钟(clk)频率为 36.864MHz,INIT_CYCLES=16777216,则初始化时间为 16777216/36864000=0.45 秒
parameter logic ANGLE_INV = 1'b0, // 若角度传感器没装反(A->B->C->A 的旋转方向与 φ 增大的方向相同),则该参数应设为 0。若角度传感器装反了(A->B->C->A 的旋转方向与 φ 增大的方向相反),则该参数应设为 1。
parameter logic [ 7:0] POLE_PAIR = 8'd7, // 电机极对数 (简记为N),取值范围1~255,根据电机型号决定。(电角度ψ = 极对数N * 机械角度φ)
parameter logic [ 8:0] MAX_AMP = 9'd384, // SVPWM 的最大振幅,取值范围为1~511,该值越小,电机能达到的最大力矩越小;但考虑到使用3相下桥臂电阻采样法来采样电流,该值也不能太大,以保证3个下桥臂有足够的持续导通时间来供ADC进行采样。
Expand Down
6 changes: 5 additions & 1 deletion RTL/foc/hold_detect.sv
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
`timescale 1 ns/1 ns

// 模块: hold_detect
// Type : synthesizable
// Standard: SystemVerilog 2005 (IEEE1800-2005)
// 功能: 检测 in 从高电平变为低电平并保持 SAMPLE_DELAY 个时钟周期,在 sn_adc 信号上产生一个时钟周期的高电平。

module hold_detect #(
parameter [15:0] SAMPLE_DELAY = 16'd100
Expand Down
6 changes: 5 additions & 1 deletion RTL/foc/park_tr.sv
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
`timescale 1 ns/1 ns

// 模块: park_tr
// Type : synthesizable
// Standard: SystemVerilog 2005 (IEEE1800-2005)
// 功能: park 变换器

module park_tr(
input wire rstn,
Expand Down
6 changes: 5 additions & 1 deletion RTL/foc/pi_controller.sv
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
`timescale 1 ns/1 ns

// 模块: pi_controller
// Type : synthesizable
// Standard: SystemVerilog 2005 (IEEE1800-2005)
// 功能: PI 控制器

module pi_controller #(
parameter logic [23:0] Kp = 24'd32768,
Expand Down
8 changes: 7 additions & 1 deletion RTL/foc/sincos.sv
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
`timescale 1 ns/1 ns

// 模块:sincos
// Type : synthesizable
// Standard: SystemVerilog 2005 (IEEE1800-2005)
// 功能: 计算 sin 和 cos
// 在 i_theta 上给出角度(0~π被映射为0~4095),
// 在 o_sin 和 o_cos 上产生 sinθ 和 cosθ (-1~+1 被映射为 -16384~+16384)

module sincos(
input wire rstn,
Expand Down
4 changes: 3 additions & 1 deletion RTL/foc/svpwm.sv
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
`timescale 1 ns/1 ns

// 模块:svpwm
// Type : synthesizable
// Standard: SystemVerilog 2005 (IEEE1800-2005)
// 功能:7 段式 SVPWM 生成器(调制器)
// 输入:定子极坐标系下的电压矢量 Vsρ, Vsθ
// 输出:PWM使能信号 pwm_en
// 3相PWM信号 pwm_a, pwm_b, pwm_c
// 说明:该模块产生的 PWM 的频率是 clk 频率 / 2048。例如 clk 为 36.864MHz ,则 PWM 的频率为 36.864MHz / 2048 = 18kHz

module svpwm (
input wire rstn,
input wire clk,
Expand Down
120 changes: 0 additions & 120 deletions RTL/pll.v

This file was deleted.

4 changes: 3 additions & 1 deletion RTL/sensors/adc_ad7928.sv
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
`timescale 1 ns/1 ns

// 模块: adc_ad7928
// Type : synthesizable
// Standard: SystemVerilog 2005 (IEEE1800-2005)
// 功能:通过 SPI 接口从 ADC7928 (ADC芯片) 中读出 ADC 值。
// 参数:详见下方注释,该模块可以使用参数完全自由地配置单次转换要用多少个通道以及用哪些通道
// 输入输出:详见下方注释

module adc_ad7928 #(
parameter [2:0] CH_CNT = 3'd7, // 单次 ADC 转换使用的通道数为 CH_CNT+1,例如若 CH_CNT=0,则只使用 CH0 。若 CH_CNT=2,则使用 CH0,CH1,CH2。 若 CH_CNT=7,则使用 CH0,CH1,CH2,CH3,CH4,CH5,CH6,CH7。用的通道越多,ADC转换时延越长(即从 sn_adc 到 en_adc 之间的时间差越长)
parameter [2:0] CH0 = 3'd0, // 指示了 CH0 对应 AD7928 的哪个通道
Expand Down
4 changes: 3 additions & 1 deletion RTL/sensors/as5600_read.sv
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
`timescale 1 ns/1 ns

// 模块: as5600_read
// Type : synthesizable
// Standard: SystemVerilog 2005 (IEEE1800-2005)
// 功能:通过 I2C 接口从 AS5600 磁编码器中读出转子机械角度 φ
// 参数:详见下方注释
// 输入输出:详见下方注释

module as5600_read #(
parameter [15:0] CLK_DIV = 16'd10 // I2C SCL 时钟信号分频系数,SCL 时钟频率 = clk频率 / (4*CLK_DIV) ,例如若 clk 为 40MHz,CLK_DIV=10,则 SCL 频率为 40/(4*10) = 1MHz。注,AS5600 芯片要求 SCL 频率不超过 1MHz
)(
Expand Down
6 changes: 5 additions & 1 deletion RTL/sensors/i2c_register_read.sv
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
`timescale 1 ns/1 ns

// 模块: i2c_register_read
// Type : synthesizable
// Standard: SystemVerilog 2005 (IEEE1800-2005)
// 功能 : I2C 读控制器

module i2c_register_read #(
parameter [15:0] CLK_DIV = 16'd16,
Expand Down
12 changes: 6 additions & 6 deletions RTL/top.sv
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
`timescale 1 ns/1 ns

// 模块:top
// Type : synthesizable, FPGA's top
// Standard: SystemVerilog 2005 (IEEE1800-2005)
// 功能:FOC 使用示例,是FPGA工程的顶层模块,控制电机的切向力矩一会顺时针一会逆时针,同时可以通过 UART 监测电流环控制的跟随曲线
// 参数:无
// 输入输出:详见下方注释

module top(
input wire clk_50m, // 50MHz 时钟
// ------- 3相 PWM 信号,(包含使能信号) -----------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -44,11 +46,9 @@ reg signed [15:0] iq_aim; // 转子 q 轴(交轴)的目标电流值,

// PLL,用 50MHz 时钟产生 36.864 MHz 时钟
// 注:该模块仅适用于 Altera Cyclone IV FPGA ,对于其他厂家或系列的FPGA,请使用各自相同效果的IP核/原语(例如Xilinx的clock wizard)代替该模块。
pll pll_i (
.inclk0 ( clk_50m ), // input : clk_50m
.c0 ( clk ), // output: clk
.locked ( rstn ) // output: rstn
);
wire [3:0] subwire0;
altpll altpll_i ( .inclk ( {1'b0, clk_50m} ), .clk ( {subwire0, clk} ), .locked ( rstn ), .activeclock (), .areset (1'b0), .clkbad (), .clkena ({6{1'b1}}), .clkloss (), .clkswitch (1'b0), .configupdate (1'b0), .enable0 (), .enable1 (), .extclk (), .extclkena ({4{1'b1}}), .fbin (1'b1), .fbmimicbidir (), .fbout (), .fref (), .icdrclk (), .pfdena (1'b1), .phasecounterselect ({4{1'b1}}), .phasedone (), .phasestep (1'b1), .phaseupdown (1'b1), .pllena (1'b1), .scanaclr (1'b0), .scanclk (1'b0), .scanclkena (1'b1), .scandata (1'b0), .scandataout (), .scandone (), .scanread (1'b0), .scanwrite (1'b0), .sclkout0 (), .sclkout1 (), .vcooverrange (), .vcounderrange ());
defparam altpll_i.bandwidth_type = "AUTO", altpll_i.clk0_divide_by = 99, altpll_i.clk0_duty_cycle = 50, altpll_i.clk0_multiply_by = 73, altpll_i.clk0_phase_shift = "0", altpll_i.compensate_clock = "CLK0", altpll_i.inclk0_input_frequency = 20000, altpll_i.intended_device_family = "Cyclone IV E", altpll_i.lpm_hint = "CBX_MODULE_PREFIX=pll", altpll_i.lpm_type = "altpll", altpll_i.operation_mode = "NORMAL", altpll_i.pll_type = "AUTO", altpll_i.port_activeclock = "PORT_UNUSED", altpll_i.port_areset = "PORT_UNUSED", altpll_i.port_clkbad0 = "PORT_UNUSED", altpll_i.port_clkbad1 = "PORT_UNUSED", altpll_i.port_clkloss = "PORT_UNUSED", altpll_i.port_clkswitch = "PORT_UNUSED", altpll_i.port_configupdate = "PORT_UNUSED", altpll_i.port_fbin = "PORT_UNUSED", altpll_i.port_inclk0 = "PORT_USED", altpll_i.port_inclk1 = "PORT_UNUSED", altpll_i.port_locked = "PORT_USED", altpll_i.port_pfdena = "PORT_UNUSED", altpll_i.port_phasecounterselect = "PORT_UNUSED", altpll_i.port_phasedone = "PORT_UNUSED", altpll_i.port_phasestep = "PORT_UNUSED", altpll_i.port_phaseupdown = "PORT_UNUSED", altpll_i.port_pllena = "PORT_UNUSED", altpll_i.port_scanaclr = "PORT_UNUSED", altpll_i.port_scanclk = "PORT_UNUSED", altpll_i.port_scanclkena = "PORT_UNUSED", altpll_i.port_scandata = "PORT_UNUSED", altpll_i.port_scandataout = "PORT_UNUSED", altpll_i.port_scandone = "PORT_UNUSED", altpll_i.port_scanread = "PORT_UNUSED", altpll_i.port_scanwrite = "PORT_UNUSED", altpll_i.port_clk0 = "PORT_USED", altpll_i.port_clk1 = "PORT_UNUSED", altpll_i.port_clk2 = "PORT_UNUSED", altpll_i.port_clk3 = "PORT_UNUSED", altpll_i.port_clk4 = "PORT_UNUSED", altpll_i.port_clk5 = "PORT_UNUSED", altpll_i.port_clkena0 = "PORT_UNUSED", altpll_i.port_clkena1 = "PORT_UNUSED", altpll_i.port_clkena2 = "PORT_UNUSED", altpll_i.port_clkena3 = "PORT_UNUSED", altpll_i.port_clkena4 = "PORT_UNUSED", altpll_i.port_clkena5 = "PORT_UNUSED", altpll_i.port_extclk0 = "PORT_UNUSED", altpll_i.port_extclk1 = "PORT_UNUSED", altpll_i.port_extclk2 = "PORT_UNUSED", altpll_i.port_extclk3 = "PORT_UNUSED", altpll_i.self_reset_on_loss_lock = "OFF", altpll_i.width_clock = 5;



Expand Down
52 changes: 0 additions & 52 deletions RTL/uart/itoa.sv

This file was deleted.

Loading

0 comments on commit 7c5ef65

Please sign in to comment.