From ab59175427089774cc577c3d3dce2802c7de11d7 Mon Sep 17 00:00:00 2001 From: David Garske Date: Wed, 2 Oct 2024 15:33:10 -0700 Subject: [PATCH] Fix for nRF5340 `uart_write` issue with memchr args, which caused occasional corrupt characters to be printed. Improve core state description in logging. Enabled network core test app GPIO work on `LED2 P0.29`. --- config/examples/nrf5340.config | 4 +- config/examples/nrf5340_net.config | 2 +- docs/Targets.md | 150 ++++++++++++++++++--------- hal/nrf5340.c | 49 ++++++--- hal/nrf5340.h | 1 + test-app/app_nrf5340.c | 6 +- test-app/app_nrf5340_net.c | 6 +- tools/scripts/nrf5340/build_flash.sh | 18 +++- 8 files changed, 166 insertions(+), 70 deletions(-) diff --git a/config/examples/nrf5340.config b/config/examples/nrf5340.config index 1c2480e7..18b4c5b2 100644 --- a/config/examples/nrf5340.config +++ b/config/examples/nrf5340.config @@ -57,5 +57,5 @@ CFLAGS_EXTRA+=-DFLASHBUFFER_SIZE=0x1000 # Use UART0 on P0.22 #CFLAGS_EXTRA+=-DUART_PORT=0 -DUART_PIN=22 -CFLAGS_EXTRA+=-DDEBUG_FLASH -CFLAGS_EXTRA+=-DDEBUG_QSPI=1 +#CFLAGS_EXTRA+=-DDEBUG_FLASH +#CFLAGS_EXTRA+=-DDEBUG_QSPI=1 diff --git a/config/examples/nrf5340_net.config b/config/examples/nrf5340_net.config index 45d4d894..15381049 100644 --- a/config/examples/nrf5340_net.config +++ b/config/examples/nrf5340_net.config @@ -50,4 +50,4 @@ DEBUG?=0 DEBUG_UART?=1 USE_GCC=1 -CFLAGS_EXTRA+=-DDEBUG_FLASH +#CFLAGS_EXTRA+=-DDEBUG_FLASH diff --git a/docs/Targets.md b/docs/Targets.md index 845f3daa..de433d72 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -2195,73 +2195,129 @@ Application Core: ``` wolfBoot HAL Init (app core) -QSPI Freq=24MHz (Div Clk=3/Sck=1), Addr=24-bits, PageSz=256 -QSPI Activate -QSPI Flash ID (ret 0): 0xC2 0x28 0x17 -Status Reg: Ret 0, 0x40 (Quad Enabled: Yes) -QSPI Flash Read: Ret 0, Cmd 0xEB, Len 256 , 0x0 -> 0x2000012C -Update partition: 0x0 (sz 7864, ver 0x2, type 0x201) -QSPI Flash Read: Ret 0, Cmd 0xEB, Len 256 , 0x100000 -> 0x2000012C -Update partition: 0x100000 (sz 5780, ver 0x2, type 0x202) -Network Image: Ver 0x2, Size 5780 -Network Core: Holding for update -Status: App 1 (ver 2), Net 1 (ver 1) -QSPI Flash ID (ret 0): 0xC2 0x28 0x17 -Status Reg: Ret 0, 0x40 (Quad Enabled: Yes) -QSPI Flash Read: Ret 0, Cmd 0xEB, Len 4 , 0xEDFFC -> 0x20000230 -QSPI Flash Read: Ret 0, Cmd 0xEB, Len 4 , 0xEDFFC -> 0x20000230 -Boot partition: 0xC000 (sz 7864, ver 0x1, type 0x201) -QSPI Flash Read: Ret 0, Cmd 0xEB, Len 256 , 0x0 -> 0x2000012C -Update partition: 0x0 (sz 7864, ver 0x2, type 0x201) -QSPI Flash Read: Ret 0, Cmd 0xEB, Len 4 , 0xEDFFC -> 0x20000230 -Boot partition: 0xC000 (sz 7864, ver 0x1, type 0x201) +Boot header magic 0x00000000 invalid at 0x20000128 +Update partition: 0x100000 (sz 4120, ver 0x1, type 0x202) +Network Image: Update not found +Network Core: Releasing for boot +Status: App 8 (ver 0), Net 1 (ver 1) +Boot partition: 0xC000 (sz 4832, ver 0x1, type 0x201) +Boot header magic 0x00000000 invalid at 0x20000128 +Boot partition: 0xC000 (sz 4832, ver 0x1, type 0x201) Booting version: 0x1 -QSPI Flash Read: Ret 0, Cmd 0xEB, Len 256 , 0x0 -> 0x2000012C -Update partition: 0x0 (sz 7864, ver 0x2, type 0x201) -QSPI Flash Read: Ret 0, Cmd 0xEB, Len 256 , 0x100000 -> 0x2000012C -Update partition: 0x100000 (sz 5780, ver 0x2, type 0x202) -Network Image: Ver 0x2, Size 5780 -Found Network Core update: Ver 1->2, Size 6036->6036 -QSPI Flash Read: Ret 0, Cmd 0xEB, Len 256 , 0x101700 -> 0x2000002C -Network image valid, loading into shared mem -QSPI Flash Read: Ret 0, Cmd 0xEB, Len 6036 , 0x100000 -> 0x20010020 -Network core firmware update done +Waiting for network core... ======================== nRF5340 wolfBoot (app core) Copyright 2024 wolfSSL Inc GPL v3 Version : 0x1 ======================== -Internal Flash Write: addr 0xF9FFC, len 4 -Internal Flash Write: addr 0xF9FFB, len 1 ``` Network Core: ``` wolfBoot HAL Init (net core) -Boot partition: 0x100C000 (sz 5780, ver 0x1, type 0x201) -Network Image: Ver 0x1, Size 5780 -Starting update: Ver 1->2, Size 6036->6036 -Internal Flash Erase: addr 0x100C000, len 6036 -Internal Flash Erase: page 0x100C000 -Internal Flash Erase: page 0x100C800 -Internal Flash Erase: page 0x100D000 -Internal Flash Write: addr 0x100C000, len 6036 -Boot partition: 0x100C000 (sz 5780, ver 0x2, type 0x202) -Network Image: Ver 0x2, Size 5780 -Network version (after update): 0x2 -Status: App 2 (ver 2), Net 4 (ver 2) -Boot partition: 0x100C000 (sz 5780, ver 0x2, type 0x202) +Boot partition: 0x100C000 (sz 4120, ver 0x1, type 0x202) +Network Image: Ver 0x1, Size 4120 +Waiting for status from app core... +Status: App 8 (ver 0), Net 1 (ver 2) +Boot partition: 0x100C000 (sz 4120, ver 0x1, type 0x202) +Boot header magic 0xF7E99810 invalid at 0x21000128 +Boot partition: 0x100C000 (sz 4120, ver 0x1, type 0x202) +Booting version: 0x1 +======================== +nRF5340 wolfBoot (net core) +Copyright 2024 wolfSSL Inc +GPL v3 +Version : 0x1 +======================== +``` + +Example output when doing an update: + +Application Core: + +``` +wolfBoot HAL Init (app core) +Update partition: 0x0 (sz 4832, ver 0x2, type 0x201) +Network Image: Ver 0x2, Size 4832 +Found Network Core update: Ver 1->2, Size 4376->5088 +Network image valid, loading into shared mem +Waiting for net core update to finish... +Network core firmware update done +Status: App 8 (ver 2), Net 4 (ver 2) +Update partition: 0x0 (sz 4832, ver 0x2, type 0x201) +Boot partition: 0xC000 (sz 4832, ver 0x1, type 0x201) +Update partition: 0x0 (sz 4832, ver 0x2, type 0x201) +Staring Update (fallback allowed 0) +Update partition: 0x0 (sz 4832, ver 0x2, type 0x201) +Boot partition: 0xC000 (sz 4832, ver 0x1, type 0x201) +Versions: Current 0x1, Update 0x2 +Copy sector 0 (part 1->2) +Copy sector 0 (part 0->1) +Copy sector 0 (part 2->0) +Boot partition: 0xC000 (sz 4832, ver 0x2, type 0x201) +Boot header magic 0x00000000 invalid at 0x20000128 +Copy sector 1 (part 1->2) +Copy sector 1 (part 0->1) +Copy sector 1 (part 2->0) +Erasing remainder of partitions (235 sectors)... +Boot partition: 0xC000 (sz 4832, ver 0x2, type 0x201) +Boot header magic 0x00000000 invalid at 0x20000128 +Copy sector 236 (part 0->2) +Boot partition: 0xC000 (sz 4832, ver 0x2, type 0x201) Booting version: 0x2 +Waiting for network core... +======================== +nRF5340 wolfBoot (app core) +Copyright 2024 wolfSSL Inc +GPL v3 +Version : 0x2 +======================== +``` + +Network Core: + +``` +wolfBoot HAL Init (net core) +Boot partition: 0x100C000 (sz 4120, ver 0x1, type 0x201) +Network Image: Ver 0x1, Size 4120 +Waiting for status from app core... +Starting update: Ver 1->2, Size 4376->4376 +Status: App 2 (ver 2), Net 1 (ver 1) +Update partition: 0x100000 (sz 4120, ver 0x2, type 0x202) +Boot partition: 0x100C000 (sz 4120, ver 0x1, type 0x201) +Update partition: 0x100000 (sz 4120, ver 0x2, type 0x202) +Staring Update (fallback allowed 0) +Update partition: 0x100000 (sz 4120, ver 0x2, type 0x202) +Boot partition: 0x100C000 (sz 4120, ver 0x1, type 0x201) +Versions: Current 0x1, Update 0x2 +Copy sector 0 (part 1->2) +Copy sector 0 (part 0->1) +Copy sector 0 (part 2->0) +Boot partition: 0x100C000 (sz 4120, ver 0x2, type 0x202) +Update partition: 0x100000 (sz 4120, ver 0x1, type 0x201) +Copy sector 1 (part 1->2) +Copy sector 1 (part 0->1) +Copy sector 1 (part 2->0) +Copy sector 2 (part 1->2) +Copy sector 2 (part 0->1) +Copy sector 2 (part 2->0) +Erasing remainder of partitions (88 sectors)... +Boot partition: 0x100C000 (sz 4120, ver 0x2, type 0x202) +Update partition: 0x100000 (sz 4120, ver 0x1, type 0x201) +Copy sector 90 (part 0->2) +Boot partition: 0x100C000 (sz 4120, ver 0x2, type 0x202) +Booting version: 0x2 +Boot partition: 0x100C000 (sz 4120, ver 0x2, type 0x202) +Network Image: Ver 0x2, Size 4120 +Network version (after update): 0x2 ======================== nRF5340 wolfBoot (net core) Copyright 2024 wolfSSL Inc GPL v3 Version : 0x2 ======================== -Internal Flash Write: addr 0x1039FFC, len 4 -Internal Flash Write: addr 0x1039FFB, len 1 ``` ### Building / Flashing Nordic nRF5340 diff --git a/hal/nrf5340.c b/hal/nrf5340.c index aa3a0dfd..aece4610 100644 --- a/hal/nrf5340.c +++ b/hal/nrf5340.c @@ -98,9 +98,11 @@ static int do_update = 0; #define UART_SEL 0 /* select UART 0 or 1 */ #endif #if !defined(UART_PORT) && !defined(UART_PIN) - #if UART_SEL == 0 && !defined(TARGET_nrf5340_net) + #if defined(TARGET_nrf5340_app) #define UART_PORT 0 #define UART_PIN 20 + #define UART_NET_PORT 1 + #define UART_NET_PIN 1 #else #define UART_PORT 1 #define UART_PIN 1 @@ -109,8 +111,9 @@ static int do_update = 0; void uart_init(void) { - /* nRF5340-DK: - * App: UART0=P1.01, UART1=P0.20 */ + /* nRF5340-DK: (P0.20 or P1.01) + * App: UART0=P0.20 + * Net: UART0=P1.01 */ UART_ENABLE(UART_SEL) = 0; GPIO_PIN_CNF(UART_PORT, UART_PIN) = (GPIO_CNF_OUT #ifdef TARGET_nrf5340_net @@ -122,9 +125,11 @@ void uart_init(void) UART_CONFIG(UART_SEL) = 0; /* Flow=Diabled, Stop=1-bit, Parity exclude */ UART_ENABLE(UART_SEL) = 8; - /* allow network core access to P1.01 - must be set from application core */ -#ifdef TARGET_nrf5340_app - GPIO_PIN_CNF(1, 1) = (GPIO_CNF_OUT | GPIO_CNF_MCUSEL(1)); + /* allow network core access to UART pin - must be set from app core */ +#if defined(TARGET_nrf5340_app) && \ + defined(UART_NET_PORT) && defined(UART_NET_PIN) + GPIO_PIN_CNF(UART_NET_PORT, UART_NET_PIN) = + (GPIO_CNF_OUT | GPIO_CNF_MCUSEL(1)); #endif } @@ -160,18 +165,20 @@ void uart_write(const char* buf, unsigned int sz) unsigned int lineSz; do { /* find `\n` */ - line = memchr(buf, sz, '\n'); + line = memchr(buf, '\n', sz); if (line == NULL) { uart_write_sz(buf, sz); break; } lineSz = line - buf; + if (lineSz > sz-1) + lineSz = sz-1; - uart_write_sz(line, lineSz); - uart_write_sz("\r", 1); /* handle CRLF */ + uart_write_sz(buf, lineSz); + uart_write_sz("\r\n", 2); /* handle CRLF */ buf = line; - sz -= lineSz; + sz -= lineSz + 1; /* skip \n, already sent */ } while ((int)sz > 0); } #endif /* DEBUG_UART */ @@ -427,6 +434,23 @@ static int hal_shm_status_wait(ShmInfo_t* info, uint32_t status, return ret; } +static const char* hal_shm_status_string(uint32_t status) +{ + switch (status) { + case SHARED_STATUS_READY: + return "Ready"; + case SHARED_STATUS_UPDATE_START: + return "Update Start"; + case SHARED_STATUS_UPDATE_DONE: + return "Update Done"; + case SHARED_STATUS_DO_BOOT: + return "Do boot"; + default: + break; + } + return "Unknown"; +} + /* Handles network core updates */ static void hal_net_check_version(void) { @@ -510,8 +534,9 @@ static void hal_net_check_version(void) } #endif /* TARGET_nrf5340_* */ exit: - wolfBoot_printf("Status: App %d (ver %d), Net %d (ver %d)\n", - shm->app.status, shm->app.version, shm->net.status, shm->net.version); + wolfBoot_printf("Status: App %s (ver %d), Net %s (ver %d)\n", + hal_shm_status_string(shm->app.status), shm->app.version, + hal_shm_status_string(shm->net.status), shm->net.version); } void hal_init(void) diff --git a/hal/nrf5340.h b/hal/nrf5340.h index 750aece5..535c0fe5 100644 --- a/hal/nrf5340.h +++ b/hal/nrf5340.h @@ -236,6 +236,7 @@ void sleep_us(unsigned int us); #endif #define UART_TASK_STARTTX(n) *((volatile uint32_t *)(UART_BASE(n) + 0x008)) #define UART_TASK_STOPTX(n) *((volatile uint32_t *)(UART_BASE(n) + 0x00C)) +#define UART_EVENT_TXDRDY(n) *((volatile uint32_t *)(UART_BASE(n) + 0x11C)) #define UART_EVENT_ENDTX(n) *((volatile uint32_t *)(UART_BASE(n) + 0x120)) #define UART_ENABLE(n) *((volatile uint32_t *)(UART_BASE(n) + 0x500)) #define UART_PSEL_TXD(n) *((volatile uint32_t *)(UART_BASE(n) + 0x50C)) diff --git a/test-app/app_nrf5340.c b/test-app/app_nrf5340.c index e2018344..67d35fd2 100644 --- a/test-app/app_nrf5340.c +++ b/test-app/app_nrf5340.c @@ -45,7 +45,9 @@ void main(void) uint32_t pin = 28; uint32_t app_version; - GPIO_PIN_CNF(port, pin) = 1; /* Output */ + GPIO_PIN_CNF(port, pin) = GPIO_CNF_OUT; + /* Allow network core access to P0.29 GPIO */ + GPIO_PIN_CNF(0, 29) = (GPIO_CNF_OUT | GPIO_CNF_MCUSEL(1)); app_version = wolfBoot_current_firmware_version(); @@ -55,7 +57,7 @@ void main(void) wolfBoot_printf("nRF5340 wolfBoot (app core)\n"); wolfBoot_printf("Copyright 2024 wolfSSL Inc\n"); wolfBoot_printf("GPL v3\n"); - wolfBoot_printf("Version : 0x%lx\r\n", app_version); + wolfBoot_printf("Version : 0x%lx\n", app_version); wolfBoot_printf("========================\n"); /* mark boot successful */ diff --git a/test-app/app_nrf5340_net.c b/test-app/app_nrf5340_net.c index 2a6605a6..806d5b7f 100644 --- a/test-app/app_nrf5340_net.c +++ b/test-app/app_nrf5340_net.c @@ -42,10 +42,10 @@ void main(void) * LED3 P0.30 * LED4 P0.31 */ uint32_t port = 0; - uint32_t pin = 28; + uint32_t pin = 29; uint32_t app_version; - GPIO_PIN_CNF(port, pin) = 1; /* Output */ + GPIO_PIN_CNF(port, pin) = GPIO_CNF_OUT; app_version = wolfBoot_current_firmware_version(); @@ -55,7 +55,7 @@ void main(void) wolfBoot_printf("nRF5340 wolfBoot (net core)\n"); wolfBoot_printf("Copyright 2024 wolfSSL Inc\n"); wolfBoot_printf("GPL v3\n"); - wolfBoot_printf("Version : 0x%lx\r\n", app_version); + wolfBoot_printf("Version : 0x%lx\n", app_version); wolfBoot_printf("========================\n"); /* mark boot successful */ diff --git a/tools/scripts/nrf5340/build_flash.sh b/tools/scripts/nrf5340/build_flash.sh index 6fe7d8ba..c8eebe94 100755 --- a/tools/scripts/nrf5340/build_flash.sh +++ b/tools/scripts/nrf5340/build_flash.sh @@ -4,6 +4,17 @@ # run from wolfBoot root # ./tools/scripts/nrf5340/build_flash.sh +# optionally run with "erase" argument to rease both internal and external flash +# or provide make arguments "DEBUG=1" + + +if [ "$1" == "erase" ]; then + DO_ERASE=1 + MAKE_ARGS="$2" +else + DO_ERASE=0 + MAKE_ARGS="$1" +fi rm -f ./tools/scripts/nrf5340/*.bin rm -f ./tools/scripts/nrf5340/*.hex @@ -13,7 +24,7 @@ rm -f ./tools/scripts/nrf5340/*.hex # Build net cp config/examples/nrf5340_net.config .config make clean -make DEBUG=1 +make $MAKE_ARGS cp factory.bin tools/scripts/nrf5340/factory_net.bin # Sign flash update for testing (use partition type 2 for network update) tools/keytools/sign --ecc256 --id 2 test-app/image.bin wolfboot_signing_private_key.der 2 @@ -22,7 +33,8 @@ cp test-app/image_v2_signed.bin tools/scripts/nrf5340/image_v2_signed_net.bin # Build app cp config/examples/nrf5340.config .config make clean -make DEBUG=1 +make $MAKE_ARGS + cp factory.bin tools/scripts/nrf5340/factory_app.bin # Sign flash update for testing tools/keytools/sign --ecc256 test-app/image.bin wolfboot_signing_private_key.der 2 @@ -44,7 +56,7 @@ arm-none-eabi-objcopy -I binary -O ihex --change-addresses 0x10000000 tools/scri arm-none-eabi-objcopy -I binary -O ihex --change-addresses 0x10100000 tools/scripts/nrf5340/image_v2_signed_net.bin tools/scripts/nrf5340/image_v2_signed_net.hex -if [ "$1" == "erase" ]; then +if [ "$DO_ERASE" == "1" ]; then nrfjprog -f nrf53 --recover nrfjprog -f nrf53 --qspieraseall fi