4kW dsPIC33C PSFB DC-DC DA (Part-No. )
Content
Loading...
Searching...
No Matches
os_sys_time.c
1
//=======================================================================================================
2
// Copyright(c) 2019 Microchip Technology Inc. and its subsidiaries.
3
// Subject to your compliance with these terms, you may use Microchip software and any derivatives
4
// exclusively with Microchip products. It is your responsibility to comply with third party license
5
// terms applicable to your use of third-party software (including open source software) that may
6
// accompany Microchip software.
7
// THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY,
8
// APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND
9
// FITNESS FOR A PARTICULAR PURPOSE.
10
// IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
11
// LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF
12
// MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
13
// ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT
14
// EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
15
//=======================================================================================================
16
17
//=======================================================================================================
18
// @file os_sys_time.c
19
//
20
// @brief contains the function for the system time
21
//
22
// @note counting up the time can/should be done in the interrupt routine of the scheduler
23
// reading the time can also be done outside of the interrupt without any conflicts
24
//
25
// @author M52409
26
//
27
// @date 2019-08-09
28
//=======================================================================================================
29
30
#include <string.h>
31
#include "os/os_sys_time.h"
32
#include "../project_settings.h"
33
34
#if OS_USE_SYSTIME == 1
35
36
static
volatile
OS_SYSTIME_t
os_systime;
37
38
//=======================================================================================================
39
// @brief call this function every millisecond from the interrupt in your scheduler
40
//=======================================================================================================
41
void
OS_SysTime_IncrementTime_1ms(
void
)
42
{
43
if
(os_systime.
millisecond
< 999U)
44
{
45
os_systime.
millisecond
++;
46
}
47
else
48
{
49
os_systime.
millisecond
= 0U;
50
if
(os_systime.
second
< 59U)
51
{
52
os_systime.
second
++;
53
}
54
else
55
{
56
os_systime.
second
= 0U;
57
if
(os_systime.
minute
< 59U)
58
{
59
os_systime.
minute
++;
60
}
61
else
62
{
63
os_systime.
minute
= 0U;
64
if
(os_systime.
hour
< 23U)
65
{
66
os_systime.
hour
++;
67
}
68
else
69
{
70
os_systime.
hour
= 0U;
71
}
72
}
73
}
74
}
75
}
76
77
78
//=======================================================================================================
79
// @brief function to reset the time to zero
80
// @ note use this function at boot up time or in the init function of your scheduler or
81
// whenever you want to set the system time to zero
82
//=======================================================================================================
83
void
OS_SysTime_ResetTime(
void
)
84
{
85
os_systime.
millisecond
= 0U;
//starting with milliseconds is important for consistency
86
os_systime.
second
= 0U;
87
os_systime.
minute
= 0U;
88
os_systime.
hour
= 0U;
89
}
90
91
92
//=======================================================================================================
93
// @brief get the time now
94
// @ note use this function to get a copy of the system time
95
//=======================================================================================================
96
void
OS_SysTime_GetTime(
OS_SYSTIME_t
* retVal)
97
{
98
/* LDRA_EXCLUDE 45 D */
99
/* The void* returned by memcpy is not required in this function */
100
/* LDRA_EXCLUDE 382 S */
101
memcpy (retVal, (
void
*) &os_systime,
sizeof
(
OS_SYSTIME_t
));
102
// if the 1ms interrupt changed the system time during this copy operation, the copy is not valid
103
// so we have to check for that with a simple while loop and copy again in such a case
104
/* We are comparing the right sizes between the structure pointers and the
105
return from memcmp is an int, which is being rightly checked*/
106
/* LDRA_EXCLUDE 618 S */
107
/* LDRA_EXCLUDE 114 S */
108
while
(memcmp(retVal, (
void
*) &os_systime,
sizeof
(
OS_SYSTIME_t
) != 0U))
109
{
110
/* LDRA_EXCLUDE 382 S */
111
memcpy (retVal, (
void
*) &os_systime,
sizeof
(
OS_SYSTIME_t
));
112
}
113
}
114
115
#endif
//OS_USE_SYSTIME
OS_SYSTIME_t
Definition
os_sys_time.h:41
OS_SYSTIME_t::millisecond
uint16_t millisecond
Definition
os_sys_time.h:42
OS_SYSTIME_t::second
uint8_t second
Definition
os_sys_time.h:43
OS_SYSTIME_t::minute
uint8_t minute
Definition
os_sys_time.h:44
OS_SYSTIME_t::hour
uint8_t hour
Definition
os_sys_time.h:45
© Copyright 1998-2022 Microchip Technology Inc. All rights reserved.