system_nrf52810.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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_136(void);
  28. /* Helper functions for Errata workarounds in nRF52832 */
  29. #if defined (DEVELOP_IN_NRF52832)
  30. static bool errata_12(void);
  31. static bool errata_16(void);
  32. static bool errata_32(void);
  33. static bool errata_37(void);
  34. static bool errata_57(void);
  35. static bool errata_108(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://infocenter.nordicsemi.com/ */
  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://infocenter.nordicsemi.com/ */
  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://infocenter.nordicsemi.com/ */
  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://infocenter.nordicsemi.com/ */
  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://infocenter.nordicsemi.com/ */
  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://infocenter.nordicsemi.com/ */
  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://infocenter.nordicsemi.com/ */
  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://infocenter.nordicsemi.com/ */
  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://infocenter.nordicsemi.com/ */
  142. if (errata_103()){
  143. NRF_CCM->MAXPACKETSIZE = 0xFBul;
  144. }
  145. #if defined (DEVELOP_IN_NRF52832)
  146. /* 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
  147. for your device located at https://infocenter.nordicsemi.com/ */
  148. if (errata_108()){
  149. *(volatile uint32_t *)0x40000EE4 = *(volatile uint32_t *)0x10000258 & 0x0000004F;
  150. }
  151. #endif
  152. /* Workaround for Errata 136 "System: Bits in RESETREAS are set when they should not be" found at the Errata document
  153. for your device located at https://infocenter.nordicsemi.com/ */
  154. if (errata_136()){
  155. if (NRF_POWER->RESETREAS & POWER_RESETREAS_RESETPIN_Msk){
  156. NRF_POWER->RESETREAS = ~POWER_RESETREAS_RESETPIN_Msk;
  157. }
  158. }
  159. #if defined (DEVELOP_IN_NRF52832)
  160. /* Workaround for Errata 182 "RADIO: Fixes for anomalies #102, #106, and #107 do not take effect" found at the Errata document
  161. for your device located at https://infocenter.nordicsemi.com/ */
  162. if (errata_182()){
  163. *(volatile uint32_t *) 0x4000173C |= (0x1 << 10);
  164. }
  165. #endif
  166. /* Configure GPIO pads as pPin Reset pin if Pin Reset capabilities desired. If CONFIG_GPIO_AS_PINRESET is not
  167. defined, pin reset will not be available. One GPIO (see Product Specification to see which one) will then be
  168. reserved for PinReset and not available as normal GPIO. */
  169. #if defined (CONFIG_GPIO_AS_PINRESET)
  170. if (((NRF_UICR->PSELRESET[0] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos)) ||
  171. ((NRF_UICR->PSELRESET[1] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos))){
  172. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
  173. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  174. NRF_UICR->PSELRESET[0] = 21;
  175. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  176. NRF_UICR->PSELRESET[1] = 21;
  177. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  178. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
  179. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  180. NVIC_SystemReset();
  181. }
  182. #endif
  183. SystemCoreClockUpdate();
  184. }
  185. #if defined (DEVELOP_IN_NRF52832)
  186. static bool errata_12(void)
  187. {
  188. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  189. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  190. return true;
  191. }
  192. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x40){
  193. return true;
  194. }
  195. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x50){
  196. return true;
  197. }
  198. }
  199. return false;
  200. }
  201. #endif
  202. #if defined (DEVELOP_IN_NRF52832)
  203. static bool errata_16(void)
  204. {
  205. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  206. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  207. return true;
  208. }
  209. }
  210. return false;
  211. }
  212. #endif
  213. static bool errata_31(void)
  214. {
  215. if ((*(uint32_t *)0x10000130ul == 0xAul) && (*(uint32_t *)0x10000134ul == 0x0ul)){
  216. return true;
  217. }
  218. #if defined (DEVELOP_IN_NRF52832)
  219. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  220. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  221. return true;
  222. }
  223. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x40){
  224. return true;
  225. }
  226. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x50){
  227. return true;
  228. }
  229. }
  230. #endif
  231. /* Fix should always apply. */
  232. return true;
  233. }
  234. #if defined (DEVELOP_IN_NRF52832)
  235. static bool errata_32(void)
  236. {
  237. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  238. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  239. return true;
  240. }
  241. }
  242. return false;
  243. }
  244. #endif
  245. static bool errata_36(void)
  246. {
  247. if ((*(uint32_t *)0x10000130ul == 0xAul) && (*(uint32_t *)0x10000134ul == 0x0ul)){
  248. return true;
  249. }
  250. #if defined (DEVELOP_IN_NRF52832)
  251. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  252. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  253. return true;
  254. }
  255. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x40){
  256. return true;
  257. }
  258. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x50){
  259. return true;
  260. }
  261. }
  262. #endif
  263. /* Fix should always apply. */
  264. return true;
  265. }
  266. #if defined (DEVELOP_IN_NRF52832)
  267. static bool errata_37(void)
  268. {
  269. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  270. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  271. return true;
  272. }
  273. }
  274. return false;
  275. }
  276. #endif
  277. #if defined (DEVELOP_IN_NRF52832)
  278. static bool errata_57(void)
  279. {
  280. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  281. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  282. return true;
  283. }
  284. }
  285. return false;
  286. }
  287. #endif
  288. static bool errata_66(void)
  289. {
  290. if ((*(uint32_t *)0x10000130ul == 0xAul) && (*(uint32_t *)0x10000134ul == 0x0ul)){
  291. return true;
  292. }
  293. #if defined (DEVELOP_IN_NRF52832)
  294. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  295. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x50){
  296. return true;
  297. }
  298. }
  299. #endif
  300. /* Fix should always apply. */
  301. return true;
  302. }
  303. static bool errata_103(void)
  304. {
  305. if ((*(uint32_t *)0x10000130ul == 0xAul) && (*(uint32_t *)0x10000134ul == 0x0ul)){
  306. return true;
  307. }
  308. /* Fix should always apply. */
  309. return true;
  310. }
  311. #if defined (DEVELOP_IN_NRF52832)
  312. static bool errata_108(void)
  313. {
  314. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  315. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  316. return true;
  317. }
  318. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x40){
  319. return true;
  320. }
  321. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x50){
  322. return true;
  323. }
  324. }
  325. return false;
  326. }
  327. #endif
  328. static bool errata_136(void)
  329. {
  330. if ((*(uint32_t *)0x10000130ul == 0xAul) && (*(uint32_t *)0x10000134ul == 0x0ul)){
  331. return true;
  332. }
  333. #if defined (DEVELOP_IN_NRF52832)
  334. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x6) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)){
  335. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30){
  336. return true;
  337. }
  338. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x40){
  339. return true;
  340. }
  341. if (((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x50){
  342. return true;
  343. }
  344. }
  345. #endif
  346. /* Fix should always apply. */
  347. return true;
  348. }
  349. #if defined (DEVELOP_IN_NRF52832)
  350. static bool errata_182(void)
  351. {
  352. if (*(uint32_t *)0x10000130ul == 0x6ul){
  353. if (*(uint32_t *)0x10000134ul == 0x6ul){
  354. return true;
  355. }
  356. }
  357. return false;
  358. }
  359. #endif
  360. /*lint --flb "Leave library region" */