mbed library sources
Fork of mbed-src by
targets/hal/TARGET_Atmel/TARGET_SAM21/rtc_api.c@592:a274ee790e56, 2015-07-17 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Jul 17 09:15:10 2015 +0100
- Revision:
- 592:a274ee790e56
Synchronized with git revision e7144f83a8d75df80c4877936b6ffe552b0be9e6
Full URL: https://github.com/mbedmicro/mbed/commit/e7144f83a8d75df80c4877936b6ffe552b0be9e6/
More API implementation for SAMR21
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 592:a274ee790e56 | 1 | /* mbed Microcontroller Library |
mbed_official | 592:a274ee790e56 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 592:a274ee790e56 | 3 | * |
mbed_official | 592:a274ee790e56 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 592:a274ee790e56 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 592:a274ee790e56 | 6 | * You may obtain a copy of the License at |
mbed_official | 592:a274ee790e56 | 7 | * |
mbed_official | 592:a274ee790e56 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 592:a274ee790e56 | 9 | * |
mbed_official | 592:a274ee790e56 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 592:a274ee790e56 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 592:a274ee790e56 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 592:a274ee790e56 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 592:a274ee790e56 | 14 | * limitations under the License. |
mbed_official | 592:a274ee790e56 | 15 | */ |
mbed_official | 592:a274ee790e56 | 16 | #include "mbed_assert.h" |
mbed_official | 592:a274ee790e56 | 17 | #include "rtc_api.h" |
mbed_official | 592:a274ee790e56 | 18 | |
mbed_official | 592:a274ee790e56 | 19 | #include <math.h> |
mbed_official | 592:a274ee790e56 | 20 | |
mbed_official | 592:a274ee790e56 | 21 | #include "cmsis.h" |
mbed_official | 592:a274ee790e56 | 22 | #include "system.h" |
mbed_official | 592:a274ee790e56 | 23 | |
mbed_official | 592:a274ee790e56 | 24 | #include "rtc_count.h" |
mbed_official | 592:a274ee790e56 | 25 | |
mbed_official | 592:a274ee790e56 | 26 | /* Global RTC instance*/ |
mbed_official | 592:a274ee790e56 | 27 | static struct rtc_module rtc_instance; |
mbed_official | 592:a274ee790e56 | 28 | |
mbed_official | 592:a274ee790e56 | 29 | static int rtc_inited = 0; |
mbed_official | 592:a274ee790e56 | 30 | |
mbed_official | 592:a274ee790e56 | 31 | /* Extern variables */ |
mbed_official | 592:a274ee790e56 | 32 | extern uint8_t g_sys_init; |
mbed_official | 592:a274ee790e56 | 33 | |
mbed_official | 592:a274ee790e56 | 34 | /** Initialize the RTC |
mbed_official | 592:a274ee790e56 | 35 | * |
mbed_official | 592:a274ee790e56 | 36 | * Initialize the RTC with default time |
mbed_official | 592:a274ee790e56 | 37 | * @param[void] void |
mbed_official | 592:a274ee790e56 | 38 | */ |
mbed_official | 592:a274ee790e56 | 39 | void rtc_init(void) |
mbed_official | 592:a274ee790e56 | 40 | { |
mbed_official | 592:a274ee790e56 | 41 | if (g_sys_init == 0) { |
mbed_official | 592:a274ee790e56 | 42 | system_init(); |
mbed_official | 592:a274ee790e56 | 43 | g_sys_init = 1; |
mbed_official | 592:a274ee790e56 | 44 | } |
mbed_official | 592:a274ee790e56 | 45 | |
mbed_official | 592:a274ee790e56 | 46 | struct rtc_count_config config_rtc_count; |
mbed_official | 592:a274ee790e56 | 47 | |
mbed_official | 592:a274ee790e56 | 48 | rtc_count_get_config_defaults(&config_rtc_count); |
mbed_official | 592:a274ee790e56 | 49 | |
mbed_official | 592:a274ee790e56 | 50 | config_rtc_count.prescaler = RTC_COUNT_PRESCALER_DIV_1024; |
mbed_official | 592:a274ee790e56 | 51 | config_rtc_count.mode = RTC_COUNT_MODE_32BIT; |
mbed_official | 592:a274ee790e56 | 52 | #ifdef FEATURE_RTC_CONTINUOUSLY_UPDATED |
mbed_official | 592:a274ee790e56 | 53 | config_rtc_count.continuously_update = true; |
mbed_official | 592:a274ee790e56 | 54 | #endif |
mbed_official | 592:a274ee790e56 | 55 | |
mbed_official | 592:a274ee790e56 | 56 | rtc_count_init(&rtc_instance, RTC, &config_rtc_count); |
mbed_official | 592:a274ee790e56 | 57 | |
mbed_official | 592:a274ee790e56 | 58 | rtc_count_enable(&rtc_instance); |
mbed_official | 592:a274ee790e56 | 59 | rtc_inited = 1; |
mbed_official | 592:a274ee790e56 | 60 | } |
mbed_official | 592:a274ee790e56 | 61 | |
mbed_official | 592:a274ee790e56 | 62 | /** Frees the RTC |
mbed_official | 592:a274ee790e56 | 63 | * |
mbed_official | 592:a274ee790e56 | 64 | * @param[void] void |
mbed_official | 592:a274ee790e56 | 65 | */ |
mbed_official | 592:a274ee790e56 | 66 | void rtc_free(void) |
mbed_official | 592:a274ee790e56 | 67 | { |
mbed_official | 592:a274ee790e56 | 68 | if (rtc_inited) { |
mbed_official | 592:a274ee790e56 | 69 | /* Disable the RTC module */ |
mbed_official | 592:a274ee790e56 | 70 | rtc_count_disable(&rtc_instance); |
mbed_official | 592:a274ee790e56 | 71 | /* Disable the RTC clock */ |
mbed_official | 592:a274ee790e56 | 72 | system_gclk_chan_disable(RTC_GCLK_ID); |
mbed_official | 592:a274ee790e56 | 73 | rtc_inited = 0; |
mbed_official | 592:a274ee790e56 | 74 | } |
mbed_official | 592:a274ee790e56 | 75 | } |
mbed_official | 592:a274ee790e56 | 76 | |
mbed_official | 592:a274ee790e56 | 77 | /** Checks whether RTC is enabled or not |
mbed_official | 592:a274ee790e56 | 78 | * |
mbed_official | 592:a274ee790e56 | 79 | * To check whether RTC module is enabled or not |
mbed_official | 592:a274ee790e56 | 80 | * @param[void] void |
mbed_official | 592:a274ee790e56 | 81 | * @return Non zero if RTC is already enabled, else zero |
mbed_official | 592:a274ee790e56 | 82 | */ |
mbed_official | 592:a274ee790e56 | 83 | int rtc_isenabled(void) |
mbed_official | 592:a274ee790e56 | 84 | { |
mbed_official | 592:a274ee790e56 | 85 | return rtc_inited; |
mbed_official | 592:a274ee790e56 | 86 | } |
mbed_official | 592:a274ee790e56 | 87 | |
mbed_official | 592:a274ee790e56 | 88 | /** Reads the RTC value |
mbed_official | 592:a274ee790e56 | 89 | * |
mbed_official | 592:a274ee790e56 | 90 | * Reads and return the current time in RTC |
mbed_official | 592:a274ee790e56 | 91 | * @param[void] void |
mbed_official | 592:a274ee790e56 | 92 | * @return the current value in RTC |
mbed_official | 592:a274ee790e56 | 93 | */ |
mbed_official | 592:a274ee790e56 | 94 | time_t rtc_read(void) |
mbed_official | 592:a274ee790e56 | 95 | { |
mbed_official | 592:a274ee790e56 | 96 | if (!rtc_inited) { |
mbed_official | 592:a274ee790e56 | 97 | /* Return invalid time for now! */ |
mbed_official | 592:a274ee790e56 | 98 | return 0; |
mbed_official | 592:a274ee790e56 | 99 | } |
mbed_official | 592:a274ee790e56 | 100 | return (time_t)rtc_count_get_count(&rtc_instance); |
mbed_official | 592:a274ee790e56 | 101 | } |
mbed_official | 592:a274ee790e56 | 102 | |
mbed_official | 592:a274ee790e56 | 103 | /** Write the RTC value |
mbed_official | 592:a274ee790e56 | 104 | * |
mbed_official | 592:a274ee790e56 | 105 | * Update the time value in RTC |
mbed_official | 592:a274ee790e56 | 106 | * @param[in] t The time value to be written |
mbed_official | 592:a274ee790e56 | 107 | * @return void |
mbed_official | 592:a274ee790e56 | 108 | */ |
mbed_official | 592:a274ee790e56 | 109 | void rtc_write(time_t t) |
mbed_official | 592:a274ee790e56 | 110 | { |
mbed_official | 592:a274ee790e56 | 111 | if (!rtc_inited) { |
mbed_official | 592:a274ee790e56 | 112 | /* Initialize the RTC is not yet initialized */ |
mbed_official | 592:a274ee790e56 | 113 | rtc_init(); |
mbed_official | 592:a274ee790e56 | 114 | } |
mbed_official | 592:a274ee790e56 | 115 | |
mbed_official | 592:a274ee790e56 | 116 | uint32_t count_value = (uint32_t)t; |
mbed_official | 592:a274ee790e56 | 117 | rtc_count_set_count(&rtc_instance, count_value); |
mbed_official | 592:a274ee790e56 | 118 | } |