Skip to content

Commit

Permalink
Merge pull request #7 from denizkasap/gpio_virtualization
Browse files Browse the repository at this point in the history
GPIO Virtualization.
  • Loading branch information
simone-machetti committed Mar 4, 2024
2 parents 6e3994f + 537883b commit a9cff47
Show file tree
Hide file tree
Showing 12 changed files with 989 additions and 70 deletions.
2 changes: 1 addition & 1 deletion hw/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

https://github.com/esl-epfl/x-heep-femu

abd72bf44c6c537ea60a5d0b42a3151d7881368e
ecf8dacf4fddf32260d46c66ab8b04c67aae977c
Binary file modified hw/x_heep.bit
100644 → 100755
Binary file not shown.
648 changes: 591 additions & 57 deletions hw/x_heep.hwh

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions sw/arm/apps/virtual_gpio_read/virtual_gpio_read.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2023 EPFL
# Solderpad Hardware License, Version 2.1, see LICENSE.md for details.
# SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1
#
# Author: Deniz Kasap - deniz.kasap@epfl.ch

# Import the X-HEEP Python class
from pynq import x_heep
import random

# Load the X-HEEP bitstream
x_heep = x_heep()

# Compile the application
x_heep.compile_app("virtual_gpio_read")

# Write all ones to GPIO pins
for gpio_pin in range(30):
x_heep.GPIO_write(1, gpio_pin)

# Run the application
x_heep.run_app()

# Verify the output
stdout_path = "/home/xilinx/x-heep-femu-sdk/sw/riscv/build/stdout.txt"
expected_output = "Read operation successful."
f = open(stdout_path, "r")
if f.read().strip() == expected_output:
print("Test Passed!")
else:
print("Test Failed!")
32 changes: 32 additions & 0 deletions sw/arm/apps/virtual_gpio_write/virtual_gpio_write.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2023 EPFL
# Solderpad Hardware License, Version 2.1, see LICENSE.md for details.
# SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1
#
# Author: Deniz Kasap - deniz.kasap@epfl.ch

# Import the X-HEEP Python class
from pynq import x_heep
import random

# Load the X-HEEP bitstream
x_heep = x_heep()

# Compile the application
x_heep.compile_app("virtual_gpio_write")

# Run the application
x_heep.run_app()

# Compare the results
bit_array_read = ""
bit_array_expected = "101010101010101010101010101010"

for gpio_pin in range(30):
pin_read = x_heep.GPIO_read(gpio_pin+8)
bit_array_read += str(pin_read)
print(bit_array_read)

if bit_array_read == bit_array_expected:
print("Test Passed!")
else:
print("Test Failed!")
12 changes: 0 additions & 12 deletions sw/arm/jupyter_notebooks/hello_world.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,6 @@
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
}
},
"nbformat": 4,
Expand Down
99 changes: 99 additions & 0 deletions sw/arm/jupyter_notebooks/virtual_gpio_read.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "63f1eaa7",
"metadata": {},
"outputs": [],
"source": [
"# Import the X-HEEP Python class\n",
"from pynq import x_heep\n",
"import random"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2d224b64",
"metadata": {},
"outputs": [],
"source": [
"# Load the X-HEEP bitstream\n",
"x_heep = x_heep()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "538601e2",
"metadata": {},
"outputs": [],
"source": [
"# Compile the application\n",
"x_heep.compile_app(\"virtual_gpio_read\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4fbcd989",
"metadata": {},
"outputs": [],
"source": [
"# Write all ones to GPIO pins\n",
"for gpio_pin in range(30):\n",
" x_heep.GPIO_write(1, gpio_pin)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4ce20521",
"metadata": {},
"outputs": [],
"source": [
"# Run the application\n",
"x_heep.run_app()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9e9b1537",
"metadata": {},
"outputs": [],
"source": [
"# Verify the output\n",
"stdout_path = \"/home/xilinx/x-heep-femu-sdk/sw/riscv/build/stdout.txt\"\n",
"expected_output = \"Read operation successful.\"\n",
"f = open(stdout_path, \"r\")\n",
"if f.read().strip() == expected_output:\n",
" print(\"Test Passed!\")\n",
"else:\n",
" print(\"Test Failed!\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
92 changes: 92 additions & 0 deletions sw/arm/jupyter_notebooks/virtual_gpio_write.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "63f1eaa7",
"metadata": {},
"outputs": [],
"source": [
"# Import the X-HEEP Python class\n",
"from pynq import x_heep\n",
"import random"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2d224b64",
"metadata": {},
"outputs": [],
"source": [
"# Load the X-HEEP bitstream\n",
"x_heep = x_heep()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d6af7d95",
"metadata": {},
"outputs": [],
"source": [
"# Compile the application\n",
"x_heep.compile_app(\"virtual_gpio_write\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fd8e74c7",
"metadata": {},
"outputs": [],
"source": [
"# Run the application\n",
"x_heep.run_app()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4fbcd989",
"metadata": {},
"outputs": [],
"source": [
"# Compare the results\n",
"bit_array_read = \"\"\n",
"bit_array_expected = \"101010101010101010101010101010\"\n",
"\n",
"for gpio_pin in range(30):\n",
" pin_read = x_heep.GPIO_read(gpio_pin+8)\n",
" bit_array_read += str(pin_read)\n",
"print(bit_array_read)\n",
"\n",
"if bit_array_read == bit_array_expected:\n",
" print(\"Test Passed!\")\n",
"else:\n",
" print(\"Test Failed!\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
17 changes: 17 additions & 0 deletions sw/arm/sdk/x_heep_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,23 @@ def reset_pulse(self):
self.release_reset()


def GPIO_write(self, bit, pin):

# Writes to a specifc GPIO pin (pin attribute starts from 0 and
# first 8 GPIO bits on PS are used)
gpio_pin = GPIO(GPIO.get_gpio_pin(8+pin), 'out')
gpio_pin.write(bit)


def GPIO_read(self, pin):

# Reads from a specifc GPIO pin (pin attribute starts from 0 and
# first 8 GPIO bits on PS are used)
gpio_pin = GPIO(GPIO.get_gpio_pin(pin), 'in')
pin_read = gpio_pin.read()
return pin_read


def init_flash(self):

# Allocate Flash
Expand Down
32 changes: 32 additions & 0 deletions sw/riscv/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,38 @@ apps/virtual_obi_write/virtual_obi_write.elf: apps/virtual_obi_write/virtual_obi
-Wl,-Map=apps/virtual_obi_write/virtual_obi_write.map \
-L $(RISCV)/riscv32-unknown-elf/lib \
-lc -lm -lgcc -flto -ffunction-sections -fdata-sections -specs=nano.specs

apps/virtual_gpio_write/virtual_gpio_write.elf: apps/virtual_gpio_write/virtual_gpio_write.c
$(RISCV_EXE_PREFIX)gcc -march=rv32imc -o $@ -w -Os -g -nostdlib \
$(CUSTOM_GCC_FLAGS) \
-DHOST_BUILD \
-T link/link.ld \
-I $(RISCV)/riscv32-unknown-elf/include \
$(INC_FOLDERS_GCC) \
-static \
$(LIB_CRT) \
$^ $(LIB_RUNTIME) \
$(LIB_BASE) \
$(LIB_DRIVERS) \
-Wl,-Map=apps/virtual_gpio_write/virtual_gpio_write.map \
-L $(RISCV)/riscv32-unknown-elf/lib \
-lc -lm -lgcc -flto -ffunction-sections -fdata-sections -specs=nano.specs

apps/virtual_gpio_read/virtual_gpio_read.elf: apps/virtual_gpio_read/virtual_gpio_read.c
$(RISCV_EXE_PREFIX)gcc -march=rv32imc -o $@ -w -Os -g -nostdlib \
$(CUSTOM_GCC_FLAGS) \
-DHOST_BUILD \
-T link/link.ld \
-I $(RISCV)/riscv32-unknown-elf/include \
$(INC_FOLDERS_GCC) \
-static \
$(LIB_CRT) \
$^ $(LIB_RUNTIME) \
$(LIB_BASE) \
$(LIB_DRIVERS) \
-Wl,-Map=apps/virtual_gpio_read/virtual_gpio_read.map \
-L $(RISCV)/riscv32-unknown-elf/lib \
-lc -lm -lgcc -flto -ffunction-sections -fdata-sections -specs=nano.specs

clean:
rm -rf build
47 changes: 47 additions & 0 deletions sw/riscv/apps/virtual_gpio_read/virtual_gpio_read.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <stdio.h>
#include <stdlib.h>
#include "csr.h"
#include "hart.h"
#include "handler.h"
#include "core_v_mini_mcu.h"
#include "gpio.h"

int main(int argc, char *argv[])
{
gpio_params_t gpio_params;
gpio_t gpio;
gpio_result_t gpio_res;

//Start writing to GPIO of Always-On Peripheral [7:0]
gpio_params.base_addr = mmio_region_from_addr((uintptr_t)GPIO_AO_START_ADDRESS);
bool read_from_pin;

for (int i=2; i < 8; i++){ //Skip first 2 GPIO's since they are alread used
gpio_res = gpio_init(gpio_params, &gpio);
gpio_res = gpio_input_enabled(&gpio, i, true);
gpio_res = gpio_read(&gpio, i, &read_from_pin);
if (read_from_pin != 1){
printf("An element does not match!");
printf("Not matching at GPIO-AO at pin %d", i);
return EXIT_FAILURE;
}
}

//Start writing to GPIO of Peripheral [31:8]
gpio_params.base_addr = mmio_region_from_addr((uintptr_t)GPIO_START_ADDRESS);

for (int i=8; i < 32; i++){
gpio_res = gpio_init(gpio_params, &gpio);
gpio_res = gpio_input_enabled(&gpio, i, true);
gpio_res = gpio_read(&gpio, i, &read_from_pin);
if (read_from_pin != 1){
printf("An element does not match!");
printf("Not matching at GPIO at pin %d", i);
return EXIT_FAILURE;
}
}

printf("Read operation successful.");

return EXIT_SUCCESS;
}

0 comments on commit a9cff47

Please sign in to comment.