|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2006-2013 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include "rtc_api.h" |
| 17 | + |
| 18 | +static int rtc_inited = 0; |
| 19 | + |
| 20 | +void rtc_init(void) { |
| 21 | + RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); // Enable PWR and Backup clock |
| 22 | + |
| 23 | + PWR_BackupAccessCmd(ENABLE); // Allow access to Backup Domain |
| 24 | + |
| 25 | + BKP_DeInit(); // Reset Backup Domain |
| 26 | + |
| 27 | + // Enable LSE and wait till it's ready |
| 28 | + RCC_LSEConfig(RCC_LSE_ON); |
| 29 | + while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {} |
| 30 | + |
| 31 | + RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); // Select LSE as RTC Clock Source |
| 32 | + |
| 33 | + RCC_RTCCLKCmd(ENABLE); // Enable RTC Clock |
| 34 | + |
| 35 | + RTC_WaitForSynchro(); // Wait for RTC registers synchronization |
| 36 | + |
| 37 | + RTC_WaitForLastTask(); // Wait until last write operation on RTC registers has finished |
| 38 | + |
| 39 | + // Set RTC period to 1 sec |
| 40 | + // RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) |
| 41 | + RTC_SetPrescaler(32767); |
| 42 | + |
| 43 | + RTC_WaitForLastTask(); // Wait until last write operation on RTC registers has finished |
| 44 | + |
| 45 | + rtc_inited = 1; |
| 46 | +} |
| 47 | + |
| 48 | +void rtc_free(void) { |
| 49 | + RCC_DeInit(); // Resets the RCC clock configuration to the default reset state |
| 50 | + rtc_inited = 0; |
| 51 | +} |
| 52 | + |
| 53 | +int rtc_isenabled(void) { |
| 54 | + return rtc_inited; |
| 55 | +} |
| 56 | + |
| 57 | +time_t rtc_read(void) { |
| 58 | + return (time_t)RTC_GetCounter(); |
| 59 | +} |
| 60 | + |
| 61 | +void rtc_write(time_t t) { |
| 62 | + RTC_WaitForLastTask(); // Wait until last write operation on RTC registers has finished |
| 63 | + RTC_SetCounter(t); // Change the current time |
| 64 | + RTC_WaitForLastTask(); // Wait until last write operation on RTC registers has finished |
| 65 | +} |
0 commit comments