write.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* See LICENSE of license details. */
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <gd32vf103.h>
  6. #if defined (__GNUC__)
  7. #include <unistd.h>
  8. #include <sys/types.h>
  9. #endif
  10. #include "stub.h"
  11. #include "gd32vf103.h"
  12. //typedef unsigned int size_t;
  13. extern int _put_char(int ch) __attribute__((weak));
  14. #if defined (__ICCRISCV__)
  15. size_t __write(int handle, const unsigned char *buf, size_t bufSize)
  16. {
  17. size_t nChars = 0;
  18. if (handle == -1)
  19. {
  20. return 0;
  21. }
  22. for (; bufSize > 0; --bufSize)
  23. {
  24. _put_char((uint8_t) *buf);
  25. ++buf;
  26. ++nChars;
  27. }
  28. return nChars;
  29. }
  30. int puts(const char* string) {
  31. return __write(0, (const void *) string, strlen(string));
  32. }
  33. #elif defined ( __GNUC__ )
  34. ssize_t _write(int fd, const void* ptr, size_t len) {
  35. const uint8_t * current = (const uint8_t *) ptr;
  36. {
  37. for (size_t jj = 0; jj < len; jj++) {
  38. _put_char(current[jj]);
  39. if (current[jj] == '\n') {
  40. _put_char('\r');
  41. }
  42. }
  43. return len;
  44. }
  45. return _stub(EBADF);
  46. }
  47. int puts(const char* string) {
  48. return _write(0, (const void *) string, strlen(string));
  49. }
  50. #endif
  51. int _put_char(int ch)
  52. {
  53. usart_data_transmit(USART0, (uint8_t) ch );
  54. while (usart_flag_get(USART0, USART_FLAG_TBE)== RESET){
  55. }
  56. return ch;
  57. }