You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2021/06/18 05:53:54 UTC

[incubator-nuttx] 05/06: arch/risc-v/esp32c3: Create a separate heap for the RTC memory.

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit f54804bafccb34be4c25138acbc4282563a1e187
Author: Abdelatif Guettouche <ab...@espressif.com>
AuthorDate: Wed Jun 16 17:27:13 2021 +0100

    arch/risc-v/esp32c3: Create a separate heap for the RTC memory.
    
    Signed-off-by: Abdelatif Guettouche <ab...@espressif.com>
---
 arch/risc-v/src/esp32c3/Kconfig                    |   4 +
 arch/risc-v/src/esp32c3/Make.defs                  |   4 +
 arch/risc-v/src/esp32c3/esp32c3_rtc_heap.c         | 193 +++++++++++++++++++++
 arch/risc-v/src/esp32c3/esp32c3_rtc_heap.h         | 146 ++++++++++++++++
 .../esp32c3/esp32c3-devkit/scripts/esp32c3.ld      |   5 +
 .../esp32c3-devkit/scripts/esp32c3.template.ld     |   3 +
 6 files changed, 355 insertions(+)

diff --git a/arch/risc-v/src/esp32c3/Kconfig b/arch/risc-v/src/esp32c3/Kconfig
index e1daf10..adda87b 100644
--- a/arch/risc-v/src/esp32c3/Kconfig
+++ b/arch/risc-v/src/esp32c3/Kconfig
@@ -157,6 +157,10 @@ config ESP32C3_DISABLE_STDC_ATOMIC
 	bool "Disable standard C atomic"
 	default n
 
+config ESP32C3_RTC_HEAP
+	bool "Use the RTC memory as a separate heap"
+	default n
+
 menu "ESP32-C3 Peripheral Support"
 
 config ESP32C3_ADC
diff --git a/arch/risc-v/src/esp32c3/Make.defs b/arch/risc-v/src/esp32c3/Make.defs
index 7c9c981..20b6aa7 100644
--- a/arch/risc-v/src/esp32c3/Make.defs
+++ b/arch/risc-v/src/esp32c3/Make.defs
@@ -164,6 +164,10 @@ ifeq ($(CONFIG_RTC_DRIVER),y)
 CHIP_CSRCS += esp32c3_rtc_lowerhalf.c
 endif
 
+ifeq ($(CONFIG_ESP32C3_RTC_HEAP),y)
+CHIP_CSRCS += esp32c3_rtc_heap.c
+endif
+
 ifeq ($(CONFIG_ESP32C3_WIRELESS),y)
 WIRELESS_DRV_UNPACK  = esp-wireless-drivers-3rdparty
 WIRELESS_DRV_ID      = 2b53111
diff --git a/arch/risc-v/src/esp32c3/esp32c3_rtc_heap.c b/arch/risc-v/src/esp32c3/esp32c3_rtc_heap.c
new file mode 100644
index 0000000..2d56794
--- /dev/null
+++ b/arch/risc-v/src/esp32c3/esp32c3_rtc_heap.c
@@ -0,0 +1,193 @@
+/****************************************************************************
+ * arch/risc-v/src/esp32c3/esp32c3_rtc_heap.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 <nuttx/config.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/fs/procfs.h>
+#include <nuttx/mm/mm.h>
+#include <malloc.h>
+
+#include "esp32c3_rtc_heap.h"
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct mm_heap_s g_rtc_heap;
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32c3_rtc_heap_initialize
+ *
+ * Description:
+ *   Initialize the RTC heap.
+ *
+ ****************************************************************************/
+
+void esp32c3_rtc_heap_initialize(void)
+{
+  void  *start;
+  size_t size;
+
+  /* These values come from the linker scripts (esp32c3.ld and
+   * esp32c3.template.ld.)  Check boards/risc-v/esp32c3.
+   */
+
+  extern uint8_t *_srtc_heap;
+  extern uint8_t *_ertc_heap;
+
+  start = (FAR void *)&_srtc_heap;
+  size  = (size_t)((uintptr_t)&_ertc_heap - (uintptr_t)&_srtc_heap);
+  mm_initialize(&g_rtc_heap, start, size);
+
+#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MEMINFO)
+  static struct procfs_meminfo_entry_s g_imm_procfs;
+
+  g_imm_procfs.name = "rtc_heap";
+  g_imm_procfs.mallinfo = (void *)mm_mallinfo;
+  g_imm_procfs.user_data = &g_rtc_heap;
+  procfs_register_meminfo(&g_imm_procfs);
+#endif
+}
+
+/****************************************************************************
+ * Name: esp32c3_rtc_heap_malloc
+ *
+ * Description:
+ *   Allocate memory from the RTC heap.
+ *
+ ****************************************************************************/
+
+void *esp32c3_rtc_heap_malloc(size_t size)
+{
+  return mm_malloc(&g_rtc_heap, size);
+}
+
+/****************************************************************************
+ * Name: esp32c3_rtc_heap_calloc
+ *
+ * Description:
+ *   Calculates the size of the allocation and allocate memory from
+ *   the RTC heap.
+ *
+ ****************************************************************************/
+
+void *esp32c3_rtc_heap_calloc(size_t n, size_t elem_size)
+{
+  return mm_calloc(&g_rtc_heap, n, elem_size);
+}
+
+/****************************************************************************
+ * Name: esp32c3_rtc_heap_realloc
+ *
+ * Description:
+ *   Reallocate memory from the RTC heap.
+ *
+ ****************************************************************************/
+
+void *esp32c3_rtc_heap_realloc(void *ptr, size_t size)
+{
+  return mm_realloc(&g_rtc_heap, ptr, size);
+}
+
+/****************************************************************************
+ * Name: esp32c3_rtc_heap_zalloc
+ *
+ * Description:
+ *   Allocate and zero memory from the RTC heap.
+ *
+ ****************************************************************************/
+
+void *esp32c3_rtc_heap_zalloc(size_t size)
+{
+  return mm_zalloc(&g_rtc_heap, size);
+}
+
+/****************************************************************************
+ * Name: esp32c3_rtc_heap_free
+ *
+ * Description:
+ *   Free memory from the RTC heap.
+ *
+ ****************************************************************************/
+
+void esp32c3_rtc_heap_free(FAR void *mem)
+{
+  mm_free(&g_rtc_heap, mem);
+}
+
+/****************************************************************************
+ * Name: esp32c3_rtc_heap_memalign
+ *
+ * Description:
+ *   memalign requests more than enough space from malloc, finds a region
+ *   within that chunk that meets the alignment request and then frees any
+ *   leading or trailing space.
+ *
+ *   The alignment argument must be a power of two (not checked).  8-byte
+ *   alignment is guaranteed by normal malloc calls.
+ *
+ ****************************************************************************/
+
+void *esp32c3_rtc_heap_memalign(size_t alignment, size_t size)
+{
+  return mm_memalign(&g_rtc_heap, alignment, size);
+}
+
+/****************************************************************************
+ * Name: esp32c3_rtc_heap_heapmember
+ *
+ * Description:
+ *   Check if an address lies in the RTC heap.
+ *
+ * Parameters:
+ *   mem - The address to check
+ *
+ * Return Value:
+ *   true if the address is a member of the RTC heap.  false if not
+ *
+ ****************************************************************************/
+
+bool esp32c3_rtc_heap_heapmember(FAR void *mem)
+{
+  return mm_heapmember(&g_rtc_heap, mem);
+}
+
+/****************************************************************************
+ * Name: esp32c3_rtc_heap_mallinfo
+ *
+ * Description:
+ *   mallinfo returns a copy of updated current heap information for the
+ *   user heap.
+ *
+ ****************************************************************************/
+
+int esp32c3_rtc_heap_mallinfo(FAR struct mallinfo *info)
+{
+  return mm_mallinfo(&g_rtc_heap, info);
+}
diff --git a/arch/risc-v/src/esp32c3/esp32c3_rtc_heap.h b/arch/risc-v/src/esp32c3/esp32c3_rtc_heap.h
new file mode 100644
index 0000000..31558cb
--- /dev/null
+++ b/arch/risc-v/src/esp32c3/esp32c3_rtc_heap.h
@@ -0,0 +1,146 @@
+/****************************************************************************
+ * arch/risc-v/src/esp32c3/esp32c3_rtc_heap.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_ESP32C3_RTC_HEAP_H
+#define __ARCH_RISCV_SRC_ESP32C3_ESP32C3_RTC_HEAP_H
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+struct mallinfo; /* Forward reference, see malloc.h */
+
+/****************************************************************************
+ * Name: esp32c3_rtc_heap_initialize
+ *
+ * Description:
+ *   Initialize the RTC heap.
+ *
+ ****************************************************************************/
+
+void esp32c3_rtc_heap_initialize(void);
+
+/****************************************************************************
+ * Name: esp32c3_rtc_heap_malloc
+ *
+ * Description:
+ *   Allocate memory from the RTC heap.
+ *
+ ****************************************************************************/
+
+void *esp32c3_rtc_heap_malloc(size_t size);
+
+/****************************************************************************
+ * Name: esp32c3_rtc_heap_calloc
+ *
+ * Description:
+ *   Calculates the size of the allocation and allocate memory from
+ *   the RTC heap.
+ *
+ ****************************************************************************/
+
+void *esp32c3_rtc_heap_calloc(size_t n, size_t elem_size);
+
+/****************************************************************************
+ * Name: esp32c3_rtc_heap_realloc
+ *
+ * Description:
+ *   Reallocate memory from the RTC heap.
+ *
+ ****************************************************************************/
+
+void *esp32c3_rtc_heap_realloc(void *ptr, size_t size);
+
+/****************************************************************************
+ * Name: esp32c3_rtc_heap_zalloc
+ *
+ * Description:
+ *   Allocate and zero memory from the RTC heap.
+ *
+ ****************************************************************************/
+
+void *esp32c3_rtc_heap_zalloc(size_t size);
+
+/****************************************************************************
+ * Name: esp32c3_rtc_heap_free
+ *
+ * Description:
+ *   Free memory from the RTC heap.
+ *
+ ****************************************************************************/
+
+void esp32c3_rtc_heap_free(FAR void *mem);
+
+/****************************************************************************
+ * Name: esp32c3_rtc_heap_memalign
+ *
+ * Description:
+ *   memalign requests more than enough space from malloc, finds a region
+ *   within that chunk that meets the alignment request and then frees any
+ *   leading or trailing space.
+ *
+ *   The alignment argument must be a power of two (not checked). 8-byte
+ *   alignment is guaranteed by normal malloc calls.
+ *
+ ****************************************************************************/
+
+void *esp32c3_rtc_heap_memalign(size_t alignment, size_t size);
+
+/****************************************************************************
+ * Name: esp32c3_rtc_heap_heapmember
+ *
+ * Description:
+ *   Check if an address lies in the RTC heap.
+ *
+ * Parameters:
+ *   mem - The address to check
+ *
+ * Return Value:
+ *   true if the address is a member of the RTC heap. false if not
+ *
+ ****************************************************************************/
+
+bool esp32c3_rtc_heap_heapmember(FAR void *mem);
+
+/****************************************************************************
+ * Name: esp32c3_rtc_heap_mallinfo
+ *
+ * Description:
+ *   mallinfo returns a copy of updated current heap information for the
+ *   user heap.
+ *
+ ****************************************************************************/
+
+int esp32c3_rtc_heap_mallinfo(FAR struct mallinfo *info);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ARCH_RISCV_SRC_ESP32C3_ESP32C3_RTC_HEAP_H */
diff --git a/boards/risc-v/esp32c3/esp32c3-devkit/scripts/esp32c3.ld b/boards/risc-v/esp32c3/esp32c3-devkit/scripts/esp32c3.ld
index 8115572..f6945fd 100644
--- a/boards/risc-v/esp32c3/esp32c3-devkit/scripts/esp32c3.ld
+++ b/boards/risc-v/esp32c3/esp32c3-devkit/scripts/esp32c3.ld
@@ -247,6 +247,11 @@ SECTIONS
   {
     *(.rtc.data)
     *(.rtc.rodata)
+
+   /* Whatever is left from the RTC memory is used as a special heap. */
+
+    _srtc_heap = ABSOLUTE(.);
+
   } >rtc_seg
 }
 
diff --git a/boards/risc-v/esp32c3/esp32c3-devkit/scripts/esp32c3.template.ld b/boards/risc-v/esp32c3/esp32c3-devkit/scripts/esp32c3.template.ld
index 9b34679..01dfc45 100644
--- a/boards/risc-v/esp32c3/esp32c3-devkit/scripts/esp32c3.template.ld
+++ b/boards/risc-v/esp32c3/esp32c3-devkit/scripts/esp32c3.template.ld
@@ -89,3 +89,6 @@ MEMORY
   REGION_ALIAS("default_code_seg", iram0_2_seg);
 #endif /* CONFIG_ESP32C3_DEVKIT_RUN_IRAM */
 
+/* Mark the end of the RTC heap (top of the RTC region) */
+
+_ertc_heap = 0x50001fff;