Skip to content

Commit

Permalink
* TIM_ex: fix possible div by 0
Browse files Browse the repository at this point in the history
  • Loading branch information
SMFSW committed Mar 28, 2023
1 parent 005c42c commit c65002a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion PWM.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ HAL_StatusTypeDef NONNULL__ set_PWM_Duty_Scaled(const TIM_HandleTypeDef * const
{
uint32_t tmp;

if (!scale) { return HAL_ERROR; } // Division by 0
if (!scale) { return HAL_ERROR; } // Avoid div by 0

if (duty >= scale) { tmp = pTim->Instance->ARR + 1; } // +1 To achieve real 100% duty cycle
else if (duty == 0) { tmp = 0; }
Expand Down
1 change: 1 addition & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ SOFTWARE.

## v1.5

* TIM_ex: fix possible div by 0
* ADC_ex: Fixes for compensation when calibrated values differ from VDD_VALUE
* ADC_ex: Added customizable pre-processing symbols to allow better ADC configuration when needed (ADC_RESOLUTION, Def_VAlim, Def_VCal, Def_VBatFactor)
* ADC_ex: Few more families addressed (whole F G L ranges, H7, U5, WB, WL / missing C0, MP1, H5, WBA)
Expand Down
4 changes: 4 additions & 0 deletions TIM_ex.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ HAL_StatusTypeDef NONNULL__ init_TIM_Base(TIM_HandleTypeDef * const pTim, const

HAL_StatusTypeDef NONNULL__ set_TIM_Freq(TIM_HandleTypeDef * const pTim, const uint32_t freq)
{
if (!freq) { return HAL_ERROR; } // Avoid div by 0

const uint32_t max_prescaler = 0xFFFF; // Prescaler is 16b no redeeming timer instance
uint32_t max_period = 0xFFFF; // Max period for 16b timers (most common)
uint64_t period; // For 32b timers, period needs to be 64b large for calculations
Expand Down Expand Up @@ -213,6 +215,8 @@ HAL_StatusTypeDef NONNULL__ set_TIM_Freq(TIM_HandleTypeDef * const pTim, const u

HAL_StatusTypeDef NONNULL__ set_TIM_Tick_Freq(TIM_HandleTypeDef * const pTim, const uint32_t freq)
{
if (!freq) { return HAL_ERROR; } // Avoid div by 0

if ( (pTim->Instance == TIM2)
#if defined(TIM5)
|| (pTim->Instance == TIM5)
Expand Down

0 comments on commit c65002a

Please sign in to comment.