Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Picolibc #200

Merged
merged 2 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,20 @@ libmetal_a_SOURCES = \
src/vector.S \
src/watchdog.c

########################################################
# libmetal-pico
########################################################

lib_LIBRARIES += libmetal-pico.a

libmetal_pico_a_SOURCES = \
gloss/crt0.S \
pico/iob.c \
gloss/sys_exit.c \
pico/times.c \
gloss/sys_sbrk.c \
pico/sysconf.c

########################################################
# libgloss
########################################################
Expand Down
62 changes: 57 additions & 5 deletions Makefile.in

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions gloss/crt0.S
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ _start:
#endif
2:

/* Set TLS pointer */
.weak __tls_base
la tp, __tls_base

/* At this point we're in an environment that can execute C code. The first
* thing to do is to make the callback to the parent environment if it's been
* requested to do so. */
Expand Down
1 change: 1 addition & 0 deletions gloss/sys_getcwd.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <errno.h>
#include <stddef.h>

char *_getcwd(char *buf, size_t size) {
errno = -ENOSYS;
Expand Down
17 changes: 11 additions & 6 deletions gloss/sys_sbrk.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,31 @@
* that's been allocated. */
extern char metal_segment_heap_target_start;
extern char metal_segment_heap_target_end;
static char *brk = &metal_segment_heap_target_start;
static char *__brk = &metal_segment_heap_target_start;

#ifdef _PICOLIBC__
#define _brk brk
#define _sbrk sbrk
#endif

int _brk(void *addr) {
brk = addr;
keith-packard marked this conversation as resolved.
Show resolved Hide resolved
__brk = addr;
return 0;
}

char *_sbrk(ptrdiff_t incr) {
char *old = brk;
char *old = __brk;

/* If __heap_size == 0, we can't allocate memory on the heap */
if (&metal_segment_heap_target_start == &metal_segment_heap_target_end) {
return (void *)-1;
}

/* Don't move the break past the end of the heap */
if ((brk + incr) < &metal_segment_heap_target_end) {
brk += incr;
if ((__brk + incr) < &metal_segment_heap_target_end) {
__brk += incr;
} else {
brk = &metal_segment_heap_target_end;
__brk = &metal_segment_heap_target_end;
return (void *)-1;
}

Expand Down
44 changes: 44 additions & 0 deletions pico/iob.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright © 2019 Keith Packard
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/

#include <metal/tty.h>
#include <stdio.h>

static int metal_putc(char c, FILE *file) {
(void)file;
metal_tty_putc(c);
return c;
}

static int metal_getc(FILE *file) {
int c;
(void)file;
metal_tty_getc(&c);
return c;
}

static int metal_flush(FILE *file) { return 0; }

static FILE __stdio =
FDEV_SETUP_STREAM(metal_putc, metal_getc, metal_flush, _FDEV_SETUP_RW);

FILE *const __iob[3] = {&__stdio, &__stdio, &__stdio};
16 changes: 16 additions & 0 deletions pico/sysconf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <time.h>
#include <unistd.h>

/* Get configurable system variables. */

long sysconf(int name) {
unsigned long long timebase;

switch (name) {
case _SC_CLK_TCK:
metal_timer_get_timebase_frequency(0, &timebase);
return (long)timebase;
}

return -1;
}
17 changes: 17 additions & 0 deletions pico/times.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <metal/timer.h>
#include <sys/time.h>
#include <sys/times.h>

/*
* Simple enough to return the tick count by
* just getting it from the hardware.
*/

clock_t times(struct tms *buf) {
unsigned long long mcc;
metal_timer_get_cyclecount(0, &mcc);
buf->tms_stime = 0;
buf->tms_cutime = 0;
buf->tms_cstime = 0;
return buf->tms_utime = mcc;
}