test FPGA
authorThomas Pietrzak <thomas.pietrzak@gmail.com>
Mon, 5 Nov 2018 14:37:53 +0000 (15:37 +0100)
committerThomas Pietrzak <thomas.pietrzak@gmail.com>
Mon, 5 Nov 2018 14:37:53 +0000 (15:37 +0100)
fpga/forcefader.v [new file with mode: 0644]

diff --git a/fpga/forcefader.v b/fpga/forcefader.v
new file mode 100644 (file)
index 0000000..ef55da9
--- /dev/null
@@ -0,0 +1,65 @@
+module spring (
+  position,
+  endposition,
+  stiffness,
+  force
+  );
+input [11:0] position;
+input [11:0] endposition;
+input [7:0] stiffness;
+output signed [16:0] force;
+
+assign force = stiffness * (endposition - position);
+
+endmodule
+
+
+module forcefader (
+  clk, //12MHZ
+  input_pos,
+  input_touch,
+  output_force,
+  output_direction,
+  scl,
+  miso,
+  mosi,
+  cs
+);
+input clk;
+input [11:0] input_pos;
+input input_touch;
+output reg output_force;
+output output_direction;
+input scl;
+output miso;
+input mosi;
+input cs;
+
+reg [10:0] slowclkcounter;
+always @(posedge clk) slowclkcounter <= slowclkcounter + 1;
+
+reg signed [16:0] force;
+
+wire [15:0]absforce;
+assign absforce = force < 0 ? - force : force;
+
+assign output_direction = force[16];
+
+reg [15:0] forcecnt;
+
+wire slowclk; //5.8kHz
+assign slowclk = slowclkcounter[10];
+
+always @(posedge slowclk) begin
+  forcecnt <= forcecnt + 1;
+  if ((forcecnt == 0) ^ (forcecnt == absforce)) output_force <= ~output_force;
+end
+
+spring spring_instance(
+  .position(input_pos),
+  .endposition({1'b0, 10'b1}),
+  .stiffness(8'b00010000),
+  .force(force)
+  );
+
+endmodule