console.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. * @file console.c
  15. * @brief Simulator console driver code.
  16. * @{
  17. */
  18. #include <stdio.h>
  19. #include "ch.h"
  20. #include "hal.h"
  21. #include "console.h"
  22. /*===========================================================================*/
  23. /* Driver exported variables. */
  24. /*===========================================================================*/
  25. /**
  26. * @brief Console driver 1.
  27. */
  28. BaseChannel CD1;
  29. /*===========================================================================*/
  30. /* Driver local variables. */
  31. /*===========================================================================*/
  32. /*===========================================================================*/
  33. /* Driver local functions. */
  34. /*===========================================================================*/
  35. static size_t _write(void *ip, const uint8_t *bp, size_t n) {
  36. size_t ret;
  37. (void)ip;
  38. ret = fwrite(bp, 1, n, stdout);
  39. fflush(stdout);
  40. return ret;
  41. }
  42. static size_t _read(void *ip, uint8_t *bp, size_t n) {
  43. (void)ip;
  44. return fread(bp, 1, n, stdin);
  45. }
  46. static msg_t _put(void *ip, uint8_t b) {
  47. (void)ip;
  48. fputc(b, stdout);
  49. fflush(stdout);
  50. return MSG_OK;
  51. }
  52. static msg_t _get(void *ip) {
  53. (void)ip;
  54. return fgetc(stdin);
  55. }
  56. static msg_t _putt(void *ip, uint8_t b, sysinterval_t time) {
  57. (void)ip;
  58. (void)time;
  59. fputc(b, stdout);
  60. fflush(stdout);
  61. return MSG_OK;
  62. }
  63. static msg_t _gett(void *ip, sysinterval_t time) {
  64. (void)ip;
  65. (void)time;
  66. return fgetc(stdin);
  67. }
  68. static size_t _writet(void *ip, const uint8_t *bp, size_t n, sysinterval_t time) {
  69. size_t ret;
  70. (void)ip;
  71. (void)time;
  72. ret = fwrite(bp, 1, n, stdout);
  73. fflush(stdout);
  74. return ret;
  75. }
  76. static size_t _readt(void *ip, uint8_t *bp, size_t n, sysinterval_t time) {
  77. (void)ip;
  78. (void)time;
  79. return fread(bp, 1, n, stdin);
  80. }
  81. static msg_t _ctl(void *ip, unsigned int operation, void *arg) {
  82. (void)ip;
  83. (void)operation;
  84. (void)arg;
  85. return MSG_OK;
  86. }
  87. static const struct BaseChannelVMT vmt = {
  88. (size_t)0,
  89. _write, _read, _put, _get,
  90. _putt, _gett, _writet, _readt,
  91. _ctl
  92. };
  93. /*===========================================================================*/
  94. /* Driver interrupt handlers. */
  95. /*===========================================================================*/
  96. /*===========================================================================*/
  97. /* Driver exported functions. */
  98. /*===========================================================================*/
  99. void conInit(void) {
  100. CD1.vmt = &vmt;
  101. }
  102. /** @} */