Skip to content

Commit f6c1283

Browse files
dgarskedanielinux
authored andcommitted
Enable ECC384 and SHA384. Fix sleep_us. Add Cortex-M hard-fault handler support.
1 parent 1a38293 commit f6c1283

11 files changed

Lines changed: 137 additions & 14 deletions

File tree

config/examples/nrf5340.config

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
ARCH?=ARM
22
TZEN?=0
33
TARGET?=nrf5340
4-
SIGN?=ECC256
5-
HASH?=SHA256
4+
SIGN?=ECC384
5+
HASH?=SHA384
66
WOLFBOOT_VERSION?=1
77
VTOR?=1
88
CORTEX_M0?=0
@@ -44,6 +44,7 @@ V?=0
4444
DEBUG?=0
4545
DEBUG_UART?=1
4646
USE_GCC=1
47+
OPTIMIZATION_LEVEL=2
4748

4849
# Optionally wait for network core to boot before starting application core
4950
CFLAGS_EXTRA+=-DNRF_SYNC_CORES
@@ -59,3 +60,6 @@ CFLAGS_EXTRA+=-DFLASHBUFFER_SIZE=0x1000
5960

6061
#CFLAGS_EXTRA+=-DDEBUG_FLASH
6162
#CFLAGS_EXTRA+=-DDEBUG_QSPI=1
63+
64+
# Hard fault debugging
65+
#CFLAGS_EXTRA+=-DDEBUG_HARDFAULT

config/examples/nrf5340_net.config

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
ARCH?=ARM
22
TZEN?=0
33
TARGET?=nrf5340_net
4-
SIGN?=ECC256
5-
HASH?=SHA256
4+
SIGN?=ECC384
5+
HASH?=SHA384
66
WOLFBOOT_VERSION?=1
77
VTOR?=1
88
CORTEX_M0?=0
@@ -49,5 +49,12 @@ V?=0
4949
DEBUG?=0
5050
DEBUG_UART?=1
5151
USE_GCC=1
52+
OPTIMIZATION_LEVEL=2
5253

5354
#CFLAGS_EXTRA+=-DDEBUG_FLASH
55+
56+
# If debugging network core disable the shared memory, since app core has not enabled access to it
57+
#CFLAGS_EXTRA+=-DDISABLE_SHARED_MEM
58+
59+
# Hard fault debugging
60+
#CFLAGS_EXTRA+=-DDEBUG_HARDFAULT

hal/nrf5340.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ typedef struct {
8585
/* used as "swap" */
8686
uint8_t swap[FLASH_PAGESZ_NET];
8787
} SharedMem_t;
88+
#ifndef DISABLE_SHARED_MEM
8889
static SharedMem_t* shm = (SharedMem_t*)SHARED_MEM_ADDR;
90+
#endif /* !DISABLE_SHARED_MEM */
8991

9092
#ifdef TARGET_nrf5340_net
9193
static int do_update = 0;
@@ -275,7 +277,9 @@ int ext_flash_write(uintptr_t address, const uint8_t *data, int len)
275277
wolfBoot_printf("Ext Write: Len %d, Addr 0x%x (off 0x%x) -> 0x%x\n",
276278
len, address, addr, data);
277279
#endif
280+
#ifndef DISABLE_SHARED_MEM
278281
memcpy(shm->data + addr, data, len);
282+
#endif
279283
return 0;
280284
}
281285

@@ -286,8 +290,9 @@ int ext_flash_read(uintptr_t address, uint8_t *data, int len)
286290
wolfBoot_printf("Ext Read: Len %d, Addr 0x%x (off 0x%x) -> %p\n",
287291
len, address, addr, data);
288292
#endif
289-
293+
#ifndef DISABLE_SHARED_MEM
290294
memcpy(data, shm->data + addr, len);
295+
#endif
291296
return len;
292297
}
293298

@@ -298,7 +303,9 @@ int ext_flash_erase(uintptr_t address, int len)
298303
wolfBoot_printf("Ext Erase: Len %d, Addr 0x%x (off 0x%x)\n",
299304
len, address, addr);
300305
#endif
306+
#ifndef DISABLE_SHARED_MEM
301307
memset(shm->data + addr, FLASH_BYTE_ERASED, len);
308+
#endif
302309
return 0;
303310
}
304311

@@ -325,13 +332,13 @@ static void clock_init(void)
325332
void sleep_us(unsigned int us)
326333
{
327334
/* Calculate ops per us (128MHz=128 instructions per 1us */
328-
unsigned long nop_us = (CPU_CLOCK / 10000000);
335+
unsigned long nop_us = (CPU_CLOCK / 1000000);
329336
nop_us *= us;
330337
/* instruction for each iteration */
331338
#ifdef DEBUG
332-
nop_us /= 5;
339+
nop_us /= 30;
333340
#else
334-
nop_us /= 2;
341+
nop_us /= 5;
335342
#endif
336343
while (nop_us-- > 0) {
337344
NOP();
@@ -358,6 +365,7 @@ void hal_net_core(int hold) /* 1=hold, 0=release */
358365
}
359366
#endif
360367

368+
#ifndef DISABLE_SHARED_MEM
361369
static uint8_t* get_image_hdr(struct wolfBoot_image* img)
362370
{
363371
#ifdef EXT_FLASH
@@ -538,6 +546,8 @@ static void hal_net_check_version(void)
538546
hal_shm_status_string(shm->app.status), shm->app.version,
539547
hal_shm_status_string(shm->net.status), shm->net.version);
540548
}
549+
#endif /* !DISABLE_SHARED_MEM */
550+
541551

542552
void hal_init(void)
543553
{
@@ -561,7 +571,9 @@ void hal_init(void)
561571
/* need early init of external flash to support checking network core */
562572
spi_flash_probe();
563573

574+
#ifndef DISABLE_SHARED_MEM
564575
hal_net_check_version();
576+
#endif
565577
}
566578

567579

@@ -571,6 +583,7 @@ void hal_prepare_boot(void)
571583
//WOLFBOOT_ORIGIN
572584
//BOOTLOADER_PARTITION_SIZE
573585

586+
#ifndef DISABLE_SHARED_MEM
574587
#ifdef TARGET_nrf5340_net
575588
if (do_update) {
576589
/* signal application core update done */
@@ -591,9 +604,12 @@ void hal_prepare_boot(void)
591604
/* if core synchronization enabled, then wait for update_done or do_boot */
592605
wolfBoot_printf("Waiting for network core...\n");
593606
(void)hal_shm_status_wait(&shm->net,
594-
(SHARED_STATUS_UPDATE_DONE | SHARED_STATUS_DO_BOOT), 1000000);
595-
#endif
607+
(SHARED_STATUS_UPDATE_DONE | SHARED_STATUS_DO_BOOT), 2000000);
608+
#endif /* NRF_SYNC_CORES */
609+
#endif /* TARGET_nrf5340_app */
610+
#endif /* !DISABLE_SHARED_MEM */
596611

612+
#ifdef TARGET_nrf5340_app
597613
/* Restore defaults preventing network core from accessing shared SDRAM */
598614
SPU_EXTDOMAIN_PERM(0) =
599615
(SPU_EXTDOMAIN_PERM_SECATTR_NONSECURE | SPU_EXTDOMAIN_PERM_UNLOCK);

hal/nrf5340.ld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ SECTIONS
1111
{
1212
_start_text = .;
1313
KEEP(*(.isr_vector))
14+
*(.boot*)
1415
*(.text*)
1516
*(.rodata*)
1617
*(.init*)

hal/nrf5340_net.ld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ SECTIONS
1010
{
1111
_start_text = .;
1212
KEEP(*(.isr_vector))
13+
*(.boot*)
1314
*(.text*)
1415
*(.rodata*)
1516
*(.init*)

include/loader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,10 @@ static inline void wolfBoot_panic(void)
102102
wolfBoot_panicked++;
103103
}
104104
#else
105+
#include "printf.h"
105106
static inline void wolfBoot_panic(void)
106107
{
108+
wolfBoot_printf("wolfBoot: PANIC!\n");
107109
while(1)
108110
;
109111
}

src/boot_arm.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "image.h"
2525
#include "loader.h"
26+
#include "printf.h"
2627
#include "wolfboot/wolfboot.h"
2728

2829
extern unsigned int _start_text;
@@ -296,11 +297,97 @@ asm(
296297

297298
#endif /* CORTEX_R5 */
298299

300+
#ifdef DEBUG_HARDFAULT
301+
__attribute__((section(".boot"))) __attribute__((used))
302+
void HardFault_HandlerC( uint32_t *hardfault_args )
303+
{
304+
/* Using volatile to prevent the compiler/linker optimizing them out */
305+
volatile uint32_t stacked_r0;
306+
volatile uint32_t stacked_r1;
307+
volatile uint32_t stacked_r2;
308+
volatile uint32_t stacked_r3;
309+
volatile uint32_t stacked_r12;
310+
volatile uint32_t stacked_lr;
311+
volatile uint32_t stacked_pc;
312+
volatile uint32_t stacked_psr;
313+
volatile uint32_t _CFSR;
314+
volatile uint32_t _HFSR;
315+
volatile uint32_t _DFSR;
316+
volatile uint32_t _AFSR;
317+
volatile uint32_t _BFAR;
318+
volatile uint32_t _MMAR;
319+
320+
stacked_r0 = ((uint32_t)hardfault_args[0]);
321+
stacked_r1 = ((uint32_t)hardfault_args[1]);
322+
stacked_r2 = ((uint32_t)hardfault_args[2]);
323+
stacked_r3 = ((uint32_t)hardfault_args[3]);
324+
stacked_r12 = ((uint32_t)hardfault_args[4]);
325+
stacked_lr = ((uint32_t)hardfault_args[5]);
326+
stacked_pc = ((uint32_t)hardfault_args[6]);
327+
stacked_psr = ((uint32_t)hardfault_args[7]);
328+
329+
/* Configurable Fault Status Register */
330+
/* Consists of MMSR, BFSR and UFSR */
331+
_CFSR = (*((volatile uint32_t *)(0xE000ED28)));
332+
/* Hard Fault Status Register */
333+
_HFSR = (*((volatile uint32_t *)(0xE000ED2C)));
334+
/* Debug Fault Status Register */
335+
_DFSR = (*((volatile uint32_t *)(0xE000ED30)));
336+
/* Auxiliary Fault Status Register */
337+
_AFSR = (*((volatile uint32_t *)(0xE000ED3C)));
338+
/* MemManage Fault Address Register */
339+
_MMAR = (*((volatile uint32_t *)(0xE000ED34)));
340+
/* Bus Fault Address Register */
341+
_BFAR = (*((volatile uint32_t *)(0xE000ED38)));
342+
343+
wolfBoot_printf("\n\nHard fault handler (all numbers in hex):\n");
344+
wolfBoot_printf("R0 = %lx\n", stacked_r0);
345+
wolfBoot_printf("R1 = %lx\n", stacked_r1);
346+
wolfBoot_printf("R2 = %lx\n", stacked_r2);
347+
wolfBoot_printf("R3 = %lx\n", stacked_r3);
348+
wolfBoot_printf("R12 = %lx\n", stacked_r12);
349+
wolfBoot_printf("LR [R14] = %lx subroutine call return address\n",
350+
stacked_lr);
351+
wolfBoot_printf("PC [R15] = %lx program counter\n", stacked_pc);
352+
wolfBoot_printf("PSR = %lx\n", stacked_psr);
353+
wolfBoot_printf("CFSR = %lx\n", _CFSR);
354+
wolfBoot_printf("HFSR = %lx\n", _HFSR);
355+
wolfBoot_printf("DFSR = %lx\n", _DFSR);
356+
wolfBoot_printf("AFSR = %lx\n", _AFSR);
357+
wolfBoot_printf("MMAR = %lx\n", _MMAR);
358+
wolfBoot_printf("BFAR = %lx\n", _BFAR);
359+
360+
/* Break into the debugger */
361+
__asm("BKPT #0\n");
362+
}
363+
364+
__attribute__((section(".boot"))) __attribute__((naked))
365+
void isr_fault(void)
366+
{
367+
__asm volatile
368+
(
369+
" movs r0,#4 \n" /* load bit mask into R0 */
370+
" mov r1, lr \n" /* load link register into R1 */
371+
" tst r0, r1 \n" /* compare with bitmask */
372+
" beq _MSP \n" /* if bitmask is set: stack pointer is in PSP. Otherwise in MSP */
373+
" mrs r0, psp \n" /* otherwise: stack pointer is in PSP */
374+
" b _GetPC \n" /* go to part which loads the PC */
375+
"_MSP: \n" /* stack pointer is in MSP register */
376+
" mrs r0, msp \n" /* load stack pointer into R0 */
377+
"_GetPC: \n" /* find out where the hard fault happened */
378+
" ldr r1,[r0,#20] \n" /* load program counter into R1. R1 contains address of the next instruction where the hard fault happened */
379+
" ldr r2, =HardFault_HandlerC \n"
380+
" bx r2 \n"
381+
" bx lr \n" /* decode more information. R0 contains pointer to stack frame */
382+
);
383+
}
384+
#else
299385
void isr_fault(void)
300386
{
301387
/* Panic. */
302388
wolfBoot_panic();
303389
}
390+
#endif /* DEBUG_HARDFAULT */
304391

305392
void isr_empty(void)
306393
{

src/loader.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
#include "image.h"
3434
#include "hal.h"
3535
#include "spi_flash.h"
36+
#ifdef UART_FLASH
3637
#include "uart_flash.h"
38+
#endif
3739
#include "wolfboot/wolfboot.h"
3840

3941
#ifdef WOLFBOOT_TPM

test-app/app_nrf5340.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@ void main(void)
6767
while (1) {
6868
gpiotoggle(port, pin);
6969

70-
sleep_us(100 * 1000);
70+
sleep_us(500 * 1000);
7171
}
7272
}

test-app/app_nrf5340_net.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ void main(void)
6565
while (1) {
6666
gpiotoggle(port, pin);
6767

68-
sleep_us(100 * 1000);
68+
sleep_us(500 * 1000);
6969
}
7070
}

0 commit comments

Comments
 (0)