Dual Active Bridge Development Board (Part-No. )
Content
Loading...
Searching...
No Matches
os_sys_time.c
1
2
//=======================================================================================================
3
// @file os_sys_time.c
4
//
5
// @brief contains the function for the system time
6
//
7
// @note counting up the time can/should be done in the interrupt routine of the scheduler
8
// reading the time can also be done outside of the interrupt without any conflicts
9
//
10
// @author M52409
11
//
12
// @date 2019-08-09
13
//=======================================================================================================
14
15
#include <string.h>
16
#include "os/os_sys_time.h"
17
#include "os_config.h"
18
19
#if OS_USE_SYSTIME == 1
20
21
static
volatile
OS_SYSTIME_t
os_systime;
22
23
//=======================================================================================================
24
// @brief call this function every millisecond from the interrupt in your scheduler
25
//=======================================================================================================
26
void
OS_SysTime_IncrementTime_1ms(
void
)
27
{
28
if
(os_systime.
millisecond
< 999U)
29
{
30
os_systime.
millisecond
++;
31
}
32
else
33
{
34
os_systime.
millisecond
= 0U;
35
if
(os_systime.
second
< 59U)
36
{
37
os_systime.
second
++;
38
}
39
else
40
{
41
os_systime.
second
= 0U;
42
if
(os_systime.
minute
< 59U)
43
{
44
os_systime.
minute
++;
45
}
46
else
47
{
48
os_systime.
minute
= 0U;
49
if
(os_systime.
hour
< 23U)
50
{
51
os_systime.
hour
++;
52
}
53
else
54
{
55
os_systime.
hour
= 0U;
56
}
57
}
58
}
59
}
60
}
61
62
63
//=======================================================================================================
64
// @brief function to reset the time to zero
65
// @ note use this function at boot up time or in the init function of your scheduler or
66
// whenever you want to set the system time to zero
67
//=======================================================================================================
68
void
OS_SysTime_ResetTime(
void
)
69
{
70
os_systime.
millisecond
= 0U;
//starting with milliseconds is important for consistency
71
os_systime.
second
= 0U;
72
os_systime.
minute
= 0U;
73
os_systime.
hour
= 0U;
74
}
75
76
77
//=======================================================================================================
78
// @brief get the time now
79
// @ note use this function to get a copy of the system time
80
//=======================================================================================================
81
void
OS_SysTime_GetTime(
OS_SYSTIME_t
* retVal)
82
{
83
/* LDRA_EXCLUDE 45 D */
84
/* The void* returned by memcpy is not required in this function */
85
/* LDRA_EXCLUDE 382 S */
86
memcpy (retVal, (
void
*) &os_systime,
sizeof
(
OS_SYSTIME_t
));
87
// if the 1ms interrupt changed the system time during this copy operation, the copy is not valid
88
// so we have to check for that with a simple while loop and copy again in such a case
89
/* We are comparing the right sizes between the structure pointers and the
90
return from memcmp is an int, which is being rightly checked*/
91
/* LDRA_EXCLUDE 618 S */
92
/* LDRA_EXCLUDE 114 S */
93
while
(memcmp(retVal, (
void
*) &os_systime,
sizeof
(
OS_SYSTIME_t
) != 0U))
94
{
95
/* LDRA_EXCLUDE 382 S */
96
memcpy (retVal, (
void
*) &os_systime,
sizeof
(
OS_SYSTIME_t
));
97
}
98
}
99
100
#endif
//OS_USE_SYSTIME
OS_SYSTIME_t
Definition
os_sys_time.h:26
OS_SYSTIME_t::millisecond
uint16_t millisecond
Definition
os_sys_time.h:27
OS_SYSTIME_t::second
uint8_t second
Definition
os_sys_time.h:28
OS_SYSTIME_t::minute
uint8_t minute
Definition
os_sys_time.h:29
OS_SYSTIME_t::hour
uint8_t hour
Definition
os_sys_time.h:30
© Copyright 1998-2022 Microchip Technology Inc. All rights reserved.