]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_NORDIC/TARGET_MCU_NRF51822/system_nrf51.c
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_NORDIC / TARGET_MCU_NRF51822 / system_nrf51.c
1 /* Copyright (c) 2013, Nordic Semiconductor ASA
2 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * * Neither the name of Nordic Semiconductor ASA nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 */
30
31 /* NOTE: Template files (including this one) are application specific and therefore expected to
32 be copied into the application project folder prior to its use! */
33
34 #include <stdint.h>
35 #include <stdbool.h>
36 #include "nrf.h"
37 #include "system_nrf51.h"
38
39 /*lint ++flb "Enter library region" */
40
41 #define __SYSTEM_CLOCK (16000000UL) /*!< nRF51 devices use a fixed System Clock Frequency of 16MHz */
42
43 static bool is_manual_peripheral_setup_needed(void);
44 static bool is_disabled_in_debug_needed(void);
45
46
47 #if defined ( __CC_ARM )
48 uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK;
49 #elif defined ( __ICCARM__ )
50 __root uint32_t SystemCoreClock = __SYSTEM_CLOCK;
51 #elif defined ( __GNUC__ )
52 uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK;
53 #endif
54
55 void SystemCoreClockUpdate(void)
56 {
57 SystemCoreClock = __SYSTEM_CLOCK;
58 }
59
60 void SystemInit(void)
61 {
62 /* If desired, switch off the unused RAM to lower consumption by the use of RAMON register.
63 It can also be done in the application main() function. */
64
65 /* Prepare the peripherals for use as indicated by the PAN 26 "System: Manual setup is required
66 to enable the use of peripherals" found at Product Anomaly document for your device found at
67 https://www.nordicsemi.com/. The side effect of executing these instructions in the devices
68 that do not need it is that the new peripherals in the second generation devices (LPCOMP for
69 example) will not be available. */
70 if (is_manual_peripheral_setup_needed())
71 {
72 *(uint32_t volatile *)0x40000504 = 0xC007FFDF;
73 *(uint32_t volatile *)0x40006C18 = 0x00008000;
74 }
75
76 /* Disable PROTENSET registers under debug, as indicated by PAN 59 "MPU: Reset value of DISABLEINDEBUG
77 register is incorrect" found at Product Anomaly document four your device found at
78 https://www.nordicsemi.com/. There is no side effect of using these instruction if not needed. */
79 if (is_disabled_in_debug_needed())
80 {
81 NRF_MPU->DISABLEINDEBUG = MPU_DISABLEINDEBUG_DISABLEINDEBUG_Disabled << MPU_DISABLEINDEBUG_DISABLEINDEBUG_Pos;
82 }
83
84 // Start the external 32khz crystal oscillator.
85
86 #if defined(TARGET_DELTA_DFCM_NNN40) || defined(TARGET_HRM1017)
87 NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos);
88 #else
89 NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos);
90 #endif
91 NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
92 NRF_CLOCK->TASKS_LFCLKSTART = 1;
93
94 // Wait for the external oscillator to start up.
95 while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0) {
96 // Do nothing.
97 }
98 }
99
100 static bool is_manual_peripheral_setup_needed(void)
101 {
102 if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x1) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0))
103 {
104 if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x00) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0))
105 {
106 return true;
107 }
108 if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x10) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0))
109 {
110 return true;
111 }
112 if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0))
113 {
114 return true;
115 }
116 }
117
118 return false;
119 }
120
121 static bool is_disabled_in_debug_needed(void)
122 {
123 if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x1) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0))
124 {
125 if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x40) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0))
126 {
127 return true;
128 }
129 }
130
131 return false;
132 }
133
134 /*lint --flb "Leave library region" */
Imprint / Impressum