-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRCC.c
152 lines (112 loc) · 3.87 KB
/
RCC.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stdint.h>
#include "stm32f446xx.h"
#include "ErrType.h"
#include "RCC_Interface.h"
#include "RCC_Prv.h"
#include "ErrType.h"
#include "stm32f446xx.h"
RCC_ErrorState_t RCC_SetClkSts(RCC_Clock_t Copy_ClockType, RCC_Status_t Copy_StatusType)
{
uint32_t Local_TimeoutAmount = TIMEOUT_AMOUNT;
if(RCC_STATUSTYPE_ON == Copy_StatusType)
{
switch (Copy_ClockType)
{
case RCC_CLOCKTYPE_PLL: SET_BIT(RCC->CR, RCC_CR_PLLON_POS); while(!GET_BIT(RCC->CR, RCC_CR_PLLRDY_POS) && --Local_TimeoutAmount) break;
case RCC_CLOCKTYPE_HSE: SET_BIT(RCC->CR, RCC_CR_HSEON_POS); while(!GET_BIT(RCC->CR, RCC_CR_HSERDY_POS) && --Local_TimeoutAmount) break;
case RCC_CLOCKTYPE_HSI: SET_BIT(RCC->CR, RCC_CR_HSION_POS); while(!GET_BIT(RCC->CR, RCC_CR_HSIRDY_POS) && --Local_TimeoutAmount) break;
default: return RCC_ErrorState_WRONG_OPTION;
}
}
else if(RCC_STATUSTYPE_OFF == Copy_StatusType)
{
switch (Copy_ClockType)
{
case RCC_CLOCKTYPE_PLL: CLR_BIT(RCC->CR, RCC_CR_PLLON_POS); break;
case RCC_CLOCKTYPE_HSE: CLR_BIT(RCC->CR, RCC_CR_HSEON_POS); break;
case RCC_CLOCKTYPE_HSI: CLR_BIT(RCC->CR, RCC_CR_HSION_POS); break;
default: return RCC_ErrorState_WRONG_OPTION;
}
}
else
{
return RCC_ErrorState_WRONG_OPTION;
}
if(0 == Local_TimeoutAmount)
{
return RCC_ErrorState_TIMEOUT;
}
return RCC_ErrorState_OK;
}
RCC_ErrorState_t RCC_SetSysClk(RCC_Clock_t Copy_ClockType)
{
switch (Copy_ClockType)
{
case RCC_CLOCKTYPE_PLL: SET_BITS(RCC->CFGR, RCC_CFGR_SW_BIT1); break;
case RCC_CLOCKTYPE_HSE: SET_BITS(RCC->CFGR, RCC_CFGR_SW_BIT0); break;
case RCC_CLOCKTYPE_HSI: CLR_BITS(RCC->CFGR, RCC_CFGR_SW); break;
default: return RCC_ErrorState_WRONG_OPTION;
}
return RCC_ErrorState_OK;
}
RCC_ErrorState_t RCC_SetHSEConfig(RCC_HSE_DivFactor Copy_DivFactor)
{
if(Copy_DivFactor == RCC_HSE_DIVFACTOR_DIV0)
{
CLR_BIT(RCC->CFGR, RCC_CFGR_PLLXTPRE_POS);
}
else if(Copy_DivFactor == RCC_HSE_DIVFACTOR_DIV2)
{
SET_BIT(RCC->CFGR, RCC_CFGR_PLLXTPRE_POS);
}
else
{
return RCC_ErrorState_WRONG_OPTION;
}
return RCC_ErrorState_OK;
}
RCC_ErrorState_t RCC_SetPLLConfig(RCC_PLL_MulFactor Copy_PllMulFactor, RCC_PLL_ClkSrc_t Copy_PllSource)
{
uint32_t Local_TmpReg = READ_REG(RCC->CFGR);
if(Copy_PllMulFactor > RCC_PLL_MULFACTOR_MUL16 || Copy_PllMulFactor < RCC_PLL_MULFACTOR_MUL2)
{
return RCC_ErrorState_WRONG_OPTION;
}
if((Copy_PllSource != RCC_PLL_CLKSRC_HSI) && (Copy_PllSource != RCC_PLL_CLKSRC_HSE))
{
return RCC_ErrorState_WRONG_OPTION;
}
SET_BITS(Local_TmpReg, (Copy_PllMulFactor << RCC_CFGR_PLLMUL_POS));
SET_BITS(Local_TmpReg, (Copy_PllSource << RCC_CFGR_PLLSRC_POS));
WRITE_REG(RCC->CFGR, Local_TmpReg);
return RCC_ErrorState_OK;
}
void RCC_AHB1EnableClock(RCC_AHB1_Peripheral_t Copy_Ahb1PeripheralName)
{
SET_BIT(RCC->AHB1ENR, Copy_Ahb1PeripheralName);
}
RCC_ErrorState_t RCC_AHB1DisableClock(RCC_AHB1_Peripheral_t Copy_Ahb1PeripheralName)
{
CLR_BIT(RCC->AHB1ENR, Copy_Ahb1PeripheralName);
return RCC_ErrorState_OK;
}
RCC_ErrorState_t RCC_APB1EnableClock(RCC_APB1_Peripheral_t Copy_Apb1PeripheralName)
{
SET_BIT(RCC->APB1ENR, Copy_Apb1PeripheralName);
return RCC_ErrorState_OK;
}
RCC_ErrorState_t RCC_APB1DisableClock(RCC_APB1_Peripheral_t Copy_Apb1PeripheralName)
{
CLR_BIT(RCC->APB1ENR, Copy_Apb1PeripheralName);
return RCC_ErrorState_OK;
}
RCC_ErrorState_t RCC_APB2EnableClock(RCC_APB2_Peripheral_t Copy_Apb2PeripheralName)
{
SET_BIT(RCC->APB2ENR, Copy_Apb2PeripheralName);
return RCC_ErrorState_OK;
}
RCC_ErrorState_t RCC_APB2DisableClock(RCC_APB2_Peripheral_t Copy_Apb2PeripheralName)
{
CLR_BIT(RCC->APB2ENR, Copy_Apb2PeripheralName);
return RCC_ErrorState_OK;
}