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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
#include "unistd.h"
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cerrno>
#include "third_party/printf/printf.h"
#include "fcntl.h"
#include "uart.h"
#ifdef errno
#undef errno
#endif
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wold-style-cast"
#endif
enum class FileType {
kUART,
kUARTDirect,
};
struct File
{
FileType type;
int flags;
void *extra;
};
static constexpr size_t kFilesCount = 5;
static File files[5] {
{ FileType::kUART, O_NONBLOCK, reinterpret_cast<void *>(USART1), },
{ FileType::kUART, O_NONBLOCK, reinterpret_cast<void *>(USART1), },
{ FileType::kUARTDirect, 0, reinterpret_cast<void *>(USART1), },
{ FileType::kUART, O_NONBLOCK, reinterpret_cast<void *>(USART3), },
{ FileType::kUART, O_NONBLOCK, reinterpret_cast<void *>(USART4), },
};
int errno{};
extern "C" [[noreturn]] void vApplicationMallocFailedHook(void)
{
for (;;);
}
extern "C" [[noreturn]] void vApplicationStackOverflowHook(void *task_handle, char *task_name)
{
(void) task_handle;
(void) task_name;
for (;;);
}
extern "C" void vApplicationIdleHook(void)
{
}
extern "C" [[noreturn]] void _exit(int __status)
{
(void) __status;
for (;;);
}
/* func can be NULL, in which case no function information is given. */
extern "C" [[noreturn]] void __assert_func (
const char *file, int line, const char *func, const char *failedexpr)
{
dprintf(STDERR_FILENO,
"assertion \"%s\" failed: file \"%s\", line %d%s%s\n",
failedexpr, file, line,
func ? ", function: " : "", func ? func : "");
abort();
/* NOTREACHED */
}
static File* FileOfDescriptor(int fd)
{
if (fd < 0 || static_cast<unsigned>(fd) >= kFilesCount)
return nullptr;
return files + fd;
}
extern "C" ssize_t read(int fd, void *buf, size_t nbytes)
{
File* file = FileOfDescriptor(fd);
if (file == nullptr) {
errno = EBADF;
return -1;
}
size_t ret{};
switch (file->type) {
case FileType::kUART:
ret = UARTRead(reinterpret_cast<USART_TypeDef*>(file->extra), buf, nbytes);
break;
case FileType::kUARTDirect:
ret = UARTReadDirect(reinterpret_cast<USART_TypeDef*>(file->extra), buf, nbytes);
break;
}
return ret;
}
extern "C" ssize_t write(int fd, const void *buf, size_t nbytes)
{
File* file = FileOfDescriptor(fd);
if (file == nullptr) {
errno = EBADF;
return -1;
}
size_t ret{};
switch (file->type) {
case FileType::kUART:
ret = UARTWrite(reinterpret_cast<USART_TypeDef*>(file->extra), buf, nbytes);
break;
case FileType::kUARTDirect:
ret = UARTWriteDirect(reinterpret_cast<USART_TypeDef*>(file->extra), buf, nbytes);
break;
}
return ret;
}
static void dputchar(char c, void* fd)
{
write(reinterpret_cast<int>(fd), &c, 1);
}
static void dputchar(char c, int fd)
{
write(fd, &c, 1);
}
extern "C" int dprintf(int fd, const char * format, ...)
{
va_list args;
va_start(args, format);
const int ret = vfctprintf(dputchar, reinterpret_cast<void *>(fd), format, args);
va_end(args);
return ret;
}
extern "C" int vdprintf(int fd, const char * format, va_list args)
{
return vfctprintf(dputchar, reinterpret_cast<void *>(fd), format, args);
}
extern "C" void putchar_(char c)
{
dputchar(c, STDIN_FILENO);
}
|