<jupyter_start><jupyter_text>Using PS GPIO with PYNQ GoalThe aim of this notebook is to show how to use the Zynq PS GPIO from PYNQ. The PS GPIO are simple wires from the PS, and don't need a controller in the programmable logic. Up to 64 PS GPIO are available, and they can be used to connect simple control and data signals to IP or peripherals in the PL.  Hardware designThis example uses a bitstream that connects PS GPIO to the LEDs, buttons, and switches and can be used with the PYNQ-Z1 or PYNQ-Z2 board. ![PS GPIO Design](./images/ps_gpio_design.png "PS GPIO Design") GPIO Map: * GPIO 0 - 3: Buttons* GPIO 4 - 5: Switches* GPIO 6 - 9: LEDs  1. Download the tutorial overlayThe `ps_gpio.bit` and `ps_gpio.tcl` files can be found in the bitstreams directory local to this folder. The bitstream can be downloaded by passing the relative path to the Overlay class.  * Check the bitstream and .tcl exists in the bitstream directory<jupyter_code>!dir ./bitstream<jupyter_output><empty_output><jupyter_text>* Download the bitstream<jupyter_code>from pynq import Overlay
ps_gpio_design = Overlay("./bitstream/ps_gpio.bit")<jupyter_output><empty_output><jupyter_text>GPIO classThe GPIO class will be used to access the PS GPIO.   1. Controlling the switches and push-buttonsIn the design PS GPIO pins 0 to 3 are connected to the pushbuttons, and pins 4 to 5 are connected to the dip-switches on the PYNQ-Z1 and PYNQ-Z2 boards.<jupyter_code>from pynq import GPIO

button0 = GPIO(GPIO.get_gpio_pin(0), 'in')
button0.read()<jupyter_output><empty_output><jupyter_text>Try pressing the button BTN0 on the board and rerunning the cell above. The other buttons and switches can be read in a similar way.<jupyter_code>button1 = GPIO(GPIO.get_gpio_pin(1), 'in')
button2 = GPIO(GPIO.get_gpio_pin(2), 'in')
button3 = GPIO(GPIO.get_gpio_pin(3), 'in')

switch0 = GPIO(GPIO.get_gpio_pin(4), 'in')
switch1 = GPIO(GPIO.get_gpio_pin(5), 'in')<jupyter_output><empty_output><jupyter_text>Try pressing different buttons (BTN1, BTN2, BTN3), and moving the switches (SW0, SW1) while executing the cell below.<jupyter_code>print(f"Button0: {button0.read()}")
print(f"Button1: {button1.read()}")
print(f"Button2: {button2.read()}")
print(f"Button3: {button3.read()}")

print("")
print(f"Switch0: {switch0.read()}")
print(f"Switch1: {switch1.read()}")<jupyter_output><empty_output><jupyter_text>2. Controlling the LEDsThe LEDs can be used in a similar way, the only difference is the direction passed to the GPIO class. The LEDs are connected to PS GPIO 6 to 9 in the design we are using.<jupyter_code>led0 = GPIO(GPIO.get_gpio_pin(6), 'out')
led0.write(1)
led1 = GPIO(GPIO.get_gpio_pin(7), 'out')
led2 = GPIO(GPIO.get_gpio_pin(8), 'out')
led3 = GPIO(GPIO.get_gpio_pin(9), 'out')
from time import sleep

led1.write(1)
sleep(1)
led2.write(1)
sleep(1)
led3.write(1)<jupyter_output><empty_output><jupyter_text>* Reset the LEDs<jupyter_code>led0.write(0)
led1.write(0)
led2.write(0)
led3.write(0)<jupyter_output><empty_output><jupyter_text>3 putting it togetherRun a loop to set the LEDs to the value of the pushbuttons. Before executing the next cell, make sure Switch 0 (SW0) is "on". While the loop is running, press a push-button and notice the corresponding LED turns on. To exist the loop, change Switch 0 to off.<jupyter_code>while(switch0.read() is 1):
    led0.write(button0.read())
    led1.write(button1.read())
    led2.write(button2.read())
    led3.write(button3.read())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10709158771174275, 'fraction_numerical': 0.02641401091013494, 'mean_word_length': 5.636190476190476, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '51374', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1224, 'n_tokens_neox': 1186, 'n_words': 407, 'path': 'Session_4/1_ps_gpio.ipynb', 'repo_name': 'yutiansut/PYNQ_Workshop', 'size': '3483'}