GPIO: Set output pin by sample count

Discussions related to embedded firmware, driver, and user mode application software development

Moderator: robert.ghilduta

Post Reply
hankivstmb
Posts: 4
Joined: Thu Mar 16, 2017 10:16 am

GPIO: Set output pin by sample count

Post by hankivstmb » Mon May 28, 2018 5:08 pm

First, I'm a total FPGA newbie.

So far, I have successfully compiled the blink_leds tutorial [https://github.com/Nuand/bladeRF/wiki/F ... -some-leds].

Next, I would like to modify the following tutorial code to count the number of RX samples instead of clock cycles:

Code: Select all

blink_leds : process (c4_clock)
    variable counter : natural range 0 to 57_600_000 := 57_600_000;
begin
    if (rising_edge(c4_clock)) then
        counter := counter - 1;
        if (counter = 0) then
            counter := 57_600_000;
        elsif (counter < 19_200_000) then
            led(2) <= '0';
            led(1) <= '1';
            led(3) <= '1';
        elsif (counter < 38_400_000) then
            led(2) <= '1';
            led(1) <= '0';
            led(3) <= '1';
        else
            led(2) <= '1';
            led(1) <= '1';
            led(3) <= '0';
        end if;
    end if;
end process;
Can I replace 'c4_clock' with 'rx_sample_fifo.wreq' and adjust my count by a factor of 32 to count RX samples?

I think I would need to change rising_edge() to something else.

If I get that working, my final goal is to toggle an output pin on the XB-100 GPIO expansion board depending on the RX sample count.

Can I accomplish my goal only modifying the code in the bladerf-hosted.vhd source file?

Thanks!

bglod
Posts: 116
Joined: Thu Jun 18, 2015 6:10 pm

Re: GPIO: Set output pin by sample count

Post by bglod » Tue May 29, 2018 1:25 pm

Yes, you can do this. I would suggest rising_edge( rx_sample_fifo.wclock ), then count the number of cycles that rx_sample_fifo.wreq is '1' to get your sample count. Also consider how you want to clear the counter. One suggestion is to do this when rx_enable is '0'.
Electrical Engineer
Nuand, LLC.

hankivstmb
Posts: 4
Joined: Thu Mar 16, 2017 10:16 am

Re: GPIO: Set output pin by sample count

Post by hankivstmb » Thu May 31, 2018 8:22 am

Edited Code Below:

Code: Select all

blink_leds : process (rx_enable, rx_sample_fifo.wclock)
    variable counter : natural range 0 to 57_600_000 := 57_600_000;
begin
    if (rising_edge(rx_sample_fifo.wclock)) then
        counter := counter - 1;
        if (counter = 0) then
            counter := 57_600_000;
        elsif (counter < 19_200_000) then
            led(2) <= '0';
            led(1) <= '1';
            led(3) <= '1';
        elsif (counter < 38_400_000) then
            led(2) <= '1';
            led(1) <= '0';
            led(3) <= '1';
        else
            led(2) <= '1';
            led(1) <= '1';
            led(3) <= '0';
        end if;
    end if;
end process;
I still need to handle the logic 'when rx_enable is 0'

Is rx_sample_fifo.wclock equivalent to lms_rx_clock_out in the diagram shown in the tutorial [https://github.com/Nuand/bladeRF/wiki/F ... chitecture]?

My sample rate is going to be 20M. So if I'm reading the diagram correctly, the lms_rx_clock_out will be 40M (or 2x sample rate).

So, I think to change the LED's every second I would need to edit counter to range 0 to 120_000_000 and the elsif statements to 40_000_000 and 80_000_000.

Does that seem correct?

bglod
Posts: 116
Joined: Thu Jun 18, 2015 6:10 pm

Re: GPIO: Set output pin by sample count

Post by bglod » Fri Jun 01, 2018 7:53 am

hankivstmb wrote:Is rx_sample_fifo.wclock equivalent to lms_rx_clock_out in the diagram shown in the tutorial [https://github.com/Nuand/bladeRF/wiki/F ... chitecture]?
Yes.
hankivstmb wrote:My sample rate is going to be 20M. So if I'm reading the diagram correctly, the lms_rx_clock_out will be 40M (or 2x sample rate).
Yes.
hankivstmb wrote:So, I think to change the LED's every second I would need to edit counter to range 0 to 120_000_000 and the elsif statements to 40_000_000 and 80_000_000.
That's correct! Just keep in mind that if your sample rate changes, then your LEDs will speed up or slow down accordingly.
Electrical Engineer
Nuand, LLC.

hankivstmb
Posts: 4
Joined: Thu Mar 16, 2017 10:16 am

Re: GPIO: Set output pin by sample count

Post by hankivstmb » Thu Jun 07, 2018 8:01 pm

Here's what I have tried so far:

Code: Select all

    blink_leds : process (rx_enable, rx_sample_fifo.wclock)
        variable counter : natural range 0 to 80_000_000 := 80_000_000;
    begin
        if (rising_edge(rx_sample_fifo.wclock)) then 
            counter := counter -1;
            if (counter = 0) then 
                counter := 80_000_000;
            elsif (counter < 40_000_000) then 
                if (rx_enable = '1') then 
                    exp_gpio(3) <= '1'; 
                    exp_gpio(4) <= '0'; 
                    led(2) <= '1'; 
                    led(1) <= '0'; 
                    led(3) <= '1'; 
                else 
                    exp_gpio(3) <= '0'; 
                    exp_gpio(4) <= '0'; 
                    led(2) <= '0'; 
                    led(1) <= '0'; 
                    led(3) <= '0'; 
                end if;
            else 
                if (rx_enable = '1') then 
                    exp_gpio(3) <= '1'; 
                    exp_gpio(4) <= '0'; 
                    led(2) <= '0'; 
                    led(1) <= '0'; 
                    led(3) <= '1'; 
                else 
                    exp_gpio(3) <= '0'; 
                    exp_gpio(4) <= '0'; 
                    led(2) <= '0'; 
                    led(1) <= '0'; 
                    led(3) <= '0'; 
                end if;
            end if;
        end if;
    end process;
I get the following error when I try to compile:

Code: Select all

Error (10028): Can't resolve multiple constant drivers for net "exp_gpio[4]" at bladerf-hosted.vhd(1171) File: /home/berglund/repos/bladeRF/hdl/fpga/platforms/bladerf/vhdl/bladerf-hosted.vhd Line: 1171
Error (10029): Constant driver at bladerf-hosted.vhd(1244) File: /home/berglund/repos/bladeRF/hdl/fpga/platforms/bladerf/vhdl/bladerf-hosted.vhd Line: 1244
Error (10028): Can't resolve multiple constant drivers for net "exp_gpio[3]" at bladerf-hosted.vhd(1171) File: /home/berglund/repos/bladeRF/hdl/fpga/platforms/bladerf/vhdl/bladerf-hosted.vhd Line: 1171
Error (12153): Can't elaborate top-level user hierarchy
I'm not sure what "Can't resolve multiple constant drivers means. But I have a feeling it has something to do with tri-state drivers. Which I don't know anything about. My goal is to control the output of the GPIO pin, i don't need to handle any input.

Am I on the right track?

Thanks

User avatar
rtucker
Posts: 44
Joined: Sun Jan 25, 2015 10:38 am

Re: GPIO: Set output pin by sample count

Post by rtucker » Fri Jun 08, 2018 2:27 pm

So in this case, it's likely conflicting with the xb_gpio_direction process up around line 1171:

Code: Select all

    xb_gpio_direction : process(all)
    begin
        for i in 0 to 31 loop
            if (xb_gpio_dir(i) = '1') then
                nios_xb_gpio_in(i) <= nios_xb_gpio_out(i);
                if (xb_mode = "10" and i + 1 = 2) then
                    exp_gpio(i+1) <= nios_ss_n(1);
                elsif (i + 1 /= 1) then
                    exp_gpio(i+1) <= nios_xb_gpio_out(i);
                end if;
            else
                if (i + 1 = 1) then
                    nios_xb_gpio_in(i) <= exp_clock_in;
                else
                    nios_xb_gpio_in(i) <= exp_gpio(i + 1);
                    exp_gpio(i + 1) <= 'Z';
                end if;
            end if;
        end loop ;
    end process ;
 
Skipping the iterations of the loop that affect exp_gpio(3) and exp_gpio(4) might take care of it... just off the top of my head:

Code: Select all

    xb_gpio_direction : process(all)
    begin
        for i in 0 to 31 loop
            if (i+1 /= 3 and i+1 /= 4) then
                if (xb_gpio_dir(i) = '1') then
                    -- (omitted for brevity)
                end if;
            end if;
        end loop ;
    end process ;
There are probably better ways to do this, but the same idea applies :)
Rey Tucker (she/her)
Systems Engineer, Nuand LLC
Rochester, NY, USA

hankivstmb
Posts: 4
Joined: Thu Mar 16, 2017 10:16 am

Re: GPIO: Set output pin by sample count

Post by hankivstmb » Tue Jun 19, 2018 1:46 pm

After I excluded the GPIO pins from the xb_gpio_direction process I was able compile the code and measure changes in voltage on the XB-100 GPIO board when I collected RX samples.

Thank you for your help bglod & rtucker!

bglod
Posts: 116
Joined: Thu Jun 18, 2015 6:10 pm

Re: GPIO: Set output pin by sample count

Post by bglod » Tue Jun 19, 2018 9:27 pm

You're welcome!
Electrical Engineer
Nuand, LLC.

Post Reply