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:
Fri Sep 11 09:30:09 2015 +0100
Revision:
621:9c82b0f79f3d
Parent:
462:e03396e14338
Synchronized with git revision 6c1d63e069ab9bd86de92e8296ca783681257538

Full URL: https://github.com/mbedmicro/mbed/commit/6c1d63e069ab9bd86de92e8296ca783681257538/

ignore target files not supported by the yotta module

Who changed what in which revision?

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