The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Wed Feb 20 20:53:29 2019 +0000
Revision:
172:65be27845400
Parent:
171:3a7713b1edbc
mbed library release version 165

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Anna Bridge 169:a7c7b631e539 1 /*******************************************************************************
Anna Bridge 169:a7c7b631e539 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
Anna Bridge 169:a7c7b631e539 3 *
Anna Bridge 169:a7c7b631e539 4 * Permission is hereby granted, free of charge, to any person obtaining a
Anna Bridge 169:a7c7b631e539 5 * copy of this software and associated documentation files (the "Software"),
Anna Bridge 169:a7c7b631e539 6 * to deal in the Software without restriction, including without limitation
Anna Bridge 169:a7c7b631e539 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
Anna Bridge 169:a7c7b631e539 8 * and/or sell copies of the Software, and to permit persons to whom the
Anna Bridge 169:a7c7b631e539 9 * Software is furnished to do so, subject to the following conditions:
Anna Bridge 169:a7c7b631e539 10 *
Anna Bridge 169:a7c7b631e539 11 * The above copyright notice and this permission notice shall be included
Anna Bridge 169:a7c7b631e539 12 * in all copies or substantial portions of the Software.
Anna Bridge 169:a7c7b631e539 13 *
Anna Bridge 169:a7c7b631e539 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
Anna Bridge 169:a7c7b631e539 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Anna Bridge 169:a7c7b631e539 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
Anna Bridge 169:a7c7b631e539 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
Anna Bridge 169:a7c7b631e539 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
Anna Bridge 169:a7c7b631e539 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
Anna Bridge 169:a7c7b631e539 20 * OTHER DEALINGS IN THE SOFTWARE.
Anna Bridge 169:a7c7b631e539 21 *
Anna Bridge 169:a7c7b631e539 22 * Except as contained in this notice, the name of Maxim Integrated
Anna Bridge 169:a7c7b631e539 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
Anna Bridge 169:a7c7b631e539 24 * Products, Inc. Branding Policy.
Anna Bridge 169:a7c7b631e539 25 *
Anna Bridge 169:a7c7b631e539 26 * The mere transfer of this software does not imply any licenses
Anna Bridge 169:a7c7b631e539 27 * of trade secrets, proprietary technology, copyrights, patents,
Anna Bridge 169:a7c7b631e539 28 * trademarks, maskwork rights, or any other form of intellectual
Anna Bridge 169:a7c7b631e539 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
Anna Bridge 169:a7c7b631e539 30 * ownership rights.
Anna Bridge 169:a7c7b631e539 31 *
Anna Bridge 169:a7c7b631e539 32 * $Date: 2016-03-21 10:05:23 -0500 (Mon, 21 Mar 2016) $
Anna Bridge 169:a7c7b631e539 33 * $Revision: 22008 $
Anna Bridge 169:a7c7b631e539 34 *
Anna Bridge 169:a7c7b631e539 35 ******************************************************************************/
Anna Bridge 169:a7c7b631e539 36
Anna Bridge 169:a7c7b631e539 37 /**
Anna Bridge 169:a7c7b631e539 38 * @file rtc.h
Anna Bridge 169:a7c7b631e539 39 * @addtogroup rtc Real-time Clock
Anna Bridge 169:a7c7b631e539 40 * @{
Anna Bridge 169:a7c7b631e539 41 * @brief This is the high level API for the real-time clock module
Anna Bridge 169:a7c7b631e539 42 */
Anna Bridge 169:a7c7b631e539 43
Anna Bridge 169:a7c7b631e539 44 #ifndef _RTC_H
Anna Bridge 169:a7c7b631e539 45 #define _RTC_H
Anna Bridge 169:a7c7b631e539 46
Anna Bridge 169:a7c7b631e539 47 #include "mxc_config.h"
Anna Bridge 169:a7c7b631e539 48 #include "rtc_regs.h"
Anna Bridge 169:a7c7b631e539 49
Anna Bridge 169:a7c7b631e539 50 #ifdef __cplusplus
Anna Bridge 169:a7c7b631e539 51 extern "C" {
Anna Bridge 169:a7c7b631e539 52 #endif
Anna Bridge 169:a7c7b631e539 53
Anna Bridge 169:a7c7b631e539 54 /// @enum rtc_prescale_t Defines clock divider for 4096Hz input clock.
Anna Bridge 169:a7c7b631e539 55 typedef enum {
Anna Bridge 169:a7c7b631e539 56 RTC_PRESCALE_DIV_2_0 = MXC_V_RTC_PRESCALE_DIV_2_0, ///< (4.096kHz) divide input clock by \f$ 2^{0} = 1 \f$ (0x001)
Anna Bridge 169:a7c7b631e539 57 RTC_PRESCALE_DIV_2_1 = MXC_V_RTC_PRESCALE_DIV_2_1, ///< (2.048kHz) divide input clock by \f$ 2^{1} = 2 \f$ (0x002)
Anna Bridge 169:a7c7b631e539 58 RTC_PRESCALE_DIV_2_2 = MXC_V_RTC_PRESCALE_DIV_2_2, ///< (1.024kHz) divide input clock by \f$ 2^{2} = 4 \f$
Anna Bridge 169:a7c7b631e539 59 RTC_PRESCALE_DIV_2_3 = MXC_V_RTC_PRESCALE_DIV_2_3, ///< (512Hz) divide input clock by \f$ 2^{3} = 8 \f$
Anna Bridge 169:a7c7b631e539 60 RTC_PRESCALE_DIV_2_4 = MXC_V_RTC_PRESCALE_DIV_2_4, ///< (256Hz) divide input clock by \f$ 2^{4} = 16 \f$
Anna Bridge 169:a7c7b631e539 61 RTC_PRESCALE_DIV_2_5 = MXC_V_RTC_PRESCALE_DIV_2_5, ///< (128Hz) divide input clock by \f$ 2^{5} = 32 \f$
Anna Bridge 169:a7c7b631e539 62 RTC_PRESCALE_DIV_2_6 = MXC_V_RTC_PRESCALE_DIV_2_6, ///< (64Hz) divide input clock by \f$ 2^{6} = 64 \f$
Anna Bridge 169:a7c7b631e539 63 RTC_PRESCALE_DIV_2_7 = MXC_V_RTC_PRESCALE_DIV_2_7, ///< (32Hz) divide input clock by \f$ 2^{7} = 128 \f$
Anna Bridge 169:a7c7b631e539 64 RTC_PRESCALE_DIV_2_8 = MXC_V_RTC_PRESCALE_DIV_2_8, ///< (16Hz) divide input clock by \f$ 2^{8} = 256 \f$
Anna Bridge 169:a7c7b631e539 65 RTC_PRESCALE_DIV_2_9 = MXC_V_RTC_PRESCALE_DIV_2_9, ///< (8Hz) divide input clock by \f$ 2^{9} = 512 \f$
Anna Bridge 169:a7c7b631e539 66 RTC_PRESCALE_DIV_2_10 = MXC_V_RTC_PRESCALE_DIV_2_10, ///< (4Hz) divide input clock by \f$ 2^{10} = 1024 \f$
Anna Bridge 169:a7c7b631e539 67 RTC_PRESCALE_DIV_2_11 = MXC_V_RTC_PRESCALE_DIV_2_11, ///< (2Hz) divide input clock by \f$ 2^{11} = 2048 \f$ (0x07FF)
Anna Bridge 169:a7c7b631e539 68 RTC_PRESCALE_DIV_2_12 = MXC_V_RTC_PRESCALE_DIV_2_12, ///< (1Hz) divide input clock by \f$ 2^{12} = 4096 \f$ (0x0FFF)
Anna Bridge 169:a7c7b631e539 69 } rtc_prescale_t;
Anna Bridge 169:a7c7b631e539 70
Anna Bridge 169:a7c7b631e539 71 /// @def RTC_CTRL_ACTIVE_TRANS Active Transaction Flags for the RTC
Anna Bridge 169:a7c7b631e539 72 #define RTC_CTRL_ACTIVE_TRANS (MXC_F_RTC_CTRL_RTC_ENABLE_ACTIVE | \
Anna Bridge 169:a7c7b631e539 73 MXC_F_RTC_CTRL_OSC_GOTO_LOW_ACTIVE | \
Anna Bridge 169:a7c7b631e539 74 MXC_F_RTC_CTRL_OSC_FRCE_SM_EN_ACTIVE | \
Anna Bridge 169:a7c7b631e539 75 MXC_F_RTC_CTRL_OSC_FRCE_ST_ACTIVE | \
Anna Bridge 169:a7c7b631e539 76 MXC_F_RTC_CTRL_RTC_SET_ACTIVE | \
Anna Bridge 169:a7c7b631e539 77 MXC_F_RTC_CTRL_RTC_CLR_ACTIVE | \
Anna Bridge 169:a7c7b631e539 78 MXC_F_RTC_CTRL_ROLLOVER_CLR_ACTIVE | \
Anna Bridge 169:a7c7b631e539 79 MXC_F_RTC_CTRL_PRESCALE_CMPR0_ACTIVE | \
Anna Bridge 169:a7c7b631e539 80 MXC_F_RTC_CTRL_PRESCALE_UPDATE_ACTIVE | \
Anna Bridge 169:a7c7b631e539 81 MXC_F_RTC_CTRL_CMPR1_CLR_ACTIVE | \
Anna Bridge 169:a7c7b631e539 82 MXC_F_RTC_CTRL_CMPR0_CLR_ACTIVE | \
Anna Bridge 169:a7c7b631e539 83 MXC_F_RTC_CTRL_TRIM_ENABLE_ACTIVE | \
Anna Bridge 169:a7c7b631e539 84 MXC_F_RTC_CTRL_TRIM_SLOWER_ACTIVE | \
Anna Bridge 169:a7c7b631e539 85 MXC_F_RTC_CTRL_TRIM_CLR_ACTIVE | \
Anna Bridge 169:a7c7b631e539 86 MXC_F_RTC_CTRL_ACTIVE_TRANS_0)
Anna Bridge 169:a7c7b631e539 87
Anna Bridge 169:a7c7b631e539 88 /// @def RTC_FLAGS_CLEAR_ALL Number of RTC compare registers
Anna Bridge 169:a7c7b631e539 89 #define RTC_FLAGS_CLEAR_ALL (MXC_F_RTC_FLAGS_COMP0 | \
Anna Bridge 169:a7c7b631e539 90 MXC_F_RTC_FLAGS_COMP1| \
Anna Bridge 169:a7c7b631e539 91 MXC_F_RTC_FLAGS_PRESCALE_COMP | \
Anna Bridge 169:a7c7b631e539 92 MXC_F_RTC_FLAGS_OVERFLOW | \
Anna Bridge 169:a7c7b631e539 93 MXC_F_RTC_FLAGS_TRIM)
Anna Bridge 169:a7c7b631e539 94 /// @enum rtc_snooze_t Defines the snooze modes
Anna Bridge 169:a7c7b631e539 95 typedef enum {
Anna Bridge 169:a7c7b631e539 96 RTC_SNOOZE_DISABLE = MXC_V_RTC_CTRL_SNOOZE_DISABLE, ///< Snooze Mode Disabled
Anna Bridge 169:a7c7b631e539 97 RTC_SNOOZE_MODE_A = MXC_V_RTC_CTRL_SNOOZE_MODE_A, ///< COMP1 = COMP1 + RTC_SNZ_VALUE when snooze flag is set
Anna Bridge 169:a7c7b631e539 98 RTC_SNOOZE_MODE_B = MXC_V_RTC_CTRL_SNOOZE_MODE_B, ///< COMP1 = RTC_TIMER + RTC_SNZ_VALUE when snooze flag is set
Anna Bridge 169:a7c7b631e539 99 } rtc_snooze_t; /// Defines the snooze modes
Anna Bridge 169:a7c7b631e539 100
Anna Bridge 169:a7c7b631e539 101 /// @def RTC_NUM_COMPARE Number of RTC compare registers
Anna Bridge 169:a7c7b631e539 102 #define RTC_NUM_COMPARE 2
Anna Bridge 169:a7c7b631e539 103
Anna Bridge 169:a7c7b631e539 104 /// @brief A structure that represents the configuration of the RTC peripheral
Anna Bridge 169:a7c7b631e539 105 typedef struct {
Anna Bridge 169:a7c7b631e539 106 rtc_prescale_t prescaler; /// prescale value rtc_prescale_t
Anna Bridge 169:a7c7b631e539 107 rtc_prescale_t prescalerMask; /// Mask value used to compare to the rtc prescale value, when the \f$ \big((Count_{prescaler}\,\&\,Prescale\,Mask) == 0\big) \f$, the prescale compare flag will be set.
Anna Bridge 169:a7c7b631e539 108 uint32_t compareCount[RTC_NUM_COMPARE]; /// Values used for the RTC alarms. See RTC_SetCompare() and RTC_GetCompare()
Anna Bridge 169:a7c7b631e539 109 uint32_t snoozeCount; /// The number of RTC ticks to snooze if enabled.
Anna Bridge 169:a7c7b631e539 110 rtc_snooze_t snoozeMode; /// The desired snooze mode
Anna Bridge 169:a7c7b631e539 111 } rtc_cfg_t;
Anna Bridge 169:a7c7b631e539 112
Anna Bridge 169:a7c7b631e539 113 /**
Anna Bridge 169:a7c7b631e539 114 * @brief Initializes the RTC
Anna Bridge 169:a7c7b631e539 115 * @note Must setup clocking and power prior to this function.
Anna Bridge 169:a7c7b631e539 116 *
Anna Bridge 169:a7c7b631e539 117 * @param cfg configuration
Anna Bridge 169:a7c7b631e539 118 *
Anna Bridge 169:a7c7b631e539 119 * @retval E_NO_ERROR if everything is successful
Anna Bridge 169:a7c7b631e539 120 * @retval E_NULL_PTR if cfg pointer is NULL
Anna Bridge 169:a7c7b631e539 121 * @retval E_INVALID if comparison index, prescaler mask or snooze mask are
Anna Bridge 169:a7c7b631e539 122 * out of bounds
Anna Bridge 169:a7c7b631e539 123 */
Anna Bridge 169:a7c7b631e539 124 int RTC_Init(const rtc_cfg_t *cfg);
Anna Bridge 169:a7c7b631e539 125
Anna Bridge 169:a7c7b631e539 126 /**
Anna Bridge 169:a7c7b631e539 127 * @brief Enable and start the real-time clock continuing from its current value
Anna Bridge 169:a7c7b631e539 128 */
Anna Bridge 169:a7c7b631e539 129 __STATIC_INLINE void RTC_Start(void)
Anna Bridge 169:a7c7b631e539 130 {
Anna Bridge 169:a7c7b631e539 131 MXC_RTCTMR->ctrl |= MXC_F_RTC_CTRL_ENABLE;
Anna Bridge 169:a7c7b631e539 132
Anna Bridge 169:a7c7b631e539 133 //wait for pending actions to complete
Anna Bridge 169:a7c7b631e539 134 while(MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING);
Anna Bridge 169:a7c7b631e539 135 }
Anna Bridge 169:a7c7b631e539 136
Anna Bridge 169:a7c7b631e539 137 /**
Anna Bridge 169:a7c7b631e539 138 * @brief Disable and stop the real-time clock
Anna Bridge 169:a7c7b631e539 139 */
Anna Bridge 169:a7c7b631e539 140 __STATIC_INLINE void RTC_Stop(void)
Anna Bridge 169:a7c7b631e539 141 {
Anna Bridge 169:a7c7b631e539 142 MXC_RTCTMR->ctrl &= ~(MXC_F_RTC_CTRL_ENABLE);
Anna Bridge 169:a7c7b631e539 143
Anna Bridge 169:a7c7b631e539 144 //wait for pending actions to complete
Anna Bridge 169:a7c7b631e539 145 while(MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING);
Anna Bridge 169:a7c7b631e539 146 }
Anna Bridge 169:a7c7b631e539 147
Anna Bridge 169:a7c7b631e539 148 /**
Anna Bridge 169:a7c7b631e539 149 * @brief Determines if the RTC is running or not.
Anna Bridge 169:a7c7b631e539 150 *
Anna Bridge 169:a7c7b631e539 151 * @retval 0 if Disabled, Non-zero if Active
Anna Bridge 169:a7c7b631e539 152 */
Anna Bridge 169:a7c7b631e539 153 __STATIC_INLINE uint32_t RTC_IsActive(void)
Anna Bridge 169:a7c7b631e539 154 {
Anna Bridge 169:a7c7b631e539 155 return (MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_ENABLE);
Anna Bridge 169:a7c7b631e539 156 }
Anna Bridge 169:a7c7b631e539 157
Anna Bridge 169:a7c7b631e539 158 /**
Anna Bridge 169:a7c7b631e539 159 * @brief Set the current count of the RTC
Anna Bridge 169:a7c7b631e539 160 *
Anna Bridge 169:a7c7b631e539 161 * @param count count value to set current real-time count.
Anna Bridge 169:a7c7b631e539 162 */
Anna Bridge 169:a7c7b631e539 163 __STATIC_INLINE void RTC_SetCount(uint32_t count)
Anna Bridge 169:a7c7b631e539 164 {
Anna Bridge 169:a7c7b631e539 165 MXC_RTCTMR->timer = count;
Anna Bridge 169:a7c7b631e539 166
Anna Bridge 169:a7c7b631e539 167 //wait for pending actions to complete
Anna Bridge 169:a7c7b631e539 168 while(MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING);
Anna Bridge 169:a7c7b631e539 169 }
Anna Bridge 169:a7c7b631e539 170
Anna Bridge 169:a7c7b631e539 171 /**
Anna Bridge 169:a7c7b631e539 172 * @brief Get the current timer value of the RTC.
Anna Bridge 169:a7c7b631e539 173 *
Anna Bridge 169:a7c7b631e539 174 * @retval The value of the RTC counter.
Anna Bridge 169:a7c7b631e539 175 */
Anna Bridge 169:a7c7b631e539 176 __STATIC_INLINE uint32_t RTC_GetCount(void)
Anna Bridge 169:a7c7b631e539 177 {
Anna Bridge 169:a7c7b631e539 178 return (MXC_RTCTMR->timer);
Anna Bridge 169:a7c7b631e539 179 }
Anna Bridge 169:a7c7b631e539 180
Anna Bridge 169:a7c7b631e539 181 /**
Anna Bridge 169:a7c7b631e539 182 * @brief Set the comparator value
Anna Bridge 169:a7c7b631e539 183 *
Anna Bridge 169:a7c7b631e539 184 * @param compareIndex Index of comparator to set, see RTC_NUM_COMPARE
Anna Bridge 169:a7c7b631e539 185 * for the total number of compare registers available.
Anna Bridge 169:a7c7b631e539 186 * @param counts Unsigned 32-bit compare value to set.
Anna Bridge 169:a7c7b631e539 187 * @retval E_NO_ERROR Compare count register set successfully for requested
Anna Bridge 169:a7c7b631e539 188 * comparator.
Anna Bridge 169:a7c7b631e539 189 * @retval E_INVALID compareIndex is \>= RTC_NUM_COMPARE.
Anna Bridge 169:a7c7b631e539 190 */
Anna Bridge 169:a7c7b631e539 191 int RTC_SetCompare(uint8_t compareIndex, uint32_t counts);
Anna Bridge 169:a7c7b631e539 192
Anna Bridge 169:a7c7b631e539 193 /**
Anna Bridge 169:a7c7b631e539 194 * @brief Get the comparator value
Anna Bridge 169:a7c7b631e539 195 *
Anna Bridge 169:a7c7b631e539 196 * @param compareIndex Index of the comparator to get. See RTC_NUM_COMPARE
Anna Bridge 169:a7c7b631e539 197 * for the total number of compare registers available.
Anna Bridge 169:a7c7b631e539 198 *
Anna Bridge 169:a7c7b631e539 199 * @retval uint32_t The current value of the specified compare register for the RTC
Anna Bridge 169:a7c7b631e539 200 */
Anna Bridge 169:a7c7b631e539 201 uint32_t RTC_GetCompare(uint8_t compareIndex);
Anna Bridge 169:a7c7b631e539 202
Anna Bridge 169:a7c7b631e539 203 /**
Anna Bridge 169:a7c7b631e539 204 * @brief Set the prescale reload value for the real-time clock.
Anna Bridge 169:a7c7b631e539 205 * @details The prescale reload value determines the number of 4kHz ticks
Anna Bridge 169:a7c7b631e539 206 * occur before the timer is incremented. See @ref prescaler_val "Table"
Anna Bridge 169:a7c7b631e539 207 * for accepted values and corresponding timer resolution.
Anna Bridge 169:a7c7b631e539 208 *
Anna Bridge 169:a7c7b631e539 209 * <table>
Anna Bridge 169:a7c7b631e539 210 * <caption id="prescaler_val">Prescaler Settings and Corresponding RTC Resolutions</caption>
Anna Bridge 169:a7c7b631e539 211 * <tr><th>PRESCALE <th>Prescale Reload <th>4kHz ticks in LSB <th>Min Timer Value (sec) <th> Max Timer Value (sec) <th>Max Timer Value (Days) <th> Max Timer Value (Years)
Anna Bridge 169:a7c7b631e539 212 * <tr><td>0h <td> RTC_PRESCALE_DIV_2_0 <td> 1 <td> 0.00024 <td> 1048576 <td> 12 <td> 0.0
Anna Bridge 169:a7c7b631e539 213 * <tr><td>1h <td> RTC_PRESCALE_DIV_2_1 <td> 2 <td> 0.00049 <td> 2097152 <td> 24 <td> 0.1
Anna Bridge 169:a7c7b631e539 214 * <tr><td>2h <td> RTC_PRESCALE_DIV_2_2 <td> 4 <td> 0.00098 <td> 4194304 <td> 49 <td> 0.1
Anna Bridge 169:a7c7b631e539 215 * <tr><td>3h <td> RTC_PRESCALE_DIV_2_3 <td> 8 <td> 0.00195 <td> 8388608 <td> 97 <td> 0.3
Anna Bridge 169:a7c7b631e539 216 * <tr><td>4h <td> RTC_PRESCALE_DIV_2_4 <td> 16 <td> 0.00391 <td> 16777216 <td> 194 <td> 0.5
Anna Bridge 169:a7c7b631e539 217 * <tr><td>5h <td> RTC_PRESCALE_DIV_2_5 <td> 32 <td> 0.00781 <td> 33554432 <td> 388 <td> 1.1
Anna Bridge 169:a7c7b631e539 218 * <tr><td>6h <td> RTC_PRESCALE_DIV_2_6 <td> 64 <td> 0.01563 <td> 67108864 <td> 777 <td> 2.2
Anna Bridge 169:a7c7b631e539 219 * <tr><td>7h <td> RTC_PRESCALE_DIV_2_7 <td> 128 <td> 0.03125 <td> 134217728 <td> 1553 <td> 4.4
Anna Bridge 169:a7c7b631e539 220 * <tr><td>8h <td> RTC_PRESCALE_DIV_2_8 <td> 256 <td> 0.06250 <td> 268435456 <td> 3107 <td> 8.7
Anna Bridge 169:a7c7b631e539 221 * <tr><td>9h <td> RTC_PRESCALE_DIV_2_9 <td> 512 <td> 0.12500 <td> 536870912 <td> 6214 <td> 17.5
Anna Bridge 169:a7c7b631e539 222 * <tr><td>Ah <td> RTC_PRESCALE_DIV_2_10 <td> 1024 <td> 0.25000 <td> 1073741824 <td> 12428 <td> 34.9
Anna Bridge 169:a7c7b631e539 223 * <tr><td>Bh <td> RTC_PRESCALE_DIV_2_11 <td> 2048 <td> 0.50000 <td> 2147483648 <td> 24855 <td> 69.8
Anna Bridge 169:a7c7b631e539 224 * <tr><td>Ch <td> RTC_PRESCALE_DIV_2_12 <td> 4096 <td> 1.00000 <td> 4294967296 <td> 49710 <td> 139.6
Anna Bridge 169:a7c7b631e539 225 * </table>
Anna Bridge 169:a7c7b631e539 226 *
Anna Bridge 169:a7c7b631e539 227 * @param prescaler Prescale value to set, see rtc_prescale_t.
Anna Bridge 169:a7c7b631e539 228 */
Anna Bridge 169:a7c7b631e539 229 __STATIC_INLINE void RTC_SetPrescaler(rtc_prescale_t prescaler)
Anna Bridge 169:a7c7b631e539 230 {
Anna Bridge 169:a7c7b631e539 231 MXC_RTCTMR->prescale = prescaler;
Anna Bridge 169:a7c7b631e539 232
Anna Bridge 169:a7c7b631e539 233 //wait for pending actions to complete
Anna Bridge 169:a7c7b631e539 234 while(MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING);
Anna Bridge 169:a7c7b631e539 235 }
Anna Bridge 169:a7c7b631e539 236
Anna Bridge 169:a7c7b631e539 237 /**
Anna Bridge 169:a7c7b631e539 238 * @brief Get the current value of the real-time clock prescaler.
Anna Bridge 169:a7c7b631e539 239 *
Anna Bridge 169:a7c7b631e539 240 * @retval rtc_prescale_t Returns the current RTC prescaler setting,
Anna Bridge 169:a7c7b631e539 241 * See rtc_prescale_t for values of the prescaler.
Anna Bridge 169:a7c7b631e539 242 */
Anna Bridge 169:a7c7b631e539 243 __STATIC_INLINE rtc_prescale_t RTC_GetPrescaler(void)
Anna Bridge 169:a7c7b631e539 244 {
Anna Bridge 169:a7c7b631e539 245 return (rtc_prescale_t)(MXC_RTCTMR->prescale);
Anna Bridge 169:a7c7b631e539 246 }
Anna Bridge 169:a7c7b631e539 247
Anna Bridge 169:a7c7b631e539 248 /**
Anna Bridge 169:a7c7b631e539 249 * @brief Set the prescaler mask, which is used to set the RTC prescale counter
Anna Bridge 169:a7c7b631e539 250 * compare flag when the prescaler timer matches the bits indicated
Anna Bridge 169:a7c7b631e539 251 * by the mask.
Anna Bridge 169:a7c7b631e539 252 * @param mask A bit mask that is used to set the prescale compare flag if the
Anna Bridge 169:a7c7b631e539 253 * prescale timer has the corresponding bits set. @note This mask must
Anna Bridge 169:a7c7b631e539 254 * be less than or equal to the prescaler reload value.
Anna Bridge 169:a7c7b631e539 255 * See RTC_SetPrescaler()
Anna Bridge 169:a7c7b631e539 256 * @details When \f$ \big((Count_{prescaler}\,\&\,Prescale\,Mask) == 0\big) \f$, the prescale compare flag is set
Anna Bridge 169:a7c7b631e539 257 * @retval int Returns E_NO_ERROR if prescale value is valid and is set.
Anna Bridge 169:a7c7b631e539 258 * @retval int Returns E_INVALID if mask is \> than prescaler value
Anna Bridge 169:a7c7b631e539 259 */
Anna Bridge 169:a7c7b631e539 260 __STATIC_INLINE int RTC_SetPrescalerMask(rtc_prescale_t mask)
Anna Bridge 169:a7c7b631e539 261 {
Anna Bridge 169:a7c7b631e539 262 if (mask > ((rtc_prescale_t)(MXC_RTCTMR->prescale))) {
Anna Bridge 169:a7c7b631e539 263 return E_INVALID;
Anna Bridge 169:a7c7b631e539 264 }
Anna Bridge 169:a7c7b631e539 265 MXC_RTCTMR->prescale_mask = mask;
Anna Bridge 169:a7c7b631e539 266
Anna Bridge 169:a7c7b631e539 267 //wait for pending actions to complete
Anna Bridge 169:a7c7b631e539 268 while(MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING);
Anna Bridge 169:a7c7b631e539 269 return E_NO_ERROR;
Anna Bridge 169:a7c7b631e539 270 }
Anna Bridge 169:a7c7b631e539 271
Anna Bridge 169:a7c7b631e539 272 /**
Anna Bridge 169:a7c7b631e539 273 * @brief Set the number of ticks for snooze mode. See RTC_Snooze().
Anna Bridge 169:a7c7b631e539 274 * @param count Sets the count used for snoozing when snooze mode is enabled and
Anna Bridge 169:a7c7b631e539 275 * the snooze flag is set.
Anna Bridge 169:a7c7b631e539 276 * @retval E_NO_ERROR If snooze value is set correctly and value is valid.
Anna Bridge 169:a7c7b631e539 277 * @retval E_INVALID If SnoozeCount exceeds maximum supported, see MXC_F_RTC_SNZ_VAL_VALUE
Anna Bridge 169:a7c7b631e539 278 *
Anna Bridge 169:a7c7b631e539 279 */
Anna Bridge 169:a7c7b631e539 280 __STATIC_INLINE int RTC_SetSnoozeCount(uint32_t count)
Anna Bridge 169:a7c7b631e539 281 {
Anna Bridge 169:a7c7b631e539 282 // Check to make sure max value is not being exceeded
Anna Bridge 169:a7c7b631e539 283 if (count > MXC_F_RTC_SNZ_VAL_VALUE)
Anna Bridge 169:a7c7b631e539 284 return E_INVALID;
Anna Bridge 169:a7c7b631e539 285
Anna Bridge 169:a7c7b631e539 286 MXC_RTCTMR->snz_val = count;
Anna Bridge 169:a7c7b631e539 287
Anna Bridge 169:a7c7b631e539 288 //wait for pending actions to complete
Anna Bridge 169:a7c7b631e539 289 while(MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING);
Anna Bridge 169:a7c7b631e539 290 return E_NO_ERROR;
Anna Bridge 169:a7c7b631e539 291 }
Anna Bridge 169:a7c7b631e539 292
Anna Bridge 169:a7c7b631e539 293 /**
Anna Bridge 169:a7c7b631e539 294 * @brief Gets the Snooze Count that is currently loaded in the RTC timer
Anna Bridge 169:a7c7b631e539 295 * @details Returns the current value for the Snooze. This value is used as
Anna Bridge 169:a7c7b631e539 296 * part of the snooze calculation depending on the snooze mode. @see RTC_SetSnoozeMode
Anna Bridge 169:a7c7b631e539 297 * @retval uint32_t value of the snooze register
Anna Bridge 169:a7c7b631e539 298 *
Anna Bridge 169:a7c7b631e539 299 */
Anna Bridge 169:a7c7b631e539 300 __STATIC_INLINE uint32_t RTC_GetSnoozeCount(void)
Anna Bridge 169:a7c7b631e539 301 {
Anna Bridge 169:a7c7b631e539 302 return MXC_RTCTMR->snz_val;
Anna Bridge 169:a7c7b631e539 303 }
Anna Bridge 169:a7c7b631e539 304
Anna Bridge 169:a7c7b631e539 305 /**
Anna Bridge 169:a7c7b631e539 306 * @brief Set the flags to activate the snooze
Anna Bridge 169:a7c7b631e539 307 * @details Begins a snooze of the RTC. When this function is called
Anna Bridge 169:a7c7b631e539 308 * the snooze count is determined based on the snooze mode.
Anna Bridge 169:a7c7b631e539 309 * See RTC_GetCount() and RTC_SetSnoozeMode()
Anna Bridge 169:a7c7b631e539 310 */
Anna Bridge 169:a7c7b631e539 311 __STATIC_INLINE void RTC_Snooze(void)
Anna Bridge 169:a7c7b631e539 312 {
Anna Bridge 169:a7c7b631e539 313 MXC_RTCTMR->flags = MXC_F_RTC_FLAGS_SNOOZE_A | MXC_F_RTC_FLAGS_SNOOZE_B;
Anna Bridge 169:a7c7b631e539 314
Anna Bridge 169:a7c7b631e539 315 //wait for pending actions to complete
Anna Bridge 169:a7c7b631e539 316 while(MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING);
Anna Bridge 169:a7c7b631e539 317 }
Anna Bridge 169:a7c7b631e539 318
Anna Bridge 169:a7c7b631e539 319 /**
Anna Bridge 169:a7c7b631e539 320 * @brief Sets the Snooze Mode.
Anna Bridge 169:a7c7b631e539 321 * @details <table>
Anna Bridge 169:a7c7b631e539 322 * <caption id="snoozeModesTable">Snooze Modes</caption>
Anna Bridge 169:a7c7b631e539 323 * <tr><th>Mode<th>Snooze Time Calculation
Anna Bridge 169:a7c7b631e539 324 * <tr><td>RTC_SNOOZE_DISABLE<td>Snooze Disabled
Anna Bridge 169:a7c7b631e539 325 * <tr><td>RTC_SNOOZE_MODE_A<td>\f$ compare1 = compare1 + snoozeCount \f$
Anna Bridge 169:a7c7b631e539 326 * <tr><td>RTC_SNOOZE_MODE_B<td>\f$ compare1 = count + snoozeCount \f$
Anna Bridge 169:a7c7b631e539 327 * </table>
Anna Bridge 169:a7c7b631e539 328 * \a count is the value of the RTC counter when RTC_Snooze() is called to begin snooze
Anna Bridge 169:a7c7b631e539 329 */
Anna Bridge 169:a7c7b631e539 330 __STATIC_INLINE void RTC_SetSnoozeMode(rtc_snooze_t mode)
Anna Bridge 169:a7c7b631e539 331 {
Anna Bridge 169:a7c7b631e539 332 uint32_t ctrl;
Anna Bridge 169:a7c7b631e539 333 // Get the control register and mask off the non-snooze bits
Anna Bridge 169:a7c7b631e539 334 ctrl = (MXC_RTCTMR->ctrl & ~(MXC_F_RTC_CTRL_SNOOZE_ENABLE));
Anna Bridge 169:a7c7b631e539 335 // set the requested snooze mode bits and save the settings
Anna Bridge 169:a7c7b631e539 336 MXC_RTCTMR->ctrl = (ctrl | (mode << MXC_F_RTC_CTRL_SNOOZE_ENABLE_POS));
Anna Bridge 169:a7c7b631e539 337
Anna Bridge 169:a7c7b631e539 338 //wait for pending actions to complete
Anna Bridge 169:a7c7b631e539 339 while(MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING);
Anna Bridge 169:a7c7b631e539 340 }
Anna Bridge 169:a7c7b631e539 341
Anna Bridge 169:a7c7b631e539 342 /**
Anna Bridge 169:a7c7b631e539 343 * @brief Enables the interrupts defined by the mask for the RTC.
Anna Bridge 169:a7c7b631e539 344 * @details <table>
Anna Bridge 169:a7c7b631e539 345 * <caption id="RTC_interrupts">RTC Interrupts</caption>
Anna Bridge 169:a7c7b631e539 346 * <tr><th>Interrupt<th>Mask
Anna Bridge 169:a7c7b631e539 347 * <tr><td>Compare 0<td>MXC_F_RTC_INTEN_COMP0
Anna Bridge 169:a7c7b631e539 348 * <tr><td>Compare 1 \\ Snooze<td>MXC_F_RTC_INTEN_COMP1
Anna Bridge 169:a7c7b631e539 349 * <tr><td>Prescale Comp<td>MXC_F_RTC_FLAGS_INTEN_COMP
Anna Bridge 169:a7c7b631e539 350 * <tr><td>RTC Count Overflow<td>MXC_F_RTC_INTEN_OVERFLOW
Anna Bridge 169:a7c7b631e539 351 * <tr><td>Trim<td>MXC_F_RTC_INTEN_TRIM
Anna Bridge 169:a7c7b631e539 352 * </table>
Anna Bridge 169:a7c7b631e539 353 * @param mask set the bits of the interrupts to enable
Anna Bridge 169:a7c7b631e539 354 */
Anna Bridge 169:a7c7b631e539 355 __STATIC_INLINE void RTC_EnableINT(uint32_t mask)
Anna Bridge 169:a7c7b631e539 356 {
Anna Bridge 169:a7c7b631e539 357 MXC_RTCTMR->inten |= mask;
Anna Bridge 169:a7c7b631e539 358 }
Anna Bridge 169:a7c7b631e539 359
Anna Bridge 169:a7c7b631e539 360 /**
Anna Bridge 169:a7c7b631e539 361 * @brief Disable RTC interrupts based on the mask, See @ref RTC_interrupts
Anna Bridge 169:a7c7b631e539 362 *
Anna Bridge 169:a7c7b631e539 363 * @param mask set the bits of the interrupts to disable
Anna Bridge 169:a7c7b631e539 364 */
Anna Bridge 169:a7c7b631e539 365 __STATIC_INLINE void RTC_DisableINT(uint32_t mask)
Anna Bridge 169:a7c7b631e539 366 {
Anna Bridge 169:a7c7b631e539 367 MXC_RTCTMR->inten &= ~mask;
Anna Bridge 169:a7c7b631e539 368 }
Anna Bridge 169:a7c7b631e539 369
Anna Bridge 169:a7c7b631e539 370 /**
Anna Bridge 169:a7c7b631e539 371 * @brief Gets the RTC's interrupt flags
Anna Bridge 169:a7c7b631e539 372 *
Anna Bridge 169:a7c7b631e539 373 * @retval uint32_t interrupt flags
Anna Bridge 169:a7c7b631e539 374 */
Anna Bridge 169:a7c7b631e539 375 __STATIC_INLINE uint32_t RTC_GetFlags(void)
Anna Bridge 169:a7c7b631e539 376 {
Anna Bridge 169:a7c7b631e539 377 return (MXC_RTCTMR->flags);
Anna Bridge 169:a7c7b631e539 378 }
Anna Bridge 169:a7c7b631e539 379
Anna Bridge 169:a7c7b631e539 380 /**
Anna Bridge 169:a7c7b631e539 381 * @brief Clears the RTC's interrupt flags based on the mask provided
Anna Bridge 169:a7c7b631e539 382 *
Anna Bridge 169:a7c7b631e539 383 * @param mask masked used to clear individual interrupt flags
Anna Bridge 169:a7c7b631e539 384 */
Anna Bridge 169:a7c7b631e539 385 __STATIC_INLINE void RTC_ClearFlags(uint32_t mask)
Anna Bridge 169:a7c7b631e539 386 {
Anna Bridge 169:a7c7b631e539 387 MXC_RTCTMR->flags = mask;
Anna Bridge 169:a7c7b631e539 388
Anna Bridge 169:a7c7b631e539 389 //wait for pending actions to complete
Anna Bridge 169:a7c7b631e539 390 while(MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING);
Anna Bridge 169:a7c7b631e539 391 }
Anna Bridge 169:a7c7b631e539 392
Anna Bridge 169:a7c7b631e539 393 /**
Anna Bridge 169:a7c7b631e539 394 * @brief Gets the active transaction flags
Anna Bridge 169:a7c7b631e539 395 *
Anna Bridge 169:a7c7b631e539 396 * @retval 0 = no active transactions , nonzero = active transactions bits
Anna Bridge 169:a7c7b631e539 397 */
Anna Bridge 169:a7c7b631e539 398 __STATIC_INLINE uint32_t RTC_GetActiveTrans(void)
Anna Bridge 169:a7c7b631e539 399 {
Anna Bridge 169:a7c7b631e539 400 return (MXC_RTCTMR->ctrl & RTC_CTRL_ACTIVE_TRANS);
Anna Bridge 169:a7c7b631e539 401 }
Anna Bridge 169:a7c7b631e539 402
Anna Bridge 169:a7c7b631e539 403 /**
Anna Bridge 169:a7c7b631e539 404 * @brief Sets the trim value and trim slow/fast option
Anna Bridge 169:a7c7b631e539 405 * @note Ensure RTC is disabled prior to calling this function
Anna Bridge 169:a7c7b631e539 406 *
Anna Bridge 169:a7c7b631e539 407 * @param trim trim value - maximum trim value setting of 0x03FFFF
Anna Bridge 169:a7c7b631e539 408 * @param trimSlow 1 = trim slow, 0 = trim fast
Anna Bridge 169:a7c7b631e539 409 *
Anna Bridge 169:a7c7b631e539 410 * @retval E_NO_ERROR Trim value is valid and set.
Anna Bridge 169:a7c7b631e539 411 * @retval E_INVALID Trim value exceeds max trim.
Anna Bridge 169:a7c7b631e539 412 * @retval E_BAD_STATE RTC is not disabled.
Anna Bridge 169:a7c7b631e539 413 *
Anna Bridge 169:a7c7b631e539 414 */
Anna Bridge 169:a7c7b631e539 415 int RTC_SetTrim(uint32_t trim, uint8_t trimSlow);
Anna Bridge 169:a7c7b631e539 416
Anna Bridge 169:a7c7b631e539 417 /**
Anna Bridge 169:a7c7b631e539 418 * @brief Gets the trim value currently set
Anna Bridge 169:a7c7b631e539 419 * @note Ensure RTC is disabled prior to calling this function
Anna Bridge 169:a7c7b631e539 420 *
Anna Bridge 169:a7c7b631e539 421 * @retval uint32_t Current trim value of RTC.
Anna Bridge 169:a7c7b631e539 422 */
Anna Bridge 169:a7c7b631e539 423 uint32_t RTC_GetTrim(void);
Anna Bridge 169:a7c7b631e539 424
Anna Bridge 169:a7c7b631e539 425 /**
Anna Bridge 169:a7c7b631e539 426 * @brief Enabled the trim.
Anna Bridge 169:a7c7b631e539 427 * @note Ensure RTC is disabled prior to calling this function
Anna Bridge 169:a7c7b631e539 428 * @retval E_NO_ERROR Trim enabled
Anna Bridge 169:a7c7b631e539 429 * @retval E_INVALID
Anna Bridge 169:a7c7b631e539 430 */
Anna Bridge 169:a7c7b631e539 431 int RTC_TrimEnable(void);
Anna Bridge 169:a7c7b631e539 432
Anna Bridge 169:a7c7b631e539 433 /**
Anna Bridge 169:a7c7b631e539 434 * @brief Disable the trim.
Anna Bridge 169:a7c7b631e539 435 */
Anna Bridge 169:a7c7b631e539 436 void RTC_TrimDisable(void);
Anna Bridge 169:a7c7b631e539 437
Anna Bridge 169:a7c7b631e539 438 /**
Anna Bridge 169:a7c7b631e539 439 * @}
Anna Bridge 169:a7c7b631e539 440 */
Anna Bridge 169:a7c7b631e539 441
Anna Bridge 169:a7c7b631e539 442 #ifdef __cplusplus
Anna Bridge 169:a7c7b631e539 443 }
Anna Bridge 169:a7c7b631e539 444 #endif
Anna Bridge 169:a7c7b631e539 445
Anna Bridge 169:a7c7b631e539 446 #endif /* _RTC_H */