mbed-os
Fork of mbed-os by
targets/TARGET_NORDIC/TARGET_NRF5/rtc_api.c@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* |
elessair | 0:f269e3021894 | 2 | * Copyright (c) 2013 Nordic Semiconductor ASA |
elessair | 0:f269e3021894 | 3 | * All rights reserved. |
elessair | 0:f269e3021894 | 4 | * |
elessair | 0:f269e3021894 | 5 | * Redistribution and use in source and binary forms, with or without modification, |
elessair | 0:f269e3021894 | 6 | * are permitted provided that the following conditions are met: |
elessair | 0:f269e3021894 | 7 | * |
elessair | 0:f269e3021894 | 8 | * 1. Redistributions of source code must retain the above copyright notice, this list |
elessair | 0:f269e3021894 | 9 | * of conditions and the following disclaimer. |
elessair | 0:f269e3021894 | 10 | * |
elessair | 0:f269e3021894 | 11 | * 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA |
elessair | 0:f269e3021894 | 12 | * integrated circuit in a product or a software update for such product, must reproduce |
elessair | 0:f269e3021894 | 13 | * the above copyright notice, this list of conditions and the following disclaimer in |
elessair | 0:f269e3021894 | 14 | * the documentation and/or other materials provided with the distribution. |
elessair | 0:f269e3021894 | 15 | * |
elessair | 0:f269e3021894 | 16 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be |
elessair | 0:f269e3021894 | 17 | * used to endorse or promote products derived from this software without specific prior |
elessair | 0:f269e3021894 | 18 | * written permission. |
elessair | 0:f269e3021894 | 19 | * |
elessair | 0:f269e3021894 | 20 | * 4. This software, with or without modification, must only be used with a |
elessair | 0:f269e3021894 | 21 | * Nordic Semiconductor ASA integrated circuit. |
elessair | 0:f269e3021894 | 22 | * |
elessair | 0:f269e3021894 | 23 | * 5. Any software provided in binary or object form under this license must not be reverse |
elessair | 0:f269e3021894 | 24 | * engineered, decompiled, modified and/or disassembled. |
elessair | 0:f269e3021894 | 25 | * |
elessair | 0:f269e3021894 | 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
elessair | 0:f269e3021894 | 27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
elessair | 0:f269e3021894 | 28 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
elessair | 0:f269e3021894 | 29 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
elessair | 0:f269e3021894 | 30 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
elessair | 0:f269e3021894 | 31 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
elessair | 0:f269e3021894 | 32 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
elessair | 0:f269e3021894 | 33 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
elessair | 0:f269e3021894 | 34 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
elessair | 0:f269e3021894 | 35 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
elessair | 0:f269e3021894 | 36 | * |
elessair | 0:f269e3021894 | 37 | */ |
elessair | 0:f269e3021894 | 38 | |
elessair | 0:f269e3021894 | 39 | #include "rtc_api.h" |
elessair | 0:f269e3021894 | 40 | |
elessair | 0:f269e3021894 | 41 | #if DEVICE_RTC |
elessair | 0:f269e3021894 | 42 | |
elessair | 0:f269e3021894 | 43 | #include "common_rtc.h" |
elessair | 0:f269e3021894 | 44 | #include "nrf_drv_clock.h" |
elessair | 0:f269e3021894 | 45 | #include "app_util_platform.h" |
elessair | 0:f269e3021894 | 46 | |
elessair | 0:f269e3021894 | 47 | static time_t m_time_base; |
elessair | 0:f269e3021894 | 48 | |
elessair | 0:f269e3021894 | 49 | void rtc_init(void) |
elessair | 0:f269e3021894 | 50 | { |
elessair | 0:f269e3021894 | 51 | common_rtc_init(); |
elessair | 0:f269e3021894 | 52 | } |
elessair | 0:f269e3021894 | 53 | |
elessair | 0:f269e3021894 | 54 | void rtc_free(void) |
elessair | 0:f269e3021894 | 55 | { |
elessair | 0:f269e3021894 | 56 | // A common counter is used for RTC, lp_ticker and us_ticker, so it can't be |
elessair | 0:f269e3021894 | 57 | // disabled here, but this does not cause any extra cost. Besides, currently |
elessair | 0:f269e3021894 | 58 | // this function is not used by RTC API in mbed-drivers. |
elessair | 0:f269e3021894 | 59 | } |
elessair | 0:f269e3021894 | 60 | |
elessair | 0:f269e3021894 | 61 | int rtc_isenabled(void) |
elessair | 0:f269e3021894 | 62 | { |
elessair | 0:f269e3021894 | 63 | return m_common_rtc_enabled; |
elessair | 0:f269e3021894 | 64 | } |
elessair | 0:f269e3021894 | 65 | |
elessair | 0:f269e3021894 | 66 | static uint32_t rtc_seconds_get(void) |
elessair | 0:f269e3021894 | 67 | { |
elessair | 0:f269e3021894 | 68 | // Convert current counter value to seconds. |
elessair | 0:f269e3021894 | 69 | uint32_t seconds = nrf_rtc_counter_get(COMMON_RTC_INSTANCE) / RTC_INPUT_FREQ; |
elessair | 0:f269e3021894 | 70 | // Add proper amount of seconds for each registered overflow of the counter. |
elessair | 0:f269e3021894 | 71 | uint32_t seconds_per_overflow = (1uL << RTC_COUNTER_BITS) / RTC_INPUT_FREQ; |
elessair | 0:f269e3021894 | 72 | return (seconds + (m_common_rtc_overflows * seconds_per_overflow)); |
elessair | 0:f269e3021894 | 73 | } |
elessair | 0:f269e3021894 | 74 | |
elessair | 0:f269e3021894 | 75 | time_t rtc_read(void) |
elessair | 0:f269e3021894 | 76 | { |
elessair | 0:f269e3021894 | 77 | return m_time_base + rtc_seconds_get(); |
elessair | 0:f269e3021894 | 78 | } |
elessair | 0:f269e3021894 | 79 | |
elessair | 0:f269e3021894 | 80 | void rtc_write(time_t t) |
elessair | 0:f269e3021894 | 81 | { |
elessair | 0:f269e3021894 | 82 | uint32_t seconds; |
elessair | 0:f269e3021894 | 83 | do { |
elessair | 0:f269e3021894 | 84 | seconds = rtc_seconds_get(); |
elessair | 0:f269e3021894 | 85 | m_time_base = t - seconds; |
elessair | 0:f269e3021894 | 86 | // If the number of seconds indicated by the counter changed during the |
elessair | 0:f269e3021894 | 87 | // update of the time base, just repeat the update, now using the new |
elessair | 0:f269e3021894 | 88 | // number of seconds. |
elessair | 0:f269e3021894 | 89 | } while (seconds != rtc_seconds_get()); |
elessair | 0:f269e3021894 | 90 | } |
elessair | 0:f269e3021894 | 91 | |
elessair | 0:f269e3021894 | 92 | #endif // DEVICE_RTC |