apm32f10x_rtc.c 6.1 KB
Newer Older
A
abbcc 已提交
1
/*!
2
 * @file        apm32f10x_rtc.c
A
abbcc 已提交
3
 *
4
 * @brief       This file provides all the RTC firmware functions
A
abbcc 已提交
5
 *
6
 * @version     V1.0.4
A
abbcc 已提交
7
 *
8
 * @date        2022-12-01
A
abbcc 已提交
9
 *
10 11 12 13 14 15 16 17
 * @attention
 *
 *  Copyright (C) 2020-2022 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  The program is only for reference, which is distributed in the hope
18
 *  that it will be useful and instructional for customers to develop
19 20 21 22 23
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
A
abbcc 已提交
24
 */
25

A
abbcc 已提交
26 27
#include "apm32f10x_rtc.h"

28
/** @addtogroup APM32F10x_StdPeriphDriver
A
abbcc 已提交
29 30 31 32
  @{
*/

/** @addtogroup RTC_Driver RTC Driver
33
  * @brief RTC driver modules
A
abbcc 已提交
34 35 36
  @{
*/

37
/** @defgroup RTC_Functions Functions
A
abbcc 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  @{
*/

/*!
 * @brief     Enter RTC configuration mode.
 *
 * @param     None
 *
 * @retval    None
 */
void RTC_EnableConfigMode(void)
{
    RTC->CSTS_B.CFGMFLG = BIT_SET;
}

/*!
 * @brief     Exit RTC configuration mode.
 *
 * @param     None
 *
 * @retval    None
 */
void RTC_DisableConfigMode(void)
{
    RTC->CSTS_B.CFGMFLG = BIT_RESET;
}

/*!
 * @brief     Read the RTC counter value.
 *
 * @param     None
 *
 * @retval    RTC counter value.
 */
uint32_t RTC_ReadCounter(void)
{
74 75 76 77
    uint32_t reg = 0;
    reg = (RTC->CNTH_B.CNTH) << 16;
    reg |= (RTC->CNTL_B.CNTL);
    return (reg);
A
abbcc 已提交
78 79 80
}

/*!
81
 * @brief     Configures the RTC counter value.
A
abbcc 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95
 *
 * @param     value: RTC counter new value.
 *
 * @retval    None
 */
void RTC_ConfigCounter(uint32_t value)
{
    RTC_EnableConfigMode();
    RTC->CNTH_B.CNTH = value >> 16;
    RTC->CNTL_B.CNTL = value & 0x0000FFFF;
    RTC_DisableConfigMode();
}

/*!
96
 * @brief     Configures the RTC prescaler value.
A
abbcc 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110
 *
 * @param     value: RTC prescaler new value.
 *
 * @retval    None
 */
void RTC_ConfigPrescaler(uint32_t value)
{
    RTC_EnableConfigMode();
    RTC->PSCRLDH_B.PSCRLDH = value >> 16;
    RTC->PSCRLDL_B.PSCRLDL = value & 0x0000FFFF;
    RTC_DisableConfigMode();
}

/*!
111
 * @brief     Configures the RTC alarm value.
A
abbcc 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125
 *
 * @param     value: RTC alarm new value.
 *
 * @retval    None
 */
void RTC_ConfigAlarm(uint32_t value)
{
    RTC_EnableConfigMode();
    RTC->ALRH_B.ALRH = value >> 16;
    RTC->ALRL_B.ALRL = value & 0x0000FFFF;
    RTC_DisableConfigMode();
}

/*!
126
 * @brief     Read the RTC divider value.
A
abbcc 已提交
127 128 129 130 131 132 133
 *
 * @param     None
 *
 * @retval    RTC Divider value.
 */
uint32_t RTC_ReadDivider(void)
{
134 135 136 137
    uint32_t reg = 0;
    reg = (RTC->PSCH_B.PSCH & 0x000F) << 16;
    reg |= (RTC->PSCL_B.PSCL);
    return (reg);
A
abbcc 已提交
138 139 140 141 142 143 144 145 146 147 148
}

/*!
 * @brief     Waits until last write operation on RTC registers has finished.
 *
 * @param     None
 *
 * @retval    None
 */
void RTC_WaitForLastTask(void)
{
A
Aligagago 已提交
149
    while (RTC->CSTS_B.OCFLG == BIT_RESET)
A
abbcc 已提交
150 151 152 153 154
    {
    }
}

/*!
155
 * @brief     Waits until the RTC registers Synchronized.
A
abbcc 已提交
156 157 158 159 160
 *
 * @param     None
 *
 * @retval    None
 */
161
void RTC_WaitForSynchro(void)
A
abbcc 已提交
162 163
{
    RTC->CSTS_B.RSYNCFLG = BIT_RESET;
A
Aligagago 已提交
164
    while (RTC->CSTS_B.RSYNCFLG == BIT_RESET);
A
abbcc 已提交
165 166 167 168 169
}

/*!
 * @brief     Enable RTC interrupts.
 *
170 171 172 173 174
 * @param     interrupt: specifies the RTC interrupt sources to be enabled
 *                  This parameter can be any combination of the following values:
 *                  @arg RTC_INT_OVR : Overflow interrupt
 *                  @arg RTC_INT_ALR : Alarm interrupt
 *                  @arg RTC_INT_SEC : Second interrupt
A
abbcc 已提交
175 176 177 178 179 180 181 182 183
 */
void RTC_EnableInterrupt(uint16_t interrupt)
{
    RTC->CTRL |= interrupt;
}

/*!
 * @brief     Disable RTC interrupts.
 *
184 185 186 187 188
 * @param     interrupt: specifies the RTC interrupt sources to be disabled
 *                  This parameter can be any combination of the following values:
 *                  @arg RTC_INT_OVR : Overflow interrupt
 *                  @arg RTC_INT_ALR : Alarm interrupt
 *                  @arg RTC_INT_SEC : Second interrupt
A
abbcc 已提交
189 190 191 192 193
 *
 * @retval    None
 */
void RTC_DisableInterrupt(uint16_t interrupt)
{
A
Aligagago 已提交
194
    RTC->CTRL &= (uint32_t)~interrupt;
A
abbcc 已提交
195 196 197 198 199
}

/*!
 * @brief     Read flag bit
 *
200 201 202 203 204 205 206
 * @param     flag: specifies the flag to check.
 *                  This parameter can be one of the following values:
 *                  @arg RTC_FLAG_OC   : RTC Operation Complete flag
 *                  @arg RTC_FLAG_RSYNC: Registers Synchronized flag
 *                  @arg RTC_FLAG_OVR  : Overflow flag
 *                  @arg RTC_FLAG_ALR  : Alarm flag
 *                  @arg RTC_FLAG_SEC  : Second flag
A
abbcc 已提交
207
 *
208
 * @retval    SET or RESET
A
abbcc 已提交
209 210 211
 */
uint8_t RTC_ReadStatusFlag(RTC_FLAG_T flag)
{
A
Aligagago 已提交
212
    return (RTC->CSTS & flag) ? SET : RESET;
A
abbcc 已提交
213 214 215 216 217
}

/*!
 * @brief     Clear flag bit
 *
218 219 220 221 222
 * @param     flag: specifies the flag to clear.
 *                  This parameter can be any combination of the following values:
 *                  @arg RTC_FLAG_OVR : Overflow flag
 *                  @arg RTC_FLAG_ALR : Alarm flag
 *                  @arg RTC_FLAG_SEC : Second flag
A
abbcc 已提交
223 224 225 226 227 228 229 230 231 232 233
 *
 * @retval    None
 */
void RTC_ClearStatusFlag(uint16_t flag)
{
    RTC->CSTS &= (uint32_t)~flag;
}

/*!
 * @brief     Read interrupt flag bit is set
 *
234 235 236 237 238
 * @param     flag: specifies the flag to check.
 *                  This parameter can be any combination of the following values:
 *                  @arg RTC_INT_OVR : Overflow interrupt
 *                  @arg RTC_INT_ALR : Alarm interrupt
 *                  @arg RTC_INT_SEC : Second interrupt
A
abbcc 已提交
239 240 241 242 243 244 245 246 247 248 249
 *
 * @retval    None
 */
uint8_t RTC_ReadIntFlag(RTC_INT_T flag)
{
    return (RTC->CSTS & flag) ? SET : RESET;
}

/*!
 * @brief     Clear RTC interrupt flag bit
 *
250 251 252 253 254
 * @param     flag: specifies the flag to clear.
 *                  This parameter can be one of the following values:
 *                  @arg RTC_INT_OVR : Overflow interrupt
 *                  @arg RTC_INT_ALR : Alarm interrupt
 *                  @arg RTC_INT_SEC : Second interrupt
A
abbcc 已提交
255 256 257 258 259 260 261 262
 *
 * @retval    None
 */
void RTC_ClearIntFlag(uint16_t flag)
{
    RTC->CSTS &= (uint32_t)~flag;
}

263 264 265
/**@} end of group RTC_Functions */
/**@} end of group RTC_Driver */
/**@} end of group APM32F10x_StdPeriphDriver */