1 /*!
2     \file    gd32a50x_rtc.h
3     \brief   definitions for the RTC
4 
5     \version 2022-01-30, V1.0.0, firmware for GD32A50x
6 */
7 
8 /*
9     Copyright (c) 2022, GigaDevice Semiconductor Inc.
10 
11     Redistribution and use in source and binary forms, with or without modification,
12 are permitted provided that the following conditions are met:
13 
14     1. Redistributions of source code must retain the above copyright notice, this
15        list of conditions and the following disclaimer.
16     2. Redistributions in binary form must reproduce the above copyright notice,
17        this list of conditions and the following disclaimer in the documentation
18        and/or other materials provided with the distribution.
19     3. Neither the name of the copyright holder nor the names of its contributors
20        may be used to endorse or promote products derived from this software without
21        specific prior written permission.
22 
23     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 OF SUCH DAMAGE.
33 */
34 
35 #ifndef GD32A50X_RTC_H
36 #define GD32A50X_RTC_H
37 
38 #include "gd32a50x.h"
39 
40 /* RTC definitions */
41 #define RTC                          RTC_BASE
42 
43 /* registers definitions */
44 #define RTC_INTEN                    REG32(RTC + 0x00U)      /*!< interrupt enable register */
45 #define RTC_CTL                      REG32(RTC + 0x04U)      /*!< control register */
46 #define RTC_PSCH                     REG32(RTC + 0x08U)      /*!< prescaler high register */
47 #define RTC_PSCL                     REG32(RTC + 0x0CU)      /*!< prescaler low register */
48 #define RTC_DIVH                     REG32(RTC + 0x10U)      /*!< divider high register */
49 #define RTC_DIVL                     REG32(RTC + 0x14U)      /*!< divider low register */
50 #define RTC_CNTH                     REG32(RTC + 0x18U)      /*!< counter high register */
51 #define RTC_CNTL                     REG32(RTC + 0x1CU)      /*!< counter low register */
52 #define RTC_ALRMH                    REG32(RTC + 0x20U)      /*!< alarm high register */
53 #define RTC_ALRML                    REG32(RTC + 0x24U)      /*!< alarm low register */
54 
55 /* bits definitions */
56 /* RTC_INTEN */
57 #define RTC_INTEN_SCIE               BIT(0)                   /*!< second interrupt enable */
58 #define RTC_INTEN_ALRMIE             BIT(1)                   /*!< alarm interrupt enable */
59 #define RTC_INTEN_OVIE               BIT(2)                   /*!< overflow interrupt enable */
60 
61 /* RTC_CTL */
62 #define RTC_CTL_SCIF                 BIT(0)                   /*!< second interrupt flag */
63 #define RTC_CTL_ALRMIF               BIT(1)                   /*!< alarm interrupt flag */
64 #define RTC_CTL_OVIF                 BIT(2)                   /*!< overflow interrupt flag */
65 #define RTC_CTL_RSYNF                BIT(3)                   /*!< registers synchronized flag */
66 #define RTC_CTL_CMF                  BIT(4)                   /*!< configuration mode flag */
67 #define RTC_CTL_LWOFF                BIT(5)                   /*!< last write operation finished flag */
68 
69 /* RTC_PSC */
70 #define RTC_PSCH_PSC                 BITS(0, 3)               /*!< prescaler high value */
71 #define RTC_PSCL_PSC                 BITS(0, 15)              /*!< prescaler low value */
72 
73 /* RTC_DIV */
74 #define RTC_DIVH_DIV                 BITS(0, 3)               /*!< divider high value */
75 #define RTC_DIVL_DIV                 BITS(0, 15)              /*!< divider low value */
76 
77 /* RTC_CNT */
78 #define RTC_CNTH_CNT                 BITS(0, 15)              /*!< counter high value */
79 #define RTC_CNTL_CNT                 BITS(0, 15)              /*!< counter low value */
80 
81 /* RTC_ALRM */
82 #define RTC_ALRMH_ALRM               BITS(0, 15)              /*!< alarm high value */
83 #define RTC_ALRML_ALRM               BITS(0, 15)              /*!< alarm low value */
84 
85 /* constants definitions */
86 #define RTC_HIGH_VALUE               0x000F0000U              /*!< RTC high value */
87 #define RTC_LOW_VALUE                0x0000FFFFU              /*!< RTC low value */
88 
89 /* RTC interrupt definitions */
90 #define RTC_INT_SECOND               RTC_INTEN_SCIE           /*!< second interrupt enable */
91 #define RTC_INT_ALARM                RTC_INTEN_ALRMIE         /*!< alarm interrupt enable */
92 #define RTC_INT_OVERFLOW             RTC_INTEN_OVIE           /*!< overflow interrupt enable */
93 
94 /* RTC interrupt flag definitions */
95 #define RTC_INT_FLAG_SECOND          RTC_CTL_SCIF             /*!< second interrupt flag */
96 #define RTC_INT_FLAG_ALARM           RTC_CTL_ALRMIF           /*!< alarm interrupt flag */
97 #define RTC_INT_FLAG_OVERFLOW        RTC_CTL_OVIF             /*!< overflow interrupt flag */
98 
99 /* RTC flag definitions */
100 #define RTC_FLAG_SECOND              RTC_CTL_SCIF             /*!< second interrupt flag */
101 #define RTC_FLAG_ALARM               RTC_CTL_ALRMIF           /*!< alarm interrupt flag */
102 #define RTC_FLAG_OVERFLOW            RTC_CTL_OVIF             /*!< overflow interrupt flag */
103 #define RTC_FLAG_RSYN                RTC_CTL_RSYNF            /*!< registers synchronized flag */
104 #define RTC_FLAG_LWOF                RTC_CTL_LWOFF            /*!< last write operation finished flag */
105 
106 /* function declarations */
107 /* enter RTC configuration mode */
108 void rtc_configuration_mode_enter(void);
109 /* exit RTC configuration mode */
110 void rtc_configuration_mode_exit(void);
111 
112 /* wait RTC last write operation finished flag set */
113 void rtc_lwoff_wait(void);
114 /* wait RTC registers synchronized flag set */
115 void rtc_register_sync_wait(void);
116 
117 /* get RTC counter value */
118 uint32_t rtc_counter_get(void);
119 /* set RTC counter value */
120 void rtc_counter_set(uint32_t cnt);
121 
122 /* set RTC prescaler value */
123 void rtc_prescaler_set(uint32_t psc);
124 /* set RTC alarm value */
125 void rtc_alarm_config(uint32_t alarm);
126 /* get RTC divider value */
127 uint32_t rtc_divider_get(void);
128 
129 /* enable RTC interrupt */
130 void rtc_interrupt_enable(uint32_t interrupt);
131 /* disable RTC interrupt */
132 void rtc_interrupt_disable(uint32_t interrupt);
133 /* get RTC flag status */
134 FlagStatus rtc_flag_get(uint32_t flag);
135 /* clear RTC flag status */
136 void rtc_flag_clear(uint32_t flag);
137 /* get RTC interrupt flag status */
138 FlagStatus rtc_interrupt_flag_get(uint32_t flag);
139 /* clear RTC interrupt flag status */
140 void rtc_interrupt_flag_clear(uint32_t flag);
141 
142 #endif /* GD32A50X_RTC_H */
143