libpynq (release 5EWC0-2023 version 0.2.2 of 2023-09-24 22:22)
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;
102 uint8_t channel;
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
126// pin_number: the number of the pin to set.
127// pin_type: the type to set the pin to (0 or 1).
128void switchbox_set_pin(const pin_t pin_number, const uint8_t pin_type) {
129 int numWordstoPass, byteNumber;
130 uint32_t switchConfigValue;
131
132 PIN_CHECK(pin_number);
133
134 // If gpio is initialized, set the pin as input, if PIN_TYPE is
135 // not gpio
136 if (pin_type != SWB_GPIO && gpio_is_initialized()) {
137 // set pin as input.
138 if (gpio_get_direction(pin_number) != GPIO_DIR_INPUT) {
139 pynq_warning("pin: %s is set as GPIO ouput, but not mapped as GPIO. "
140 "Reconfiguring as input.",
141 pin_names[pin_number]);
143 }
144 }
145
146 // calculate the word and byte number for the given pin number
147 numWordstoPass = pin_number / 4;
148 byteNumber = pin_number % 4;
149
150 // get the current value of the word containing the pin
151 switchConfigValue = ioswitch[numWordstoPass];
152
153 // clear the byte containing the pin type and set it to the new value
154 switchConfigValue = (switchConfigValue & (~(0xFF << (byteNumber * 8)))) |
155 (pin_type << (byteNumber * 8));
156
157 // update the word in the switchbox with the new value
158 ioswitch[numWordstoPass] = switchConfigValue;
159}
160
161// pin_number: the number of the pin to get
162// returns: the type of the given pin
163uint8_t switchbox_get_pin(const pin_t pin_number) {
164 int numWordstoPass, byteNumber;
165 uint32_t switchConfigValue;
166
167 PIN_CHECK(pin_number);
168
169 // calculate the word and byte number for the given pin number
170 numWordstoPass = pin_number / 4;
171 byteNumber = pin_number % 4;
172
173 // get the value of the word containing the pin and extract the value of the
174 // byte containing the pin type
175 switchConfigValue = ioswitch[numWordstoPass];
176 switchConfigValue = (switchConfigValue >> (byteNumber * 8)) & 0xFF;
177
178 // return pintype
179 return switchConfigValue;
180}
void arm_shared_close(arm_shared *handle)
void * arm_shared_init(arm_shared *handle, const uint32_t address, const uint32_t length)
bool gpio_is_initialized(void)
Definition gpio.c:35
gpio_direction_t gpio_get_direction(const pin_t pin)
Returns the direction the set pin is initialized in.
Definition gpio.c:95
void gpio_set_direction(const pin_t pin, const gpio_direction_t dir)
Set the GPIO pin as in input or output.
Definition gpio.c:81
@ GPIO_DIR_INPUT
Definition gpio.h:83
#define pynq_warning(...)
Definition log.h:109
#define PIN_CHECK(pin)
Definition pinmap.h:150
pin_t
Definition pinmap.h:45
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
#define NUM_SWITCHBOX_NAMES
Definition switchbox.h:134
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
uint8_t switchbox_get_pin(const pin_t pin_number)
Sets the mode of a specified pin.
Definition switchbox.c:163
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 pin_t pin_number, const uint8_t pin_type)
Set the type of a switch pin.
Definition switchbox.c:128
@ SWB_GPIO
Definition switchbox.h:63
void check_version(void)
Definition version.c:68
char * state
Definition switchbox.c:101
uint8_t channel
Definition switchbox.c:102
char * name
Definition switchbox.c:100
volatile uint32_t * ioswitch
Definition switchbox.c:97
arm_shared ioswitch_handle
Definition switchbox.c:96