mbed library sources. Supersedes mbed-src.
Dependents: Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more
targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/TARGET_DELTA_DFCM_NNN40/rtc_api.c@0:9b334a45a8ff, 2015-10-01 (annotated)
- Committer:
- bogdanm
- Date:
- Thu Oct 01 15:25:22 2015 +0300
- Revision:
- 0:9b334a45a8ff
- Child:
- 144:ef7eb2e8f9f7
Initial commit on mbed-dev
Replaces mbed-src (now inactive)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 0:9b334a45a8ff | 1 | /* mbed Microcontroller Library |
bogdanm | 0:9b334a45a8ff | 2 | * Copyright (c) 2006-2013 ARM Limited |
bogdanm | 0:9b334a45a8ff | 3 | * |
bogdanm | 0:9b334a45a8ff | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 0:9b334a45a8ff | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 0:9b334a45a8ff | 6 | * You may obtain a copy of the License at |
bogdanm | 0:9b334a45a8ff | 7 | * |
bogdanm | 0:9b334a45a8ff | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 0:9b334a45a8ff | 9 | * |
bogdanm | 0:9b334a45a8ff | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 0:9b334a45a8ff | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 0:9b334a45a8ff | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 0:9b334a45a8ff | 13 | * See the License for the specific language governing permissions and |
bogdanm | 0:9b334a45a8ff | 14 | * limitations under the License. |
bogdanm | 0:9b334a45a8ff | 15 | */ |
bogdanm | 0:9b334a45a8ff | 16 | #include "rtc_api.h" |
bogdanm | 0:9b334a45a8ff | 17 | |
bogdanm | 0:9b334a45a8ff | 18 | |
bogdanm | 0:9b334a45a8ff | 19 | #define LFCLK_FREQUENCY (32768UL) |
bogdanm | 0:9b334a45a8ff | 20 | #define RTC0_COUNTER_PRESCALER ((LFCLK_FREQUENCY/8) - 1) |
bogdanm | 0:9b334a45a8ff | 21 | #define COMPARE_COUNTERTIME (691200UL) //86400 x 8 |
bogdanm | 0:9b334a45a8ff | 22 | |
bogdanm | 0:9b334a45a8ff | 23 | |
bogdanm | 0:9b334a45a8ff | 24 | time_t initTime; |
bogdanm | 0:9b334a45a8ff | 25 | |
bogdanm | 0:9b334a45a8ff | 26 | void rtc_init(void) { |
bogdanm | 0:9b334a45a8ff | 27 | |
bogdanm | 0:9b334a45a8ff | 28 | NVIC_EnableIRQ(RTC0_IRQn); // Enable Interrupt for the RTC in the core. |
bogdanm | 0:9b334a45a8ff | 29 | //NRF_RTC0->TASKS_STOP =1; |
bogdanm | 0:9b334a45a8ff | 30 | NRF_RTC0->PRESCALER = RTC0_COUNTER_PRESCALER; // Set prescaler to a TICK of RTC_FREQUENCY. |
bogdanm | 0:9b334a45a8ff | 31 | NRF_RTC0->CC[0] = COMPARE_COUNTERTIME; // Compare0 after approx COMPARE_COUNTERTIME seconds. |
bogdanm | 0:9b334a45a8ff | 32 | |
bogdanm | 0:9b334a45a8ff | 33 | // Enable COMPARE0 event and COMPARE0 interrupt: |
bogdanm | 0:9b334a45a8ff | 34 | NRF_RTC0->EVTENSET = RTC_EVTENSET_COMPARE0_Msk; |
bogdanm | 0:9b334a45a8ff | 35 | NRF_RTC0->INTENSET = RTC_INTENSET_COMPARE0_Msk; |
bogdanm | 0:9b334a45a8ff | 36 | NRF_RTC0->TASKS_START = 1; |
bogdanm | 0:9b334a45a8ff | 37 | } |
bogdanm | 0:9b334a45a8ff | 38 | |
bogdanm | 0:9b334a45a8ff | 39 | void rtc_free(void) { |
bogdanm | 0:9b334a45a8ff | 40 | // [TODO] |
bogdanm | 0:9b334a45a8ff | 41 | } |
bogdanm | 0:9b334a45a8ff | 42 | |
bogdanm | 0:9b334a45a8ff | 43 | /* |
bogdanm | 0:9b334a45a8ff | 44 | * Little check routine to see if the RTC has been enabled |
bogdanm | 0:9b334a45a8ff | 45 | * |
bogdanm | 0:9b334a45a8ff | 46 | * Clock Control Register |
bogdanm | 0:9b334a45a8ff | 47 | * RTC_CCR[0] : 0 = Disabled, 1 = Enabled |
bogdanm | 0:9b334a45a8ff | 48 | * |
bogdanm | 0:9b334a45a8ff | 49 | */ |
bogdanm | 0:9b334a45a8ff | 50 | int rtc_isenabled(void) { |
bogdanm | 0:9b334a45a8ff | 51 | // [TODO] return(((NRF_RTC0->TASKS_START) & 0x01) != 0); |
bogdanm | 0:9b334a45a8ff | 52 | } |
bogdanm | 0:9b334a45a8ff | 53 | |
bogdanm | 0:9b334a45a8ff | 54 | time_t rtc_read(void) { |
bogdanm | 0:9b334a45a8ff | 55 | |
bogdanm | 0:9b334a45a8ff | 56 | time_t t = initTime; |
bogdanm | 0:9b334a45a8ff | 57 | t += (86400*NRF_RTC0->EVENTS_COMPARE[0]); |
bogdanm | 0:9b334a45a8ff | 58 | t += (int)((NRF_RTC0->COUNTER)/8); |
bogdanm | 0:9b334a45a8ff | 59 | return(t); |
bogdanm | 0:9b334a45a8ff | 60 | } |
bogdanm | 0:9b334a45a8ff | 61 | |
bogdanm | 0:9b334a45a8ff | 62 | void rtc_write(time_t t) { |
bogdanm | 0:9b334a45a8ff | 63 | // Convert the time in to a tm |
bogdanm | 0:9b334a45a8ff | 64 | |
bogdanm | 0:9b334a45a8ff | 65 | // Pause clock, and clear counter register (clears us count) |
bogdanm | 0:9b334a45a8ff | 66 | NRF_RTC0->TASKS_STOP = 1; |
bogdanm | 0:9b334a45a8ff | 67 | |
bogdanm | 0:9b334a45a8ff | 68 | initTime = t; |
bogdanm | 0:9b334a45a8ff | 69 | // Restart clock |
bogdanm | 0:9b334a45a8ff | 70 | NRF_RTC0->TASKS_START = 1; |
bogdanm | 0:9b334a45a8ff | 71 | } |