libpynq  (release 5EID0-2023 version 0.3.0 of 2024-04-25 09:42 )
switchbox.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 "switchbox.h"
23 #include <libpynq.h>
24 
27  "SWB_GPIO",
29  "SWB_Interrupt_In",
31  "SWB_UART0_TX",
33  "SWB_UART0_RX",
35  "SWB_SPI0_CLK",
37  "SWB_SPI0_MISO",
39  "SWB_SPI0_MOSI",
41  "SWB_SPI0_SS",
43  "SWB_SPI1_CLK",
45  "SWB_SPI1_MISO",
47  "SWB_SPI1_MOSI",
49  "SWB_SPI1_SS",
51  "SWB_IIC0_SDA",
53  "SWB_IIC0_SCL",
55  "SWB_IIC1_SDA",
57  "SWB_IIC1_SCL",
59  "SWB_PWM0",
61  "SWB_PWM1",
63  "SWB_PWM2",
65  "SWB_PWM3",
67  "SWB_PWM4",
69  "SWB_PWM5",
70  "SWB_TIMER_G0",
71  "SWB_TIMER_G1",
73  "SWB_TIMER_G2",
75  "SWB_TIMER_G3",
77  "SWB_TIMER_G4",
79  "SWB_TIMER_G5",
81  "SWB_TIMER_G6",
83  "SWB_TIMER_G7",
84  "SWB_UART1_TX",
85  "SWB_UART1_RX",
86  "SWB_TIMER_IC0",
87  "SWB_TIMER_IC1",
88  "SWB_TIMER_IC2",
89  "SWB_TIMER_IC3",
90  "SWB_TIMER_IC4",
91  "SWB_TIMER_IC5",
92  "SWB_TIMER_IC6",
93  "SWB_TIMER_IC7",
94 };
95 
97 volatile uint32_t *ioswitch = NULL;
98 
99 typedef struct {
100  char *name;
101  char *state;
102  io_configuration_t channel; // was uint8_t
103 } pin;
104 
105 void switchbox_init(void) {
106  // allocate shared memory for the switchbox and store the pointer in
107  // `ioswitch`
108  check_version();
109  ioswitch = arm_shared_init(&ioswitch_handle, io_switch_0, 4096);
110 }
111 
112 void switchbox_destroy(void) {
113  // free the sared memory in the switchbox
115 }
116 
117 // reset all switchbox pins to 0
118 void switchbox_reset(void) {
119  // 32 pins to remap, 4 per word.
120  for (uint_fast32_t i = 0; i < (64 / 4); i++) {
121  // set all words to 0
122  ioswitch[i] = 0;
123  }
124 }
125 
126 void switchbox_set_pin(const io_t pin_number,
127  const io_configuration_t io_type) {
128  int numWordstoPass, byteNumber;
129  uint32_t switchConfigValue;
130 
131  PIN_CHECK(pin_number);
132 
133  // If gpio is initialized, set the pin as input, if PIN_TYPE is
134  // not gpio
135  if (io_type != SWB_GPIO && gpio_is_initialized()) {
136  // set pin as input.
137  if (gpio_get_direction(pin_number) != GPIO_DIR_INPUT) {
138  pynq_warning("pin: %s is set as GPIO ouput, but not mapped as GPIO. "
139  "Reconfiguring as input.",
140  pin_names[pin_number]);
141  gpio_set_direction(pin_number, GPIO_DIR_INPUT);
142  }
143  }
144 
145  // calculate the word and byte number for the given pin number
146  numWordstoPass = pin_number / 4;
147  byteNumber = pin_number % 4;
148 
149  // get the current value of the word containing the pin
150  switchConfigValue = ioswitch[numWordstoPass];
151 
152  // clear the byte containing the pin type and set it to the new value
153  switchConfigValue = (switchConfigValue & (~(0xFF << (byteNumber * 8)))) |
154  (io_type << (byteNumber * 8));
155 
156  // update the word in the switchbox with the new value
157  ioswitch[numWordstoPass] = switchConfigValue;
158 }
159 
160 // pin_number: the number of the pin to get
161 // returns: the type of the given pin
163  int numWordstoPass, byteNumber;
164  uint32_t switchConfigValue;
165 
166  PIN_CHECK(pin_number);
167 
168  // calculate the word and byte number for the given pin number
169  numWordstoPass = pin_number / 4;
170  byteNumber = pin_number % 4;
171 
172  // get the value of the word containing the pin and extract the value of the
173  // byte containing the pin type
174  switchConfigValue = ioswitch[numWordstoPass];
175  switchConfigValue = (switchConfigValue >> (byteNumber * 8)) & 0xFF;
176 
177  // return pintype
178  return switchConfigValue;
179 }
switchbox_get_pin
uint8_t switchbox_get_pin(const pin_t pin_number)
Definition: switchbox.c:7
check_version
void check_version(void)
Definition: version.c:19
SWB_GPIO
@ SWB_GPIO
Definition: switchbox.h:64
switchbox_set_pin
void switchbox_set_pin(const pin_t pin_number, const uint8_t pin_type)
Definition: switchbox.c:4
NUM_SWITCHBOX_NAMES
#define NUM_SWITCHBOX_NAMES
Definition: switchbox.h:135
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
switchbox.h
pin_names
char *const pin_names[]
Pin names.
Definition: pinmap.c:24
switchbox_init
void switchbox_init(void)
Initializes the switch box.
Definition: switchbox.c:3
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
switchbox_reset
void switchbox_reset(void)
Resets all pins of the switch box to be input.
Definition: switchbox.c:5
pin::name
char * name
Definition: switchbox.c:100
GPIO_DIR_INPUT
@ GPIO_DIR_INPUT
Definition: gpio.h:90
gpio_is_initialized
bool gpio_is_initialized(void)
Definition: gpio.c:35
pin
Definition: switchbox.c:99
pynq_warning
#define pynq_warning(...)
Definition: log.h:109
pin::channel
io_configuration_t channel
Definition: switchbox.c:102
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
switchbox_destroy
void switchbox_destroy(void)
Resets all pins of the switch box to be input.
Definition: switchbox.c:6
pin::state
char * state
Definition: switchbox.c:101
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
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
ioswitch_handle
arm_shared ioswitch_handle
Definition: switchbox.c:96
ioswitch
volatile uint32_t * ioswitch
Definition: switchbox.c:97
io_configuration_t
enum io_configuration io_configuration_t
io_t
io_t
Definition: pinmap.h:45
libpynq.h