system_nrf52811.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. Copyright (c) 2009-2020 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_erratas.h"
  21. #include "system_nrf52811.h"
  22. /*lint ++flb "Enter library region" */
  23. #define __SYSTEM_CLOCK_64M (64000000UL)
  24. #if defined ( __CC_ARM )
  25. uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK_64M;
  26. #elif defined ( __ICCARM__ )
  27. __root uint32_t SystemCoreClock = __SYSTEM_CLOCK_64M;
  28. #elif defined ( __GNUC__ )
  29. uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK_64M;
  30. #endif
  31. void SystemCoreClockUpdate(void)
  32. {
  33. SystemCoreClock = __SYSTEM_CLOCK_64M;
  34. }
  35. void SystemInit(void)
  36. {
  37. /* Workaround for Errata 31 "CLOCK: Calibration values are not correctly loaded from FICR at reset" found at the Errata document
  38. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  39. if (nrf52_errata_31()){
  40. *(volatile uint32_t *)0x4000053C = ((*(volatile uint32_t *)0x10000244) & 0x0000E000) >> 13;
  41. }
  42. /* Workaround for Errata 36 "CLOCK: Some registers are not reset when expected" found at the Errata document
  43. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  44. if (nrf52_errata_36()){
  45. NRF_CLOCK->EVENTS_DONE = 0;
  46. NRF_CLOCK->EVENTS_CTTO = 0;
  47. NRF_CLOCK->CTIV = 0;
  48. }
  49. /* Workaround for Errata 66 "TEMP: Linearity specification not met with default settings" found at the Errata document
  50. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  51. if (nrf52_errata_66()){
  52. NRF_TEMP->A0 = NRF_FICR->TEMP.A0;
  53. NRF_TEMP->A1 = NRF_FICR->TEMP.A1;
  54. NRF_TEMP->A2 = NRF_FICR->TEMP.A2;
  55. NRF_TEMP->A3 = NRF_FICR->TEMP.A3;
  56. NRF_TEMP->A4 = NRF_FICR->TEMP.A4;
  57. NRF_TEMP->A5 = NRF_FICR->TEMP.A5;
  58. NRF_TEMP->B0 = NRF_FICR->TEMP.B0;
  59. NRF_TEMP->B1 = NRF_FICR->TEMP.B1;
  60. NRF_TEMP->B2 = NRF_FICR->TEMP.B2;
  61. NRF_TEMP->B3 = NRF_FICR->TEMP.B3;
  62. NRF_TEMP->B4 = NRF_FICR->TEMP.B4;
  63. NRF_TEMP->B5 = NRF_FICR->TEMP.B5;
  64. NRF_TEMP->T0 = NRF_FICR->TEMP.T0;
  65. NRF_TEMP->T1 = NRF_FICR->TEMP.T1;
  66. NRF_TEMP->T2 = NRF_FICR->TEMP.T2;
  67. NRF_TEMP->T3 = NRF_FICR->TEMP.T3;
  68. NRF_TEMP->T4 = NRF_FICR->TEMP.T4;
  69. }
  70. #ifdef DEVELOP_IN_NRF52840
  71. /* Workaround for Errata 103 "CCM: Wrong reset value of CCM MAXPACKETSIZE" found at the Errata document
  72. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  73. if (nrf52_errata_103()){
  74. NRF_CCM->MAXPACKETSIZE = 0xFBul;
  75. }
  76. /* 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
  77. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  78. if (nrf52_errata_115()){
  79. *(volatile uint32_t *)0x40000EE4 = (*(volatile uint32_t *)0x40000EE4 & 0xFFFFFFF0) | (*(uint32_t *)0x10000258 & 0x0000000F);
  80. }
  81. #endif
  82. /* Workaround for Errata 136 "System: Bits in RESETREAS are set when they should not be" found at the Errata document
  83. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  84. if (nrf52_errata_136()){
  85. if (NRF_POWER->RESETREAS & POWER_RESETREAS_RESETPIN_Msk){
  86. NRF_POWER->RESETREAS = ~POWER_RESETREAS_RESETPIN_Msk;
  87. }
  88. }
  89. /* 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
  90. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  91. if (nrf52_errata_217()){
  92. *(volatile uint32_t *)0x40000EE4ul |= 0x0000000Ful;
  93. }
  94. /* Configure GPIO pads as pPin Reset pin if Pin Reset capabilities desired. If CONFIG_GPIO_AS_PINRESET is not
  95. defined, pin reset will not be available. One GPIO (see Product Specification to see which one) will then be
  96. reserved for PinReset and not available as normal GPIO. */
  97. #if defined (CONFIG_GPIO_AS_PINRESET)
  98. #ifdef DEVELOP_IN_NRF52840
  99. #define RESET_PIN 18
  100. #else
  101. #define RESET_PIN 21
  102. #endif
  103. if (((NRF_UICR->PSELRESET[0] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos)) ||
  104. ((NRF_UICR->PSELRESET[1] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos))){
  105. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
  106. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  107. NRF_UICR->PSELRESET[0] = RESET_PIN;
  108. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  109. NRF_UICR->PSELRESET[1] = RESET_PIN;
  110. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  111. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
  112. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  113. NVIC_SystemReset();
  114. }
  115. #endif
  116. /* When developing on an nrf52840, make sure NFC pins are mapped as GPIO. */
  117. #if defined (DEVELOP_IN_NRF52840)
  118. if (((*((uint32_t *)0x10001200) & (1 << 0)) != 0) || ((*((uint32_t *)0x10001204) & (1 << 0)) != 0)){
  119. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
  120. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  121. *((uint32_t *)0x10001200) = 0;
  122. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  123. *((uint32_t *)0x10001204) = 0;
  124. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  125. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
  126. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  127. NVIC_SystemReset();
  128. }
  129. #endif
  130. SystemCoreClockUpdate();
  131. }
  132. /*lint --flb "Leave library region" */