system_nrf52.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. Copyright (c) 2009-2018 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 "system_nrf52.h"
  21. /*lint ++flb "Enter library region" */
  22. #define __SYSTEM_CLOCK_64M (64000000UL)
  23. static bool errata_12(void);
  24. static bool errata_16(void);
  25. static bool errata_31(void);
  26. static bool errata_32(void);
  27. static bool errata_36(void);
  28. static bool errata_37(void);
  29. static bool errata_57(void);
  30. static bool errata_66(void);
  31. static bool errata_108(void);
  32. static bool errata_136(void);
  33. static bool errata_182(void);
  34. #if defined ( __CC_ARM )
  35. uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK_64M;
  36. #elif defined ( __ICCARM__ )
  37. __root uint32_t SystemCoreClock = __SYSTEM_CLOCK_64M;
  38. #elif defined ( __GNUC__ )
  39. uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK_64M;
  40. #endif
  41. void SystemCoreClockUpdate(void)
  42. {
  43. SystemCoreClock = __SYSTEM_CLOCK_64M;
  44. }
  45. void SystemInit(void)
  46. {
  47. /* Enable SWO trace functionality. If ENABLE_SWO is not defined, SWO pin will be used as GPIO (see Product
  48. Specification to see which one). */
  49. #if defined (ENABLE_SWO)
  50. CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  51. NRF_CLOCK->TRACECONFIG |= CLOCK_TRACECONFIG_TRACEMUX_Serial << CLOCK_TRACECONFIG_TRACEMUX_Pos;
  52. 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);
  53. #endif
  54. /* Enable Trace functionality. If ENABLE_TRACE is not defined, TRACE pins will be used as GPIOs (see Product
  55. Specification to see which ones). */
  56. #if defined (ENABLE_TRACE)
  57. CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  58. NRF_CLOCK->TRACECONFIG |= CLOCK_TRACECONFIG_TRACEMUX_Parallel << CLOCK_TRACECONFIG_TRACEMUX_Pos;
  59. 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);
  60. 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);
  61. 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);
  62. 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);
  63. 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);
  64. #endif
  65. /* Workaround for Errata 12 "COMP: Reference ladder not correctly calibrated" found at the Errata document
  66. for your device located at https://infocenter.nordicsemi.com/ */
  67. if (errata_12()){
  68. *(volatile uint32_t *)0x40013540 = (*(uint32_t *)0x10000324 & 0x00001F00) >> 8;
  69. }
  70. /* Workaround for Errata 16 "System: RAM may be corrupt on wakeup from CPU IDLE" found at the Errata document
  71. for your device located at https://infocenter.nordicsemi.com/ */
  72. if (errata_16()){
  73. *(volatile uint32_t *)0x4007C074 = 3131961357ul;
  74. }
  75. /* Workaround for Errata 31 "CLOCK: Calibration values are not correctly loaded from FICR at reset" found at the Errata document
  76. for your device located at https://infocenter.nordicsemi.com/ */
  77. if (errata_31()){
  78. *(volatile uint32_t *)0x4000053C = ((*(volatile uint32_t *)0x10000244) & 0x0000E000) >> 13;
  79. }
  80. /* Workaround for Errata 32 "DIF: Debug session automatically enables TracePort pins" found at the Errata document
  81. for your device located at https://infocenter.nordicsemi.com/ */
  82. if (errata_32()){
  83. CoreDebug->DEMCR &= ~CoreDebug_DEMCR_TRCENA_Msk;
  84. }
  85. /* Workaround for Errata 36 "CLOCK: Some registers are not reset when expected" found at the Errata document
  86. for your device located at https://infocenter.nordicsemi.com/ */
  87. if (errata_36()){
  88. NRF_CLOCK->EVENTS_DONE = 0;
  89. NRF_CLOCK->EVENTS_CTTO = 0;
  90. NRF_CLOCK->CTIV = 0;
  91. }
  92. /* Workaround for Errata 37 "RADIO: Encryption engine is slow by default" found at the Errata document
  93. for your device located at https://infocenter.nordicsemi.com/ */
  94. if (errata_37()){
  95. *(volatile uint32_t *)0x400005A0 = 0x3;
  96. }
  97. /* Workaround for Errata 57 "NFCT: NFC Modulation amplitude" found at the Errata document
  98. for your device located at https://infocenter.nordicsemi.com/ */
  99. if (errata_57()){
  100. *(volatile uint32_t *)0x40005610 = 0x00000005;
  101. *(volatile uint32_t *)0x40005688 = 0x00000001;
  102. *(volatile uint32_t *)0x40005618 = 0x00000000;
  103. *(volatile uint32_t *)0x40005614 = 0x0000003F;
  104. }
  105. /* Workaround for Errata 66 "TEMP: Linearity specification not met with default settings" found at the Errata document
  106. for your device located at https://infocenter.nordicsemi.com/ */
  107. if (errata_66()){
  108. NRF_TEMP->A0 = NRF_FICR->TEMP.A0;
  109. NRF_TEMP->A1 = NRF_FICR->TEMP.A1;
  110. NRF_TEMP->A2 = NRF_FICR->TEMP.A2;
  111. NRF_TEMP->A3 = NRF_FICR->TEMP.A3;
  112. NRF_TEMP->A4 = NRF_FICR->TEMP.A4;
  113. NRF_TEMP->A5 = NRF_FICR->TEMP.A5;
  114. NRF_TEMP->B0 = NRF_FICR->TEMP.B0;
  115. NRF_TEMP->B1 = NRF_FICR->TEMP.B1;
  116. NRF_TEMP->B2 = NRF_FICR->TEMP.B2;
  117. NRF_TEMP->B3 = NRF_FICR->TEMP.B3;
  118. NRF_TEMP->B4 = NRF_FICR->TEMP.B4;
  119. NRF_TEMP->B5 = NRF_FICR->TEMP.B5;
  120. NRF_TEMP->T0 = NRF_FICR->TEMP.T0;
  121. NRF_TEMP->T1 = NRF_FICR->TEMP.T1;
  122. NRF_TEMP->T2 = NRF_FICR->TEMP.T2;
  123. NRF_TEMP->T3 = NRF_FICR->TEMP.T3;
  124. NRF_TEMP->T4 = NRF_FICR->TEMP.T4;
  125. }
  126. /* 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
  127. for your device located at https://infocenter.nordicsemi.com/ */
  128. if (errata_108()){
  129. *(volatile uint32_t *)0x40000EE4 = *(volatile uint32_t *)0x10000258 & 0x0000004F;
  130. }
  131. /* Workaround for Errata 136 "System: Bits in RESETREAS are set when they should not be" found at the Errata document
  132. for your device located at https://infocenter.nordicsemi.com/ */
  133. if (errata_136()){
  134. if (NRF_POWER->RESETREAS & POWER_RESETREAS_RESETPIN_Msk){
  135. NRF_POWER->RESETREAS = ~POWER_RESETREAS_RESETPIN_Msk;
  136. }
  137. }
  138. /* Workaround for Errata 182 "RADIO: Fixes for anomalies #102, #106, and #107 do not take effect" found at the Errata document
  139. for your device located at https://infocenter.nordicsemi.com/ */
  140. if (errata_182()){
  141. *(volatile uint32_t *) 0x4000173C |= (0x1 << 10);
  142. }
  143. /* Enable the FPU if the compiler used floating point unit instructions. __FPU_USED is a MACRO defined by the
  144. * compiler. Since the FPU consumes energy, remember to disable FPU use in the compiler if floating point unit
  145. * operations are not used in your code. */
  146. #if (__FPU_USED == 1)
  147. SCB->CPACR |= (3UL << 20) | (3UL << 22);
  148. __DSB();
  149. __ISB();
  150. #endif
  151. /* Configure NFCT pins as GPIOs if NFCT is not to be used in your code. If CONFIG_NFCT_PINS_AS_GPIOS is not defined,
  152. two GPIOs (see Product Specification to see which ones) will be reserved for NFC and will not be available as
  153. normal GPIOs. */
  154. #if defined (CONFIG_NFCT_PINS_AS_GPIOS)
  155. if ((NRF_UICR->NFCPINS & UICR_NFCPINS_PROTECT_Msk) == (UICR_NFCPINS_PROTECT_NFC << UICR_NFCPINS_PROTECT_Pos)){
  156. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
  157. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  158. NRF_UICR->NFCPINS &= ~UICR_NFCPINS_PROTECT_Msk;
  159. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  160. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
  161. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  162. NVIC_SystemReset();
  163. }
  164. #endif
  165. /* Configure GPIO pads as pPin Reset pin if Pin Reset capabilities desired. If CONFIG_GPIO_AS_PINRESET is not
  166. defined, pin reset will not be available. One GPIO (see Product Specification to see which one) will then be
  167. reserved for PinReset and not available as normal GPIO. */
  168. #if defined (CONFIG_GPIO_AS_PINRESET)
  169. if (((NRF_UICR->PSELRESET[0] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos)) ||
  170. ((NRF_UICR->PSELRESET[1] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos))){
  171. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
  172. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  173. NRF_UICR->PSELRESET[0] = 21;
  174. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  175. NRF_UICR->PSELRESET[1] = 21;
  176. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  177. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
  178. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  179. NVIC_SystemReset();
  180. }
  181. #endif
  182. SystemCoreClockUpdate();
  183. }
  184. static bool errata_12(void)
  185. {
  186. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  187. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  188. return true;
  189. }
  190. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x40){
  191. return true;
  192. }
  193. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x50){
  194. return true;
  195. }
  196. }
  197. return false;
  198. }
  199. static bool errata_16(void)
  200. {
  201. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  202. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  203. return true;
  204. }
  205. }
  206. return false;
  207. }
  208. static bool errata_31(void)
  209. {
  210. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  211. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  212. return true;
  213. }
  214. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x40){
  215. return true;
  216. }
  217. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x50){
  218. return true;
  219. }
  220. }
  221. return false;
  222. }
  223. static bool errata_32(void)
  224. {
  225. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  226. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  227. return true;
  228. }
  229. }
  230. return false;
  231. }
  232. static bool errata_36(void)
  233. {
  234. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  235. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  236. return true;
  237. }
  238. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x40){
  239. return true;
  240. }
  241. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x50){
  242. return true;
  243. }
  244. }
  245. return false;
  246. }
  247. static bool errata_37(void)
  248. {
  249. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  250. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  251. return true;
  252. }
  253. }
  254. return false;
  255. }
  256. static bool errata_57(void)
  257. {
  258. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  259. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  260. return true;
  261. }
  262. }
  263. return false;
  264. }
  265. static bool errata_66(void)
  266. {
  267. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  268. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x50){
  269. return true;
  270. }
  271. }
  272. return false;
  273. }
  274. static bool errata_108(void)
  275. {
  276. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  277. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  278. return true;
  279. }
  280. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x40){
  281. return true;
  282. }
  283. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x50){
  284. return true;
  285. }
  286. }
  287. return false;
  288. }
  289. static bool errata_136(void)
  290. {
  291. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  292. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  293. return true;
  294. }
  295. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x40){
  296. return true;
  297. }
  298. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x50){
  299. return true;
  300. }
  301. }
  302. return false;
  303. }
  304. static bool errata_182(void)
  305. {
  306. if (*(uint32_t *)0x10000130ul == 0x6ul){
  307. if (*(uint32_t *)0x10000134ul == 0x6ul){
  308. return true;
  309. }
  310. }
  311. return false;
  312. }
  313. /*lint --flb "Leave library region" */