Greg Steiert / pegasus_dev

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

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