Nordic Semiconductor nRF5 AirFuel SDK  version 2.2.0
common.c
Go to the documentation of this file.
1 /* Copyright (c) Nordic Semiconductor. All Rights Reserved.
2  *
3  * The information contained herein is property of Nordic Semiconductor ASA.
4  * Terms and conditions of usage are described in detail in NORDIC
5  * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
6  *
7  * Licensees are granted free, non-transferable use of the information. NO
8  * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
9  * the file.
10  *
11  */
12 
13 
16 #include <stdint.h>
17 
18 #include "nrf_soc.h"
19 #include "app_error.h"
20 #include "nrf_sdm.h"
21 
22 #ifdef DFU_SUPPORT
23  #include "bootloader_types.h"
24  #include "bootloader_util.h"
25  #define NOF_IRQS 32
26 #endif // DFU_SUPPORT
27 
28 #define MAX_RTC_COUNTER_VAL 0x00FFFFFF
30 /******************************************************************************/
31 
33 /******************************************************************************/
34 
35 
36 #ifdef DFU_SUPPORT
37 
39 static void interrupts_disable(void)
40 {
41  uint32_t interrupt_setting_mask;
42  uint32_t irq = 0; // We start from first interrupt, i.e. interrupt 0.
43 
44  // Fetch the current interrupt settings.
45  interrupt_setting_mask = NVIC->ISER[0];
46 
47  for (; irq < NOF_IRQS; irq++)
48  {
49  if (interrupt_setting_mask & (0x01 << irq))
50  {
51  // The interrupt was enabled, and hence disable it.
52  NVIC_DisableIRQ((IRQn_Type)irq);
53  }
54  }
55 }
56 #endif // DFU_SUPPORT
57 
60 /******************************************************************************/
63 /******************************************************************************/
64 
65 void power_manage(void)
66 {
67  uint32_t err_code = sd_app_evt_wait();
68  APP_ERROR_CHECK(err_code);
69 }
70 
71 
72 void system_off(void)
73 {
74  uint32_t err_code = sd_power_system_off();
75  APP_ERROR_CHECK(err_code);
76 }
77 
78 uint32_t ticks_diff(uint32_t ticks_now, uint32_t ticks_old)
79 {
80  if(ticks_old>ticks_now) //Overflow occured
81  {
82  return (((MAX_RTC_COUNTER_VAL-ticks_old)+ticks_now) & MAX_RTC_COUNTER_VAL);
83  }
84  else
85  {
86  return ((ticks_now-ticks_old) & MAX_RTC_COUNTER_VAL);
87  }
88 }
89 
90 
91 #ifdef DFU_SUPPORT
92 
93 void dfu_start(void)
94 {
95  uint32_t err_code = sd_power_gpregret_set(BOOTLOADER_DFU_START);
96  APP_ERROR_CHECK(err_code);
97 
98  err_code = sd_softdevice_disable();
99  APP_ERROR_CHECK(err_code);
100 
101  err_code = sd_softdevice_vector_table_base_set(NRF_UICR->NRFFW[0]);
102  APP_ERROR_CHECK(err_code);
103 
104  NVIC_ClearPendingIRQ(SWI2_IRQn);
106  bootloader_util_app_start(NRF_UICR->NRFFW[0]);
107 }
108 #endif // DFU_SUPPORT
109 
112 //lint -restore
void power_manage(void)
Power manager.
Definition: common.c:65
uint32_t ticks_diff(uint32_t ticks_now, uint32_t ticks_old)
Definition: common.c:78
#define NOF_IRQS
Definition: common.c:25
#define MAX_RTC_COUNTER_VAL
Definition: common.c:28
void dfu_start(void)
Starts DFU mode. The function writes a special value to retention register to tell bootloader to star...
Definition: common.c:93
void system_off(void)
Function to put system in SYSTEMOFF mode.
Definition: common.c:72
static void interrupts_disable(void)
Function for disabling all interrupts before jumping from bootloader to application.
Definition: common.c:39