mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Fri Feb 07 18:00:11 2014 +0000
Revision:
85:e1a8e879a6a9
Child:
104:a6a92e2e5a92
Synchronized with git revision 4b2b368a6a3b1f0fd33d99917981c67436c4aebe

Full URL: https://github.com/mbedmicro/mbed/commit/4b2b368a6a3b1f0fd33d99917981c67436c4aebe/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 85:e1a8e879a6a9 1 /* mbed Microcontroller Library
mbed_official 85:e1a8e879a6a9 2
mbed_official 85:e1a8e879a6a9 3 * Copyright (c) 2006-2013 ARM Limited
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 85:e1a8e879a6a9 24
mbed_official 85:e1a8e879a6a9 25 #define __SYSTEM_CLOCK (16000000UL) //!< nRF51 devices use a fixed System Clock Frequency of 16MHz
mbed_official 85:e1a8e879a6a9 26
mbed_official 85:e1a8e879a6a9 27 static bool is_manual_peripheral_setup_needed(void);
mbed_official 85:e1a8e879a6a9 28 static bool is_disabled_in_debug_needed(void);
mbed_official 85:e1a8e879a6a9 29
mbed_official 85:e1a8e879a6a9 30
mbed_official 85:e1a8e879a6a9 31 #if defined ( __CC_ARM )
mbed_official 85:e1a8e879a6a9 32 uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK;
mbed_official 85:e1a8e879a6a9 33 #elif defined ( __ICCARM__ )
mbed_official 85:e1a8e879a6a9 34 __root uint32_t SystemCoreClock = __SYSTEM_CLOCK;
mbed_official 85:e1a8e879a6a9 35 #elif defined ( __GNUC__ )
mbed_official 85:e1a8e879a6a9 36 uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK;
mbed_official 85:e1a8e879a6a9 37 #endif
mbed_official 85:e1a8e879a6a9 38
mbed_official 85:e1a8e879a6a9 39
mbed_official 85:e1a8e879a6a9 40 void SystemCoreClockUpdate(void)
mbed_official 85:e1a8e879a6a9 41 {
mbed_official 85:e1a8e879a6a9 42 SystemCoreClock = __SYSTEM_CLOCK;
mbed_official 85:e1a8e879a6a9 43 }
mbed_official 85:e1a8e879a6a9 44
mbed_official 85:e1a8e879a6a9 45 void SystemInit(void)
mbed_official 85:e1a8e879a6a9 46 {
mbed_official 85:e1a8e879a6a9 47
mbed_official 85:e1a8e879a6a9 48 //Write the necessary UICR and FWID values if needed
mbed_official 85:e1a8e879a6a9 49 if (NRF_UICR->CLENR0 == 0xFFFFFFFF){
mbed_official 85:e1a8e879a6a9 50 NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos);
mbed_official 85:e1a8e879a6a9 51 while (NRF_NVMC->READY == NVMC_READY_READY_Busy){
mbed_official 85:e1a8e879a6a9 52 }
mbed_official 85:e1a8e879a6a9 53
mbed_official 85:e1a8e879a6a9 54 NRF_UICR->CLENR0 = 0x14000;
mbed_official 85:e1a8e879a6a9 55 while (NRF_NVMC->READY == NVMC_READY_READY_Busy){
mbed_official 85:e1a8e879a6a9 56 }
mbed_official 85:e1a8e879a6a9 57
mbed_official 85:e1a8e879a6a9 58 //write FWID (NRF_UICR->FWID is readonly)
mbed_official 85:e1a8e879a6a9 59 *(uint32_t *)0x10001010 = 0xFFFF0049;
mbed_official 85:e1a8e879a6a9 60
mbed_official 85:e1a8e879a6a9 61 while (NRF_NVMC->READY == NVMC_READY_READY_Busy){
mbed_official 85:e1a8e879a6a9 62 }
mbed_official 85:e1a8e879a6a9 63
mbed_official 85:e1a8e879a6a9 64 NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
mbed_official 85:e1a8e879a6a9 65 while (NRF_NVMC->READY == NVMC_READY_READY_Busy){
mbed_official 85:e1a8e879a6a9 66 }
mbed_official 85:e1a8e879a6a9 67
mbed_official 85:e1a8e879a6a9 68 NVIC_SystemReset();
mbed_official 85:e1a8e879a6a9 69 while (true){
mbed_official 85:e1a8e879a6a9 70 }
mbed_official 85:e1a8e879a6a9 71 }
mbed_official 85:e1a8e879a6a9 72
mbed_official 85:e1a8e879a6a9 73 // Prepare the peripherals for use as indicated by the PAN 26 "System: Manual setup is required
mbed_official 85:e1a8e879a6a9 74 // to enable the use of peripherals" found at Product Anomaly document for your device found at
mbed_official 85:e1a8e879a6a9 75 // https://www.nordicsemi.com/. The side effect of executing these instructions in the devices
mbed_official 85:e1a8e879a6a9 76 // that do not need it is that the new peripherals in the second generation devices (LPCOMP for
mbed_official 85:e1a8e879a6a9 77 // example) will not be available.
mbed_official 85:e1a8e879a6a9 78
mbed_official 85:e1a8e879a6a9 79 if (is_manual_peripheral_setup_needed()){
mbed_official 85:e1a8e879a6a9 80 *(uint32_t volatile *)0x40000504 = 0xC007FFDF;
mbed_official 85:e1a8e879a6a9 81 *(uint32_t volatile *)0x40006C18 = 0x00008000;
mbed_official 85:e1a8e879a6a9 82 }
mbed_official 85:e1a8e879a6a9 83
mbed_official 85:e1a8e879a6a9 84 // Disable PROTENSET registers under debug, as indicated by PAN 59 "MPU: Reset value of DISABLEINDEBUG
mbed_official 85:e1a8e879a6a9 85 // register is incorrect" found at Product Anomaly document four your device found at
mbed_official 85:e1a8e879a6a9 86 // https://www.nordicsemi.com/. There is no side effect of using these instruction if not needed.
mbed_official 85:e1a8e879a6a9 87 if (is_disabled_in_debug_needed()){
mbed_official 85:e1a8e879a6a9 88 NRF_MPU->DISABLEINDEBUG = MPU_DISABLEINDEBUG_DISABLEINDEBUG_Disabled << MPU_DISABLEINDEBUG_DISABLEINDEBUG_Pos;
mbed_official 85:e1a8e879a6a9 89 }
mbed_official 85:e1a8e879a6a9 90
mbed_official 85:e1a8e879a6a9 91 // Start 16 MHz crystal oscillator.
mbed_official 85:e1a8e879a6a9 92 NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
mbed_official 85:e1a8e879a6a9 93 NRF_CLOCK->TASKS_HFCLKSTART = 1;
mbed_official 85:e1a8e879a6a9 94
mbed_official 85:e1a8e879a6a9 95 // Wait for the external oscillator to start up.
mbed_official 85:e1a8e879a6a9 96 while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) {
mbed_official 85:e1a8e879a6a9 97 // Do nothing.
mbed_official 85:e1a8e879a6a9 98 }
mbed_official 85:e1a8e879a6a9 99 }
mbed_official 85:e1a8e879a6a9 100
mbed_official 85:e1a8e879a6a9 101 static bool is_manual_peripheral_setup_needed(void)
mbed_official 85:e1a8e879a6a9 102 {
mbed_official 85:e1a8e879a6a9 103 if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x1) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0))
mbed_official 85:e1a8e879a6a9 104 {
mbed_official 85:e1a8e879a6a9 105 if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x00) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0))
mbed_official 85:e1a8e879a6a9 106 {
mbed_official 85:e1a8e879a6a9 107 return true;
mbed_official 85:e1a8e879a6a9 108 }
mbed_official 85:e1a8e879a6a9 109 if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x10) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0))
mbed_official 85:e1a8e879a6a9 110 {
mbed_official 85:e1a8e879a6a9 111 return true;
mbed_official 85:e1a8e879a6a9 112 }
mbed_official 85:e1a8e879a6a9 113 if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0))
mbed_official 85:e1a8e879a6a9 114 {
mbed_official 85:e1a8e879a6a9 115 return true;
mbed_official 85:e1a8e879a6a9 116 }
mbed_official 85:e1a8e879a6a9 117 }
mbed_official 85:e1a8e879a6a9 118
mbed_official 85:e1a8e879a6a9 119 return false;
mbed_official 85:e1a8e879a6a9 120 }
mbed_official 85:e1a8e879a6a9 121
mbed_official 85:e1a8e879a6a9 122 static bool is_disabled_in_debug_needed(void)
mbed_official 85:e1a8e879a6a9 123 {
mbed_official 85:e1a8e879a6a9 124 if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x1) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0))
mbed_official 85:e1a8e879a6a9 125 {
mbed_official 85:e1a8e879a6a9 126 if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x40) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0))
mbed_official 85:e1a8e879a6a9 127 {
mbed_official 85:e1a8e879a6a9 128 return true;
mbed_official 85:e1a8e879a6a9 129 }
mbed_official 85:e1a8e879a6a9 130 }
mbed_official 85:e1a8e879a6a9 131
mbed_official 85:e1a8e879a6a9 132 return false;
mbed_official 85:e1a8e879a6a9 133 }
mbed_official 85:e1a8e879a6a9 134