Choco OS  V.0.16.9.0
Join to the chocolate world
oc_ktime.c
Go to the documentation of this file.
1 
27 #include <oc_ktime.h>
28 #include <oc_timer_lld.h>
29 #include <oc_system_cfg.h>
30 #include <oc_timer.h>
31 #include <oc_errors.h>
32 #include <oc_intman.h>
33 
39 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
40 
41 static void TimerHandler(oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , oC_TIMER_LLD_EventFlags_t EventFlags );
42 static void MemoryFaultHandler( void * Address , MemoryEventFlags_t Event , const char * Function, uint32_t LineNumber );
43 
44 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
45 
46 
52 #define _________________________________________VARIABLES_SECTION__________________________________________________________________________
53 
54 static bool ModuleEnabledFlag = false;
55 static oC_Timestamp_t TimeStamp = 0;
56 static oC_TIMER_Context_t Context = NULL;
57 extern uint64_t oC_ThreadManTickCounter;
58 
59 static const oC_TIMER_Config_t TimerConfig = {
60  .Mode = oC_TIMER_Mode_Periodic ,
61  .MaximumTimeForWait = CFG_TIME_MAXIMUM_TIME_FOR_INITIALIZATION_OF_SYSTEM ,
62  .Frequency = oC_Frequency_FromTime(CFG_TIME_TIMESTAMP_INCREMENT_TIME) ,
63  .PermissibleDifference = 0 ,
64  .MaximumValue = 10000 ,
65  .EventHandler = TimerHandler ,
66  .EventFlags = oC_TIMER_LLD_EventFlags_TimeoutInterrupt
67 };
68 
69 static const oC_Allocator_t Allocator = {
70  .Name = "ktime" ,
71  .EventHandler = MemoryFaultHandler ,
72  .EventFlags = MemoryEventFlags_BufferOverflow |
73  MemoryEventFlags_PossibleMemoryLeak
74 };
75 
76 #undef _________________________________________VARIABLES_SECTION__________________________________________________________________________
77 
78 
84 #define _________________________________________FUNCTIONS_SECTION__________________________________________________________________________
85 
86 //==========================================================================================================================================
87 //==========================================================================================================================================
88 oC_ErrorCode_t oC_KTime_TurnOn( void )
89 {
90  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
91 
92  if(
93  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag == false , oC_ErrorCode_ModuleIsTurnedOn)
94  )
95  {
96  TimeStamp = 0;
97  ModuleEnabledFlag = true;
98  errorCode = oC_ErrorCode_None;
99 
100  if(oC_TIMER_IsTurnedOn())
101  {
102  oC_AssignErrorCode(&errorCode , oC_TIMER_Configure(&TimerConfig , &Context)) &&
103  oC_AssignErrorCode(&errorCode , oC_TIMER_Start(Context));
104  }
105  }
106 
107  return errorCode;
108 }
109 
110 //==========================================================================================================================================
111 //==========================================================================================================================================
112 oC_ErrorCode_t oC_KTime_TurnOff( void )
113 {
114  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
115 
116  if(
117  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag == true , oC_ErrorCode_ModuleNotStartedYet) &&
118  oC_AssignErrorCode(&errorCode , oC_TIMER_Stop(Context)) &&
119  oC_AssignErrorCode(&errorCode , oC_TIMER_Unconfigure(&TimerConfig , &Context))
120  )
121  {
122  ModuleEnabledFlag = false;
123  errorCode = oC_ErrorCode_None;
124  }
125 
126  return errorCode;
127 }
128 
129 //==========================================================================================================================================
130 //==========================================================================================================================================
131 oC_Timestamp_t oC_KTime_GetTimestamp( void )
132 {
133  oC_Timestamp_t timeStamp = 0;
134 
135  if(oC_TIMER_IsTurnedOn() && Context != NULL)
136  {
137  uint64_t timerValue = 0;
138 
139  oC_TIMER_ReadValue(Context,&timerValue);
140 
141  timeStamp = TimeStamp + (timerValue * CFG_TIME_TIMESTAMP_INCREMENT_TIME);
142  }
143  else
144  {
145  oC_IntMan_EnterCriticalSection();
146  timeStamp = oC_KTime_TickToTime(oC_ThreadManTickCounter);
147  oC_IntMan_ExitCriticalSection();
148  }
149 
150  return timeStamp;
151 }
152 
153 //==========================================================================================================================================
154 //==========================================================================================================================================
155 uint64_t oC_KTime_GetCurrentTick ( void )
156 {
157  return oC_ThreadManTickCounter;
158 }
159 
160 //==========================================================================================================================================
161 //==========================================================================================================================================
162 oC_Time_t oC_KTime_TickToTime( uint64_t Ticks )
163 {
164  return oC_Frequency_ToTime(CFG_FREQUENCY_DEFAULT_SYSTEM_TIMER_FREQUENCY) * Ticks;
165 }
166 
167 #undef _________________________________________FUNCTIONS_SECTION__________________________________________________________________________
168 
174 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
175 
176 //==========================================================================================================================================
177 //==========================================================================================================================================
178 static void TimerHandler(oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , oC_TIMER_LLD_EventFlags_t EventFlags )
179 {
180  TimeStamp += CFG_TIME_TIMESTAMP_INCREMENT_TIME * 10000;
181 }
182 
183 //==========================================================================================================================================
184 //==========================================================================================================================================
185 static void MemoryFaultHandler( void * Address , MemoryEventFlags_t Event , const char * Function, uint32_t LineNumber )
186 {
187  while(1);
188 }
189 
190 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
static bool ModuleEnabledFlag
Definition: oc_memman.c:211
const char * Name
Definition: oc_stdlib.h:161
The file with LLD interface for the TIMER driver.
The file with interface for the timer driver.
identifier for allocations
Definition: oc_stdlib.h:159
The file with interface for interrupt manager.
stores ETH context
Definition: oc_eth.c:97
The file with interface of kernel time module.
#define NULL
pointer to a zero
Definition: oc_null.h:37