libpynq (release 5EWC0-2023 version 0.2.4 of 2023-10-07 15:07)
Loading...
Searching...
No Matches
switchbox.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 "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
97volatile uint32_t *ioswitch = NULL;
98
99typedef struct {
100 char *name;
101 char *state;
103} pin;
104
105void switchbox_init(void) {
106 // allocate shared memory for the switchbox and store the pointer in
107 // `ioswitch`
109 ioswitch = arm_shared_init(&ioswitch_handle, io_switch_0, 4096);
110}
111
113 // free the sared memory in the switchbox
115}
116
117// reset all switchbox pins to 0
118void 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
126void 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]);
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}
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:95
bool gpio_is_initialized(void)
Definition gpio.c:35
void gpio_set_direction(const io_t pin, const gpio_direction_t dir)
Set the IO pin as in input or output.
Definition gpio.c:81
@ GPIO_DIR_INPUT
Definition gpio.h:90
#define pynq_warning(...)
Definition log.h:109
io_t
Definition pinmap.h:45
#define PIN_CHECK(pin)
Definition pinmap.h:160
char *const pin_names[]
Pin names.
Definition pinmap.c:24
void switchbox_destroy(void)
Resets all pins of the switch box to be input.
Definition switchbox.c:112
io_configuration_t switchbox_get_pin(const io_t pin_number)
Sets the mode of a specified pin.
Definition switchbox.c:162
#define NUM_SWITCHBOX_NAMES
Definition switchbox.h:135
void switchbox_reset(void)
Resets all pins of the switch box to be input.
Definition switchbox.c:118
void switchbox_init(void)
Initializes the switch box.
Definition switchbox.c:105
char *const switchbox_names[NUM_SWITCHBOX_NAMES]
Taken from scpi_names.h, lookup table for channels in the mapping_info function.
Definition switchbox.c:25
void switchbox_set_pin(const io_t pin_number, const io_configuration_t io_type)
Set the type of a switch pin.
Definition switchbox.c:126
enum io_configuration io_configuration_t
@ SWB_GPIO
Definition switchbox.h:64
void check_version(void)
Definition version.c:68
char * state
Definition switchbox.c:101
char * name
Definition switchbox.c:100
io_configuration_t channel
Definition switchbox.c:102
volatile uint32_t * ioswitch
Definition switchbox.c:97
arm_shared ioswitch_handle
Definition switchbox.c:96