1 /*
2 
3 Copyright (c) 2009-2020 ARM Limited. All rights reserved.
4 
5     SPDX-License-Identifier: Apache-2.0
6 
7 Licensed under the Apache License, Version 2.0 (the License); you may
8 not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10 
11     www.apache.org/licenses/LICENSE-2.0
12 
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an AS IS BASIS, WITHOUT
15 WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18 
19 NOTICE: This file has been modified by Nordic Semiconductor ASA.
20 
21 */
22 
23 /* NOTE: Template files (including this one) are application specific and therefore expected to
24    be copied into the application project folder prior to its use! */
25 
26 #include <stdint.h>
27 #include <stdbool.h>
28 #include "nrf.h"
29 #include "nrf_erratas.h"
30 #include "system_nrf52820.h"
31 
32 /*lint ++flb "Enter library region" */
33 
34 #define __SYSTEM_CLOCK_64M      (64000000UL)
35 
36 #if defined ( __CC_ARM )
37     uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK_64M;
38 #elif defined ( __ICCARM__ )
39     __root uint32_t SystemCoreClock = __SYSTEM_CLOCK_64M;
40 #elif defined ( __GNUC__ )
41     uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK_64M;
42 #endif
43 
SystemCoreClockUpdate(void)44 void SystemCoreClockUpdate(void)
45 {
46     SystemCoreClock = __SYSTEM_CLOCK_64M;
47 }
48 
SystemInit(void)49 void SystemInit(void)
50 {
51     /* Workaround for Errata 36 "CLOCK: Some registers are not reset when expected" found at the Errata document
52        for your device located at https://infocenter.nordicsemi.com/index.jsp  */
53     if (nrf52_errata_36()){
54         NRF_CLOCK->EVENTS_DONE = 0;
55         NRF_CLOCK->EVENTS_CTTO = 0;
56         NRF_CLOCK->CTIV = 0;
57     }
58 
59     /* Workaround for Errata 66 "TEMP: Linearity specification not met with default settings" found at the Errata document
60        for your device located at https://infocenter.nordicsemi.com/index.jsp  */
61     if (nrf52_errata_66()){
62         NRF_TEMP->A0 = NRF_FICR->TEMP.A0;
63         NRF_TEMP->A1 = NRF_FICR->TEMP.A1;
64         NRF_TEMP->A2 = NRF_FICR->TEMP.A2;
65         NRF_TEMP->A3 = NRF_FICR->TEMP.A3;
66         NRF_TEMP->A4 = NRF_FICR->TEMP.A4;
67         NRF_TEMP->A5 = NRF_FICR->TEMP.A5;
68         NRF_TEMP->B0 = NRF_FICR->TEMP.B0;
69         NRF_TEMP->B1 = NRF_FICR->TEMP.B1;
70         NRF_TEMP->B2 = NRF_FICR->TEMP.B2;
71         NRF_TEMP->B3 = NRF_FICR->TEMP.B3;
72         NRF_TEMP->B4 = NRF_FICR->TEMP.B4;
73         NRF_TEMP->B5 = NRF_FICR->TEMP.B5;
74         NRF_TEMP->T0 = NRF_FICR->TEMP.T0;
75         NRF_TEMP->T1 = NRF_FICR->TEMP.T1;
76         NRF_TEMP->T2 = NRF_FICR->TEMP.T2;
77         NRF_TEMP->T3 = NRF_FICR->TEMP.T3;
78         NRF_TEMP->T4 = NRF_FICR->TEMP.T4;
79     }
80 
81     /* Workaround for Errata 136 "System: Bits in RESETREAS are set when they should not be" found at the Errata document
82        for your device located at https://infocenter.nordicsemi.com/index.jsp  */
83     if (nrf52_errata_136()){
84         if (NRF_POWER->RESETREAS & POWER_RESETREAS_RESETPIN_Msk){
85             NRF_POWER->RESETREAS =  ~POWER_RESETREAS_RESETPIN_Msk;
86         }
87     }
88 
89     /* Enable the FPU if the compiler used floating point unit instructions. __FPU_USED is a MACRO defined by the
90      * compiler. Since the FPU consumes energy, remember to disable FPU use in the compiler if floating point unit
91      * operations are not used in your code. */
92     #if (__FPU_USED == 1)
93         SCB->CPACR |= (3UL << 20) | (3UL << 22);
94         __DSB();
95         __ISB();
96     #endif
97 
98     /* Configure GPIO pads as pPin Reset pin if Pin Reset capabilities desired. If CONFIG_GPIO_AS_PINRESET is not
99       defined, pin reset will not be available. One GPIO (see Product Specification to see which one) will then be
100       reserved for PinReset and not available as normal GPIO. */
101     #if defined (CONFIG_GPIO_AS_PINRESET)
102         if (((NRF_UICR->PSELRESET[0] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos)) ||
103             ((NRF_UICR->PSELRESET[1] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos))){
104             NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
105             while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
106             NRF_UICR->PSELRESET[0] = 18;
107             while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
108             NRF_UICR->PSELRESET[1] = 18;
109             while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
110             NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
111             while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
112             NVIC_SystemReset();
113         }
114     #endif
115 
116     SystemCoreClockUpdate();
117 }
118 
119 /*lint --flb "Leave library region" */
120