summaryrefslogtreecommitdiff
path: root/bsp/gd32/risc-v/libraries/GD32VF103_Firmware_Library/RISCV/stubs/write.c
blob: d1916f0427ed2e5e3475707de7dcdd22dbecdaec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* See LICENSE of license details. */

#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <gd32vf103.h>

#if   defined (__GNUC__)
#include <unistd.h>
#include <sys/types.h>
#endif

#include "stub.h"
#include "gd32vf103.h"

//typedef unsigned int size_t;
extern int _put_char(int ch) __attribute__((weak));

#if   defined (__ICCRISCV__)
size_t __write(int handle, const unsigned char *buf, size_t bufSize)
{
  size_t nChars = 0;

  if (handle == -1)
  {
    return 0;
  }

  for (; bufSize > 0; --bufSize)
  {
    _put_char((uint8_t) *buf);
    ++buf;
    ++nChars;
  }

  return nChars;
 }

int puts(const char* string) {
    return __write(0, (const void *) string, strlen(string));
}

#elif defined ( __GNUC__ )
ssize_t _write(int fd, const void* ptr, size_t len) {
    const uint8_t * current = (const uint8_t *) ptr;
    {
        for (size_t jj = 0; jj < len; jj++) {
            _put_char(current[jj]);

            if (current[jj] == '\n') {
                _put_char('\r');
            }
        }
        return len;
    }

    return _stub(EBADF);
}

int puts(const char* string) {
    return _write(0, (const void *) string, strlen(string));
}
#endif

int _put_char(int ch)
{
    usart_data_transmit(USART0, (uint8_t) ch );
    while (usart_flag_get(USART0, USART_FLAG_TBE)== RESET){
    }

    return ch;
}