Browse Source

[bsp/nrf5x] fix gcc compile exception and add onchip filesystem

Signed-off-by: chenyingchun0312 <chenyingchun0312@163.com>
chenyingchun0312 4 years ago
parent
commit
670000dd0f

+ 3 - 0
bsp/nrf5x/libraries/drivers/SConscript

@@ -40,6 +40,9 @@ if GetDepend(['BSP_USING_WDT']):
 if GetDepend(['BSP_USING_ONCHIP_RTC']):
     src += ['drv_rtc.c']
 
+if GetDepend(['BSP_USING_ON_CHIP_FS']):
+    src += ['drv_fs.c']
+
 path =  [cwd]
 
 group = DefineGroup('Drivers', src, depend = [''], CPPPATH = path)

+ 100 - 0
bsp/nrf5x/libraries/drivers/drv_fs.c

@@ -0,0 +1,100 @@
+/*
+ * Copyright (c) 2006-2021, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ *  on chip filesystem support
+ * Change Logs:
+ * Date         Author        Notes
+ * 2021-06-27   Chenyingchun  first version
+ */
+#include "board.h"
+#include <rtthread.h>
+#include <rtdevice.h>
+
+#ifdef BSP_USING_ON_CHIP_FS
+
+#ifndef PKG_USING_FAL
+#error "if you want to use on chip filesystem, you need to enable FAL package()"
+#endif
+
+#ifndef RT_USING_DFS
+#error "if you want to use on chip filesystem, you need to enable DFS componment"
+#endif
+
+#ifndef BSP_USING_ON_CHIP_FLASH
+#error "if you want to use on chip filesystem, you need to enable on-chip flash"
+#endif
+
+#ifndef RT_USING_MTD_NOR
+#error "if you want to use on chip filesystem, you need to enable mtd nor"
+#endif
+
+#include "fal.h"
+#include <dfs_posix.h>
+
+#define LOG_TAG             "drv.fs"
+#define DBG_LVL              DBG_LOG
+#include <rtdbg.h>
+
+#define FS_PARTITION_NAME ON_CHIP_PARTION_NAME
+
+
+/**
+  * @brief  on chip filesystem init
+  * @param  void
+  * @retval 0: filesystem init success, -1: filesystem init failed
+  */
+
+static int on_chip_fs_init(void)
+{
+    int result = 0;
+    
+    fal_init();
+
+    struct rt_device *flash_dev = fal_mtd_nor_device_create(FS_PARTITION_NAME);
+
+    if (flash_dev == NULL)
+    {
+        LOG_E("Can't create a block device on '%s' partition.", FS_PARTITION_NAME);
+        result = -1;
+        goto err;
+    }
+    else
+    {
+        LOG_D("Create a block device on the %s partition of flash successful.", FS_PARTITION_NAME);
+    }
+
+    if (rt_device_find(FS_PARTITION_NAME) != RT_NULL)
+    {
+        int mkfs_res = dfs_mkfs("lfs", FS_PARTITION_NAME);
+
+        if (mkfs_res != 0)
+        {
+            LOG_E("dfs_mkfs error, errno = %d", rt_get_errno());
+            result = -1;
+            goto err;
+        }
+
+        if (dfs_mount(FS_PARTITION_NAME, "/", "lfs", 0, 0) == RT_EOK)
+        {
+            LOG_D("onchip elm filesystem mount to '/'");
+        }
+        else
+        {
+            LOG_E("onchip elm filesystem mount to '/' failed!");
+            result = -1;
+            goto err;
+        }
+    }
+    else
+    {
+        LOG_E("find filesystem portion failed");
+    }
+err:
+    return result;
+}
+
+INIT_ENV_EXPORT(on_chip_fs_init);
+
+#endif /* BSP_USING_ON_CHIP_FS */

+ 4 - 0
bsp/nrf5x/nrf52832/board/Kconfig

@@ -341,6 +341,10 @@ menu "On-chip Peripheral Drivers"
         select PKG_USING_FAL
         bool "Enable on-chip FLASH"
         default n
+    
+    config BSP_USING_ON_CHIP_FS
+        bool "Enable on-chip filesystem" 
+        default n
 
     menu "On-chip flash config"
 

+ 34 - 0
bsp/nrf5x/nrf52832/board/board.c

@@ -49,6 +49,40 @@ void SysTick_Configuration(void)
 
 }
 
+/**
+ * The time delay function.
+ *
+ * @param microseconds.
+ */
+void rt_hw_us_delay(rt_uint32_t us)
+{
+    rt_uint32_t ticks;
+    rt_uint32_t told, tnow, tcnt = 0;
+    rt_uint32_t reload = SysTick->LOAD;
+
+    ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
+    told = SysTick->VAL;
+    while (1)
+    {
+        tnow = SysTick->VAL;
+        if (tnow != told)
+        {
+            if (tnow < told)
+            {
+                tcnt += told - tnow;
+            }
+            else
+            {
+                tcnt += reload - tnow + told;
+            }
+            told = tnow;
+            if (tcnt >= ticks)
+            {
+                break;
+            }
+        }
+    }
+}
 
 void rt_hw_board_init(void)
 {

+ 51 - 0
bsp/nrf5x/nrf52832/board/fal_cfg.h

@@ -0,0 +1,51 @@
+/*
+ * File      : fal_cfg.h
+ * This file is part of FAL (Flash Abstraction Layer) package
+ * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Change Logs:
+ * Date           Author        Notes
+ * 2021-06-26     chenyingchun  the first version
+ */
+
+#ifndef _FAL_CFG_H_
+#define _FAL_CFG_H_
+
+#include <rtconfig.h>
+#include <board.h>
+
+#define ON_CHIP_FLASH_DEV_NAME  "mcu_onchip_flash"
+#define ON_CHIP_PARTION_NAME    "filesystem"
+
+/* ===================== Flash device Configuration ========================= */
+extern const struct fal_flash_dev mcu_onchip_flash;
+
+/* flash device table */
+#define FAL_FLASH_DEV_TABLE \
+    {                       \
+        &mcu_onchip_flash,  \
+    }
+/* ====================== Partition Configuration ========================== */
+#ifdef FAL_PART_HAS_TABLE_CFG
+/* partition table */
+#define FAL_PART_TABLE                                                                                  \
+    {                                                                                                   \
+        {FAL_PART_MAGIC_WORD, ON_CHIP_PARTION_NAME, ON_CHIP_FLASH_DEV_NAME, 224 * 1024, 120 * 1024, 0}, \
+    }
+#endif /* FAL_PART_HAS_TABLE_CFG */
+
+#endif /* _FAL_CFG_H_ */

+ 155 - 7
bsp/nrf5x/nrf52832/board/linker_scripts/link.lds

@@ -1,16 +1,164 @@
 /* Linker script to configure memory regions. */
 
-SEARCH_DIR(.)
-GROUP(-lgcc -lc -lnosys)
-
 MEMORY
 {
-  FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x80000
-  RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x10000
-  CODE_RAM (rwx) : ORIGIN = 0x800000, LENGTH = 0x10000
+  ROM (rx) : ORIGIN = 0x00000000, LENGTH = 0x80000  /* 512K FLASH */
+  RAM (rw) : ORIGIN = 0x20000000, LENGTH = 0x10000  /* 64K RAM    */
 }
+ENTRY(Reset_Handler)
+_system_stack_size = 0x200;
+
+SECTIONS
+{
+    .text :
+    {
+        . = ALIGN(4);
+        _stext = .;
+        KEEP(*(.isr_vector))            /* Startup code */
+
+        . = ALIGN(4);
+        *(.text)                        /* remaining code */
+        *(.text.*)                      /* remaining code */
+        *(.rodata)                      /* read-only data (constants) */
+        *(.rodata*)
+        *(.glue_7)
+        *(.glue_7t)
+        *(.gnu.linkonce.t*)
+
+        /* section information for finsh shell */
+        . = ALIGN(4);
+        __fsymtab_start = .;
+        KEEP(*(FSymTab))
+        __fsymtab_end = .;
+
+        . = ALIGN(4);
+        __vsymtab_start = .;
+        KEEP(*(VSymTab))
+        __vsymtab_end = .;
+
+        /* section information for initial. */
+        . = ALIGN(4);
+        __rt_init_start = .;
+        KEEP(*(SORT(.rti_fn*)))
+        __rt_init_end = .;
+
+        /* section information for modules */
+        . = ALIGN(4);
+        __rtmsymtab_start = .;
+        KEEP(*(RTMSymTab))
+        __rtmsymtab_end = .;
+
+        . = ALIGN(4);
+        
+        PROVIDE(__ctors_start__ = .);
+        KEEP (*(SORT(.init_array.*)))
+        KEEP (*(.init_array))
+        PROVIDE(__ctors_end__ = .);
+
+        . = ALIGN(4);
+
+        _etext = .;
+    } > ROM = 0
 
-INCLUDE "packages/nrfx-v2.1.0/mdk/nrf_common.ld"
+    /* .ARM.exidx is sorted, so has to go in its own output section.  */
+    __exidx_start = .;
+    .ARM.exidx :
+    {
+        *(.ARM.exidx* .gnu.linkonce.armexidx.*)
 
+        /* This is used by the startup in order to initialize the .data secion */
+        _sidata = .;
+    } > ROM
+    __exidx_end = .;
 
+    /* .data section which is used for initialized data */
+    .data : AT (_sidata)
+    {
+        . = ALIGN(4);
+        /* This is used by the startup in order to initialize the .data secion */
+        _sdata = . ;
 
+        *(.data)
+        *(.data.*)
+        *(.gnu.linkonce.d*)
+
+        PROVIDE(__dtors_start__ = .);
+        KEEP(*(SORT(.dtors.*)))
+        KEEP(*(.dtors))
+        PROVIDE(__dtors_end__ = .);
+
+        . = ALIGN(4);
+        /* This is used by the startup in order to initialize the .data secion */
+        _edata = . ;
+    } >RAM
+
+    .stack : 
+    {
+        . = ALIGN(4);
+        _sstack = .;
+        . = . + _system_stack_size;
+        . = ALIGN(4);
+        _estack = .;
+    } >RAM
+
+    __bss_start = .;
+    .bss :
+    {
+        . = ALIGN(4);
+        /* This is used by the startup in order to initialize the .bss secion */
+        _sbss = .;
+
+        *(.bss)
+        *(.bss.*)
+        *(COMMON)
+
+        . = ALIGN(4);
+        /* This is used by the startup in order to initialize the .bss secion */
+        _ebss = . ;
+        
+        *(.bss.init)
+    } > RAM
+    __bss_end = .;
+
+    _end = .;
+
+    PROVIDE(__etext = __exidx_end);
+    PROVIDE(__data_start__ = _sdata);
+    PROVIDE(__bss_start__ = __bss_start);
+    PROVIDE(__bss_end__ = __bss_end);
+    PROVIDE(__StackTop = _estack);
+
+    /* Stabs debugging sections.  */
+    .stab          0 : { *(.stab) }
+    .stabstr       0 : { *(.stabstr) }
+    .stab.excl     0 : { *(.stab.excl) }
+    .stab.exclstr  0 : { *(.stab.exclstr) }
+    .stab.index    0 : { *(.stab.index) }
+    .stab.indexstr 0 : { *(.stab.indexstr) }
+    .comment       0 : { *(.comment) }
+    /* DWARF debug sections.
+     * Symbols in the DWARF debugging sections are relative to the beginning
+     * of the section so we begin them at 0.  */
+    /* DWARF 1 */
+    .debug          0 : { *(.debug) }
+    .line           0 : { *(.line) }
+    /* GNU DWARF 1 extensions */
+    .debug_srcinfo  0 : { *(.debug_srcinfo) }
+    .debug_sfnames  0 : { *(.debug_sfnames) }
+    /* DWARF 1.1 and DWARF 2 */
+    .debug_aranges  0 : { *(.debug_aranges) }
+    .debug_pubnames 0 : { *(.debug_pubnames) }
+    /* DWARF 2 */
+    .debug_info     0 : { *(.debug_info .gnu.linkonce.wi.*) }
+    .debug_abbrev   0 : { *(.debug_abbrev) }
+    .debug_line     0 : { *(.debug_line) }
+    .debug_frame    0 : { *(.debug_frame) }
+    .debug_str      0 : { *(.debug_str) }
+    .debug_loc      0 : { *(.debug_loc) }
+    .debug_macinfo  0 : { *(.debug_macinfo) }
+    /* SGI/MIPS DWARF 2 extensions */
+    .debug_weaknames 0 : { *(.debug_weaknames) }
+    .debug_funcnames 0 : { *(.debug_funcnames) }
+    .debug_typenames 0 : { *(.debug_typenames) }
+    .debug_varnames  0 : { *(.debug_varnames) }
+}

+ 10 - 1
bsp/nrf5x/nrf52832/rtconfig.py

@@ -13,7 +13,7 @@ if os.getenv('RTT_CC'):
 
 if  CROSS_TOOL == 'gcc':
     PLATFORM    = 'gcc'
-    EXEC_PATH   = 'D:/SourceryGCC/bin'
+    EXEC_PATH   = r'D:\RT-ThreadStudio\repo\Extract\ToolChain_Support_Packages\ARM\GNU_Tools_for_ARM_Embedded_Processors\5.4.1\bin'
 elif CROSS_TOOL == 'keil':
     PLATFORM    = 'armcc'
     EXEC_PATH   = 'C:/Keil_v5'
@@ -34,17 +34,26 @@ if PLATFORM == 'gcc':
     CC = PREFIX + 'gcc'
     AS = PREFIX + 'gcc'
     AR = PREFIX + 'ar'
+    CXX= PREFIX + 'g++'
     LINK = PREFIX + 'gcc'
     TARGET_EXT = 'elf'
     SIZE = PREFIX + 'size'
     OBJDUMP = PREFIX + 'objdump'
     OBJCPY = PREFIX + 'objcopy'
+    STRIP  = PREFIX + 'strip'
 
     DEVICE = ' -mcpu=cortex-m4 -mthumb -ffunction-sections -fdata-sections'
     CFLAGS = DEVICE
     AFLAGS = ' -c' + DEVICE + ' -x assembler-with-cpp'
     LFLAGS = DEVICE + ' -Wl,--gc-sections,-Map=rtthread.map,-cref,-u,Reset_Handler -T board/linker_scripts/link.lds'
 
+    CXXFLAGS = CFLAGS 
+    M_CFLAGS = CFLAGS + ' -mlong-calls -fPIC '
+    M_CXXFLAGS = CXXFLAGS + ' -mlong-calls -fPIC'
+    M_LFLAGS = DEVICE + CXXFLAGS + ' -Wl,--gc-sections,-z,max-page-size=0x4' +\
+                                    ' -shared -fPIC -nostartfiles -nostdlib -static-libgcc'
+    M_POST_ACTION = STRIP + ' -R .hash $TARGET\n' + SIZE + ' $TARGET \n'
+
     CPATH = ''
     LPATH = ''