zangman / de10-nano

Absolute beginner's guide to the de10-nano
Apache License 2.0
182 stars 43 forks source link

How to create a 'simple_counter'? #31

Closed jorgeasmz closed 2 months ago

jorgeasmz commented 4 months ago

Hello,

I've been following the guides in this repo because I was assigned a project consisting in programming a DE10-Nano as a photon counter. The way it should work would be, connecting a photon detector to the board and then having it count the TTL pulses fired by the detector. I tried several things before finding this repo, but I think I'm closer by using a SoC project approach. So far, the guides have been really helpful. I completed the simple_adder project without much trouble, and since it has some things I would need for my project, I'm using it as a base project. For now, I'm interested in doing just a simple_counter project, that counts intervals of time. It has two parameters, a total_time_interval, and a time_interval. I want to return the counting when the time_interval is completed. I used this System Verilog code:

module simple_counter(
    input logic clk,
    input logic reset,
    input logic [63:0] time_interval,
    input logic [63:0] total_time_interval,
    output logic [63:0] count
);

    logic [31:0] timer;
    logic interval_done;

    always_ff @(posedge clk or posedge reset) begin
        if (reset) begin
            timer <= 32'h0;
            count <= 32'h0;
            interval_done <= 1'b0;
        end else begin
            if (timer == time_interval) begin
                timer <= 32'h0;
                interval_done <= 1'b1;
                if (count < total_time_interval) begin
                    count <= count + 1'b1;
                end
            end else begin
                timer <= timer + 1'b1;
                interval_done <= 1'b0;
            end
        end
    end

endmodule

I changed the names of the pio IPs in the Platform Designer, and I instantiated the module in the Top Level Entity. I compiled the project succesfully, and I tried modifying the C code provided in the guide:

if (bridge_map == MAP_FAILED) {
    perror("mmap failed.");
    close(fd);
    return -3;
  }

  time_interval_map = bridge_map + TIME_INTERVAL;
  total_time_interval_map = bridge_map + TOTAL_TIME_INTERVAL;

  *((uint64_t *)time_interval_map) = time_interval;
  *((uint64_t *)total_time_interval_map) = total_time_interval;

  while (1) {
    interval_counting_map = bridge_map + INTERVAL_COUNTING;
    interval_counting = *((uint64_t *)interval_counting_map);

    printf("%" PRIu64 "\n", interval_counting);

    // Add a small delay between readings
    usleep(100000); // 100ms delay
  }

  result = munmap(bridge_map, BRIDGE_SPAN);

  if (result < 0) {
    perror("Couldnt unmap bridge.");
    close(fd);
    return -4;
  }

  close(fd);
  return 0;

As you can see, all I'm doing is repeteadly obtain the interval_counting value, but when I compile it, all I get is 0s.

I don't have experience with this. What could be going on? How would be the correct way to have a counter project.