1 /******************************************************************************
2  * @file     vio.c
3  * @brief    Virtual I/O implementation template
4  * @version  V1.0.0
5  * @date     23. March 2020
6  ******************************************************************************/
7 /*
8  * Copyright (c) 2019-2020 Arm Limited. All rights reserved.
9  *
10  * SPDX-License-Identifier: Apache-2.0
11  *
12  * Licensed under the Apache License, Version 2.0 (the License); you may
13  * not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  * www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
20  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  */
24 
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include "cmsis_vio.h"
29 
30 #include "RTE_Components.h"             // Component selection
31 #include CMSIS_device_header
32 
33 #if !defined CMSIS_VOUT || !defined CMSIS_VIN
34 // Add user includes here:
35 
36 #endif
37 
38 // VIO input, output definitions
39 #define VIO_PRINT_MAX_SIZE      64U     // maximum size of print memory
40 #define VIO_PRINTMEM_NUM         4U     // number of print memories
41 #define VIO_VALUE_NUM            3U     // number of values
42 #define VIO_VALUEXYZ_NUM         3U     // number of XYZ values
43 #define VIO_IPV4_ADDRESS_NUM     2U     // number of IPv4 addresses
44 #define VIO_IPV6_ADDRESS_NUM     2U     // number of IPv6 addresses
45 
46 // VIO input, output variables
47 __USED uint32_t      vioSignalIn;                                       // Memory for incoming signal
48 __USED uint32_t      vioSignalOut;                                      // Memory for outgoing signal
49 __USED char          vioPrintMem[VIO_PRINTMEM_NUM][VIO_PRINT_MAX_SIZE]; // Memory for the last value for each level
50 __USED int32_t       vioValue   [VIO_VALUE_NUM];                        // Memory for value used in vioGetValue/vioSetValue
51 __USED vioValueXYZ_t vioValueXYZ[VIO_VALUEXYZ_NUM];                     // Memory for XYZ value for 3-D vector
52 __USED vioAddrIPv4_t vioAddrIPv4[VIO_IPV4_ADDRESS_NUM];                 // Memory for IPv4 address value used in vioSetIPv4/vioGetIPv4
53 __USED vioAddrIPv6_t vioAddrIPv6[VIO_IPV6_ADDRESS_NUM];                 // Memory for IPv6 address value used in vioSetIPv6/vioGetIPv6
54 
55 #if !defined CMSIS_VOUT
56 // Add global user types, variables, functions here:
57 
58 #endif
59 
60 #if !defined CMSIS_VIN
61 // Add global user types, variables, functions here:
62 
63 #endif
64 
65 // Initialize test input, output.
vioInit(void)66 void vioInit (void) {
67 #if !defined CMSIS_VOUT
68 // Add user variables here:
69 
70 #endif
71 #if !defined CMSIS_VIN
72 // Add user variables here:
73 
74 #endif
75 
76   vioSignalIn  = 0U;
77   vioSignalOut = 0U;
78 
79   memset (vioPrintMem, 0, sizeof(vioPrintMem));
80   memset (vioValue,    0, sizeof(vioValue));
81   memset (vioValueXYZ, 0, sizeof(vioValueXYZ));
82   memset (vioAddrIPv4, 0, sizeof(vioAddrIPv4));
83   memset (vioAddrIPv6, 0, sizeof(vioAddrIPv6));
84 
85 #if !defined CMSIS_VOUT
86 // Add user code here:
87 
88 #endif
89 
90 #if !defined CMSIS_VIN
91 // Add user code here:
92 
93 #endif
94 }
95 
96 // Print formated string to test terminal.
vioPrint(uint32_t level,const char * format,...)97 int32_t vioPrint (uint32_t level, const char *format, ...) {
98   va_list args;
99   int32_t ret;
100 #if !defined CMSIS_VOUT
101 // Add user variables here:
102 
103 #endif
104 
105   if (level > vioLevelError) {
106     return (-1);
107   }
108 
109   if (level > VIO_PRINTMEM_NUM) {
110     return (-1);
111   }
112 
113   va_start(args, format);
114 
115   ret = vsnprintf((char *)vioPrintMem[level], sizeof(vioPrintMem[level]), format, args);
116 
117   va_end(args);
118 
119 #if !defined CMSIS_VOUT
120 // Add user code here:
121 
122 #endif
123 
124   return (ret);
125 }
126 
127 // Set signal output.
vioSetSignal(uint32_t mask,uint32_t signal)128 void vioSetSignal (uint32_t mask, uint32_t signal) {
129 #if !defined CMSIS_VOUT
130 // Add user variables here:
131 
132 #endif
133 
134   vioSignalOut &= ~mask;
135   vioSignalOut |=  mask & signal;
136 
137 #if !defined CMSIS_VOUT
138 // Add user code here:
139 
140 #endif
141 }
142 
143 // Get signal input.
vioGetSignal(uint32_t mask)144 uint32_t vioGetSignal (uint32_t mask) {
145   uint32_t signal;
146 #if !defined CMSIS_VIN
147 // Add user variables here:
148 
149 #endif
150 
151 #if !defined CMSIS_VIN
152 // Add user code here:
153 
154 //   vioSignalIn = ...;
155 #endif
156 
157   signal = vioSignalIn;
158 
159   return (signal & mask);
160 }
161 
162 // Set value output.
vioSetValue(uint32_t id,int32_t value)163 void vioSetValue (uint32_t id, int32_t value) {
164   uint32_t index = id;
165 #if !defined CMSIS_VOUT
166 // Add user variables here:
167 
168 #endif
169 
170   if (index >= VIO_VALUE_NUM) {
171     return;                             /* return in case of out-of-range index */
172   }
173 
174   vioValue[index] = value;
175 
176 #if !defined CMSIS_VOUT
177 // Add user code here:
178 
179 #endif
180 }
181 
182 // Get value input.
vioGetValue(uint32_t id)183 int32_t vioGetValue (uint32_t id) {
184   uint32_t index = id;
185   int32_t  value = 0;
186 #if !defined CMSIS_VIN
187 // Add user variables here:
188 
189 #endif
190 
191   if (index >= VIO_VALUE_NUM) {
192     return value;                       /* return default in case of out-of-range index */
193   }
194 
195 #if !defined CMSIS_VIN
196 // Add user code here:
197 
198 //   vioValue[index] = ...;
199 #endif
200 
201   value = vioValue[index];
202 
203   return value;
204 }
205 
206 // Set XYZ value output.
vioSetXYZ(uint32_t id,vioValueXYZ_t valueXYZ)207 void vioSetXYZ (uint32_t id, vioValueXYZ_t valueXYZ) {
208   uint32_t index = id;
209 #if !defined CMSIS_VOUT
210 // Add user variables here:
211 
212 #endif
213 
214   if (index >= VIO_VALUEXYZ_NUM) {
215     return;                             /* return in case of out-of-range index */
216   }
217 
218   vioValueXYZ[index] = valueXYZ;
219 
220 #if !defined CMSIS_VOUT
221 // Add user code here:
222 
223 #endif
224 }
225 
226 // Get XYZ value input.
vioGetXYZ(uint32_t id)227 vioValueXYZ_t vioGetXYZ (uint32_t id) {
228   uint32_t index = id;
229   vioValueXYZ_t valueXYZ = {0, 0, 0};
230 #if !defined CMSIS_VIN
231 // Add user variables here:
232 
233 #endif
234 
235   if (index >= VIO_VALUEXYZ_NUM) {
236     return valueXYZ;                    /* return default in case of out-of-range index */
237   }
238 
239 #if !defined CMSIS_VIN
240 // Add user code here:
241 
242 //   vioValueXYZ[index] = ...;
243 #endif
244 
245   valueXYZ = vioValueXYZ[index];
246 
247   return valueXYZ;
248 }
249 
250 // Set IPv4 address output.
vioSetIPv4(uint32_t id,vioAddrIPv4_t addrIPv4)251 void vioSetIPv4 (uint32_t id, vioAddrIPv4_t addrIPv4) {
252   uint32_t index = id;
253 #if !defined CMSIS_VOUT
254 // Add user variables here:
255 
256 #endif
257 
258   if (index >= VIO_IPV4_ADDRESS_NUM) {
259     return;                             /* return in case of out-of-range index */
260   }
261 
262   vioAddrIPv4[index] = addrIPv4;
263 
264 #if !defined CMSIS_VOUT
265 // Add user code here:
266 
267 #endif
268 }
269 
270 // Get IPv4 address input.
vioGetIPv4(uint32_t id)271 vioAddrIPv4_t vioGetIPv4 (uint32_t id) {
272   uint32_t index = id;
273   vioAddrIPv4_t addrIPv4 = {0U, 0U, 0U, 0U};
274 #if !defined CMSIS_VIN
275 // Add user variables here:
276 
277 #endif
278 
279   if (index >= VIO_IPV4_ADDRESS_NUM) {
280     return addrIPv4;                    /* return default in case of out-of-range index */
281   }
282 
283 #if !defined CMSIS_VIN
284 // Add user code here:
285 
286 //   vioAddrIPv4[index] = ...;
287 #endif
288 
289   addrIPv4 = vioAddrIPv4[index];
290 
291   return addrIPv4;
292 }
293 
294 // Set IPv6 address output.
vioSetIPv6(uint32_t id,vioAddrIPv6_t addrIPv6)295 void vioSetIPv6 (uint32_t id, vioAddrIPv6_t addrIPv6) {
296   uint32_t index = id;
297 #if !defined CMSIS_VOUT
298 // Add user variables here:
299 
300 #endif
301 
302   if (index >= VIO_IPV6_ADDRESS_NUM) {
303     return;                             /* return in case of out-of-range index */
304   }
305 
306   vioAddrIPv6[index] = addrIPv6;
307 
308 #if !defined CMSIS_VOUT
309 // Add user code here:
310 
311 #endif
312 }
313 
314 // Get IPv6 address input.
vioGetIPv6(uint32_t id)315 vioAddrIPv6_t vioGetIPv6 (uint32_t id) {
316   uint32_t index = id;
317   vioAddrIPv6_t addrIPv6 = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U,
318                             0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U};
319 #if !defined CMSIS_VIN
320 // Add user variables here:
321 
322 #endif
323 
324   if (index >= VIO_IPV6_ADDRESS_NUM) {
325     return addrIPv6;                    /* return default in case of out-of-range index */
326   }
327 
328 #if !defined CMSIS_VIN
329 // Add user code here:
330 
331 //   vioAddrIPv6[index] = ...;
332 #endif
333 
334   addrIPv6 = vioAddrIPv6[index];
335 
336   return addrIPv6;
337 }
338