summaryrefslogtreecommitdiff
path: root/components/libc/compilers/newlib
diff options
context:
space:
mode:
authorcliff-cmc <[email protected]>2021-02-26 11:09:27 +0800
committerCliff Chen <[email protected]>2021-02-26 11:45:19 +0800
commiteea0c3d74a40f73235da45c6f77e8b411ac94cf2 (patch)
treea6906f1aed7118ab53dbe18f2ee4727a754583af /components/libc/compilers/newlib
parent1a9ecf27f31277750d92c921b62503f75275e122 (diff)
parentfae0fa8eea4c0e797940bf1eddc8ee6c1711734a (diff)
Add memtrace command for memheap.c
Diffstat (limited to 'components/libc/compilers/newlib')
-rw-r--r--components/libc/compilers/newlib/README.md9
-rw-r--r--components/libc/compilers/newlib/SConscript2
-rw-r--r--components/libc/compilers/newlib/libc.h19
-rw-r--r--components/libc/compilers/newlib/libc_syms.c4
-rw-r--r--components/libc/compilers/newlib/machine/time.h12
-rw-r--r--components/libc/compilers/newlib/syscalls.c241
-rw-r--r--components/libc/compilers/newlib/termios.h15
-rw-r--r--components/libc/compilers/newlib/time.c82
8 files changed, 88 insertions, 296 deletions
diff --git a/components/libc/compilers/newlib/README.md b/components/libc/compilers/newlib/README.md
new file mode 100644
index 0000000000..385f329a4f
--- /dev/null
+++ b/components/libc/compilers/newlib/README.md
@@ -0,0 +1,9 @@
+# NEWLIB (GCC) porting for RT-Thread
+
+Please define RT_USING_LIBC and compile RT-Thread with GCC compiler.
+
+
+
+## More Information
+
+https://sourceware.org/newlib/libc.html#Reentrancy \ No newline at end of file
diff --git a/components/libc/compilers/newlib/SConscript b/components/libc/compilers/newlib/SConscript
index 06e5faad09..d0868bbdea 100644
--- a/components/libc/compilers/newlib/SConscript
+++ b/components/libc/compilers/newlib/SConscript
@@ -15,7 +15,7 @@ CPPDEFINES = ['RT_USING_NEWLIB']
LIBS = ['c', 'm']
if rtconfig.PLATFORM == 'gcc':
- group = DefineGroup('newlib', src, depend = ['RT_USING_LIBC'],
+ group = DefineGroup('libc', src, depend = ['RT_USING_LIBC'],
CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES, LIBS = LIBS)
Return('group')
diff --git a/components/libc/compilers/newlib/libc.h b/components/libc/compilers/newlib/libc.h
index fdf2ab4f60..9a76ffb79f 100644
--- a/components/libc/compilers/newlib/libc.h
+++ b/components/libc/compilers/newlib/libc.h
@@ -10,23 +10,10 @@
#ifndef __RTT_LIBC_H__
#define __RTT_LIBC_H__
-#include <stdio.h>
-#include <stdint.h>
-#include <string.h>
-#include <sys/time.h>
-
#ifndef _EXFUN
-#define _EXFUN(name, proto) name proto
+#define _EXFUN(name, proto) name proto
#endif
-#define MILLISECOND_PER_SECOND 1000UL
-#define MICROSECOND_PER_SECOND 1000000UL
-#define NANOSECOND_PER_SECOND 1000000000UL
-
-#define MILLISECOND_PER_TICK (MILLISECOND_PER_SECOND / RT_TICK_PER_SECOND)
-#define MICROSECOND_PER_TICK (MICROSECOND_PER_SECOND / RT_TICK_PER_SECOND)
-#define NANOSECOND_PER_TICK (NANOSECOND_PER_SECOND / RT_TICK_PER_SECOND)
-
#ifdef __cplusplus
extern "C" {
#endif
@@ -34,10 +21,6 @@ int libc_system_init(void);
int libc_stdio_set_console(const char* device_name, int mode);
int libc_stdio_get_console(void);
-/* some time related function */
-int libc_set_time(const struct timespec* time);
-int libc_get_time(struct timespec* time);
-int libc_time_to_tick(const struct timespec* time);
#ifdef __cplusplus
}
#endif
diff --git a/components/libc/compilers/newlib/libc_syms.c b/components/libc/compilers/newlib/libc_syms.c
index b150c10fd6..4b0ee355f0 100644
--- a/components/libc/compilers/newlib/libc_syms.c
+++ b/components/libc/compilers/newlib/libc_syms.c
@@ -39,10 +39,6 @@ RTM_EXPORT(snprintf);
RTM_EXPORT(fwrite);
-#include <time.h>
-RTM_EXPORT(localtime);
-RTM_EXPORT(time);
-
#include <setjmp.h>
RTM_EXPORT(longjmp);
RTM_EXPORT(setjmp);
diff --git a/components/libc/compilers/newlib/machine/time.h b/components/libc/compilers/newlib/machine/time.h
new file mode 100644
index 0000000000..d46533eb39
--- /dev/null
+++ b/components/libc/compilers/newlib/machine/time.h
@@ -0,0 +1,12 @@
+#ifndef _MACHTIME_H_
+#define _MACHTIME_H_
+
+#include <rtconfig.h>
+#define _CLOCKS_PER_SEC_ RT_TICK_PER_SECOND
+
+#ifdef __SPU__
+#include <sys/_timespec.h>
+int nanosleep (const struct timespec *, struct timespec *);
+#endif
+
+#endif /* _MACHTIME_H_ */
diff --git a/components/libc/compilers/newlib/syscalls.c b/components/libc/compilers/newlib/syscalls.c
index 228fa0d140..949d8a61de 100644
--- a/components/libc/compilers/newlib/syscalls.c
+++ b/components/libc/compilers/newlib/syscalls.c
@@ -5,11 +5,16 @@
*
* Change Logs:
* Date Author Notes
+ * 2021-02-11 Meco Man remove _gettimeofday_r() and _times_r()
+ * 2020-02-13 Meco Man re-implement exit() and abort()
+ * 2020-02-21 Meco Man improve and beautify syscalls
+ * 2020-02-24 Meco Man fix bug of _isatty_r()
*/
+
#include <reent.h>
-#include <sys/errno.h>
-#include <sys/time.h>
+#include <errno.h>
#include <stdio.h>
+#include <sys/time.h>
#include <rtthread.h>
@@ -32,10 +37,18 @@ __errno ()
#endif
int
+_getpid_r(struct _reent *ptr)
+{
+ return 0;
+}
+
+int
_close_r(struct _reent *ptr, int fd)
{
#ifndef RT_USING_DFS
- return 0;
+ /* return "not supported" */
+ ptr->_errno = ENOTSUP;
+ return -1;
#else
return close(fd);
#endif
@@ -74,21 +87,17 @@ _fstat_r(struct _reent *ptr, int fd, struct stat *pstat)
}
int
-_getpid_r(struct _reent *ptr)
-{
- return 0;
-}
-
-int
_isatty_r(struct _reent *ptr, int fd)
{
- if (fd >=0 && fd < 3) return 1;
-
- /* return "not supported" */
- ptr->_errno = ENOTSUP;
- return -1;
+ if (fd >=0 && fd < 3)
+ {
+ return 1;
+ }
+ else
+ {
+ return 0;
+ }
}
-
int
_kill_r(struct _reent *ptr, int pid, int sig)
{
@@ -109,7 +118,9 @@ _off_t
_lseek_r(struct _reent *ptr, int fd, _off_t pos, int whence)
{
#ifndef RT_USING_DFS
- return 0;
+ /* return "not supported" */
+ ptr->_errno = ENOTSUP;
+ return -1;
#else
_off_t rc;
@@ -122,7 +133,9 @@ int
_mkdir_r(struct _reent *ptr, const char *name, int mode)
{
#ifndef RT_USING_DFS
- return 0;
+ /* return "not supported" */
+ ptr->_errno = ENOTSUP;
+ return -1;
#else
int rc;
@@ -135,7 +148,9 @@ int
_open_r(struct _reent *ptr, const char *file, int flags, int mode)
{
#ifndef RT_USING_DFS
- return 0;
+ /* return "not supported" */
+ ptr->_errno = ENOTSUP;
+ return -1;
#else
int rc;
@@ -148,7 +163,9 @@ _ssize_t
_read_r(struct _reent *ptr, int fd, void *buf, size_t nbytes)
{
#ifndef RT_USING_DFS
- return 0;
+ /* return "not supported" */
+ ptr->_errno = ENOTSUP;
+ return -1;
#else
_ssize_t rc;
@@ -161,7 +178,9 @@ int
_rename_r(struct _reent *ptr, const char *old, const char *new)
{
#ifndef RT_USING_DFS
- return 0;
+ /* return "not supported" */
+ ptr->_errno = ENOTSUP;
+ return -1;
#else
int rc;
@@ -181,7 +200,9 @@ int
_stat_r(struct _reent *ptr, const char *file, struct stat *pstat)
{
#ifndef RT_USING_DFS
- return 0;
+ /* return "not supported" */
+ ptr->_errno = ENOTSUP;
+ return -1;
#else
int rc;
@@ -190,24 +211,15 @@ _stat_r(struct _reent *ptr, const char *file, struct stat *pstat)
#endif
}
-_CLOCK_T_
-_times_r(struct _reent *ptr, struct tms *ptms)
-{
- /* return "not supported" */
- ptr->_errno = ENOTSUP;
- return -1;
-}
-
int
_unlink_r(struct _reent *ptr, const char *file)
{
#ifndef RT_USING_DFS
- return 0;
+ /* return "not supported" */
+ ptr->_errno = ENOTSUP;
+ return -1;
#else
- int rc;
-
- rc = unlink(file);
- return rc;
+ return unlink(file);
#endif
}
@@ -219,11 +231,11 @@ _wait_r(struct _reent *ptr, int *status)
return -1;
}
-#ifdef RT_USING_DEVICE
_ssize_t
_write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
{
#ifndef RT_USING_DFS
+#ifdef RT_USING_DEVICE
if (fileno(stdout) == fd)
{
rt_device_t console;
@@ -233,120 +245,18 @@ _write_r(struct _reent *ptr, int fd, const void *buf, size_t nbytes)
}
return 0;
-
#else
- _ssize_t rc;
-
- rc = write(fd, buf, nbytes);
- return rc;
-#endif
-}
-#endif
-
-#ifdef RT_USING_PTHREADS
-
-#include <clock_time.h>
-/* POSIX timer provides clock_gettime function */
-#include <time.h>
-int
-_gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp)
-{
- struct timespec tp;
-
- if (clock_gettime(CLOCK_REALTIME, &tp) == 0)
- {
- if (__tp != RT_NULL)
- {
- __tp->tv_sec = tp.tv_sec;
- __tp->tv_usec = tp.tv_nsec / 1000UL;
- }
-
- return tp.tv_sec;
- }
-
/* return "not supported" */
ptr->_errno = ENOTSUP;
return -1;
-}
-
+#endif /*RT_USING_DEVICE*/
#else
+ _ssize_t rc;
-#define MILLISECOND_PER_SECOND 1000UL
-#define MICROSECOND_PER_SECOND 1000000UL
-#define NANOSECOND_PER_SECOND 1000000000UL
-
-#define MILLISECOND_PER_TICK (MILLISECOND_PER_SECOND / RT_TICK_PER_SECOND)
-#define MICROSECOND_PER_TICK (MICROSECOND_PER_SECOND / RT_TICK_PER_SECOND)
-#define NANOSECOND_PER_TICK (NANOSECOND_PER_SECOND / RT_TICK_PER_SECOND)
-
-struct timeval _timevalue = {0};
-#ifdef RT_USING_DEVICE
-static void libc_system_time_init(void)
-{
- time_t time;
- rt_tick_t tick;
- rt_device_t device;
-
- time = 0;
- device = rt_device_find("rtc");
- if (device != RT_NULL)
- {
- /* get realtime seconds */
- rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
- }
-
- /* get tick */
- tick = rt_tick_get();
-
- _timevalue.tv_usec = MICROSECOND_PER_SECOND - (tick%RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK;
- _timevalue.tv_sec = time - tick/RT_TICK_PER_SECOND - 1;
-}
+ rc = write(fd, buf, nbytes);
+ return rc;
#endif
-
-int libc_get_time(struct timespec *time)
-{
- rt_tick_t tick;
- static rt_bool_t inited = 0;
-
- RT_ASSERT(time != RT_NULL);
-
- /* initialize system time */
- if (inited == 0)
- {
- libc_system_time_init();
- inited = 1;
- }
-
- /* get tick */
- tick = rt_tick_get();
-
- time->tv_sec = _timevalue.tv_sec + tick / RT_TICK_PER_SECOND;
- time->tv_nsec = (_timevalue.tv_usec + (tick % RT_TICK_PER_SECOND) * MICROSECOND_PER_TICK) * 1000;
-
- return 0;
-}
-
-int
-_gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp)
-{
- struct timespec tp;
-
- if (libc_get_time(&tp) == 0)
- {
- if (__tp != RT_NULL)
- {
- __tp->tv_sec = tp.tv_sec;
- __tp->tv_usec = tp.tv_nsec / 1000UL;
- }
-
- return tp.tv_sec;
- }
-
- /* return "not supported" */
- ptr->_errno = ENOTSUP;
- return -1;
}
-#endif
/* Memory routine */
void *
@@ -396,27 +306,20 @@ _free_r (struct _reent *ptr, void *addr)
rt_free (addr);
}
-void
-exit (int status)
+/* for exit() and abort() */
+__attribute__ ((noreturn)) void
+_exit (int status)
{
-#ifdef RT_USING_MODULE
- if (dlmodule_self())
- {
- dlmodule_exit(status);
- }
-#endif
-
- rt_kprintf("thread:%s exit with %d\n", rt_thread_self()->name, status);
- RT_ASSERT(0);
-
- while (1);
+ extern void __rt_libc_exit(int status);
+ __rt_libc_exit(status);
+ while(1);
}
void
_system(const char *s)
{
- /* not support this call */
- return;
+ extern int __rt_libc_system(const char *string);
+ __rt_libc_system(s);
}
void __libc_init_array(void)
@@ -424,26 +327,6 @@ void __libc_init_array(void)
/* we not use __libc init_aray to initialize C++ objects */
}
-void abort(void)
-{
- if (rt_thread_self())
- {
- rt_thread_t self = rt_thread_self();
-
- rt_kprintf("thread:%-8.*s abort!\n", RT_NAME_MAX, self->name);
- rt_thread_suspend(self);
-
- rt_schedule();
- }
-
- while (1);
-}
-
-uid_t getuid(void)
-{
- return 0;
-}
-
mode_t umask(mode_t mask)
{
return 022;
@@ -453,3 +336,9 @@ int flock(int fd, int operation)
{
return 0;
}
+
+/*
+These functions are implemented and replaced by the 'common/time.c' file
+int _gettimeofday_r(struct _reent *ptr, struct timeval *__tp, void *__tzp);
+_CLOCK_T_ _times_r(struct _reent *ptr, struct tms *ptms);
+*/
diff --git a/components/libc/compilers/newlib/termios.h b/components/libc/compilers/newlib/termios.h
deleted file mode 100644
index 1956121225..0000000000
--- a/components/libc/compilers/newlib/termios.h
+++ /dev/null
@@ -1,15 +0,0 @@
-/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- */
-#ifndef _TERMIOS_H__
-#define _TERMIOS_H__
-
-#include <sys/types.h>
-#include <posix_termios.h>
-
-#endif
diff --git a/components/libc/compilers/newlib/time.c b/components/libc/compilers/newlib/time.c
deleted file mode 100644
index f099d51330..0000000000
--- a/components/libc/compilers/newlib/time.c
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- */
-#include <sys/time.h>
-#include <rtthread.h>
-
-#ifdef RT_USING_DEVICE
-int gettimeofday(struct timeval *tp, void *ignore)
-{
- time_t time;
- rt_device_t device;
-
- device = rt_device_find("rtc");
- RT_ASSERT(device != RT_NULL);
-
- rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
- if (tp != RT_NULL)
- {
- tp->tv_sec = time;
- tp->tv_usec = 0;
- }
-
- return time;
-}
-#endif
-
-/**
- * Returns the current time.
- *
- * @param time_t * t the timestamp pointer, if not used, keep NULL.
- *
- * @return time_t return timestamp current.
- *
- */
-/* for IAR 6.2 later Compiler */
-#if defined (__IAR_SYSTEMS_ICC__) && (__VER__) >= 6020000
-#pragma module_name = "?time"
-time_t (__time32)(time_t *t) /* Only supports 32-bit timestamp */
-#else
-time_t time(time_t *t)
-#endif
-{
- time_t time_now = 0;
-
-#ifdef RT_USING_RTC
- static rt_device_t device = RT_NULL;
-
- /* optimization: find rtc device only first. */
- if (device == RT_NULL)
- {
- device = rt_device_find("rtc");
- }
-
- /* read timestamp from RTC device. */
- if (device != RT_NULL)
- {
- if (rt_device_open(device, 0) == RT_EOK)
- {
- rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time_now);
- rt_device_close(device);
- }
- }
-#endif /* RT_USING_RTC */
-
- /* if t is not NULL, write timestamp to *t */
- if (t != RT_NULL)
- {
- *t = time_now;
- }
-
- return time_now;
-}
-
-RT_WEAK clock_t clock(void)
-{
- return rt_tick_get();
-}