system_nrf52840.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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_nrf52840.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. /* Enable SWO trace functionality. If ENABLE_SWO is not defined, SWO pin will be used as GPIO (see Product
  38. Specification to see which one). */
  39. #if defined (ENABLE_SWO)
  40. CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  41. NRF_CLOCK->TRACECONFIG |= CLOCK_TRACECONFIG_TRACEMUX_Serial << CLOCK_TRACECONFIG_TRACEMUX_Pos;
  42. NRF_P1->PIN_CNF[0] = (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);
  43. #endif
  44. /* Enable Trace functionality. If ENABLE_TRACE is not defined, TRACE pins will be used as GPIOs (see Product
  45. Specification to see which ones). */
  46. #if defined (ENABLE_TRACE)
  47. CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  48. NRF_CLOCK->TRACECONFIG |= CLOCK_TRACECONFIG_TRACEMUX_Parallel << CLOCK_TRACECONFIG_TRACEMUX_Pos;
  49. NRF_P0->PIN_CNF[7] = (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);
  50. NRF_P1->PIN_CNF[0] = (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);
  51. NRF_P0->PIN_CNF[12] = (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);
  52. NRF_P0->PIN_CNF[11] = (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. NRF_P1->PIN_CNF[9] = (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);
  54. #endif
  55. /* Workaround for Errata 36 "CLOCK: Some registers are not reset when expected" found at the Errata document
  56. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  57. if (nrf52_errata_36()){
  58. NRF_CLOCK->EVENTS_DONE = 0;
  59. NRF_CLOCK->EVENTS_CTTO = 0;
  60. NRF_CLOCK->CTIV = 0;
  61. }
  62. /* Workaround for Errata 66 "TEMP: Linearity specification not met with default settings" found at the Errata document
  63. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  64. if (nrf52_errata_66()){
  65. NRF_TEMP->A0 = NRF_FICR->TEMP.A0;
  66. NRF_TEMP->A1 = NRF_FICR->TEMP.A1;
  67. NRF_TEMP->A2 = NRF_FICR->TEMP.A2;
  68. NRF_TEMP->A3 = NRF_FICR->TEMP.A3;
  69. NRF_TEMP->A4 = NRF_FICR->TEMP.A4;
  70. NRF_TEMP->A5 = NRF_FICR->TEMP.A5;
  71. NRF_TEMP->B0 = NRF_FICR->TEMP.B0;
  72. NRF_TEMP->B1 = NRF_FICR->TEMP.B1;
  73. NRF_TEMP->B2 = NRF_FICR->TEMP.B2;
  74. NRF_TEMP->B3 = NRF_FICR->TEMP.B3;
  75. NRF_TEMP->B4 = NRF_FICR->TEMP.B4;
  76. NRF_TEMP->B5 = NRF_FICR->TEMP.B5;
  77. NRF_TEMP->T0 = NRF_FICR->TEMP.T0;
  78. NRF_TEMP->T1 = NRF_FICR->TEMP.T1;
  79. NRF_TEMP->T2 = NRF_FICR->TEMP.T2;
  80. NRF_TEMP->T3 = NRF_FICR->TEMP.T3;
  81. NRF_TEMP->T4 = NRF_FICR->TEMP.T4;
  82. }
  83. /* Workaround for Errata 98 "NFCT: Not able to communicate with the peer" found at the Errata document
  84. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  85. if (nrf52_errata_98()){
  86. *(volatile uint32_t *)0x4000568Cul = 0x00038148ul;
  87. }
  88. /* Workaround for Errata 103 "CCM: Wrong reset value of CCM MAXPACKETSIZE" found at the Errata document
  89. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  90. if (nrf52_errata_103()){
  91. NRF_CCM->MAXPACKETSIZE = 0xFBul;
  92. }
  93. /* 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
  94. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  95. if (nrf52_errata_115()){
  96. *(volatile uint32_t *)0x40000EE4ul = (*(volatile uint32_t *)0x40000EE4ul & 0xFFFFFFF0ul) | (*(uint32_t *)0x10000258ul & 0x0000000Ful);
  97. }
  98. /* Workaround for Errata 120 "QSPI: Data read or written is corrupted" found at the Errata document
  99. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  100. if (nrf52_errata_120()){
  101. *(volatile uint32_t *)0x40029640ul = 0x200ul;
  102. }
  103. /* Workaround for Errata 136 "System: Bits in RESETREAS are set when they should not be" found at the Errata document
  104. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  105. if (nrf52_errata_136()){
  106. if (NRF_POWER->RESETREAS & POWER_RESETREAS_RESETPIN_Msk){
  107. NRF_POWER->RESETREAS = ~POWER_RESETREAS_RESETPIN_Msk;
  108. }
  109. }
  110. /* Enable the FPU if the compiler used floating point unit instructions. __FPU_USED is a MACRO defined by the
  111. * compiler. Since the FPU consumes energy, remember to disable FPU use in the compiler if floating point unit
  112. * operations are not used in your code. */
  113. #if (__FPU_USED == 1)
  114. SCB->CPACR |= (3UL << 20) | (3UL << 22);
  115. __DSB();
  116. __ISB();
  117. #endif
  118. /* Configure NFCT pins as GPIOs if NFCT is not to be used in your code. If CONFIG_NFCT_PINS_AS_GPIOS is not defined,
  119. two GPIOs (see Product Specification to see which ones) will be reserved for NFC and will not be available as
  120. normal GPIOs. */
  121. #if defined (CONFIG_NFCT_PINS_AS_GPIOS)
  122. if ((NRF_UICR->NFCPINS & UICR_NFCPINS_PROTECT_Msk) == (UICR_NFCPINS_PROTECT_NFC << UICR_NFCPINS_PROTECT_Pos)){
  123. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
  124. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  125. NRF_UICR->NFCPINS &= ~UICR_NFCPINS_PROTECT_Msk;
  126. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  127. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
  128. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  129. NVIC_SystemReset();
  130. }
  131. #endif
  132. /* Configure GPIO pads as pPin Reset pin if Pin Reset capabilities desired. If CONFIG_GPIO_AS_PINRESET is not
  133. defined, pin reset will not be available. One GPIO (see Product Specification to see which one) will then be
  134. reserved for PinReset and not available as normal GPIO. */
  135. #if defined (CONFIG_GPIO_AS_PINRESET)
  136. if (((NRF_UICR->PSELRESET[0] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos)) ||
  137. ((NRF_UICR->PSELRESET[1] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos))){
  138. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
  139. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  140. NRF_UICR->PSELRESET[0] = 18;
  141. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  142. NRF_UICR->PSELRESET[1] = 18;
  143. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  144. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
  145. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  146. NVIC_SystemReset();
  147. }
  148. #endif
  149. SystemCoreClockUpdate();
  150. }
  151. /*lint --flb "Leave library region" */