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