vio_memory.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /******************************************************************************
  2. * @file vio_memory.c
  3. * @brief Virtual I/O implementation using memory only
  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. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdarg.h>
  27. #include "cmsis_vio.h"
  28. #include "RTE_Components.h" // Component selection
  29. #include CMSIS_device_header
  30. // VIO input, output definitions
  31. #define VIO_PRINT_MAX_SIZE 64U // maximum size of print memory
  32. #define VIO_PRINTMEM_NUM 4U // number of print memories
  33. #ifndef VIO_VALUE_NUM
  34. #define VIO_VALUE_NUM 3U // number of values
  35. #endif
  36. #ifndef VIO_VALUEXYZ_NUM
  37. #define VIO_VALUEXYZ_NUM 3U // number of XYZ values
  38. #endif
  39. #ifndef VIO_IPV4_ADDRESS_NUM
  40. #define VIO_IPV4_ADDRESS_NUM 2U // number of IPv4 addresses
  41. #endif
  42. #ifndef VIO_IPV6_ADDRESS_NUM
  43. #define VIO_IPV6_ADDRESS_NUM 2U // number of IPv6 addresses
  44. #endif
  45. // VIO input, output variables
  46. __USED uint32_t vioSignalIn;
  47. __USED uint32_t vioSignalOut;
  48. __USED char vioPrintMem[VIO_PRINTMEM_NUM][VIO_PRINT_MAX_SIZE];
  49. __USED int32_t vioValue [VIO_VALUE_NUM];
  50. __USED vioValueXYZ_t vioValueXYZ[VIO_VALUEXYZ_NUM];
  51. __USED vioAddrIPv4_t vioAddrIPv4[VIO_IPV4_ADDRESS_NUM];
  52. __USED vioAddrIPv6_t vioAddrIPv6[VIO_IPV6_ADDRESS_NUM];
  53. // Initialize test input, output.
  54. void vioInit (void) {
  55. vioSignalIn = 0U;
  56. vioSignalOut = 0U;
  57. memset (vioPrintMem, 0, sizeof(vioPrintMem));
  58. memset (vioValue, 0, sizeof(vioValue));
  59. memset (vioValueXYZ, 0, sizeof(vioValueXYZ));
  60. memset (vioAddrIPv4, 0, sizeof(vioAddrIPv4));
  61. memset (vioAddrIPv6, 0, sizeof(vioAddrIPv6));
  62. }
  63. // Print formated string to test terminal.
  64. int32_t vioPrint (uint32_t level, const char *format, ...) {
  65. va_list args;
  66. int32_t ret;
  67. if (level > vioLevelError) {
  68. return (-1);
  69. }
  70. if (level > VIO_PRINTMEM_NUM) {
  71. return (-1);
  72. }
  73. va_start(args, format);
  74. ret = vsnprintf((char *)vioPrintMem[level], sizeof(vioPrintMem[level]), format, args);
  75. va_end(args);
  76. return (ret);
  77. }
  78. // Set signal output.
  79. void vioSetSignal (uint32_t mask, uint32_t signal) {
  80. vioSignalOut &= ~mask;
  81. vioSignalOut |= mask & signal;
  82. }
  83. // Get signal input.
  84. uint32_t vioGetSignal (uint32_t mask) {
  85. uint32_t signal;
  86. signal = vioSignalIn;
  87. return (signal & mask);
  88. }
  89. // Set value output.
  90. void vioSetValue (uint32_t id, int32_t value) {
  91. uint32_t index = id;
  92. if (index >= VIO_VALUE_NUM) {
  93. return; /* return in case of out-of-range index */
  94. }
  95. vioValue[index] = value;
  96. }
  97. // Get value input.
  98. int32_t vioGetValue (uint32_t id) {
  99. uint32_t index = id;
  100. int32_t value = 0;
  101. if (index >= VIO_VALUE_NUM) {
  102. return value; /* return default in case of out-of-range index */
  103. }
  104. value = vioValue[index];
  105. return value;
  106. }
  107. // Set XYZ value output.
  108. void vioSetXYZ (uint32_t id, vioValueXYZ_t valueXYZ) {
  109. uint32_t index = id;
  110. if (index >= VIO_VALUEXYZ_NUM) {
  111. return; /* return in case of out-of-range index */
  112. }
  113. vioValueXYZ[index] = valueXYZ;
  114. }
  115. // Get XYZ value input.
  116. vioValueXYZ_t vioGetXYZ (uint32_t id) {
  117. uint32_t index = id;
  118. vioValueXYZ_t valueXYZ = {0, 0, 0};
  119. if (index >= VIO_VALUEXYZ_NUM) {
  120. return valueXYZ; /* return default in case of out-of-range index */
  121. }
  122. valueXYZ = vioValueXYZ[index];
  123. return valueXYZ;
  124. }
  125. // Set IPv4 address output.
  126. void vioSetIPv4 (uint32_t id, vioAddrIPv4_t addrIPv4) {
  127. uint32_t index = id;
  128. if (index >= VIO_IPV4_ADDRESS_NUM) {
  129. return; /* return in case of out-of-range index */
  130. }
  131. vioAddrIPv4[index] = addrIPv4;
  132. }
  133. // Get IPv4 address input.
  134. vioAddrIPv4_t vioGetIPv4 (uint32_t id) {
  135. uint32_t index = id;
  136. vioAddrIPv4_t addrIPv4 = {0U, 0U, 0U, 0U};
  137. if (index >= VIO_IPV4_ADDRESS_NUM) {
  138. return addrIPv4; /* return default in case of out-of-range index */
  139. }
  140. addrIPv4 = vioAddrIPv4[index];
  141. return addrIPv4;
  142. }
  143. // Set IPv6 address output.
  144. void vioSetIPv6 (uint32_t id, vioAddrIPv6_t addrIPv6) {
  145. uint32_t index = id;
  146. if (index >= VIO_IPV6_ADDRESS_NUM) {
  147. return; /* return in case of out-of-range index */
  148. }
  149. vioAddrIPv6[index] = addrIPv6;
  150. }
  151. // Get IPv6 address input.
  152. vioAddrIPv6_t vioGetIPv6 (uint32_t id) {
  153. uint32_t index = id;
  154. vioAddrIPv6_t addrIPv6 = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U,
  155. 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U};
  156. if (index >= VIO_IPV6_ADDRESS_NUM) {
  157. return addrIPv6; /* return default in case of out-of-range index */
  158. }
  159. addrIPv6 = vioAddrIPv6[index];
  160. return addrIPv6;
  161. }