Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update GPIO driver #246

Merged
merged 36 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
0782ff7
add code structure
Mar 31, 2023
3287b93
Merge branch 'main' into gpios
Mar 31, 2023
4c13714
Adding gpio functionalities
Ho3eintaji Apr 4, 2023
e2cd233
Merge branch 'main' into gpios
Apr 4, 2023
8325361
Merge branch 'gpios' of github.com:Ho3eintaji/x-heep into gpios
Apr 4, 2023
e75d77b
removing pre version from code
Apr 4, 2023
b0d6810
Merge branch 'main' into gpios
Apr 5, 2023
1c336d5
removing some bugs of function definition
Apr 5, 2023
873a93f
solving the bug on gpio_reset, and updating example_gpio_cnt
Apr 5, 2023
0d289a5
removing mmio header
Apr 6, 2023
4c87803
easily configuration of pins by gpio_config
Apr 6, 2023
6436e16
adding MAX_PIN
Apr 6, 2023
267f99c
example_power_gating_corewas updated
Apr 6, 2023
b8260df
updating gpio_pmw
Apr 6, 2023
da57c41
updating blinky_freertos
Apr 6, 2023
d4878dc
removing left comments
Apr 6, 2023
017f8fc
removing run.sh
Apr 6, 2023
292f6e7
Merge branch 'main' into gpios
Apr 14, 2023
d299e2b
bitfield modification, removing magic numbers
Apr 17, 2023
83e9e91
adding max pin to xheep.h
Apr 24, 2023
2532fb8
Merge branch 'main' into gpios
Apr 24, 2023
128ab63
gpio_toggle bug was solved
Apr 24, 2023
dc3683f
adding error types
Apr 24, 2023
e3b1106
Merge branch 'main' into gpios
Apr 24, 2023
7a62e52
Resolved merge conflict by new updates of rplic which were using prev…
May 5, 2023
4583c7c
some minor modifications
May 8, 2023
8f060ab
trim code
May 26, 2023
5487b8a
solving conflict with main
May 26, 2023
85cc508
blinky_freertos bug
May 26, 2023
825c599
resolving the conflict with example_power_gating_core
May 31, 2023
3530a72
Merge branch 'esl-epfl:main' into gpios
JoseCalero Jun 23, 2023
09d1d50
Merged gpio hal refactoring and main. Bug pending on freertos blinky
JuanSapriza Jun 29, 2023
a0a4861
Updated the GPIOS being used for the LED5
JuanSapriza Jun 29, 2023
783c152
Merge branch 'main' into pr_gpios
JuanSapriza Jun 30, 2023
74fd1d8
Merged main into pr_gpios
JuanSapriza Jul 10, 2023
2cd850e
removed unnecessary comments
JuanSapriza Jul 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ sw/device/lib/drivers/**/*_structs.h

# openroad
flow/OpenROAD-flow-scripts

run.sh
28 changes: 28 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#! usr/bin/bash# This script is to be run in the x-heep base directory (aka BASE)
# It takes only one argument: the application name.
# It takes for granted that myApplication.c is located in
# BASE/sw/applications/myApplication/# Run this script from the BASE with
# $ sudo bash pynq-run <name-of-your-app># The pynq-z2 board must be
#   - Powered
#   - Loaded with the appropriate bitstream (use vivado)
#   - Connected to the EPFL programmer
#   - With sw1=0(outside) and sw0=1(inside)
#echo Will build for $1
echo Will run application $1

make app-clean
# Compile and build the binaries to be loaded in the flash.
# To use different LINKER options, just modify this script.
# (remember to also change the make flash-load command below)
make app PROJECT=$1 LINKER=flash_load TARGET=pynq-z2



# To use the iceprog

( cd sw/vendor/yosyshq_icestorm/iceprog && make clean && make all)

# Make and load the flash programmable binary into the iceprog
make flash-prog &&
# Open picocom to communicate with the board through UART
picocom -b 115200 -r -l --imap lfcrlf /dev/ttyUSB2
58 changes: 26 additions & 32 deletions sw/applications/blinky_freertos/main.c
Copy link
Contributor

@JuanSapriza JuanSapriza Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the I2S was introduced, it was decided to use GPIOs 20-22 (because they were the last ones).
These were already mapped to the RGB LED5, so they were re-mapped so that GPIOs 15-17 were assigned instead (in the pin_assign.xdc).
This change was never reflected in the example_freertos_blinky, which continued to use GPIOs 20-22.

The only change needed for it to work is to change the #define GPIO_LD5_* xx to their proper numbers.

There is no mux to change, as I2S and LED5 go to different pins in the FPGA board.

I will make a PR to this branch with this change and a merge of main.

Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,6 @@ int8_t intr_flag = 0;
/* Temporal counter to store blinking status */
int8_t intr_blink = 0;

/* GPIO struct */
gpio_t gpio;

/****************************************************************************/
/** **/
/* EXPORTED FUNCTIONS */
Expand All @@ -251,25 +248,22 @@ void system_init(void)
soc_ctrl.base_addr = mmio_region_from_addr((uintptr_t)SOC_CTRL_START_ADDRESS);
uint32_t freq_hz = soc_ctrl_get_frequency(&soc_ctrl);

// Set GPIOs
gpio_params_t gpio_params;
gpio_result_t gpio_res;
gpio_params.base_addr = mmio_region_from_addr((uintptr_t)GPIO_START_ADDRESS);
gpio_res = gpio_init(gpio_params, &gpio);
if (gpio_res != kGpioOk)
{
printf("GPIO Failed\n;");
}

gpio_res = gpio_output_set_enabled(&gpio, GPIO_LD5_R, true);
if (gpio_res != kGpioOk) printf("Failed\n;");
gpio_write(&gpio, GPIO_LD5_R, false);
gpio_res = gpio_output_set_enabled(&gpio, GPIO_LD5_B, true);
if (gpio_res != kGpioOk) printf("Failed\n;");
gpio_write(&gpio, GPIO_LD5_B, false);
gpio_res = gpio_output_set_enabled(&gpio, GPIO_LD5_G, true);
gpio_result_t gpio_res;
gpio_cfg_t pin_cfg = {
.pin= GPIO_LD5_R,
.mode= GpioModeOutPushPull
};
gpio_res = gpio_config(pin_cfg);
pin_cfg.pin = GPIO_LD5_B;
gpio_res |= gpio_config(pin_cfg);
pin_cfg.pin = GPIO_LD5_G;
gpio_res |= gpio_config(pin_cfg);
if (gpio_res != kGpioOk) printf("Failed\n;");
gpio_write(&gpio, GPIO_LD5_G, false);

gpio_write(GPIO_LD5_R, false);
gpio_write(GPIO_LD5_B, false);
gpio_write(GPIO_LD5_G, false);
Ho3eintaji marked this conversation as resolved.
Show resolved Hide resolved

// Setup rv_timer_0_1
mmio_region_t timer_0_1_reg = mmio_region_from_addr(RV_TIMER_AO_START_ADDRESS);
Expand Down Expand Up @@ -444,30 +438,30 @@ void vToggleLED( void )
{
if (intr_blink == 0)
{
gpio_write(&gpio, GPIO_LD5_R, true);
gpio_write(&gpio, GPIO_LD5_B, false);
gpio_write(&gpio, GPIO_LD5_G, false);
gpio_write(GPIO_LD5_R, true);
gpio_write(GPIO_LD5_B, false);
gpio_write(GPIO_LD5_G, false);
intr_blink++;
}
else if (intr_blink == 1)
{
gpio_write(&gpio, GPIO_LD5_R, false);
gpio_write(&gpio, GPIO_LD5_B, true);
gpio_write(&gpio, GPIO_LD5_G, false);
gpio_write(GPIO_LD5_R, false);
gpio_write(GPIO_LD5_B, true);
gpio_write(GPIO_LD5_G, false);
intr_blink++;
}
else if (intr_blink == 2)
{
gpio_write(&gpio, GPIO_LD5_R, false);
gpio_write(&gpio, GPIO_LD5_B, false);
gpio_write(&gpio, GPIO_LD5_G, true);
gpio_write(GPIO_LD5_R, false);
gpio_write(GPIO_LD5_B, false);
gpio_write(GPIO_LD5_G, true);
intr_blink++;
}
else
{
gpio_write(&gpio, GPIO_LD5_R, false);
gpio_write(&gpio, GPIO_LD5_B, false);
gpio_write(&gpio, GPIO_LD5_G, false);
gpio_write(GPIO_LD5_R, false);
gpio_write(GPIO_LD5_B, false);
gpio_write(GPIO_LD5_G, false);
intr_blink = 0;
}

Expand Down
45 changes: 22 additions & 23 deletions sw/applications/example_gpio_cnt/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "pad_control.h"
#include "pad_control_regs.h" // Generated.
#include "x-heep.h"
#include "gpio_structs.h" //remove

/*
Notes:
Expand Down Expand Up @@ -71,16 +72,6 @@ int main(int argc, char *argv[])
pad_control_set_mux(&pad_control, (ptrdiff_t)(PAD_CONTROL_PAD_MUX_I2C_SDA_REG_OFFSET), 1);
#endif

gpio_params_t gpio_params;
gpio_t gpio;
gpio_result_t gpio_res;
gpio_params.base_addr = mmio_region_from_addr((uintptr_t)GPIO_START_ADDRESS);
gpio_res = gpio_init(gpio_params, &gpio);
if (gpio_res != kGpioOk) {
printf("Failed\n;");
return -1;
}

plic_res = dif_plic_irq_set_priority(&rv_plic, GPIO_INTR, 1);
if (plic_res != kDifPlicOk) {
printf("Failed\n;");
Expand All @@ -101,29 +92,37 @@ int main(int argc, char *argv[])
CSR_SET_BITS(CSR_REG_MIE, mask);
external_intr_flag = 0;

gpio_res = gpio_output_set_enabled(&gpio, GPIO_TB_OUT, true);
if (gpio_res != kGpioOk) {
printf("Failed\n;");
return -1;
}
gpio_write(&gpio, GPIO_TB_OUT, false);
//gpio_reset_all();
gpio_result_t gpio_res;

gpio_res = gpio_input_enabled(&gpio, GPIO_TB_IN, true);
if (gpio_res != kGpioOk) {
gpio_cfg_t cfg_out = {
.pin = GPIO_TB_OUT,
.mode = GpioModeOutPushPull
};
gpio_res = gpio_config(cfg_out);
if (gpio_res != GpioOk) {
printf("Failed\n;");
return -1;
}


gpio_res = gpio_irq_set_trigger(&gpio, GPIO_TB_IN, true, kGpioIrqTriggerEdgeRising);
if (gpio_res != kGpioOk) {
gpio_res = gpio_write(GPIO_TB_OUT, false);

gpio_cfg_t cfg_in = {
.pin = GPIO_TB_IN,
.mode = GpioModeIn,
.en_input_sampling = true,
.en_intr = true,
.intr_type = GpioIntrEdgeRising
};
gpio_res = gpio_config(cfg_in);
if (gpio_res != GpioOk) {
printf("Failed\n;");
return -1;
}

printf("Write 1 to GPIO 30 and wait for interrupt...\n");
external_intr_flag = 0;
while(external_intr_flag==0) {
gpio_write(&gpio, GPIO_TB_OUT, true);
gpio_res = gpio_write(GPIO_TB_OUT, true);
wait_for_interrupt();
}
printf("Success\n");
Expand Down
29 changes: 19 additions & 10 deletions sw/applications/example_power_gating_core/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ static const uint64_t kTickFreqHz = 1000 * 1000; // 1 MHz

static power_manager_t power_manager;
static dif_plic_t rv_plic;
static gpio_t gpio;
// static gpio_t gpio;

int main(int argc, char *argv[])
{
// Setup fast interrupt controller
fast_intr_ctrl_t fast_intr_ctrl;
fast_intr_ctrl.base_addr = mmio_region_from_addr((uintptr_t)FAST_INTR_CTRL_START_ADDRESS);
// fast_intr_ctrl_t fast_intr_ctrl;
Ho3eintaji marked this conversation as resolved.
Show resolved Hide resolved
// fast_intr_ctrl.base_addr = mmio_region_from_addr((uintptr_t)FAST_INTR_CTRL_START_ADDRESS);

// Setup power_manager
mmio_region_t power_manager_reg = mmio_region_from_addr(POWER_MANAGER_START_ADDRESS);
Expand Down Expand Up @@ -56,9 +56,9 @@ int main(int argc, char *argv[])
rv_timer_init(timer_2_3_reg, (rv_timer_config_t){.hart_count = 2, .comparator_count = 1}, &timer_2_3);

// Setup gpio
gpio_params_t gpio_params;
gpio_params.base_addr = mmio_region_from_addr((uintptr_t)GPIO_START_ADDRESS);
gpio_init(gpio_params, &gpio);
// gpio_params_t gpio_params;
Ho3eintaji marked this conversation as resolved.
Show resolved Hide resolved
// gpio_params.base_addr = mmio_region_from_addr((uintptr_t)GPIO_START_ADDRESS);
// gpio_init(gpio_params, &gpio);

// Init cpu_subsystem's counters
if (power_gate_counters_init(&power_manager_cpu_counters, 30, 30, 30, 30, 30, 30, 0, 0) != kPowerManagerOk_e)
Expand Down Expand Up @@ -127,10 +127,19 @@ int main(int argc, char *argv[])
// Power-gate and wake-up due to plic
dif_plic_irq_set_priority(&rv_plic, GPIO_INTR_31, 1);
dif_plic_irq_set_enabled(&rv_plic, GPIO_INTR_31, 0, kDifPlicToggleEnabled);
gpio_output_set_enabled(&gpio, 30, true);
gpio_irq_set_trigger(&gpio, 1 << 31, kGpioIrqTriggerLevelHigh);
gpio_irq_set_enabled(&gpio, 31, true);
gpio_write(&gpio, 30, true);

// gpio_output_set_enabled(&gpio, 30, true);
gpio_cfg_t pin1_cfg = {.pin = 30, .mode = GpioModeOutPushPull};
gpio_config (pin1_cfg);

// gpio_irq_set_trigger(&gpio, 1 << 31, kGpioIrqTriggerLevelHigh);
// gpio_irq_set_enabled(&gpio, 31, true);
gpio_cfg_t pin2_cfg = {.pin = 31, .mode = GpioModeIn,.en_input_sampling = true,
.en_intr = true, .intr_type = GpioIntrLevelHigh};
gpio_config (pin2_cfg);

// gpio_write(&gpio, 30, true);
gpio_write(30, true);

CSR_CLEAR_BITS(CSR_REG_MSTATUS, 0x8);
if (power_gate_core(&power_manager, kPlic_pm_e, &power_manager_cpu_counters) != kPowerManagerOk_e)
Expand Down
29 changes: 20 additions & 9 deletions sw/applications/gpio_pmw/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,37 @@

int main(int argc, char *argv[])
{
gpio_params_t gpio_params;
gpio_t gpio;
// gpio_params_t gpio_params;
// gpio_t gpio;
gpio_result_t gpio_res;
gpio_params.base_addr = mmio_region_from_addr((uintptr_t)GPIO_AO_START_ADDRESS);
gpio_res = gpio_init(gpio_params, &gpio);
gpio_res = gpio_output_set_enabled(&gpio, GPIO_PMW, true);
// gpio_params.base_addr = mmio_region_from_addr((uintptr_t)GPIO_AO_START_ADDRESS);
// gpio_res = gpio_init(gpio_params, &gpio);
// gpio_res = gpio_output_set_enabled(&gpio, GPIO_PMW, true);
gpio_cfg_t pin_cfg = {
.pin = GPIO_PMW,
.mode = GpioModeOutPushPull
};
gpio_res = gpio_config (pin_cfg);
if (gpio_res != GpioOk)
printf("Gpio initialization failed!\n");

#ifdef TARGET_PYNQ_Z2
#pragma message ( "this application never ends" )
while(1) {
gpio_write(&gpio, GPIO_PMW, true);
// gpio_write(&gpio, GPIO_PMW, true);
gpio_write(GPIO_PMW, true);
for(int i=0;i<10;i++) asm volatile("nop");
gpio_write(&gpio, GPIO_PMW, false);
// gpio_write(&gpio, GPIO_PMW, false);
gpio_write(GPIO_PMW, false);
for(int i=0;i<10;i++) asm volatile("nop");
}
#else
for(int i=0;i<100;i++) {
gpio_write(&gpio, GPIO_PMW, true);
// gpio_write(&gpio, GPIO_PMW, true);
pio_write(GPIO_PMW, true);
for(int i=0;i<10;i++) asm volatile("nop");
gpio_write(&gpio, GPIO_PMW, false);
// gpio_write(&gpio, GPIO_PMW, false);
gpio_write(GPIO_PMW, false);
for(int i=0;i<10;i++) asm volatile("nop");
}
#endif
Expand Down
Loading