diff --git a/arch/risc-v/src/esp32c3/Make.defs b/arch/risc-v/src/esp32c3/Make.defs index 469cb1ebb4e..58a6f13eff0 100644 --- a/arch/risc-v/src/esp32c3/Make.defs +++ b/arch/risc-v/src/esp32c3/Make.defs @@ -35,7 +35,7 @@ CMN_ASRCS := $(filter-out riscv_vectors.S,$(CMN_ASRCS)) # Specify our C code within this directory to be included CHIP_CSRCS = esp32c3_allocateheap.c esp32c3_start.c esp32c3_wdt.c esp32c3_idle.c -CHIP_CSRCS += esp32c3_irq.c +CHIP_CSRCS += esp32c3_irq.c esp32c3_libc_stubs.c CHIP_CSRCS += esp32c3_clockconfig.c esp32c3_gpio.c CHIP_CSRCS += esp32c3_lowputc.c esp32c3_serial.c CHIP_CSRCS += esp32c3_systemreset.c esp32c3_resetcause.c diff --git a/arch/risc-v/src/esp32c3/esp32c3_libc_stubs.c b/arch/risc-v/src/esp32c3/esp32c3_libc_stubs.c new file mode 100644 index 00000000000..4c6a1e290e2 --- /dev/null +++ b/arch/risc-v/src/esp32c3/esp32c3_libc_stubs.c @@ -0,0 +1,317 @@ +/**************************************************************************** + * arch/risc-v/src/esp32c3/esp32c3_libc_stubs.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include "rom/esp32c3_libc_stubs.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define _lock_t int + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +static mutex_t g_nxlock_common; +static mutex_t g_nxlock_recursive; + +/* Forward declaration */ + +struct _reent; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int _close_r(struct _reent *r, int fd) +{ + return close(fd); +} + +int _fstat_r(struct _reent *r, int fd, struct stat *statbuf) +{ + return fstat(fd, statbuf); +} + +int _getpid_r(struct _reent *r) +{ + return getpid(); +} + +int _kill_r(struct _reent *r, int pid, int sig) +{ + return kill(pid, sig); +} + +int _link_r(struct _reent *r, const char *oldpath, + const char *newpath) +{ + /* TODO */ + + return 0; +} + +int _lseek_r(struct _reent *r, int fd, int offset, int whence) +{ + return lseek(fd, offset, whence); +} + +int _open_r(struct _reent *r, const char *pathname, + int flags, int mode) +{ + return open(pathname, flags, mode); +} + +int _read_r(struct _reent *r, int fd, void *buf, int count) +{ + return read(fd, buf, count); +} + +int _rename_r(struct _reent *r, const char *oldpath, + const char *newpath) +{ + return rename(oldpath, newpath); +} + +void *_sbrk_r(struct _reent *r, ptrdiff_t increment) +{ + /* TODO: sbrk is only supported on Kernel mode */ + + errno = -ENOMEM; + return (void *) -1; +} + +int _stat_r(struct _reent *r, const char *pathname, + struct stat *statbuf) +{ + return stat(pathname, statbuf); +} + +clock_t _times_r(struct _reent *r, struct tms *buf) +{ + return times(buf); +} + +int _unlink_r(struct _reent *r, const char *pathname) +{ + return unlink(pathname); +} + +int _write_r(struct _reent *r, int fd, const void *buf, int count) +{ + return write(fd, buf, count); +} + +int _gettimeofday_r(struct _reent *r, struct timeval *tv, void *tz) +{ + return gettimeofday(tv, tz); +} + +void *_malloc_r(struct _reent *r, size_t size) +{ + return malloc(size); +} + +void *_realloc_r(struct _reent *r, void *ptr, size_t size) +{ + return realloc(ptr, size); +} + +void *_calloc_r(struct _reent *r, size_t nmemb, size_t size) +{ + return calloc(nmemb, size); +} + +void _free_r(struct _reent *r, void *ptr) +{ + free(ptr); +} + +void _abort(void) +{ + abort(); +} + +void _raise_r(struct _reent *r) +{ + /* FIXME */ +} + +void _lock_init(_lock_t *lock) +{ + nxmutex_init(&g_nxlock_common); + nxsem_get_value(&g_nxlock_common, lock); +} + +void _lock_init_recursive(_lock_t *lock) +{ + nxmutex_init(&g_nxlock_recursive); + nxsem_get_value(&g_nxlock_recursive, lock); +} + +void _lock_close(_lock_t *lock) +{ + nxmutex_destroy(&g_nxlock_common); + *lock = 0; +} + +void _lock_close_recursive(_lock_t *lock) +{ + nxmutex_destroy(&g_nxlock_recursive); + *lock = 0; +} + +void _lock_acquire(_lock_t *lock) +{ + nxmutex_lock(&g_nxlock_common); + nxsem_get_value(&g_nxlock_common, lock); +} + +void _lock_acquire_recursive(_lock_t *lock) +{ + nxmutex_lock(&g_nxlock_recursive); + nxsem_get_value(&g_nxlock_recursive, lock); +} + +int _lock_try_acquire(_lock_t *lock) +{ + nxmutex_trylock(&g_nxlock_common); + nxsem_get_value(&g_nxlock_common, lock); + return 0; +} + +int _lock_try_acquire_recursive(_lock_t *lock) +{ + nxmutex_trylock(&g_nxlock_recursive); + nxsem_get_value(&g_nxlock_recursive, lock); + return 0; +} + +void _lock_release(_lock_t *lock) +{ + nxmutex_unlock(&g_nxlock_common); + nxsem_get_value(&g_nxlock_common, lock); +} + +void _lock_release_recursive(_lock_t *lock) +{ + nxmutex_unlock(&g_nxlock_recursive); + nxsem_get_value(&g_nxlock_recursive, lock); +} + +struct _reent *__getreent(void) +{ + /* TODO */ + + return (struct _reent *) NULL; +} + +int _system_r(struct _reent *r, const char *command) +{ + /* TODO: Implement system() */ + + return 0; +} + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct syscall_stub_table g_stub_table = +{ + .__getreent = &__getreent, + ._malloc_r = &_malloc_r, + ._free_r = &_free_r, + ._realloc_r = &_realloc_r, + ._calloc_r = &_calloc_r, + ._abort = NULL, + ._system_r = &_system_r, + ._rename_r = &_rename_r, + ._times_r = &_times_r, + ._gettimeofday_r = &_gettimeofday_r, + ._raise_r = &_raise_r, + ._unlink_r = &_unlink_r, + ._link_r = &_link_r, + ._stat_r = &_stat_r, + ._fstat_r = &_fstat_r, + ._sbrk_r = &_sbrk_r, + ._getpid_r = &_getpid_r, + ._kill_r = &_kill_r, + ._exit_r = NULL, + ._close_r = &_close_r, + ._open_r = &_open_r, + ._write_r = &_write_r, + ._lseek_r = &_lseek_r, + ._read_r = &_read_r, + ._lock_init = &_lock_init, + ._lock_init_recursive = &_lock_init_recursive, + ._lock_close = &_lock_close, + ._lock_close_recursive = &_lock_close_recursive, + ._lock_acquire = &_lock_acquire, + ._lock_acquire_recursive = &_lock_acquire_recursive, + ._lock_try_acquire = &_lock_try_acquire, + ._lock_try_acquire_recursive = &_lock_try_acquire_recursive, + ._lock_release = &_lock_release, + ._lock_release_recursive = &_lock_release_recursive, + ._printf_float = NULL, + ._scanf_float = NULL, +}; + +/**************************************************************************** + * Name: setup_syscall_table + * + * Description: + * Configure the syscall table used by the ROM code for calling C library + * functions. + * ESP32-C3 ROM code contains implementations of some of C library + * functions. Whenever a function in ROM needs to use a syscall, it calls + * a pointer to the corresponding syscall implementation defined in the + * syscall_stub_table struct. + * + * Input Parameters: + * None. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +void setup_syscall_table(void) +{ + syscall_table_ptr = &g_stub_table; +} + diff --git a/arch/risc-v/src/esp32c3/esp32c3_start.c b/arch/risc-v/src/esp32c3/esp32c3_start.c index c5adb08f2e8..25dd57f5e41 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_start.c +++ b/arch/risc-v/src/esp32c3/esp32c3_start.c @@ -47,6 +47,7 @@ #endif #include "hardware/esp32c3_cache_memory.h" #include "hardware/extmem_reg.h" +#include "rom/esp32c3_libc_stubs.h" /**************************************************************************** * Pre-processor Definitions @@ -293,6 +294,10 @@ void __esp32c3_start(void) *dest++ = 0; } + /* Setup the syscall table needed by the ROM code */ + + setup_syscall_table(); + showprogress('B'); /* Disable any wdt enabled by bootloader */ diff --git a/arch/risc-v/src/esp32c3/rom/esp32c3_libc_stubs.h b/arch/risc-v/src/esp32c3/rom/esp32c3_libc_stubs.h new file mode 100644 index 00000000000..d540acab44f --- /dev/null +++ b/arch/risc-v/src/esp32c3/rom/esp32c3_libc_stubs.h @@ -0,0 +1,113 @@ +/**************************************************************************** + * arch/risc-v/src/esp32c3/rom/esp32c3_libc_stubs.h + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_RISCV_SRC_ESP32C3_ROM_ESP32C3_LIBC_STUBS_H +#define __ARCH_RISCV_SRC_ESP32C3_ROM_ESP32C3_LIBC_STUBS_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#define _lock_t int + +/* Forward declaration */ + +struct _reent; + +struct syscall_stub_table +{ + struct _reent *(* __getreent)(void); + void *(* _malloc_r)(struct _reent *r, size_t); + void (* _free_r)(struct _reent *r, void *); + void *(* _realloc_r)(struct _reent *r, void *, size_t); + void *(* _calloc_r)(struct _reent *r, size_t, size_t); + void (* _abort)(void); + int (* _system_r)(struct _reent *r, const char *); + int (* _rename_r)(struct _reent *r, const char *, const char *); + clock_t (* _times_r)(struct _reent *r, struct tms *); + int (* _gettimeofday_r) (struct _reent *r, struct timeval *, void *); + void (* _raise_r)(struct _reent *r); + int (* _unlink_r)(struct _reent *r, const char *); + int (* _link_r)(struct _reent *r, const char *, const char *); + int (* _stat_r)(struct _reent *r, const char *, struct stat *); + int (* _fstat_r)(struct _reent *r, int, struct stat *); + void *(* _sbrk_r)(struct _reent *r, ptrdiff_t); + int (* _getpid_r)(struct _reent *r); + int (* _kill_r)(struct _reent *r, int, int); + void (* _exit_r)(struct _reent *r, int); + int (* _close_r)(struct _reent *r, int); + int (* _open_r)(struct _reent *r, const char *, int, int); + int (* _write_r)(struct _reent *r, int, const void *, int); + int (* _lseek_r)(struct _reent *r, int, int, int); + int (* _read_r)(struct _reent *r, int, void *, int); + void (* _lock_init)(_lock_t *lock); + void (* _lock_init_recursive)(_lock_t *lock); + void (* _lock_close)(_lock_t *lock); + void (* _lock_close_recursive)(_lock_t *lock); + void (* _lock_acquire)(_lock_t *lock); + void (* _lock_acquire_recursive)(_lock_t *lock); + int (* _lock_try_acquire)(_lock_t *lock); + int (* _lock_try_acquire_recursive)(_lock_t *lock); + void (* _lock_release)(_lock_t *lock); + void (* _lock_release_recursive)(_lock_t *lock); + int (* _printf_float)(struct _reent *data, void *pdata, FILE *fp, + int (*pfunc) (struct _reent *, FILE *, + const char *, size_t len), va_list * ap); + int (* _scanf_float) (struct _reent *rptr, void *pdata, FILE *fp, + va_list *ap); +}; + +extern const struct syscall_stub_table *syscall_table_ptr; + +/**************************************************************************** + * Name: setup_syscall_table + * + * Description: + * Configure the syscall table used by the ROM code for calling C library + * functions. + * ESP32-C3 ROM code contains implementations of some of C library + * functions. Whenever a function in ROM needs to use a syscall, it calls + * a pointer to the corresponding syscall implementation defined in the + * syscall_stub_table struct. + * + * Input Parameters: + * None. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +void setup_syscall_table(void); + +#endif /* __ARCH_RISCV_SRC_ESP32C3_ROM_ESP32C3_LIBC_STUBS_H */ diff --git a/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/scripts/esp32c3_rom.ld b/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/scripts/esp32c3_rom.ld index 8a0aee43bcd..d4a3152dd21 100644 --- a/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/scripts/esp32c3_rom.ld +++ b/boards/risc-v/esp32c3/esp32c3-devkit-rust-1/scripts/esp32c3_rom.ld @@ -2027,6 +2027,7 @@ __umoddi3 = 0x400008bc; __umodsi3 = 0x400008c0; __unorddf2 = 0x400008c4; __unordsf2 = 0x400008c8; +syscall_table_ptr = 0x3fcdffe0; /*************************************** Redefine functions diff --git a/boards/risc-v/esp32c3/esp32c3-devkit/scripts/Make.defs b/boards/risc-v/esp32c3/esp32c3-devkit/scripts/Make.defs index dd7da72241d..77d0896231a 100644 --- a/boards/risc-v/esp32c3/esp32c3-devkit/scripts/Make.defs +++ b/boards/risc-v/esp32c3/esp32c3-devkit/scripts/Make.defs @@ -39,7 +39,7 @@ ARCHSCRIPT += $(BOARD_DIR)$(DELIM)scripts$(DELIM)esp32c3_rom.ld ARCHPICFLAGS = -fpic -CFLAGS := $(ARCHCFLAGS) $(ARCHOPTIMIZATION) $(ARCHCPUFLAGS) $(ARCHINCLUDES) $(ARCHDEFINES) $(EXTRAFLAGS) -pipe +CFLAGS := $(ARCHCFLAGS) $(ARCHOPTIMIZATION) $(ARCHCPUFLAGS) $(ARCHINCLUDES) $(ARCHDEFINES) $(EXTRAFLAGS) -pipe -Werror=return-type -Werror CPICFLAGS = $(ARCHPICFLAGS) $(CFLAGS) CXXFLAGS := $(ARCHCXXFLAGS) $(ARCHOPTIMIZATION) $(ARCHCPUFLAGS) $(ARCHXXINCLUDES) $(ARCHDEFINES) $(EXTRAFLAGS) -pipe CXXPICFLAGS = $(ARCHPICFLAGS) $(CXXFLAGS) diff --git a/boards/risc-v/esp32c3/esp32c3-devkit/scripts/esp32c3_rom.ld b/boards/risc-v/esp32c3/esp32c3-devkit/scripts/esp32c3_rom.ld index 8a0aee43bcd..d4a3152dd21 100644 --- a/boards/risc-v/esp32c3/esp32c3-devkit/scripts/esp32c3_rom.ld +++ b/boards/risc-v/esp32c3/esp32c3-devkit/scripts/esp32c3_rom.ld @@ -2027,6 +2027,7 @@ __umoddi3 = 0x400008bc; __umodsi3 = 0x400008c0; __unorddf2 = 0x400008c4; __unordsf2 = 0x400008c8; +syscall_table_ptr = 0x3fcdffe0; /*************************************** Redefine functions