Skip to content

Commit f1f6226

Browse files
Split newlib syscall support to its own file (#141)
1 parent 1de1d04 commit f1f6226

File tree

2 files changed

+155
-139
lines changed

2 files changed

+155
-139
lines changed

cores/rp2040/main.cpp

-139
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
#include <Arduino.h>
2222
#include "RP2040USB.h"
23-
#include <pico/stdlib.h>
2423
#include <pico/multicore.h>
2524

2625
RP2040 rp2040;
@@ -94,141 +93,3 @@ extern "C" int main() {
9493
}
9594
return 0;
9695
}
97-
98-
99-
/* NEWLIB syscall overloads, so we can ::print() and ::getc() using our objects */
100-
/* Placed here to ensure this compilation unit will be present when it's time */
101-
/* to link newlib.a. */
102-
103-
#include <stdint.h>
104-
#include <errno.h>
105-
#include <_syslist.h>
106-
#include <sys/times.h>
107-
#undef errno
108-
109-
110-
// TODO - check the fd, create a VFS, etc.
111-
112-
extern "C" int errno;
113-
114-
extern "C" ssize_t _write(int fd, const void *buf, size_t count) {
115-
#if defined DEBUG_RP2040_PORT
116-
return DEBUG_RP2040_PORT.write((const char *)buf, count);
117-
#else
118-
return 0;
119-
#endif
120-
}
121-
122-
extern "C" int _chown (const char *path, uid_t owner, gid_t group) {
123-
errno = ENOSYS;
124-
return -1;
125-
}
126-
127-
extern "C" int _close (int fildes) {
128-
errno = ENOSYS;
129-
return -1;
130-
}
131-
132-
extern "C" int _execve (char *name, char **argv, char **env) {
133-
errno = ENOSYS;
134-
return -1;
135-
}
136-
137-
extern "C" int _fork (void) {
138-
errno = ENOSYS;
139-
return -1;
140-
}
141-
142-
extern "C" int _fstat (int fildes, struct stat *st) {
143-
errno = ENOSYS;
144-
return -1;
145-
}
146-
147-
extern "C" int _getpid (void) {
148-
errno = ENOSYS;
149-
return -1;
150-
}
151-
152-
153-
static int64_t __timedelta_us = 0.0;
154-
155-
extern "C" int _gettimeofday (struct timeval *tv, void *tz) {
156-
uint64_t now_us = to_us_since_boot(get_absolute_time()) + __timedelta_us;
157-
if (tv) {
158-
tv->tv_sec = now_us / 1000000L;
159-
tv->tv_usec = now_us % 1000000L;
160-
}
161-
return 0;
162-
}
163-
164-
extern "C" int settimeofday (const struct timeval *tv, const struct timezone *tz) {
165-
uint64_t now_us = to_us_since_boot(get_absolute_time());
166-
if (tv) {
167-
uint64_t newnow_us;
168-
newnow_us = tv->tv_sec * 1000000L;
169-
newnow_us += tv->tv_usec;
170-
__timedelta_us = newnow_us - now_us;
171-
}
172-
return 0;
173-
}
174-
175-
extern "C" int _isatty (int file) {
176-
errno = ENOSYS;
177-
return 0;
178-
}
179-
180-
extern "C" int _kill (int pid, int sig) {
181-
errno = ENOSYS;
182-
return -1;
183-
}
184-
185-
extern "C" int _link (char *existing, char *newlink) {
186-
errno = ENOSYS;
187-
return -1;
188-
}
189-
190-
extern "C" int _lseek (int file, int ptr, int dir) {
191-
errno = ENOSYS;
192-
return -1;
193-
}
194-
195-
extern "C" int _open (char *file, int flags, int mode) {
196-
errno = ENOSYS;
197-
return -1;
198-
}
199-
200-
extern "C" int _read (int file, char *ptr, int len)
201-
{
202-
// return Serial.read(ptr, len);
203-
return -1;
204-
}
205-
206-
extern "C" int _readlink (const char *path, char *buf, size_t bufsize) {
207-
errno = ENOSYS;
208-
return -1;
209-
}
210-
211-
extern "C" int _stat (const char *file, struct stat *st) {
212-
errno = ENOSYS;
213-
return -1;
214-
}
215-
216-
extern "C" int _symlink (const char *path1, const char *path2) {
217-
errno = ENOSYS;
218-
return -1;
219-
}
220-
221-
extern "C" clock_t _times (struct tms *buf) {
222-
errno = ENOSYS;
223-
return -1;
224-
}
225-
226-
extern "C" int _unlink (char *name) {
227-
errno = ENOSYS;
228-
return -1;
229-
}
230-
231-
extern "C" int _wait (int *status) {
232-
errno = ENOSYS;
233-
return -1;
234-
}

cores/rp2040/posix.cpp

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* NEWLIB syscall implementations
3+
*
4+
* Copyright (c) 2021 Earle F. Philhower, III <earlephilhower@yahoo.com>
5+
*
6+
* This library is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 2.1 of the License, or (at your option) any later version.
10+
*
11+
* This library is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public
17+
* License along with this library; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#include <Arduino.h>
22+
#include <stdint.h>
23+
#include <errno.h>
24+
#include <_syslist.h>
25+
#include <sys/times.h>
26+
#include <pico/stdlib.h>
27+
#include <pico/multicore.h>
28+
29+
#undef errno
30+
31+
32+
// TODO - check the fd, create a VFS, etc.
33+
34+
extern "C" int errno;
35+
36+
extern "C" ssize_t _write(int fd, const void *buf, size_t count) {
37+
#if defined DEBUG_RP2040_PORT
38+
return DEBUG_RP2040_PORT.write((const char *)buf, count);
39+
#else
40+
return 0;
41+
#endif
42+
}
43+
44+
extern "C" int _chown (const char *path, uid_t owner, gid_t group) {
45+
errno = ENOSYS;
46+
return -1;
47+
}
48+
49+
extern "C" int _close (int fildes) {
50+
errno = ENOSYS;
51+
return -1;
52+
}
53+
54+
extern "C" int _execve (char *name, char **argv, char **env) {
55+
errno = ENOSYS;
56+
return -1;
57+
}
58+
59+
extern "C" int _fork (void) {
60+
errno = ENOSYS;
61+
return -1;
62+
}
63+
64+
extern "C" int _fstat (int fildes, struct stat *st) {
65+
errno = ENOSYS;
66+
return -1;
67+
}
68+
69+
extern "C" int _getpid (void) {
70+
errno = ENOSYS;
71+
return -1;
72+
}
73+
74+
static int64_t __timedelta_us = 0.0;
75+
76+
extern "C" int _gettimeofday (struct timeval *tv, void *tz) {
77+
uint64_t now_us = to_us_since_boot(get_absolute_time()) + __timedelta_us;
78+
if (tv) {
79+
tv->tv_sec = now_us / 1000000L;
80+
tv->tv_usec = now_us % 1000000L;
81+
}
82+
return 0;
83+
}
84+
85+
extern "C" int settimeofday (const struct timeval *tv, const struct timezone *tz) {
86+
uint64_t now_us = to_us_since_boot(get_absolute_time());
87+
if (tv) {
88+
uint64_t newnow_us;
89+
newnow_us = tv->tv_sec * 1000000L;
90+
newnow_us += tv->tv_usec;
91+
__timedelta_us = newnow_us - now_us;
92+
}
93+
return 0;
94+
}
95+
96+
extern "C" int _isatty (int file) {
97+
errno = ENOSYS;
98+
return 0;
99+
}
100+
101+
extern "C" int _kill (int pid, int sig) {
102+
errno = ENOSYS;
103+
return -1;
104+
}
105+
106+
extern "C" int _link (char *existing, char *newlink) {
107+
errno = ENOSYS;
108+
return -1;
109+
}
110+
111+
extern "C" int _lseek (int file, int ptr, int dir) {
112+
errno = ENOSYS;
113+
return -1;
114+
}
115+
116+
extern "C" int _open (char *file, int flags, int mode) {
117+
errno = ENOSYS;
118+
return -1;
119+
}
120+
121+
extern "C" int _read (int file, char *ptr, int len)
122+
{
123+
// return Serial.read(ptr, len);
124+
return -1;
125+
}
126+
127+
extern "C" int _readlink (const char *path, char *buf, size_t bufsize) {
128+
errno = ENOSYS;
129+
return -1;
130+
}
131+
132+
extern "C" int _stat (const char *file, struct stat *st) {
133+
errno = ENOSYS;
134+
return -1;
135+
}
136+
137+
extern "C" int _symlink (const char *path1, const char *path2) {
138+
errno = ENOSYS;
139+
return -1;
140+
}
141+
142+
extern "C" clock_t _times (struct tms *buf) {
143+
errno = ENOSYS;
144+
return -1;
145+
}
146+
147+
extern "C" int _unlink (char *name) {
148+
errno = ENOSYS;
149+
return -1;
150+
}
151+
152+
extern "C" int _wait (int *status) {
153+
errno = ENOSYS;
154+
return -1;
155+
}

0 commit comments

Comments
 (0)