Home | Projects | Notes > MCU Peripheral Drivers > RCC Driver (stm32f407xx_rcc_driver.h/.c
)
stm32f407xx_rcc_driver.h/.c
)
stm32f407xx_rcc_driver.h
Path: Project/Drivers/Inc/
xxxxxxxxxx
261/*******************************************************************************
2 * File : stm32f407xx_rcc_driver.h
3 * Brief : STM32F407xx MCU specific RCC driver header file
4 * Author ; Kyungjae Lee
5 * Date : Jun 20, 2023
6 *
7 * Note : This code includes only the features that are necessary for my
8 * personal projects.
9 * ****************************************************************************/
10
11
12
13
14
15
16/*******************************************************************************
17 * APIs supported by the SPI driver
18 * (See function definitions for more information)
19 ******************************************************************************/
20
21uint32_t RCC_GetPCLK1Value(void);
22uint32_t RCC_GetPCLK2Value(void);
23uint32_t RCC_GetPLLOutputClk(void);
24
25
26/* STM32F407XX_RCC_DRIVER_H */
stm32f407xx_rcc_driver.c
Path: Project/Drivers/Src/
xxxxxxxxxx
1521/*******************************************************************************
2 * File : stm32f407xx_spi_driver.c
3 * Brief : STM32F407xx MCU specific RCC driver source file
4 * Author ; Kyungjae Lee
5 * Date : Jun 20, 2023
6 *
7 * Note : This code includes only the features that are necessary for my
8 * personal projects.
9 * ****************************************************************************/
10
11
12
13/* RCC_CFGR HPRE[7:4] */
14uint16_t AHB_Prescalar[8] = {2, 4, 8, 16, 64, 128, 256, 512};
15
16/* RCC_CFGR PPRE1[10:8] */
17uint16_t APB1_Prescalar[4] = {2, 4, 8, 16};
18
19/**
20 * RCC_GetPCLK1Value()
21 * Brief : Computes and returns APB1 peripheral clock frequency
22 * Param : None
23 * Retval : APB1 peripheral clock frequency
24 * Note : N/A
25 */
26uint32_t RCC_GetPCLK1Value(void)
27{
28 uint32_t pclk1; /* APB1 peripheral clock frequency */
29 uint32_t sysClk; /* System clock frequency */
30 uint8_t clkSrc; /* Clock source */
31 uint8_t temp;
32 uint16_t ahbp; /* AHB prescalar */
33 uint16_t apb1p; /* APB1 prescalar */
34
35 clkSrc = (RCC->CFGR >> RCC_CFGR_SWS) & 0x03;
36
37 if (clkSrc == 0)
38 {
39 /* 00: HSI oscillator used as the system clock */
40 sysClk = 16000000; /* System clock = 16 MHz */
41 }
42 else if (clkSrc == 1)
43 {
44 /* 01: HSE oscillator used as the system clock */
45 sysClk = 8000000; /* In the case of STM32 Discovery board */
46 }
47 else if (clkSrc == 2)
48 {
49 /* 10: PLL used as the system clock */
50 sysClk = RCC_GetPLLOutputClk();
51 }
52
53 /* Obtain AHB prescalar */
54 temp = ((RCC->CFGR >> RCC_CFGR_HPRE) & 0xF);
55
56 if (temp < 8)
57 {
58 ahbp = 1;
59 }
60 else
61 {
62 ahbp = AHB_Prescalar[temp - 8];
63 }
64
65 /* Obtain APB1 prescalar */
66 temp = ((RCC->CFGR >> RCC_CFGR_PPRE1) & 0x7);
67
68 if (temp < 4)
69 {
70 apb1p = 1;
71 }
72 else
73 {
74 apb1p = APB1_Prescalar[temp - 4];
75 }
76
77 /* Compute APB1 peripheral clock frequency */
78 pclk1 = (sysClk / ahbp) / apb1p;
79
80 return pclk1;
81} /* End of RCC_GetPCLK1Value */
82
83/**
84 * RCC_GetPCLK2Value()
85 * Brief : Computes and returns APB2 peripheral clock frequency
86 * Param : None
87 * Retval : APB2 peripheral clock frequency
88 * Note : N/A
89 */
90uint32_t RCC_GetPCLK2Value(void)
91{
92 uint32_t pclk2; /* APB2 peripheral clock frequency */
93 uint32_t sysClk; /* System clock frequency */
94 uint8_t clkSrc; /* Clock source */
95 uint8_t temp;
96 uint16_t ahbp; /* AHB prescalar */
97 uint16_t apb2p; /* APB2 prescalar */
98
99 clkSrc = (RCC->CFGR >> RCC_CFGR_SWS) & 0x03;
100
101 if (clkSrc == 0)
102 {
103 /* 00: HSI oscillator used as the system clock */
104 sysClk = 16000000; /* System clock = 16 MHz */
105 }
106 else
107 {
108 sysClk = 8000000; /* In the case of STM32 Discovery board */
109 }
110
111 /* Obtain AHB prescalar */
112 temp = ((RCC->CFGR >> RCC_CFGR_HPRE) & 0xF);
113
114 if (temp < 8)
115 {
116 ahbp = 1;
117 }
118 else
119 {
120 ahbp = AHB_Prescalar[temp - 8];
121 }
122
123 /* Obtain APB2 prescalar */
124 temp = ((RCC->CFGR >> RCC_CFGR_PPRE2) & 0x7);
125
126 if (temp < 4)
127 {
128 apb2p = 1;
129 }
130 else
131 {
132 apb2p = APB1_Prescalar[temp - 4];
133 }
134
135 /* Compute APB2 peripheral clock frequency */
136 pclk2 = (sysClk / ahbp) / apb2p;
137
138 return pclk2;
139} /* End of RCC_GetPCLK2Value */
140
141/**
142 * RCC_GetPLLOutputClk()
143 * Brief : Controls PLL ouput clock
144 * Param : None
145 * Retval : None
146 * Note : To be implemented as per the project requirements. Not used
147 * at this point.
148 */
149uint32_t RCC_GetPLLOutputClk(void)
150{
151 return 0;
152} /* End of RCC_GetPLLOutputClk */