Monday, July 26, 2021

Using GPIO Pins on x300

Hey everyone,

I'm using an Ettus Research x300 with GNURadio and I'm going to need to use the front panel GPIO pins to automatically switch an external amplifier into the RF chain when transmitting. I found the following code on Ettus Research's github that demonstrates how to use the GPIO pins in various ATR modes: https://github.com/EttusResearch/uhd/blob/master/host/examples/gpio.cpp

I was just a little confused on how I would implement this code in a gnuradio flowgraph to configure the gpio pins. I looked through the mailing list and found a couple of relevant threads but it seems that the issue was resolved off-thread so the solution isn't posted. I don't have much experience with OOT modules and from the tutorial on the wiki, I wasn't sure how I would use the USRP UHD object that's created in the USRP source/sink block's code in a separate block's code. If someone could just point me in the right direction or provide some other advice that would be greatly appreciated.

Thanks,
Conner

2 comments:

  1. hi friend,
    I had the same problem as you, and after a long-time searching, I may find a way to use GPIO in gnuradio.
    Just as Marcus said, you can add a "Python Snippet" block in your gnuradio diagram. In the coding box, I just used the following python codes.

    Here are some useful websites:
    [gnuradio.uhd - python functions] https://www.gnuradio.org/doc/sphinx-3.7.2/uhd.html
    [E3x0/X3x0 GPIO API - C++ functions] https://files.ettus.com/manual/page_gpio_api.html

    ##################################################Self defined function##################################################
    # Description: Set X300 AUX I/O (GPIO)
    # Set GPIO6 as manually pulled-up input.
    # Set GPIO7 to be automatically controlled by ATR as an output.
    # Set GPIO7 to be high when transmitting only, and low in all other cases.

    # set the masks, define the pin numbers
    MAN_GPIO_MASK = (1 << 6)
    AMP_GPIO_MASK = (1 << 7)
    ATR_MASKS = (AMP_GPIO_MASK | MAN_GPIO_MASK)

    # set the values for ATR control: 1 for ATR, 0 for manual
    ATR_CONTROL = (AMP_GPIO_MASK & ~MAN_GPIO_MASK)

    # set the GPIO directions: 1 for output, 0 for input
    GPIO_DDR = (AMP_GPIO_MASK | MAN_GPIO_MASK)

    # print the GPIO bank information
    temp_gpio_bank=self.uhd_usrp_sink_0.get_gpio_banks(0)
    print('GPIO Banks:')
    print(temp_gpio_bank)


    # set basic ATR setup
    self.uhd_usrp_sink_0.set_gpio_attr("FP0", "CTRL", ATR_CONTROL, ATR_MASKS)
    self.uhd_usrp_sink_0.set_gpio_attr("FP0", "DDR", GPIO_DDR, ATR_MASKS)

    # manually set GPIO6 high
    self.uhd_usrp_sink_0.set_gpio_attr("FP0", "OUT", (1<<6), MAN_GPIO_MASK)

    # set GPIO7 as described above
    self.uhd_usrp_sink_0.set_gpio_attr("FP0", "ATR_0X", 0, AMP_GPIO_MASK)
    self.uhd_usrp_sink_0.set_gpio_attr("FP0", "ATR_RX", 0, AMP_GPIO_MASK)
    self.uhd_usrp_sink_0.set_gpio_attr("FP0", "ATR_TX", (1<<7), AMP_GPIO_MASK)
    self.uhd_usrp_sink_0.set_gpio_attr("FP0", "ATR_XX", 0, AMP_GPIO_MASK)

    # See if the configuration is successful
    temp_gpio_ctrl = self.uhd_usrp_sink_0.get_gpio_attr("FP0","CTRL")
    print('Attribute: FP0, CTRL')
    print(temp_gpio_ctrl)

    temp_gpio_ddr = self.uhd_usrp_sink_0.get_gpio_attr("FP0","DDR")
    print('Attribute: FP0, DDR')
    print(temp_gpio_ddr)

    temp_gpio_out = self.uhd_usrp_sink_0.get_gpio_attr("FP0","OUT")
    print('Attribute: FP0, OUT')
    print(temp_gpio_out)
    ##################################################Self defined function##################################################

    ReplyDelete