summaryrefslogtreecommitdiff
path: root/bsp/airm2m/air105/libraries/HAL_Driver/Src/core_rtc.c
blob: 35dcabe7bc400f54343dcb8cbaa2d7a8a002b6cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * Copyright (c) 2022 OpenLuat & AirM2M
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "user.h"

static int32_t RTC_DummyCB(void *pData, void *pParam)
{
    DBG("!");
    return 0;
}

static CBFuncEx_t prvRTCCB;
static void *prvParam;

static void RTC_IrqHandler(int32_t Line, void *pData)
{
    RTC->RTC_INTCLR = 1;
    ISR_OnOff(RTC_IRQn, 0);
    prvRTCCB(pData, prvParam);
}

void RTC_GlobalInit(void)
{
    int8_t Buf[4][6];
    LongInt Tamp;
    char *strMon[12] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
    int Day,Year,Mon,Hour,Min,Sec,i;
    CmdParam CP;
    Date_UserDataStruct BuildDate;
    Time_UserDataStruct BuildTime;
    prvRTCCB = RTC_DummyCB;
    if (!RTC->RTC_REF)
    {
        DBG("rtc lost power!");
        memset(&CP, 0, sizeof(CP));
        memset(Buf, 0, sizeof(Buf));
        CP.param_max_len = 6;
        CP.param_max_num = 4;
        CP.param_str = (int8_t *)Buf;
        CmdParseParam(__DATE__, &CP, ' ');
        Mon = 0;
        for (i = 0; i < 12; i++)
        {
            if (!strcmp(strMon[i], Buf[0]))
            {
                Mon = i + 1;
            }
        }

        if (Buf[1][0])
        {
            Day = strtol(Buf[1], NULL, 10);
            Year = strtol(Buf[2], NULL, 10);
        }
        else
        {
            Day = strtol(Buf[2], NULL, 10);
            Year = strtol(Buf[3], NULL, 10);
        }


        CP.param_num = 0;
        memset(Buf, 0, sizeof(Buf));

        CP.param_str = (int8_t *)Buf;
        CmdParseParam(__TIME__, &CP, ':');
        Hour = strtol(Buf[0], NULL, 10);
        Min = strtol(Buf[1], NULL, 10);
        Sec = strtol(Buf[2], NULL, 10);
        BuildDate.Year = Year;
        BuildDate.Mon = Mon;
        BuildDate.Day = Day;
        BuildTime.Hour = Hour;
        BuildTime.Min = Min;
        BuildTime.Sec = Sec;
        RTC_SetDateTime(&BuildDate, &BuildTime, 0);
    }
#ifdef __BUILD_OS__
    ISR_SetPriority(RTC_IRQn, IRQ_MAX_PRIORITY + 1);
#else
    ISR_SetPriority(RTC_IRQn, 3);
#endif
    ISR_SetHandler(RTC_IRQn, RTC_IrqHandler, NULL);
//  RTC_GetDateTime(&uBuildDate, &uBuildTime);
//  DBG("%04u-%02u-%02u %02u:%02u:%02u", uBuildDate.Date.Year, uBuildDate.Date.Mon,
//          uBuildDate.Date.Day, uBuildTime.Time.Hour, uBuildTime.Time.Min,
//          uBuildTime.Time.Sec);

}

void RTC_SetStamp(uint32_t Stamp)
{
    while (!(RTC->RTC_CS & RTC_CS_READY)) {;}
    RTC->RTC_CS |= RTC_CS_CLR;
    while (RTC->RTC_CS & RTC_CS_CLR) {;}
    RTC->RTC_REF = Stamp;
}

void RTC_SetDateTime(Date_UserDataStruct *pDate, Time_UserDataStruct *pTime, uint8_t isForce)
{
    uint64_t Tamp = UTC2Tamp(pDate, pTime);
    uint32_t curTime;
    uint32_t newTime = (uint32_t)Tamp;
    while (!(RTC->RTC_CS & RTC_CS_READY)) {;}
    if (isForce)
    {
        RTC->RTC_CS |= RTC_CS_CLR;
        while (RTC->RTC_CS & RTC_CS_CLR) {;}
        RTC->RTC_REF = newTime;
    }
    else
    {
        RTC->RTC_CS |= RTC_CS_LOCK_TIM;
        curTime = RTC->RTC_TIM;
        RTC->RTC_CS &= ~RTC_CS_LOCK_TIM;
        curTime += RTC->RTC_REF;
        if (newTime > curTime)
        {
            RTC->RTC_CS |= RTC_CS_CLR;
            while (RTC->RTC_CS & RTC_CS_CLR) {;}
            RTC->RTC_REF = newTime;
        }
    }
}

void RTC_GetDateTime(Date_UserDataStruct *pDate, Time_UserDataStruct *pTime)
{
    Tamp2UTC(RTC_GetUTC(), pDate, pTime, 0);
}

uint64_t RTC_GetUTC(void)
{
    uint64_t curTime;
    while (!(RTC->RTC_CS & RTC_CS_READY)) {;}
    RTC->RTC_CS |= RTC_CS_LOCK_TIM;
    curTime = RTC->RTC_TIM;
    RTC->RTC_CS &= ~RTC_CS_LOCK_TIM;
    curTime += RTC->RTC_REF;
    return curTime;
}

void RTC_SetAlarm(uint32_t TimeSecond, CBFuncEx_t CB, void *pParam)
{
    while (!(RTC->RTC_CS & RTC_CS_READY)) {;}
    RTC->RTC_INTCLR = 1;
    RTC->RTC_CS &= ~RTC_CS_ALARM_EN;
    RTC->RTC_CS |= RTC_CS_LOCK_TIM;
    RTC->RTC_ARM = RTC->RTC_TIM + TimeSecond;
    RTC->RTC_CS &= ~RTC_CS_LOCK_TIM;
    if (CB)
    {
        prvRTCCB = CB;
    }
    else
    {
        prvRTCCB = RTC_DummyCB;
    }
    prvParam = pParam;
    RTC->RTC_CS |= RTC_CS_ALARM_EN;
    ISR_OnOff(RTC_IRQn, 1);
}