Skip to content

Commit

Permalink
update GPIO driver (#246)
Browse files Browse the repository at this point in the history
* add code structure

* Adding gpio functionalities

* removing pre version from code

* removing some bugs of function definition

* solving the bug on gpio_reset, and updating example_gpio_cnt

* removing mmio header

* easily configuration of pins by gpio_config

* adding MAX_PIN

* example_power_gating_corewas updated

* updating gpio_pmw

* updating blinky_freertos

* removing left comments

* removing run.sh

* bitfield modification, removing magic numbers

* adding max pin to xheep.h

* gpio_toggle bug was solved

* adding error types

* some minor modifications

* trim code

* blinky_freertos bug

* Updated the GPIOS being used for the LED5

* removed unnecessary comments

---------

Co-authored-by: Taji Hossein <taji@eslpc2.intranet.epfl.ch>
Co-authored-by: Hossein <hossein.taji@epfl.ch>
Co-authored-by: jmiranda <mirandacalero.joseangel@gmail.com>
Co-authored-by: Juan Sapriza <juan.sapriza@epfl.ch>
  • Loading branch information
5 people authored Jul 12, 2023
1 parent 5d7dae4 commit a55e7d0
Show file tree
Hide file tree
Showing 8 changed files with 826 additions and 536 deletions.
66 changes: 30 additions & 36 deletions sw/applications/example_freertos_blinky/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ or 0 to run the more comprehensive test and demo application. */


#ifdef TARGET_PYNQ_Z2
#define GPIO_LD5_R 20
#define GPIO_LD5_B 21
#define GPIO_LD5_G 22
#define GPIO_LD5_R 15
#define GPIO_LD5_B 16
#define GPIO_LD5_G 17
#pragma message ( "Executing FreeRTOS using X-HEEP and Pynq-z2" )
#else
#define GPIO_LD5_R 29
Expand Down 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);
if (gpio_res != kGpioOk) printf("Failed\n;");
gpio_write(&gpio, GPIO_LD5_G, false);
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 != GpioOk) printf("Failed\n;");

gpio_write(GPIO_LD5_R, false);
gpio_write(GPIO_LD5_B, false);
gpio_write(GPIO_LD5_G, false);

// 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
55 changes: 26 additions & 29 deletions sw/applications/example_gpio_intr/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 <limits.h> //todo: remove

/*
Notes:
Expand All @@ -26,17 +27,17 @@
#define DEFAULT_PRINTF_BEHAVIOR 1

/* By default, printfs are activated for FPGA and disabled for simulation. */
#ifdef TARGET_PYNQ_Z2
#ifdef TARGET_PYNQ_Z2
#define ENABLE_PRINTF DEFAULT_PRINTF_BEHAVIOR
#else
#else
#define ENABLE_PRINTF !DEFAULT_PRINTF_BEHAVIOR
#endif

#if ENABLE_PRINTF
#define PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
#else
#define PRINTF(...)
#endif
#endif


#ifndef RV_PLIC_IS_INCLUDED
Expand Down Expand Up @@ -81,16 +82,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\r;");
return -1;
}

plic_res = plic_irq_set_priority(GPIO_INTR, 1);
if (plic_res != kPlicOk) {
PRINTF("Failed\n\r;");
Expand All @@ -111,33 +102,39 @@ int main(int argc, char *argv[])
CSR_SET_BITS(CSR_REG_MIE, mask);
plic_intr_flag = 0;

gpio_res = gpio_output_set_enabled(&gpio, GPIO_TB_OUT, true);
if (gpio_res != kGpioOk) {
PRINTF("Failed\n\r;");
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) {
PRINTF("Failed\n\r;");
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) {
PRINTF("Failed\n\r;");
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\r");
while(plic_intr_flag==0) {

// disable_interrupts
// this does not prevent waking up the core as this is controlled by the MIP register
CSR_CLEAR_BITS(CSR_REG_MSTATUS, 0x8);
gpio_write(&gpio, GPIO_TB_OUT, true);
gpio_write(GPIO_TB_OUT, true);
wait_for_interrupt();
CSR_SET_BITS(CSR_REG_MSTATUS, 0x8);
}
Expand Down
21 changes: 12 additions & 9 deletions sw/applications/example_gpio_toggle/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,21 @@

int main(int argc, char *argv[])
{
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_TOGGLE, true);
gpio_cfg_t pin_cfg = {
.pin = GPIO_TOGGLE,
.mode = GpioModeOutPushPull
};
gpio_res = gpio_config (pin_cfg);
if (gpio_res != GpioOk)
PRINTF("Gpio initialization failed!\n");


for(int i=0;i<100;i++) {
gpio_write(&gpio, GPIO_TOGGLE, true);
for(int i=0;i<10;i++) asm volatile("nop");
gpio_write(&gpio, GPIO_TOGGLE, false);
for(int i=0;i<10;i++) asm volatile("nop");
gpio_write(GPIO_TOGGLE, true);
for(int i=0;i<10;i++) asm volatile("nop");
gpio_write(GPIO_TOGGLE, false);
for(int i=0;i<10;i++) asm volatile("nop");
}

PRINTF("Success.\n");
Expand Down
27 changes: 12 additions & 15 deletions sw/applications/example_power_gating_core/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@
static rv_timer_t timer_0_1;
static rv_timer_t timer_2_3;
static const uint64_t kTickFreqHz = 1000 * 1000; // 1 MHz

static power_manager_t power_manager;
static gpio_t gpio;

#ifndef TARGET_PYNQ_Z2
#define GPIO_TB_OUT 30
Expand Down Expand Up @@ -87,11 +85,6 @@ int main(int argc, char *argv[])
mmio_region_t timer_2_3_reg = mmio_region_from_addr(RV_TIMER_START_ADDRESS);
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);

// 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 @@ -157,14 +150,18 @@ int main(int argc, char *argv[])

#ifndef TARGET_PYNQ_Z2
// Power-gate and wake-up due to plic
bool state = false;
plic_irq_set_priority(GPIO_INTR, 1);
plic_irq_set_enabled(GPIO_INTR, kPlicToggleEnabled);
gpio_output_set_enabled(&gpio, GPIO_TB_OUT, true);
gpio_input_enabled(&gpio, GPIO_TB_IN, true);
gpio_irq_set_trigger(&gpio, GPIO_TB_IN, true, kGpioIrqTriggerEdgeRising);
gpio_write(&gpio, GPIO_TB_OUT, true);

bool state = false;
plic_irq_set_priority(GPIO_INTR_31, 1);
plic_irq_set_enabled(GPIO_INTR_31, kPlicToggleEnabled);

gpio_cfg_t pin1_cfg = {.pin = GPIO_TB_OUT, .mode = GpioModeOutPushPull};
gpio_config (pin1_cfg);
gpio_write(GPIO_TB_OUT, true);

gpio_cfg_t pin2_cfg = {.pin = GPIO_TB_IN, .mode = GpioModeIn,.en_input_sampling = true,
.en_intr = true, .intr_type = GpioIntrEdgeRising};
gpio_config (pin2_cfg);

CSR_CLEAR_BITS(CSR_REG_MSTATUS, 0x8);
if (power_gate_core(&power_manager, kPlic_pm_e, &power_manager_cpu_counters) != kPowerManagerOk_e)
{
Expand Down
Loading

0 comments on commit a55e7d0

Please sign in to comment.