system_nrf9160.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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_nrf9160.h"
  21. /*lint ++flb "Enter library region" */
  22. #define __SYSTEM_CLOCK (64000000UL) /*!< nRF9160 Application core uses a fixed System Clock Frequency of 64MHz */
  23. #define TRACE_PIN_CNF_VALUE ( (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos) | \
  24. (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | \
  25. (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) | \
  26. (GPIO_PIN_CNF_DRIVE_H0H1 << GPIO_PIN_CNF_DRIVE_Pos) | \
  27. (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | \
  28. (GPIO_PIN_CNF_MCUSEL_TND << GPIO_PIN_CNF_MCUSEL_Pos))
  29. #define TRACE_TRACECLK_PIN (21)
  30. #define TRACE_TRACEDATA0_PIN (22)
  31. #define TRACE_TRACEDATA1_PIN (23)
  32. #define TRACE_TRACEDATA2_PIN (24)
  33. #define TRACE_TRACEDATA3_PIN (25)
  34. #if defined ( __CC_ARM )
  35. uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK;
  36. #elif defined ( __ICCARM__ )
  37. __root uint32_t SystemCoreClock = __SYSTEM_CLOCK;
  38. #elif defined ( __GNUC__ )
  39. uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK;
  40. #endif
  41. /* Errata are only handled in secure mode since they usually need access to FICR. */
  42. #if !defined(NRF_TRUSTZONE_NONSECURE)
  43. static bool uicr_HFXOSRC_erased(void);
  44. static bool uicr_HFXOCNT_erased(void);
  45. static bool errata_6(void);
  46. static bool errata_14(void);
  47. static bool errata_15(void);
  48. static bool errata_20(void);
  49. #endif
  50. void SystemCoreClockUpdate(void)
  51. {
  52. SystemCoreClock = __SYSTEM_CLOCK;
  53. }
  54. void SystemInit(void)
  55. {
  56. #if !defined(NRF_TRUSTZONE_NONSECURE)
  57. /* Perform Secure-mode initialization routines. */
  58. /* Set all ARM SAU regions to NonSecure if TrustZone extensions are enabled.
  59. * Nordic SPU should handle Secure Attribution tasks */
  60. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  61. SAU->CTRL |= (1 << SAU_CTRL_ALLNS_Pos);
  62. #endif
  63. /* Trimming of the device. Copy all the trimming values from FICR into the target addresses. Trim
  64. until one ADDR is not initialized. */
  65. uint32_t index = 0;
  66. for (index = 0; index < 256ul && NRF_FICR_S->TRIMCNF[index].ADDR != 0xFFFFFFFFul; index++){
  67. #if defined ( __ICCARM__ )
  68. #pragma diag_suppress=Pa082
  69. #endif
  70. *(volatile uint32_t *)NRF_FICR_S->TRIMCNF[index].ADDR = NRF_FICR_S->TRIMCNF[index].DATA;
  71. #if defined ( __ICCARM__ )
  72. #pragma diag_default=Pa082
  73. #endif
  74. }
  75. /* Set UICR->HFXOSRC and UICR->HFXOCNT to working defaults if UICR was erased */
  76. if (uicr_HFXOSRC_erased() || uicr_HFXOCNT_erased()) {
  77. /* Wait for pending NVMC operations to finish */
  78. while (NRF_NVMC_S->READY != NVMC_READY_READY_Ready);
  79. /* Enable write mode in NVMC */
  80. NRF_NVMC_S->CONFIG = NVMC_CONFIG_WEN_Wen;
  81. while (NRF_NVMC_S->READY != NVMC_READY_READY_Ready);
  82. if (uicr_HFXOSRC_erased()){
  83. /* Write default value to UICR->HFXOSRC */
  84. NRF_UICR_S->HFXOSRC = (NRF_UICR_S->HFXOSRC & ~UICR_HFXOSRC_HFXOSRC_Msk) | UICR_HFXOSRC_HFXOSRC_TCXO;
  85. while (NRF_NVMC_S->READY != NVMC_READY_READY_Ready);
  86. }
  87. if (uicr_HFXOCNT_erased()){
  88. /* Write default value to UICR->HFXOCNT */
  89. NRF_UICR_S->HFXOCNT = (NRF_UICR_S->HFXOCNT & ~UICR_HFXOCNT_HFXOCNT_Msk) | 0x20;
  90. while (NRF_NVMC_S->READY != NVMC_READY_READY_Ready);
  91. }
  92. /* Enable read mode in NVMC */
  93. NRF_NVMC_S->CONFIG = NVMC_CONFIG_WEN_Ren;
  94. while (NRF_NVMC_S->READY != NVMC_READY_READY_Ready);
  95. /* Reset to apply clock select update */
  96. NVIC_SystemReset();
  97. }
  98. /* Workaround for Errata 6 "POWER: SLEEPENTER and SLEEPEXIT events asserted after pin reset" found at the Errata document
  99. for your device located at https://www.nordicsemi.com/DocLib */
  100. if (errata_6()){
  101. NRF_POWER_S->EVENTS_SLEEPENTER = (POWER_EVENTS_SLEEPENTER_EVENTS_SLEEPENTER_NotGenerated << POWER_EVENTS_SLEEPENTER_EVENTS_SLEEPENTER_Pos);
  102. NRF_POWER_S->EVENTS_SLEEPEXIT = (POWER_EVENTS_SLEEPEXIT_EVENTS_SLEEPEXIT_NotGenerated << POWER_EVENTS_SLEEPEXIT_EVENTS_SLEEPEXIT_Pos);
  103. }
  104. /* Workaround for Errata 14 "REGULATORS: LDO mode at startup" found at the Errata document
  105. for your device located at https://www.nordicsemi.com/DocLib */
  106. if (errata_14()){
  107. *((volatile uint32_t *)0x50004A38) = 0x01ul;
  108. NRF_REGULATORS_S->DCDCEN = REGULATORS_DCDCEN_DCDCEN_Enabled << REGULATORS_DCDCEN_DCDCEN_Pos;
  109. }
  110. /* Workaround for Errata 15 "REGULATORS: LDO mode at startup" found at the Errata document
  111. for your device located at https://www.nordicsemi.com/DocLib */
  112. if (errata_15()){
  113. *((volatile uint32_t *)0x50004A38) = 0x00ul;
  114. NRF_REGULATORS_S->DCDCEN = REGULATORS_DCDCEN_DCDCEN_Enabled << REGULATORS_DCDCEN_DCDCEN_Pos;
  115. }
  116. /* Workaround for Errata 20 "RAM content cannot be trusted upon waking up from System ON Idle or System OFF mode" found at the Errata document
  117. for your device located at https://www.nordicsemi.com/DocLib */
  118. if (errata_20()){
  119. *((volatile uint32_t *)0x5003AEE4) = 0xC;
  120. }
  121. /* Enable SWO trace functionality. If ENABLE_SWO is not defined, SWO pin will be used as GPIO (see Product
  122. Specification to see which one). */
  123. #if defined (ENABLE_SWO)
  124. NRF_TAD_S->ENABLE = TAD_ENABLE_ENABLE_Msk;
  125. NRF_TAD_S->CLOCKSTART = TAD_CLOCKSTART_START_Msk;
  126. NRF_TAD_S->PSEL.TRACEDATA0 = TRACE_TRACEDATA0_PIN;
  127. NRF_TAD_S->TRACEPORTSPEED = TAD_TRACEPORTSPEED_TRACEPORTSPEED_32MHz;
  128. NRF_P0_S->PIN_CNF[TRACE_TRACEDATA0_PIN] = TRACE_PIN_CNF_VALUE;
  129. #endif
  130. /* Enable Trace functionality. If ENABLE_TRACE is not defined, TRACE pins will be used as GPIOs (see Product
  131. Specification to see which ones). */
  132. #if defined (ENABLE_TRACE)
  133. NRF_TAD_S->ENABLE = TAD_ENABLE_ENABLE_Msk;
  134. NRF_TAD_S->CLOCKSTART = TAD_CLOCKSTART_START_Msk;
  135. NRF_TAD_S->PSEL.TRACECLK = TRACE_TRACECLK_PIN;
  136. NRF_TAD_S->PSEL.TRACEDATA0 = TRACE_TRACEDATA0_PIN;
  137. NRF_TAD_S->PSEL.TRACEDATA1 = TRACE_TRACEDATA1_PIN;
  138. NRF_TAD_S->PSEL.TRACEDATA2 = TRACE_TRACEDATA2_PIN;
  139. NRF_TAD_S->PSEL.TRACEDATA3 = TRACE_TRACEDATA3_PIN;
  140. NRF_TAD_S->TRACEPORTSPEED = TAD_TRACEPORTSPEED_TRACEPORTSPEED_32MHz;
  141. NRF_P0_S->PIN_CNF[TRACE_TRACECLK_PIN] = TRACE_PIN_CNF_VALUE;
  142. NRF_P0_S->PIN_CNF[TRACE_TRACEDATA0_PIN] = TRACE_PIN_CNF_VALUE;
  143. NRF_P0_S->PIN_CNF[TRACE_TRACEDATA1_PIN] = TRACE_PIN_CNF_VALUE;
  144. NRF_P0_S->PIN_CNF[TRACE_TRACEDATA2_PIN] = TRACE_PIN_CNF_VALUE;
  145. NRF_P0_S->PIN_CNF[TRACE_TRACEDATA3_PIN] = TRACE_PIN_CNF_VALUE;
  146. #endif
  147. /* Allow Non-Secure code to run FPU instructions.
  148. * If only the secure code should control FPU power state these registers should be configured accordingly in the secure application code. */
  149. SCB->NSACR |= (3UL << 10);
  150. #endif
  151. /* Enable the FPU if the compiler used floating point unit instructions. __FPU_USED is a MACRO defined by the
  152. * compiler. Since the FPU consumes energy, remember to disable FPU use in the compiler if floating point unit
  153. * operations are not used in your code. */
  154. #if (__FPU_USED == 1)
  155. SCB->CPACR |= (3UL << 20) | (3UL << 22);
  156. __DSB();
  157. __ISB();
  158. #endif
  159. SystemCoreClockUpdate();
  160. }
  161. #if !defined(NRF_TRUSTZONE_NONSECURE)
  162. bool uicr_HFXOCNT_erased()
  163. {
  164. if (NRF_UICR_S->HFXOCNT == 0xFFFFFFFFul) {
  165. return true;
  166. }
  167. return false;
  168. }
  169. bool uicr_HFXOSRC_erased()
  170. {
  171. if ((NRF_UICR_S->HFXOSRC & UICR_HFXOSRC_HFXOSRC_Msk) != UICR_HFXOSRC_HFXOSRC_TCXO) {
  172. return true;
  173. }
  174. return false;
  175. }
  176. bool errata_6()
  177. {
  178. if (*(uint32_t *)0x00FF0130 == 0x9ul){
  179. if (*(uint32_t *)0x00FF0134 == 0x01ul){
  180. return true;
  181. }
  182. if (*(uint32_t *)0x00FF0134 == 0x02ul){
  183. return true;
  184. }
  185. }
  186. return false;
  187. }
  188. bool errata_14()
  189. {
  190. if (*(uint32_t *)0x00FF0130 == 0x9ul){
  191. if (*(uint32_t *)0x00FF0134 == 0x01ul){
  192. return true;
  193. }
  194. }
  195. return false;
  196. }
  197. bool errata_15()
  198. {
  199. if (*(uint32_t *)0x00FF0130 == 0x9ul){
  200. if (*(uint32_t *)0x00FF0134 == 0x02ul){
  201. return true;
  202. }
  203. }
  204. return false;
  205. }
  206. bool errata_20()
  207. {
  208. if (*(uint32_t *)0x00FF0130 == 0x9ul){
  209. if (*(uint32_t *)0x00FF0134 == 0x02ul){
  210. return true;
  211. }
  212. }
  213. return false;
  214. }
  215. #endif
  216. /*lint --flb "Leave library region" */