Select one of the symbols to view example projects that use it.
 
Outline
#include <stdio.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/times.h>
#include <time.h>
#include <unistd.h>
#include "pico/bootrom.h"
#include "pico/time.h"
#include "pico/runtime_init.h"
#include "pico/printf.h"
#define weak_raw_printf
#define weak_raw_vprintf
#include "pico/stdio.h"
#include "pico/bootrom.h"
#define STDIO_HANDLE_STDIN
#define STDIO_HANDLE_STDOUT
#define STDIO_HANDLE_STDERR
_exit(int)
_sbrk(int)
epoch_time_us_since_boot
_gettimeofday(struct timeval *restrict, void *restrict)
settimeofday(const struct timeval *, const struct timezone *)
_times(struct tms *)
_getpid()
_kill(pid_t, int)
_read(int, char *, int)
_write(int, char *, int)
_open(const char *, int, ...)
_close(int)
_lseek(int, off_t, int)
_fstat(int, struct stat *)
_isatty(int)
exit(int)
__assert_func(const char *, int, const char *, const char *)
runtime_init()
Files
loading...
SourceVuRaspberry Pi Pico SDK and ExamplesPicoSDKsrc/rp2_common/pico_clib_interface/newlib_interface.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * Copyright (c) 2020 Raspberry Pi (Trading) Ltd. * * SPDX-License-Identifier: BSD-3-Clause *//* ... */ #include <stdio.h> #include <stdarg.h> #include <sys/stat.h> #include <sys/time.h> #include <sys/times.h> #include <time.h> #include <unistd.h> 7 includes#if PICO_ENTER_USB_BOOT_ON_EXIT #include "pico/bootrom.h" #endif #include "pico/time.h" #include "pico/runtime_init.h" #if LIB_PICO_PRINTF_PICO #include "pico/printf.h" #else #define weak_raw_printf printf #define weak_raw_vprintf vprintf /* ... */#endif #if LIB_PICO_STDIO #include "pico/stdio.h" #endif #if PICO_ENTER_USB_BOOT_ON_EXIT #include "pico/bootrom.h" #endif extern char __StackLimit; /* Set by linker. */ #define STDIO_HANDLE_STDIN 0 #define STDIO_HANDLE_STDOUT 1 #define STDIO_HANDLE_STDERR 2 void __attribute__((noreturn)) __weak _exit(__unused int status) { #if PICO_ENTER_USB_BOOT_ON_EXIT reset_usb_boot(0,0); #else while (1) { __breakpoint(); }while (1) { ... } /* ... */#endif }{ ... } __weak void *_sbrk(int incr) { extern char end; /* Set by linker. */ static char *heap_end; char *prev_heap_end; if (heap_end == 0) heap_end = &end; prev_heap_end = heap_end; char *next_heap_end = heap_end + incr; if (__builtin_expect(next_heap_end > (&__StackLimit), false)) { #if PICO_USE_OPTIMISTIC_SBRK if (heap_end == &__StackLimit) { // errno = ENOMEM; return (char *) -1; }if (heap_end == &__StackLimit) { ... } next_heap_end = &__StackLimit;/* ... */ #else return (char *) -1; #endif }if (__builtin_expect(next_heap_end > (&__StackLimit), false)) { ... } heap_end = next_heap_end; return (void *) prev_heap_end; }{ ... } static int64_t epoch_time_us_since_boot; __weak int _gettimeofday (struct timeval *__restrict tv, __unused void *__restrict tz) { if (tv) { int64_t us_since_epoch = ((int64_t)to_us_since_boot(get_absolute_time())) - epoch_time_us_since_boot; tv->tv_sec = (time_t)(us_since_epoch / 1000000); tv->tv_usec = (suseconds_t)(us_since_epoch % 1000000); }if (tv) { ... } return 0; }{ ... } __weak int settimeofday(__unused const struct timeval *tv, __unused const struct timezone *tz) { if (tv) { int64_t us_since_epoch = tv->tv_sec * 1000000 + tv->tv_usec; epoch_time_us_since_boot = (int64_t)to_us_since_boot(get_absolute_time()) - us_since_epoch; }if (tv) { ... } return 0; }{ ... } __weak int _times(struct tms *tms) { #if CLOCKS_PER_SEC >= 1000000 tms->tms_utime = (clock_t)(to_us_since_boot(get_absolute_time()) * (CLOCKS_PER_SEC / 1000000)); #else tms->tms_utime = (clock_t)(to_us_since_boot(get_absolute_time()) / (1000000 / CLOCKS_PER_SEC)); #endif tms->tms_stime = 0; tms->tms_cutime = 0; tms->tms_cstime = 0; return 0; }{ ... } __weak pid_t _getpid(void) { return 0; }{ ... } __weak int _kill(__unused pid_t pid, __unused int sig) { return -1; }{ ... } int __attribute__((weak)) _read(int handle, char *buffer, int length) { #if LIB_PICO_STDIO if (handle == STDIO_HANDLE_STDIN) { return stdio_get_until(buffer, length, at_the_end_of_time); }if (handle == STDIO_HANDLE_STDIN) { ... } /* ... */#endif return -1; }{ ... } int __attribute__((weak)) _write(int handle, char *buffer, int length) { #if LIB_PICO_STDIO if (handle == STDIO_HANDLE_STDOUT || handle == STDIO_HANDLE_STDERR) { stdio_put_string(buffer, length, false, true); return length; }if (handle == STDIO_HANDLE_STDOUT || handle == STDIO_HANDLE_STDERR) { ... } /* ... */#endif return -1; }{ ... } int __attribute__((weak)) _open(__unused const char *fn, __unused int oflag, ...) { return -1; }{ ... } int __attribute__((weak)) _close(__unused int fd) { return -1; }{ ... } off_t __attribute__((weak)) _lseek(__unused int fd, __unused off_t pos, __unused int whence) { return -1; }{ ... } int __attribute__((weak)) _fstat(__unused int fd, __unused struct stat *buf) { return -1; }{ ... } int __attribute__((weak)) _isatty(int fd) { return fd == STDIO_HANDLE_STDIN || fd == STDIO_HANDLE_STDOUT || fd == STDIO_HANDLE_STDERR; }{ ... } // exit is not useful... no desire to pull in __call_exitprocs void exit(int status) { _exit(status); }{ ... } // incorrect warning from GCC 6 GCC_Pragma("GCC diagnostic push") GCC_Pragma("GCC diagnostic ignored \"-Wsuggest-attribute=format\"") void __weak __assert_func(const char *file, int line, const char *func, const char *failedexpr) { weak_raw_printf("assertion \"%s\" failed: file \"%s\", line %d%s%s\n", failedexpr, file, line, func ? ", function: " : "", func ? func : ""); _exit(1); }{ ... } GCC_Pragma("GCC diagnostic pop") void runtime_init(void) { #ifndef NDEBUG if (__get_current_exception()) { // crap; started in exception handler __breakpoint(); }if (__get_current_exception()) { ... } /* ... */#endif #if !PICO_RUNTIME_SKIP_INIT_PER_CORE_INSTALL_STACK_GUARD // install core0 stack guard extern char __StackBottom; runtime_init_per_core_install_stack_guard(&__StackBottom);/* ... */ #endif // todo maybe we want to do this in the future, but it does stuff like register_tm_clones // which we didn't do in previous SDKs //extern void __libc_init_array(void); //__libc_init_array(); // ... so instead just do the __preinit_array runtime_run_initializers(); // ... and the __init_array extern void (*__init_array_start)(void); extern void (*__init_array_end)(void); for (void (**p)(void) = &__init_array_start; p < &__init_array_end; ++p) { (*p)(); }for (void (**p)(void) = &__init_array_start; p < &__init_array_end; ++p) { ... } }{ ... }
Details