1 // Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <stdbool.h>
16 #include <stdlib.h>
17 #include <stdarg.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20 #include <errno.h>
21 #include <reent.h>
22 #include <sys/fcntl.h>
23 #include "sdkconfig.h"
24 #include "esp_rom_uart.h"
25
syscall_not_implemented(void)26 static int syscall_not_implemented(void)
27 {
28 errno = ENOSYS;
29 return -1;
30 }
31
syscall_not_implemented_aborts(void)32 static int syscall_not_implemented_aborts(void)
33 {
34 abort();
35 }
36
_write_r_console(struct _reent * r,int fd,const void * data,size_t size)37 ssize_t _write_r_console(struct _reent *r, int fd, const void * data, size_t size)
38 {
39 const char* cdata = (const char*) data;
40 if (fd == STDOUT_FILENO || fd == STDERR_FILENO) {
41 for (size_t i = 0; i < size; ++i) {
42 esp_rom_uart_tx_one_char(cdata[i]);
43 }
44 return size;
45 }
46 errno = EBADF;
47 return -1;
48 }
49
_read_r_console(struct _reent * r,int fd,void * data,size_t size)50 ssize_t _read_r_console(struct _reent *r, int fd, void * data, size_t size)
51 {
52 char* cdata = (char*) data;
53 if (fd == STDIN_FILENO) {
54 size_t received;
55 for (received = 0; received < size; ++received) {
56 int status = esp_rom_uart_rx_one_char((uint8_t*) &cdata[received]);
57 if (status != 0) {
58 break;
59 }
60 }
61 return received;
62 }
63 errno = EBADF;
64 return -1;
65 }
66
67
68 /* The following weak definitions of syscalls will be used unless
69 * another definition is provided. That definition may come from
70 * VFS, LWIP, or the application.
71 */
72 ssize_t _read_r(struct _reent *r, int fd, void * dst, size_t size)
73 __attribute__((weak,alias("_read_r_console")));
74 ssize_t _write_r(struct _reent *r, int fd, const void * data, size_t size)
75 __attribute__((weak,alias("_write_r_console")));
76
77
78 /* The aliases below are to "syscall_not_implemented", which
79 * doesn't have the same signature as the original function.
80 * Disable type mismatch warnings for this reason.
81 */
82 #pragma GCC diagnostic push
83 #pragma GCC diagnostic ignored "-Wattribute-alias"
84
85
86 #pragma GCC diagnostic pop
87
88
89 /* No-op function, used to force linking this file,
90 instead of the syscalls implementation from libgloss.
91 */
newlib_include_syscalls_impl(void)92 void newlib_include_syscalls_impl(void)
93 {
94 }
95