Choco OS  V.0.16.9.0
Join to the chocolate world
oc_timer_lld.c
1 
27 #include <oc_timer_lld.h>
28 #include <oc_mem_lld.h>
29 #include <oc_clock_lld.h>
30 #include <oc_gpio_lld.h>
31 #include <oc_gpio_mslld.h>
32 
33 #include <oc_math.h>
34 #include <oc_array.h>
35 
41 #define _________________________________________LOCAL_MACROS_SECTION_______________________________________________________________________
42 
43 #define GPTMTAMR(Channel) oC_Machine_Register(Channel,GPTMTAMR)
44 #define GPTMTBMR(Channel) oC_Machine_Register(Channel,GPTMTBMR)
45 #define GPTMCTL(Channel) oC_Machine_Register(Channel,GPTMCTL)
46 #define GPTMPP(Channel) oC_Machine_Register(Channel,GPTMPP)
47 #define GPTMIMR(Channel) oC_Machine_Register(Channel,GPTMIMR)
48 #define GPTMMIS(Channel) oC_Machine_Register(Channel,GPTMMIS)
49 #define GPTMICR(Channel) oC_Machine_Register(Channel,GPTMICR)
50 
51 #define IsChannelPoweredOn(Channel) (oC_Machine_GetPowerStateForChannel(Channel) == oC_Power_On)
52 #define IsChannelCorrect(channel) (oC_Channel_IsCorrect(TIMER,channel))
53 #define MODE(Channel) Modes[oC_TIMER_LLD_ChannelToChannelIndex(Channel)]
54 
55 
56 #undef _________________________________________LOCAL_MACROS_SECTION_______________________________________________________________________
57 
63 #define _________________________________________LOCAL_TYPES_SECTION________________________________________________________________________
64 
65 typedef enum
66 {
67  GPTMTxMR_TxMR_Reserved = 0 ,
68  GPTMTxMR_TxMR_OneShotTimerMode = 0x1 ,
69  GPTMTxMR_TxMR_PeriodicTimerMode = 0x2 ,
70  GPTMTxMR_TxMR_CaptureMode = 0x3
71 } GPTMTxMR_TxMR_t;
72 
73 typedef enum
74 {
75  GPTMCTL_TxEVENT_PositiveEdge = 0 ,
76  GPTMCTL_TxEVENT_NegativeEdge = 1 ,
77  GPTMCTL_TxEVENT_BothEdges = 3 ,
78 } GPTMCTL_TxEVENT_t;
79 
80 typedef enum
81 {
82  GPTMCFG_TimerWidth_Full = 0x0 ,
83  GPTMCFG_TimerWidth_FullRtc = 0x1 ,
84  GPTMCFG_TimerWidth_Half = 0x4
85 } GPTMCFG_TimerWidth_t;
86 
87 typedef enum
88 {
89  GPTMTxMR_TxAMS_AlternateMode_CaptureOrCompare = 0 ,
90  GPTMTxMR_TxAMS_AlternateMode_Pwm = 1
91 } GPTMTxMR_TxAMS_AlternateMode_t;
92 
93 typedef enum
94 {
95  GPTMTxMR_TxCMR_CaptureMode_EdgeCount = 0 ,
96  GPTMTxMR_TxCMR_CaptureMode_EdgeTime = 1
97 } GPTMTxMR_TxCMR_CaptureMode_t;
98 
99 typedef enum
100 {
101  GPTMCTL_TxPWML_PwmOutput_Unaffected = 0 ,
102  GPTMCTL_TxPWML_PwmOutput_Inverted = 1
103 } GPTMCTL_TxPWML_PwmOutput_t;
104 
105 typedef enum
106 {
107  GPTMTxMR_TxPWMIE_PwmInterruptEnable_Disabled = 0 ,
108  GPTMTxMR_TxPWMIE_PwmInterruptEnable_Enabled = 1
109 } GPTMTxMR_TxPWMIE_PwmInterruptEnable_t;
110 
111 typedef enum
112 {
113  GPTMTxMR_TxPLO_PwmLegacyOperations_Disabled = 0 ,
114  GPTMTxMR_TxPLO_PwmLegacyOperations_Enabled = 1
115 } GPTMTxMR_TxPLO_PwmLegacyOperations_t;
116 
117 typedef enum
118 {
119  GPTMCTL_TxEVENT_EventMode_PositiveEdge = 0 ,
120  GPTMCTL_TxEVENT_EventMode_NegativeEdge = 1 ,
121  GPTMCTL_TxEVENT_EventMode_BothEdges = 3
122 } GPTMCTL_TxEVENT_EventMode_t;
123 
124 #undef _________________________________________LOCAL_TYPES_SECTION________________________________________________________________________
125 
126 
132 #define _________________________________________LOCAL_VARIABLES_SECTION____________________________________________________________________
133 
134 
135 /* Variables */
136 static bool ModuleEnabledFlag = false;
137 
138 /* Arrays */
139 static oC_TIMER_LLD_EventFlags_t EventFlagsArray[oC_ModuleChannel_NumberOfElements(TIMER)][2];
140 static oC_TIMER_LLD_Mode_t Modes[oC_ModuleChannel_NumberOfElements(TIMER)][2];
141 static oC_TIMER_LLD_EventHandler_t EventHandlers[oC_ModuleChannel_NumberOfElements(TIMER)][2];
142 
143 
144 #undef _________________________________________LOCAL_VARIABLES_SECTION____________________________________________________________________
145 
151 #define _________________________________________LOCAL_PROTOTYPES_SECTION_________________________________________________________________
152 
153 static inline bool IsModeSelected ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer );
154 static inline bool IsTimerEnabled ( oC_TIMER_Channel_t Channel );
155 static inline bool IsWideChannel ( oC_TIMER_Channel_t Channel );
156 static inline bool CanBeFullWidth ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer );
157 static inline void SetCountDirection ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , oC_TIMER_LLD_CountDirection_t CountDirection );
158 static inline oC_TIMER_LLD_CountDirection_t GetCountDirection ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer );
159 static inline void SetTimersMode ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , GPTMTxMR_TxMR_t Mode );
160 static inline void SetInputTrigger ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , GPTMCTL_TxEVENT_t Trigger );
161 static inline GPTMCTL_TxEVENT_t GetInputTrigger ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer );
162 static inline void EnableTimer ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer );
163 static inline void DisableTimer ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer );
164 static void CallEvent ( oC_TIMER_Channel_t Channel , oC_ChannelIndex_t ChannelIndex , oC_TIMER_LLD_SubTimer_t SubTimer , oC_TIMER_LLD_EventFlags_t EventFlags );
165 static inline uint32_t GetPrescaler ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer );
166 static inline void SetTimerWidth ( oC_TIMER_Channel_t Channel , GPTMCFG_TimerWidth_t TimerWidth );
167 static inline GPTMCFG_TimerWidth_t GetTimerWidth ( oC_TIMER_Channel_t Channel );
168 static inline void SetMaxValue ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , uint64_t MaxValue );
169 static inline uint64_t GetMaxValue ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer);
170 static inline void SetValue ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , uint64_t Value );
171 static inline uint64_t GetValue ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer);
172 static inline void SetMatchValue ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , uint64_t Value );
173 static inline uint64_t GetMatchValue ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer );
174 static inline void SetAlternateMode ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , GPTMTxMR_TxAMS_AlternateMode_t Mode );
175 static inline void SetCaptureMode ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , GPTMTxMR_TxCMR_CaptureMode_t Mode );
176 static inline void SetPwmOutput ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , GPTMCTL_TxPWML_PwmOutput_t PwmOutput );
177 static inline GPTMCTL_TxPWML_PwmOutput_t GetPwmOutput ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer );
178 static void EnableEvents ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , oC_TIMER_LLD_EventFlags_t EventFlags );
179 static void DisableEvents ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , oC_TIMER_LLD_EventFlags_t EventFlags );
180 static void SetPwmInterrupt ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , GPTMTxMR_TxPWMIE_PwmInterruptEnable_t Enabled );
181 static inline void SetPwmLegacyOperations ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , GPTMTxMR_TxPLO_PwmLegacyOperations_t Enabled );
182 static inline oC_ErrorCode_t InitializeMode ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , oC_TIMER_LLD_Mode_t Mode );
183 static oC_TIMER_LLD_Mode_t GetMode ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer );
184 static void SetMode ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , oC_TIMER_LLD_Mode_t Mode );
185 static inline void SetEventMode ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , GPTMCTL_TxEVENT_EventMode_t EventMode );
186 
187 #undef _________________________________________LOCAL_PROTOTYPES_SECTION_________________________________________________________________
188 
189 
195 #define _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________
196 
197 //==========================================================================================================================================
202 //==========================================================================================================================================
203 oC_ErrorCode_t oC_TIMER_LLD_TurnOnDriver( void )
204 {
205  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
206 
207  if(oC_AssignErrorCodeIfFalse(&errorCode , !ModuleEnabledFlag , oC_ErrorCode_ModuleIsTurnedOn))
208  {
209  oC_TIMER_LLD_ForEachChannel(Channel)
210  {
211  oC_ChannelIndex_t channelIndex = oC_Channel_ToIndex(TIMER,Channel);
212  EventHandlers[channelIndex][0] = NULL;
213  EventHandlers[channelIndex][1] = NULL;
214  Modes[channelIndex][0] = oC_TIMER_LLD_Mode_NotSelected;
215  Modes[channelIndex][1] = oC_TIMER_LLD_Mode_NotSelected;
216  EventFlagsArray[channelIndex][0] = 0;
217  EventFlagsArray[channelIndex][1] = 0;
218  }
219 
220  ModuleEnabledFlag = true;
221  errorCode = oC_ErrorCode_None;
222  }
223 
224  return errorCode;
225 }
226 
227 //==========================================================================================================================================
232 //==========================================================================================================================================
233 oC_ErrorCode_t oC_TIMER_LLD_TurnOffDriver( void )
234 {
235  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
236 
237  if(oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet))
238  {
239  ModuleEnabledFlag = false;
240 
241  oC_TIMER_LLD_ForEachChannel(Channel)
242  {
243  oC_ChannelIndex_t channelIndex = oC_Channel_ToIndex(TIMER,Channel);
244 
245  oC_TIMER_LLD_RestoreDefaultStateOnChannel(Channel,oC_TIMER_LLD_SubTimer_Both);
246 
247  EventHandlers[channelIndex][0] = NULL;
248  EventHandlers[channelIndex][1] = NULL;
249  Modes[channelIndex][0] = oC_TIMER_LLD_Mode_NotSelected;
250  Modes[channelIndex][1] = oC_TIMER_LLD_Mode_NotSelected;
251  EventFlagsArray[channelIndex][0] = 0;
252  EventFlagsArray[channelIndex][1] = 0;
253  }
254 
255  }
256 
257  return errorCode;
258 }
259 
260 //==========================================================================================================================================
265 //==========================================================================================================================================
266 bool oC_TIMER_LLD_IsChannelCorrect( oC_TIMER_Channel_t Channel )
267 {
268  return oC_Channel_IsCorrect(TIMER , Channel);
269 }
270 
271 //==========================================================================================================================================
276 //==========================================================================================================================================
277 bool oC_TIMER_LLD_IsChannelIndexCorrect( oC_ChannelIndex_t ChannelIndex )
278 {
279  return ChannelIndex < oC_ModuleChannel_NumberOfElements(TIMER);
280 }
281 
282 //==========================================================================================================================================
287 //==========================================================================================================================================
288 bool oC_TIMER_LLD_IsModulePinDefined( oC_TIMER_Pin_t ModulePin )
289 {
290  oC_Pin_t pin = oC_ModulePin_GetPin(ModulePin);
291  return oC_GPIO_LLD_IsPinDefined(pin);
292 }
293 
294 //==========================================================================================================================================
299 //==========================================================================================================================================
300 bool oC_TIMER_LLD_IsSubTimerCorrect(oC_TIMER_LLD_SubTimer_t SubTimer)
301 {
302  return SubTimer > oC_TIMER_LLD_SubTimer_None && SubTimer <= oC_TIMER_LLD_SubTimer_Both;
303 }
304 
305 //==========================================================================================================================================
310 //==========================================================================================================================================
311 oC_Pin_t oC_TIMER_LLD_GetPinOfModulePin( oC_TIMER_Pin_t ModulePin )
312 {
313  return oC_ModulePin_GetPin(ModulePin);
314 }
315 
316 //==========================================================================================================================================
321 //==========================================================================================================================================
322 const char * oC_TIMER_LLD_GetModulePinName( oC_TIMER_Pin_t ModulePin )
323 {
324  return oC_GPIO_LLD_GetPinName( oC_ModulePin_GetPin(ModulePin) );
325 }
326 
327 //==========================================================================================================================================
332 //==========================================================================================================================================
333 const char * oC_TIMER_LLD_GetChannelName( oC_TIMER_Channel_t Channel )
334 {
335  return oC_Channel_GetName(Channel);
336 }
337 
338 //==========================================================================================================================================
343 //==========================================================================================================================================
344 oC_TIMER_Channel_t oC_TIMER_LLD_GetChannelOfModulePin( oC_TIMER_Pin_t ModulePin )
345 {
346  return oC_ModulePin_GetChannel(ModulePin);
347 }
348 
349 //==========================================================================================================================================
354 //==========================================================================================================================================
355 oC_ChannelIndex_t oC_TIMER_LLD_ChannelToChannelIndex( oC_TIMER_Channel_t Channel )
356 {
357  return oC_Channel_ToIndex(TIMER,Channel);
358 }
359 
360 //==========================================================================================================================================
365 //==========================================================================================================================================
366 oC_TIMER_Channel_t oC_TIMER_LLD_ChannelIndexToChannel( oC_TIMER_LLD_ChannelIndex_t Channel )
367 {
368  return oC_Channel_FromIndex(TIMER,Channel);
369 }
370 
371 //==========================================================================================================================================
376 //==========================================================================================================================================
377 oC_ErrorCode_t oC_TIMER_LLD_SetPower( oC_TIMER_Channel_t Channel , oC_Power_t Power )
378 {
379  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
380 
381  if(
382  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet) &&
383  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,Channel) , oC_ErrorCode_WrongChannel) &&
384  oC_AssignErrorCodeIfFalse(&errorCode , Power == oC_Power_On || Power == oC_Power_Off , oC_ErrorCode_PowerStateNotCorrect) &&
385  oC_AssignErrorCodeIfFalse(&errorCode , oC_Machine_SetPowerStateForChannel(Channel,Power) , oC_ErrorCode_CannotEnableChannel)
386  )
387  {
388  errorCode = oC_ErrorCode_None;
389  }
390 
391  return errorCode;
392 }
393 
394 //==========================================================================================================================================
399 //==========================================================================================================================================
400 oC_ErrorCode_t oC_TIMER_LLD_ReadPower( oC_TIMER_Channel_t Channel , oC_Power_t * outPower )
401 {
402  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
403 
404  if(
405  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet) &&
406  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,Channel) , oC_ErrorCode_WrongChannel) &&
407  oC_AssignErrorCodeIfFalse(&errorCode , oC_MEM_LLD_IsRamAddress(outPower) , oC_ErrorCode_OutputAddressNotInRAM)
408  )
409  {
410  *outPower = oC_Machine_GetPowerStateForChannel(Channel);
411  errorCode = oC_ErrorCode_None;
412  }
413 
414  return errorCode;
415 }
416 
417 //==========================================================================================================================================
422 //==========================================================================================================================================
423 bool oC_TIMER_LLD_RestoreDefaultStateOnChannel( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer )
424 {
425  bool stateRestored = false;
426 
427  if( ModuleEnabledFlag && oC_Channel_IsCorrect(TIMER,Channel) )
428  {
429  SetMode(Channel,SubTimer,oC_TIMER_LLD_Mode_NotSelected);
430  DisableTimer(Channel,SubTimer);
431  DisableEvents(Channel,SubTimer,oC_TIMER_LLD_EventFlags_All);
432 
434  {
435  stateRestored = true;
436  }
437  }
438 
439 
440  return stateRestored;
441 }
442 
443 //==========================================================================================================================================
448 //==========================================================================================================================================
449 oC_ErrorCode_t oC_TIMER_LLD_TimerStart( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer )
450 {
451  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
452 
453  if(
454  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet ) &&
455  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,Channel) , oC_ErrorCode_WrongChannel ) &&
456  oC_AssignErrorCodeIfFalse(&errorCode , oC_TIMER_LLD_IsSubTimerCorrect(SubTimer) , oC_ErrorCode_TIMERSubTimerNotCorrect) &&
457  oC_AssignErrorCodeIfFalse(&errorCode , IsChannelPoweredOn(Channel) , oC_ErrorCode_ChannelNotPoweredOn ) &&
458  oC_AssignErrorCodeIfFalse(&errorCode , IsModeSelected(Channel,SubTimer) , oC_ErrorCode_TIMERModeNotSelected )
459  )
460  {
461  EnableTimer(Channel,SubTimer);
462  errorCode = oC_ErrorCode_None;
463  }
464 
465  return errorCode;
466 }
467 
468 //==========================================================================================================================================
473 //==========================================================================================================================================
474 oC_ErrorCode_t oC_TIMER_LLD_TimerStop( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer )
475 {
476  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
477 
478  if(
479  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet ) &&
480  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,Channel) , oC_ErrorCode_WrongChannel ) &&
481  oC_AssignErrorCodeIfFalse(&errorCode , oC_TIMER_LLD_IsSubTimerCorrect(SubTimer) , oC_ErrorCode_TIMERSubTimerNotCorrect) &&
482  oC_AssignErrorCodeIfFalse(&errorCode , IsChannelPoweredOn(Channel) , oC_ErrorCode_ChannelNotPoweredOn )
483  )
484  {
485  DisableTimer(Channel,SubTimer);
486  errorCode = oC_ErrorCode_None;
487  }
488 
489  return errorCode;
490 }
491 
492 //==========================================================================================================================================
497 //==========================================================================================================================================
498 oC_ErrorCode_t oC_TIMER_LLD_ChangeFrequency( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , oC_Frequency_t Frequency , oC_Frequency_t PermissibleDifference )
499 {
500  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
502 
503  if(
504  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet ) &&
505  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,Channel) , oC_ErrorCode_WrongChannel ) &&
506  oC_AssignErrorCodeIfFalse(&errorCode , oC_TIMER_LLD_IsSubTimerCorrect(SubTimer) , oC_ErrorCode_TIMERSubTimerNotCorrect ) &&
507  oC_AssignErrorCodeIfFalse(&errorCode , IsChannelPoweredOn(Channel) , oC_ErrorCode_ChannelNotPoweredOn ) &&
508  oC_AssignErrorCodeIfFalse(&errorCode , !IsTimerEnabled(Channel) , oC_ErrorCode_TIMERChannelNotStopped ) &&
509  oC_AssignErrorCodeIfFalse(&errorCode , IsModeSelected(Channel,SubTimer) , oC_ErrorCode_TIMERModeNotSelected ) &&
510  oC_AssignErrorCodeIfFalse(&errorCode , (Frequency > 0) , oC_ErrorCode_WrongFrequency )
511  )
512  {
513  uint32_t prescaler = (uint32_t)(clockFrequency / Frequency);
514  uint32_t prescalerMax = IsWideChannel(Channel) ? oC_uint16_MAX : oC_uint8_MAX;
515 
516  if(prescaler <= prescalerMax && (prescaler > 0))
517  {
518  oC_Frequency_t realFrequency = clockFrequency / prescaler;
519 
520  if(oC_ABS(realFrequency,Frequency) <= PermissibleDifference)
521  {
522  if(prescaler == 1)
523  {
524  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
525  {
526  oC_Machine_WriteRegister(Channel, GPTMTAPR , prescaler - 1);
527  }
528  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerB)
529  {
530  oC_Machine_WriteRegister(Channel, GPTMTBPR , prescaler - 1);
531  }
532  errorCode = oC_ErrorCode_None;
533  }
534  else if(GetCountDirection(Channel,SubTimer) == oC_TIMER_LLD_CountDirection_Up)
535  {
536  /*
537  * When the timer is counting up, it is not possible to configure the prescaler
538  */
539  errorCode = oC_ErrorCode_TIMERCountsDirNotPossible;
540  }
541  else if(GetTimerWidth(Channel) != GPTMCFG_TimerWidth_Half)
542  {
543  errorCode = oC_ErrorCode_TIMERFrequencyNotPossibleInFullMode;
544  }
545  else
546  {
547  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
548  {
549  oC_Machine_WriteRegister(Channel, GPTMTAPR , prescaler - 1);
550  }
551  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerB)
552  {
553  oC_Machine_WriteRegister(Channel, GPTMTBPR , prescaler - 1);
554  }
555  errorCode = oC_ErrorCode_None;
556  }
557  }
558  else
559  {
560  errorCode = oC_ErrorCode_FrequencyNotPossible;
561  }
562  }
563  else
564  {
565  errorCode = oC_ErrorCode_FrequencyNotPossible;
566  }
567  }
568 
569  return errorCode;
570 }
571 
572 //==========================================================================================================================================
577 //==========================================================================================================================================
578 oC_ErrorCode_t oC_TIMER_LLD_ReadFrequency( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , oC_Frequency_t * outFrequency )
579 {
580  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
581 
582  if(
583  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet ) &&
584  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,Channel) , oC_ErrorCode_WrongChannel ) &&
585  oC_AssignErrorCodeIfFalse(&errorCode , oC_TIMER_LLD_IsSubTimerCorrect(SubTimer) , oC_ErrorCode_TIMERSubTimerNotCorrect ) &&
586  oC_AssignErrorCodeIfFalse(&errorCode , IsChannelPoweredOn(Channel) , oC_ErrorCode_ChannelNotPoweredOn ) &&
587  oC_AssignErrorCodeIfFalse(&errorCode , oC_MEM_LLD_IsRamAddress(outFrequency) , oC_ErrorCode_OutputAddressNotInRAM )
588  )
589  {
591  uint32_t prescaler = GetPrescaler(Channel,SubTimer);
592 
593  *outFrequency = clockFrequency / prescaler;
594  errorCode = oC_ErrorCode_None;
595  }
596 
597  return errorCode;
598 }
599 
600 //==========================================================================================================================================
605 //==========================================================================================================================================
606 oC_ErrorCode_t oC_TIMER_LLD_ChangeMaximumValue( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , uint64_t Value )
607 {
608  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
609 
610  if(
611  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet ) &&
612  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,Channel) , oC_ErrorCode_WrongChannel ) &&
613  oC_AssignErrorCodeIfFalse(&errorCode , oC_TIMER_LLD_IsSubTimerCorrect(SubTimer) , oC_ErrorCode_TIMERSubTimerNotCorrect ) &&
614  oC_AssignErrorCodeIfFalse(&errorCode , IsChannelPoweredOn(Channel) , oC_ErrorCode_ChannelNotPoweredOn ) &&
615  oC_AssignErrorCodeIfFalse(&errorCode , IsModeSelected(Channel,SubTimer) , oC_ErrorCode_TIMERModeNotSelected )
616  )
617  {
618  if( (Value <= oC_uint16_MAX) || (IsWideChannel(Channel) && (Value <= oC_uint32_MAX)) )
619  {
620  if(GetMode(Channel,SubTimer) != oC_TIMER_LLD_Mode_RealTimeClock)
621  {
622  SetTimerWidth(Channel , GPTMCFG_TimerWidth_Half);
623  }
624  errorCode = oC_ErrorCode_None;
625  }
626  else if ( CanBeFullWidth(Channel,SubTimer) && ( (!IsWideChannel(Channel) && (Value <= oC_uint32_MAX)) || IsWideChannel(Channel) ) )
627  {
628  if(SubTimer & oC_TIMER_LLD_SubTimer_Both)
629  {
630  SetTimerWidth(Channel , GPTMCFG_TimerWidth_Full);
631  errorCode = oC_ErrorCode_None;
632  }
633  else
634  {
635  errorCode = oC_ErrorCode_TIMERBothSubTimersNeeeded;
636  }
637  }
638  else
639  {
640  errorCode = oC_ErrorCode_TIMERMaximumValueTooBig;
641  }
642 
643  if(errorCode == oC_ErrorCode_None)
644  {
645  SetMaxValue(Channel,SubTimer,Value);
646  }
647  }
648 
649  return errorCode;
650 }
651 
652 //==========================================================================================================================================
657 //==========================================================================================================================================
658 oC_ErrorCode_t oC_TIMER_LLD_ReadMaximumValue( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , uint64_t * outValue )
659 {
660  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
661 
662  if(
663  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet ) &&
664  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,Channel) , oC_ErrorCode_WrongChannel ) &&
665  oC_AssignErrorCodeIfFalse(&errorCode , oC_TIMER_LLD_IsSubTimerCorrect(SubTimer) , oC_ErrorCode_TIMERSubTimerNotCorrect ) &&
666  oC_AssignErrorCodeIfFalse(&errorCode , IsChannelPoweredOn(Channel) , oC_ErrorCode_ChannelNotPoweredOn ) &&
667  oC_AssignErrorCodeIfFalse(&errorCode , oC_MEM_LLD_IsRamAddress(outValue) , oC_ErrorCode_OutputAddressNotInRAM )
668  )
669  {
670  *outValue = GetMaxValue(Channel,SubTimer);
671  errorCode = oC_ErrorCode_None;
672  }
673 
674  return errorCode;
675 }
676 
677 //==========================================================================================================================================
682 //==========================================================================================================================================
683 oC_ErrorCode_t oC_TIMER_LLD_ChangeCurrentValue( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , uint64_t Value )
684 {
685  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
686 
687  if(
688  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet ) &&
689  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,Channel) , oC_ErrorCode_WrongChannel ) &&
690  oC_AssignErrorCodeIfFalse(&errorCode , oC_TIMER_LLD_IsSubTimerCorrect(SubTimer) , oC_ErrorCode_TIMERSubTimerNotCorrect ) &&
691  oC_AssignErrorCodeIfFalse(&errorCode , IsChannelPoweredOn(Channel) , oC_ErrorCode_ChannelNotPoweredOn ) &&
692  oC_AssignErrorCodeIfFalse(&errorCode , IsModeSelected(Channel,SubTimer) , oC_ErrorCode_TIMERModeNotSelected )
693  )
694  {
695  if( (Value <= oC_uint16_MAX) || (IsWideChannel(Channel) && (Value <= oC_uint32_MAX)) )
696  {
697  if(GetMode(Channel,SubTimer) != oC_TIMER_LLD_Mode_RealTimeClock)
698  {
699  SetTimerWidth(Channel , GPTMCFG_TimerWidth_Half);
700  }
701  errorCode = oC_ErrorCode_None;
702  }
703  else if ( CanBeFullWidth(Channel,SubTimer) && ( (!IsWideChannel(Channel) && (Value <= oC_uint32_MAX)) || IsWideChannel(Channel) ) )
704  {
705  if(SubTimer & oC_TIMER_LLD_SubTimer_Both)
706  {
707  SetTimerWidth(Channel , GPTMCFG_TimerWidth_Full);
708  errorCode = oC_ErrorCode_None;
709  }
710  else
711  {
712  errorCode = oC_ErrorCode_TIMERBothSubTimersNeeeded;
713  }
714  }
715  else
716  {
717  errorCode = oC_ErrorCode_TIMERMaximumValueTooBig;
718  }
719 
720  if(errorCode == oC_ErrorCode_None)
721  {
722  SetValue(Channel,SubTimer,Value);
723  }
724  }
725 
726  return errorCode;
727 }
728 
729 //==========================================================================================================================================
734 //==========================================================================================================================================
735 oC_ErrorCode_t oC_TIMER_LLD_ReadCurrentValue( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , uint64_t * outValue )
736 {
737  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
738 
739  if(
740  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet ) &&
741  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,Channel) , oC_ErrorCode_WrongChannel ) &&
742  oC_AssignErrorCodeIfFalse(&errorCode , oC_TIMER_LLD_IsSubTimerCorrect(SubTimer) , oC_ErrorCode_TIMERSubTimerNotCorrect ) &&
743  oC_AssignErrorCodeIfFalse(&errorCode , IsChannelPoweredOn(Channel) , oC_ErrorCode_ChannelNotPoweredOn ) &&
744  oC_AssignErrorCodeIfFalse(&errorCode , oC_MEM_LLD_IsRamAddress(outValue) , oC_ErrorCode_OutputAddressNotInRAM )
745  )
746  {
747  *outValue = GetValue(Channel,SubTimer);
748  errorCode = oC_ErrorCode_None;
749  }
750 
751  return errorCode;
752 }
753 
754 //==========================================================================================================================================
759 //==========================================================================================================================================
760 oC_ErrorCode_t oC_TIMER_LLD_ChangeMatchValue( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , uint64_t Value )
761 {
762  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
763 
764  if(
765  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet ) &&
766  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,Channel) , oC_ErrorCode_WrongChannel ) &&
767  oC_AssignErrorCodeIfFalse(&errorCode , oC_TIMER_LLD_IsSubTimerCorrect(SubTimer) , oC_ErrorCode_TIMERSubTimerNotCorrect ) &&
768  oC_AssignErrorCodeIfFalse(&errorCode , IsChannelPoweredOn(Channel) , oC_ErrorCode_ChannelNotPoweredOn ) &&
769  oC_AssignErrorCodeIfFalse(&errorCode , IsModeSelected(Channel,SubTimer) , oC_ErrorCode_TIMERModeNotSelected )
770  )
771  {
772  if( (Value <= oC_uint16_MAX) || (IsWideChannel(Channel) && (Value <= oC_uint32_MAX)) )
773  {
774  if(GetMode(Channel,SubTimer) != oC_TIMER_LLD_Mode_RealTimeClock)
775  {
776  SetTimerWidth(Channel , GPTMCFG_TimerWidth_Half);
777  }
778  errorCode = oC_ErrorCode_None;
779  }
780  else if ( CanBeFullWidth(Channel,SubTimer) && ( (!IsWideChannel(Channel) && (Value <= oC_uint32_MAX)) || IsWideChannel(Channel) ) )
781  {
782  if(SubTimer & oC_TIMER_LLD_SubTimer_Both)
783  {
784  SetTimerWidth(Channel , GPTMCFG_TimerWidth_Full);
785  errorCode = oC_ErrorCode_None;
786  }
787  else
788  {
789  errorCode = oC_ErrorCode_TIMERBothSubTimersNeeeded;
790  }
791  }
792  else
793  {
794  errorCode = oC_ErrorCode_TIMERMaximumValueTooBig;
795  }
796 
797  if(errorCode == oC_ErrorCode_None)
798  {
799  SetMatchValue(Channel,SubTimer,Value);
800  }
801  }
802 
803  return errorCode;
804 }
805 
806 //==========================================================================================================================================
811 //==========================================================================================================================================
812 oC_ErrorCode_t oC_TIMER_LLD_ReadMatchValue( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , uint64_t * outValue )
813 {
814  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
815 
816  if(
817  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet ) &&
818  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,Channel) , oC_ErrorCode_WrongChannel ) &&
819  oC_AssignErrorCodeIfFalse(&errorCode , oC_TIMER_LLD_IsSubTimerCorrect(SubTimer) , oC_ErrorCode_TIMERSubTimerNotCorrect ) &&
820  oC_AssignErrorCodeIfFalse(&errorCode , IsChannelPoweredOn(Channel) , oC_ErrorCode_ChannelNotPoweredOn ) &&
821  oC_AssignErrorCodeIfFalse(&errorCode , oC_MEM_LLD_IsRamAddress(outValue) , oC_ErrorCode_OutputAddressNotInRAM )
822  )
823  {
824  *outValue = GetMatchValue(Channel,SubTimer);
825  errorCode = oC_ErrorCode_None;
826  }
827 
828  return errorCode;
829 }
830 
831 //==========================================================================================================================================
836 //==========================================================================================================================================
837 oC_ErrorCode_t oC_TIMER_LLD_ChangeMode( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , oC_TIMER_LLD_Mode_t Mode )
838 {
839  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
840 
841  if(
842  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet ) &&
843  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,Channel) , oC_ErrorCode_WrongChannel ) &&
844  oC_AssignErrorCodeIfFalse(&errorCode , oC_TIMER_LLD_IsSubTimerCorrect(SubTimer) , oC_ErrorCode_TIMERSubTimerNotCorrect ) &&
845  oC_AssignErrorCodeIfFalse(&errorCode , !IsTimerEnabled(Channel) , oC_ErrorCode_TIMERChannelNotStopped ) &&
846  oC_AssignErrorCodeIfFalse(&errorCode , IsChannelPoweredOn(Channel) , oC_ErrorCode_ChannelNotPoweredOn )
847  )
848  {
849  SetMode(Channel,SubTimer,Mode);
850  errorCode = InitializeMode(Channel,SubTimer,Mode);
851  }
852 
853  return errorCode;
854 }
855 
856 //==========================================================================================================================================
861 //==========================================================================================================================================
862 oC_ErrorCode_t oC_TIMER_LLD_ReadMode( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , oC_TIMER_LLD_Mode_t * outMode )
863 {
864  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
865 
866  if(
867  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet ) &&
868  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,Channel) , oC_ErrorCode_WrongChannel ) &&
869  oC_AssignErrorCodeIfFalse(&errorCode , oC_TIMER_LLD_IsSubTimerCorrect(SubTimer) , oC_ErrorCode_TIMERSubTimerNotCorrect ) &&
870  oC_AssignErrorCodeIfFalse(&errorCode , oC_MEM_LLD_IsRamAddress(outMode) , oC_ErrorCode_OutputAddressNotInRAM)
871  )
872  {
873  *outMode = GetMode(Channel,SubTimer);
874  errorCode = oC_ErrorCode_None;
875  }
876 
877  return errorCode;
878 }
879 
880 //==========================================================================================================================================
885 //==========================================================================================================================================
886 oC_ErrorCode_t oC_TIMER_LLD_ChangeCountDirection( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , oC_TIMER_LLD_CountDirection_t CountDirection )
887 {
888  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
889 
890  if(
891  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet ) &&
892  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,Channel) , oC_ErrorCode_WrongChannel ) &&
893  oC_AssignErrorCodeIfFalse(&errorCode , oC_TIMER_LLD_IsSubTimerCorrect(SubTimer) , oC_ErrorCode_TIMERSubTimerNotCorrect ) &&
894  oC_AssignErrorCodeIfFalse(&errorCode , CountDirection <= oC_TIMER_LLD_CountDirection_Down , oC_ErrorCode_TIMERCountsDirNotCorrect ) &&
895  oC_AssignErrorCodeIfFalse(&errorCode , IsChannelPoweredOn(Channel) , oC_ErrorCode_ChannelNotPoweredOn ) &&
896  oC_AssignErrorCodeIfFalse(&errorCode , !IsTimerEnabled(Channel) , oC_ErrorCode_TIMERChannelNotStopped ) &&
897  oC_AssignErrorCodeIfFalse(&errorCode , IsModeSelected(Channel,SubTimer) , oC_ErrorCode_TIMERModeNotSelected )
898  )
899  {
900  uint32_t prescaler = GetPrescaler(Channel,SubTimer);
901 
902  /* Saved mode of the timer */
903  switch(GetMode(Channel,SubTimer))
904  {
905  /* ****************************************************************************************************************************
906  *
907  * For most of timer modes each count direction is possible.
908  *
909  * ****************************************************************************************************************************/
910  default:
911  if((prescaler > 1) && (CountDirection == oC_TIMER_LLD_CountDirection_Up))
912  {
913  errorCode = oC_ErrorCode_TIMERCountsDirNotPossible;
914  }
915  else
916  {
917  /* If it does not matter what the direction is, then just set it to down */
918  if(CountDirection == oC_TIMER_LLD_CountDirection_DoesntMatter)
919  {
920  CountDirection = oC_TIMER_LLD_CountDirection_Down;
921  }
922 
923  errorCode = oC_ErrorCode_None;
924  }
925 
926  break;
927 
928  /* ****************************************************************************************************************************
929  *
930  * For PWM timer mode it is possible to configure only down counting direction.
931  *
932  * ****************************************************************************************************************************/
933  case oC_TIMER_LLD_Mode_PWM:
934  if(CountDirection == oC_TIMER_LLD_CountDirection_Up)
935  {
936  errorCode = oC_ErrorCode_TIMERCountsDirNotPossible;
937  }
938  else
939  {
940  CountDirection = oC_TIMER_LLD_CountDirection_Down;
941  errorCode = oC_ErrorCode_None;
942  }
943  break;
944 
945  /* ****************************************************************************************************************************
946  *
947  * For RTC timer mode it is possible to configure only up counting direction.
948  *
949  * ****************************************************************************************************************************/
950  case oC_TIMER_LLD_Mode_RealTimeClock:
951 
952  if(CountDirection == oC_TIMER_LLD_CountDirection_Down)
953  {
954  errorCode = oC_ErrorCode_TIMERCountsDirNotPossible;
955  }
956  else
957  {
958  CountDirection = oC_TIMER_LLD_CountDirection_Up;
959  errorCode = oC_ErrorCode_None;
960  }
961 
962  break;
963  }
964 
965  /* If there was not any error, then it can be set to new direction */
966  if(errorCode == oC_ErrorCode_None)
967  {
968  SetCountDirection(Channel,SubTimer,CountDirection);
969  }
970 
971  }
972 
973  return errorCode;
974 }
975 
976 //==========================================================================================================================================
981 //==========================================================================================================================================
982 oC_ErrorCode_t oC_TIMER_LLD_ReadCountDirection( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , oC_TIMER_LLD_CountDirection_t * outCountDirection )
983 {
984  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
985 
986  if(
987  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet ) &&
988  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,Channel) , oC_ErrorCode_WrongChannel ) &&
989  oC_AssignErrorCodeIfFalse(&errorCode , oC_TIMER_LLD_IsSubTimerCorrect(SubTimer) , oC_ErrorCode_TIMERSubTimerNotCorrect ) &&
990  oC_AssignErrorCodeIfFalse(&errorCode , IsChannelPoweredOn(Channel) , oC_ErrorCode_ChannelNotPoweredOn ) &&
991  oC_AssignErrorCodeIfFalse(&errorCode , oC_MEM_LLD_IsRamAddress(outCountDirection) , oC_ErrorCode_OutputAddressNotInRAM)
992  )
993  {
994  *outCountDirection = GetCountDirection(Channel,SubTimer);
995  errorCode = oC_ErrorCode_None;
996  }
997 
998  return errorCode;
999 }
1000 
1001 //==========================================================================================================================================
1006 //==========================================================================================================================================
1007 oC_ErrorCode_t oC_TIMER_LLD_ChangeTrigger( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , oC_TIMER_LLD_Trigger_t Trigger )
1008 {
1009  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
1010 
1011  if(
1012  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet ) &&
1013  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,Channel) , oC_ErrorCode_WrongChannel ) &&
1014  oC_AssignErrorCodeIfFalse(&errorCode , oC_TIMER_LLD_IsSubTimerCorrect(SubTimer) , oC_ErrorCode_TIMERSubTimerNotCorrect ) &&
1015  oC_AssignErrorCodeIfFalse(&errorCode , IsChannelPoweredOn(Channel) , oC_ErrorCode_ChannelNotPoweredOn ) &&
1016  oC_AssignErrorCodeIfFalse(&errorCode , IsModeSelected(Channel,SubTimer) , oC_ErrorCode_TIMERModeNotSelected )
1017  )
1018  {
1019  /* If trigger is set to None, then no action is needed */
1020  if(Trigger == oC_TIMER_LLD_Trigger_None)
1021  {
1022  errorCode = oC_ErrorCode_None;
1023  }
1024  else
1025  {
1026  switch(GetMode(Channel,SubTimer))
1027  {
1028  /* *************************************************************************************************************************
1029  *
1030  * If it is input mode, then trigger can be set
1031  *
1032  **************************************************************************************************************************/
1033  case oC_TIMER_LLD_Mode_InputEdgeCount:
1034  case oC_TIMER_LLD_Mode_InputEdgeTime:
1035  case oC_TIMER_LLD_Mode_PWM:
1036 
1037  if(Trigger == oC_TIMER_LLD_Trigger_Both)
1038  {
1039  SetInputTrigger(Channel,SubTimer,GPTMCTL_TxEVENT_BothEdges);
1040  errorCode = oC_ErrorCode_None;
1041  }
1042  else if(Trigger == oC_TIMER_LLD_Trigger_RisingEdge)
1043  {
1044  SetInputTrigger(Channel,SubTimer,GPTMCTL_TxEVENT_PositiveEdge);
1045  errorCode = oC_ErrorCode_None;
1046  }
1047  else if(Trigger == oC_TIMER_LLD_Trigger_FallingEdge)
1048  {
1049  SetInputTrigger(Channel,SubTimer,GPTMCTL_TxEVENT_NegativeEdge);
1050  errorCode = oC_ErrorCode_None;
1051  }
1052  else
1053  {
1054  errorCode = oC_ErrorCode_TIMERTriggerNotCorrect;
1055  }
1056 
1057  break;
1058  /* *************************************************************************************************************************
1059  *
1060  * Only input modes allow to configure trigger
1061  *
1062  **************************************************************************************************************************/
1063  default:
1064  errorCode = oC_ErrorCode_TIMERModeNotCorrect;
1065  break;
1066  }
1067 
1068  }
1069  }
1070 
1071  return errorCode;
1072 }
1073 
1074 //==========================================================================================================================================
1079 //==========================================================================================================================================
1080 oC_ErrorCode_t oC_TIMER_LLD_ReadTrigger( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , oC_TIMER_LLD_Trigger_t * outTrigger )
1081 {
1082  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
1083 
1084  if(
1085  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet ) &&
1086  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,Channel) , oC_ErrorCode_WrongChannel ) &&
1087  oC_AssignErrorCodeIfFalse(&errorCode , oC_TIMER_LLD_IsSubTimerCorrect(SubTimer) , oC_ErrorCode_TIMERSubTimerNotCorrect ) &&
1088  oC_AssignErrorCodeIfFalse(&errorCode , IsChannelPoweredOn(Channel) , oC_ErrorCode_ChannelNotPoweredOn ) &&
1089  oC_AssignErrorCodeIfFalse(&errorCode , IsModeSelected(Channel,SubTimer) , oC_ErrorCode_TIMERModeNotSelected ) &&
1090  oC_AssignErrorCodeIfFalse(&errorCode , oC_MEM_LLD_IsRamAddress(outTrigger) , oC_ErrorCode_OutputAddressNotInRAM )
1091  )
1092  {
1093  GPTMCTL_TxEVENT_t trigger = GetInputTrigger(Channel,SubTimer);
1094 
1095  errorCode = oC_ErrorCode_None;
1096 
1097  switch(trigger)
1098  {
1099  case GPTMCTL_TxEVENT_NegativeEdge: *outTrigger = oC_TIMER_LLD_Trigger_FallingEdge; break;
1100  case GPTMCTL_TxEVENT_PositiveEdge: *outTrigger = oC_TIMER_LLD_Trigger_RisingEdge; break;
1101  case GPTMCTL_TxEVENT_BothEdges: *outTrigger = oC_TIMER_LLD_Trigger_Both; break;
1102  default: errorCode = oC_ErrorCode_MachineCanBeDamaged; break;
1103  }
1104  }
1105 
1106  return errorCode;
1107 }
1108 
1109 //==========================================================================================================================================
1114 //==========================================================================================================================================
1115 oC_ErrorCode_t oC_TIMER_LLD_ChangeEventHandler( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , oC_TIMER_LLD_EventHandler_t EventHandler , oC_TIMER_LLD_EventFlags_t EventFlags )
1116 {
1117  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
1118 
1119  if(
1120  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet ) &&
1121  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,Channel) , oC_ErrorCode_WrongChannel ) &&
1122  oC_AssignErrorCodeIfFalse(&errorCode , oC_TIMER_LLD_IsSubTimerCorrect(SubTimer) , oC_ErrorCode_TIMERSubTimerNotCorrect ) &&
1123  oC_AssignErrorCodeIfFalse(&errorCode , IsChannelPoweredOn(Channel) , oC_ErrorCode_ChannelNotPoweredOn ) &&
1124  oC_AssignErrorCodeIfFalse(&errorCode , oC_MEM_LLD_IsRamAddress(EventHandler) || oC_MEM_LLD_IsFlashAddress(EventHandler) , oC_ErrorCode_WrongAddress)
1125  )
1126  {
1127  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
1128  {
1129  EventHandlers[oC_TIMER_LLD_ChannelToChannelIndex(Channel)][0] = EventHandler;
1130  }
1131  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerB)
1132  {
1133  EventHandlers[oC_TIMER_LLD_ChannelToChannelIndex(Channel)][1] = EventHandler;
1134  }
1135  EnableEvents(Channel,SubTimer,EventFlags);
1136  errorCode = oC_ErrorCode_None;
1137  }
1138 
1139  return errorCode;
1140 }
1141 
1142 //==========================================================================================================================================
1147 //==========================================================================================================================================
1148 oC_ErrorCode_t oC_TIMER_LLD_ReadEventHandler(oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , oC_TIMER_LLD_EventHandler_t * outEventHandler , oC_TIMER_LLD_EventFlags_t * outEventFlags )
1149 {
1150  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
1151 
1152  if(
1153  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet ) &&
1154  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,Channel) , oC_ErrorCode_WrongChannel ) &&
1155  oC_AssignErrorCodeIfFalse(&errorCode , oC_TIMER_LLD_IsSubTimerCorrect(SubTimer) , oC_ErrorCode_TIMERSubTimerNotCorrect ) &&
1156  oC_AssignErrorCodeIfFalse(&errorCode , IsChannelPoweredOn(Channel) , oC_ErrorCode_ChannelNotPoweredOn ) &&
1157  oC_AssignErrorCodeIfFalse(&errorCode , oC_MEM_LLD_IsRamAddress(outEventHandler) , oC_ErrorCode_OutputAddressNotInRAM ) &&
1158  oC_AssignErrorCodeIfFalse(&errorCode , oC_MEM_LLD_IsRamAddress(outEventFlags) , oC_ErrorCode_OutputAddressNotInRAM )
1159  )
1160  {
1161  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
1162  {
1163  *outEventHandler = EventHandlers[oC_TIMER_LLD_ChannelToChannelIndex(Channel)][0];
1164  *outEventFlags = EventFlagsArray[oC_TIMER_LLD_ChannelToChannelIndex(Channel)][0];
1165  }
1166  else if(SubTimer & oC_TIMER_LLD_SubTimer_TimerB)
1167  {
1168  *outEventHandler = EventHandlers[oC_TIMER_LLD_ChannelToChannelIndex(Channel)][1];
1169  *outEventFlags = EventFlagsArray[oC_TIMER_LLD_ChannelToChannelIndex(Channel)][1];
1170  }
1171  errorCode = oC_ErrorCode_None;
1172  }
1173 
1174  return errorCode;
1175 }
1176 
1177 //==========================================================================================================================================
1182 //==========================================================================================================================================
1183 oC_ErrorCode_t oC_TIMER_LLD_ChangeStartPwmState( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , oC_TIMER_LLD_PwmState_t State)
1184 {
1185  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
1186 
1187  if(
1188  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet ) &&
1189  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,Channel) , oC_ErrorCode_WrongChannel ) &&
1190  oC_AssignErrorCodeIfFalse(&errorCode , oC_TIMER_LLD_IsSubTimerCorrect(SubTimer) , oC_ErrorCode_TIMERSubTimerNotCorrect ) &&
1191  oC_AssignErrorCodeIfFalse(&errorCode , IsChannelPoweredOn(Channel) , oC_ErrorCode_ChannelNotPoweredOn ) &&
1192  oC_AssignErrorCodeIfFalse(&errorCode , IsModeSelected(Channel,SubTimer) , oC_ErrorCode_TIMERModeNotSelected ) &&
1193  oC_AssignErrorCodeIfFalse(&errorCode , GetMode(Channel,SubTimer) == oC_TIMER_LLD_Mode_PWM , oC_ErrorCode_TIMERModeNotCorrect )
1194  )
1195  {
1196  if(oC_TIMER_LLD_PwmState_Low == State)
1197  {
1198  SetPwmOutput(Channel,SubTimer,GPTMCTL_TxPWML_PwmOutput_Inverted);
1199  }
1200  else
1201  {
1202  SetPwmOutput(Channel,SubTimer,GPTMCTL_TxPWML_PwmOutput_Unaffected);
1203  }
1204  errorCode = oC_ErrorCode_None;
1205  }
1206 
1207  return errorCode;
1208 }
1209 
1210 //==========================================================================================================================================
1215 //==========================================================================================================================================
1216 oC_ErrorCode_t oC_TIMER_LLD_ReadStartPwmState( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , oC_TIMER_LLD_PwmState_t * outState)
1217 {
1218  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
1219 
1220  if(
1221  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet ) &&
1222  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,Channel) , oC_ErrorCode_WrongChannel ) &&
1223  oC_AssignErrorCodeIfFalse(&errorCode , oC_TIMER_LLD_IsSubTimerCorrect(SubTimer) , oC_ErrorCode_TIMERSubTimerNotCorrect ) &&
1224  oC_AssignErrorCodeIfFalse(&errorCode , IsChannelPoweredOn(Channel) , oC_ErrorCode_ChannelNotPoweredOn ) &&
1225  oC_AssignErrorCodeIfFalse(&errorCode , oC_MEM_LLD_IsRamAddress(outState) , oC_ErrorCode_OutputAddressNotInRAM) &&
1226  oC_AssignErrorCodeIfFalse(&errorCode , IsModeSelected(Channel,SubTimer) , oC_ErrorCode_TIMERModeNotSelected ) &&
1227  oC_AssignErrorCodeIfFalse(&errorCode , GetMode(Channel,SubTimer) == oC_TIMER_LLD_Mode_PWM , oC_ErrorCode_TIMERModeNotCorrect )
1228  )
1229  {
1230  if(GPTMCTL_TxPWML_PwmOutput_Inverted == GetPwmOutput(Channel,SubTimer))
1231  {
1232  *outState = oC_TIMER_LLD_PwmState_Low;
1233  }
1234  else
1235  {
1236  *outState = oC_TIMER_LLD_PwmState_High;
1237  }
1238  errorCode = oC_ErrorCode_None;
1239  }
1240 
1241  return errorCode;
1242 }
1243 
1244 //==========================================================================================================================================
1249 //==========================================================================================================================================
1250 oC_ErrorCode_t oC_TIMER_LLD_ConnectModulePin(oC_TIMER_Pin_t ModulePin )
1251 {
1252  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
1253  oC_TIMER_Channel_t channel = oC_TIMER_LLD_GetChannelOfModulePin(ModulePin);
1254  oC_TIMER_LLD_SubTimer_t subTimer;
1255 
1256  if( oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet ) )
1257  {
1258  oC_Pin_t pin = oC_ModulePin_GetPin(ModulePin);
1259 
1260  if(
1261  ErrorCondition( IsChannelCorrect(channel) , oC_ErrorCode_WrongChannel ) &&
1262  ErrorCondition( oC_GPIO_LLD_IsPinDefined(pin) , oC_ErrorCode_PinNotDefined )
1263  )
1264  {
1265  bool unused = false;
1266 
1268 
1269  if(
1270  oC_AssignErrorCode( &errorCode , oC_GPIO_LLD_ArePinsUnused(pin,&unused) ) &&
1271  ErrorCondition( unused , oC_ErrorCode_PinIsUsed ) &&
1272  oC_AssignErrorCode( &errorCode , oC_GPIO_LLD_SetPinsUsed(pin) ) &&
1273  oC_AssignErrorCode( &errorCode , oC_TIMER_LLD_ReadSubTimerOfModulePin(ModulePin,&subTimer)) &&
1274  oC_AssignErrorCodeIfFalse( &errorCode , oC_TIMER_LLD_IsSubTimerCorrect(subTimer) , oC_ErrorCode_TIMERSubTimerNotCorrect ) &&
1275  oC_AssignErrorCodeIfFalse( &errorCode , IsModeSelected(channel,subTimer) , oC_ErrorCode_TIMERModeNotSelected ) &&
1276  oC_AssignErrorCodeIfFalse( &errorCode , IsChannelPoweredOn(channel) , oC_ErrorCode_ChannelNotPoweredOn ) &&
1277  oC_AssignErrorCode( &errorCode , oC_GPIO_MSLLD_ConnectModulePin(ModulePin) )
1278  )
1279  {
1280  errorCode = oC_ErrorCode_None;
1281  }
1282  else
1283  {
1284  oC_GPIO_LLD_SetPinsUnused(pin);
1285  }
1286 
1288  }
1289  }
1290  return errorCode;
1291 }
1292 
1293 //==========================================================================================================================================
1298 //==========================================================================================================================================
1299 oC_ErrorCode_t oC_TIMER_LLD_ReadSubTimerOfModulePin(oC_TIMER_Pin_t PeripheralPin , oC_TIMER_LLD_SubTimer_t * outSubTimer )
1300 {
1301  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
1302  oC_TIMER_Channel_t channel = oC_TIMER_LLD_GetChannelOfModulePin(PeripheralPin);
1303 
1304  if(
1305  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet ) &&
1306  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,channel) , oC_ErrorCode_WrongChannel ) &&
1307  oC_AssignErrorCodeIfFalse(&errorCode , oC_TIMER_LLD_IsModulePinDefined(PeripheralPin) , oC_ErrorCode_PeripheralPinNotDefined ) &&
1308  oC_AssignErrorCodeIfFalse(&errorCode , oC_MEM_LLD_IsRamAddress(outSubTimer) , oC_ErrorCode_OutputAddressNotInRAM)
1309  )
1310  {
1311 
1312  oC_PinFunction_t pinFunction = oC_ModulePin_GetPinFunction(PeripheralPin);
1313 
1314  /*
1315  * This is wrong way for recognize sub-timer, but it is the best that I have for now...
1316  */
1317  if(pinFunction)
1318  {
1319  *outSubTimer = oC_TIMER_LLD_SubTimer_TimerB;
1320  }
1321  else
1322  {
1323  *outSubTimer = oC_TIMER_LLD_SubTimer_TimerA;
1324  }
1325 
1326  errorCode = oC_ErrorCode_None;
1327  }
1328 
1329  return errorCode;
1330 }
1331 
1332 //==========================================================================================================================================
1337 //==========================================================================================================================================
1338 oC_ErrorCode_t oC_TIMER_LLD_SetChannelUsed( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer )
1339 {
1340  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
1341 
1342  if(
1343  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet) &&
1344  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,Channel) , oC_ErrorCode_WrongChannel ) &&
1345  oC_AssignErrorCodeIfFalse(&errorCode , oC_TIMER_LLD_IsSubTimerCorrect(SubTimer) , oC_ErrorCode_TIMERSubTimerNotCorrect) &&
1346  oC_AssignErrorCodeIfFalse(&errorCode , !oC_TIMER_LLD_IsChannelUsed(Channel,SubTimer), oC_ErrorCode_ChannelIsUsed)
1347  )
1348  {
1349  SetMode(Channel,SubTimer,oC_TIMER_LLD_Mode_Reserved);
1350  errorCode = oC_ErrorCode_None;
1351  }
1352 
1353  return errorCode;
1354 }
1355 
1356 //==========================================================================================================================================
1361 //==========================================================================================================================================
1362 oC_ErrorCode_t oC_TIMER_LLD_SetChannelUnused( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer )
1363 {
1364  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
1365 
1366  if(
1367  oC_AssignErrorCodeIfFalse(&errorCode , ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet) &&
1368  oC_AssignErrorCodeIfFalse(&errorCode , oC_Channel_IsCorrect(TIMER,Channel) , oC_ErrorCode_WrongChannel ) &&
1369  oC_AssignErrorCodeIfFalse(&errorCode , oC_TIMER_LLD_IsSubTimerCorrect(SubTimer) , oC_ErrorCode_TIMERSubTimerNotCorrect)
1370  )
1371  {
1372  SetMode(Channel,SubTimer,oC_TIMER_LLD_Mode_NotSelected);
1373  errorCode = oC_ErrorCode_None;
1374  }
1375 
1376  return errorCode;
1377 }
1378 
1379 //==========================================================================================================================================
1384 //==========================================================================================================================================
1385 bool oC_TIMER_LLD_IsChannelUsed( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer )
1386 {
1387  return oC_Channel_IsCorrect(TIMER,Channel) && oC_TIMER_LLD_IsSubTimerCorrect(SubTimer) && (GetMode(Channel,SubTimer) != oC_TIMER_LLD_Mode_NotSelected);
1388 }
1389 
1390 //==========================================================================================================================================
1395 //==========================================================================================================================================
1396 oC_ErrorCode_t oC_TIMER_LLD_ReadModulePinsOfPin( oC_Pins_t Pin , oC_TIMER_Pin_t * outPeripheralPinsArray , uint32_t * ArraySize )
1397 {
1398  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
1399 
1400  if(
1401  oC_AssignErrorCodeIfFalse(&errorCode , oC_GPIO_LLD_IsSinglePin(Pin) , oC_ErrorCode_NotSinglePin ) &&
1402  oC_AssignErrorCodeIfFalse(&errorCode , oC_TIMER_LLD_IsModulePinDefined(Pin) , oC_ErrorCode_PinNotDefined ) &&
1403  oC_AssignErrorCodeIfFalse(&errorCode , oC_MEM_LLD_IsRamAddress(outPeripheralPinsArray) , oC_ErrorCode_OutputAddressNotInRAM ) &&
1404  oC_AssignErrorCodeIfFalse(&errorCode , oC_MEM_LLD_IsRamAddress(ArraySize) , oC_ErrorCode_OutputAddressNotInRAM ) &&
1405  oC_AssignErrorCodeIfFalse(&errorCode , (*ArraySize) < 255 , oC_ErrorCode_ValueTooBig )
1406  )
1407  {
1408  uint8_t outArrayIndex = 0;
1409 
1410  errorCode = oC_ErrorCode_None;
1411 
1412  oC_ModulePin_ForeachDefined(modulePin)
1413  {
1414  if(modulePin->Pin == Pin && oC_Channel_IsCorrect(UART,modulePin->Channel))
1415  {
1416  if(outArrayIndex < (*ArraySize))
1417  {
1418  outPeripheralPinsArray[outArrayIndex++] = modulePin->ModulePinIndex;
1419  }
1420  else
1421  {
1422  errorCode = oC_ErrorCode_OutputArrayToSmall;
1423  }
1424  }
1425  }
1426 
1427  if(outArrayIndex == 0)
1428  {
1429  errorCode = oC_ErrorCode_PeripheralPinNotDefined;
1430  }
1431 
1432  *ArraySize = outArrayIndex;
1433  }
1434 
1435  return errorCode;
1436 }
1437 
1438 #undef _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________
1439 
1440 
1446 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
1447 
1448 //==========================================================================================================================================
1452 //==========================================================================================================================================
1453 static inline bool IsModeSelected( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer)
1454 {
1455  oC_TIMER_LLD_Mode_t mode = GetMode(Channel,SubTimer);
1456  return (mode > oC_TIMER_LLD_Mode_Reserved) &&
1457  (mode <= oC_TIMER_LLD_Mode_PWM);
1458 }
1459 
1460 //==========================================================================================================================================
1464 //==========================================================================================================================================
1465 static inline bool IsTimerEnabled( oC_TIMER_Channel_t Channel )
1466 {
1467  return GPTMCTL(Channel)->TAEN && GPTMCTL(Channel)->TBEN;
1468 }
1469 
1470 //==========================================================================================================================================
1474 //==========================================================================================================================================
1475 static inline bool IsWideChannel( oC_TIMER_Channel_t Channel )
1476 {
1477  return GPTMPP(Channel)->SIZE == 1;
1478 }
1479 
1480 //==========================================================================================================================================
1484 //==========================================================================================================================================
1485 static inline bool CanBeFullWidth( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer )
1486 {
1487  oC_TIMER_LLD_Mode_t mode = GetMode(Channel,SubTimer);
1488  uint32_t prescaler = GetPrescaler(Channel,SubTimer);
1489 
1490  return
1491  (
1492  mode == oC_TIMER_LLD_Mode_RealTimeClock ||
1493  mode == oC_TIMER_LLD_Mode_PeriodicTimer
1494  ) &&
1495  (prescaler == 1);
1496 
1497 }
1498 
1499 //==========================================================================================================================================
1503 //==========================================================================================================================================
1504 static inline void SetCountDirection( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , oC_TIMER_LLD_CountDirection_t CountDirection )
1505 {
1506  if(CountDirection == oC_TIMER_LLD_CountDirection_Up)
1507  {
1508  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
1509  {
1510  GPTMTAMR(Channel)->TACDIR = 1;
1511  }
1512  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerB)
1513  {
1514  GPTMTBMR(Channel)->TBCDIR = 1;
1515  }
1516  }
1517  else
1518  {
1519  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
1520  {
1521  GPTMTAMR(Channel)->TACDIR = 0;
1522  }
1523  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerB)
1524  {
1525  GPTMTBMR(Channel)->TBCDIR = 0;
1526  }
1527  }
1528 }
1529 
1530 //==========================================================================================================================================
1534 //==========================================================================================================================================
1535 static inline oC_TIMER_LLD_CountDirection_t GetCountDirection( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer )
1536 {
1537  oC_TIMER_LLD_CountDirection_t countDirection;
1538 
1539  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
1540  {
1541  if(GPTMTAMR(Channel)->TACDIR == 1)
1542  {
1543  countDirection = oC_TIMER_LLD_CountDirection_Up;
1544  }
1545  else
1546  {
1547  countDirection = oC_TIMER_LLD_CountDirection_Down;
1548  }
1549  }
1550  else
1551  {
1552  if(GPTMTBMR(Channel)->TBCDIR == 1)
1553  {
1554  countDirection = oC_TIMER_LLD_CountDirection_Up;
1555  }
1556  else
1557  {
1558  countDirection = oC_TIMER_LLD_CountDirection_Down;
1559  }
1560  }
1561 
1562  return countDirection;
1563 }
1564 
1565 //==========================================================================================================================================
1569 //==========================================================================================================================================
1570 static inline void SetTimersMode( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , GPTMTxMR_TxMR_t Mode )
1571 {
1572  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
1573  {
1574  GPTMTAMR(Channel)->TAMR = Mode;
1575  }
1576  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
1577  {
1578  GPTMTBMR(Channel)->TBMR = Mode;
1579  }
1580 }
1581 
1582 //==========================================================================================================================================
1586 //==========================================================================================================================================
1587 static inline void SetInputTrigger( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , GPTMCTL_TxEVENT_t Trigger )
1588 {
1589  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
1590  {
1591  GPTMCTL(Channel)->TAEVENT = Trigger;
1592  }
1593  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerB)
1594  {
1595  GPTMCTL(Channel)->TBEVENT = Trigger;
1596  }
1597 }
1598 
1599 //==========================================================================================================================================
1603 //==========================================================================================================================================
1604 static inline GPTMCTL_TxEVENT_t GetInputTrigger( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer )
1605 {
1606  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
1607  {
1608  return GPTMCTL(Channel)->TAEVENT;
1609  }
1610  else
1611  {
1612  return GPTMCTL(Channel)->TBEVENT;
1613  }
1614 }
1615 
1616 //==========================================================================================================================================
1620 //==========================================================================================================================================
1621 static inline void EnableTimer( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer )
1622 {
1623  if(oC_Machine_IsChannelPoweredOn(Channel))
1624  {
1625  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
1626  {
1627  GPTMCTL(Channel)->TAEN = 1;
1628  }
1629 
1630  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerB)
1631  {
1632  GPTMCTL(Channel)->TBEN = 1;
1633  }
1634  }
1635 }
1636 
1637 //==========================================================================================================================================
1641 //==========================================================================================================================================
1642 static inline void DisableTimer( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer )
1643 {
1644  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
1645  {
1646  GPTMCTL(Channel)->TAEN = 0;
1647  }
1648 
1649  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerB)
1650  {
1651  GPTMCTL(Channel)->TBEN = 0;
1652  }
1653 }
1654 
1655 //==========================================================================================================================================
1659 //==========================================================================================================================================
1660 static void CallEvent( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_ChannelIndex_t ChannelIndex , oC_TIMER_LLD_SubTimer_t SubTimer , oC_TIMER_LLD_EventFlags_t EventFlags )
1661 {
1662  oC_TIMER_LLD_EventHandler_t eventHandler = NULL;
1663  oC_TIMER_LLD_EventFlags_t enabledEventFlags = 0;
1664 
1665  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
1666  {
1667  eventHandler = EventHandlers[ChannelIndex][0];
1668  enabledEventFlags = EventFlagsArray[ChannelIndex][0];
1669  }
1670  else
1671  {
1672  eventHandler = EventHandlers[ChannelIndex][1];
1673  enabledEventFlags = EventFlagsArray[ChannelIndex][1];
1674  }
1675 
1676  if(eventHandler && (EventFlags & enabledEventFlags))
1677  {
1678  eventHandler(Channel,SubTimer,EventFlags);
1679  }
1680 }
1681 
1682 //==========================================================================================================================================
1686 //==========================================================================================================================================
1687 static inline uint32_t GetPrescaler( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer )
1688 {
1689  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
1690  return oC_Machine_ReadRegister( Channel, GPTMTAPR ) + 1;
1691  else
1692  return oC_Machine_ReadRegister( Channel, GPTMTBPR ) + 1;
1693 }
1694 
1695 //==========================================================================================================================================
1699 //==========================================================================================================================================
1700 static inline void SetTimerWidth( oC_TIMER_Channel_t Channel , GPTMCFG_TimerWidth_t TimerWidth )
1701 {
1702  oC_Machine_WriteRegister(Channel, GPTMCFG , TimerWidth);
1703 }
1704 
1705 //==========================================================================================================================================
1709 //==========================================================================================================================================
1710 static inline GPTMCFG_TimerWidth_t GetTimerWidth( oC_TIMER_Channel_t Channel )
1711 {
1712  return oC_Machine_ReadRegister(Channel, GPTMCFG) & 0x7;
1713 }
1714 
1715 //==========================================================================================================================================
1719 //==========================================================================================================================================
1720 static inline void SetMaxValue( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , uint64_t MaxValue )
1721 {
1722  uint32_t lowWord = 0;
1723  uint32_t highWord = 0;
1724 
1725  oC_Bits_SplitU64ToU32(MaxValue , &lowWord , &highWord);
1726 
1727  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
1728  {
1729  oC_Machine_WriteRegister(Channel, GPTMTAILR , lowWord );
1730  }
1731  if(oC_Bits_AreBitsSetU32(SubTimer , oC_TIMER_LLD_SubTimer_Both))
1732  {
1733  oC_Machine_WriteRegister(Channel, GPTMTBILR , highWord );
1734  }
1735  else if (SubTimer & oC_TIMER_LLD_SubTimer_TimerB)
1736  {
1737  oC_Machine_WriteRegister(Channel, GPTMTBILR , lowWord );
1738  }
1739 }
1740 
1741 //==========================================================================================================================================
1745 //==========================================================================================================================================
1746 static inline uint64_t GetMaxValue( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer )
1747 {
1748  uint64_t maxValue = 0;
1749  uint32_t lowWord = oC_Machine_ReadRegister(Channel, GPTMTAILR );
1750  uint32_t highWord = oC_Machine_ReadRegister(Channel, GPTMTBILR );
1751 
1752  if(oC_Bits_AreBitsSetU32(SubTimer,oC_TIMER_LLD_SubTimer_Both))
1753  {
1754  maxValue = oC_Bits_JoinU32ToU64(lowWord,highWord);
1755  }
1756  else if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
1757  {
1758  maxValue = lowWord;
1759  }
1760  else if(SubTimer & oC_TIMER_LLD_SubTimer_TimerB)
1761  {
1762  maxValue = highWord;
1763  }
1764 
1765  return maxValue;
1766 }
1767 
1768 //==========================================================================================================================================
1772 //==========================================================================================================================================
1773 static inline void SetValue( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , uint64_t Value )
1774 {
1775  uint32_t lowWord = 0;
1776  uint32_t highWord = 0;
1777 
1778  oC_Bits_SplitU64ToU32(Value , &lowWord , &highWord);
1779 
1780  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
1781  {
1782  oC_Machine_WriteRegister(Channel,GPTMTAV , lowWord );
1783  }
1784  if(oC_Bits_AreBitsSetU32(SubTimer , oC_TIMER_LLD_SubTimer_Both))
1785  {
1786  oC_Machine_WriteRegister(Channel, GPTMTBV , highWord );
1787  }
1788  else if (SubTimer & oC_TIMER_LLD_SubTimer_TimerB)
1789  {
1790  oC_Machine_WriteRegister(Channel, GPTMTBV , lowWord );
1791  }
1792 }
1793 
1794 //==========================================================================================================================================
1798 //==========================================================================================================================================
1799 static inline uint64_t GetValue( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer )
1800 {
1801  uint64_t maxValue = 0;
1802  uint32_t lowWord = oC_Machine_ReadRegister(Channel, GPTMTAR );
1803  uint32_t highWord = oC_Machine_ReadRegister(Channel, GPTMTBR );
1804 
1805  if(oC_Bits_AreBitsSetU32(SubTimer,oC_TIMER_LLD_SubTimer_Both))
1806  {
1807  maxValue = oC_Bits_JoinU32ToU64(lowWord,highWord);
1808  }
1809  else if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
1810  {
1811  maxValue = lowWord;
1812  }
1813  else if(SubTimer & oC_TIMER_LLD_SubTimer_TimerB)
1814  {
1815  maxValue = highWord;
1816  }
1817 
1818  return maxValue;
1819 }
1820 
1821 //==========================================================================================================================================
1825 //==========================================================================================================================================
1826 static inline void SetMatchValue( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , uint64_t Value )
1827 {
1828  uint32_t lowWord = 0;
1829  uint32_t highWord = 0;
1830 
1831  oC_Bits_SplitU64ToU32(Value , &lowWord , &highWord);
1832 
1833  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
1834  {
1835  oC_Machine_WriteRegister(Channel, GPTMTAMATCHR , lowWord );
1836  }
1837  if(oC_Bits_AreBitsSetU32(SubTimer , oC_TIMER_LLD_SubTimer_Both))
1838  {
1839  oC_Machine_WriteRegister(Channel, GPTMTBMATCHR , highWord );
1840  }
1841  else if (SubTimer & oC_TIMER_LLD_SubTimer_TimerB)
1842  {
1843  oC_Machine_WriteRegister(Channel, GPTMTBMATCHR , lowWord );
1844  }
1845 }
1846 
1847 //==========================================================================================================================================
1851 //==========================================================================================================================================
1852 static inline uint64_t GetMatchValue( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer )
1853 {
1854  uint64_t maxValue = 0;
1855  uint32_t lowWord = oC_Machine_ReadRegister(Channel, GPTMTAMATCHR );
1856  uint32_t highWord = oC_Machine_ReadRegister(Channel, GPTMTBMATCHR );
1857 
1858  if(oC_Bits_AreBitsSetU32(SubTimer,oC_TIMER_LLD_SubTimer_Both))
1859  {
1860  maxValue = oC_Bits_JoinU32ToU64(lowWord,highWord);
1861  }
1862  else if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
1863  {
1864  maxValue = lowWord;
1865  }
1866  else if(SubTimer & oC_TIMER_LLD_SubTimer_TimerB)
1867  {
1868  maxValue = highWord;
1869  }
1870 
1871  return maxValue;
1872 }
1873 
1874 //==========================================================================================================================================
1878 //==========================================================================================================================================
1879 static inline void SetAlternateMode( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , GPTMTxMR_TxAMS_AlternateMode_t Mode )
1880 {
1881  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
1882  {
1883  GPTMTAMR(Channel)->TAAMS = Mode;
1884  }
1885  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerB)
1886  {
1887  GPTMTBMR(Channel)->TBAMS = Mode;
1888  }
1889 }
1890 
1891 //==========================================================================================================================================
1895 //==========================================================================================================================================
1896 static inline void SetCaptureMode( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , GPTMTxMR_TxCMR_CaptureMode_t Mode )
1897 {
1898  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
1899  {
1900  GPTMTAMR(Channel)->TACMR = Mode;
1901  }
1902  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerB)
1903  {
1904  GPTMTBMR(Channel)->TBCMR = Mode;
1905  }
1906 }
1907 
1908 //==========================================================================================================================================
1912 //==========================================================================================================================================
1913 static inline void SetPwmOutput( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , GPTMCTL_TxPWML_PwmOutput_t PwmOutput )
1914 {
1915  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
1916  {
1917  GPTMCTL(Channel)->TAPWML = PwmOutput;
1918  }
1919  else
1920  {
1921  GPTMCTL(Channel)->TBPWML = PwmOutput;
1922  }
1923 }
1924 
1925 //==========================================================================================================================================
1929 //==========================================================================================================================================
1930 static inline GPTMCTL_TxPWML_PwmOutput_t GetPwmOutput( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer )
1931 {
1932  GPTMCTL_TxPWML_PwmOutput_t pwmOutput;
1933 
1934  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
1935  {
1936  pwmOutput = GPTMCTL(Channel)->TAPWML;
1937  }
1938  else
1939  {
1940  pwmOutput = GPTMCTL(Channel)->TBPWML;
1941  }
1942 
1943  return pwmOutput;
1944 }
1945 
1946 //==========================================================================================================================================
1950 //==========================================================================================================================================
1951 static void EnableEvents( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , oC_TIMER_LLD_EventFlags_t EventFlags )
1952 {
1953  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
1954  {
1955  EventFlagsArray[oC_TIMER_LLD_ChannelToChannelIndex(Channel)][0] = EventFlags;
1956  oC_Channel_EnableInterrupt(Channel,TimerA);
1957 
1958  if(EventFlags & oC_TIMER_LLD_EventFlags_TimeoutInterrupt)
1959  {
1960  GPTMIMR(Channel)->TATOIM = 1;
1961  }
1962 
1963  if(EventFlags & oC_TIMER_LLD_EventFlags_MatchValueInterrupt)
1964  {
1965  GPTMIMR(Channel)->TAMIM = 1;
1966  }
1967 
1968  if(
1969  ( EventFlags & oC_TIMER_LLD_EventFlags_PwmPinInLowState ) ||
1970  ( EventFlags & oC_TIMER_LLD_EventFlags_PwmPinInHighState )
1971 
1972  )
1973  {
1974  GPTMIMR(Channel)->CAEIM = 1;
1975  GPTMIMR(Channel)->CAEIM = 1;
1976  }
1977  }
1978  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerB)
1979  {
1980  EventFlagsArray[oC_TIMER_LLD_ChannelToChannelIndex(Channel)][1] = EventFlags;
1981  oC_Channel_EnableInterrupt(Channel,TimerB);
1982 
1983  if(EventFlags & oC_TIMER_LLD_EventFlags_TimeoutInterrupt)
1984  {
1985  GPTMIMR(Channel)->TBTOIM = 1;
1986  }
1987 
1988  if(EventFlags & oC_TIMER_LLD_EventFlags_MatchValueInterrupt)
1989  {
1990  GPTMIMR(Channel)->TBMIM = 1;
1991  }
1992 
1993  if(
1994  ( EventFlags & oC_TIMER_LLD_EventFlags_PwmPinInLowState ) ||
1995  ( EventFlags & oC_TIMER_LLD_EventFlags_PwmPinInHighState )
1996 
1997  )
1998  {
1999  GPTMIMR(Channel)->CBEIM = 1;
2000  GPTMIMR(Channel)->CBEIM = 1;
2001  }
2002  }
2003 
2004  if(
2005  oC_Bits_AreBitsSetU32(EventFlags , oC_TIMER_LLD_EventFlags_PwmPinInLowState) &&
2006  oC_Bits_AreBitsSetU32(EventFlags , oC_TIMER_LLD_EventFlags_PwmPinInHighState)
2007  )
2008  {
2009  SetEventMode(Channel,SubTimer,GPTMCTL_TxEVENT_EventMode_BothEdges);
2010  }
2011  else if (oC_Bits_AreBitsSetU32(EventFlags , oC_TIMER_LLD_EventFlags_PwmPinInLowState))
2012  {
2013  SetEventMode(Channel,SubTimer,GPTMCTL_TxEVENT_EventMode_NegativeEdge);
2014  }
2015  else if (oC_Bits_AreBitsSetU32(EventFlags , oC_TIMER_LLD_EventFlags_PwmPinInHighState))
2016  {
2017  SetEventMode(Channel,SubTimer,GPTMCTL_TxEVENT_EventMode_PositiveEdge);
2018  }
2019 }
2020 
2021 //==========================================================================================================================================
2025 //==========================================================================================================================================
2026 static void DisableEvents( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , oC_TIMER_LLD_EventFlags_t EventFlags )
2027 {
2028  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
2029  {
2030  EventFlagsArray[oC_TIMER_LLD_ChannelToChannelIndex(Channel)][0] &= ~EventFlags;
2031  oC_Channel_EnableInterrupt(Channel,TimerA);
2032 
2033  if(EventFlags & oC_TIMER_LLD_EventFlags_TimeoutInterrupt)
2034  {
2035  GPTMIMR(Channel)->TATOIM = 0;
2036  }
2037 
2038  if(EventFlags & oC_TIMER_LLD_EventFlags_MatchValueInterrupt)
2039  {
2040  GPTMIMR(Channel)->TAMIM = 0;
2041  }
2042 
2043  if(
2044  ( EventFlags & oC_TIMER_LLD_EventFlags_PwmPinInLowState ) ||
2045  ( EventFlags & oC_TIMER_LLD_EventFlags_PwmPinInHighState )
2046 
2047  )
2048  {
2049  GPTMIMR(Channel)->CAEIM = 0;
2050  GPTMIMR(Channel)->CAEIM = 0;
2051  }
2052  }
2053  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerB)
2054  {
2055  EventFlagsArray[oC_TIMER_LLD_ChannelToChannelIndex(Channel)][1] &= ~EventFlags;
2056  oC_Channel_EnableInterrupt(Channel,TimerB);
2057 
2058  if(EventFlags & oC_TIMER_LLD_EventFlags_TimeoutInterrupt)
2059  {
2060  GPTMIMR(Channel)->TBTOIM = 0;
2061  }
2062 
2063  if(EventFlags & oC_TIMER_LLD_EventFlags_MatchValueInterrupt)
2064  {
2065  GPTMIMR(Channel)->TBMIM = 0;
2066  }
2067 
2068  if(
2069  ( EventFlags & oC_TIMER_LLD_EventFlags_PwmPinInLowState ) ||
2070  ( EventFlags & oC_TIMER_LLD_EventFlags_PwmPinInHighState )
2071 
2072  )
2073  {
2074  GPTMIMR(Channel)->CBEIM = 0;
2075  GPTMIMR(Channel)->CBEIM = 0;
2076  }
2077  }
2078 
2079 }
2080 
2081 //==========================================================================================================================================
2085 //==========================================================================================================================================
2086 static void SetPwmInterrupt( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , GPTMTxMR_TxPWMIE_PwmInterruptEnable_t Enabled )
2087 {
2088  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
2089  {
2090  GPTMTAMR(Channel)->TAPWMIE = Enabled;
2091  }
2092  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerB)
2093  {
2094  GPTMTBMR(Channel)->TBPWMIE = Enabled;
2095  }
2096 }
2097 
2098 //==========================================================================================================================================
2102 //==========================================================================================================================================
2103 static inline void SetPwmLegacyOperations( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , GPTMTxMR_TxPLO_PwmLegacyOperations_t Enabled )
2104 {
2105  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
2106  {
2107  GPTMTAMR(Channel)->TAPLO = Enabled;
2108  }
2109 
2110  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerB)
2111  {
2112  GPTMTBMR(Channel)->TBPLO = Enabled;
2113  }
2114 }
2115 
2116 
2117 //==========================================================================================================================================
2121 //==========================================================================================================================================
2122 static inline oC_ErrorCode_t InitializeMode( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , oC_TIMER_LLD_Mode_t Mode )
2123 {
2124  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
2125  switch(Mode)
2126  {
2127  /* *****************************************************************************************************************************
2128  *
2129  * PWM must be in individual timer width, and can be done with periodic timer mode
2130  *
2131  ******************************************************************************************************************************/
2132  case oC_TIMER_LLD_Mode_PWM:
2133  SetTimerWidth(Channel , GPTMCFG_TimerWidth_Half);
2134  SetTimersMode(Channel , SubTimer , GPTMTxMR_TxMR_PeriodicTimerMode);
2135  SetAlternateMode(Channel , SubTimer , GPTMTxMR_TxAMS_AlternateMode_Pwm);
2136  SetPwmInterrupt(Channel , SubTimer , GPTMTxMR_TxPWMIE_PwmInterruptEnable_Enabled);
2137  SetCaptureMode(Channel , SubTimer , GPTMTxMR_TxCMR_CaptureMode_EdgeCount);
2138  SetPwmLegacyOperations(Channel, SubTimer , GPTMTxMR_TxPLO_PwmLegacyOperations_Enabled);
2139  SetPwmOutput(Channel, SubTimer , GPTMCTL_TxPWML_PwmOutput_Inverted);
2140  errorCode = oC_ErrorCode_None;
2141  break;
2142  /* *****************************************************************************************************************************
2143  *
2144  * These modes just need to set the periodic timer mode
2145  *
2146  ******************************************************************************************************************************/
2147  case oC_TIMER_LLD_Mode_PeriodicTimer:
2148  if(
2149  ((SubTimer & oC_TIMER_LLD_SubTimer_TimerA) == oC_TIMER_LLD_SubTimer_TimerA) ||
2150  ((SubTimer & oC_TIMER_LLD_SubTimer_TimerB) == oC_TIMER_LLD_SubTimer_TimerB)
2151  )
2152  {
2153  SetTimerWidth(Channel , GPTMCFG_TimerWidth_Half);
2154  }
2155  else
2156  {
2157  SetTimerWidth(Channel , GPTMCFG_TimerWidth_Full);
2158  }
2159 
2160  SetTimersMode(Channel , SubTimer , GPTMTxMR_TxMR_PeriodicTimerMode);
2161  SetAlternateMode(Channel , SubTimer , GPTMTxMR_TxAMS_AlternateMode_CaptureOrCompare);
2162  SetPwmLegacyOperations(Channel , SubTimer, GPTMTxMR_TxPLO_PwmLegacyOperations_Disabled);
2163  errorCode = oC_ErrorCode_None;
2164  break;
2165  /* *****************************************************************************************************************************
2166  *
2167  * In real time clock mode the timers mode selection is not needed, but timer width must be full
2168  *
2169  ******************************************************************************************************************************/
2170  case oC_TIMER_LLD_Mode_RealTimeClock:
2171  if(oC_Bits_AreBitsSetU32(SubTimer,oC_TIMER_LLD_SubTimer_Both))
2172  {
2173  SetTimerWidth(Channel,GPTMCFG_TimerWidth_FullRtc);
2174  SetAlternateMode(Channel , SubTimer , GPTMTxMR_TxAMS_AlternateMode_CaptureOrCompare);
2175  SetPwmLegacyOperations(Channel , SubTimer, GPTMTxMR_TxPLO_PwmLegacyOperations_Disabled);
2176  errorCode = oC_ErrorCode_None;
2177  }
2178  else
2179  {
2180  errorCode = oC_ErrorCode_TIMERBothSubTimersNeeeded;
2181  }
2182  break;
2183  /* *****************************************************************************************************************************
2184  *
2185  * In input edge modes the timers modes must be configured in capture mode
2186  *
2187  ******************************************************************************************************************************/
2188  case oC_TIMER_LLD_Mode_InputEdgeCount:
2189  SetTimerWidth(Channel , GPTMCFG_TimerWidth_Half);
2190  SetTimersMode(Channel , SubTimer , GPTMTxMR_TxMR_CaptureMode);
2191  SetAlternateMode(Channel , SubTimer , GPTMTxMR_TxAMS_AlternateMode_CaptureOrCompare);
2192  SetPwmLegacyOperations(Channel , SubTimer, GPTMTxMR_TxPLO_PwmLegacyOperations_Disabled);
2193  SetCaptureMode(Channel , SubTimer , GPTMTxMR_TxCMR_CaptureMode_EdgeCount);
2194  errorCode = oC_ErrorCode_None;
2195  break;
2196  /* *****************************************************************************************************************************
2197  *
2198  * In input edge modes the timers modes must be configured in capture mode
2199  *
2200  ******************************************************************************************************************************/
2201  case oC_TIMER_LLD_Mode_InputEdgeTime:
2202  SetTimerWidth(Channel , GPTMCFG_TimerWidth_Half);
2203  SetTimersMode(Channel , SubTimer , GPTMTxMR_TxMR_CaptureMode);
2204  SetAlternateMode(Channel , SubTimer , GPTMTxMR_TxAMS_AlternateMode_CaptureOrCompare);
2205  SetPwmLegacyOperations(Channel , SubTimer, GPTMTxMR_TxPLO_PwmLegacyOperations_Disabled);
2206  SetCaptureMode(Channel , SubTimer , GPTMTxMR_TxCMR_CaptureMode_EdgeTime);
2207  errorCode = oC_ErrorCode_None;
2208  break;
2209  /* *****************************************************************************************************************************
2210  *
2211  * Other modes are not handled (not recognized)
2212  *
2213  ******************************************************************************************************************************/
2214  default:
2215  errorCode = oC_ErrorCode_TIMERModeNotCorrect;
2216  break;
2217  }
2218 
2219  return errorCode;
2220 }
2221 
2222 //==========================================================================================================================================
2226 //==========================================================================================================================================
2227 static oC_TIMER_LLD_Mode_t GetMode( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer )
2228 {
2229  oC_TIMER_LLD_Mode_t Mode;
2230 
2231  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
2232  {
2233  Mode = MODE(Channel)[0];
2234  }
2235  else if(SubTimer & oC_TIMER_LLD_SubTimer_TimerB)
2236  {
2237  Mode = MODE(Channel)[1];
2238  }
2239 
2240  return Mode;
2241 }
2242 
2243 //==========================================================================================================================================
2247 //==========================================================================================================================================
2248 static void SetMode( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , oC_TIMER_LLD_Mode_t Mode )
2249 {
2250  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
2251  {
2252  MODE(Channel)[0] = Mode;
2253  }
2254  else
2255  {
2256  MODE(Channel)[1] = Mode;
2257  }
2258 }
2259 
2260 //==========================================================================================================================================
2264 //==========================================================================================================================================
2265 static inline void SetEventMode( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer , GPTMCTL_TxEVENT_EventMode_t EventMode )
2266 {
2267  if(SubTimer & oC_TIMER_LLD_SubTimer_TimerA)
2268  {
2269  GPTMCTL(Channel)->TAEVENT = EventMode;
2270  }
2271  else
2272  {
2273  GPTMCTL(Channel)->TBEVENT = EventMode;
2274  }
2275 }
2276 
2277 
2278 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
2279 
2280 #define MODULE_NAME TIMER
2281 #define INTERRUPT_TYPE TimerA
2282 
2283 #undef INTERRUPT_TYPE
2284 #define INTERRUPT_TYPE TimerB
2285 #undef INTERRUPT_TYPE
2286 
2287 
2293 #define _________________________________________INTERRUPTS_SECTION_________________________________________________________________________
2294 #define MODULE_NAME TIMER
2295 #define INTERRUPT_TYPE_NAME TimerA
2296 
2298 {
2299  oC_TIMER_LLD_EventFlags_t EventFlags = 0;
2300  oC_ChannelIndex_t channelIndex = oC_Channel_ToIndex(TIMER,Channel);
2301 
2302  if(GPTMMIS(Channel)->TAMMIS)
2303  {
2304  EventFlags |= oC_TIMER_LLD_EventFlags_MatchValueInterrupt;
2305  GPTMICR(Channel)->TAMCINT = 1;
2306  }
2307 
2308  if(GPTMMIS(Channel)->TATOMIS)
2309  {
2310  EventFlags |= oC_TIMER_LLD_EventFlags_TimeoutInterrupt;
2311  GPTMICR(Channel)->TATOCINT = 1;
2312  }
2313 
2314  if(GPTMMIS(Channel)->CAEMIS)
2315  {
2316  oC_TIMER_LLD_Mode_t mode = GetMode(Channel,oC_TIMER_LLD_SubTimer_TimerA);
2317  if(mode == oC_TIMER_LLD_Mode_PWM)
2318  {
2319  EventFlags |= oC_TIMER_LLD_EventFlags_PwmPinInHighState;
2320  EventFlags |= oC_TIMER_LLD_EventFlags_PwmPinInLowState;
2321 
2322  }
2323  else
2324  {
2325  EventFlags |= oC_TIMER_LLD_EventFlags_EdgeDetect;
2326  }
2327  GPTMICR(Channel)->CAECINT = 1;
2328  }
2329 
2330 
2331  CallEvent(Channel,channelIndex,oC_TIMER_LLD_SubTimer_TimerA,EventFlags);
2332  GPTMICR(Channel)->TATOCINT = 1;
2333 }
2334 #undef INTERRUPT_TYPE_NAME
2335 
2336 #define INTERRUPT_TYPE_NAME TimerB
2337 
2339 {
2340  oC_TIMER_LLD_EventFlags_t EventFlags = 0;
2341  oC_ChannelIndex_t channelIndex = oC_Channel_ToIndex(TIMER,Channel);
2342 
2343  if(GPTMMIS(Channel)->TBMMIS)
2344  {
2345  EventFlags |= oC_TIMER_LLD_EventFlags_MatchValueInterrupt;
2346  GPTMICR(Channel)->TBMCINT = 1;
2347  }
2348 
2349  if(GPTMMIS(Channel)->TBTOMIS)
2350  {
2351  EventFlags |= oC_TIMER_LLD_EventFlags_TimeoutInterrupt;
2352  GPTMICR(Channel)->TBTOCINT = 1;
2353  }
2354 
2355  if(GPTMMIS(Channel)->CBEMIS)
2356  {
2357  oC_TIMER_LLD_Mode_t mode = GetMode(Channel,oC_TIMER_LLD_SubTimer_TimerB);
2358  if(mode == oC_TIMER_LLD_Mode_PWM)
2359  {
2360  EventFlags |= oC_TIMER_LLD_EventFlags_PwmPinInHighState;
2361  EventFlags |= oC_TIMER_LLD_EventFlags_PwmPinInLowState;
2362 
2363  }
2364  else
2365  {
2366  EventFlags |= oC_TIMER_LLD_EventFlags_EdgeDetect;
2367  }
2368 
2369  GPTMICR(Channel)->CBECINT = 1;
2370  }
2371 
2372 
2373  CallEvent(Channel,channelIndex,oC_TIMER_LLD_SubTimer_TimerB,EventFlags);
2374  GPTMICR(Channel)->TBTOCINT = 1;
2375 }
2376 #undef INTERRUPT_TYPE_NAME
2377 
2378 #undef _________________________________________INTERRUPTS_SECTION_________________________________________________________________________
2379 
Something is powered on.
Definition: oc_stdtypes.h:252
Basic math operations.
static bool oC_Bits_AreBitsSetU32(uint32_t BitMask, uint32_t BitsToCheck)
checks if all bits in field are set
Definition: oc_bits.h:884
static bool ModuleEnabledFlag
Definition: oc_memman.c:211
The file with LLD interface for the TIMER driver.
double oC_Frequency_t
type to store frequency
Definition: oc_frequency.h:76
#define oC_Machine_ReadRegister(Channel, REGISTER_NAME)
redefinition of oC_Machine_ReadRegisterDirectStaticOffset
Definition: oc_machine.h:221
bool oC_MEM_LLD_IsFlashAddress(const void *Address)
checks if the pointer is in flash section
Definition: oc_mem_lld.c:433
#define oC_Channel_GetName(Channel)
returns name of channel
Definition: oc_channels.h:391
The file with LLD interface for the MEM driver.
The file with LLD interface for the GPIO driver.
#define oC_Machine_WriteRegister(Channel, REGISTER_NAME, Value)
redefinition of oC_Machine_WriteRegisterDirectStaticOffset
Definition: oc_machine.h:200
#define oC_uint8_MAX
machine memory size unsigned integer
Definition: oc_stdtypes.h:135
static void oC_Bits_SplitU64ToU32(uint64_t Variable, uint32_t *outLow, uint32_t *outHigh)
Definition: oc_bits.h:431
oC_Frequency_t oC_CLOCK_LLD_GetClockFrequency(void)
returns frequency of the system clock
Definition: oc_clock_lld.c:237
Something is powered off.
Definition: oc_stdtypes.h:251
The file with LLD interface for the CLOCK driver.
oC_Channel_t
stores machine channel
Definition: oc_channels.h:573
#define oC_Channel_IsCorrect(MODULE_NAME, Channel)
checks if channel is correct
Definition: oc_channels.h:283
#define oC_uint32_MAX
maximum value for uint32_t type
Definition: oc_stdtypes.h:163
#define oC_ABS(A, B)
Definition: oc_math.h:46
static uint64_t oC_Bits_JoinU32ToU64(uint32_t LowPart, uint32_t HighPart)
Definition: oc_bits.h:512
#define oC_Channel_FromIndex(MODULE_NAME, ChannelIndex)
returns channel according to index in module
Definition: oc_channels.h:313
#define oC_ModuleChannel_NumberOfElements(MODULE_NAME)
Number of elements in module channel.
Definition: oc_channels.h:465
static void oC_MCS_EnterCriticalSection(void)
Enters to critical section.
Definition: oc_mcs.h:755
Static array definitions.
static bool oC_Machine_IsChannelPoweredOn(oC_Channel_t Channel)
checks if channel is powered on
Definition: oc_machine.h:640
#define oC_uint16_MAX
maximum value for uint16_t type
Definition: oc_stdtypes.h:149
static oC_Power_t oC_Machine_GetPowerStateForChannel(oC_Channel_t Channel)
returns power state for channel
Definition: oc_machine.h:550
static void CallEvent(void *Address, MemoryEventFlags_t EventFlags, const char *Function, uint32_t LineNumber)
Definition: oc_memman.c:2584
static bool oC_MCS_ExitCriticalSection(void)
Exits from critical section.
Definition: oc_mcs.h:784
#define oC_Channel_ToIndex(MODULE_NAME, Channel)
returns index in module according to channel
Definition: oc_channels.h:298
#define oC_Channel_InterruptHandler(ChannelVariableName)
creates common interrupt handler for channel
Definition: oc_channels.h:514
static bool oC_Machine_SetPowerStateForChannel(oC_Channel_t Channel, oC_Power_t Power)
configures power state for machine channel
Definition: oc_machine.h:593
oC_Power_t
stores registers power state
Definition: oc_stdtypes.h:249
bool oC_MEM_LLD_IsRamAddress(const void *Address)
checks if the pointer is in ram section
Definition: oc_mem_lld.c:400
#define NULL
pointer to a zero
Definition: oc_null.h:37