PWM
|
PWM.c
Go to the documentation of this file.
00001 00076 /*********************************************************************************************************************** 00077 * HEADER FILES 00078 **********************************************************************************************************************/ 00079 #include "pwm.h" 00080 00081 /********************************************************************************************************************** 00082 * MACROS 00083 **********************************************************************************************************************/ 00084 #define PWM_MAX_DUTY_CYCLE ((uint32_t)10000) 00085 #define PWM_DUTY_CYCLE_SCALE ((uint32_t)100) 00086 #define PWM_MAX_PRESCALER ((uint32_t)15) 00087 #define PWM_MAX_PERIOD_VALUE ((uint32_t) 65535) 00088 00089 /*********************************************************************************************************************** 00090 * LOCAL ROUTINES 00091 **********************************************************************************************************************/ 00092 #ifdef PWM_SLICE_USED_CCU4 00093 /*Initialize the App and XMC_CCU4 slice. */ 00094 PWM_STATUS_t PWM_lCCU4_Init(PWM_t *const handle_ptr); 00095 00096 /*Initialize interrupts*/ 00097 void PWM_lCCU4_ConfigInterrupts(PWM_t *const handle_ptr); 00098 00099 /*Starts the CCU4 slice. */ 00100 void PWM_lCCU4_Start(PWM_t *const handle_ptr); 00101 00102 /*Stops the CCU4 slice. */ 00103 void PWM_lCCU4_Stop(PWM_t *const handle_ptr); 00104 00105 /*Sets the duty cycle for CCU4 slice. */ 00106 PWM_STATUS_t PWM_lCCU4_SetDutyCycle(PWM_t *const handle_ptr, uint32_t duty_cycle); 00107 00108 /*Sets the frequency for CCU4 slice. */ 00109 PWM_STATUS_t PWM_lCCU4_SetFreq(PWM_t *const handle_ptr, uint32_t pwm_freq_hz); 00110 00111 /*Sets the frequency and duty cycle for CCU4 slice. */ 00112 PWM_STATUS_t PWM_lCCU4_SetFreqAndDutyCycle(PWM_t *const handle_ptr, uint32_t pwm_freq_hz, uint32_t duty_cycle); 00113 #endif 00114 00115 #ifdef PWM_SLICE_USED_CCU8 00116 /*Initialize the App and XMC_CCU8 slice. */ 00117 PWM_STATUS_t PWM_lCCU8_Init(PWM_t *const handle_ptr); 00118 00119 /*Initialize interrupts*/ 00120 void PWM_lCCU8_ConfigInterrupts(PWM_t *const handle_ptr); 00121 00122 /*Starts the CCU8 slice. */ 00123 void PWM_lCCU8_Start(PWM_t *const handle_ptr); 00124 00125 /*Stops the CCU8 slice. */ 00126 void PWM_lCCU8_Stop(PWM_t *const handle_ptr); 00127 00128 /*Sets the duty cycle for CCU8 slice. */ 00129 PWM_STATUS_t PWM_lCCU8_SetDutyCycle(PWM_t *const handle_ptr, uint32_t duty_cycle); 00130 00131 /*Sets the frequency for CCU8 slice. */ 00132 PWM_STATUS_t PWM_lCCU8_SetFreq(PWM_t *const handle_ptr, uint32_t pwm_freq_hz); 00133 00134 /*Sets the frequency and duty cycle for CCU8 slice. */ 00135 PWM_STATUS_t PWM_lCCU8_SetFreqAndDutyCycle(PWM_t *const handle_ptr, uint32_t pwm_freq_hz, uint32_t duty_cycle); 00136 #endif 00137 00138 #ifdef PWM_SLICE_USED_CCU4 00139 00140 /*Initialize the APP and CCU4 slice. */ 00141 PWM_STATUS_t PWM_lCCU4_Init(PWM_t *const handle_ptr) 00142 { 00143 PWM_STATUS_t status = PWM_STATUS_FAILURE; 00144 00145 XMC_ASSERT("PWM_lCCU4_Init:Invalid handle_ptr" , (handle_ptr != NULL)) 00146 00147 if (PWM_STATUS_UNINITIALIZED == handle_ptr->state) 00148 { 00149 /* Initialize consumed Apps */ 00150 status = (PWM_STATUS_t)GLOBAL_CCU4_Init(handle_ptr->global_ccu4_handle); 00151 00152 /*Initialize CCU4 slice */ 00153 if (PWM_STATUS_SUCCESS == status)/*check GLOBAL_CCU4_Init status*/ 00154 { 00155 XMC_DEBUG("PWM_lCCU4_Init:Initilizing Slice") 00156 XMC_CCU4_SLICE_CompareInit(handle_ptr->ccu4_slice_ptr, handle_ptr->ccu4_slice_config_ptr); 00157 00158 /* Set the period and compare register values */ 00159 XMC_CCU4_SLICE_SetTimerPeriodMatch(handle_ptr->ccu4_slice_ptr, 00160 (uint16_t)handle_ptr->period_value); 00161 00162 XMC_CCU4_SLICE_SetTimerCompareMatch(handle_ptr->ccu4_slice_ptr, 00163 (uint16_t)handle_ptr->compare_value); 00164 00165 XMC_CCU4_EnableShadowTransfer(handle_ptr->ccu4_kernel_ptr, handle_ptr->shadow_mask); 00166 00167 /* Initialize interrupts */ 00168 PWM_lCCU4_ConfigInterrupts(handle_ptr); 00169 00170 XMC_GPIO_Init(handle_ptr->gpio_out_port,handle_ptr->gpio_out_pin, 00171 handle_ptr->gpio_out_config); 00172 00173 handle_ptr->state = PWM_STATUS_SUCCESS; 00174 00175 /* Start the PWM generation if start at initialization is enabled */ 00176 if ((bool) true == handle_ptr->start_control) 00177 { 00178 PWM_Start(handle_ptr); 00179 } 00180 status = PWM_STATUS_SUCCESS; 00181 } 00182 else 00183 { 00184 handle_ptr->state = PWM_STATUS_UNINITIALIZED; 00185 } 00186 00187 } 00188 return (status); 00189 } /* end of PWM_lCCU4_Init() api */ 00190 00191 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 00192 /* Initialize interrupts */ 00193 void PWM_lCCU4_ConfigInterrupts(PWM_t *const handle_ptr) 00194 { 00195 if ((bool) true == handle_ptr->period_match_enable) 00196 { 00197 XMC_DEBUG("PWM_lCCU4_ConfigInterrupts:period match enable") 00198 XMC_CCU4_SLICE_EnableEvent(handle_ptr->ccu4_slice_ptr, XMC_CCU4_SLICE_IRQ_ID_PERIOD_MATCH); 00199 00200 /* Bind event to Service Request Node to period match event*/ 00201 XMC_CCU4_SLICE_SetInterruptNode(handle_ptr->ccu4_slice_ptr, XMC_CCU4_SLICE_IRQ_ID_PERIOD_MATCH, 00202 handle_ptr->ccu4_slice_period_match_node); 00203 } 00204 00205 if ((bool) true == handle_ptr->compare_match_enable) 00206 { 00207 XMC_DEBUG("PWM_lCCU4_ConfigInterrupts:compare match enable") 00208 XMC_CCU4_SLICE_EnableEvent(handle_ptr->ccu4_slice_ptr, XMC_CCU4_SLICE_IRQ_ID_COMPARE_MATCH_UP); 00209 00210 /* Bind event to Service Request Node to compare match event */ 00211 XMC_CCU4_SLICE_SetInterruptNode(handle_ptr->ccu4_slice_ptr, XMC_CCU4_SLICE_IRQ_ID_COMPARE_MATCH_UP, 00212 handle_ptr->ccu4_slice_compare_match_node); 00213 } 00214 } 00215 00216 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 00217 /*Starts the CCU4 slice. */ 00218 void PWM_lCCU4_Start(PWM_t *const handle_ptr) 00219 { 00220 if ((PWM_STATUS_SUCCESS == handle_ptr->state) || (PWM_STATUS_STOPPED == handle_ptr->state)) 00221 { 00222 /* Clears the IDLE mode for the slice */ 00223 XMC_CCU4_EnableClock(handle_ptr->ccu4_kernel_ptr,handle_ptr->slice_number); 00224 XMC_CCU4_SLICE_StartTimer(handle_ptr->ccu4_slice_ptr); 00225 00226 handle_ptr->state = PWM_STATUS_RUNNING; 00227 XMC_DEBUG("PWM_lCCU4_Start:start PWM") 00228 } 00229 } /* end of PWM_lCCU4_Start() api */ 00230 00231 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 00232 /*Stops the CCU4 slice. */ 00233 void PWM_lCCU4_Stop(PWM_t *const handle_ptr) 00234 { 00235 if (PWM_STATUS_UNINITIALIZED != handle_ptr->state) 00236 { 00237 XMC_CCU4_SLICE_StopTimer(handle_ptr->ccu4_slice_ptr); 00238 XMC_CCU4_SLICE_ClearTimer(handle_ptr->ccu4_slice_ptr); 00239 XMC_CCU4_DisableClock(handle_ptr->ccu4_kernel_ptr,handle_ptr->slice_number); 00240 00241 handle_ptr->state = PWM_STATUS_STOPPED; 00242 XMC_DEBUG("PWM_lCCU4_Stop:stop PWM") 00243 } 00244 } /* end of PWM_lCCU4_Stop() api */ 00245 00246 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 00247 /*Sets the duty cycle for CCU4 slice. */ 00248 PWM_STATUS_t PWM_lCCU4_SetDutyCycle(PWM_t *const handle_ptr, uint32_t duty_cycle) 00249 { 00250 uint32_t period; 00251 uint32_t compare; 00252 PWM_STATUS_t status; 00253 00254 XMC_ASSERT("PWM_lCCU4_SetDutyCycle:Invalid duty_cycle " , ((duty_cycle >= 0) && (duty_cycle <= PWM_MAX_DUTY_CYCLE))) 00255 00256 status = PWM_STATUS_FAILURE; 00257 if (PWM_STATUS_UNINITIALIZED != handle_ptr->state) 00258 { 00259 /* Duty cycle needs between 0 and 10000 */ 00260 if (duty_cycle <= PWM_MAX_DUTY_CYCLE) 00261 { 00262 /* period = (PR + 1) */ 00263 period = (uint32_t)handle_ptr->period_value + 1U; 00264 00265 /* Duty Cycle(symmetric) = (PR-CR1)+1 / period */ 00266 compare = ((period * (PWM_MAX_DUTY_CYCLE - duty_cycle)) / ((uint32_t) 100 * PWM_DUTY_CYCLE_SCALE)); 00267 00268 handle_ptr->compare_value = compare; 00269 handle_ptr->duty_cycle = duty_cycle; 00270 00271 XMC_CCU4_SLICE_SetTimerCompareMatch(handle_ptr->ccu4_slice_ptr, (uint16_t)compare); 00272 XMC_CCU4_EnableShadowTransfer(handle_ptr->ccu4_kernel_ptr, handle_ptr->shadow_mask); 00273 status = PWM_STATUS_SUCCESS; 00274 } 00275 } 00276 00277 XMC_DEBUG("PWM_lCCU4_SetDutyCycle:dutycycle set") 00278 return (status); 00279 } /* end of PWM_lCCU4_SetDutyCycle() api */ 00280 00281 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 00282 /*Sets the frequency for CCU4 slice. */ 00283 PWM_STATUS_t PWM_lCCU4_SetFreq(PWM_t *const handle_ptr, uint32_t pwm_freq_hz) 00284 { 00285 PWM_STATUS_t status; 00286 uint32_t module_freq; 00287 uint8_t prescaler; 00288 uint32_t period_value; 00289 uint32_t compare; 00290 00291 XMC_ASSERT("PWM_lCCU4_SetFreq:Invalid pwm_freq_hz " , (pwm_freq_hz != 0U)) 00292 00293 status = handle_ptr->state; 00294 prescaler = 0U; 00295 period_value = 0U; 00296 00297 /* Can't set the frequency when the PWM is not yet initialized or when required frequency is 0*/ 00298 if ((status != PWM_STATUS_UNINITIALIZED) && ((uint32_t)0 != pwm_freq_hz)) 00299 { 00300 status = PWM_STATUS_SUCCESS; 00301 /*Get the Module frequency*/ 00302 module_freq = handle_ptr->global_ccu4_handle->module_frequency; 00303 00304 /*Calculate the prescaler and the period register values.*/ 00305 while (prescaler <= PWM_MAX_PRESCALER) 00306 { 00307 period_value = (uint32_t)((uint32_t)module_freq / (uint32_t)pwm_freq_hz) >> (uint32_t)prescaler; 00308 /*If the prescaler selected is not big enough goto the next prescaler value else come out.*/ 00309 if (period_value <= PWM_MAX_TIMER_COUNT ) 00310 { 00311 break; 00312 } 00313 prescaler++; 00314 } 00315 00316 /*Can't set the frequency if the required value is too small or when the required frequency is too large.*/ 00317 if ((prescaler > PWM_MAX_PRESCALER) || ((uint32_t)0 == period_value)) 00318 { 00319 XMC_DEBUG("PWM_lCCU4_SetFreq:Frequency could not be set") 00320 status = PWM_STATUS_FAILURE; 00321 } 00322 else 00323 { 00324 /*Calculate the new compare values using new period values */ 00325 compare = (period_value * (PWM_MAX_DUTY_CYCLE - handle_ptr->duty_cycle)) 00326 / ((uint32_t) 100 * PWM_DUTY_CYCLE_SCALE); 00327 00328 XMC_CCU4_SLICE_SetPrescaler(handle_ptr->ccu4_slice_ptr, prescaler); 00329 00330 /* The period register is always one count less than calculated.*/ 00331 period_value = period_value - (uint32_t)1; 00332 XMC_CCU4_SLICE_SetTimerPeriodMatch(handle_ptr->ccu4_slice_ptr, (uint16_t)(period_value)); 00333 00334 XMC_CCU4_SLICE_SetTimerCompareMatch(handle_ptr->ccu4_slice_ptr, (uint16_t)compare); 00335 00336 XMC_CCU4_EnableShadowTransfer(handle_ptr->ccu4_kernel_ptr, handle_ptr->shadow_mask); 00337 00338 handle_ptr->compare_value = compare; 00339 handle_ptr->period_value = period_value; 00340 XMC_DEBUG("PWM_lCCU4_SetFreq:frequency set") 00341 } 00342 } 00343 else 00344 { 00345 status = PWM_STATUS_FAILURE; 00346 XMC_DEBUG("PWM_lCCU4_SetFreq:Frequency could not be set") 00347 } 00348 00349 00350 return status; 00351 } /* end of PWM_lCCU4_SetFreq() api */ 00352 00353 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 00354 /*Sets the frequency and duty cycle for CCU4 slice. */ 00355 PWM_STATUS_t PWM_lCCU4_SetFreqAndDutyCycle(PWM_t *const handle_ptr, uint32_t pwm_freq_hz, uint32_t duty_cycle) 00356 { 00357 PWM_STATUS_t status; 00358 uint32_t module_freq; 00359 uint8_t prescaler; 00360 uint32_t period_value; 00361 uint32_t compare; 00362 00363 XMC_ASSERT("PWM_lCCU4_SetFreqAndDutyCycle:Invalid pwm_freq_hz " , (pwm_freq_hz != 0U)) 00364 XMC_ASSERT("PWM_lCCU4_SetFreqAndDutyCycle:Invalid duty_cycle",((duty_cycle >= 0) && 00365 (duty_cycle <= PWM_MAX_DUTY_CYCLE))) 00366 00367 status = handle_ptr->state; 00368 prescaler = 0U; 00369 period_value = 0U; 00370 00371 /* Can't set the frequency when the PWM is not yet initialized or when required frequency is 0*/ 00372 if ((status != PWM_STATUS_UNINITIALIZED) && ((uint32_t)0 != pwm_freq_hz)) 00373 { 00374 status = PWM_STATUS_SUCCESS; 00375 /*Get the Module frequency*/ 00376 module_freq = handle_ptr->global_ccu4_handle->module_frequency; 00377 00378 /*Calculate the prescaler and the period register values.*/ 00379 while (prescaler <= PWM_MAX_PRESCALER) 00380 { 00381 period_value = (uint32_t)((uint32_t)module_freq / (uint32_t)pwm_freq_hz) >> (uint32_t)prescaler; 00382 /*If the prescaler selected is not big enough goto the next prescaler value else come out.*/ 00383 if (period_value <= PWM_MAX_TIMER_COUNT ) 00384 { 00385 break; 00386 } 00387 00388 prescaler++; 00389 } 00390 00391 /*Can't set the frequency if the required value is too small or when the required frequency is too large.*/ 00392 if ((prescaler > PWM_MAX_PRESCALER) || (duty_cycle > PWM_MAX_DUTY_CYCLE) || ((uint32_t)0 == period_value)) 00393 { 00394 XMC_DEBUG("PWM_lCCU4_SetFreqAndDutyCycle:Frequency or duty cycle could not be set") 00395 status = PWM_STATUS_FAILURE; 00396 } 00397 else 00398 { 00399 /*Calculate the new compare values using new period values */ 00400 compare = (period_value * ((uint32_t)PWM_MAX_DUTY_CYCLE - duty_cycle)) / ((uint32_t) 100 * PWM_DUTY_CYCLE_SCALE); 00401 00402 XMC_CCU4_SLICE_SetPrescaler(handle_ptr->ccu4_slice_ptr, prescaler); 00403 00404 /* The period register is always one count less than calculated.*/ 00405 period_value = period_value - (uint32_t)1; 00406 XMC_CCU4_SLICE_SetTimerPeriodMatch(handle_ptr->ccu4_slice_ptr, (uint16_t)(period_value)); 00407 00408 XMC_CCU4_SLICE_SetTimerCompareMatch(handle_ptr->ccu4_slice_ptr, (uint16_t)compare); 00409 00410 XMC_CCU4_EnableShadowTransfer(handle_ptr->ccu4_kernel_ptr, handle_ptr->shadow_mask); 00411 00412 handle_ptr->compare_value = compare; 00413 handle_ptr->period_value = period_value; 00414 handle_ptr->duty_cycle = duty_cycle; 00415 XMC_DEBUG("PWM_lCCU4_SetFreqAndDutyCycle:frequency and duty cycle set") 00416 } 00417 } 00418 else 00419 { 00420 status = PWM_STATUS_FAILURE; 00421 XMC_DEBUG("PWM_lCCU4_SetFreqAndDutyCycle:Frequency or duty cycle could not be set") 00422 } 00423 00424 00425 return status; 00426 } /* end of PWM_lCCU4_SetFreqAndDutyCycle() api */ 00427 00428 #endif /* end of CCU4 function definitions */ 00429 00430 #ifdef PWM_SLICE_USED_CCU8 00431 00432 /*Initialize the APP and CCU8 slice. */ 00433 PWM_STATUS_t PWM_lCCU8_Init(PWM_t *const handle_ptr) 00434 { 00435 PWM_STATUS_t status = PWM_STATUS_FAILURE; 00436 00437 if (PWM_STATUS_UNINITIALIZED == handle_ptr->state) 00438 { 00439 /* Initialize consumed Apps */ 00440 status = (PWM_STATUS_t)GLOBAL_CCU8_Init(handle_ptr->global_ccu8_handle); 00441 00442 /*Initialize CCU8 slice */ 00443 if (PWM_STATUS_SUCCESS == status) 00444 { 00445 XMC_DEBUG("PWM_lCCU8_Init:Initilizing Slice") 00446 XMC_CCU8_SLICE_CompareInit(handle_ptr->ccu8_slice_ptr, handle_ptr->ccu8_slice_config_ptr); 00447 00448 /* Set the period and compare register values */ 00449 XMC_CCU8_SLICE_SetTimerPeriodMatch(handle_ptr->ccu8_slice_ptr, 00450 (uint16_t)handle_ptr->period_value); 00451 00452 XMC_CCU8_SLICE_SetTimerCompareMatch(handle_ptr->ccu8_slice_ptr, XMC_CCU8_SLICE_COMPARE_CHANNEL_1, 00453 (uint16_t)handle_ptr->compare_value); 00454 00455 XMC_CCU8_EnableShadowTransfer(handle_ptr->ccu8_kernel_ptr, handle_ptr->shadow_mask); 00456 00457 /* Initialize interrupts */ 00458 PWM_lCCU8_ConfigInterrupts(handle_ptr); 00459 00460 XMC_GPIO_Init(handle_ptr->gpio_out_port,handle_ptr->gpio_out_pin, 00461 handle_ptr->gpio_out_config); 00462 00463 handle_ptr->state = PWM_STATUS_SUCCESS; 00464 00465 /* Start the PWM generation if start at initialization is enabled */ 00466 if ((bool) true == handle_ptr->start_control) 00467 { 00468 PWM_Start(handle_ptr); 00469 } 00470 status = PWM_STATUS_SUCCESS; 00471 } 00472 else 00473 { 00474 handle_ptr->state = PWM_STATUS_UNINITIALIZED; 00475 } 00476 00477 } 00478 return(status); 00479 } /* end of PWM_lCCU8_Init() api */ 00480 00481 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 00482 /* Initialize interrupts */ 00483 void PWM_lCCU8_ConfigInterrupts(PWM_t *const handle_ptr) 00484 { 00485 if ((bool) true == handle_ptr->period_match_enable) 00486 { 00487 XMC_DEBUG("PWM_lCCU8_ConfigInterrupts:period match event enable") 00488 00489 XMC_CCU8_SLICE_EnableEvent(handle_ptr->ccu8_slice_ptr, XMC_CCU8_SLICE_IRQ_ID_PERIOD_MATCH); 00490 00491 /* Bind event to Service Request Node for period match event */ 00492 XMC_CCU8_SLICE_SetInterruptNode(handle_ptr->ccu8_slice_ptr, XMC_CCU8_SLICE_IRQ_ID_PERIOD_MATCH, 00493 handle_ptr->ccu8_slice_period_match_node); 00494 } 00495 00496 if ((bool) true == handle_ptr->compare_match_enable) 00497 { 00498 XMC_DEBUG("PWM_lCCU8_ConfigInterrupts:compare match event enable ") 00499 00500 XMC_CCU8_SLICE_EnableEvent(handle_ptr->ccu8_slice_ptr, XMC_CCU8_SLICE_IRQ_ID_COMPARE_MATCH_UP_CH_1); 00501 /* Bind event to Service Request Node for compare match event */ 00502 XMC_CCU8_SLICE_SetInterruptNode(handle_ptr->ccu8_slice_ptr, XMC_CCU8_SLICE_IRQ_ID_COMPARE_MATCH_UP_CH_1, 00503 handle_ptr->ccu8_slice_compare_match_node); 00504 } 00505 } 00506 00507 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 00508 /*Starts the CCU8 slice. */ 00509 void PWM_lCCU8_Start(PWM_t *const handle_ptr) 00510 { 00511 XMC_ASSERT("PWM_lCCU8_Start:Invalid handle_ptr" , (handle_ptr != NULL)) 00512 00513 if ((PWM_STATUS_SUCCESS == handle_ptr->state) || (PWM_STATUS_STOPPED == handle_ptr->state)) 00514 { 00515 /* Clears IDLE mode for the slice */ 00516 XMC_CCU8_EnableClock(handle_ptr->ccu8_kernel_ptr,handle_ptr->slice_number); 00517 XMC_CCU8_SLICE_StartTimer(handle_ptr->ccu8_slice_ptr); 00518 00519 handle_ptr->state = PWM_STATUS_RUNNING; 00520 XMC_DEBUG("PWM_lCCU8_Start:start PWM") 00521 } 00522 } /* end of PWM_lCCU8_Start() api */ 00523 00524 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 00525 /*Stops the CCU8 slice. */ 00526 void PWM_lCCU8_Stop(PWM_t *const handle_ptr) 00527 { 00528 XMC_ASSERT("PWM_lCCU8_Stop:Invalid handle_ptr" , (handle_ptr != NULL)) 00529 00530 if (PWM_STATUS_UNINITIALIZED != handle_ptr->state) 00531 { 00532 XMC_CCU8_SLICE_StopTimer(handle_ptr->ccu8_slice_ptr); 00533 XMC_CCU8_SLICE_ClearTimer(handle_ptr->ccu8_slice_ptr); 00534 XMC_CCU8_DisableClock(handle_ptr->ccu8_kernel_ptr, handle_ptr->slice_number); 00535 00536 handle_ptr->state = PWM_STATUS_STOPPED; 00537 XMC_DEBUG("PWM_lCCU8_Stop:stop PWM") 00538 } 00539 } /* end of PWM_lCCU8_Stop() api */ 00540 00541 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 00542 /*Sets the duty cycle for CCU8 slice. */ 00543 PWM_STATUS_t PWM_lCCU8_SetDutyCycle(PWM_t *const handle_ptr, uint32_t duty_cycle) 00544 { 00545 uint32_t period; 00546 uint32_t compare; 00547 PWM_STATUS_t status; 00548 00549 XMC_ASSERT("PWM_lCCU8_SetDutyCycle:Invalid handle_ptr" , (handle_ptr != NULL)) 00550 XMC_ASSERT("PWM_lCCU8_SetDutyCycle:Invalid duty_cycle",((duty_cycle >= 0) && 00551 (duty_cycle <= PWM_MAX_DUTY_CYCLE))) 00552 00553 status = PWM_STATUS_FAILURE; 00554 if (handle_ptr->state != PWM_STATUS_UNINITIALIZED) 00555 { 00556 /* Duty cycle needs between 0 and 10000 */ 00557 if (duty_cycle <= PWM_MAX_DUTY_CYCLE) 00558 { 00559 period = (uint32_t)handle_ptr->period_value + 1U; 00560 00561 /* Duty Cycle(symmetric) = (PR-CR1)+1 / period */ 00562 compare = ((period * ((uint32_t) PWM_MAX_DUTY_CYCLE - duty_cycle)) / ((uint32_t) 100 * PWM_DUTY_CYCLE_SCALE)); 00563 00564 handle_ptr->compare_value = compare; 00565 handle_ptr->duty_cycle = duty_cycle; 00566 00567 XMC_CCU8_SLICE_SetTimerCompareMatch(handle_ptr->ccu8_slice_ptr, XMC_CCU8_SLICE_COMPARE_CHANNEL_1, 00568 (uint16_t)compare); 00569 00570 XMC_CCU8_EnableShadowTransfer(handle_ptr->ccu8_kernel_ptr, handle_ptr->shadow_mask); 00571 status = PWM_STATUS_SUCCESS; 00572 } 00573 } 00574 00575 XMC_DEBUG("PWM_lCCU8_SetDutyCycle:dutycycle set") 00576 return (status); 00577 } /* end of PWM_lCCU8_SetDutyCycle() api */ 00578 00579 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 00580 /*Sets the frequency for CCU8 slice. */ 00581 PWM_STATUS_t PWM_lCCU8_SetFreq(PWM_t *const handle_ptr, uint32_t pwm_freq_hz) 00582 { 00583 PWM_STATUS_t status; 00584 uint32_t module_freq; 00585 uint8_t prescaler; 00586 uint32_t period_value; 00587 uint32_t compare; 00588 00589 XMC_ASSERT("PWM_lCCU8_SetFreq:Invalid pwm_freq_hz " , (pwm_freq_hz != 0U)) 00590 00591 status = handle_ptr->state; 00592 prescaler = 0U; 00593 period_value = 0U; 00594 00595 /* Can't set the frequency when the PWM is not yet initialized or when required frequency is 0*/ 00596 if ((status != PWM_STATUS_UNINITIALIZED) && ((uint32_t)0 != pwm_freq_hz)) 00597 { 00598 status = PWM_STATUS_SUCCESS; 00599 /*Get the Module frequency*/ 00600 module_freq = handle_ptr->global_ccu8_handle->module_frequency; 00601 00602 /*Calculate the prescaler and the period register values.*/ 00603 while (prescaler <= PWM_MAX_PRESCALER) 00604 { 00605 period_value = (uint32_t)((uint32_t)module_freq / (uint32_t)pwm_freq_hz) >> (uint32_t)prescaler; 00606 /*If the prescaler selected is not big enough goto the next prescaler value else come out.*/ 00607 if (period_value <= PWM_MAX_TIMER_COUNT) 00608 { 00609 break; 00610 } 00611 00612 prescaler++; 00613 } 00614 00615 /*Can't set the frequency if the required value is too small or when the required frequency is too large.*/ 00616 if ((prescaler > PWM_MAX_PRESCALER) || ((uint32_t)0 == period_value)) 00617 { 00618 XMC_DEBUG("PWM_lCCU8_SetFreq:frequency could not be set") 00619 status = PWM_STATUS_FAILURE; 00620 } 00621 else 00622 { 00623 /*Calculate the new compare values using new period values*/ 00624 compare = (period_value * (PWM_MAX_DUTY_CYCLE - handle_ptr->duty_cycle)) 00625 / ((uint32_t) 100 * PWM_DUTY_CYCLE_SCALE); 00626 00627 XMC_CCU8_SLICE_SetPrescaler(handle_ptr->ccu8_slice_ptr, prescaler); 00628 00629 /* The period register is always one count less than calculated.*/ 00630 period_value = period_value - (uint32_t)1; 00631 XMC_CCU8_SLICE_SetTimerPeriodMatch(handle_ptr->ccu8_slice_ptr, (uint16_t)(period_value)); 00632 00633 XMC_CCU8_SLICE_SetTimerCompareMatch(handle_ptr->ccu8_slice_ptr, XMC_CCU8_SLICE_COMPARE_CHANNEL_1, 00634 (uint16_t)compare); 00635 00636 XMC_CCU8_EnableShadowTransfer(handle_ptr->ccu8_kernel_ptr, handle_ptr->shadow_mask); 00637 00638 handle_ptr->compare_value = compare; 00639 handle_ptr->period_value = period_value; 00640 XMC_DEBUG("PWM_lCCU8_SetFreq:frequency set") 00641 } 00642 } 00643 else 00644 { 00645 status = PWM_STATUS_FAILURE; 00646 XMC_DEBUG("PWM_lCCU8_SetFreq:frequency could not be set") 00647 } 00648 00649 return status; 00650 } /* end of PWM_lCCU8_SetFreq() api */ 00651 00652 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 00653 /*Sets the frequency and duty cycle for CCU8 slice. */ 00654 PWM_STATUS_t PWM_lCCU8_SetFreqAndDutyCycle(PWM_t *const handle_ptr, uint32_t pwm_freq_hz, uint32_t duty_cycle) 00655 { 00656 PWM_STATUS_t status; 00657 uint32_t module_freq; 00658 uint8_t prescaler; 00659 uint32_t period_value; 00660 uint32_t compare; 00661 00662 XMC_ASSERT("PWM_lCCU8_SetFreqAndDutyCycle:Invalid pwm_freq_hz " , (pwm_freq_hz != 0U)) 00663 XMC_ASSERT("PWM_lCCU8_SetFreqAndDutyCycle:Invalid duty_cycle",((duty_cycle >= 0) && 00664 (duty_cycle <= PWM_MAX_DUTY_CYCLE))) 00665 00666 status = handle_ptr->state; 00667 prescaler = 0U; 00668 period_value = 0U; 00669 00670 /* Can't set the frequency when the PWM is not yet initialized or when required frequency is 0*/ 00671 if ((status != PWM_STATUS_UNINITIALIZED) && ((uint32_t)0 != pwm_freq_hz)) 00672 { 00673 status = PWM_STATUS_SUCCESS; 00674 /*Get the Module frequency*/ 00675 module_freq = handle_ptr->global_ccu8_handle->module_frequency; 00676 00677 /*Calculate the prescaler and the period register values.*/ 00678 while (prescaler <= PWM_MAX_PRESCALER) 00679 { 00680 period_value = (uint32_t)((uint32_t)module_freq / (uint32_t)pwm_freq_hz) >> (uint32_t)prescaler; 00681 /*If the prescaler selected is not big enough goto the next prescaler value else come out.*/ 00682 if (period_value <= PWM_MAX_TIMER_COUNT) 00683 { 00684 break; 00685 } 00686 prescaler++; 00687 } 00688 00689 /*Can't set the frequency if the required value is too small or when the required frequency is too large.*/ 00690 if ((prescaler > PWM_MAX_PRESCALER) || (duty_cycle > PWM_MAX_DUTY_CYCLE) || ((uint32_t)0 == period_value)) 00691 { 00692 XMC_DEBUG("PWM_lCCU8_SetFreqAndDutyCycle:Frequency or duty cycle could not be set") 00693 status = PWM_STATUS_FAILURE; 00694 } 00695 else 00696 { 00697 /*Calculate the new compare values using new period values */ 00698 compare = (period_value * ((uint32_t)PWM_MAX_DUTY_CYCLE - duty_cycle)) / ((uint32_t) 100 * PWM_DUTY_CYCLE_SCALE); 00699 00700 XMC_CCU8_SLICE_SetPrescaler(handle_ptr->ccu8_slice_ptr, prescaler); 00701 00702 /* The period register is always one count less than calculated.*/ 00703 period_value = period_value - (uint32_t)1; 00704 XMC_CCU8_SLICE_SetTimerPeriodMatch(handle_ptr->ccu8_slice_ptr, (uint16_t)(period_value)); 00705 00706 XMC_CCU8_SLICE_SetTimerCompareMatch(handle_ptr->ccu8_slice_ptr, XMC_CCU8_SLICE_COMPARE_CHANNEL_1, 00707 (uint16_t)compare); 00708 00709 XMC_CCU8_EnableShadowTransfer(handle_ptr->ccu8_kernel_ptr, handle_ptr->shadow_mask); 00710 00711 handle_ptr->compare_value = compare; 00712 handle_ptr->period_value = period_value; 00713 handle_ptr->duty_cycle = duty_cycle; 00714 XMC_DEBUG("PWM_lCCU8_SetFreqAndDutyCycle:Frequency and Duty cycle set") 00715 } 00716 } 00717 else 00718 { 00719 status = PWM_STATUS_FAILURE; 00720 XMC_DEBUG("PWM_lCCU8_SetFreqAndDutyCycle:Frequency and Duty cycle could not be set") 00721 } 00722 00723 00724 return status; 00725 } /* end of PWM_lCCU8_SetFreqAndDutyCycle() api */ 00726 00727 #endif /* end of CCU8 function definitions */ 00728 00729 /********************************************************************************************************************** 00730 * API IMPLEMENTATION 00731 **********************************************************************************************************************/ 00732 00733 /*This function returns the version of the PWM App*/ 00734 DAVE_APP_VERSION_t PWM_GetAppVersion(void) 00735 { 00736 DAVE_APP_VERSION_t version; 00737 00738 version.major = (uint8_t) PWM_MAJOR_VERSION; 00739 version.minor = (uint8_t) PWM_MINOR_VERSION; 00740 version.patch = (uint8_t) PWM_PATCH_VERSION; 00741 00742 return version; 00743 } 00744 00745 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 00746 /* This function initializes the app */ 00747 PWM_STATUS_t PWM_Init(PWM_t *const handle_ptr) 00748 { 00749 PWM_STATUS_t status; 00750 status = PWM_STATUS_FAILURE; 00751 00752 XMC_ASSERT("PWM_Init:Invalid handle_ptr" , (handle_ptr != NULL)) 00753 00754 #ifdef PWM_SLICE_USED_CCU4 00755 if (PWM_TIMER_SLICE_CCU4 == handle_ptr->timer_type) 00756 { 00757 status = PWM_lCCU4_Init(handle_ptr); 00758 } 00759 #endif 00760 00761 #ifdef PWM_SLICE_USED_CCU8 00762 if (PWM_TIMER_SLICE_CCU8 == handle_ptr->timer_type) 00763 { 00764 status = PWM_lCCU8_Init(handle_ptr); 00765 } 00766 #endif 00767 00768 return (status); 00769 } 00770 00771 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 00772 /* This function starts the PWM generation. This needs to be called even if external start is configured.*/ 00773 void PWM_Start(PWM_t *const handle_ptr) 00774 { 00775 XMC_ASSERT("PWM_Start:Invalid handle_ptr" , (handle_ptr != NULL)) 00776 00777 #ifdef PWM_SLICE_USED_CCU4 00778 if (PWM_TIMER_SLICE_CCU4 == handle_ptr->timer_type) 00779 { 00780 PWM_lCCU4_Start(handle_ptr); 00781 } 00782 #endif 00783 00784 #ifdef PWM_SLICE_USED_CCU8 00785 if (PWM_TIMER_SLICE_CCU8 == handle_ptr->timer_type) 00786 { 00787 PWM_lCCU8_Start(handle_ptr); 00788 } 00789 #endif 00790 } 00791 00792 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 00793 /* This function stops the PWM generation. */ 00794 void PWM_Stop(PWM_t *const handle_ptr) 00795 { 00796 00797 XMC_ASSERT("PWM_Stop:Invalid handle_ptr" , (handle_ptr != NULL)) 00798 00799 #ifdef PWM_SLICE_USED_CCU4 00800 if (PWM_TIMER_SLICE_CCU4 == handle_ptr->timer_type) 00801 { 00802 PWM_lCCU4_Stop(handle_ptr); 00803 } 00804 #endif 00805 00806 #ifdef PWM_SLICE_USED_CCU8 00807 if (PWM_TIMER_SLICE_CCU8 == handle_ptr->timer_type) 00808 { 00809 PWM_lCCU8_Stop(handle_ptr); 00810 } 00811 #endif 00812 } 00813 00814 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 00815 /*This function is used to set the duty cycle (uint32_t) of the PWM waveform */ 00816 PWM_STATUS_t PWM_SetDutyCycle(PWM_t *const handle_ptr, uint32_t duty_cycle) 00817 { 00818 PWM_STATUS_t status; 00819 status = PWM_STATUS_FAILURE; 00820 00821 XMC_ASSERT("PWM_SetDutyCycle:Invalid handle_ptr" , (handle_ptr != NULL)) 00822 00823 #ifdef PWM_SLICE_USED_CCU4 00824 if (PWM_TIMER_SLICE_CCU4 == handle_ptr->timer_type) 00825 { 00826 status = PWM_lCCU4_SetDutyCycle(handle_ptr, duty_cycle); 00827 } 00828 #endif 00829 00830 #ifdef PWM_SLICE_USED_CCU8 00831 if (PWM_TIMER_SLICE_CCU8 == handle_ptr->timer_type) 00832 { 00833 status = PWM_lCCU8_SetDutyCycle(handle_ptr, duty_cycle); 00834 } 00835 #endif 00836 return (status); 00837 } 00838 00839 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 00840 /*This function changes the PWM frequency. Input parameter is the frequency value in Hz */ 00841 PWM_STATUS_t PWM_SetFreq(PWM_t *const handle_ptr, uint32_t pwm_freq_hz) 00842 { 00843 PWM_STATUS_t status; 00844 status = PWM_STATUS_FAILURE; 00845 00846 XMC_ASSERT("PWM_SetFreq:Invalid handle_ptr" , (handle_ptr != NULL)) 00847 00848 #ifdef PWM_SLICE_USED_CCU4 00849 if (PWM_TIMER_SLICE_CCU4 == handle_ptr->timer_type) 00850 { 00851 status = PWM_lCCU4_SetFreq(handle_ptr, pwm_freq_hz); 00852 } 00853 #endif 00854 00855 #ifdef PWM_SLICE_USED_CCU8 00856 if (PWM_TIMER_SLICE_CCU8 == handle_ptr->timer_type) 00857 { 00858 status = PWM_lCCU8_SetFreq(handle_ptr, pwm_freq_hz); 00859 } 00860 #endif 00861 return status; 00862 } 00863 00864 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 00865 /*This function sets frequency and the duty cycle */ 00866 PWM_STATUS_t PWM_SetFreqAndDutyCycle(PWM_t *const handle_ptr, uint32_t pwm_freq_hz, uint32_t duty_cycle) 00867 { 00868 PWM_STATUS_t status; 00869 status = PWM_STATUS_FAILURE; 00870 00871 XMC_ASSERT("PWM_SetFreqAndDutyCycle:Invalid handle_ptr" , (handle_ptr != NULL)) 00872 00873 #ifdef PWM_SLICE_USED_CCU4 00874 if (PWM_TIMER_SLICE_CCU4 == handle_ptr->timer_type) 00875 { 00876 status = PWM_lCCU4_SetFreqAndDutyCycle(handle_ptr, pwm_freq_hz, duty_cycle); 00877 } 00878 #endif 00879 00880 #ifdef PWM_SLICE_USED_CCU8 00881 if (PWM_TIMER_SLICE_CCU8 == handle_ptr->timer_type) 00882 { 00883 status = PWM_lCCU8_SetFreqAndDutyCycle(handle_ptr, pwm_freq_hz, duty_cycle); 00884 } 00885 #endif 00886 return status; 00887 } 00888 00889 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 00890 /*This function changes the PWM timer status_timer */ 00891 bool PWM_GetTimerStatus(PWM_t *const handle_ptr) 00892 { 00893 bool status_timer; 00894 status_timer = (bool)false; 00895 00896 XMC_ASSERT("PWM_GetTimerStatus:Invalid handle_ptr" , (handle_ptr != NULL)) 00897 00898 #ifdef PWM_SLICE_USED_CCU4 00899 if (PWM_TIMER_SLICE_CCU4 == handle_ptr->timer_type) 00900 { 00901 status_timer = XMC_CCU4_SLICE_IsTimerRunning(handle_ptr->ccu4_slice_ptr); 00902 } 00903 #endif 00904 00905 #ifdef PWM_SLICE_USED_CCU8 00906 if (PWM_TIMER_SLICE_CCU8 == handle_ptr->timer_type) 00907 { 00908 status_timer = XMC_CCU8_SLICE_IsTimerRunning(handle_ptr->ccu8_slice_ptr); 00909 } 00910 #endif 00911 00912 return (status_timer); 00913 } 00914 00915 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 00916 /*This function returns the interrupt status_timer */ 00917 bool PWM_GetInterruptStatus(PWM_t *const handle_ptr, PWM_INTERRUPT_t pwm_interrupt) 00918 { 00919 bool status; 00920 status = (bool) false; 00921 00922 XMC_ASSERT("PWM_GetInterruptStatus:Invalid handle_ptr" , (handle_ptr != NULL)) 00923 00924 #ifdef PWM_SLICE_USED_CCU4 00925 if (PWM_TIMER_SLICE_CCU4 == handle_ptr->timer_type) 00926 { 00927 status = XMC_CCU4_SLICE_GetEvent(handle_ptr->ccu4_slice_ptr, (XMC_CCU4_SLICE_IRQ_ID_t)pwm_interrupt); 00928 } 00929 #endif 00930 00931 #ifdef PWM_SLICE_USED_CCU8 00932 if (PWM_TIMER_SLICE_CCU8 == handle_ptr->timer_type) 00933 { 00934 status = XMC_CCU8_SLICE_GetEvent(handle_ptr->ccu8_slice_ptr, (XMC_CCU8_SLICE_IRQ_ID_t)pwm_interrupt); 00935 } 00936 #endif 00937 00938 return status; 00939 } 00940 00941 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 00942 /*This function Acknowledges the corresponding interrupt */ 00943 void PWM_ClearEvent(PWM_t *const handle_ptr, PWM_INTERRUPT_t pwm_interrupt) 00944 { 00945 XMC_ASSERT("PWM_ClearEvent:Invalid handle_ptr" , (handle_ptr != NULL)) 00946 00947 #ifdef PWM_SLICE_USED_CCU4 00948 if (PWM_TIMER_SLICE_CCU4 == handle_ptr->timer_type) 00949 { 00950 XMC_CCU4_SLICE_ClearEvent(handle_ptr->ccu4_slice_ptr, (XMC_CCU4_SLICE_IRQ_ID_t) pwm_interrupt); 00951 } 00952 #endif 00953 00954 #ifdef PWM_SLICE_USED_CCU8 00955 if (PWM_TIMER_SLICE_CCU8 == handle_ptr->timer_type) 00956 { 00957 XMC_CCU8_SLICE_ClearEvent(handle_ptr->ccu8_slice_ptr, (XMC_CCU8_SLICE_IRQ_ID_t) pwm_interrupt); 00958 } 00959 #endif 00960 } 00961 00962 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 00963 /*This function sets the passive level of the PWM*/ 00964 void PWM_SetPassiveLevel(PWM_t *const handle_ptr, PWM_OUTPUT_PASSIVE_LEVEL_t pwm_output_passive_level) 00965 { 00966 XMC_ASSERT("PWM_SetPassiveLevel:Invalid handle_ptr" , (handle_ptr != NULL)) 00967 XMC_ASSERT("PWM_SetPassiveLevel:Invalid pwm_output_passive_level " , 00968 (pwm_output_passive_level < PWM_OUTPUT_PASSIVE_LEVEL_MAX)); 00969 00970 #ifdef PWM_SLICE_USED_CCU4 00971 if (PWM_TIMER_SLICE_CCU4 == handle_ptr->timer_type) 00972 { 00973 XMC_CCU4_SLICE_SetPassiveLevel(handle_ptr->ccu4_slice_ptr, 00974 (XMC_CCU4_SLICE_OUTPUT_PASSIVE_LEVEL_t)pwm_output_passive_level); 00975 00976 XMC_CCU4_EnableShadowTransfer(handle_ptr->ccu4_kernel_ptr, handle_ptr->shadow_mask); 00977 XMC_DEBUG("PWM_SetPassiveLevel:CCU4 slice, passive level changed") 00978 } 00979 #endif 00980 00981 #ifdef PWM_SLICE_USED_CCU8 00982 if (PWM_TIMER_SLICE_CCU8 == handle_ptr->timer_type) 00983 { 00984 XMC_CCU8_SLICE_SetPassiveLevel(handle_ptr->ccu8_slice_ptr, XMC_CCU8_SLICE_OUTPUT_0, 00985 (XMC_CCU8_SLICE_OUTPUT_PASSIVE_LEVEL_t)pwm_output_passive_level); 00986 00987 XMC_CCU8_EnableShadowTransfer(handle_ptr->ccu8_kernel_ptr, handle_ptr->shadow_mask); 00988 XMC_DEBUG("PWM_SetPassiveLevel:CCU8 slice, passive level changed") 00989 } 00990 #endif 00991 } 00992 00993 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 00994 /*Configures the period register */ 00995 PWM_STATUS_t PWM_SetPeriodMatchValue(PWM_t *const handle_ptr, uint32_t period_match_value) 00996 { 00997 uint32_t compare; 00998 PWM_STATUS_t status; 00999 01000 XMC_ASSERT("PWM_SetPeriodMatchValue:Invalid handle_ptr" , (handle_ptr != NULL)) 01001 XMC_ASSERT("PWM_SetPeriodMatchValue:Invalid period_match_value" , (period_match_value <= PWM_MAX_PERIOD_VALUE)) 01002 01003 status = handle_ptr->state; 01004 01005 if ( (status != PWM_STATUS_UNINITIALIZED) && (PWM_MAX_PERIOD_VALUE >= period_match_value)) 01006 { 01007 compare = (period_match_value * ((uint32_t)PWM_MAX_DUTY_CYCLE - handle_ptr->duty_cycle)) 01008 / ((uint32_t) 100 * PWM_DUTY_CYCLE_SCALE); 01009 01010 #ifdef PWM_SLICE_USED_CCU4 01011 if (PWM_TIMER_SLICE_CCU4 == handle_ptr->timer_type) 01012 { 01013 XMC_CCU4_SLICE_SetTimerPeriodMatch(handle_ptr->ccu4_slice_ptr, (uint16_t)period_match_value); 01014 01015 XMC_CCU4_SLICE_SetTimerCompareMatch(handle_ptr->ccu4_slice_ptr, (uint16_t)compare ); 01016 01017 XMC_CCU4_EnableShadowTransfer(handle_ptr->ccu4_kernel_ptr, handle_ptr->shadow_mask); 01018 } 01019 #endif 01020 01021 #ifdef PWM_SLICE_USED_CCU8 01022 if (PWM_TIMER_SLICE_CCU8 == handle_ptr->timer_type) 01023 { 01024 XMC_CCU8_SLICE_SetTimerPeriodMatch(handle_ptr->ccu8_slice_ptr, (uint16_t)period_match_value); 01025 01026 XMC_CCU8_SLICE_SetTimerCompareMatch(handle_ptr->ccu8_slice_ptr, XMC_CCU8_SLICE_COMPARE_CHANNEL_1, 01027 (uint16_t)compare); 01028 01029 XMC_CCU8_EnableShadowTransfer(handle_ptr->ccu8_kernel_ptr, handle_ptr->shadow_mask); 01030 } 01031 #endif 01032 handle_ptr->period_value = period_match_value; 01033 handle_ptr->compare_value = compare; 01034 status = PWM_STATUS_SUCCESS; 01035 } 01036 else 01037 { 01038 status = PWM_STATUS_FAILURE; 01039 } 01040 01041 return (status); 01042 } 01043 /*CODE_BLOCK_END*/ 01044