libpynq  (release 5EID0-2023 version 0.3.0 of 2024-04-25 09:42 )
util.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 <libpynq.h>
23 #include <unistd.h>
24 
25 typedef struct {
26  char *name;
28  uint8_t channel;
29  char *level;
30 } pin_state_t;
31 
32 void sleep_msec(int msec) {
33  if (msec > 0)
34  usleep(msec * 1000);
35 }
36 
37 void mapping_info(void) {
38  const char *const dir[2] = {"Input", "Output"};
39  printf("Pin\tName\tI/O\tLevel\tChannel\tCh_Name\t\tState\n");
40  for (int i = 0; i < IO_NUM_PINS; i++) {
41  pin_state_t pin_array = {
42  0,
43  };
44  pin_array.name = pin_names[i];
45  pin_array.state = gpio_get_direction(i);
46  if (gpio_get_level(i) == GPIO_LEVEL_HIGH) {
47  pin_array.level = "high";
48  } else if (gpio_get_level(i) == GPIO_LEVEL_LOW) {
49  pin_array.level = "low";
50  } else {
51  pin_array.level = "undef";
52  }
53  // get the index of the channel the pin is mapped to, 0 for none
54  pin_array.channel = switchbox_get_pin(i);
55 
56  printf("%i\t%s\t%s\t%s\t%u\t", i, pin_array.name, dir[pin_array.state],
57  pin_array.level, pin_array.channel);
58 
59  printf("%s\t", switchbox_names[pin_array.channel]);
60  if (pin_array.channel != SWB_GPIO && pin_array.state != GPIO_DIR_INPUT) {
61  printf("Invalid\n");
62  } else {
63  printf("Valid\n");
64  }
65  }
66 }
switchbox_get_pin
uint8_t switchbox_get_pin(const pin_t pin_number)
Definition: switchbox.c:7
SWB_GPIO
@ SWB_GPIO
Definition: switchbox.h:64
gpio_direction_t
gpio_direction_t
Definition: gpio.h:88
sleep_msec
void sleep_msec(int msec)
Wait for msec milliseconds.
Definition: util.c:2
pin_state_t
Definition: util.c:25
pin_names
char *const pin_names[]
Pin names.
Definition: pinmap.c:24
pin_state_t::name
char * name
Definition: util.c:26
GPIO_LEVEL_LOW
@ GPIO_LEVEL_LOW
Definition: gpio.h:100
pin_state_t::state
gpio_direction_t state
Definition: util.c:27
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_DIR_INPUT
@ GPIO_DIR_INPUT
Definition: gpio.h:90
mapping_info
void mapping_info(void)
Displays a table to see where all pins have been mapped, what channels have been linked where and the...
Definition: util.c:3
pin_state_t::channel
uint8_t channel
Definition: util.c:28
switchbox_names
char *const switchbox_names[NUM_SWITCHBOX_NAMES]
Taken from scpi_names.h, lookup table for channels in the mapping_info function.
Definition: switchbox.c:2
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
pin_state_t::level
char * level
Definition: util.c:29
libpynq.h
IO_NUM_PINS
@ IO_NUM_PINS
Definition: pinmap.h:144