mbed library sources
Fork of mbed-src by
targets/cmsis/TARGET_NORDIC/TARGET_MCU_NRF51822/system_nrf51.c@637:ed69428d4850, 2015-12-22 (annotated)
- Committer:
- jaerts
- Date:
- Tue Dec 22 13:22:16 2015 +0000
- Revision:
- 637:ed69428d4850
- Parent:
- 620:49241b7c6da5
Add very shady LPC1768 CAN Filter implementation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 501:36015dec7d16 | 1 | /* Copyright (c) 2013, Nordic Semiconductor ASA |
mbed_official | 501:36015dec7d16 | 2 | * All rights reserved. |
mbed_official | 501:36015dec7d16 | 3 | * |
mbed_official | 501:36015dec7d16 | 4 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 501:36015dec7d16 | 5 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 501:36015dec7d16 | 6 | * |
mbed_official | 501:36015dec7d16 | 7 | * * Redistributions of source code must retain the above copyright notice, this |
mbed_official | 501:36015dec7d16 | 8 | * list of conditions and the following disclaimer. |
mbed_official | 501:36015dec7d16 | 9 | * |
mbed_official | 501:36015dec7d16 | 10 | * * Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 501:36015dec7d16 | 11 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 501:36015dec7d16 | 12 | * and/or other materials provided with the distribution. |
mbed_official | 501:36015dec7d16 | 13 | * |
mbed_official | 501:36015dec7d16 | 14 | * * Neither the name of Nordic Semiconductor ASA nor the names of its |
mbed_official | 501:36015dec7d16 | 15 | * contributors may be used to endorse or promote products derived from |
mbed_official | 501:36015dec7d16 | 16 | * this software without specific prior written permission. |
mbed_official | 501:36015dec7d16 | 17 | * |
mbed_official | 501:36015dec7d16 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 501:36015dec7d16 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 501:36015dec7d16 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 501:36015dec7d16 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 501:36015dec7d16 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 501:36015dec7d16 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 501:36015dec7d16 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 501:36015dec7d16 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 501:36015dec7d16 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 501:36015dec7d16 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 501:36015dec7d16 | 28 | * |
mbed_official | 501:36015dec7d16 | 29 | */ |
mbed_official | 501:36015dec7d16 | 30 | |
mbed_official | 501:36015dec7d16 | 31 | /* NOTE: Template files (including this one) are application specific and therefore expected to |
mbed_official | 501:36015dec7d16 | 32 | be copied into the application project folder prior to its use! */ |
mbed_official | 501:36015dec7d16 | 33 | |
mbed_official | 501:36015dec7d16 | 34 | #include <stdint.h> |
mbed_official | 501:36015dec7d16 | 35 | #include <stdbool.h> |
mbed_official | 501:36015dec7d16 | 36 | #include "nrf.h" |
mbed_official | 603:3c75ef011213 | 37 | #include "nrf_delay.h" |
mbed_official | 501:36015dec7d16 | 38 | #include "system_nrf51.h" |
mbed_official | 501:36015dec7d16 | 39 | |
mbed_official | 501:36015dec7d16 | 40 | /*lint ++flb "Enter library region" */ |
mbed_official | 501:36015dec7d16 | 41 | |
mbed_official | 501:36015dec7d16 | 42 | #define __SYSTEM_CLOCK (16000000UL) /*!< nRF51 devices use a fixed System Clock Frequency of 16MHz */ |
mbed_official | 501:36015dec7d16 | 43 | |
mbed_official | 501:36015dec7d16 | 44 | static bool is_manual_peripheral_setup_needed(void); |
mbed_official | 501:36015dec7d16 | 45 | static bool is_disabled_in_debug_needed(void); |
mbed_official | 603:3c75ef011213 | 46 | static void init_clock(void); |
mbed_official | 501:36015dec7d16 | 47 | |
mbed_official | 501:36015dec7d16 | 48 | |
mbed_official | 501:36015dec7d16 | 49 | #if defined ( __CC_ARM ) |
mbed_official | 501:36015dec7d16 | 50 | uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK; |
mbed_official | 501:36015dec7d16 | 51 | #elif defined ( __ICCARM__ ) |
mbed_official | 501:36015dec7d16 | 52 | __root uint32_t SystemCoreClock = __SYSTEM_CLOCK; |
mbed_official | 501:36015dec7d16 | 53 | #elif defined ( __GNUC__ ) |
mbed_official | 501:36015dec7d16 | 54 | uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK; |
mbed_official | 501:36015dec7d16 | 55 | #endif |
mbed_official | 501:36015dec7d16 | 56 | |
mbed_official | 501:36015dec7d16 | 57 | void SystemCoreClockUpdate(void) |
mbed_official | 501:36015dec7d16 | 58 | { |
mbed_official | 501:36015dec7d16 | 59 | SystemCoreClock = __SYSTEM_CLOCK; |
mbed_official | 501:36015dec7d16 | 60 | } |
mbed_official | 501:36015dec7d16 | 61 | |
mbed_official | 501:36015dec7d16 | 62 | void SystemInit(void) |
mbed_official | 501:36015dec7d16 | 63 | { |
mbed_official | 501:36015dec7d16 | 64 | /* If desired, switch off the unused RAM to lower consumption by the use of RAMON register. |
mbed_official | 501:36015dec7d16 | 65 | It can also be done in the application main() function. */ |
mbed_official | 501:36015dec7d16 | 66 | |
mbed_official | 501:36015dec7d16 | 67 | /* Prepare the peripherals for use as indicated by the PAN 26 "System: Manual setup is required |
mbed_official | 501:36015dec7d16 | 68 | to enable the use of peripherals" found at Product Anomaly document for your device found at |
mbed_official | 501:36015dec7d16 | 69 | https://www.nordicsemi.com/. The side effect of executing these instructions in the devices |
mbed_official | 501:36015dec7d16 | 70 | that do not need it is that the new peripherals in the second generation devices (LPCOMP for |
mbed_official | 501:36015dec7d16 | 71 | example) will not be available. */ |
mbed_official | 501:36015dec7d16 | 72 | if (is_manual_peripheral_setup_needed()) |
mbed_official | 501:36015dec7d16 | 73 | { |
mbed_official | 501:36015dec7d16 | 74 | *(uint32_t volatile *)0x40000504 = 0xC007FFDF; |
mbed_official | 501:36015dec7d16 | 75 | *(uint32_t volatile *)0x40006C18 = 0x00008000; |
mbed_official | 501:36015dec7d16 | 76 | } |
mbed_official | 501:36015dec7d16 | 77 | |
mbed_official | 501:36015dec7d16 | 78 | /* Disable PROTENSET registers under debug, as indicated by PAN 59 "MPU: Reset value of DISABLEINDEBUG |
mbed_official | 501:36015dec7d16 | 79 | register is incorrect" found at Product Anomaly document four your device found at |
mbed_official | 501:36015dec7d16 | 80 | https://www.nordicsemi.com/. There is no side effect of using these instruction if not needed. */ |
mbed_official | 501:36015dec7d16 | 81 | if (is_disabled_in_debug_needed()) |
mbed_official | 501:36015dec7d16 | 82 | { |
mbed_official | 501:36015dec7d16 | 83 | NRF_MPU->DISABLEINDEBUG = MPU_DISABLEINDEBUG_DISABLEINDEBUG_Disabled << MPU_DISABLEINDEBUG_DISABLEINDEBUG_Pos; |
mbed_official | 501:36015dec7d16 | 84 | } |
mbed_official | 501:36015dec7d16 | 85 | |
mbed_official | 501:36015dec7d16 | 86 | // Start the external 32khz crystal oscillator. |
mbed_official | 603:3c75ef011213 | 87 | init_clock(); |
mbed_official | 603:3c75ef011213 | 88 | } |
mbed_official | 501:36015dec7d16 | 89 | |
mbed_official | 603:3c75ef011213 | 90 | void init_clock(void) |
mbed_official | 603:3c75ef011213 | 91 | { |
mbed_official | 603:3c75ef011213 | 92 | /* For compatibility purpose, the default behaviour is to first attempt to initialise an |
mbed_official | 603:3c75ef011213 | 93 | external clock, and after a timeout, use the internal RC one. To avoid this wait, boards that |
mbed_official | 603:3c75ef011213 | 94 | don't have an external oscillator can set TARGET_NRF_LFCLK_RC directly. */ |
mbed_official | 620:49241b7c6da5 | 95 | uint32_t i = 0; |
mbed_official | 603:3c75ef011213 | 96 | const uint32_t polling_period = 200; |
mbed_official | 603:3c75ef011213 | 97 | const uint32_t timeout = 1000000; |
mbed_official | 603:3c75ef011213 | 98 | |
mbed_official | 598:2d5fc5624619 | 99 | #if defined(TARGET_NRF_LFCLK_RC) |
mbed_official | 501:36015dec7d16 | 100 | NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos); |
mbed_official | 501:36015dec7d16 | 101 | #else |
mbed_official | 501:36015dec7d16 | 102 | NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos); |
mbed_official | 501:36015dec7d16 | 103 | #endif |
mbed_official | 501:36015dec7d16 | 104 | NRF_CLOCK->EVENTS_LFCLKSTARTED = 0; |
mbed_official | 501:36015dec7d16 | 105 | NRF_CLOCK->TASKS_LFCLKSTART = 1; |
mbed_official | 501:36015dec7d16 | 106 | |
mbed_official | 603:3c75ef011213 | 107 | /* Wait for the external oscillator to start up. |
mbed_official | 603:3c75ef011213 | 108 | nRF51822 product specification (8.1.5) gives a typical value of 300ms for external clock |
mbed_official | 603:3c75ef011213 | 109 | startup duration, and a maximum value of 1s. When using the internal RC source, typical delay |
mbed_official | 603:3c75ef011213 | 110 | will be 390µs, so we use a polling period of 200µs. |
mbed_official | 603:3c75ef011213 | 111 | |
mbed_official | 603:3c75ef011213 | 112 | We can't use us_ticker at this point, so we have to rely on a less precise method for |
mbed_official | 603:3c75ef011213 | 113 | measuring our timeout. Because of this, the actual timeout will be slightly longer than 1 |
mbed_official | 603:3c75ef011213 | 114 | second, which isn't an issue at all, since this fallback should only be used as a safety net. |
mbed_official | 603:3c75ef011213 | 115 | */ |
mbed_official | 603:3c75ef011213 | 116 | for (i = 0; i < (timeout / polling_period); i++) { |
mbed_official | 603:3c75ef011213 | 117 | if (NRF_CLOCK->EVENTS_LFCLKSTARTED != 0) |
mbed_official | 603:3c75ef011213 | 118 | return; |
mbed_official | 603:3c75ef011213 | 119 | nrf_delay_us(polling_period); |
mbed_official | 603:3c75ef011213 | 120 | } |
mbed_official | 603:3c75ef011213 | 121 | |
mbed_official | 603:3c75ef011213 | 122 | /* Fallback to internal clock. Belt and braces, since the internal clock is used by default |
mbed_official | 603:3c75ef011213 | 123 | whilst no external source is running. This is not only a sanity check, but it also allows |
mbed_official | 603:3c75ef011213 | 124 | code down the road (e.g. ble initialisation) to directly know which clock is used. */ |
mbed_official | 603:3c75ef011213 | 125 | NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos); |
mbed_official | 603:3c75ef011213 | 126 | NRF_CLOCK->TASKS_LFCLKSTART = 1; |
mbed_official | 501:36015dec7d16 | 127 | while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0) { |
mbed_official | 501:36015dec7d16 | 128 | // Do nothing. |
mbed_official | 501:36015dec7d16 | 129 | } |
mbed_official | 501:36015dec7d16 | 130 | } |
mbed_official | 501:36015dec7d16 | 131 | |
mbed_official | 501:36015dec7d16 | 132 | static bool is_manual_peripheral_setup_needed(void) |
mbed_official | 501:36015dec7d16 | 133 | { |
mbed_official | 501:36015dec7d16 | 134 | if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x1) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)) |
mbed_official | 501:36015dec7d16 | 135 | { |
mbed_official | 501:36015dec7d16 | 136 | if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x00) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0)) |
mbed_official | 501:36015dec7d16 | 137 | { |
mbed_official | 501:36015dec7d16 | 138 | return true; |
mbed_official | 501:36015dec7d16 | 139 | } |
mbed_official | 501:36015dec7d16 | 140 | if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x10) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0)) |
mbed_official | 501:36015dec7d16 | 141 | { |
mbed_official | 501:36015dec7d16 | 142 | return true; |
mbed_official | 501:36015dec7d16 | 143 | } |
mbed_official | 501:36015dec7d16 | 144 | if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0)) |
mbed_official | 501:36015dec7d16 | 145 | { |
mbed_official | 501:36015dec7d16 | 146 | return true; |
mbed_official | 501:36015dec7d16 | 147 | } |
mbed_official | 501:36015dec7d16 | 148 | } |
mbed_official | 501:36015dec7d16 | 149 | |
mbed_official | 501:36015dec7d16 | 150 | return false; |
mbed_official | 501:36015dec7d16 | 151 | } |
mbed_official | 501:36015dec7d16 | 152 | |
mbed_official | 501:36015dec7d16 | 153 | static bool is_disabled_in_debug_needed(void) |
mbed_official | 501:36015dec7d16 | 154 | { |
mbed_official | 501:36015dec7d16 | 155 | if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x1) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)) |
mbed_official | 501:36015dec7d16 | 156 | { |
mbed_official | 501:36015dec7d16 | 157 | if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x40) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0)) |
mbed_official | 501:36015dec7d16 | 158 | { |
mbed_official | 501:36015dec7d16 | 159 | return true; |
mbed_official | 501:36015dec7d16 | 160 | } |
mbed_official | 501:36015dec7d16 | 161 | } |
mbed_official | 501:36015dec7d16 | 162 | |
mbed_official | 501:36015dec7d16 | 163 | return false; |
mbed_official | 501:36015dec7d16 | 164 | } |
mbed_official | 501:36015dec7d16 | 165 | |
mbed_official | 501:36015dec7d16 | 166 | /*lint --flb "Leave library region" */ |