system_nrf52840.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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_nrf52840.h"
  21. /*lint ++flb "Enter library region" */
  22. #define __SYSTEM_CLOCK_64M (64000000UL)
  23. static bool errata_36(void);
  24. static bool errata_66(void);
  25. static bool errata_98(void);
  26. static bool errata_103(void);
  27. static bool errata_115(void);
  28. static bool errata_120(void);
  29. static bool errata_136(void);
  30. #if defined ( __CC_ARM )
  31. uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK_64M;
  32. #elif defined ( __ICCARM__ )
  33. __root uint32_t SystemCoreClock = __SYSTEM_CLOCK_64M;
  34. #elif defined ( __GNUC__ )
  35. uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK_64M;
  36. #endif
  37. void SystemCoreClockUpdate(void)
  38. {
  39. SystemCoreClock = __SYSTEM_CLOCK_64M;
  40. }
  41. void SystemInit(void)
  42. {
  43. /* Enable SWO trace functionality. If ENABLE_SWO is not defined, SWO pin will be used as GPIO (see Product
  44. Specification to see which one). */
  45. #if defined (ENABLE_SWO)
  46. CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  47. NRF_CLOCK->TRACECONFIG |= CLOCK_TRACECONFIG_TRACEMUX_Serial << CLOCK_TRACECONFIG_TRACEMUX_Pos;
  48. 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);
  49. #endif
  50. /* Enable Trace functionality. If ENABLE_TRACE is not defined, TRACE pins will be used as GPIOs (see Product
  51. Specification to see which ones). */
  52. #if defined (ENABLE_TRACE)
  53. CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  54. NRF_CLOCK->TRACECONFIG |= CLOCK_TRACECONFIG_TRACEMUX_Parallel << CLOCK_TRACECONFIG_TRACEMUX_Pos;
  55. 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);
  56. 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);
  57. 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);
  58. 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);
  59. 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);
  60. #endif
  61. /* Workaround for Errata 36 "CLOCK: Some registers are not reset when expected" found at the Errata document
  62. for your device located at https://www.nordicsemi.com/DocLib */
  63. if (errata_36()){
  64. NRF_CLOCK->EVENTS_DONE = 0;
  65. NRF_CLOCK->EVENTS_CTTO = 0;
  66. NRF_CLOCK->CTIV = 0;
  67. }
  68. /* Workaround for Errata 66 "TEMP: Linearity specification not met with default settings" found at the Errata document
  69. for your device located at https://www.nordicsemi.com/DocLib */
  70. if (errata_66()){
  71. NRF_TEMP->A0 = NRF_FICR->TEMP.A0;
  72. NRF_TEMP->A1 = NRF_FICR->TEMP.A1;
  73. NRF_TEMP->A2 = NRF_FICR->TEMP.A2;
  74. NRF_TEMP->A3 = NRF_FICR->TEMP.A3;
  75. NRF_TEMP->A4 = NRF_FICR->TEMP.A4;
  76. NRF_TEMP->A5 = NRF_FICR->TEMP.A5;
  77. NRF_TEMP->B0 = NRF_FICR->TEMP.B0;
  78. NRF_TEMP->B1 = NRF_FICR->TEMP.B1;
  79. NRF_TEMP->B2 = NRF_FICR->TEMP.B2;
  80. NRF_TEMP->B3 = NRF_FICR->TEMP.B3;
  81. NRF_TEMP->B4 = NRF_FICR->TEMP.B4;
  82. NRF_TEMP->B5 = NRF_FICR->TEMP.B5;
  83. NRF_TEMP->T0 = NRF_FICR->TEMP.T0;
  84. NRF_TEMP->T1 = NRF_FICR->TEMP.T1;
  85. NRF_TEMP->T2 = NRF_FICR->TEMP.T2;
  86. NRF_TEMP->T3 = NRF_FICR->TEMP.T3;
  87. NRF_TEMP->T4 = NRF_FICR->TEMP.T4;
  88. }
  89. /* Workaround for Errata 98 "NFCT: Not able to communicate with the peer" found at the Errata document
  90. for your device located at https://www.nordicsemi.com/DocLib */
  91. if (errata_98()){
  92. *(volatile uint32_t *)0x4000568Cul = 0x00038148ul;
  93. }
  94. /* Workaround for Errata 103 "CCM: Wrong reset value of CCM MAXPACKETSIZE" found at the Errata document
  95. for your device located at https://www.nordicsemi.com/DocLib */
  96. if (errata_103()){
  97. NRF_CCM->MAXPACKETSIZE = 0xFBul;
  98. }
  99. /* 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
  100. for your device located at https://www.nordicsemi.com/DocLib */
  101. if (errata_115()){
  102. *(volatile uint32_t *)0x40000EE4 = (*(volatile uint32_t *)0x40000EE4 & 0xFFFFFFF0) | (*(uint32_t *)0x10000258 & 0x0000000F);
  103. }
  104. /* Workaround for Errata 120 "QSPI: Data read or written is corrupted" found at the Errata document
  105. for your device located at https://www.nordicsemi.com/DocLib */
  106. if (errata_120()){
  107. *(volatile uint32_t *)0x40029640ul = 0x200ul;
  108. }
  109. /* Workaround for Errata 136 "System: Bits in RESETREAS are set when they should not be" found at the Errata document
  110. for your device located at https://www.nordicsemi.com/DocLib */
  111. if (errata_136()){
  112. if (NRF_POWER->RESETREAS & POWER_RESETREAS_RESETPIN_Msk){
  113. NRF_POWER->RESETREAS = ~POWER_RESETREAS_RESETPIN_Msk;
  114. }
  115. }
  116. /* Enable the FPU if the compiler used floating point unit instructions. __FPU_USED is a MACRO defined by the
  117. * compiler. Since the FPU consumes energy, remember to disable FPU use in the compiler if floating point unit
  118. * operations are not used in your code. */
  119. #if (__FPU_USED == 1)
  120. SCB->CPACR |= (3UL << 20) | (3UL << 22);
  121. __DSB();
  122. __ISB();
  123. #endif
  124. /* Configure NFCT pins as GPIOs if NFCT is not to be used in your code. If CONFIG_NFCT_PINS_AS_GPIOS is not defined,
  125. two GPIOs (see Product Specification to see which ones) will be reserved for NFC and will not be available as
  126. normal GPIOs. */
  127. #if defined (CONFIG_NFCT_PINS_AS_GPIOS)
  128. if ((NRF_UICR->NFCPINS & UICR_NFCPINS_PROTECT_Msk) == (UICR_NFCPINS_PROTECT_NFC << UICR_NFCPINS_PROTECT_Pos)){
  129. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
  130. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  131. NRF_UICR->NFCPINS &= ~UICR_NFCPINS_PROTECT_Msk;
  132. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  133. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
  134. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  135. NVIC_SystemReset();
  136. }
  137. #endif
  138. /* Configure GPIO pads as pPin Reset pin if Pin Reset capabilities desired. If CONFIG_GPIO_AS_PINRESET is not
  139. defined, pin reset will not be available. One GPIO (see Product Specification to see which one) will then be
  140. reserved for PinReset and not available as normal GPIO. */
  141. #if defined (CONFIG_GPIO_AS_PINRESET)
  142. if (((NRF_UICR->PSELRESET[0] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos)) ||
  143. ((NRF_UICR->PSELRESET[1] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos))){
  144. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
  145. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  146. NRF_UICR->PSELRESET[0] = 18;
  147. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  148. NRF_UICR->PSELRESET[1] = 18;
  149. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  150. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
  151. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  152. NVIC_SystemReset();
  153. }
  154. #endif
  155. SystemCoreClockUpdate();
  156. }
  157. static bool errata_36(void)
  158. {
  159. if (*(uint32_t *)0x10000130ul == 0x8ul){
  160. if (*(uint32_t *)0x10000134ul == 0x0ul){
  161. return true;
  162. }
  163. if (*(uint32_t *)0x10000134ul == 0x1ul){
  164. return true;
  165. }
  166. if (*(uint32_t *)0x10000134ul == 0x2ul){
  167. return true;
  168. }
  169. if (*(uint32_t *)0x10000134ul == 0x3ul){
  170. return true;
  171. }
  172. }
  173. /* Apply by default for unknown devices until errata is confirmed fixed. */
  174. return true;
  175. }
  176. static bool errata_66(void)
  177. {
  178. if (*(uint32_t *)0x10000130ul == 0x8ul){
  179. if (*(uint32_t *)0x10000134ul == 0x0ul){
  180. return true;
  181. }
  182. if (*(uint32_t *)0x10000134ul == 0x1ul){
  183. return true;
  184. }
  185. if (*(uint32_t *)0x10000134ul == 0x2ul){
  186. return true;
  187. }
  188. if (*(uint32_t *)0x10000134ul == 0x3ul){
  189. return true;
  190. }
  191. }
  192. /* Apply by default for unknown devices until errata is confirmed fixed. */
  193. return true;
  194. }
  195. static bool errata_98(void)
  196. {
  197. if (*(uint32_t *)0x10000130ul == 0x8ul){
  198. if (*(uint32_t *)0x10000134ul == 0x0ul){
  199. return true;
  200. }
  201. }
  202. return false;
  203. }
  204. static bool errata_103(void)
  205. {
  206. if (*(uint32_t *)0x10000130ul == 0x8ul){
  207. if (*(uint32_t *)0x10000134ul == 0x0ul){
  208. return true;
  209. }
  210. }
  211. return false;
  212. }
  213. static bool errata_115(void)
  214. {
  215. if (*(uint32_t *)0x10000130ul == 0x8ul){
  216. if (*(uint32_t *)0x10000134ul == 0x0ul){
  217. return true;
  218. }
  219. }
  220. return false;
  221. }
  222. static bool errata_120(void)
  223. {
  224. if (*(uint32_t *)0x10000130ul == 0x8ul){
  225. if (*(uint32_t *)0x10000134ul == 0x0ul){
  226. return true;
  227. }
  228. }
  229. return false;
  230. }
  231. static bool errata_136(void)
  232. {
  233. if (*(uint32_t *)0x10000130ul == 0x8ul){
  234. if (*(uint32_t *)0x10000134ul == 0x0ul){
  235. return true;
  236. }
  237. if (*(uint32_t *)0x10000134ul == 0x1ul){
  238. return true;
  239. }
  240. if (*(uint32_t *)0x10000134ul == 0x2ul){
  241. return true;
  242. }
  243. if (*(uint32_t *)0x10000134ul == 0x3ul){
  244. return true;
  245. }
  246. }
  247. /* Apply by default for unknown devices until errata is confirmed fixed. */
  248. return true;
  249. }
  250. /*lint --flb "Leave library region" */