system_nrf52811.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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_nrf52811.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_108(void);
  27. static bool errata_136(void);
  28. /* nRF52840 erratas */
  29. #ifdef DEVELOP_IN_NRF52840
  30. static bool errata_103(void);
  31. static bool errata_115(void);
  32. #endif
  33. #if defined ( __CC_ARM )
  34. uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK_64M;
  35. #elif defined ( __ICCARM__ )
  36. __root uint32_t SystemCoreClock = __SYSTEM_CLOCK_64M;
  37. #elif defined ( __GNUC__ )
  38. uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK_64M;
  39. #endif
  40. void SystemCoreClockUpdate(void)
  41. {
  42. SystemCoreClock = __SYSTEM_CLOCK_64M;
  43. }
  44. void SystemInit(void)
  45. {
  46. /* Workaround for Errata 31 "CLOCK: Calibration values are not correctly loaded from FICR at reset" found at the Errata document
  47. for your device located at https://www.nordicsemi.com/DocLib */
  48. if (errata_31()){
  49. *(volatile uint32_t *)0x4000053C = ((*(volatile uint32_t *)0x10000244) & 0x0000E000) >> 13;
  50. }
  51. /* Workaround for Errata 36 "CLOCK: Some registers are not reset when expected" found at the Errata document
  52. for your device located at https://www.nordicsemi.com/DocLib */
  53. if (errata_36()){
  54. NRF_CLOCK->EVENTS_DONE = 0;
  55. NRF_CLOCK->EVENTS_CTTO = 0;
  56. NRF_CLOCK->CTIV = 0;
  57. }
  58. /* Workaround for Errata 66 "TEMP: Linearity specification not met with default settings" found at the Errata document
  59. for your device located at https://www.nordicsemi.com/DocLib */
  60. if (errata_66()){
  61. NRF_TEMP->A0 = NRF_FICR->TEMP.A0;
  62. NRF_TEMP->A1 = NRF_FICR->TEMP.A1;
  63. NRF_TEMP->A2 = NRF_FICR->TEMP.A2;
  64. NRF_TEMP->A3 = NRF_FICR->TEMP.A3;
  65. NRF_TEMP->A4 = NRF_FICR->TEMP.A4;
  66. NRF_TEMP->A5 = NRF_FICR->TEMP.A5;
  67. NRF_TEMP->B0 = NRF_FICR->TEMP.B0;
  68. NRF_TEMP->B1 = NRF_FICR->TEMP.B1;
  69. NRF_TEMP->B2 = NRF_FICR->TEMP.B2;
  70. NRF_TEMP->B3 = NRF_FICR->TEMP.B3;
  71. NRF_TEMP->B4 = NRF_FICR->TEMP.B4;
  72. NRF_TEMP->B5 = NRF_FICR->TEMP.B5;
  73. NRF_TEMP->T0 = NRF_FICR->TEMP.T0;
  74. NRF_TEMP->T1 = NRF_FICR->TEMP.T1;
  75. NRF_TEMP->T2 = NRF_FICR->TEMP.T2;
  76. NRF_TEMP->T3 = NRF_FICR->TEMP.T3;
  77. NRF_TEMP->T4 = NRF_FICR->TEMP.T4;
  78. }
  79. #ifdef DEVELOP_IN_NRF52840
  80. /* Workaround for Errata 103 "CCM: Wrong reset value of CCM MAXPACKETSIZE" found at the Errata document
  81. for your device located at https://www.nordicsemi.com/DocLib */
  82. if (errata_103()){
  83. NRF_CCM->MAXPACKETSIZE = 0xFBul;
  84. }
  85. /* 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
  86. for your device located at https://www.nordicsemi.com/DocLib */
  87. if (errata_115()){
  88. *(volatile uint32_t *)0x40000EE4 = (*(volatile uint32_t *)0x40000EE4 & 0xFFFFFFF0) | (*(uint32_t *)0x10000258 & 0x0000000F);
  89. }
  90. #endif
  91. /* 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
  92. for your device located at https://www.nordicsemi.com/DocLib */
  93. if (errata_108()){
  94. *(volatile uint32_t *)0x40000EE4 = *(volatile uint32_t *)0x10000258 & 0x0000004F;
  95. }
  96. /* Workaround for Errata 136 "System: Bits in RESETREAS are set when they should not be" found at the Errata document
  97. for your device located at https://www.nordicsemi.com/DocLib */
  98. if (errata_136()){
  99. if (NRF_POWER->RESETREAS & POWER_RESETREAS_RESETPIN_Msk){
  100. NRF_POWER->RESETREAS = ~POWER_RESETREAS_RESETPIN_Msk;
  101. }
  102. }
  103. /* Configure GPIO pads as pPin Reset pin if Pin Reset capabilities desired. If CONFIG_GPIO_AS_PINRESET is not
  104. defined, pin reset will not be available. One GPIO (see Product Specification to see which one) will then be
  105. reserved for PinReset and not available as normal GPIO. */
  106. #if defined (CONFIG_GPIO_AS_PINRESET)
  107. #ifdef DEVELOP_IN_NRF52840
  108. #define RESET_PIN 18
  109. #else
  110. #define RESET_PIN 21
  111. #endif
  112. if (((NRF_UICR->PSELRESET[0] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos)) ||
  113. ((NRF_UICR->PSELRESET[1] & UICR_PSELRESET_CONNECT_Msk) != (UICR_PSELRESET_CONNECT_Connected << UICR_PSELRESET_CONNECT_Pos))){
  114. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
  115. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  116. NRF_UICR->PSELRESET[0] = RESET_PIN;
  117. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  118. NRF_UICR->PSELRESET[1] = RESET_PIN;
  119. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  120. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
  121. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  122. NVIC_SystemReset();
  123. }
  124. #endif
  125. SystemCoreClockUpdate();
  126. }
  127. static bool errata_31(void)
  128. {
  129. if (*(uint32_t *)0x10000130ul == 0xEul){
  130. if (*(uint32_t *)0x10000134ul == 0x0ul){
  131. return true;
  132. }
  133. }
  134. /* Apply by default for unknown devices until errata is confirmed fixed. */
  135. return true;
  136. }
  137. static bool errata_36(void)
  138. {
  139. if (*(uint32_t *)0x10000130ul == 0xEul){
  140. if (*(uint32_t *)0x10000134ul == 0x0ul){
  141. return true;
  142. }
  143. }
  144. #ifdef DEVELOP_IN_NRF52840
  145. if (*(uint32_t *)0x10000130ul == 0x8ul){
  146. if (*(uint32_t *)0x10000134ul == 0x0ul){
  147. return true;
  148. }
  149. if (*(uint32_t *)0x10000134ul == 0x1ul){
  150. return true;
  151. }
  152. if (*(uint32_t *)0x10000134ul == 0x2ul){
  153. return true;
  154. }
  155. if (*(uint32_t *)0x10000134ul == 0x3ul){
  156. return true;
  157. }
  158. }
  159. #endif
  160. /* Apply by default for unknown devices until errata is confirmed fixed. */
  161. return true;
  162. }
  163. static bool errata_66(void)
  164. {
  165. if (*(uint32_t *)0x10000130ul == 0xEul){
  166. if (*(uint32_t *)0x10000134ul == 0x0ul){
  167. return true;
  168. }
  169. }
  170. #ifdef DEVELOP_IN_NRF52840
  171. if (*(uint32_t *)0x10000130ul == 0x8ul){
  172. if (*(uint32_t *)0x10000134ul == 0x0ul){
  173. return true;
  174. }
  175. if (*(uint32_t *)0x10000134ul == 0x1ul){
  176. return true;
  177. }
  178. if (*(uint32_t *)0x10000134ul == 0x2ul){
  179. return true;
  180. }
  181. if (*(uint32_t *)0x10000134ul == 0x3ul){
  182. return true;
  183. }
  184. }
  185. #endif
  186. /* Apply by default for unknown devices until errata is confirmed fixed. */
  187. return true;
  188. }
  189. static bool errata_108(void)
  190. {
  191. if (*(uint32_t *)0x10000130ul == 0xEul){
  192. if (*(uint32_t *)0x10000134ul == 0x0ul){
  193. return true;
  194. }
  195. }
  196. /* Apply by default for unknown devices until errata is confirmed fixed. */
  197. return true;
  198. }
  199. static bool errata_136(void)
  200. {
  201. if (*(uint32_t *)0x10000130ul == 0xEul){
  202. if (*(uint32_t *)0x10000134ul == 0x0ul){
  203. return true;
  204. }
  205. }
  206. #ifdef DEVELOP_IN_NRF52840
  207. if (*(uint32_t *)0x10000130ul == 0x8ul){
  208. if (*(uint32_t *)0x10000134ul == 0x0ul){
  209. return true;
  210. }
  211. if (*(uint32_t *)0x10000134ul == 0x1ul){
  212. return true;
  213. }
  214. if (*(uint32_t *)0x10000134ul == 0x2ul){
  215. return true;
  216. }
  217. if (*(uint32_t *)0x10000134ul == 0x3ul){
  218. return true;
  219. }
  220. }
  221. #endif
  222. /* Apply by default for unknown devices until errata is confirmed fixed. */
  223. return true;
  224. }
  225. #ifdef DEVELOP_IN_NRF52840
  226. static bool errata_103(void)
  227. {
  228. if (*(uint32_t *)0x10000130ul == 0x8ul){
  229. if (*(uint32_t *)0x10000134ul == 0x0ul){
  230. return true;
  231. }
  232. }
  233. return false;
  234. }
  235. static bool errata_115(void)
  236. {
  237. if (*(uint32_t *)0x10000130ul == 0x8ul){
  238. if (*(uint32_t *)0x10000134ul == 0x0ul){
  239. return true;
  240. }
  241. }
  242. return false;
  243. }
  244. #endif
  245. /*lint --flb "Leave library region" */