mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by Umar Naeem

Committer:
ranaumarnaeem
Date:
Tue May 23 12:54:50 2017 +0000
Revision:
165:2dd56e6daeec
Parent:
157:ff67d9f36b67
jhjg

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 157:ff67d9f36b67 1 /**
<> 157:ff67d9f36b67 2 * @file
<> 157:ff67d9f36b67 3 * @brief Real-Time Clock Function Implementations.
<> 157:ff67d9f36b67 4 */
<> 157:ff67d9f36b67 5 /* ****************************************************************************
<> 157:ff67d9f36b67 6 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
<> 157:ff67d9f36b67 7 *
<> 157:ff67d9f36b67 8 * Permission is hereby granted, free of charge, to any person obtaining a
<> 157:ff67d9f36b67 9 * copy of this software and associated documentation files (the "Software"),
<> 157:ff67d9f36b67 10 * to deal in the Software without restriction, including without limitation
<> 157:ff67d9f36b67 11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
<> 157:ff67d9f36b67 12 * and/or sell copies of the Software, and to permit persons to whom the
<> 157:ff67d9f36b67 13 * Software is furnished to do so, subject to the following conditions:
<> 157:ff67d9f36b67 14 *
<> 157:ff67d9f36b67 15 * The above copyright notice and this permission notice shall be included
<> 157:ff67d9f36b67 16 * in all copies or substantial portions of the Software.
<> 157:ff67d9f36b67 17 *
<> 157:ff67d9f36b67 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
<> 157:ff67d9f36b67 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
<> 157:ff67d9f36b67 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
<> 157:ff67d9f36b67 21 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
<> 157:ff67d9f36b67 22 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
<> 157:ff67d9f36b67 23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
<> 157:ff67d9f36b67 24 * OTHER DEALINGS IN THE SOFTWARE.
<> 157:ff67d9f36b67 25 *
<> 157:ff67d9f36b67 26 * Except as contained in this notice, the name of Maxim Integrated
<> 157:ff67d9f36b67 27 * Products, Inc. shall not be used except as stated in the Maxim Integrated
<> 157:ff67d9f36b67 28 * Products, Inc. Branding Policy.
<> 157:ff67d9f36b67 29 *
<> 157:ff67d9f36b67 30 * The mere transfer of this software does not imply any licenses
<> 157:ff67d9f36b67 31 * of trade secrets, proprietary technology, copyrights, patents,
<> 157:ff67d9f36b67 32 * trademarks, maskwork rights, or any other form of intellectual
<> 157:ff67d9f36b67 33 * property whatsoever. Maxim Integrated Products, Inc. retains all
<> 157:ff67d9f36b67 34 * ownership rights.
<> 157:ff67d9f36b67 35 *
<> 157:ff67d9f36b67 36 * $Date: 2016-09-08 17:43:13 -0500 (Thu, 08 Sep 2016) $
<> 157:ff67d9f36b67 37 * $Revision: 24326 $
<> 157:ff67d9f36b67 38 *
<> 157:ff67d9f36b67 39 **************************************************************************** */
<> 157:ff67d9f36b67 40
<> 157:ff67d9f36b67 41 /* **** Includes **** */
<> 157:ff67d9f36b67 42 #include <string.h>
<> 157:ff67d9f36b67 43
<> 157:ff67d9f36b67 44 #include "rtc.h"
<> 157:ff67d9f36b67 45 #include "mxc_assert.h"
<> 157:ff67d9f36b67 46 #include "mxc_sys.h"
<> 157:ff67d9f36b67 47
<> 157:ff67d9f36b67 48 /**
<> 157:ff67d9f36b67 49 * @ingroup rtc
<> 157:ff67d9f36b67 50 * @{
<> 157:ff67d9f36b67 51 */
<> 157:ff67d9f36b67 52 /* ************************************************************************* */
<> 157:ff67d9f36b67 53 int RTC_Init(const rtc_cfg_t *cfg)
<> 157:ff67d9f36b67 54 {
<> 157:ff67d9f36b67 55 int err;
<> 157:ff67d9f36b67 56 int i = 0;
<> 157:ff67d9f36b67 57
<> 157:ff67d9f36b67 58 //init function -> validate configuration pointer is not NULL
<> 157:ff67d9f36b67 59 if (cfg == NULL)
<> 157:ff67d9f36b67 60 return E_NULL_PTR;
<> 157:ff67d9f36b67 61 //check to make sure that the passed in parameters, prescaler mask and snooze, are valid
<> 157:ff67d9f36b67 62 if ((cfg->prescalerMask > ((rtc_prescale_t)cfg->prescaler)) || (cfg->snoozeCount > MXC_F_RTC_SNZ_VAL_VALUE))
<> 157:ff67d9f36b67 63 return E_INVALID;
<> 157:ff67d9f36b67 64
<> 157:ff67d9f36b67 65 // Set system level configurations
<> 157:ff67d9f36b67 66 if ((err = SYS_RTC_Init()) != E_NO_ERROR) {
<> 157:ff67d9f36b67 67 return err;
<> 157:ff67d9f36b67 68 }
<> 157:ff67d9f36b67 69
<> 157:ff67d9f36b67 70 //disable rtc
<> 157:ff67d9f36b67 71 MXC_RTCTMR->ctrl &= ~(MXC_F_RTC_CTRL_ENABLE);
<> 157:ff67d9f36b67 72
<> 157:ff67d9f36b67 73 //disable all interrupts
<> 157:ff67d9f36b67 74 MXC_RTCTMR->inten = 0;
<> 157:ff67d9f36b67 75
<> 157:ff67d9f36b67 76 //clear all interrupts
<> 157:ff67d9f36b67 77 MXC_RTCTMR->flags = RTC_FLAGS_CLEAR_ALL;
<> 157:ff67d9f36b67 78
<> 157:ff67d9f36b67 79 //reset starting count
<> 157:ff67d9f36b67 80 MXC_RTCTMR->timer = 0;
<> 157:ff67d9f36b67 81
<> 157:ff67d9f36b67 82 //set the compare registers to the values passed in
<> 157:ff67d9f36b67 83 for(i = 0; i < RTC_NUM_COMPARE; i++)
<> 157:ff67d9f36b67 84 MXC_RTCTMR->comp[i] = cfg->compareCount[i];
<> 157:ff67d9f36b67 85
<> 157:ff67d9f36b67 86 // set the prescaler
<> 157:ff67d9f36b67 87 MXC_RTCTMR->prescale = cfg->prescaler;
<> 157:ff67d9f36b67 88 // set the prescale mask, checked it for validity on entry
<> 157:ff67d9f36b67 89 MXC_RTCTMR->prescale_mask = cfg->prescalerMask;
<> 157:ff67d9f36b67 90
<> 157:ff67d9f36b67 91 //set snooze mode (rtc_snooze_t)
<> 157:ff67d9f36b67 92 MXC_RTCTMR->ctrl &= (~MXC_F_RTC_CTRL_SNOOZE_ENABLE);
<> 157:ff67d9f36b67 93 MXC_RTCTMR->ctrl |= (cfg->snoozeMode << MXC_F_RTC_CTRL_SNOOZE_ENABLE_POS);
<> 157:ff67d9f36b67 94
<> 157:ff67d9f36b67 95 //set the snooze count. Checked for validity on entry.
<> 157:ff67d9f36b67 96 MXC_RTCTMR->snz_val = (cfg->snoozeCount << MXC_F_RTC_SNZ_VAL_VALUE_POS) & MXC_F_RTC_SNZ_VAL_VALUE;
<> 157:ff67d9f36b67 97
<> 157:ff67d9f36b67 98 //wait for pending actions to complete
<> 157:ff67d9f36b67 99 while(MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING);
<> 157:ff67d9f36b67 100
<> 157:ff67d9f36b67 101 //reset trim to defaults, trim disabled, trim faster override disabled
<> 157:ff67d9f36b67 102 MXC_RTCTMR->trim_ctrl &= ~(MXC_F_RTC_TRIM_CTRL_TRIM_ENABLE_R | MXC_F_RTC_TRIM_CTRL_TRIM_FASTER_OVR_R);
<> 157:ff67d9f36b67 103
<> 157:ff67d9f36b67 104 //set trim slower control bit to 0, which is trim faster by default
<> 157:ff67d9f36b67 105 MXC_RTCTMR->trim_value &= ~(MXC_F_RTC_TRIM_VALUE_TRIM_SLOWER_CONTROL);
<> 157:ff67d9f36b67 106
<> 157:ff67d9f36b67 107 return E_NO_ERROR;
<> 157:ff67d9f36b67 108 }
<> 157:ff67d9f36b67 109
<> 157:ff67d9f36b67 110 /* ************************************************************************* */
<> 157:ff67d9f36b67 111 int RTC_SetCompare(uint8_t compareIndex, uint32_t counts)
<> 157:ff67d9f36b67 112 {
<> 157:ff67d9f36b67 113 //check for invalid index
<> 157:ff67d9f36b67 114 if (compareIndex >= RTC_NUM_COMPARE)
<> 157:ff67d9f36b67 115 return E_INVALID;
<> 157:ff67d9f36b67 116
<> 157:ff67d9f36b67 117 MXC_RTCTMR->comp[compareIndex] = counts;
<> 157:ff67d9f36b67 118
<> 157:ff67d9f36b67 119 //wait for pending actions to complete
<> 157:ff67d9f36b67 120 while(MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING);
<> 157:ff67d9f36b67 121
<> 157:ff67d9f36b67 122 return E_NO_ERROR;
<> 157:ff67d9f36b67 123 }
<> 157:ff67d9f36b67 124
<> 157:ff67d9f36b67 125 /* ************************************************************************* */
<> 157:ff67d9f36b67 126 uint32_t RTC_GetCompare(uint8_t compareIndex)
<> 157:ff67d9f36b67 127 {
<> 157:ff67d9f36b67 128 //Debug Assert for Invalid Index
<> 157:ff67d9f36b67 129 MXC_ASSERT(compareIndex < RTC_NUM_COMPARE);
<> 157:ff67d9f36b67 130 //check for invalid index
<> 157:ff67d9f36b67 131 if (compareIndex >= RTC_NUM_COMPARE)
<> 157:ff67d9f36b67 132 return (uint32_t)(E_BAD_PARAM); /* Unsigned int, so if out of bounds we return 0xFFFFFFFD (-3) */
<> 157:ff67d9f36b67 133
<> 157:ff67d9f36b67 134 return MXC_RTCTMR->comp[compareIndex];
<> 157:ff67d9f36b67 135 }
<> 157:ff67d9f36b67 136
<> 157:ff67d9f36b67 137 /* ************************************************************************* */
<> 157:ff67d9f36b67 138 int RTC_SetTrim(uint32_t trim, uint8_t trimSlow)
<> 157:ff67d9f36b67 139 {
<> 157:ff67d9f36b67 140 // make sure rtc is disabled
<> 157:ff67d9f36b67 141 if(MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_ENABLE)
<> 157:ff67d9f36b67 142 return E_BAD_STATE; // RTC is active, bad state
<> 157:ff67d9f36b67 143
<> 157:ff67d9f36b67 144 // Can check against this because it starts at bit 0 in the register
<> 157:ff67d9f36b67 145 // Need to check because too large of a value messes with the upper bits in
<> 157:ff67d9f36b67 146 // the trim register.
<> 157:ff67d9f36b67 147 if (trim > MXC_F_RTC_TRIM_VALUE_TRIM_VALUE)
<> 157:ff67d9f36b67 148 return E_INVALID;
<> 157:ff67d9f36b67 149
<> 157:ff67d9f36b67 150 // write the trim to the hardware trim_value register
<> 157:ff67d9f36b67 151 MXC_RTCTMR->trim_value = (trim << MXC_F_RTC_TRIM_VALUE_TRIM_VALUE_POS) & MXC_F_RTC_TRIM_VALUE_TRIM_VALUE;
<> 157:ff67d9f36b67 152
<> 157:ff67d9f36b67 153 if(trimSlow)
<> 157:ff67d9f36b67 154 MXC_RTCTMR->trim_value |= MXC_F_RTC_TRIM_VALUE_TRIM_SLOWER_CONTROL;
<> 157:ff67d9f36b67 155 else
<> 157:ff67d9f36b67 156 MXC_RTCTMR->trim_value &= ~MXC_F_RTC_TRIM_VALUE_TRIM_SLOWER_CONTROL;
<> 157:ff67d9f36b67 157
<> 157:ff67d9f36b67 158 //wait for pending actions to complete
<> 157:ff67d9f36b67 159 while(MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING);
<> 157:ff67d9f36b67 160
<> 157:ff67d9f36b67 161 return E_NO_ERROR;
<> 157:ff67d9f36b67 162 }
<> 157:ff67d9f36b67 163
<> 157:ff67d9f36b67 164 /* ************************************************************************* */
<> 157:ff67d9f36b67 165 uint32_t RTC_GetTrim()
<> 157:ff67d9f36b67 166 {
<> 157:ff67d9f36b67 167 return MXC_RTCTMR->trim_value; // return the register value for trim
<> 157:ff67d9f36b67 168 }
<> 157:ff67d9f36b67 169
<> 157:ff67d9f36b67 170 /* ************************************************************************* */
<> 157:ff67d9f36b67 171 int RTC_TrimEnable(void)
<> 157:ff67d9f36b67 172 {
<> 157:ff67d9f36b67 173 // make sure rtc is disabled
<> 157:ff67d9f36b67 174 if(MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_ENABLE)
<> 157:ff67d9f36b67 175 return E_BAD_STATE; // RTC is active, bad state
<> 157:ff67d9f36b67 176
<> 157:ff67d9f36b67 177 MXC_RTCTMR->trim_ctrl = MXC_F_RTC_TRIM_CTRL_TRIM_ENABLE_R;
<> 157:ff67d9f36b67 178
<> 157:ff67d9f36b67 179 //wait for pending actions to complete
<> 157:ff67d9f36b67 180 while(MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING);
<> 157:ff67d9f36b67 181
<> 157:ff67d9f36b67 182 return E_NO_ERROR;
<> 157:ff67d9f36b67 183 }
<> 157:ff67d9f36b67 184
<> 157:ff67d9f36b67 185 /* ************************************************************************* */
<> 157:ff67d9f36b67 186 void RTC_TrimDisable(void)
<> 157:ff67d9f36b67 187 {
<> 157:ff67d9f36b67 188 // clear the trim enable bit
<> 157:ff67d9f36b67 189 MXC_RTCTMR->trim_ctrl &= ~MXC_F_RTC_TRIM_CTRL_TRIM_ENABLE_R;
<> 157:ff67d9f36b67 190
<> 157:ff67d9f36b67 191 //wait for pending actions to complete
<> 157:ff67d9f36b67 192 while(MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING);
<> 157:ff67d9f36b67 193
<> 157:ff67d9f36b67 194 return;
<> 157:ff67d9f36b67 195 }
<> 157:ff67d9f36b67 196
<> 157:ff67d9f36b67 197 /**@} end of ingroup rtc*/