Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Hobbyking_Cheetah_Compact Hobbyking_Cheetah_Compact_DRV8323_14bit Hobbyking_Cheetah_Compact_DRV8323_V51_201907 HKC_MiniCheetah ... more
Fork of mbed-dev by
targets/hal/TARGET_NXP/TARGET_LPC176X/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 | // ensure rtc is running (unchanged if already running) |
| bogdanm | 0:9b334a45a8ff | 19 | |
| bogdanm | 0:9b334a45a8ff | 20 | /* Setup the RTC based on a time structure, ensuring RTC is enabled |
| bogdanm | 0:9b334a45a8ff | 21 | * |
| bogdanm | 0:9b334a45a8ff | 22 | * Can be clocked by a 32.768KHz oscillator or prescale divider based on the APB clock |
| bogdanm | 0:9b334a45a8ff | 23 | * - We want to use the 32khz clock, allowing for sleep mode |
| bogdanm | 0:9b334a45a8ff | 24 | * |
| bogdanm | 0:9b334a45a8ff | 25 | * Most registers are not changed by a Reset |
| bogdanm | 0:9b334a45a8ff | 26 | * - We must initialize these registers between power-on and setting the RTC into operation |
| bogdanm | 0:9b334a45a8ff | 27 | |
| bogdanm | 0:9b334a45a8ff | 28 | * Clock Control Register |
| bogdanm | 0:9b334a45a8ff | 29 | * RTC_CCR[0] : Enable - 0 = Disabled, 1 = Enabled |
| bogdanm | 0:9b334a45a8ff | 30 | * RTC_CCR[1] : Reset - 0 = Normal, 1 = Reset |
| bogdanm | 0:9b334a45a8ff | 31 | * RTC_CCR[4] : Clock Source - 0 = Prescaler, 1 = 32k Xtal |
| bogdanm | 0:9b334a45a8ff | 32 | * |
| bogdanm | 0:9b334a45a8ff | 33 | * The RTC may already be running, so we should set it up |
| bogdanm | 0:9b334a45a8ff | 34 | * without impacting if it is the case |
| bogdanm | 0:9b334a45a8ff | 35 | */ |
| bogdanm | 0:9b334a45a8ff | 36 | void rtc_init(void) { |
| bogdanm | 0:9b334a45a8ff | 37 | LPC_SC->PCONP |= 0x200; // Ensure power is on |
| bogdanm | 0:9b334a45a8ff | 38 | LPC_RTC->CCR = 0x00; |
| bogdanm | 0:9b334a45a8ff | 39 | |
| bogdanm | 0:9b334a45a8ff | 40 | LPC_RTC->CCR |= 1 << 0; // Ensure the RTC is enabled |
| bogdanm | 0:9b334a45a8ff | 41 | } |
| bogdanm | 0:9b334a45a8ff | 42 | |
| bogdanm | 0:9b334a45a8ff | 43 | void rtc_free(void) { |
| bogdanm | 0:9b334a45a8ff | 44 | // [TODO] |
| bogdanm | 0:9b334a45a8ff | 45 | } |
| bogdanm | 0:9b334a45a8ff | 46 | |
| bogdanm | 0:9b334a45a8ff | 47 | /* |
| bogdanm | 0:9b334a45a8ff | 48 | * Little check routine to see if the RTC has been enabled |
| bogdanm | 0:9b334a45a8ff | 49 | * |
| bogdanm | 0:9b334a45a8ff | 50 | * Clock Control Register |
| bogdanm | 0:9b334a45a8ff | 51 | * RTC_CCR[0] : 0 = Disabled, 1 = Enabled |
| bogdanm | 0:9b334a45a8ff | 52 | * |
| bogdanm | 0:9b334a45a8ff | 53 | */ |
| bogdanm | 0:9b334a45a8ff | 54 | int rtc_isenabled(void) { |
| bogdanm | 0:9b334a45a8ff | 55 | return(((LPC_RTC->CCR) & 0x01) != 0); |
| bogdanm | 0:9b334a45a8ff | 56 | } |
| bogdanm | 0:9b334a45a8ff | 57 | |
| bogdanm | 0:9b334a45a8ff | 58 | /* |
| bogdanm | 0:9b334a45a8ff | 59 | * RTC Registers |
| bogdanm | 0:9b334a45a8ff | 60 | * RTC_SEC Seconds 0-59 |
| bogdanm | 0:9b334a45a8ff | 61 | * RTC_MIN Minutes 0-59 |
| bogdanm | 0:9b334a45a8ff | 62 | * RTC_HOUR Hour 0-23 |
| bogdanm | 0:9b334a45a8ff | 63 | * RTC_DOM Day of Month 1-28..31 |
| bogdanm | 0:9b334a45a8ff | 64 | * RTC_DOW Day of Week 0-6 |
| bogdanm | 0:9b334a45a8ff | 65 | * RTC_DOY Day of Year 1-365 |
| bogdanm | 0:9b334a45a8ff | 66 | * RTC_MONTH Month 1-12 |
| bogdanm | 0:9b334a45a8ff | 67 | * RTC_YEAR Year 0-4095 |
| bogdanm | 0:9b334a45a8ff | 68 | * |
| bogdanm | 0:9b334a45a8ff | 69 | * struct tm |
| bogdanm | 0:9b334a45a8ff | 70 | * tm_sec seconds after the minute 0-61 |
| bogdanm | 0:9b334a45a8ff | 71 | * tm_min minutes after the hour 0-59 |
| bogdanm | 0:9b334a45a8ff | 72 | * tm_hour hours since midnight 0-23 |
| bogdanm | 0:9b334a45a8ff | 73 | * tm_mday day of the month 1-31 |
| bogdanm | 0:9b334a45a8ff | 74 | * tm_mon months since January 0-11 |
| bogdanm | 0:9b334a45a8ff | 75 | * tm_year years since 1900 |
| bogdanm | 0:9b334a45a8ff | 76 | * tm_wday days since Sunday 0-6 |
| bogdanm | 0:9b334a45a8ff | 77 | * tm_yday days since January 1 0-365 |
| bogdanm | 0:9b334a45a8ff | 78 | * tm_isdst Daylight Saving Time flag |
| bogdanm | 0:9b334a45a8ff | 79 | */ |
| bogdanm | 0:9b334a45a8ff | 80 | time_t rtc_read(void) { |
| bogdanm | 0:9b334a45a8ff | 81 | // Setup a tm structure based on the RTC |
| bogdanm | 0:9b334a45a8ff | 82 | struct tm timeinfo; |
| bogdanm | 0:9b334a45a8ff | 83 | timeinfo.tm_sec = LPC_RTC->SEC; |
| bogdanm | 0:9b334a45a8ff | 84 | timeinfo.tm_min = LPC_RTC->MIN; |
| bogdanm | 0:9b334a45a8ff | 85 | timeinfo.tm_hour = LPC_RTC->HOUR; |
| bogdanm | 0:9b334a45a8ff | 86 | timeinfo.tm_mday = LPC_RTC->DOM; |
| bogdanm | 0:9b334a45a8ff | 87 | timeinfo.tm_mon = LPC_RTC->MONTH - 1; |
| bogdanm | 0:9b334a45a8ff | 88 | timeinfo.tm_year = LPC_RTC->YEAR - 1900; |
| bogdanm | 0:9b334a45a8ff | 89 | |
| bogdanm | 0:9b334a45a8ff | 90 | // Convert to timestamp |
| bogdanm | 0:9b334a45a8ff | 91 | time_t t = mktime(&timeinfo); |
| bogdanm | 0:9b334a45a8ff | 92 | |
| bogdanm | 0:9b334a45a8ff | 93 | return t; |
| bogdanm | 0:9b334a45a8ff | 94 | } |
| bogdanm | 0:9b334a45a8ff | 95 | |
| bogdanm | 0:9b334a45a8ff | 96 | void rtc_write(time_t t) { |
| bogdanm | 0:9b334a45a8ff | 97 | // Convert the time in to a tm |
| bogdanm | 0:9b334a45a8ff | 98 | struct tm *timeinfo = localtime(&t); |
| bogdanm | 0:9b334a45a8ff | 99 | |
| bogdanm | 0:9b334a45a8ff | 100 | // Pause clock, and clear counter register (clears us count) |
| bogdanm | 0:9b334a45a8ff | 101 | LPC_RTC->CCR |= 2; |
| bogdanm | 0:9b334a45a8ff | 102 | |
| bogdanm | 0:9b334a45a8ff | 103 | // Set the RTC |
| bogdanm | 0:9b334a45a8ff | 104 | LPC_RTC->SEC = timeinfo->tm_sec; |
| bogdanm | 0:9b334a45a8ff | 105 | LPC_RTC->MIN = timeinfo->tm_min; |
| bogdanm | 0:9b334a45a8ff | 106 | LPC_RTC->HOUR = timeinfo->tm_hour; |
| bogdanm | 0:9b334a45a8ff | 107 | LPC_RTC->DOM = timeinfo->tm_mday; |
| bogdanm | 0:9b334a45a8ff | 108 | LPC_RTC->MONTH = timeinfo->tm_mon + 1; |
| bogdanm | 0:9b334a45a8ff | 109 | LPC_RTC->YEAR = timeinfo->tm_year + 1900; |
| bogdanm | 0:9b334a45a8ff | 110 | |
| bogdanm | 0:9b334a45a8ff | 111 | // Restart clock |
| bogdanm | 0:9b334a45a8ff | 112 | LPC_RTC->CCR &= ~((uint32_t)2); |
| bogdanm | 0:9b334a45a8ff | 113 | } |
