libpynq (release 5EWC0-2023 version 0.2.0 of 2023-08-28 20:33)
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 int32_t pin_number, const uint8_t pin_type) {
129 int numWordstoPass, byteNumber;
130 uint32_t switchConfigValue;
131
132 // calculate the word and byte number for the given pin number
133 numWordstoPass = pin_number / 4;
134 byteNumber = pin_number % 4;
135
136 // get the current value of the word containing the pin
137 switchConfigValue = ioswitch[numWordstoPass];
138
139 // clear the byte containing the pin type and set it to the new value
140 switchConfigValue = (switchConfigValue & (~(0xFF << (byteNumber * 8)))) |
141 (pin_type << (byteNumber * 8));
142
143 // update the word in the switchbox with the new value
144 ioswitch[numWordstoPass] = switchConfigValue;
145}
146
147// pin_number: the number of the pin to get
148// returns: the type of the given pin
149uint8_t switchbox_get_pin(const int32_t pin_number) {
150 int numWordstoPass, byteNumber;
151 uint32_t switchConfigValue;
152
153 // calculate the word and byte number for the given pin number
154 numWordstoPass = pin_number / 4;
155 byteNumber = pin_number % 4;
156
157 // get the value of the word containing the pin and extract the value of the
158 // byte containing the pin type
159 switchConfigValue = ioswitch[numWordstoPass];
160 switchConfigValue = (switchConfigValue >> (byteNumber * 8)) & 0xFF;
161
162 // return pintype
163 return switchConfigValue;
164}
void arm_shared_close(arm_shared *handle)
void * arm_shared_init(arm_shared *handle, const uint32_t address, const uint32_t length)
void switchbox_destroy(void)
Resets all pins of the switch box to be input.
Definition switchbox.c:6
uint8_t switchbox_get_pin(const int32_t pin_number)
Sets the mode of a specified pin.
Definition switchbox.c:7
void switchbox_set_pin(const int32_t pin_number, const uint8_t pin_type)
Set the type of a switch pin.
Definition switchbox.c:4
#define NUM_SWITCHBOX_NAMES
Definition switchbox.h:133
void switchbox_reset(void)
Resets all pins of the switch box to be input.
Definition switchbox.c:5
void switchbox_init(void)
Initializes the switch box.
Definition switchbox.c:3
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
void check_version(void)
Definition version.c:19
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