Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-stm32l0/l1-src by
targets/cmsis/TARGET_NORDIC/TARGET_MCU_NRF51822/system_nrf51822.c@472:7bbab527289d, 2015-02-12 (annotated)
- Committer:
 - mbed_official
 - Date:
 - Thu Feb 12 13:00:07 2015 +0000
 - Revision:
 - 472:7bbab527289d
 - Parent:
 - 466:12a9a5f8fea0
 
Synchronized with git revision 4a7b7d067deeff485150756fa782d3952e160017
Full URL: https://github.com/mbedmicro/mbed/commit/4a7b7d067deeff485150756fa782d3952e160017/
Modify 32MHz crystal setting for NRF51822, overrides file for DFCM-NNN40 target
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| mbed_official | 85:e1a8e879a6a9 | 1 | /* mbed Microcontroller Library | 
| mbed_official | 85:e1a8e879a6a9 | 2 | |
| mbed_official | 459:397407b8d9f7 | 3 | * Copyright (c) 2015 Nordic Semiconductor. | 
| mbed_official | 85:e1a8e879a6a9 | 4 | * | 
| mbed_official | 85:e1a8e879a6a9 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
| mbed_official | 85:e1a8e879a6a9 | 6 | * you may not use this file except in compliance with the License. | 
| mbed_official | 85:e1a8e879a6a9 | 7 | * You may obtain a copy of the License at | 
| mbed_official | 85:e1a8e879a6a9 | 8 | * | 
| mbed_official | 85:e1a8e879a6a9 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 | 
| mbed_official | 85:e1a8e879a6a9 | 10 | * | 
| mbed_official | 85:e1a8e879a6a9 | 11 | * Unless required by applicable law or agreed to in writing, software | 
| mbed_official | 85:e1a8e879a6a9 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, | 
| mbed_official | 85:e1a8e879a6a9 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| mbed_official | 85:e1a8e879a6a9 | 14 | * See the License for the specific language governing permissions and | 
| mbed_official | 85:e1a8e879a6a9 | 15 | * limitations under the License. | 
| mbed_official | 85:e1a8e879a6a9 | 16 | */ | 
| mbed_official | 85:e1a8e879a6a9 | 17 | |
| mbed_official | 85:e1a8e879a6a9 | 18 | |
| mbed_official | 85:e1a8e879a6a9 | 19 | #include <stdint.h> | 
| mbed_official | 85:e1a8e879a6a9 | 20 | #include <stdbool.h> | 
| mbed_official | 85:e1a8e879a6a9 | 21 | #include "nrf51822.h" | 
| mbed_official | 85:e1a8e879a6a9 | 22 | #include "system_nrf51822.h" | 
| mbed_official | 85:e1a8e879a6a9 | 23 | |
| mbed_official | 472:7bbab527289d | 24 | #define __SYSTEM_CLOCK (16000000UL) /*!< nRF51 devices use a fixed System Clock Frequency of 16MHz */ | 
| mbed_official | 85:e1a8e879a6a9 | 25 | |
| mbed_official | 85:e1a8e879a6a9 | 26 | static bool is_manual_peripheral_setup_needed(void); | 
| mbed_official | 85:e1a8e879a6a9 | 27 | static bool is_disabled_in_debug_needed(void); | 
| mbed_official | 85:e1a8e879a6a9 | 28 | |
| mbed_official | 85:e1a8e879a6a9 | 29 | |
| mbed_official | 85:e1a8e879a6a9 | 30 | #if defined ( __CC_ARM ) | 
| mbed_official | 304:89b9c3a9a045 | 31 | uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK; | 
| mbed_official | 85:e1a8e879a6a9 | 32 | #elif defined ( __ICCARM__ ) | 
| mbed_official | 85:e1a8e879a6a9 | 33 | __root uint32_t SystemCoreClock = __SYSTEM_CLOCK; | 
| mbed_official | 85:e1a8e879a6a9 | 34 | #elif defined ( __GNUC__ ) | 
| mbed_official | 85:e1a8e879a6a9 | 35 | uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK; | 
| mbed_official | 85:e1a8e879a6a9 | 36 | #endif | 
| mbed_official | 85:e1a8e879a6a9 | 37 | |
| mbed_official | 85:e1a8e879a6a9 | 38 | void SystemCoreClockUpdate(void) | 
| mbed_official | 85:e1a8e879a6a9 | 39 | { | 
| mbed_official | 85:e1a8e879a6a9 | 40 | SystemCoreClock = __SYSTEM_CLOCK; | 
| mbed_official | 85:e1a8e879a6a9 | 41 | } | 
| mbed_official | 85:e1a8e879a6a9 | 42 | |
| mbed_official | 85:e1a8e879a6a9 | 43 | void SystemInit(void) | 
| mbed_official | 304:89b9c3a9a045 | 44 | { | 
| mbed_official | 306:d33324c3b5a2 | 45 | /* If desired, switch off the unused RAM to lower consumption by the use of RAMON register. | 
| mbed_official | 306:d33324c3b5a2 | 46 | It can also be done in the application main() function. */ | 
| mbed_official | 306:d33324c3b5a2 | 47 | |
| mbed_official | 85:e1a8e879a6a9 | 48 | // Prepare the peripherals for use as indicated by the PAN 26 "System: Manual setup is required | 
| mbed_official | 85:e1a8e879a6a9 | 49 | // to enable the use of peripherals" found at Product Anomaly document for your device found at | 
| mbed_official | 304:89b9c3a9a045 | 50 | // https://www.nordicsemi.com/. The side effect of executing these instructions in the devices | 
| mbed_official | 85:e1a8e879a6a9 | 51 | // that do not need it is that the new peripherals in the second generation devices (LPCOMP for | 
| mbed_official | 85:e1a8e879a6a9 | 52 | // example) will not be available. | 
| mbed_official | 306:d33324c3b5a2 | 53 | if (is_manual_peripheral_setup_needed()) | 
| mbed_official | 306:d33324c3b5a2 | 54 | { | 
| mbed_official | 85:e1a8e879a6a9 | 55 | *(uint32_t volatile *)0x40000504 = 0xC007FFDF; | 
| mbed_official | 85:e1a8e879a6a9 | 56 | *(uint32_t volatile *)0x40006C18 = 0x00008000; | 
| mbed_official | 85:e1a8e879a6a9 | 57 | } | 
| mbed_official | 304:89b9c3a9a045 | 58 | |
| mbed_official | 85:e1a8e879a6a9 | 59 | // Disable PROTENSET registers under debug, as indicated by PAN 59 "MPU: Reset value of DISABLEINDEBUG | 
| mbed_official | 304:89b9c3a9a045 | 60 | // register is incorrect" found at Product Anomaly document four your device found at | 
| mbed_official | 304:89b9c3a9a045 | 61 | // https://www.nordicsemi.com/. There is no side effect of using these instruction if not needed. | 
| mbed_official | 306:d33324c3b5a2 | 62 | if (is_disabled_in_debug_needed()) | 
| mbed_official | 306:d33324c3b5a2 | 63 | { | 
| mbed_official | 85:e1a8e879a6a9 | 64 | NRF_MPU->DISABLEINDEBUG = MPU_DISABLEINDEBUG_DISABLEINDEBUG_Disabled << MPU_DISABLEINDEBUG_DISABLEINDEBUG_Pos; | 
| mbed_official | 85:e1a8e879a6a9 | 65 | } | 
| mbed_official | 304:89b9c3a9a045 | 66 | |
| mbed_official | 304:89b9c3a9a045 | 67 | // Start the external 32khz crystal oscillator. | 
| mbed_official | 308:29bf8b44dbf6 | 68 | |
| mbed_official | 466:12a9a5f8fea0 | 69 | #if defined(TARGET_DELTA_DFCM_NNN40) || defined(TARGET_HRM1017) | 
| mbed_official | 308:29bf8b44dbf6 | 70 | NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos); | 
| mbed_official | 308:29bf8b44dbf6 | 71 | #else | 
| mbed_official | 304:89b9c3a9a045 | 72 | NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos); | 
| mbed_official | 308:29bf8b44dbf6 | 73 | #endif | 
| mbed_official | 304:89b9c3a9a045 | 74 | NRF_CLOCK->EVENTS_LFCLKSTARTED = 0; | 
| mbed_official | 304:89b9c3a9a045 | 75 | NRF_CLOCK->TASKS_LFCLKSTART = 1; | 
| mbed_official | 85:e1a8e879a6a9 | 76 | |
| mbed_official | 85:e1a8e879a6a9 | 77 | // Wait for the external oscillator to start up. | 
| mbed_official | 304:89b9c3a9a045 | 78 | while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0) { | 
| mbed_official | 85:e1a8e879a6a9 | 79 | // Do nothing. | 
| mbed_official | 85:e1a8e879a6a9 | 80 | } | 
| mbed_official | 85:e1a8e879a6a9 | 81 | } | 
| mbed_official | 85:e1a8e879a6a9 | 82 | |
| mbed_official | 304:89b9c3a9a045 | 83 | static bool is_manual_peripheral_setup_needed(void) | 
| mbed_official | 85:e1a8e879a6a9 | 84 | { | 
| mbed_official | 85:e1a8e879a6a9 | 85 | if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x1) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)) | 
| mbed_official | 85:e1a8e879a6a9 | 86 | { | 
| mbed_official | 85:e1a8e879a6a9 | 87 | if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x00) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0)) | 
| mbed_official | 85:e1a8e879a6a9 | 88 | { | 
| mbed_official | 85:e1a8e879a6a9 | 89 | return true; | 
| mbed_official | 85:e1a8e879a6a9 | 90 | } | 
| mbed_official | 85:e1a8e879a6a9 | 91 | if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x10) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0)) | 
| mbed_official | 85:e1a8e879a6a9 | 92 | { | 
| mbed_official | 85:e1a8e879a6a9 | 93 | return true; | 
| mbed_official | 85:e1a8e879a6a9 | 94 | } | 
| mbed_official | 85:e1a8e879a6a9 | 95 | if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0)) | 
| mbed_official | 85:e1a8e879a6a9 | 96 | { | 
| mbed_official | 85:e1a8e879a6a9 | 97 | return true; | 
| mbed_official | 85:e1a8e879a6a9 | 98 | } | 
| mbed_official | 85:e1a8e879a6a9 | 99 | } | 
| mbed_official | 304:89b9c3a9a045 | 100 | |
| mbed_official | 85:e1a8e879a6a9 | 101 | return false; | 
| mbed_official | 85:e1a8e879a6a9 | 102 | } | 
| mbed_official | 85:e1a8e879a6a9 | 103 | |
| mbed_official | 304:89b9c3a9a045 | 104 | static bool is_disabled_in_debug_needed(void) | 
| mbed_official | 85:e1a8e879a6a9 | 105 | { | 
| mbed_official | 85:e1a8e879a6a9 | 106 | if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x1) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)) | 
| mbed_official | 85:e1a8e879a6a9 | 107 | { | 
| mbed_official | 85:e1a8e879a6a9 | 108 | if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x40) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0)) | 
| mbed_official | 85:e1a8e879a6a9 | 109 | { | 
| mbed_official | 85:e1a8e879a6a9 | 110 | return true; | 
| mbed_official | 85:e1a8e879a6a9 | 111 | } | 
| mbed_official | 85:e1a8e879a6a9 | 112 | } | 
| mbed_official | 304:89b9c3a9a045 | 113 | |
| mbed_official | 85:e1a8e879a6a9 | 114 | return false; | 
| mbed_official | 85:e1a8e879a6a9 | 115 | } | 
| mbed_official | 85:e1a8e879a6a9 | 116 | 
