Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stm32] start implementing timer_get_frequency #286

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions include/libopencm3/stm32/common/timer_common_all.h
Expand Up @@ -1115,6 +1115,7 @@ void timer_slave_set_prescaler(uint32_t timer, enum tim_ic_psc psc);
void timer_slave_set_polarity(uint32_t timer, enum tim_et_pol pol);
void timer_slave_set_mode(uint32_t timer, uint8_t mode);
void timer_slave_set_trigger(uint32_t timer, uint8_t trigger);
uint32_t timer_get_frequency(uint32_t timer);

END_DECLS

Expand Down
2 changes: 2 additions & 0 deletions include/libopencm3/stm32/f1/rcc.h
Expand Up @@ -697,7 +697,9 @@ void rcc_set_pll_source(uint32_t pllsrc);
void rcc_set_pllxtpre(uint32_t pllxtpre);
void rcc_set_adcpre(uint32_t adcpre);
void rcc_set_ppre2(uint32_t ppre2);
uint32_t rcc_get_ppre2(void);
void rcc_set_ppre1(uint32_t ppre1);
uint32_t rcc_get_ppre1(void);
void rcc_set_hpre(uint32_t hpre);
void rcc_set_usbpre(uint32_t usbpre);
void rcc_set_prediv1(uint32_t prediv);
Expand Down
2 changes: 2 additions & 0 deletions include/libopencm3/stm32/f4/rcc.h
Expand Up @@ -772,7 +772,9 @@ void rcc_osc_bypass_disable(enum rcc_osc osc);
void rcc_set_sysclk_source(uint32_t clk);
void rcc_set_pll_source(uint32_t pllsrc);
void rcc_set_ppre2(uint32_t ppre2);
uint32_t rcc_get_ppre2(void);
void rcc_set_ppre1(uint32_t ppre1);
uint32_t rcc_get_ppre1(void);
void rcc_set_hpre(uint32_t hpre);
void rcc_set_rtcpre(uint32_t rtcpre);
void rcc_set_main_pll_hsi(uint32_t pllm, uint32_t plln, uint32_t pllp,
Expand Down
37 changes: 37 additions & 0 deletions lib/stm32/common/timer_common_all.c
Expand Up @@ -2182,6 +2182,43 @@ void timer_slave_set_trigger(uint32_t timer_peripheral, uint8_t trigger)
TIM_SMCR(timer_peripheral) |= trigger;
}

*---------------------------------------------------------------------------*/
/** @brief Get Timer clock frequency (before prescaling)
Only valid if using the internal clock for the timer.
@param[in] timer_peripheral Unsigned int32. Timer register address base
@return Unsigned int32. Timer base frequency
*/

uint32_t timer_get_frequency(uint32_t timer_peripheral)
{
switch (timer_peripheral) {
#if ADVANCED_TIMERS
case TIM1:
case TIM8:
if (!rcc_get_ppre2())
// no APB2 prescaler
return rcc_ppre2_frequency;
else
return rcc_ppre2_frequency * 2;
#endif
case TIM2:
case TIM3:
case TIM4:
case TIM5:
case TIM6:
case TIM7:
if (!rcc_get_ppre1())
// no APB2 prescaler
return rcc_ppre1_frequency;
else
return rcc_ppre1_frequency * 2;
default:
// other timers currently not supported
break;
}
return 0;
}

/* TODO Timer DMA burst */

/**@}*/
Expand Down
10 changes: 10 additions & 0 deletions lib/stm32/f1/rcc.c
Expand Up @@ -527,6 +527,11 @@ void rcc_set_ppre2(uint32_t ppre2)
(ppre2 << RCC_CFGR_PPRE2_SHIFT);
}

uint32_t rcc_get_ppre2(void)
{
return RCC_CFGR & RCC_CFGR_PPRE2;
}

/*---------------------------------------------------------------------------*/
/** @brief RCC Set the APB1 Prescale Factor.

Expand All @@ -542,6 +547,11 @@ void rcc_set_ppre1(uint32_t ppre1)

}

uint32_t rcc_get_ppre1(void)
{
return RCC_CFGR & RCC_CFGR_PPRE1;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any real motivation to have this as api funtions visible to users, it's something you would just use internally. If a user needs this information, they (should) have it from when they set up the clock themselves in the first place

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the only reason was to get this independent of the STM family, so the rest of the get_timer_frequency doesn't have to be implemented for each family...

/*---------------------------------------------------------------------------*/
/** @brief RCC Set the AHB Prescale Factor.

Expand Down
10 changes: 10 additions & 0 deletions lib/stm32/f4/rcc.c
Expand Up @@ -430,6 +430,11 @@ void rcc_set_ppre2(uint32_t ppre2)
RCC_CFGR = (reg32 | (ppre2 << 13));
}

uint32_t rcc_get_ppre2(void)
{
return RCC_CFGR &((1 << 13) | (1 << 14) | (1 << 15));
}

void rcc_set_ppre1(uint32_t ppre1)
{
uint32_t reg32;
Expand All @@ -439,6 +444,11 @@ void rcc_set_ppre1(uint32_t ppre1)
RCC_CFGR = (reg32 | (ppre1 << 10));
}

uint32_t rcc_get_ppre1(void)
{
return RCC_CFGR &((1 << 10) | (1 << 11) | (1 << 12));
}

void rcc_set_hpre(uint32_t hpre)
{
uint32_t reg32;
Expand Down