-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathretarget.c
116 lines (91 loc) · 2.14 KB
/
retarget.c
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <stddef.h>
#include <string.h>
#include "gd32vf103.h"
void write_hex(int fd, unsigned long int hex)
{
uint8_t ii;
uint8_t jj;
char towrite;
write(fd , "0x", 2);
for (ii = sizeof(unsigned long int) * 2 ; ii > 0; ii--) {
jj = ii - 1;
uint8_t digit = ((hex & (0xF << (jj*4))) >> (jj*4));
towrite = digit < 0xA ? ('0' + digit) : ('A' + (digit - 0xA));
write(fd, &towrite, 1);
}
}
static inline int _stub(int err)
{
return -1;
}
void _exit(int code)
{
const char message[] = "\nProgram has exited with code:";
write(STDERR_FILENO, message, sizeof(message) - 1);
write_hex(STDERR_FILENO, code);
write(STDERR_FILENO, "\n", 1);
for (;;);
}
int _close(int fd)
{
return _stub(EBADF);
}
int _fstat(int fd, struct stat* st)
{
if (isatty(fd)) {
st->st_mode = S_IFCHR;
return 0;
}
return _stub(EBADF);
}
int _isatty(int fd)
{
if (fd == STDOUT_FILENO || fd == STDERR_FILENO)
return 1;
return 0;
}
off_t _lseek(int fd, off_t ptr, int dir)
{
if (isatty(fd))
return 0;
return _stub(EBADF);
}
ssize_t _read(int fd, void* ptr, size_t len)
{
return _stub(EBADF);
}
void *_sbrk(ptrdiff_t incr)
{
extern char _stack_end[];
extern char _heap_start[];
static char *curbrk = _stack_end;
if ((curbrk + incr < _stack_end) || (curbrk + incr > _heap_start))
return NULL - 1;
curbrk += incr;
return curbrk - incr;
}
typedef unsigned int size_t;
/* retarget the C library printf function to the USART */
int _put_char(int ch)
{
usart_data_transmit(USART0, (uint8_t) ch );
while ( usart_flag_get(USART0, USART_FLAG_TBE)== RESET) {}
return ch;
}
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;
}
int puts(const char* string) {
return _write(0, (const void *) string, strlen(string));
}