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_nrf52810.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     #if defined (DEVELOP_IN_NRF52832)
52     /* Workaround for Errata 12 "COMP: Reference ladder not correctly calibrated" found at the Errata document
53        for nRF52832 device located at https://infocenter.nordicsemi.com/index.jsp */
54     if (nrf52_errata_12()){
55         *(volatile uint32_t *)0x40013540 = (*(uint32_t *)0x10000324 & 0x00001F00) >> 8;
56     }
57     #endif
58 
59     #if defined (DEVELOP_IN_NRF52832)
60     /* Workaround for Errata 16 "System: RAM may be corrupt on wakeup from CPU IDLE" found at the Errata document
61        for nRF52832 device located at https://infocenter.nordicsemi.com/index.jsp */
62     if (nrf52_errata_16()){
63         *(volatile uint32_t *)0x4007C074 = 3131961357ul;
64     }
65     #endif
66 
67     /* Workaround for Errata 31 "CLOCK: Calibration values are not correctly loaded from FICR at reset" found at the Errata document
68        for your device located at https://infocenter.nordicsemi.com/index.jsp */
69     if (nrf52_errata_31()){
70         *(volatile uint32_t *)0x4000053C = ((*(volatile uint32_t *)0x10000244) & 0x0000E000) >> 13;
71     }
72 
73     #if defined (DEVELOP_IN_NRF52832)
74     /* Workaround for Errata 32 "DIF: Debug session automatically enables TracePort pins" found at the Errata document
75        for nRF52832 device located at https://infocenter.nordicsemi.com/index.jsp */
76     if (nrf52_errata_32()){
77         CoreDebug->DEMCR &= ~CoreDebug_DEMCR_TRCENA_Msk;
78     }
79     #endif
80 
81     /* Workaround for Errata 36 "CLOCK: Some registers are not reset when expected" found at the Errata document
82        for your device located at https://infocenter.nordicsemi.com/index.jsp  */
83     if (nrf52_errata_36()){
84         NRF_CLOCK->EVENTS_DONE = 0;
85         NRF_CLOCK->EVENTS_CTTO = 0;
86         NRF_CLOCK->CTIV = 0;
87     }
88 
89     #if defined (DEVELOP_IN_NRF52832)
90     /* Workaround for Errata 37 "RADIO: Encryption engine is slow by default" found at the Errata document
91        for your device located at https://infocenter.nordicsemi.com/index.jsp  */
92     if (nrf52_errata_37()){
93         *(volatile uint32_t *)0x400005A0 = 0x3;
94     }
95     #endif
96 
97     #if defined (DEVELOP_IN_NRF52832)
98     /* Workaround for Errata 57 "NFCT: NFC Modulation amplitude" found at the Errata document
99        for your device located at https://infocenter.nordicsemi.com/index.jsp  */
100     if (nrf52_errata_57()){
101         *(volatile uint32_t *)0x40005610 = 0x00000005;
102         *(volatile uint32_t *)0x40005688 = 0x00000001;
103         *(volatile uint32_t *)0x40005618 = 0x00000000;
104         *(volatile uint32_t *)0x40005614 = 0x0000003F;
105     }
106     #endif
107 
108     /* Workaround for Errata 66 "TEMP: Linearity specification not met with default settings" found at the Errata document
109        for your device located at https://infocenter.nordicsemi.com/index.jsp  */
110     if (nrf52_errata_66()){
111         NRF_TEMP->A0 = NRF_FICR->TEMP.A0;
112         NRF_TEMP->A1 = NRF_FICR->TEMP.A1;
113         NRF_TEMP->A2 = NRF_FICR->TEMP.A2;
114         NRF_TEMP->A3 = NRF_FICR->TEMP.A3;
115         NRF_TEMP->A4 = NRF_FICR->TEMP.A4;
116         NRF_TEMP->A5 = NRF_FICR->TEMP.A5;
117         NRF_TEMP->B0 = NRF_FICR->TEMP.B0;
118         NRF_TEMP->B1 = NRF_FICR->TEMP.B1;
119         NRF_TEMP->B2 = NRF_FICR->TEMP.B2;
120         NRF_TEMP->B3 = NRF_FICR->TEMP.B3;
121         NRF_TEMP->B4 = NRF_FICR->TEMP.B4;
122         NRF_TEMP->B5 = NRF_FICR->TEMP.B5;
123         NRF_TEMP->T0 = NRF_FICR->TEMP.T0;
124         NRF_TEMP->T1 = NRF_FICR->TEMP.T1;
125         NRF_TEMP->T2 = NRF_FICR->TEMP.T2;
126         NRF_TEMP->T3 = NRF_FICR->TEMP.T3;
127         NRF_TEMP->T4 = NRF_FICR->TEMP.T4;
128     }
129 
130     /* Workaround for Errata 103 "CCM: Wrong reset value of CCM MAXPACKETSIZE" found at the Errata document
131        for your device located at https://infocenter.nordicsemi.com/index.jsp  */
132     if (nrf52_errata_103()){
133         NRF_CCM->MAXPACKETSIZE = 0xFBul;
134     }
135 
136     #if defined (DEVELOP_IN_NRF52832)
137     /* Workaround for Errata 108 "RAM: RAM content cannot be trusted upon waking up from System ON Idle or System OFF mode" found at the Errata document
138        for your device located at https://infocenter.nordicsemi.com/index.jsp  */
139     if (nrf52_errata_108()){
140         *(volatile uint32_t *)0x40000EE4ul = *(volatile uint32_t *)0x10000258ul & 0x0000004Ful;
141     }
142     #endif
143 
144     /* Workaround for Errata 136 "System: Bits in RESETREAS are set when they should not be" found at the Errata document
145        for your device located at https://infocenter.nordicsemi.com/index.jsp  */
146     if (nrf52_errata_136()){
147         if (NRF_POWER->RESETREAS & POWER_RESETREAS_RESETPIN_Msk){
148             NRF_POWER->RESETREAS =  ~POWER_RESETREAS_RESETPIN_Msk;
149         }
150     }
151 
152     #if defined (DEVELOP_IN_NRF52832)
153     /* Workaround for Errata 182 "RADIO: Fixes for anomalies #102, #106, and #107 do not take effect" found at the Errata document
154        for your device located at https://infocenter.nordicsemi.com/index.jsp  */
155     if (nrf52_errata_182()){
156         *(volatile uint32_t *) 0x4000173C |= (0x1 << 10);
157     }
158     #endif
159 
160     /* Workaround for Errata 217 "RAM: RAM content cannot be trusted upon waking up from System ON Idle or System OFF mode" found at the Errata document
161        for your device located at https://infocenter.nordicsemi.com/index.jsp  */
162     if (nrf52_errata_217()){
163         *(volatile uint32_t *)0x40000EE4ul |= 0x0000000Ful;
164     }
165 
166     /* Configure GPIO pads as pPin Reset pin if Pin Reset capabilities desired. If CONFIG_GPIO_AS_PINRESET is not
167       defined, pin reset will not be available. One GPIO (see Product Specification to see which one) will then be
168       reserved for PinReset and not available as normal GPIO. */
169     #if defined (CONFIG_GPIO_AS_PINRESET)
170         if (((NRF_UICR->PSELRESET[0] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos)) ||
171             ((NRF_UICR->PSELRESET[1] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos))){
172             NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
173             while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
174             NRF_UICR->PSELRESET[0] = 21;
175             while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
176             NRF_UICR->PSELRESET[1] = 21;
177             while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
178             NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
179             while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
180             NVIC_SystemReset();
181         }
182     #endif
183 
184     /* When developing on an nrf52832, make sure NFC pins are mapped as GPIO. */
185     #if defined (DEVELOP_IN_NRF52832)
186         if (((*((uint32_t *)0x10001200) & (1 << 0)) != 0) || ((*((uint32_t *)0x10001204) & (1 << 0)) != 0)){
187             NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
188             while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
189             *((uint32_t *)0x10001200) = 0;
190             while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
191             *((uint32_t *)0x10001204) = 0;
192             while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
193             NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
194             while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
195             NVIC_SystemReset();
196         }
197     #endif
198 
199     SystemCoreClockUpdate();
200 }
201 
202 
203 /*lint --flb "Leave library region" */
204