You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by pa...@apache.org on 2016/08/27 00:10:33 UTC

incubator-mynewt-core git commit: changes to support split images on nrf51dk

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop 079026143 -> 6d6d3210b


changes to support split images on nrf51dk

This is a good example of what you need to do to support split images.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/6d6d3210
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/6d6d3210
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/6d6d3210

Branch: refs/heads/develop
Commit: 6d6d3210ba5628702fd50315e896b6c53ecb1aa8
Parents: 0790261
Author: Paul Dietrich <pa...@yahoo.com>
Authored: Fri Aug 26 17:09:25 2016 -0700
Committer: Paul Dietrich <pa...@yahoo.com>
Committed: Fri Aug 26 17:10:22 2016 -0700

----------------------------------------------------------------------
 apps/splitty/src/main.c                         |   2 +-
 hw/bsp/nrf51dk/nrf51dk_download.sh              |  17 +-
 hw/bsp/nrf51dk/pkg.yml                          |   1 +
 hw/bsp/nrf51dk/split-nrf51dk.ld                 | 185 +++++++++++++++++++
 .../src/arch/cortex_m0/gcc_startup_nrf51.s      |   5 +
 .../arch/cortex_m0/gcc_startup_nrf51_split.s    | 168 +++++++++++++++++
 hw/bsp/nrf51dk/src/os_bsp.c                     |   8 +-
 hw/bsp/nrf51dk/src/sbrk.c                       |  18 +-
 8 files changed, 395 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6d6d3210/apps/splitty/src/main.c
----------------------------------------------------------------------
diff --git a/apps/splitty/src/main.c b/apps/splitty/src/main.c
index 838a4a7..682acfe 100755
--- a/apps/splitty/src/main.c
+++ b/apps/splitty/src/main.c
@@ -119,7 +119,7 @@ static struct conf_fcb my_conf = {
 #endif
 
 #define DEFAULT_MBUF_MPOOL_BUF_LEN (256)
-#define DEFAULT_MBUF_MPOOL_NBUFS (10)
+#define DEFAULT_MBUF_MPOOL_NBUFS (9)
 
 static uint8_t default_mbuf_mpool_data[DEFAULT_MBUF_MPOOL_BUF_LEN *
     DEFAULT_MBUF_MPOOL_NBUFS];

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6d6d3210/hw/bsp/nrf51dk/nrf51dk_download.sh
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf51dk/nrf51dk_download.sh b/hw/bsp/nrf51dk/nrf51dk_download.sh
index 2ee17e9..249cb55 100755
--- a/hw/bsp/nrf51dk/nrf51dk_download.sh
+++ b/hw/bsp/nrf51dk/nrf51dk_download.sh
@@ -29,8 +29,14 @@ if [ $# -lt 2 ]; then
     exit 1
 fi
 
+if [ $# -lt 3 ]; then
+    echo "Need image slot to download"
+    exit 1
+fi
+
 IS_BOOTLOADER=0
 BASENAME=$2
+IMAGE_SLOT=$3
 #JLINK_SCRIPT=.download.jlink
 GDB_CMD_FILE=.gdb_cmds
 
@@ -47,17 +53,24 @@ done
 if [ $IS_BOOTLOADER -eq 1 ]; then
     FLASH_OFFSET=0x0
     FILE_NAME=$BASENAME.elf.bin
-else
+elif [ $IMAGE_SLOT -eq 0 ]; then
     FLASH_OFFSET=0x8000
     FILE_NAME=$BASENAME.img
+elif [ $IMAGE_SLOT -eq 1 ]; then
+    FLASH_OFFSET=0x23800
+    FILE_NAME=$BASENAME.img
+else
+    echo "Invalid Image Slot Number: $IMAGE_SLOT"
+    exit 1
 fi
 
+
 echo "Downloading" $FILE_NAME "to" $FLASH_OFFSET
 
 # XXX for some reason JLinkExe overwrites flash at offset 0 when
 # downloading somewhere in the flash. So need to figure out how to tell it
 # not to do that, or report failure if gdb fails to write this file
-# 
+#
 echo "shell /bin/sh -c 'trap \"\" 2;JLinkGDBServer -device nRF51422_xxAC -speed 4000 -if SWD -port 3333 -singlerun' & " > $GDB_CMD_FILE
 echo "target remote localhost:3333" >> $GDB_CMD_FILE
 echo "restore $FILE_NAME binary $FLASH_OFFSET" >> $GDB_CMD_FILE

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6d6d3210/hw/bsp/nrf51dk/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf51dk/pkg.yml b/hw/bsp/nrf51dk/pkg.yml
index 4a1a93b..56de75a 100644
--- a/hw/bsp/nrf51dk/pkg.yml
+++ b/hw/bsp/nrf51dk/pkg.yml
@@ -30,6 +30,7 @@ pkg.arch: cortex_m0
 pkg.compiler: compiler/arm-none-eabi-m0
 pkg.linkerscript: "nrf51dk.ld"
 pkg.linkerscript.bootloader.OVERWRITE: "boot-nrf51dk.ld"
+pkg.part2linkerscript: "split-nrf51dk.ld"
 pkg.downloadscript: nrf51dk_download.sh
 pkg.debugscript: nrf51dk_debug.sh
 pkg.cflags: -DNRF51 -DBSP_HAS_32768_XTAL

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6d6d3210/hw/bsp/nrf51dk/split-nrf51dk.ld
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf51dk/split-nrf51dk.ld b/hw/bsp/nrf51dk/split-nrf51dk.ld
new file mode 100755
index 0000000..bd8c2be
--- /dev/null
+++ b/hw/bsp/nrf51dk/split-nrf51dk.ld
@@ -0,0 +1,185 @@
+/* Linker script for Nordic Semiconductor nRF5 devices
+ *
+ * Version: Sourcery G++ 4.5-1
+ * Support: https://support.codesourcery.com/GNUToolchain/
+ *
+ * Copyright (c) 2007, 2008, 2009, 2010 CodeSourcery, Inc.
+ *
+ * The authors hereby grant permission to use, copy, modify, distribute,
+ * and license this software and its documentation for any purpose, provided
+ * that existing copyright notices are retained in all copies and that this
+ * notice is included verbatim in any distributions.  No written agreement,
+ * license, or royalty fee is required for any of the authorized uses.
+ * Modifications to this software may be copyrighted by their authors
+ * and need not follow the licensing terms described here, provided that
+ * the new terms are clearly indicated on the first page of each file where
+ * they apply.
+ */
+OUTPUT_FORMAT ("elf32-littlearm", "elf32-bigarm", "elf32-littlearm")
+
+MEMORY
+{
+  FLASH (rx) : ORIGIN = 0x00023800, LENGTH = 0x1b800
+  RAM (rwx) :  ORIGIN = 0x20000000, LENGTH = 0x8000
+}
+
+/* Linker script to place sections and symbol values. Should be used together
+ * with other linker script that defines memory regions FLASH and RAM.
+ * It references following symbols, which must be defined in code:
+ *   Reset_Handler_split : Entry of reset handler
+ *
+ * It defines following symbols, which code can use without definition:
+ *   __exidx_start
+ *   __exidx_end
+ *   __etext
+ *   __data_start__
+ *   __preinit_array_start
+ *   __preinit_array_end
+ *   __init_array_start
+ *   __init_array_end
+ *   __fini_array_start
+ *   __fini_array_end
+ *   __data_end__
+ *   __bss_start__
+ *   __bss_end__
+ *   __HeapBase
+ *   __HeapLimit
+ *   __StackLimit
+ *   __StackTop
+ *   __stack
+ *   __bssnz_start__
+ *   __bssnz_end__
+ */
+ENTRY(Reset_Handler_split)
+
+SECTIONS
+{
+    .imghdr (NOLOAD):
+    {
+    	. = . + 0x20;
+    } > FLASH
+
+    .text :
+    {
+        __split_isr_vector_start = .;
+        KEEP(*(.isr_vector_split))
+        __split_isr_vector_end = .;
+        *(.text*)
+
+        KEEP(*(.init))
+        KEEP(*(.fini))
+
+        /* .ctors */
+        *crtbegin.o(.ctors)
+        *crtbegin?.o(.ctors)
+        *(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors)
+        *(SORT(.ctors.*))
+        *(.ctors)
+
+        /* .dtors */
+        *crtbegin.o(.dtors)
+        *crtbegin?.o(.dtors)
+        *(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors)
+        *(SORT(.dtors.*))
+        *(.dtors)
+
+        *(.rodata*)
+
+        *(.eh_frame*)
+        . = ALIGN(4);
+    } > FLASH
+
+
+    .ARM.extab :
+    {
+        *(.ARM.extab* .gnu.linkonce.armextab.*)
+        . = ALIGN(4);
+    } > FLASH
+
+    __exidx_start = .;
+    .ARM.exidx :
+    {
+        *(.ARM.exidx* .gnu.linkonce.armexidx.*)
+        . = ALIGN(4);
+    } > FLASH
+    __exidx_end = .;
+
+    __etext = .;
+
+    /* save RAM used by the split image. This assumes that
+     * the loader uses all the RAM up to its HeapBase  */
+    .loader_ram_contents :
+    {
+        _loader_ram_start = .;
+         /* this symbol comes from the loader linker */
+          . = . + (ABSOLUTE(__HeapBase_loader) - _loader_ram_start);
+         _loader_ram_end = .;
+    } > RAM
+
+    .data : AT (__etext)
+    {
+        __data_start__ = .;
+        *(.data*)
+
+        . = ALIGN(4);
+        /* preinit data */
+        PROVIDE_HIDDEN (__preinit_array_start = .);
+        *(.preinit_array)
+        PROVIDE_HIDDEN (__preinit_array_end = .);
+
+        . = ALIGN(4);
+        /* init data */
+        PROVIDE_HIDDEN (__init_array_start = .);
+        *(SORT(.init_array.*))
+        *(.init_array)
+        PROVIDE_HIDDEN (__init_array_end = .);
+
+
+        . = ALIGN(4);
+        /* finit data */
+        PROVIDE_HIDDEN (__fini_array_start = .);
+        *(SORT(.fini_array.*))
+        *(.fini_array)
+        PROVIDE_HIDDEN (__fini_array_end = .);
+
+        *(.jcr)
+        . = ALIGN(4);
+        /* All data end */
+        __data_end__ = .;
+    } > RAM
+
+    .bss :
+    {
+        . = ALIGN(4);
+        __bss_start__ = .;
+        *(.bss*)
+        *(COMMON)
+        . = ALIGN(4);
+        __bss_end__ = .;
+    } > RAM
+
+    /* Heap starts after BSS */
+    __HeapBase = .;
+
+    /* .stack_dummy section doesn't contains any symbols. It is only
+     * used for linker to calculate size of stack sections, and assign
+     * values to stack symbols later */
+    .stack_dummy (COPY):
+    {
+        *(.stack*)
+    } > RAM
+
+    _ram_start = ORIGIN(RAM);
+
+    /* Set stack top to end of RAM, and stack limit move down by
+     * size of stack_dummy section */
+    __StackTop = ORIGIN(RAM) + LENGTH(RAM);
+    __StackLimit = __StackTop - SIZEOF(.stack_dummy);
+    PROVIDE(__stack = __StackTop);
+
+    /* Top of head is the bottom of the stack */
+    __HeapLimit = __StackLimit;
+
+    /* Check if data + heap + stack exceeds RAM limit */
+    ASSERT(__HeapBase <= __HeapLimit, "region RAM overflowed with stack")
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6d6d3210/hw/bsp/nrf51dk/src/arch/cortex_m0/gcc_startup_nrf51.s
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf51dk/src/arch/cortex_m0/gcc_startup_nrf51.s b/hw/bsp/nrf51dk/src/arch/cortex_m0/gcc_startup_nrf51.s
index 431849d..515e30f 100755
--- a/hw/bsp/nrf51dk/src/arch/cortex_m0/gcc_startup_nrf51.s
+++ b/hw/bsp/nrf51dk/src/arch/cortex_m0/gcc_startup_nrf51.s
@@ -169,6 +169,11 @@ Reset_Handler:
     str    r0, [r2,r3]
     bgt    .LC1
 .LC0:
+    LDR     R0, =__HeapBase
+    LDR     R1, =__HeapLimit
+    BL      _sbrkInit
+
+    BL      bsp_slot_init_split_application
 
     LDR     R0, =SystemInit
     BLX     R0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6d6d3210/hw/bsp/nrf51dk/src/arch/cortex_m0/gcc_startup_nrf51_split.s
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf51dk/src/arch/cortex_m0/gcc_startup_nrf51_split.s b/hw/bsp/nrf51dk/src/arch/cortex_m0/gcc_startup_nrf51_split.s
new file mode 100755
index 0000000..545b975
--- /dev/null
+++ b/hw/bsp/nrf51dk/src/arch/cortex_m0/gcc_startup_nrf51_split.s
@@ -0,0 +1,168 @@
+/*
+Copyright (c) 2015, Nordic Semiconductor ASA
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright notice, this
+  list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above copyright notice,
+  this list of conditions and the following disclaimer in the documentation
+  and/or other materials provided with the distribution.
+
+* Neither the name of Nordic Semiconductor ASA nor the names of its
+  contributors may be used to endorse or promote products derived from
+  this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/*
+NOTE: Template files (including this one) are application specific and therefore
+expected to be copied into the application project folder prior to its use!
+*/
+
+    .syntax unified
+    .arch armv6-m
+
+    .section .stack
+    .align 3
+    .equ    Stack_Size, 432
+    .globl    __StackTop
+    .globl    __StackLimit
+__StackLimit:
+    .space    Stack_Size
+    .size __StackLimit, . - __StackLimit
+__StackTop:
+    .size __StackTop, . - __StackTop
+
+    .section .heap
+    .align 3
+#ifdef __HEAP_SIZE
+    .equ    Heap_Size, __HEAP_SIZE
+#else
+    .equ    Heap_Size, 0
+#endif
+    .globl    __HeapBase
+    .globl    __HeapLimit
+__HeapBase:
+    .if    Heap_Size
+    .space    Heap_Size
+    .endif
+    .size __HeapBase, . - __HeapBase
+__HeapLimit:
+    .size __HeapLimit, . - __HeapLimit
+
+    .section .isr_vector_split
+    .align 2
+    .globl __isr_vector_split
+__isr_vector_split:
+    .long    __StackTop                 /* Top of Stack */
+    .long   Reset_Handler_split         /* Reset Handler */
+
+    .size    __isr_vector_split, . - __isr_vector_split
+
+/* Reset Handler */
+
+    .equ    NRF_POWER_RAMON_ADDRESS,             0x40000524
+    .equ    NRF_POWER_RAMONB_ADDRESS,            0x40000554
+    .equ    NRF_POWER_RAMONx_RAMxON_ONMODE_Msk,  0x3
+
+    .text
+    .thumb
+    .thumb_func
+    .align 1
+    .globl    Reset_Handler_split
+    .type    Reset_Handler_split, %function
+Reset_Handler_split:
+    .fnstart
+
+/* Make sure ALL RAM banks are powered on */
+    MOVS    R1, #NRF_POWER_RAMONx_RAMxON_ONMODE_Msk
+
+    LDR     R0, =NRF_POWER_RAMON_ADDRESS
+    LDR     R2, [R0]
+    ORRS    R2, R1
+    STR     R2, [R0]
+
+    LDR     R0, =NRF_POWER_RAMONB_ADDRESS
+    LDR     R2, [R0]
+    ORRS    R2, R1
+    STR     R2, [R0]
+
+/*     Loop to copy data from read only memory to RAM. The ranges
+ *      of copy from/to are specified by following symbols evaluated in
+ *      linker script.
+ *      __etext: End of code section, i.e., begin of data sections to copy from.
+ *      __data_start__/__data_end__: RAM address range that data should be
+ *      copied to. Both must be aligned to 4 bytes boundary.  */
+
+    ldr    r1, =__etext
+    ldr    r2, =__data_start__
+    ldr    r3, =__data_end__
+
+    subs    r3, r2
+    ble     .LC0
+
+.LC1:
+    subs    r3, 4
+    ldr    r0, [r1,r3]
+    str    r0, [r2,r3]
+    bgt    .LC1
+.LC0:
+    ldr    r1, =__etext_loader
+    ldr    r2, =__data_start___loader
+    ldr    r3, =__data_end___loader
+
+    subs    r3, r2
+    ble     .LC2
+
+.LC3:
+    subs    r3, 4
+    ldr    r0, [r1,r3]
+    str    r0, [r2,r3]
+    bgt    .LC3
+.LC2:
+
+    subs    r0, r0
+    ldr    r2, =__bss_start___loader
+    ldr    r3, =__bss_end___loader
+
+    subs    r3, r2
+    ble     .LC4
+
+.LC5:
+    subs    r3, 4
+    str    r0, [r2,r3]
+    bgt    .LC5
+.LC4:
+    LDR     R0, =__HeapBase
+    LDR     R1, =__HeapLimit
+    BL      _sbrkInit
+
+    BL      bsp_slot_init_split_application
+
+    LDR     R0, =SystemInit
+    BLX     R0
+    LDR     R0, =_start
+    BX      R0
+
+    .pool
+    .cantunwind
+    .fnend
+    .size   Reset_Handler_split,.-Reset_Handler_split
+
+    .section ".text"
+
+  .end

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6d6d3210/hw/bsp/nrf51dk/src/os_bsp.c
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf51dk/src/os_bsp.c b/hw/bsp/nrf51dk/src/os_bsp.c
index 98088a7..59c4693 100644
--- a/hw/bsp/nrf51dk/src/os_bsp.c
+++ b/hw/bsp/nrf51dk/src/os_bsp.c
@@ -57,10 +57,16 @@ void _close(int fd);
  * this routine would have to figure out which one of those slots is being
  * used.
  */
+int current_image_slot = 1;
+
 int
 bsp_imgr_current_slot(void)
 {
-    return FLASH_AREA_IMAGE_0;
+    return current_image_slot;
+}
+
+void bsp_slot_init_split_application(void) {
+    current_image_slot = FLASH_AREA_IMAGE_1;
 }
 
 void

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6d6d3210/hw/bsp/nrf51dk/src/sbrk.c
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf51dk/src/sbrk.c b/hw/bsp/nrf51dk/src/sbrk.c
index 7daef1a..36a1af0 100644
--- a/hw/bsp/nrf51dk/src/sbrk.c
+++ b/hw/bsp/nrf51dk/src/sbrk.c
@@ -20,10 +20,18 @@
 #include <errno.h>
 #include <hal/hal_bsp.h>
 
-extern char __HeapBase;
-extern char __HeapLimit;
+/* put these in the data section so they are not cleared by _start */
+static char *sbrkBase __attribute__ ((section (".data")));
+static char *sbrkLimit __attribute__ ((section (".data")));
+static char *brk __attribute__ ((section (".data")));
+
+void
+_sbrkInit(char *base, char *limit) {
+    sbrkBase = base;
+    sbrkLimit = limit;
+    brk = base;
+}
 
-static char *brk = &__HeapBase;
 void *
 _sbrk(int incr)
 {
@@ -32,7 +40,7 @@ _sbrk(int incr)
     if (incr < 0) {
         /* Returning memory to the heap. */
         incr = -incr;
-        if (brk - incr < &__HeapBase) {
+        if (brk - incr < sbrkBase) {
             prev_brk = (void *)-1;
             errno = EINVAL;
         } else {
@@ -41,7 +49,7 @@ _sbrk(int incr)
         }
     } else {
         /* Allocating memory from the heap. */
-        if (&__HeapLimit - brk >= incr) {
+        if (sbrkLimit - brk >= incr) {
             prev_brk = brk;
             brk += incr;
         } else {