syscalls.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /*
  14. * **** This file incorporates work covered by the following copyright and ****
  15. * **** permission notice: ****
  16. *
  17. * Copyright (c) 2009 by Michael Fischer. All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. *
  23. * 1. Redistributions of source code must retain the above copyright
  24. * notice, this list of conditions and the following disclaimer.
  25. * 2. Redistributions in binary form must reproduce the above copyright
  26. * notice, this list of conditions and the following disclaimer in the
  27. * documentation and/or other materials provided with the distribution.
  28. * 3. Neither the name of the author nor the names of its contributors may
  29. * be used to endorse or promote products derived from this software
  30. * without specific prior written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  33. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  34. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  35. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  36. * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  37. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  38. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  39. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  40. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  41. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  42. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  43. * SUCH DAMAGE.
  44. *
  45. ****************************************************************************
  46. * History:
  47. *
  48. * 28.03.09 mifi First Version, based on the original syscall.c from
  49. * newlib version 1.17.0
  50. * 17.08.09 gdisirio Modified the file for use under ChibiOS/RT
  51. * 15.11.09 gdisirio Added read and write handling
  52. ****************************************************************************/
  53. #include <stdlib.h>
  54. #include <errno.h>
  55. #include <string.h>
  56. #include <sys/stat.h>
  57. #include <sys/types.h>
  58. #include "ch.h"
  59. #if defined(STDOUT_SD) || defined(STDIN_SD)
  60. #include "hal.h"
  61. #endif
  62. /***************************************************************************/
  63. __attribute__((used))
  64. int _read_r(struct _reent *r, int file, char * ptr, int len)
  65. {
  66. (void)r;
  67. #if defined(STDIN_SD)
  68. if (!len || (file != 0)) {
  69. __errno_r(r) = EINVAL;
  70. return -1;
  71. }
  72. len = sdRead(&STDIN_SD, (uint8_t *)ptr, (size_t)len);
  73. return len;
  74. #else
  75. (void)file;
  76. (void)ptr;
  77. (void)len;
  78. __errno_r(r) = EINVAL;
  79. return -1;
  80. #endif
  81. }
  82. /***************************************************************************/
  83. __attribute__((used))
  84. int _lseek_r(struct _reent *r, int file, int ptr, int dir)
  85. {
  86. (void)r;
  87. (void)file;
  88. (void)ptr;
  89. (void)dir;
  90. return 0;
  91. }
  92. /***************************************************************************/
  93. __attribute__((used))
  94. int _write_r(struct _reent *r, int file, char * ptr, int len)
  95. {
  96. (void)r;
  97. (void)file;
  98. (void)ptr;
  99. #if defined(STDOUT_SD)
  100. if (file != 1) {
  101. __errno_r(r) = EINVAL;
  102. return -1;
  103. }
  104. sdWrite(&STDOUT_SD, (uint8_t *)ptr, (size_t)len);
  105. #endif
  106. return len;
  107. }
  108. /***************************************************************************/
  109. __attribute__((used))
  110. int _close_r(struct _reent *r, int file)
  111. {
  112. (void)r;
  113. (void)file;
  114. return 0;
  115. }
  116. /***************************************************************************/
  117. __attribute__((used))
  118. caddr_t _sbrk_r(struct _reent *r, int incr)
  119. {
  120. #if CH_CFG_USE_MEMCORE
  121. void *p;
  122. chDbgCheck(incr >= 0);
  123. p = chCoreAlloc((size_t)incr);
  124. if (p == NULL) {
  125. __errno_r(r) = ENOMEM;
  126. return (caddr_t)-1;
  127. }
  128. return (caddr_t)p;
  129. #else
  130. (void)incr;
  131. __errno_r(r) = ENOMEM;
  132. return (caddr_t)-1;
  133. #endif
  134. }
  135. /***************************************************************************/
  136. __attribute__((used))
  137. int _fstat_r(struct _reent *r, int file, struct stat * st)
  138. {
  139. (void)r;
  140. (void)file;
  141. memset(st, 0, sizeof(*st));
  142. st->st_mode = S_IFCHR;
  143. return 0;
  144. }
  145. /***************************************************************************/
  146. __attribute__((used))
  147. int _isatty_r(struct _reent *r, int fd)
  148. {
  149. (void)r;
  150. (void)fd;
  151. return 1;
  152. }
  153. /*** EOF ***/