system_nrf51.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_nrf51.h"
  21. /*lint ++flb "Enter library region" */
  22. #define __SYSTEM_CLOCK (16000000UL) /*!< nRF51 devices use a fixed System Clock Frequency of 16MHz */
  23. static bool is_manual_peripheral_setup_needed(void);
  24. static bool is_disabled_in_debug_needed(void);
  25. static bool is_peripheral_domain_setup_needed(void);
  26. #if defined ( __CC_ARM )
  27. uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK;
  28. #elif defined ( __ICCARM__ )
  29. __root uint32_t SystemCoreClock = __SYSTEM_CLOCK;
  30. #elif defined ( __GNUC__ )
  31. uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK;
  32. #endif
  33. void SystemCoreClockUpdate(void)
  34. {
  35. SystemCoreClock = __SYSTEM_CLOCK;
  36. }
  37. void SystemInit(void)
  38. {
  39. /* If desired, switch off the unused RAM to lower consumption by the use of RAMON register.
  40. It can also be done in the application main() function. */
  41. /* Prepare the peripherals for use as indicated by the PAN 26 "System: Manual setup is required
  42. to enable the use of peripherals" found at Product Anomaly document for your device found at
  43. https://www.nordicsemi.com/DocLib The side effect of executing these instructions in the devices
  44. that do not need it is that the new peripherals in the second generation devices (LPCOMP for
  45. example) will not be available. */
  46. if (is_manual_peripheral_setup_needed())
  47. {
  48. *(uint32_t volatile *)0x40000504 = 0xC007FFDF;
  49. *(uint32_t volatile *)0x40006C18 = 0x00008000;
  50. }
  51. /* Disable PROTENSET registers under debug, as indicated by PAN 59 "MPU: Reset value of DISABLEINDEBUG
  52. register is incorrect" found at Product Anomaly document for your device found at
  53. https://www.nordicsemi.com/DocLib There is no side effect of using these instruction if not needed. */
  54. if (is_disabled_in_debug_needed())
  55. {
  56. NRF_MPU->DISABLEINDEBUG = MPU_DISABLEINDEBUG_DISABLEINDEBUG_Disabled << MPU_DISABLEINDEBUG_DISABLEINDEBUG_Pos;
  57. }
  58. /* Execute the following code to eliminate excessive current in sleep mode with RAM retention in nRF51802 devices,
  59. as indicated by PAN 76 "System: Excessive current in sleep mode with retention" found at Product Anomaly document
  60. for your device found at https://www.nordicsemi.com/DocLib */
  61. if (is_peripheral_domain_setup_needed()){
  62. if (*(uint32_t volatile *)0x4006EC00 != 1){
  63. *(uint32_t volatile *)0x4006EC00 = 0x9375;
  64. while (*(uint32_t volatile *)0x4006EC00 != 1){
  65. }
  66. }
  67. *(uint32_t volatile *)0x4006EC14 = 0xC0;
  68. }
  69. }
  70. static bool is_manual_peripheral_setup_needed(void)
  71. {
  72. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x1) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0))
  73. {
  74. if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x00) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0))
  75. {
  76. return true;
  77. }
  78. if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x10) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0))
  79. {
  80. return true;
  81. }
  82. if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0))
  83. {
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. static bool is_disabled_in_debug_needed(void)
  90. {
  91. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x1) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0))
  92. {
  93. if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x40) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0))
  94. {
  95. return true;
  96. }
  97. }
  98. return false;
  99. }
  100. static bool is_peripheral_domain_setup_needed(void)
  101. {
  102. if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x1) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0))
  103. {
  104. if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0xA0) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0))
  105. {
  106. return true;
  107. }
  108. if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0xD0) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0))
  109. {
  110. return true;
  111. }
  112. }
  113. return false;
  114. }
  115. /*lint --flb "Leave library region" */