libpynq (release 5EWC0-2023 version 0.2.6 of 2025-11-10 09:56)
Loading...
Searching...
No Matches
gpio.c
Go to the documentation of this file.
1/*
2Copyright (c) 2023 Eindhoven University of Technology
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in all
12copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20SOFTWARE.
21*/
22#include "gpio.h"
24#include <log.h>
25#include <pinmap.h>
26#include <platform.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <version.h>
30
31static arm_shared gpio_handle, intc0_handle;
32volatile uint32_t *gpio = NULL;
33volatile uint32_t *intc0 = NULL;
34
39typedef enum {
41 // 1 is input, 0 is output.
44 // 1 is input, 0 is output.
46
47 // ^ operation
49 // | operation
51 // &~ operation
53
54 // ^ operation
56 // | operation
58 // &~ operation
60
61 // Set output as input.
63 // Set output as output.
65 // Set output as input.
67 // Set output as output.
69
71
73 /* if gpio == NULL we know we are not inialized */
74 return (gpio != NULL) ? true : false;
75}
76
77void gpio_init(void) {
78 pynq_info("Initialize");
80 gpio = arm_shared_init(&gpio_handle, axi_gpio_0, 4096);
81 intc0 = arm_shared_init(&intc0_handle, axi_intc_0, 4096);
82}
83
84void gpio_destroy(void) {
85 pynq_info("Destroy");
86 arm_shared_close(&gpio_handle);
87 arm_shared_close(&intc0_handle);
88 gpio = NULL;
89 intc0 = NULL;
90}
91
98
99void gpio_reset(void) {
100 pynq_info("Reset all pins");
101 // set all pins as input
102 gpio[1] = 0xFFFFFFFF;
103 // re-set all outputs to 0
104 gpio[0] = 0x0;
105
106 // set all pins as input
107 gpio[3] = 0xFFFFFFFF;
108 // re-set all outputs to 0
109 gpio[2] = 0x0;
110 // disable all interrupts
111 intc0[0] = 0;
112 intc0[1] = 0;
113 // remove all pending interrupts
114 intc0[2] = 0;
115 intc0[3] = 0;
116}
117
119 PIN_CHECK(pin);
120 if (!(dir == GPIO_DIR_INPUT || dir == GPIO_DIR_OUTPUT)) {
121 pynq_error("gpio_set_direction: invalid direction %d", dir);
122 }
123 int pin_bank = pin % 32;
124 if (dir == GPIO_DIR_INPUT) {
125 int bank = pin < 32 ? GPIO_REG_1_TRI_SET : GPIO_REG_2_TRI_SET;
126 gpio[bank] = (1 << pin_bank);
127 } else {
128 int bank = pin < 32 ? GPIO_REG_1_TRI_CLR : GPIO_REG_2_TRI_CLR;
129 gpio[bank] = (1 << pin_bank);
130 }
131}
132
134 PIN_CHECK(pin);
135 int pin_bank = pin % 32;
137 int dir =
138 ((gpio[bank] & (1 << pin_bank)) != 0) ? GPIO_DIR_INPUT : GPIO_DIR_OUTPUT;
139 return dir;
140}
141
142void gpio_set_level(const io_t pin, const gpio_level_t level) {
143 PIN_CHECK(pin);
144 if (!(level == GPIO_LEVEL_HIGH || level == GPIO_LEVEL_LOW)) {
145 pynq_error("gpio_set_level: level %d is invalid", level);
146 }
147 int pin_bank = pin % 32;
148 if (level == GPIO_LEVEL_HIGH) {
150 gpio[bank] = (1 << pin_bank);
151 } else {
153 gpio[bank] = (1 << pin_bank);
154 }
155}
156
158 PIN_CHECK(pin);
159 int pin_bank = pin % 32;
160 int bank = pin < 32 ? 0 : 2;
161 return (gpio[bank] & (1 << pin_bank)) != 0 ? GPIO_LEVEL_HIGH : GPIO_LEVEL_LOW;
162}
GPRIO_REG_MAP
Definition gpio.c:39
@ GPIO_REG_1_OUTPUT_SET
Definition gpio.c:50
@ GPIO_REG_1_TRI_CLR
Definition gpio.c:64
@ GPIO_REG_2_TRI_CLR
Definition gpio.c:68
@ GPIO_REG_2_TRI_SET
Definition gpio.c:66
@ GPIO_REG_1_OUTPUT_TOGGLE
Definition gpio.c:48
@ GPIO_REG_2_OUTPUT_CLR
Definition gpio.c:59
@ GPIO_REG_1_TRI_STATE
Definition gpio.c:42
@ GPIO_REG_2_OUTPUT_STATE
Definition gpio.c:43
@ GPIO_REG_1_OUTPUT_CLR
Definition gpio.c:52
@ GPIO_REG_2_OUTPUT_SET
Definition gpio.c:57
@ GPIO_REG_1_TRI_SET
Definition gpio.c:62
@ GPIO_REG_2_OUTPUT_TOGGLE
Definition gpio.c:55
@ GPIO_REG_1_OUTPUT_STATE
Definition gpio.c:40
@ GPIO_REG_2_TRI_STATE
Definition gpio.c:45
volatile uint32_t * intc0
Definition gpio.c:33
volatile uint32_t * gpio
Definition gpio.c:32
void arm_shared_close(arm_shared *handle)
void * arm_shared_init(arm_shared *handle, const uint32_t address, const uint32_t length)
gpio_direction_t gpio_get_direction(const io_t pin)
Returns the direction the set pin is initialized in.
Definition gpio.c:6
bool gpio_is_initialized(void)
Definition gpio.c:72
void gpio_set_direction(const io_t pin, const gpio_direction_t direction)
Set the IO pin as in input or output.
Definition gpio.c:5
void gpio_reset(void)
Reset all IO pins.
Definition gpio.c:18
void gpio_set_level(const io_t pin, const gpio_level_t level)
Set the level of the output IO pin. If the pin is configured as input, this function does nothing.
Definition gpio.c:7
gpio_direction_t
Definition gpio.h:88
gpio_level_t
Definition gpio.h:98
gpio_level_t gpio_get_level(const io_t pin)
Return the level of the IO pin.
Definition gpio.c:8
void gpio_destroy(void)
Definition gpio.c:3
void gpio_reset_pin(const io_t pin)
Function is currently a no-op placeholder for arduino compatibility.
Definition gpio.c:4
void gpio_init(void)
Definition gpio.c:2
@ GPIO_DIR_OUTPUT
Definition gpio.h:92
@ GPIO_DIR_INPUT
Definition gpio.h:90
@ GPIO_LEVEL_LOW
Definition gpio.h:100
@ GPIO_LEVEL_HIGH
Definition gpio.h:102
#define pynq_info(...)
Definition log.h:100
#define pynq_error(...)
Definition log.h:118
io_t
Definition pinmap.h:45
#define PIN_CHECK(pin)
Definition pinmap.h:160
void check_version(void)
Definition version.c:19