PER Firmware
Loading...
Searching...
No Matches
rcc.h
Go to the documentation of this file.
1
11#ifndef _PHAL_RCC_H_
12#define _PHAL_RCC_H_
13
14#include "common/phal_F4_F7/phal_F4_F7.h"
15
16#define HSE_CLOCK_RATE_HZ (16000000)
17
18#define HSE_CLOCK_RATE_HZ_INVALID (1) /* High Speed External oscilator value */
19#ifndef HSE_CLOCK_RATE_HZ
20#define HSE_CLOCK_RATE_HZ HSE_CLOCK_RATE_HZ_INVALID /* Define this in order to configure clocks to use the HSE clock */
21#endif // HSE_CLOCK_RATE_HZ
22
23#define HSI_CLOCK_RATE_HZ (16000000)
24#define MCO_OUT_PIN (8)
25
26// RCC Constants
27#if defined(STM32F407xx)
28
29#define RCC_MAX_VCO_RATE_HZ ((uint32_t)432e6)
30#define RCC_MIN_VCO_RATE_HZ ((uint32_t)100e6)
31#define RCC_MIN_PLL_INPUT_DIVISOR (2U)
32#define RCC_MAX_PLL_INPUT_DIVISOR (63U)
33#define RCC_MIN_PLL_OUTPUT_MULTIPLIER (50U)
34#define RCC_MAX_PLL_OUTPUT_MULTIPLIER (432U)
35#define RCC_MAX_SYSCLK_TARGET_HZ (168000000)
36
37#elif defined(STM32F732xx)
38
39#define RCC_MAX_VCO_RATE_HZ ((uint32_t)432e6)
40#define RCC_MIN_VCO_RATE_HZ ((uint32_t)100e6)
41#define RCC_MIN_PLL_INPUT_DIVISOR (2U)
42#define RCC_MAX_PLL_INPUT_DIVISOR (63U)
43#define RCC_MIN_PLL_OUTPUT_MULTIPLIER (50U)
44#define RCC_MAX_PLL_OUTPUT_MULTIPLIER (432U)
45#define RCC_MAX_SYSCLK_TARGET_HZ (216000000)
46
47#else
48#error "Please define a MCU arch"
49#endif
50
51typedef enum {
52 PLL_SRC_HSI16,
53 PLL_SRC_HSE
54} PLLSrc_t;
55
56typedef enum {
57 MCO1_SRC_HSI = 0,
58 MCO1_SRC_LSE = 1,
59 MCO1_SRC_HSE = 2,
60 MCO1_SRC_PLL = 3,
61
62} MCO1Source_t;
63
64typedef enum {
65 MCO_DIV_NONE = 0,
66 MCO_DIV_2 = 4,
67 MCO_DIV_3 = 5,
68 MCO_DIV_4 = 6,
69 MCO_DIV_5 = 7
70
71} MCODivisor_t;
72
73typedef enum {
74 CLOCK_SOURCE_HSI = 0,
75 CLOCK_SOURCE_HSE = 1,
76} ClockSrc_t;
77
78typedef enum {
79 RCC_ERROR_AHB_INIT = 0,
80 RCC_ERROR_APB1_INIT = 1,
81 RCC_ERROR_APB2_INIT = 2,
82 RCC_ERROR_HSI_INIT = 3,
83 RCC_ERROR_PLLSYS_INIT = 4,
84 RCC_ERROR_PLLVCO_INIT = 5,
85 RCC_ERROR_HSE_INIT = 6,
86} RCCErrors_t;
87
88typedef struct {
89 ClockSrc_t clock_source; /* Use HSE or not */
90 bool use_pll; /* Use PLL or not */
91 uint32_t system_clock_target_hz; /* System Core Clock rate */
92 uint32_t ahb_clock_target_hz; /* AHB clock rate target */
93 uint32_t apb1_clock_target_hz; /* APB1 clock rate target */
94 uint32_t apb2_clock_target_hz; /* APB2 clock rate target */
95
96 /* Only used for use_pll == true */
97 PLLSrc_t pll_src; /* Input source for PLL VCO */
98 uint32_t vco_output_rate_target_hz; /* VCO output target rate */
99 uint32_t msi_output_rate_target_hz; /* Use if pll_src == MSI */
101
110
121bool PHAL_configurePLLVCO(PLLSrc_t pll_source, uint32_t vco_output_rate_target_hz);
122
131bool PHAL_configurePLLSystemClock(uint32_t system_clock_target_hz);
132
141
150
158bool PHAL_configureAHBClock(uint32_t ahb_clock_target_hz);
159
167bool PHAL_configureAPB1Clock(uint32_t apb1_clock_target_hz);
168
176bool PHAL_configureAPB2Clock(uint32_t apb2_clock_target_hz);
177
184void PHAL_trimHSI(uint8_t trim_val);
185
203bool PHAL_enableMCO1(GPIO_TypeDef* bank, uint8_t pin, MCO1Source_t source, MCODivisor_t division);
204
205#endif // _PHAL_PLL_H_
bool PHAL_configureAPB1Clock(uint32_t apb1_clock_target_hz)
Configure APB1 Clock rate by modifying the APB1 prescaler value.
Definition rcc.c:318
bool PHAL_configureAHBClock(uint32_t ahb_clock_target_hz)
Configure AHB Clock rate by modifying the AHB prescaler value.
Definition rcc.c:267
bool PHAL_configureHSISystemClock()
Configure HSI CLK as the System Clock. SHOULD BE DONE BEFORE ANY OF THE AHB OR APB CLOCKS ARE CHANGED...
Definition rcc.c:215
uint8_t PHAL_configureClockRates(ClockRateConfig_t *config)
Configure all AHB/APB/System clocks from the provided configuration.
Definition rcc.c:20
bool PHAL_enableMCO1(GPIO_TypeDef *bank, uint8_t pin, MCO1Source_t source, MCODivisor_t division)
Enable the Clock Out Pin 1 It is highly recommended to change this only after reset before enabling t...
Definition rcc.c:402
bool PHAL_configurePLLSystemClock(uint32_t system_clock_target_hz)
Configure PLL CLK as the System Clock at the desired target frequency. SHOULD BE DONE BEFORE ANY OF T...
Definition rcc.c:121
bool PHAL_configureHSESystemClock()
Configure HSE CLK as the System Clock. SHOULD BE DONE BEFORE ANY OF THE AHB OR APB CLOCKS ARE CHANGED...
Definition rcc.c:237
bool PHAL_configureAPB2Clock(uint32_t apb2_clock_target_hz)
Configure APB1 Clock rate by modifying the APB2 prescaler value.
Definition rcc.c:356
bool PHAL_configurePLLVCO(PLLSrc_t pll_source, uint32_t vco_output_rate_target_hz)
Configure PLL VCO Clock rate The VCO clock is the input clock for the different PLL outputs....
Definition rcc.c:52
void PHAL_trimHSI(uint8_t trim_val)
Trim the HSI clock 0 <= trim_val <= 31 17 = increase speed by ~0.2% 15 = decrease speed by ~0....
Definition rcc.c:393
Definition rcc.h:88