system_nrf5340_application.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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_erratas.h"
  21. #include "system_nrf5340_application.h"
  22. #include "system_nrf53_approtect.h"
  23. /*lint ++flb "Enter library region" */
  24. /* NRF5340 application core uses a variable System Clock Frequency that starts at 64MHz */
  25. #define __SYSTEM_CLOCK_MAX (128000000UL)
  26. #define __SYSTEM_CLOCK_INITIAL ( 64000000UL)
  27. #define TRACE_PIN_CNF_VALUE ( (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos) | \
  28. (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | \
  29. (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) | \
  30. (GPIO_PIN_CNF_DRIVE_H0H1 << GPIO_PIN_CNF_DRIVE_Pos) | \
  31. (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | \
  32. (GPIO_PIN_CNF_MCUSEL_TND << GPIO_PIN_CNF_MCUSEL_Pos))
  33. #define TRACE_TRACECLK_PIN TAD_PSEL_TRACECLK_PIN_Traceclk
  34. #define TRACE_TRACEDATA0_PIN TAD_PSEL_TRACEDATA0_PIN_Tracedata0
  35. #define TRACE_TRACEDATA1_PIN TAD_PSEL_TRACEDATA1_PIN_Tracedata1
  36. #define TRACE_TRACEDATA2_PIN TAD_PSEL_TRACEDATA2_PIN_Tracedata2
  37. #define TRACE_TRACEDATA3_PIN TAD_PSEL_TRACEDATA3_PIN_Tracedata3
  38. #if defined ( __CC_ARM )
  39. uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK_INITIAL;
  40. #elif defined ( __ICCARM__ )
  41. __root uint32_t SystemCoreClock = __SYSTEM_CLOCK_INITIAL;
  42. #elif defined ( __GNUC__ )
  43. uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK_INITIAL;
  44. #endif
  45. void SystemCoreClockUpdate(void)
  46. {
  47. #if defined(NRF_TRUSTZONE_NONSECURE)
  48. SystemCoreClock = __SYSTEM_CLOCK_MAX >> (NRF_CLOCK_NS->HFCLKCTRL & (CLOCK_HFCLKCTRL_HCLK_Msk));
  49. #else
  50. SystemCoreClock = __SYSTEM_CLOCK_MAX >> (NRF_CLOCK_S->HFCLKCTRL & (CLOCK_HFCLKCTRL_HCLK_Msk));
  51. #endif
  52. }
  53. void SystemInit(void)
  54. {
  55. #if !defined(NRF_TRUSTZONE_NONSECURE)
  56. /* Perform Secure-mode initialization routines. */
  57. /* Set all ARM SAU regions to NonSecure if TrustZone extensions are enabled.
  58. * Nordic SPU should handle Secure Attribution tasks */
  59. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  60. SAU->CTRL |= (1 << SAU_CTRL_ALLNS_Pos);
  61. #endif
  62. /* Workaround for Errata 97 "ERASEPROTECT, APPROTECT, or startup problems" found at the Errata document
  63. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  64. if (nrf53_errata_97())
  65. {
  66. if (*((volatile uint32_t *)0x50004A20ul) == 0)
  67. {
  68. *((volatile uint32_t *)0x50004A20ul) = 0xDul;
  69. *((volatile uint32_t *)0x5000491Cul) = 0x1ul;
  70. *((volatile uint32_t *)0x5000491Cul) = 0x0ul;
  71. }
  72. }
  73. /* Trimming of the device. Copy all the trimming values from FICR into the target addresses. Trim
  74. until one ADDR is not initialized. */
  75. uint32_t index = 0;
  76. for (index = 0; index < 32ul && NRF_FICR_S->TRIMCNF[index].ADDR != (uint32_t *)0xFFFFFFFFul; index++){
  77. #if defined ( __ICCARM__ )
  78. /* IAR will complain about the order of volatile pointer accesses. */
  79. #pragma diag_suppress=Pa082
  80. #endif
  81. *((volatile uint32_t *)NRF_FICR_S->TRIMCNF[index].ADDR) = NRF_FICR_S->TRIMCNF[index].DATA;
  82. #if defined ( __ICCARM__ )
  83. #pragma diag_default=Pa082
  84. #endif
  85. }
  86. /* errata 64 must be before errata 42, as errata 42 is dependant on the changes in errata 64*/
  87. /* Workaround for Errata 64 "VREGMAIN has invalid configuration when CPU is running at 128 MHz" found at the Errata document
  88. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  89. if (nrf53_errata_64())
  90. {
  91. *((volatile uint32_t *)0x5000470Cul) = 0x29ul;
  92. *((volatile uint32_t *)0x5000473Cul) = 0x3ul;
  93. }
  94. /* Workaround for Errata 42 "Reset value of HFCLKCTRL is invalid" found at the Errata document
  95. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  96. if (nrf53_errata_42())
  97. {
  98. *((volatile uint32_t *)0x50039530ul) = 0xBEEF0044ul;
  99. NRF_CLOCK_S->HFCLKCTRL = CLOCK_HFCLKCTRL_HCLK_Div2 << CLOCK_HFCLKCTRL_HCLK_Pos;
  100. }
  101. /* Workaround for Errata 46 "Higher power consumption of LFRC" found at the Errata document
  102. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  103. if (nrf53_errata_46())
  104. {
  105. *((volatile uint32_t *)0x5003254Cul) = 0;
  106. }
  107. /* Workaround for Errata 49 "SLEEPENTER and SLEEPEXIT events asserted after pin reset" found at the Errata document
  108. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  109. if (nrf53_errata_49())
  110. {
  111. if (NRF_RESET_S->RESETREAS & RESET_RESETREAS_RESETPIN_Msk)
  112. {
  113. NRF_POWER_S->EVENTS_SLEEPENTER = 0;
  114. NRF_POWER_S->EVENTS_SLEEPEXIT = 0;
  115. }
  116. }
  117. /* Workaround for Errata 55 "Bits in RESETREAS are set when they should not be" found at the Errata document
  118. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  119. if (nrf53_errata_55())
  120. {
  121. if (NRF_RESET_S->RESETREAS & RESET_RESETREAS_RESETPIN_Msk){
  122. NRF_RESET_S->RESETREAS = ~RESET_RESETREAS_RESETPIN_Msk;
  123. }
  124. }
  125. /* Workaround for Errata 69 "VREGMAIN configuration is not retained in System OFF" found at the Errata document
  126. for your device located at https://infocenter.nordicsemi.com/index.jsp */
  127. if (nrf53_errata_69())
  128. {
  129. *((volatile uint32_t *)0x5000470Cul) =0x65ul;
  130. }
  131. #if defined(CONFIG_NFCT_PINS_AS_GPIOS)
  132. if ((NRF_UICR_S->NFCPINS & UICR_NFCPINS_PROTECT_Msk) == (UICR_NFCPINS_PROTECT_NFC << UICR_NFCPINS_PROTECT_Pos))
  133. {
  134. NRF_NVMC_S->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
  135. while (NRF_NVMC_S->READY == NVMC_READY_READY_Busy);
  136. NRF_UICR_S->NFCPINS &= ~UICR_NFCPINS_PROTECT_Msk;
  137. while (NRF_NVMC_S->READY == NVMC_READY_READY_Busy);
  138. NRF_NVMC_S->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
  139. while (NRF_NVMC_S->READY == NVMC_READY_READY_Busy);
  140. NVIC_SystemReset();
  141. }
  142. #endif
  143. /* Enable SWO trace functionality. If ENABLE_SWO is not defined, SWO pin will be used as GPIO (see Product
  144. Specification to see which one). */
  145. #if defined (ENABLE_SWO)
  146. // Enable Trace And Debug peripheral
  147. NRF_TAD_S->ENABLE = TAD_ENABLE_ENABLE_Msk;
  148. NRF_TAD_S->CLOCKSTART = TAD_CLOCKSTART_START_Msk;
  149. // Set up Trace pad SPU firewall
  150. NRF_SPU_S->GPIOPORT[0].PERM &= ~(1 << TRACE_TRACEDATA0_PIN);
  151. // Configure trace port pad
  152. NRF_P0_S->PIN_CNF[TRACE_TRACEDATA0_PIN] = TRACE_PIN_CNF_VALUE;
  153. // Select trace pin
  154. NRF_TAD_S->PSEL.TRACEDATA0 = TRACE_TRACEDATA0_PIN;
  155. // Set trace port speed to 64 MHz
  156. NRF_TAD_S->TRACEPORTSPEED = TAD_TRACEPORTSPEED_TRACEPORTSPEED_64MHz;
  157. #endif
  158. /* Enable Trace functionality. If ENABLE_TRACE is not defined, TRACE pins will be used as GPIOs (see Product
  159. Specification to see which ones). */
  160. #if defined (ENABLE_TRACE)
  161. // Enable Trace And Debug peripheral
  162. NRF_TAD_S->ENABLE = TAD_ENABLE_ENABLE_Msk;
  163. NRF_TAD_S->CLOCKSTART = TAD_CLOCKSTART_START_Msk;
  164. // Set up Trace pads SPU firewall
  165. NRF_SPU_S->GPIOPORT[0].PERM &= ~(1 << TRACE_TRACECLK_PIN);
  166. NRF_SPU_S->GPIOPORT[0].PERM &= ~(1 << TRACE_TRACEDATA0_PIN);
  167. NRF_SPU_S->GPIOPORT[0].PERM &= ~(1 << TRACE_TRACEDATA1_PIN);
  168. NRF_SPU_S->GPIOPORT[0].PERM &= ~(1 << TRACE_TRACEDATA2_PIN);
  169. NRF_SPU_S->GPIOPORT[0].PERM &= ~(1 << TRACE_TRACEDATA3_PIN);
  170. // Configure trace port pads
  171. NRF_P0_S->PIN_CNF[TRACE_TRACECLK_PIN] = TRACE_PIN_CNF_VALUE;
  172. NRF_P0_S->PIN_CNF[TRACE_TRACEDATA0_PIN] = TRACE_PIN_CNF_VALUE;
  173. NRF_P0_S->PIN_CNF[TRACE_TRACEDATA1_PIN] = TRACE_PIN_CNF_VALUE;
  174. NRF_P0_S->PIN_CNF[TRACE_TRACEDATA2_PIN] = TRACE_PIN_CNF_VALUE;
  175. NRF_P0_S->PIN_CNF[TRACE_TRACEDATA3_PIN] = TRACE_PIN_CNF_VALUE;
  176. // Select trace pins
  177. NRF_TAD_S->PSEL.TRACECLK = TRACE_TRACECLK_PIN;
  178. NRF_TAD_S->PSEL.TRACEDATA0 = TRACE_TRACEDATA0_PIN;
  179. NRF_TAD_S->PSEL.TRACEDATA1 = TRACE_TRACEDATA1_PIN;
  180. NRF_TAD_S->PSEL.TRACEDATA2 = TRACE_TRACEDATA2_PIN;
  181. NRF_TAD_S->PSEL.TRACEDATA3 = TRACE_TRACEDATA3_PIN;
  182. // Set trace port speed to 64 MHz
  183. NRF_TAD_S->TRACEPORTSPEED = TAD_TRACEPORTSPEED_TRACEPORTSPEED_64MHz;
  184. #endif
  185. /* Allow Non-Secure code to run FPU instructions.
  186. * If only the secure code should control FPU power state these registers should be configured accordingly in the secure application code. */
  187. SCB->NSACR |= (3UL << 10);
  188. /* Handle fw-branch APPROTECT setup. */
  189. nrf53_handle_approtect();
  190. #endif
  191. /* Enable the FPU if the compiler used floating point unit instructions. __FPU_USED is a MACRO defined by the
  192. * compiler. Since the FPU consumes energy, remember to disable FPU use in the compiler if floating point unit
  193. * operations are not used in your code. */
  194. #if (__FPU_USED == 1)
  195. SCB->CPACR |= (3UL << 20) | (3UL << 22);
  196. __DSB();
  197. __ISB();
  198. #endif
  199. SystemCoreClockUpdate();
  200. }
  201. /*lint --flb "Leave library region" */