Skip to content

Commit

Permalink
v1.1: added functions related to TIM module
Browse files Browse the repository at this point in the history
  • Loading branch information
SMFSW committed Aug 16, 2017
1 parent 389ee48 commit db3bea4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 30 deletions.
60 changes: 36 additions & 24 deletions PWM.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,15 @@
/****************************************************************/


HAL_StatusTypeDef init_PWM_Chan(TIM_HandleTypeDef * pTim, uint32_t chan, uint16_t freq)
HAL_StatusTypeDef init_TIM_Base(TIM_HandleTypeDef * pTim, uint32_t freq)
{
HAL_StatusTypeDef st;
HAL_StatusTypeDef err;

/* Check the parameters */
assert_param(IS_TIM_INSTANCE(pTim->Instance));
assert_param(IS_TIM_CHANNELS(chan));

st = set_TIM_Freq(pTim, freq);
if (st) { return st; }
err = set_TIM_Interrupts(pTim, Off); // Stop interrupts if they were already started
err = set_TIM_Freq(pTim, freq); // Configure TIM frequency
if (err) { return err; }
return set_TIM_Interrupts(pTim, On); // Start interrupts

if (chan == TIM_CHANNEL_ALL)
{
#if defined(TIM_CHANNEL_6)
uint32_t chans[6] = { TIM_CHANNEL_1, TIM_CHANNEL_2, TIM_CHANNEL_3, TIM_CHANNEL_4, TIM_CHANNEL_5, TIM_CHANNEL_6 };
#else
uint32_t chans[4] = { TIM_CHANNEL_1, TIM_CHANNEL_2, TIM_CHANNEL_3, TIM_CHANNEL_4 };
#endif

for (int i = 0 ; i < SZ_OBJ(chans, uint32_t) ; i++)
{
st = set_PWM_Output(pTim, chans[i], true);
if (st) { break; }
}
return st;
}
else return set_PWM_Output(pTim, chan, true);
}


Expand Down Expand Up @@ -121,6 +103,36 @@ HAL_StatusTypeDef set_TIM_Freq(TIM_HandleTypeDef * pTim, uint32_t freq)
}


HAL_StatusTypeDef init_PWM_Chan(TIM_HandleTypeDef * pTim, uint32_t chan, uint16_t freq)
{
HAL_StatusTypeDef st;

/* Check the parameters */
assert_param(IS_TIM_INSTANCE(pTim->Instance));
assert_param(IS_TIM_CHANNELS(chan));

st = set_TIM_Freq(pTim, freq);
if (st) { return st; }

if (chan == TIM_CHANNEL_ALL)
{
#if defined(TIM_CHANNEL_6)
uint32_t chans[6] = { TIM_CHANNEL_1, TIM_CHANNEL_2, TIM_CHANNEL_3, TIM_CHANNEL_4, TIM_CHANNEL_5, TIM_CHANNEL_6 };
#else
uint32_t chans[4] = { TIM_CHANNEL_1, TIM_CHANNEL_2, TIM_CHANNEL_3, TIM_CHANNEL_4 };
#endif

for (int i = 0 ; i < SZ_OBJ(chans, uint32_t) ; i++)
{
st = set_PWM_Output(pTim, chans[i], true);
if (st) { break; }
}
return st;
}
else return set_PWM_Output(pTim, chan, true);
}


/*******************/
/*** PWM DRIVING ***/
/*******************/
Expand Down
29 changes: 23 additions & 6 deletions PWM.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,36 @@
// *****************************************************************************
// Section: Interface Routines
// *****************************************************************************
/*!\brief Init TIM PWM module channel with frequency and starts the channel
** \param[in,out] pTim - pointer to TIM instance for PWM generation
** \param[in] chan - Channel to write
** \param[in] freq - Desired PWM frequency
/*** TIM Base ***/
/*!\brief Init TIM module and start interruptions
** \param[in,out] pTim - pointer to TIM instance
** \param[in] freq - Desired TIM frequency
**/
HAL_StatusTypeDef init_PWM_Chan(TIM_HandleTypeDef * pTim, uint32_t chan, uint16_t freq);
HAL_StatusTypeDef init_TIM_Base(TIM_HandleTypeDef * pTim, uint32_t freq);


/*!\brief Set TIM module frequency
** \param[in,out] pTim - pointer to TIM instance for Frequency computation
** \param[in] freq - Desired TIM frequency
**/
HAL_StatusTypeDef set_TIM_Freq(TIM_HandleTypeDef * pTim, uint32_t freq);


/*!\brief Start TIM module interrupts
** \param[in,out] pTim - pointer to TIM instance
** \param[in] on - Time Interrupts 0: off, 1: on
**/
__INLINE HAL_StatusTypeDef INLINE__ set_TIM_Interrupts(TIM_HandleTypeDef * pTim, bool on) {
return on ? HAL_TIM_Base_Start_IT(pTim) : HAL_TIM_Base_Stop_IT(pTim); }


/*** PWM ***/
/*!\brief Init TIM PWM module channel with frequency and starts the channel
** \param[in,out] pTim - pointer to TIM instance for PWM generation
** \param[in] chan - Channel to write
** \param[in] freq - Desired PWM frequency
**/
HAL_StatusTypeDef set_TIM_Freq(TIM_HandleTypeDef * pTim, uint32_t freq);
HAL_StatusTypeDef init_PWM_Chan(TIM_HandleTypeDef * pTim, uint32_t chan, uint16_t freq);


/*!\brief Set PWM channel output on/off
Expand Down
3 changes: 3 additions & 0 deletions Release Notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ SOFTWARE.
* PWM: Fixed start/stop PWM channels all at once
* PWM: check for clock path following STM family used
* PWM: Added APB clock automatic scaling for TIM used (clock fed for frequency calculation)
* PWM: Added function to initialize TIM base and start its period elapsed interruptions
* PWM: Added inline to start/stop TIM module period elapsed interruptions
* stddream_rdir: added UNUSED macro when needed to avoid variable unused compilation warnings

## v1.0

Expand Down
8 changes: 8 additions & 0 deletions stdream_rdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ int printf_rdir(char * str, ...)
print_uart(buf_stream, len);
#endif

#if (!defined(ITM_ENABLED) && !defined(DBG_SERIAL))
UNUSED(len);
#endif

str_clr(buf_stream); // Empty string
return 0;
}
Expand All @@ -143,6 +147,10 @@ int vprintf_rdir(char * str, va_list args)
print_uart(buf_stream, len);
#endif

#if (!defined(ITM_ENABLED) && !defined(DBG_SERIAL))
UNUSED(len);
#endif

str_clr(buf_stream); // Empty string
return 0;
}
Expand Down

0 comments on commit db3bea4

Please sign in to comment.