system_nrf52810.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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_nrf52810.h"
  21. /*lint ++flb "Enter library region" */
  22. #define __SYSTEM_CLOCK_64M (64000000UL)
  23. static bool errata_31(void);
  24. static bool errata_36(void);
  25. static bool errata_66(void);
  26. static bool errata_103(void);
  27. static bool errata_108(void);
  28. static bool errata_136(void);
  29. /* Helper functions for Errata workarounds in nRF52832 */
  30. #if defined (DEVELOP_IN_NRF52832)
  31. static bool errata_12(void);
  32. static bool errata_16(void);
  33. static bool errata_32(void);
  34. static bool errata_37(void);
  35. static bool errata_57(void);
  36. static bool errata_182(void);
  37. #endif
  38. #if defined ( __CC_ARM )
  39. uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK_64M;
  40. #elif defined ( __ICCARM__ )
  41. __root uint32_t SystemCoreClock = __SYSTEM_CLOCK_64M;
  42. #elif defined ( __GNUC__ )
  43. uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK_64M;
  44. #endif
  45. void SystemCoreClockUpdate(void)
  46. {
  47. SystemCoreClock = __SYSTEM_CLOCK_64M;
  48. }
  49. void SystemInit(void)
  50. {
  51. /* Enable SWO trace functionality. If ENABLE_SWO is not defined, SWO pin will be used as GPIO (see Product
  52. Specification to see which one). Only available if the developing environment is an nRF52832 device. */
  53. #if defined (DEVELOP_IN_NRF52832) && defined (ENABLE_SWO)
  54. CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  55. NRF_CLOCK->TRACECONFIG |= CLOCK_TRACECONFIG_TRACEMUX_Serial << CLOCK_TRACECONFIG_TRACEMUX_Pos;
  56. NRF_P0->PIN_CNF[18] = (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. #endif
  58. /* Enable Trace functionality. If ENABLE_TRACE is not defined, TRACE pins will be used as GPIOs (see Product
  59. Specification to see which ones). Only available if the developing environment is an nRF52832 device. */
  60. #if defined (DEVELOP_IN_NRF52832) && defined (ENABLE_TRACE)
  61. CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  62. NRF_CLOCK->TRACECONFIG |= CLOCK_TRACECONFIG_TRACEMUX_Parallel << CLOCK_TRACECONFIG_TRACEMUX_Pos;
  63. NRF_P0->PIN_CNF[14] = (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);
  64. NRF_P0->PIN_CNF[15] = (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);
  65. NRF_P0->PIN_CNF[16] = (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);
  66. NRF_P0->PIN_CNF[18] = (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);
  67. NRF_P0->PIN_CNF[20] = (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);
  68. #endif
  69. #if defined (DEVELOP_IN_NRF52832)
  70. /* Workaround for Errata 12 "COMP: Reference ladder not correctly calibrated" found at the Errata document
  71. for nRF52832 device located at https://www.nordicsemi.com/DocLib */
  72. if (errata_12()){
  73. *(volatile uint32_t *)0x40013540 = (*(uint32_t *)0x10000324 & 0x00001F00) >> 8;
  74. }
  75. #endif
  76. #if defined (DEVELOP_IN_NRF52832)
  77. /* Workaround for Errata 16 "System: RAM may be corrupt on wakeup from CPU IDLE" found at the Errata document
  78. for nRF52832 device located at https://www.nordicsemi.com/DocLib */
  79. if (errata_16()){
  80. *(volatile uint32_t *)0x4007C074 = 3131961357ul;
  81. }
  82. #endif
  83. /* Workaround for Errata 31 "CLOCK: Calibration values are not correctly loaded from FICR at reset" found at the Errata document
  84. for your device located at https://www.nordicsemi.com/DocLib */
  85. if (errata_31()){
  86. *(volatile uint32_t *)0x4000053C = ((*(volatile uint32_t *)0x10000244) & 0x0000E000) >> 13;
  87. }
  88. #if defined (DEVELOP_IN_NRF52832)
  89. /* Workaround for Errata 32 "DIF: Debug session automatically enables TracePort pins" found at the Errata document
  90. for nRF52832 device located at https://www.nordicsemi.com/DocLib */
  91. if (errata_32()){
  92. CoreDebug->DEMCR &= ~CoreDebug_DEMCR_TRCENA_Msk;
  93. }
  94. #endif
  95. /* Workaround for Errata 36 "CLOCK: Some registers are not reset when expected" found at the Errata document
  96. for your device located at https://www.nordicsemi.com/DocLib */
  97. if (errata_36()){
  98. NRF_CLOCK->EVENTS_DONE = 0;
  99. NRF_CLOCK->EVENTS_CTTO = 0;
  100. NRF_CLOCK->CTIV = 0;
  101. }
  102. #if defined (DEVELOP_IN_NRF52832)
  103. /* Workaround for Errata 37 "RADIO: Encryption engine is slow by default" found at the Errata document
  104. for your device located at https://www.nordicsemi.com/DocLib */
  105. if (errata_37()){
  106. *(volatile uint32_t *)0x400005A0 = 0x3;
  107. }
  108. #endif
  109. #if defined (DEVELOP_IN_NRF52832)
  110. /* Workaround for Errata 57 "NFCT: NFC Modulation amplitude" found at the Errata document
  111. for your device located at https://www.nordicsemi.com/DocLib */
  112. if (errata_57()){
  113. *(volatile uint32_t *)0x40005610 = 0x00000005;
  114. *(volatile uint32_t *)0x40005688 = 0x00000001;
  115. *(volatile uint32_t *)0x40005618 = 0x00000000;
  116. *(volatile uint32_t *)0x40005614 = 0x0000003F;
  117. }
  118. #endif
  119. /* Workaround for Errata 66 "TEMP: Linearity specification not met with default settings" found at the Errata document
  120. for your device located at https://www.nordicsemi.com/DocLib */
  121. if (errata_66()){
  122. NRF_TEMP->A0 = NRF_FICR->TEMP.A0;
  123. NRF_TEMP->A1 = NRF_FICR->TEMP.A1;
  124. NRF_TEMP->A2 = NRF_FICR->TEMP.A2;
  125. NRF_TEMP->A3 = NRF_FICR->TEMP.A3;
  126. NRF_TEMP->A4 = NRF_FICR->TEMP.A4;
  127. NRF_TEMP->A5 = NRF_FICR->TEMP.A5;
  128. NRF_TEMP->B0 = NRF_FICR->TEMP.B0;
  129. NRF_TEMP->B1 = NRF_FICR->TEMP.B1;
  130. NRF_TEMP->B2 = NRF_FICR->TEMP.B2;
  131. NRF_TEMP->B3 = NRF_FICR->TEMP.B3;
  132. NRF_TEMP->B4 = NRF_FICR->TEMP.B4;
  133. NRF_TEMP->B5 = NRF_FICR->TEMP.B5;
  134. NRF_TEMP->T0 = NRF_FICR->TEMP.T0;
  135. NRF_TEMP->T1 = NRF_FICR->TEMP.T1;
  136. NRF_TEMP->T2 = NRF_FICR->TEMP.T2;
  137. NRF_TEMP->T3 = NRF_FICR->TEMP.T3;
  138. NRF_TEMP->T4 = NRF_FICR->TEMP.T4;
  139. }
  140. /* Workaround for Errata 103 "CCM: Wrong reset value of CCM MAXPACKETSIZE" found at the Errata document
  141. for your device located at https://www.nordicsemi.com/DocLib */
  142. if (errata_103()){
  143. NRF_CCM->MAXPACKETSIZE = 0xFBul;
  144. }
  145. /* Workaround for Errata 108 "RAM: RAM content cannot be trusted upon waking up from System ON Idle or System OFF mode" found at the Errata document
  146. for your device located at https://www.nordicsemi.com/DocLib */
  147. if (errata_108()){
  148. *(volatile uint32_t *)0x40000EE4 = *(volatile uint32_t *)0x10000258 & 0x0000004F;
  149. }
  150. /* Workaround for Errata 136 "System: Bits in RESETREAS are set when they should not be" found at the Errata document
  151. for your device located at https://www.nordicsemi.com/DocLib */
  152. if (errata_136()){
  153. if (NRF_POWER->RESETREAS & POWER_RESETREAS_RESETPIN_Msk){
  154. NRF_POWER->RESETREAS = ~POWER_RESETREAS_RESETPIN_Msk;
  155. }
  156. }
  157. #if defined (DEVELOP_IN_NRF52832)
  158. /* Workaround for Errata 182 "RADIO: Fixes for anomalies #102, #106, and #107 do not take effect" found at the Errata document
  159. for your device located at https://www.nordicsemi.com/DocLib */
  160. if (errata_182()){
  161. *(volatile uint32_t *) 0x4000173C |= (0x1 << 10);
  162. }
  163. #endif
  164. /* Configure GPIO pads as pPin Reset pin if Pin Reset capabilities desired. If CONFIG_GPIO_AS_PINRESET is not
  165. defined, pin reset will not be available. One GPIO (see Product Specification to see which one) will then be
  166. reserved for PinReset and not available as normal GPIO. */
  167. #if defined (CONFIG_GPIO_AS_PINRESET)
  168. if (((NRF_UICR->PSELRESET[0] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos)) ||
  169. ((NRF_UICR->PSELRESET[1] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos))){
  170. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
  171. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  172. NRF_UICR->PSELRESET[0] = 21;
  173. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  174. NRF_UICR->PSELRESET[1] = 21;
  175. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  176. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
  177. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  178. NVIC_SystemReset();
  179. }
  180. #endif
  181. SystemCoreClockUpdate();
  182. }
  183. #if defined (DEVELOP_IN_NRF52832)
  184. static bool errata_12(void)
  185. {
  186. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  187. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  188. return true;
  189. }
  190. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x40){
  191. return true;
  192. }
  193. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x50){
  194. return true;
  195. }
  196. }
  197. return false;
  198. }
  199. #endif
  200. #if defined (DEVELOP_IN_NRF52832)
  201. static bool errata_16(void)
  202. {
  203. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  204. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  205. return true;
  206. }
  207. }
  208. return false;
  209. }
  210. #endif
  211. static bool errata_31(void)
  212. {
  213. if (*(uint32_t *)0x10000130ul == 0xAul){
  214. if (*(uint32_t *)0x10000134ul == 0x0ul){
  215. return true;
  216. }
  217. if (*(uint32_t *)0x10000134ul == 0x1ul){
  218. return true;
  219. }
  220. }
  221. #if defined (DEVELOP_IN_NRF52832)
  222. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  223. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  224. return true;
  225. }
  226. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x40){
  227. return true;
  228. }
  229. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x50){
  230. return true;
  231. }
  232. return false;
  233. }
  234. #endif
  235. /* Apply by default for unknown devices until errata is confirmed fixed. */
  236. return true;
  237. }
  238. #if defined (DEVELOP_IN_NRF52832)
  239. static bool errata_32(void)
  240. {
  241. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  242. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  243. return true;
  244. }
  245. }
  246. return false;
  247. }
  248. #endif
  249. static bool errata_36(void)
  250. {
  251. if (*(uint32_t *)0x10000130ul == 0xAul){
  252. if (*(uint32_t *)0x10000134ul == 0x0ul){
  253. return true;
  254. }
  255. if (*(uint32_t *)0x10000134ul == 0x1ul){
  256. return true;
  257. }
  258. }
  259. #if defined (DEVELOP_IN_NRF52832)
  260. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  261. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  262. return true;
  263. }
  264. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x40){
  265. return true;
  266. }
  267. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x50){
  268. return true;
  269. }
  270. return false;
  271. }
  272. #endif
  273. /* Apply by default for unknown devices until errata is confirmed fixed. */
  274. return true;
  275. }
  276. #if defined (DEVELOP_IN_NRF52832)
  277. static bool errata_37(void)
  278. {
  279. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  280. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  281. return true;
  282. }
  283. }
  284. return false;
  285. }
  286. #endif
  287. #if defined (DEVELOP_IN_NRF52832)
  288. static bool errata_57(void)
  289. {
  290. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  291. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  292. return true;
  293. }
  294. }
  295. return false;
  296. }
  297. #endif
  298. static bool errata_66(void)
  299. {
  300. if (*(uint32_t *)0x10000130ul == 0xAul){
  301. if (*(uint32_t *)0x10000134ul == 0x0ul){
  302. return true;
  303. }
  304. if (*(uint32_t *)0x10000134ul == 0x1ul){
  305. return true;
  306. }
  307. }
  308. #if defined (DEVELOP_IN_NRF52832)
  309. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  310. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x50){
  311. return true;
  312. }
  313. return false;
  314. }
  315. #endif
  316. /* Apply by default for unknown devices until errata is confirmed fixed. */
  317. return true;
  318. }
  319. static bool errata_103(void)
  320. {
  321. if (*(uint32_t *)0x10000130ul == 0xAul){
  322. if (*(uint32_t *)0x10000134ul == 0x0ul){
  323. return true;
  324. }
  325. }
  326. return false;
  327. }
  328. static bool errata_108(void)
  329. {
  330. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  331. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  332. return true;
  333. }
  334. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x40){
  335. return true;
  336. }
  337. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x50){
  338. return true;
  339. }
  340. }
  341. return false;
  342. }
  343. static bool errata_136(void)
  344. {
  345. if (*(uint32_t *)0x10000130ul == 0xAul){
  346. if (*(uint32_t *)0x10000134ul == 0x0ul){
  347. return true;
  348. }
  349. if (*(uint32_t *)0x10000134ul == 0x1ul){
  350. return true;
  351. }
  352. }
  353. #if defined (DEVELOP_IN_NRF52832)
  354. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  355. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  356. return true;
  357. }
  358. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x40){
  359. return true;
  360. }
  361. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x50){
  362. return true;
  363. }
  364. return false;
  365. }
  366. #endif
  367. /* Apply by default for unknown devices until errata is confirmed fixed. */
  368. return true;
  369. }
  370. #if defined (DEVELOP_IN_NRF52832)
  371. static bool errata_182(void)
  372. {
  373. if (*(uint32_t *)0x10000130ul == 0x6ul){
  374. if (*(uint32_t *)0x10000134ul == 0x6ul){
  375. return true;
  376. }
  377. }
  378. return false;
  379. }
  380. #endif
  381. /*lint --flb "Leave library region" */