30 #include <oc_stdlib.h> 37 #define _________________________________________DRIVER_DEFINITIONS_SECTION_________________________________________________________________ 42 #define DRIVER_NAME PWM 43 #define DRIVER_FILE_NAME "pwm" 44 #define DRIVER_VERSION oC_Driver_MakeVersion(1,0,0) 45 #define REQUIRED_DRIVERS &TIMER,&GPIO 46 #define REQUIRED_BOOT_LEVEL oC_Boot_Level_RequireClock | oC_Boot_Level_RequireMemoryManager | oC_Boot_Level_RequireDriversManager 49 #define DRIVER_CONFIGURE oC_PWM_Configure 50 #define DRIVER_UNCONFIGURE oC_PWM_Unconfigure 52 #undef _________________________________________DRIVER_DEFINITIONS_SECTION_________________________________________________________________ 62 #define _________________________________________TYPES_SECTION______________________________________________________________________________ 68 uint64_t MaximumValue;
71 #undef _________________________________________TYPES_SECTION______________________________________________________________________________ 78 #define _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________ 82 #undef _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________ 90 #define _________________________________________VARIABLES_SECTION__________________________________________________________________________ 96 #undef _________________________________________VARIABLES_SECTION__________________________________________________________________________ 104 #define _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________ 110 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
113 oC_AssignErrorCodeIfFalse(&errorCode , isaddresscorrect(Config) , oC_ErrorCode_WrongConfigAddress ) &&
114 oC_AssignErrorCodeIfFalse(&errorCode , isram(outContext) , oC_ErrorCode_OutputAddressNotInRAM ) &&
115 oC_AssignErrorCodeIfFalse(&errorCode , Config->MaximumValue > 0 , oC_ErrorCode_MaximumValueNotCorrect )
120 if(oC_AssignErrorCodeIfFalse(&errorCode , isram(context) , oC_ErrorCode_AllocationError))
123 .Mode = oC_TIMER_Mode_PWM ,
124 .MaximumTimeForWait =
s(1) ,
125 .Frequency = Config->TickFrequency,
126 .PermissibleDifference = Config->PermissibleDiffernece ,
127 .MaximumValue = Config->MaximumValue ,
130 .EventHandler =
NULL ,
132 .CountDirection = oC_TIMER_CountDirection_DoesntMatter
136 oC_AssignErrorCode(&errorCode , oC_TIMER_Configure(&config,&context->TimerContext)) &&
137 oC_AssignErrorCode(&errorCode , oC_TIMER_Start(context->TimerContext))
141 context->MaximumValue = Config->MaximumValue;
142 *outContext = context;
143 errorCode = oC_ErrorCode_None;
145 else if(ksmartfree(context,
sizeof(
struct Context_t),AllocationFlags_CanWaitForever)==
false)
147 errorCode = oC_ErrorCode_ReleaseError;
159 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
162 oC_AssignErrorCodeIfFalse(&errorCode , isaddresscorrect(Config) , oC_ErrorCode_WrongConfigAddress ) &&
163 oC_AssignErrorCodeIfFalse(&errorCode , isram(outContext) , oC_ErrorCode_OutputAddressNotInRAM ) &&
164 oC_AssignErrorCodeIfFalse(&errorCode , IsContextCorrect(*outContext) , oC_ErrorCode_ContextNotCorrect )
169 .Mode = oC_TIMER_Mode_PWM ,
170 .MaximumTimeForWait =
s(1) ,
171 .Frequency = Config->TickFrequency,
172 .PermissibleDifference = Config->PermissibleDiffernece ,
173 .MaximumValue = Config->MaximumValue ,
176 .EventHandler =
NULL ,
178 .CountDirection = oC_TIMER_CountDirection_DoesntMatter
183 if(oC_AssignErrorCode(&errorCode , oC_TIMER_Unconfigure(&config,&context->TimerContext)))
186 *outContext = context;
187 errorCode = oC_ErrorCode_None;
190 if(ksmartfree(context,
sizeof(
struct Context_t),AllocationFlags_CanWaitForever)==
false)
192 errorCode = oC_ErrorCode_ReleaseError;
201 oC_ErrorCode_t oC_PWM_SetDuty(
oC_PWM_Context_t Context ,
float PercentDuty )
203 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
206 oC_AssignErrorCodeIfFalse(&errorCode , IsContextCorrect(Context) , oC_ErrorCode_ContextNotCorrect ) &&
207 oC_AssignErrorCodeIfFalse(&errorCode , PercentDuty <= 100 , oC_ErrorCode_ValueTooBig )
210 errorCode = oC_TIMER_SetMatchValue(Context,(uint64_t)((PercentDuty/100)*(
float)Context->MaximumValue));
218 oC_ErrorCode_t oC_PWM_ReadDuty(
oC_PWM_Context_t Context ,
float * outPercentDuty )
220 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
223 oC_AssignErrorCodeIfFalse(&errorCode , IsContextCorrect(Context) , oC_ErrorCode_ContextNotCorrect ) &&
224 oC_AssignErrorCodeIfFalse(&errorCode , isram(outPercentDuty) , oC_ErrorCode_OutputAddressNotInRAM ) &&
225 oC_AssignErrorCodeIfFalse(&errorCode , Context->MaximumValue > 0 , oC_ErrorCode_MaximumValueNotCorrect )
229 if(oC_AssignErrorCode(&errorCode , oC_TIMER_ReadMatchValue(Context,&value)))
231 *outPercentDuty = (((float)value)/((float)Context->MaximumValue))*100;
232 errorCode = oC_ErrorCode_None;
244 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
246 if(oC_AssignErrorCodeIfFalse(&errorCode , IsContextCorrect(Context) , oC_ErrorCode_ContextNotCorrect ))
248 errorCode = oC_TIMER_SetMatchValue(Context,Value);
256 oC_ErrorCode_t oC_PWM_ReadValue(
oC_PWM_Context_t Context , uint64_t * outValue )
258 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
261 oC_AssignErrorCodeIfFalse(&errorCode , IsContextCorrect(Context) , oC_ErrorCode_ContextNotCorrect ) &&
262 oC_AssignErrorCodeIfFalse(&errorCode , isram(outValue) , oC_ErrorCode_OutputAddressNotInRAM )
265 if(oC_AssignErrorCode(&errorCode , oC_TIMER_ReadMatchValue(Context,outValue)))
267 errorCode = oC_ErrorCode_None;
274 #undef _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________ 281 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________ 290 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________ #define s(time)
Number of s.
The file with interface for the timer driver.
identifier for allocations
The file with interface for the PWM driver.
PWM driver configuration structure.
The file with helper macros for managing objects.
The file with interface for driver creating.
uint32_t oC_ObjectControl_t
stores object control value
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
oC_ObjectControl_t ObjectControl
#define NULL
pointer to a zero