system_nrf5340_application.c 10 KB

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