You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
14 KiB

2 years ago
  1. /*
  2. Copyright (c) 2009-2021 ARM Limited. All rights reserved.
  3. SPDX-License-Identifier: Apache-2.0
  4. Licensed under the Apache License, Version 2.0 (the License); you may
  5. not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an AS IS BASIS, WITHOUT
  10. WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. NOTICE: This file has been modified by Nordic Semiconductor ASA.
  14. */
  15. /* NOTE: Template files (including this one) are application specific and therefore expected to
  16. be copied into the application project folder prior to its use! */
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include "nrf.h"
  20. #include "nrf_peripherals.h"
  21. #include "nrf52_erratas.h"
  22. #include "system_nrf52.h"
  23. #include "system_nrf52_approtect.h"
  24. #define __SYSTEM_CLOCK_64M (64000000UL)
  25. #if defined ( __CC_ARM )
  26. uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK_64M;
  27. #elif defined ( __ICCARM__ )
  28. __root uint32_t SystemCoreClock = __SYSTEM_CLOCK_64M;
  29. #elif defined ( __GNUC__ )
  30. uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK_64M;
  31. #endif
  32. /* Select correct reset pin */
  33. /* Handle DEVELOP_IN-targets first as they take precedence over the later macros */
  34. #if defined (DEVELOP_IN_NRF52805) \
  35. || defined (DEVELOP_IN_NRF52810) \
  36. || defined (DEVELOP_IN_NRF52811) \
  37. || defined (DEVELOP_IN_NRF52832)
  38. #define RESET_PIN 21
  39. #elif defined (DEVELOP_IN_NRF52820) \
  40. || defined (DEVELOP_IN_NRF52833) \
  41. || defined (DEVELOP_IN_NRF52840)
  42. #define RESET_PIN 18
  43. #elif defined (NRF52805_XXAA) \
  44. || defined (NRF52810_XXAA) \
  45. || defined (NRF52811_XXAA) \
  46. || defined (NRF52832_XXAA) \
  47. || defined (NRF52832_XXAB)
  48. #define RESET_PIN 21
  49. #elif defined (NRF52820_XXAA) \
  50. || defined (NRF52833_XXAA) \
  51. || defined (NRF52840_XXAA)
  52. #define RESET_PIN 18
  53. #else
  54. #error "A supported device macro must be defined."
  55. #endif
  56. /* -- NVMC utility functions -- */
  57. /* Waits until NVMC is done with the current pending action */
  58. void nvmc_wait(void)
  59. {
  60. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  61. }
  62. /* Configure the NVMC to "mode".
  63. Mode must be an enumerator of field NVMC_CONFIG_WEN */
  64. void nvmc_config(uint32_t mode)
  65. {
  66. NRF_NVMC->CONFIG = mode << NVMC_CONFIG_WEN_Pos;
  67. nvmc_wait();
  68. }
  69. void SystemCoreClockUpdate(void)
  70. {
  71. SystemCoreClock = __SYSTEM_CLOCK_64M;
  72. }
  73. void SystemInit(void)
  74. {
  75. /* Enable SWO trace functionality. If ENABLE_SWO is not defined, SWO pin will be used as GPIO (see Product
  76. Specification to see which one). */
  77. #if defined (ENABLE_SWO) && defined(CLOCK_TRACECONFIG_TRACEMUX_Pos)
  78. CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  79. NRF_CLOCK->TRACECONFIG |= CLOCK_TRACECONFIG_TRACEMUX_Serial << CLOCK_TRACECONFIG_TRACEMUX_Pos;
  80. NRF_P0->PIN_CNF[18] = (GPIO_PIN_CNF_DRIVE_H0H1 << GPIO_PIN_CNF_DRIVE_Pos) | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
  81. #endif
  82. /* Enable Trace functionality. If ENABLE_TRACE is not defined, TRACE pins will be used as GPIOs (see Product
  83. Specification to see which ones). */
  84. #if defined (ENABLE_TRACE) && defined(CLOCK_TRACECONFIG_TRACEMUX_Pos)
  85. CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  86. NRF_CLOCK->TRACECONFIG |= CLOCK_TRACECONFIG_TRACEMUX_Parallel << CLOCK_TRACECONFIG_TRACEMUX_Pos;
  87. NRF_P0->PIN_CNF[14] = (GPIO_PIN_CNF_DRIVE_H0H1 << GPIO_PIN_CNF_DRIVE_Pos) | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
  88. NRF_P0->PIN_CNF[15] = (GPIO_PIN_CNF_DRIVE_H0H1 << GPIO_PIN_CNF_DRIVE_Pos) | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
  89. NRF_P0->PIN_CNF[16] = (GPIO_PIN_CNF_DRIVE_H0H1 << GPIO_PIN_CNF_DRIVE_Pos) | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
  90. NRF_P0->PIN_CNF[18] = (GPIO_PIN_CNF_DRIVE_H0H1 << GPIO_PIN_CNF_DRIVE_Pos) | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
  91. NRF_P0->PIN_CNF[20] = (GPIO_PIN_CNF_DRIVE_H0H1 << GPIO_PIN_CNF_DRIVE_Pos) | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
  92. #endif
  93. #if NRF52_ERRATA_12_ENABLE_WORKAROUND
  94. /* Workaround for Errata 12 "COMP: Reference ladder not correctly calibrated" found at the Errata document
  95. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  96. if (nrf52_errata_12()){
  97. *(volatile uint32_t *)0x40013540 = (*(uint32_t *)0x10000324 & 0x00001F00) >> 8;
  98. }
  99. #endif
  100. #if NRF52_ERRATA_16_ENABLE_WORKAROUND
  101. /* Workaround for Errata 16 "System: RAM may be corrupt on wakeup from CPU IDLE" found at the Errata document
  102. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  103. if (nrf52_errata_16()){
  104. *(volatile uint32_t *)0x4007C074 = 3131961357ul;
  105. }
  106. #endif
  107. #if NRF52_ERRATA_31_ENABLE_WORKAROUND
  108. /* Workaround for Errata 31 "CLOCK: Calibration values are not correctly loaded from FICR at reset" found at the Errata document
  109. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  110. if (nrf52_errata_31()){
  111. *(volatile uint32_t *)0x4000053C = ((*(volatile uint32_t *)0x10000244) & 0x0000E000) >> 13;
  112. }
  113. #endif
  114. #if NRF52_ERRATA_32_ENABLE_WORKAROUND
  115. /* Workaround for Errata 32 "DIF: Debug session automatically enables TracePort pins" found at the Errata document
  116. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  117. if (nrf52_errata_32()){
  118. CoreDebug->DEMCR &= ~CoreDebug_DEMCR_TRCENA_Msk;
  119. }
  120. #endif
  121. #if NRF52_ERRATA_36_ENABLE_WORKAROUND
  122. /* Workaround for Errata 36 "CLOCK: Some registers are not reset when expected" found at the Errata document
  123. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  124. if (nrf52_errata_36()){
  125. NRF_CLOCK->EVENTS_DONE = 0;
  126. NRF_CLOCK->EVENTS_CTTO = 0;
  127. NRF_CLOCK->CTIV = 0;
  128. }
  129. #endif
  130. #if NRF52_ERRATA_37_ENABLE_WORKAROUND
  131. /* Workaround for Errata 37 "RADIO: Encryption engine is slow by default" found at the Errata document
  132. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  133. if (nrf52_errata_37()){
  134. *(volatile uint32_t *)0x400005A0 = 0x3;
  135. }
  136. #endif
  137. #if NRF52_ERRATA_57_ENABLE_WORKAROUND
  138. /* Workaround for Errata 57 "NFCT: NFC Modulation amplitude" found at the Errata document
  139. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  140. if (nrf52_errata_57()){
  141. *(volatile uint32_t *)0x40005610 = 0x00000005;
  142. *(volatile uint32_t *)0x40005688 = 0x00000001;
  143. *(volatile uint32_t *)0x40005618 = 0x00000000;
  144. *(volatile uint32_t *)0x40005614 = 0x0000003F;
  145. }
  146. #endif
  147. #if NRF52_ERRATA_66_ENABLE_WORKAROUND
  148. /* Workaround for Errata 66 "TEMP: Linearity specification not met with default settings" found at the Errata document
  149. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  150. if (nrf52_errata_66()){
  151. NRF_TEMP->A0 = NRF_FICR->TEMP.A0;
  152. NRF_TEMP->A1 = NRF_FICR->TEMP.A1;
  153. NRF_TEMP->A2 = NRF_FICR->TEMP.A2;
  154. NRF_TEMP->A3 = NRF_FICR->TEMP.A3;
  155. NRF_TEMP->A4 = NRF_FICR->TEMP.A4;
  156. NRF_TEMP->A5 = NRF_FICR->TEMP.A5;
  157. NRF_TEMP->B0 = NRF_FICR->TEMP.B0;
  158. NRF_TEMP->B1 = NRF_FICR->TEMP.B1;
  159. NRF_TEMP->B2 = NRF_FICR->TEMP.B2;
  160. NRF_TEMP->B3 = NRF_FICR->TEMP.B3;
  161. NRF_TEMP->B4 = NRF_FICR->TEMP.B4;
  162. NRF_TEMP->B5 = NRF_FICR->TEMP.B5;
  163. NRF_TEMP->T0 = NRF_FICR->TEMP.T0;
  164. NRF_TEMP->T1 = NRF_FICR->TEMP.T1;
  165. NRF_TEMP->T2 = NRF_FICR->TEMP.T2;
  166. NRF_TEMP->T3 = NRF_FICR->TEMP.T3;
  167. NRF_TEMP->T4 = NRF_FICR->TEMP.T4;
  168. }
  169. #endif
  170. #if NRF52_ERRATA_98_ENABLE_WORKAROUND
  171. /* Workaround for Errata 98 "NFCT: Not able to communicate with the peer" found at the Errata document
  172. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  173. if (nrf52_errata_98()){
  174. *(volatile uint32_t *)0x4000568Cul = 0x00038148ul;
  175. }
  176. #endif
  177. #if NRF52_ERRATA_103_ENABLE_WORKAROUND && defined(CCM_MAXPACKETSIZE_MAXPACKETSIZE_Pos)
  178. /* Workaround for Errata 103 "CCM: Wrong reset value of CCM MAXPACKETSIZE" found at the Errata document
  179. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  180. if (nrf52_errata_103()){
  181. NRF_CCM->MAXPACKETSIZE = 0xFBul;
  182. }
  183. #endif
  184. #if NRF52_ERRATA_108_ENABLE_WORKAROUND
  185. /* 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
  186. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  187. if (nrf52_errata_108()){
  188. *(volatile uint32_t *)0x40000EE4ul = *(volatile uint32_t *)0x10000258ul & 0x0000004Ful;
  189. }
  190. #endif
  191. #if NRF52_ERRATA_115_ENABLE_WORKAROUND
  192. /* Workaround for Errata 115 "RAM: RAM content cannot be trusted upon waking up from System ON Idle or System OFF mode" found at the Errata document
  193. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  194. if (nrf52_errata_115()){
  195. *(volatile uint32_t *)0x40000EE4 = (*(volatile uint32_t *)0x40000EE4 & 0xFFFFFFF0) | (*(uint32_t *)0x10000258 & 0x0000000F);
  196. }
  197. #endif
  198. #if NRF52_ERRATA_120_ENABLE_WORKAROUND
  199. /* Workaround for Errata 120 "QSPI: Data read or written is corrupted" found at the Errata document
  200. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  201. if (nrf52_errata_120()){
  202. *(volatile uint32_t *)0x40029640ul = 0x200ul;
  203. }
  204. #endif
  205. #if NRF52_ERRATA_136_ENABLE_WORKAROUND
  206. /* Workaround for Errata 136 "System: Bits in RESETREAS are set when they should not be" found at the Errata document
  207. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  208. if (nrf52_errata_136()){
  209. if (NRF_POWER->RESETREAS & POWER_RESETREAS_RESETPIN_Msk){
  210. NRF_POWER->RESETREAS = ~POWER_RESETREAS_RESETPIN_Msk;
  211. }
  212. }
  213. #endif
  214. #if NRF52_ERRATA_182_ENABLE_WORKAROUND
  215. /* Workaround for Errata 182 "RADIO: Fixes for anomalies #102, #106, and #107 do not take effect" found at the Errata document
  216. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  217. if (nrf52_errata_182()){
  218. *(volatile uint32_t *) 0x4000173C |= (0x1 << 10);
  219. }
  220. #endif
  221. #if NRF52_ERRATA_217_ENABLE_WORKAROUND
  222. /* 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
  223. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  224. if (nrf52_errata_217()){
  225. *(volatile uint32_t *)0x40000EE4ul |= 0x0000000Ful;
  226. }
  227. #endif
  228. /* Enable the FPU if the compiler used floating point unit instructions. __FPU_USED is a MACRO defined by the
  229. * compiler. Since the FPU consumes energy, remember to disable FPU use in the compiler if floating point unit
  230. * operations are not used in your code. */
  231. #if (__FPU_USED == 1)
  232. SCB->CPACR |= (3UL << 20) | (3UL << 22);
  233. __DSB();
  234. __ISB();
  235. #endif
  236. nrf52_handle_approtect();
  237. #if NRF52_CONFIGURATION_249_ENABLE && (defined(NRF52805_XXAA) || defined(NRF52810_XXAA) || defined(NRF52811_XXAA))
  238. if (nrf52_configuration_249() && (NRF_UICR->NRFMDK[0] == 0xFFFFFFFF || NRF_UICR->NRFMDK[1] == 0xFFFFFFFF))
  239. {
  240. nvmc_config(NVMC_CONFIG_WEN_Wen);
  241. NRF_UICR->NRFMDK[0] = 0;
  242. nvmc_wait();
  243. NRF_UICR->NRFMDK[1] = 0;
  244. nvmc_wait();
  245. nvmc_config(NVMC_CONFIG_WEN_Ren);
  246. }
  247. #endif
  248. /* Configure NFCT pins as GPIOs if NFCT is not to be used in your code. If CONFIG_NFCT_PINS_AS_GPIOS is not defined,
  249. two GPIOs (see Product Specification to see which ones) will be reserved for NFC and will not be available as
  250. normal GPIOs. */
  251. #if defined (CONFIG_NFCT_PINS_AS_GPIOS) && defined(NFCT_PRESENT)
  252. if ((NRF_UICR->NFCPINS & UICR_NFCPINS_PROTECT_Msk) == (UICR_NFCPINS_PROTECT_NFC << UICR_NFCPINS_PROTECT_Pos)){
  253. nvmc_config(NVMC_CONFIG_WEN_Wen);
  254. NRF_UICR->NFCPINS &= ~UICR_NFCPINS_PROTECT_Msk;
  255. nvmc_wait();
  256. nvmc_config(NVMC_CONFIG_WEN_Ren);
  257. NVIC_SystemReset();
  258. }
  259. #endif
  260. /* Configure GPIO pads as pPin Reset pin if Pin Reset capabilities desired. If CONFIG_GPIO_AS_PINRESET is not
  261. defined, pin reset will not be available. One GPIO (see Product Specification to see which one) will then be
  262. reserved for PinReset and not available as normal GPIO. */
  263. #if defined (CONFIG_GPIO_AS_PINRESET)
  264. if (((NRF_UICR->PSELRESET[0] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos)) ||
  265. ((NRF_UICR->PSELRESET[1] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos))){
  266. nvmc_config(NVMC_CONFIG_WEN_Wen);
  267. NRF_UICR->PSELRESET[0] = RESET_PIN;
  268. nvmc_wait();
  269. NRF_UICR->PSELRESET[1] = RESET_PIN;
  270. nvmc_wait();
  271. nvmc_config(NVMC_CONFIG_WEN_Ren);
  272. NVIC_SystemReset();
  273. }
  274. #endif
  275. /* When developing for nRF52810 on an nRF52832, or nRF52811 on an nRF52840,
  276. make sure NFC pins are mapped as GPIO. */
  277. #if defined (DEVELOP_IN_NRF52832) && defined(NRF52810_XXAA) \
  278. || defined (DEVELOP_IN_NRF52840) && defined(NRF52811_XXAA)
  279. if ((*((uint32_t *)0x1000120C) & (1 << 0)) != 0){
  280. nvmc_config(NVMC_CONFIG_WEN_Wen);
  281. *((uint32_t *)0x1000120C) = 0;
  282. nvmc_wait();
  283. nvmc_config(NVMC_CONFIG_WEN_Ren);
  284. NVIC_SystemReset();
  285. }
  286. #endif
  287. SystemCoreClockUpdate();
  288. }