3838 * Any key size greater than 128 bits must be divided and distributed over multiple key slot instances.
3939 */
4040
41+ #define USE_RTC 0 /* Use RTC0 for sleep */
42+
4143/* Network updates can be signed with "--id 2" and placed into the normal update partition,
4244 * or they can be placed into the external flash at offset 0x100000 */
4345/* Partition ID should be set HDR_IMG_TYPE_APP=2 */
@@ -321,28 +323,38 @@ void ext_flash_unlock(void)
321323
322324static void clock_init (void )
323325{
324- #ifndef TARGET_nrf5340_net
326+ #ifdef TARGET_nrf5340_app
325327 CLOCK_HFCLKSRC = 1 ; /* use external high frequency clock */
326328 CLOCK_HFCLKSTART = 1 ;
327329 /* wait for high frequency clock startup */
328330 while (CLOCK_HFCLKSTARTED == 0 );
329331#endif
332+ /* Start low frequency clock - used by RTC */
333+ CLOCK_LFCLKSRC = 0 ; /* internal low power */
334+ CLOCK_LFCLKSTART = 1 ;
335+ /* wait for high frequency clock startup */
336+ while (CLOCK_LFCLKSTARTED == 0 );
337+
338+ RTC_PRESCALER (USE_RTC ) = 0 ; /* 32768 per second */
330339}
331340
332- void sleep_us (unsigned int us )
341+ void sleep_us (uint32_t usec )
333342{
334- /* Calculate ops per us (128MHz=128 instructions per 1us */
335- unsigned long nop_us = (CPU_CLOCK / 1000000 );
336- nop_us *= us ;
337- /* instruction for each iteration */
338- #ifdef DEBUG
339- nop_us /= 30 ;
340- #else
341- nop_us /= 5 ;
342- #endif
343- while (nop_us -- > 0 ) {
344- NOP ();
345- }
343+ /* Calculate number ticks to wait */
344+ uint32_t comp = ((usec * 32768UL ) / 1000000 );
345+ if (comp == 0 )
346+ comp = 1 ; /* wait at least 1 tick */
347+ if (comp > RTC_OVERFLOW )
348+ comp = RTC_OVERFLOW ; /* max wait (512 seconds with prescaler=0) */
349+
350+ RTC_CLEAR (USE_RTC ) = 1 ;
351+ RTC_EVTENSET (USE_RTC ) = RTC_EVTENSET_CC0 ;
352+ RTC_EVENT_CC (USE_RTC , 0 ) = 0 ; /* clear compare event */
353+ RTC_CC (USE_RTC , 0 ) = comp ;
354+ RTC_START (USE_RTC ) = 1 ;
355+ /* wait for compare event */
356+ while (RTC_EVENT_CC (USE_RTC , 0 ) == 0 );
357+ RTC_STOP (USE_RTC ) = 1 ;
346358}
347359
348360#ifdef TARGET_nrf5340_app
@@ -427,15 +439,14 @@ static void hal_shm_status_set(ShmInfo_t* info, uint32_t status)
427439}
428440
429441static int hal_shm_status_wait (ShmInfo_t * info , uint32_t status ,
430- uint32_t timeout_us )
442+ uint32_t timeout_ms )
431443{
432444 int ret = 0 ;
433- uint32_t timeout = timeout_us ;
434445 while ((info -> magic != SHAREM_MEM_MAGIC || (info -> status & status ) == 0 )
435- && -- timeout > 0 ) {
436- sleep_us (1 );
446+ && -- timeout_ms > 0 ) {
447+ sleep_us (1000 );
437448 };
438- if (timeout == 0 ) {
449+ if (timeout_ms == 0 ) {
439450 wolfBoot_printf ("Timeout: status 0x%x\n" , status );
440451 ret = -1 ; /* timeout */
441452 }
@@ -474,8 +485,8 @@ static void hal_net_check_version(void)
474485 /* release network core - issue boot command */
475486 hal_net_core (0 );
476487
477- /* wait for ready status from network core */
478- ret = hal_shm_status_wait (& shm -> net , SHARED_STATUS_READY , 1000000 );
488+ /* wait for ready status from network core - 2 seconds */
489+ ret = hal_shm_status_wait (& shm -> net , SHARED_STATUS_READY , 2 * 1000 );
479490
480491 /* check if network core can continue booting or needs to wait for update */
481492 if (ret != 0 || shm -> app .version <= shm -> net .version ) {
@@ -505,9 +516,9 @@ static void hal_net_check_version(void)
505516
506517 wolfBoot_printf ("Waiting for net core update to finish...\n" );
507518
508- /* wait for update_done - note longer wait */
519+ /* wait for update_done - note longer wait - 10 seconds */
509520 ret = hal_shm_status_wait (& shm -> net ,
510- SHARED_STATUS_UPDATE_DONE , 5000000 );
521+ SHARED_STATUS_UPDATE_DONE , 10 * 1000 );
511522 if (ret == 0 ) {
512523 wolfBoot_printf ("Network core firmware update done\n" );
513524 }
@@ -524,10 +535,10 @@ static void hal_net_check_version(void)
524535 hal_net_get_image (& img , & shm -> net );
525536 hal_shm_status_set (& shm -> net , SHARED_STATUS_READY );
526537
527- /* wait for do_boot or update from app core */
538+ /* wait for do_boot or update from app core - 2 seconds */
528539 wolfBoot_printf ("Waiting for status from app core...\n" );
529540 ret = hal_shm_status_wait (& shm -> app ,
530- (SHARED_STATUS_UPDATE_START | SHARED_STATUS_DO_BOOT ), 1000000 );
541+ (SHARED_STATUS_UPDATE_START | SHARED_STATUS_DO_BOOT ), 2 * 1000 );
531542
532543 /* are we updating? */
533544 if (ret == 0 && shm -> app .status == SHARED_STATUS_UPDATE_START ) {
@@ -601,10 +612,11 @@ void hal_prepare_boot(void)
601612
602613#ifdef TARGET_nrf5340_app
603614#ifdef NRF_SYNC_CORES
604- /* if core synchronization enabled, then wait for update_done or do_boot */
615+ /* if core synchronization enabled,
616+ * then wait for update_done or do_boot (5 seconds) */
605617 wolfBoot_printf ("Waiting for network core...\n" );
606618 (void )hal_shm_status_wait (& shm -> net ,
607- (SHARED_STATUS_UPDATE_DONE | SHARED_STATUS_DO_BOOT ), 2000000 );
619+ (SHARED_STATUS_UPDATE_DONE | SHARED_STATUS_DO_BOOT ), 5 * 1000 );
608620#endif /* NRF_SYNC_CORES */
609621#endif /* TARGET_nrf5340_app */
610622#endif /* !DISABLE_SHARED_MEM */
0 commit comments