libpynq  (release 5EID0-2023 version 0.3.0 of 2024-04-25 09:42 )
gpio.c
Go to the documentation of this file.
1 /*
2 Copyright (c) 2023 Eindhoven University of Technology
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
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 
31 static arm_shared gpio_handle, intc0_handle;
32 volatile uint32_t *gpio = NULL;
33 volatile uint32_t *intc0 = NULL;
34 
35 bool gpio_is_initialized(void) {
36  /* if gpio == NULL we know we are not inialized */
37  return (gpio != NULL) ? true : false;
38 }
39 
40 void gpio_init(void) {
41  pynq_info("Initialize");
42  check_version();
43  gpio = arm_shared_init(&gpio_handle, axi_gpio_0, 4096);
44  intc0 = arm_shared_init(&intc0_handle, axi_intc_0, 4096);
45 }
46 
47 void gpio_destroy(void) {
48  pynq_info("Destroy");
49  arm_shared_close(&gpio_handle);
50  arm_shared_close(&intc0_handle);
51  gpio = NULL;
52  intc0 = NULL;
53 }
54 
55 void gpio_reset_pin(const io_t pin) {
56  PIN_CHECK(pin);
57  pynq_info("Reset pin: %d", pin);
60 }
61 
62 void gpio_reset(void) {
63  pynq_info("Reset all pins");
64  // set all pins as input
65  gpio[1] = 0xFFFFFFFF;
66  // re-set all outputs to 0
67  gpio[0] = 0x0;
68 
69  // set all pins as input
70  gpio[3] = 0xFFFFFFFF;
71  // re-set all outputs to 0
72  gpio[2] = 0x0;
73  // disable all interrupts
74  intc0[0] = 0;
75  intc0[1] = 0;
76  // remove all pending interrupts
77  intc0[2] = 0;
78  intc0[3] = 0;
79 }
80 
81 void gpio_set_direction(const io_t pin, const gpio_direction_t dir) {
82  PIN_CHECK(pin);
83  if (!(dir == GPIO_DIR_INPUT || dir == GPIO_DIR_OUTPUT)) {
84  pynq_error("gpio_set_direction: invalid direction %d", dir);
85  }
86  int pin_bank = pin % 32;
87  int bank = pin < 32 ? 1 : 3;
88  if (dir == GPIO_DIR_INPUT) {
89  gpio[bank] = gpio[bank] | (1 << pin_bank);
90  } else {
91  gpio[bank] = gpio[bank] & ~(1 << pin_bank);
92  }
93 }
94 
96  PIN_CHECK(pin);
97  int pin_bank = pin % 32;
98  int bank = pin < 32 ? 1 : 3;
99  int dir =
100  ((gpio[bank] & (1 << pin_bank)) != 0) ? GPIO_DIR_INPUT : GPIO_DIR_OUTPUT;
101  return dir;
102 }
103 
104 void gpio_set_level(const io_t pin, const gpio_level_t level) {
105  PIN_CHECK(pin);
106  if (!(level == GPIO_LEVEL_HIGH || level == GPIO_LEVEL_LOW)) {
107  pynq_error("gpio_set_level: level %d is invalid", level);
108  }
109  int pin_bank = pin % 32;
110  int bank = pin < 32 ? 0 : 2;
111  if (level == GPIO_LEVEL_HIGH) {
112  gpio[bank] = gpio[bank] | (1 << pin_bank);
113  } else {
114  gpio[bank] = gpio[bank] & ~(1 << pin_bank);
115  }
116 }
117 
119  PIN_CHECK(pin);
120  int pin_bank = pin % 32;
121  int bank = pin < 32 ? 0 : 2;
122  return (gpio[bank] & (1 << pin_bank)) != 0 ? GPIO_LEVEL_HIGH : GPIO_LEVEL_LOW;
123 }
GPIO_DIR_OUTPUT
@ GPIO_DIR_OUTPUT
Definition: gpio.h:92
check_version
void check_version(void)
Definition: version.c:19
gpio_direction_t
gpio_direction_t
Definition: gpio.h:88
pynq_error
#define pynq_error(...)
Definition: log.h:118
arm_shared_memory_system.h
gpio_set_level
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_reset_pin
void gpio_reset_pin(const io_t pin)
Function is currently a no-op placeholder for arduino compatibility.
Definition: gpio.c:4
arm_shared_t
Definition: arm_shared_memory_system.h:39
arm_shared_close
void arm_shared_close(arm_shared *handle)
Definition: arm_shared_memory_system.c:70
version.h
intc0
volatile uint32_t * intc0
Definition: gpio.c:33
GPIO_LEVEL_LOW
@ GPIO_LEVEL_LOW
Definition: gpio.h:100
gpio_get_level
gpio_level_t gpio_get_level(const io_t pin)
Return the level of the IO pin.
Definition: gpio.c:8
gpio_set_direction
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
gpio
volatile uint32_t * gpio
Definition: gpio.c:32
GPIO_DIR_INPUT
@ GPIO_DIR_INPUT
Definition: gpio.h:90
gpio_is_initialized
bool gpio_is_initialized(void)
Definition: gpio.c:35
gpio_level_t
gpio_level_t
Definition: gpio.h:98
pin
Definition: switchbox.c:99
gpio_init
void gpio_init(void)
Definition: gpio.c:2
pinmap.h
gpio_reset
void gpio_reset(void)
Reset all IO pins.
Definition: gpio.c:18
log.h
arm_shared_init
void * arm_shared_init(arm_shared *handle, const uint32_t address, const uint32_t length)
Definition: arm_shared_memory_system.c:32
gpio.h
PIN_CHECK
#define PIN_CHECK(pin)
Definition: pinmap.h:160
gpio_get_direction
gpio_direction_t gpio_get_direction(const io_t pin)
Returns the direction the set pin is initialized in.
Definition: gpio.c:6
GPIO_LEVEL_HIGH
@ GPIO_LEVEL_HIGH
Definition: gpio.h:102
io_t
io_t
Definition: pinmap.h:45
gpio_destroy
void gpio_destroy(void)
Definition: gpio.c:3
pynq_info
#define pynq_info(...)
Definition: log.h:100