t

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Tue Nov 08 17:45:16 2016 +0000
Revision:
150:02e0a0aed4ec
This updates the lib to the mbed lib v129

Who changed what in which revision?

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