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