28 #define DRIVER_NAME TIMER 29 #define DRIVER_FILE_NAME "timer" 30 #define DRIVER_VERSION oC_Driver_MakeVersion(0,1,0) 31 #define REQUIRED_DRIVERS 32 #define REQUIRED_BOOT_LEVEL oC_Boot_Level_0 36 #define DRIVER_CONFIGURE oC_TIMER_Configure 37 #define DRIVER_UNCONFIGURE oC_TIMER_Unconfigure 38 #define DRIVER_TURN_ON oC_TIMER_TurnOn 39 #define DRIVER_TURN_OFF oC_TIMER_TurnOff 40 #define IS_TURNED_ON oC_TIMER_IsTurnedOn 46 #include <oc_stdlib.h> 56 #define _________________________________________TYPES_SECTION______________________________________________________________________________ 61 oC_TIMER_Channel_t Channel;
62 oC_TIMER_LLD_SubTimer_t SubTimer;
65 #undef _________________________________________TYPES_SECTION______________________________________________________________________________ 72 #define _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________ 74 static void EventHandler (
void * Address , MemoryEventFlags_t Event ,
const char * Function, uint32_t LineNumber );
75 static bool IsModeCorrect ( oC_TIMER_Mode_t Mode );
78 static oC_ErrorCode_t ConnectPeripheralPin ( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer ,
const oC_TIMER_Config_t * Config );
80 #undef _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________ 89 #define _________________________________________VARIABLES_SECTION__________________________________________________________________________ 93 .EventHandler = EventHandler ,
94 .EventFlags = MemoryEventFlags_MemoryReleased
100 #undef _________________________________________VARIABLES_SECTION__________________________________________________________________________ 108 #define _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________ 114 oC_ErrorCode_t oC_TIMER_TurnOn(
void )
116 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
118 if(oC_AssignErrorCodeIfFalse(&errorCode , !
ModuleEnabledFlag , oC_ErrorCode_ModuleIsTurnedOn))
120 errorCode = oC_TIMER_LLD_TurnOnDriver();
122 if((errorCode == oC_ErrorCode_ModuleIsTurnedOn) || (errorCode == oC_ErrorCode_None))
124 ModuleBusyMutex = oC_Mutex_New(oC_Mutex_Type_Normal,&ModuleAllocator,AllocationFlags_NoWait);
126 errorCode = oC_ErrorCode_None;
135 oC_ErrorCode_t oC_TIMER_TurnOff(
void )
137 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
139 if(oC_AssignErrorCodeIfFalse(&errorCode ,
ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet))
142 errorCode = oC_TIMER_LLD_TurnOffDriver();
144 oC_Mutex_Delete(&ModuleBusyMutex,AllocationFlags_CanWaitForever);
148 if((errorCode == oC_ErrorCode_ModuleNotStartedYet) || (errorCode == oC_ErrorCode_None))
150 errorCode = oC_ErrorCode_None;
159 bool oC_TIMER_IsTurnedOn(
void )
168 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
171 oC_AssignErrorCodeIfFalse(&errorCode ,
ModuleEnabledFlag , oC_ErrorCode_ModuleNotStartedYet ) &&
172 oC_AssignErrorCodeIfFalse(&errorCode ,
oC_MemMan_IsRamAddress(outContext) , oC_ErrorCode_OutputAddressNotInRAM ) &&
174 oC_AssignErrorCodeIfFalse(&errorCode , IsModeCorrect(Config->Mode) , oC_ErrorCode_TIMERModeNotCorrect) &&
175 oC_AssignErrorCodeIfFalse(&errorCode , oC_Mutex_Take(ModuleBusyMutex,Config->MaximumTimeForWait) , oC_ErrorCode_ModuleIsBusy)
178 static const oC_TIMER_LLD_SubTimer_t subTimers[] = {
179 oC_TIMER_LLD_SubTimer_TimerA ,
180 oC_TIMER_LLD_SubTimer_TimerB ,
181 oC_TIMER_LLD_SubTimer_Both
184 errorCode = oC_ErrorCode_NoChannelAvailable;
186 oC_ARRAY_FOREACH_IN_ARRAY(subTimers,subTimer)
188 oC_TIMER_LLD_ForEachChannel(channel)
190 oC_IntMan_EnterCriticalSection();
192 oC_ErrorOccur(errorCode) &&
193 oC_TIMER_LLD_IsChannelUsed(channel,*subTimer) ==
false &&
194 oC_AssignErrorCode(&errorCode , oC_TIMER_LLD_SetChannelUsed(channel,*subTimer)))
196 oC_IntMan_ExitCriticalSection();
199 oC_AssignErrorCode(&errorCode , oC_TIMER_LLD_SetPower(channel,
oC_Power_On) ) &&
200 oC_AssignErrorCode(&errorCode , oC_TIMER_LLD_ChangeMode(channel,*subTimer,Config->Mode) ) &&
201 oC_AssignErrorCode(&errorCode , oC_TIMER_LLD_ChangeFrequency(channel,*subTimer,Config->Frequency,Config->PermissibleDifference) ) &&
202 oC_AssignErrorCode(&errorCode , oC_TIMER_LLD_ChangeCountDirection(channel,*subTimer,Config->CountDirection) ) &&
203 oC_AssignErrorCode(&errorCode , oC_TIMER_LLD_ChangeMaximumValue(channel,*subTimer,Config->MaximumValue) ) &&
204 oC_AssignErrorCode(&errorCode , oC_TIMER_LLD_ChangeMatchValue(channel,*subTimer,Config->MatchValue) ) &&
205 (Config->EventHandler ==
NULL ||
206 oC_AssignErrorCode(&errorCode , oC_TIMER_LLD_ChangeEventHandler(channel,*subTimer,Config->EventHandler,Config->EventFlags) )
208 (PinMustBeConnected(Config) ==
false ||
209 oC_AssignErrorCode(&errorCode , ConnectPeripheralPin(channel,*subTimer,Config))
211 (Config->Mode != oC_TIMER_Mode_PWM ||
212 oC_AssignErrorCode(&errorCode , oC_TIMER_LLD_ChangeStartPwmState(channel,*subTimer,Config->StartPwmState))
214 ((Config->Mode != oC_TIMER_Mode_InputEdgeCount && Config->Mode != oC_TIMER_Mode_InputEdgeTime) ||
215 oC_AssignErrorCode(&errorCode , oC_TIMER_LLD_ChangeTrigger(channel,*subTimer,Config->InputTrigger))
219 struct Context_t * context = ksmartalloc(
sizeof(
struct Context_t) , &ModuleAllocator ,AllocationFlags_NoWait);
223 context->Channel = channel;
224 context->SubTimer = *subTimer;
226 errorCode = oC_ErrorCode_None;
227 *outContext = context;
229 oC_ARRAY_FOREACH_BREAK(subTimers);
234 errorCode = oC_ErrorCode_AllocationError;
238 if(errorCode != oC_ErrorCode_None)
240 if(!oC_TIMER_LLD_RestoreDefaultStateOnChannel(channel,*subTimer))
242 errorCode = oC_ErrorCode_CannotRestoreDefaultState;
250 oC_IntMan_ExitCriticalSection();
255 oC_Mutex_Give(ModuleBusyMutex);
265 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
268 oC_AssignErrorCodeIfFalse(&errorCode,
ModuleEnabledFlag ==
true , oC_ErrorCode_ModuleNotStartedYet) &&
269 oC_AssignErrorCodeIfFalse(&errorCode, IsContextCorrect(*outContext) , oC_ErrorCode_ContextNotCorrect) &&
275 if(oC_TIMER_LLD_RestoreDefaultStateOnChannel(context->Channel,context->SubTimer))
277 if(ksmartfree(context,
sizeof(
struct Context_t),AllocationFlags_CanWaitForever))
280 errorCode = oC_ErrorCode_None;
284 errorCode = oC_ErrorCode_ReleaseError;
289 errorCode = oC_ErrorCode_CannotRestoreDefaultState;
300 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
303 oC_AssignErrorCodeIfFalse(&errorCode ,
ModuleEnabledFlag ==
true , oC_ErrorCode_ModuleNotStartedYet ) &&
304 oC_AssignErrorCodeIfFalse(&errorCode , IsContextCorrect(Context) , oC_ErrorCode_ContextNotCorrect ) &&
305 oC_AssignErrorCodeIfFalse(&errorCode , isram(outValue) , oC_ErrorCode_OutputAddressNotInRAM )
308 errorCode = oC_TIMER_LLD_ReadCurrentValue(Context->Channel,Context->SubTimer,outValue);
318 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
320 if(oC_AssignErrorCodeIfFalse(&errorCode , IsContextCorrect(Context) , oC_ErrorCode_ContextNotCorrect))
322 errorCode = oC_TIMER_LLD_ChangeCurrentValue(Context->Channel,Context->SubTimer,Value);
330 oC_ErrorCode_t oC_TIMER_ReadMatchValue(
oC_TIMER_Context_t Context , uint64_t * outValue )
332 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
335 oC_AssignErrorCodeIfFalse(&errorCode ,
ModuleEnabledFlag ==
true , oC_ErrorCode_ModuleNotStartedYet ) &&
336 oC_AssignErrorCodeIfFalse(&errorCode , IsContextCorrect(Context) , oC_ErrorCode_ContextNotCorrect ) &&
337 oC_AssignErrorCodeIfFalse(&errorCode , isram(outValue) , oC_ErrorCode_OutputAddressNotInRAM )
340 errorCode = oC_TIMER_LLD_ReadMatchValue(Context->Channel,Context->SubTimer,outValue);
350 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
352 if(oC_AssignErrorCodeIfFalse(&errorCode , IsContextCorrect(Context) , oC_ErrorCode_ContextNotCorrect))
354 errorCode = oC_TIMER_LLD_ChangeMatchValue(Context->Channel,Context->SubTimer,Value);
362 oC_ErrorCode_t oC_TIMER_ReadMaxValue(
oC_TIMER_Context_t Context , uint64_t * outValue )
364 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
367 oC_AssignErrorCodeIfFalse(&errorCode ,
ModuleEnabledFlag ==
true , oC_ErrorCode_ModuleNotStartedYet ) &&
368 oC_AssignErrorCodeIfFalse(&errorCode , IsContextCorrect(Context) , oC_ErrorCode_ContextNotCorrect ) &&
369 oC_AssignErrorCodeIfFalse(&errorCode , isram(outValue) , oC_ErrorCode_OutputAddressNotInRAM )
372 errorCode = oC_TIMER_LLD_ReadMaximumValue(Context->Channel,Context->SubTimer,outValue);
382 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
384 if(oC_AssignErrorCodeIfFalse(&errorCode , IsContextCorrect(Context) , oC_ErrorCode_ContextNotCorrect))
386 errorCode = oC_TIMER_LLD_ChangeMaximumValue(Context->Channel,Context->SubTimer,Value);
396 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
399 oC_AssignErrorCodeIfFalse(&errorCode ,
ModuleEnabledFlag ==
true , oC_ErrorCode_ModuleNotStartedYet ) &&
400 oC_AssignErrorCodeIfFalse(&errorCode , IsContextCorrect(Context) , oC_ErrorCode_ContextNotCorrect )
403 errorCode = oC_TIMER_LLD_TimerStart(Context->Channel,Context->SubTimer);
413 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
416 oC_AssignErrorCodeIfFalse(&errorCode ,
ModuleEnabledFlag ==
true , oC_ErrorCode_ModuleNotStartedYet ) &&
417 oC_AssignErrorCodeIfFalse(&errorCode , IsContextCorrect(Context) , oC_ErrorCode_ContextNotCorrect )
420 errorCode = oC_TIMER_LLD_TimerStop(Context->Channel,Context->SubTimer);
427 #undef _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________ 435 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________ 439 static void EventHandler(
void * Address , MemoryEventFlags_t Event ,
const char * Function, uint32_t LineNumber )
446 static bool IsModeCorrect( oC_TIMER_Mode_t Mode )
448 return Mode <= oC_TIMER_Mode_PWM;
462 return Config->Mode == oC_TIMER_Mode_PWM || Config->Mode == oC_TIMER_Mode_InputEdgeCount || Config->Mode == oC_TIMER_Mode_InputEdgeTime;
467 static oC_ErrorCode_t ConnectPeripheralPin( oC_TIMER_Channel_t Channel , oC_TIMER_LLD_SubTimer_t SubTimer ,
const oC_TIMER_Config_t * Config )
469 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
470 oC_TIMER_Pin_t pin = 0;
472 bool pinUsed =
false;
474 oC_IntMan_EnterCriticalSection();
476 oC_AssignErrorCode(&errorCode , oC_TIMER_LLD_ReadModulePinsOfPin(Config->Pin,&pin,&size)) &&
477 oC_AssignErrorCode(&errorCode , oC_GPIO_LLD_CheckIsPinUsed(Config->Pin,&pinUsed)) &&
478 oC_AssignErrorCodeIfFalse(&errorCode , pinUsed ==
false , oC_ErrorCode_PinIsUsed) &&
479 oC_AssignErrorCode(&errorCode , oC_GPIO_LLD_SetPinsUsed(Config->Pin)) &&
480 oC_AssignErrorCode(&errorCode , oC_TIMER_LLD_ConnectModulePin(pin))
483 errorCode = oC_ErrorCode_None;
485 oC_IntMan_ExitCriticalSection();
491 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
static bool ModuleEnabledFlag
The file with interface for the timer driver.
bool oC_MemMan_IsRamAddress(const void *Address)
checks if address is in ram section
identifier for allocations
bool oC_MemMan_FreeAllMemoryOfAllocator(Allocator_t Allocator)
release all memory of allocator
The file with LLD interface for the GPIO driver.
The file with helper macros for managing objects.
The file with interface for driver creating.
bool oC_MemMan_IsDynamicAllocatedAddress(const void *Address)
checks if address is in dynamic allocated section
bool oC_MemMan_IsAddressCorrect(const void *Address)
checks if address is correct - in RAM or in FLASH
uint32_t oC_ObjectControl_t
stores object control value
The file with interface for interrupt manager.
static oC_ObjectControl_t oC_CountObjectControl(void *ObjectPointer, oC_ObjectId_t ObjectId)
counts object control for object
static bool oC_CheckObjectControl(void *ObjectPointer, oC_ObjectId_t ObjectId, oC_ObjectControl_t ObjectControl)
checks if object control is correct
The file with interface for mutex managing.
oC_ObjectControl_t ObjectControl
Static array definitions.
The file with memory manager interface.
The file with interface for Thread Manager.
#define NULL
pointer to a zero