Test code for Grove Node BLE

Dependencies:   BLE_API nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Committer:
yihui
Date:
Thu Nov 27 09:30:36 2014 +0000
Revision:
10:22480ac31879
Parent:
9:05f0b5a3a70a
change to new revision hardware

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 9:05f0b5a3a70a 1 /* mbed Microcontroller Library
yihui 9:05f0b5a3a70a 2
yihui 9:05f0b5a3a70a 3 * Copyright (c) 2013 Nordic Semiconductor.
yihui 9:05f0b5a3a70a 4 *
yihui 9:05f0b5a3a70a 5 * Licensed under the Apache License, Version 2.0 (the "License");
yihui 9:05f0b5a3a70a 6 * you may not use this file except in compliance with the License.
yihui 9:05f0b5a3a70a 7 * You may obtain a copy of the License at
yihui 9:05f0b5a3a70a 8 *
yihui 9:05f0b5a3a70a 9 * http://www.apache.org/licenses/LICENSE-2.0
yihui 9:05f0b5a3a70a 10 *
yihui 9:05f0b5a3a70a 11 * Unless required by applicable law or agreed to in writing, software
yihui 9:05f0b5a3a70a 12 * distributed under the License is distributed on an "AS IS" BASIS,
yihui 9:05f0b5a3a70a 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
yihui 9:05f0b5a3a70a 14 * See the License for the specific language governing permissions and
yihui 9:05f0b5a3a70a 15 * limitations under the License.
yihui 9:05f0b5a3a70a 16 */
yihui 9:05f0b5a3a70a 17
yihui 9:05f0b5a3a70a 18
yihui 9:05f0b5a3a70a 19 #include <stdint.h>
yihui 9:05f0b5a3a70a 20 #include <stdbool.h>
yihui 9:05f0b5a3a70a 21 #include "nrf51822.h"
yihui 9:05f0b5a3a70a 22 #include "system_nrf51822.h"
yihui 9:05f0b5a3a70a 23
yihui 9:05f0b5a3a70a 24
yihui 9:05f0b5a3a70a 25 #define __SYSTEM_CLOCK (16000000UL) /*!< nRF51 devices use a fixed System Clock Frequency of 16MHz */
yihui 9:05f0b5a3a70a 26
yihui 9:05f0b5a3a70a 27 static bool is_manual_peripheral_setup_needed(void);
yihui 9:05f0b5a3a70a 28 static bool is_disabled_in_debug_needed(void);
yihui 9:05f0b5a3a70a 29
yihui 9:05f0b5a3a70a 30
yihui 9:05f0b5a3a70a 31 #if defined ( __CC_ARM )
yihui 9:05f0b5a3a70a 32 uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK;
yihui 9:05f0b5a3a70a 33 #elif defined ( __ICCARM__ )
yihui 9:05f0b5a3a70a 34 __root uint32_t SystemCoreClock = __SYSTEM_CLOCK;
yihui 9:05f0b5a3a70a 35 #elif defined ( __GNUC__ )
yihui 9:05f0b5a3a70a 36 uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK;
yihui 9:05f0b5a3a70a 37 #endif
yihui 9:05f0b5a3a70a 38
yihui 9:05f0b5a3a70a 39 void SystemCoreClockUpdate(void)
yihui 9:05f0b5a3a70a 40 {
yihui 9:05f0b5a3a70a 41 SystemCoreClock = __SYSTEM_CLOCK;
yihui 9:05f0b5a3a70a 42 }
yihui 9:05f0b5a3a70a 43
yihui 9:05f0b5a3a70a 44 void SystemInit(void)
yihui 9:05f0b5a3a70a 45 {
yihui 9:05f0b5a3a70a 46 /* If desired, switch off the unused RAM to lower consumption by the use of RAMON register.
yihui 9:05f0b5a3a70a 47 It can also be done in the application main() function. */
yihui 9:05f0b5a3a70a 48
yihui 9:05f0b5a3a70a 49 // Prepare the peripherals for use as indicated by the PAN 26 "System: Manual setup is required
yihui 9:05f0b5a3a70a 50 // to enable the use of peripherals" found at Product Anomaly document for your device found at
yihui 9:05f0b5a3a70a 51 // https://www.nordicsemi.com/. The side effect of executing these instructions in the devices
yihui 9:05f0b5a3a70a 52 // that do not need it is that the new peripherals in the second generation devices (LPCOMP for
yihui 9:05f0b5a3a70a 53 // example) will not be available.
yihui 9:05f0b5a3a70a 54 if (is_manual_peripheral_setup_needed())
yihui 9:05f0b5a3a70a 55 {
yihui 9:05f0b5a3a70a 56 *(uint32_t volatile *)0x40000504 = 0xC007FFDF;
yihui 9:05f0b5a3a70a 57 *(uint32_t volatile *)0x40006C18 = 0x00008000;
yihui 9:05f0b5a3a70a 58 }
yihui 9:05f0b5a3a70a 59
yihui 9:05f0b5a3a70a 60 // Disable PROTENSET registers under debug, as indicated by PAN 59 "MPU: Reset value of DISABLEINDEBUG
yihui 9:05f0b5a3a70a 61 // register is incorrect" found at Product Anomaly document four your device found at
yihui 9:05f0b5a3a70a 62 // https://www.nordicsemi.com/. There is no side effect of using these instruction if not needed.
yihui 9:05f0b5a3a70a 63 if (is_disabled_in_debug_needed())
yihui 9:05f0b5a3a70a 64 {
yihui 9:05f0b5a3a70a 65 NRF_MPU->DISABLEINDEBUG = MPU_DISABLEINDEBUG_DISABLEINDEBUG_Disabled << MPU_DISABLEINDEBUG_DISABLEINDEBUG_Pos;
yihui 9:05f0b5a3a70a 66 }
yihui 9:05f0b5a3a70a 67
yihui 9:05f0b5a3a70a 68 // Start the external 32khz crystal oscillator.
yihui 9:05f0b5a3a70a 69
yihui 9:05f0b5a3a70a 70 #ifdef TARGET_HRM1017
yihui 9:05f0b5a3a70a 71 NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos);
yihui 9:05f0b5a3a70a 72 #else
yihui 9:05f0b5a3a70a 73 NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos);
yihui 9:05f0b5a3a70a 74 #endif
yihui 9:05f0b5a3a70a 75 NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
yihui 9:05f0b5a3a70a 76 NRF_CLOCK->TASKS_LFCLKSTART = 1;
yihui 9:05f0b5a3a70a 77
yihui 9:05f0b5a3a70a 78 // Wait for the external oscillator to start up.
yihui 9:05f0b5a3a70a 79 while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0) {
yihui 9:05f0b5a3a70a 80 // Do nothing.
yihui 9:05f0b5a3a70a 81 }
yihui 9:05f0b5a3a70a 82 }
yihui 9:05f0b5a3a70a 83
yihui 9:05f0b5a3a70a 84 static bool is_manual_peripheral_setup_needed(void)
yihui 9:05f0b5a3a70a 85 {
yihui 9:05f0b5a3a70a 86 if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x1) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0))
yihui 9:05f0b5a3a70a 87 {
yihui 9:05f0b5a3a70a 88 if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x00) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0))
yihui 9:05f0b5a3a70a 89 {
yihui 9:05f0b5a3a70a 90 return true;
yihui 9:05f0b5a3a70a 91 }
yihui 9:05f0b5a3a70a 92 if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x10) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0))
yihui 9:05f0b5a3a70a 93 {
yihui 9:05f0b5a3a70a 94 return true;
yihui 9:05f0b5a3a70a 95 }
yihui 9:05f0b5a3a70a 96 if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0))
yihui 9:05f0b5a3a70a 97 {
yihui 9:05f0b5a3a70a 98 return true;
yihui 9:05f0b5a3a70a 99 }
yihui 9:05f0b5a3a70a 100 }
yihui 9:05f0b5a3a70a 101
yihui 9:05f0b5a3a70a 102 return false;
yihui 9:05f0b5a3a70a 103 }
yihui 9:05f0b5a3a70a 104
yihui 9:05f0b5a3a70a 105 static bool is_disabled_in_debug_needed(void)
yihui 9:05f0b5a3a70a 106 {
yihui 9:05f0b5a3a70a 107 if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x1) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0))
yihui 9:05f0b5a3a70a 108 {
yihui 9:05f0b5a3a70a 109 if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x40) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0))
yihui 9:05f0b5a3a70a 110 {
yihui 9:05f0b5a3a70a 111 return true;
yihui 9:05f0b5a3a70a 112 }
yihui 9:05f0b5a3a70a 113 }
yihui 9:05f0b5a3a70a 114
yihui 9:05f0b5a3a70a 115 return false;
yihui 9:05f0b5a3a70a 116 }
yihui 9:05f0b5a3a70a 117