write.c 910 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /* See LICENSE of license details. */
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <gd32vf103.h>
  6. #include <unistd.h>
  7. #include <sys/types.h>
  8. #include "stub.h"
  9. #include "gd32vf103.h"
  10. typedef unsigned int size_t;
  11. extern int _put_char(int ch) __attribute__((weak));
  12. ssize_t _write(int fd, const void* ptr, size_t len) {
  13. const uint8_t * current = (const uint8_t *) ptr;
  14. // if (isatty(fd))
  15. {
  16. for (size_t jj = 0; jj < len; jj++) {
  17. _put_char(current[jj]);
  18. if (current[jj] == '\n') {
  19. _put_char('\r');
  20. }
  21. }
  22. return len;
  23. }
  24. return _stub(EBADF);
  25. }
  26. int puts(const char* string) {
  27. return _write(0, (const void *) string, strlen(string));
  28. }
  29. int _put_char(int ch) {
  30. usart_data_transmit(USART0, (uint8_t) ch);
  31. while (usart_flag_get(USART0, USART_FLAG_TBE) == RESET) {
  32. }
  33. return ch;
  34. }