mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Tue Dec 16 08:15:08 2014 +0000
Revision:
440:8a0b45cd594f
Parent:
13:0645d8841f51
Synchronized with git revision 67fbbf0b635d0c0d93fbe433306c537c2ad206aa

Full URL: https://github.com/mbedmicro/mbed/commit/67fbbf0b635d0c0d93fbe433306c537c2ad206aa/

Targets: nrf51 - updating app_timer.c from Norid'c SDKv7.1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 10:3bc89ef62ce7 1 /* mbed Microcontroller Library
emilmont 10:3bc89ef62ce7 2 * Copyright (c) 2006-2013 ARM Limited
emilmont 10:3bc89ef62ce7 3 *
emilmont 10:3bc89ef62ce7 4 * Licensed under the Apache License, Version 2.0 (the "License");
emilmont 10:3bc89ef62ce7 5 * you may not use this file except in compliance with the License.
emilmont 10:3bc89ef62ce7 6 * You may obtain a copy of the License at
emilmont 10:3bc89ef62ce7 7 *
emilmont 10:3bc89ef62ce7 8 * http://www.apache.org/licenses/LICENSE-2.0
emilmont 10:3bc89ef62ce7 9 *
emilmont 10:3bc89ef62ce7 10 * Unless required by applicable law or agreed to in writing, software
emilmont 10:3bc89ef62ce7 11 * distributed under the License is distributed on an "AS IS" BASIS,
emilmont 10:3bc89ef62ce7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
emilmont 10:3bc89ef62ce7 13 * See the License for the specific language governing permissions and
emilmont 10:3bc89ef62ce7 14 * limitations under the License.
emilmont 10:3bc89ef62ce7 15 */
emilmont 10:3bc89ef62ce7 16 #include "rtc_api.h"
emilmont 10:3bc89ef62ce7 17
emilmont 10:3bc89ef62ce7 18 // ensure rtc is running (unchanged if already running)
emilmont 10:3bc89ef62ce7 19
emilmont 10:3bc89ef62ce7 20 /* Setup the RTC based on a time structure, ensuring RTC is enabled
emilmont 10:3bc89ef62ce7 21 *
emilmont 10:3bc89ef62ce7 22 * Can be clocked by a 32.768KHz oscillator or prescale divider based on the APB clock
emilmont 10:3bc89ef62ce7 23 * - We want to use the 32khz clock, allowing for sleep mode
emilmont 10:3bc89ef62ce7 24 *
emilmont 10:3bc89ef62ce7 25 * Most registers are not changed by a Reset
emilmont 10:3bc89ef62ce7 26 * - We must initialize these registers between power-on and setting the RTC into operation
emilmont 10:3bc89ef62ce7 27
emilmont 10:3bc89ef62ce7 28 * Clock Control Register
emilmont 10:3bc89ef62ce7 29 * RTC_CCR[0] : Enable - 0 = Disabled, 1 = Enabled
emilmont 10:3bc89ef62ce7 30 * RTC_CCR[1] : Reset - 0 = Normal, 1 = Reset
emilmont 10:3bc89ef62ce7 31 * RTC_CCR[4] : Clock Source - 0 = Prescaler, 1 = 32k Xtal
emilmont 10:3bc89ef62ce7 32 *
emilmont 10:3bc89ef62ce7 33 * The RTC may already be running, so we should set it up
emilmont 10:3bc89ef62ce7 34 * without impacting if it is the case
emilmont 10:3bc89ef62ce7 35 */
emilmont 10:3bc89ef62ce7 36 void rtc_init(void) {
emilmont 10:3bc89ef62ce7 37 LPC_SC->PCONP |= 0x200; // Ensure power is on
emilmont 10:3bc89ef62ce7 38 LPC_RTC->CCR = 0x00;
emilmont 10:3bc89ef62ce7 39
emilmont 10:3bc89ef62ce7 40 LPC_RTC->CCR |= 1 << 0; // Ensure the RTC is enabled
emilmont 10:3bc89ef62ce7 41 }
emilmont 10:3bc89ef62ce7 42
emilmont 10:3bc89ef62ce7 43 void rtc_free(void) {
emilmont 10:3bc89ef62ce7 44 // [TODO]
emilmont 10:3bc89ef62ce7 45 }
emilmont 10:3bc89ef62ce7 46
emilmont 10:3bc89ef62ce7 47 /*
emilmont 10:3bc89ef62ce7 48 * Little check routine to see if the RTC has been enabled
emilmont 10:3bc89ef62ce7 49 *
emilmont 10:3bc89ef62ce7 50 * Clock Control Register
emilmont 10:3bc89ef62ce7 51 * RTC_CCR[0] : 0 = Disabled, 1 = Enabled
emilmont 10:3bc89ef62ce7 52 *
emilmont 10:3bc89ef62ce7 53 */
emilmont 10:3bc89ef62ce7 54 int rtc_isenabled(void) {
emilmont 10:3bc89ef62ce7 55 return(((LPC_RTC->CCR) & 0x01) != 0);
emilmont 10:3bc89ef62ce7 56 }
emilmont 10:3bc89ef62ce7 57
emilmont 10:3bc89ef62ce7 58 /*
emilmont 10:3bc89ef62ce7 59 * RTC Registers
emilmont 10:3bc89ef62ce7 60 * RTC_SEC Seconds 0-59
emilmont 10:3bc89ef62ce7 61 * RTC_MIN Minutes 0-59
emilmont 10:3bc89ef62ce7 62 * RTC_HOUR Hour 0-23
emilmont 10:3bc89ef62ce7 63 * RTC_DOM Day of Month 1-28..31
emilmont 10:3bc89ef62ce7 64 * RTC_DOW Day of Week 0-6
emilmont 10:3bc89ef62ce7 65 * RTC_DOY Day of Year 1-365
emilmont 10:3bc89ef62ce7 66 * RTC_MONTH Month 1-12
emilmont 10:3bc89ef62ce7 67 * RTC_YEAR Year 0-4095
emilmont 10:3bc89ef62ce7 68 *
emilmont 10:3bc89ef62ce7 69 * struct tm
emilmont 10:3bc89ef62ce7 70 * tm_sec seconds after the minute 0-61
emilmont 10:3bc89ef62ce7 71 * tm_min minutes after the hour 0-59
emilmont 10:3bc89ef62ce7 72 * tm_hour hours since midnight 0-23
emilmont 10:3bc89ef62ce7 73 * tm_mday day of the month 1-31
emilmont 10:3bc89ef62ce7 74 * tm_mon months since January 0-11
emilmont 10:3bc89ef62ce7 75 * tm_year years since 1900
emilmont 10:3bc89ef62ce7 76 * tm_wday days since Sunday 0-6
emilmont 10:3bc89ef62ce7 77 * tm_yday days since January 1 0-365
emilmont 10:3bc89ef62ce7 78 * tm_isdst Daylight Saving Time flag
emilmont 10:3bc89ef62ce7 79 */
emilmont 10:3bc89ef62ce7 80 time_t rtc_read(void) {
emilmont 10:3bc89ef62ce7 81 // Setup a tm structure based on the RTC
emilmont 10:3bc89ef62ce7 82 struct tm timeinfo;
emilmont 10:3bc89ef62ce7 83 timeinfo.tm_sec = LPC_RTC->SEC;
emilmont 10:3bc89ef62ce7 84 timeinfo.tm_min = LPC_RTC->MIN;
emilmont 10:3bc89ef62ce7 85 timeinfo.tm_hour = LPC_RTC->HOUR;
emilmont 10:3bc89ef62ce7 86 timeinfo.tm_mday = LPC_RTC->DOM;
emilmont 10:3bc89ef62ce7 87 timeinfo.tm_mon = LPC_RTC->MONTH - 1;
emilmont 10:3bc89ef62ce7 88 timeinfo.tm_year = LPC_RTC->YEAR - 1900;
emilmont 10:3bc89ef62ce7 89
emilmont 10:3bc89ef62ce7 90 // Convert to timestamp
emilmont 10:3bc89ef62ce7 91 time_t t = mktime(&timeinfo);
emilmont 10:3bc89ef62ce7 92
emilmont 10:3bc89ef62ce7 93 return t;
emilmont 10:3bc89ef62ce7 94 }
emilmont 10:3bc89ef62ce7 95
emilmont 10:3bc89ef62ce7 96 void rtc_write(time_t t) {
emilmont 10:3bc89ef62ce7 97 // Convert the time in to a tm
emilmont 10:3bc89ef62ce7 98 struct tm *timeinfo = localtime(&t);
emilmont 10:3bc89ef62ce7 99
emilmont 10:3bc89ef62ce7 100 // Pause clock, and clear counter register (clears us count)
emilmont 10:3bc89ef62ce7 101 LPC_RTC->CCR |= 2;
emilmont 10:3bc89ef62ce7 102
emilmont 10:3bc89ef62ce7 103 // Set the RTC
emilmont 10:3bc89ef62ce7 104 LPC_RTC->SEC = timeinfo->tm_sec;
emilmont 10:3bc89ef62ce7 105 LPC_RTC->MIN = timeinfo->tm_min;
emilmont 10:3bc89ef62ce7 106 LPC_RTC->HOUR = timeinfo->tm_hour;
emilmont 10:3bc89ef62ce7 107 LPC_RTC->DOM = timeinfo->tm_mday;
emilmont 10:3bc89ef62ce7 108 LPC_RTC->MONTH = timeinfo->tm_mon + 1;
emilmont 10:3bc89ef62ce7 109 LPC_RTC->YEAR = timeinfo->tm_year + 1900;
emilmont 10:3bc89ef62ce7 110
emilmont 10:3bc89ef62ce7 111 // Restart clock
emilmont 10:3bc89ef62ce7 112 LPC_RTC->CCR &= ~((uint32_t)2);
emilmont 10:3bc89ef62ce7 113 }