2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
24 void RCC_ClockCmd(rccPeriphTag_t periphTag
, FunctionalState NewState
)
26 int tag
= periphTag
>> 5;
27 uint32_t mask
= 1 << (periphTag
& 0x1f);
29 #if defined(USE_HAL_DRIVER)
31 // Note on "suffix" macro parameter:
32 // ENR and RSTR naming conventions for buses with multiple registers per bus differs among MCU types.
33 // ST decided to use AxBn{L,H}ENR convention for H7 which can be handled with simple "ENR" (or "RSTR") contatenation,
34 // while use AxBnENR{1,2} convention for G4 which requires extra "suffix" to be concatenated.
35 // Here, we use "suffix" for all MCU types and leave it as empty where not applicable.
37 #define NOSUFFIX // Empty
39 #define __HAL_RCC_CLK_ENABLE(bus, suffix, enbit) do { \
40 __IO uint32_t tmpreg; \
41 SET_BIT(RCC->bus ## ENR ## suffix, enbit); \
42 /* Delay after an RCC peripheral clock enabling */ \
43 tmpreg = READ_BIT(RCC->bus ## ENR ## suffix, enbit); \
47 #define __HAL_RCC_CLK_DISABLE(bus, suffix, enbit) (RCC->bus ## ENR ## suffix &= ~(enbit))
49 #define __HAL_RCC_CLK(bus, suffix, enbit, newState) \
50 if (newState == ENABLE) { \
51 __HAL_RCC_CLK_ENABLE(bus, suffix, enbit); \
53 __HAL_RCC_CLK_DISABLE(bus, suffix, enbit); \
58 __HAL_RCC_CLK(AHB1
, NOSUFFIX
, mask
, NewState
);
62 __HAL_RCC_CLK(AHB2
, NOSUFFIX
, mask
, NewState
);
65 #if !(defined(STM32H7) || defined(STM32G4))
67 __HAL_RCC_CLK(APB1
, NOSUFFIX
, mask
, NewState
);
72 __HAL_RCC_CLK(APB2
, NOSUFFIX
, mask
, NewState
);
78 __HAL_RCC_CLK(AHB3
, NOSUFFIX
, mask
, NewState
);
82 __HAL_RCC_CLK(AHB4
, NOSUFFIX
, mask
, NewState
);
86 __HAL_RCC_CLK(APB1L
, NOSUFFIX
, mask
, NewState
);
90 __HAL_RCC_CLK(APB1H
, NOSUFFIX
, mask
, NewState
);
94 __HAL_RCC_CLK(APB3
, NOSUFFIX
, mask
, NewState
);
98 __HAL_RCC_CLK(APB4
, NOSUFFIX
, mask
, NewState
);
105 __HAL_RCC_CLK(APB1
, 1, mask
, NewState
);
109 __HAL_RCC_CLK(APB1
, 2, mask
, NewState
);
116 #if defined(STM32F3) || defined(STM32F1)
118 RCC_AHBPeriphClockCmd(mask
, NewState
);
122 RCC_APB2PeriphClockCmd(mask
, NewState
);
125 RCC_APB1PeriphClockCmd(mask
, NewState
);
129 RCC_AHB1PeriphClockCmd(mask
, NewState
);
136 void RCC_ResetCmd(rccPeriphTag_t periphTag
, FunctionalState NewState
)
138 int tag
= periphTag
>> 5;
139 uint32_t mask
= 1 << (periphTag
& 0x1f);
141 // Peripheral reset control relies on RSTR bits are identical to ENR bits where applicable
143 #define __HAL_RCC_FORCE_RESET(bus, suffix, enbit) (RCC->bus ## RSTR ## suffix |= (enbit))
144 #define __HAL_RCC_RELEASE_RESET(bus, suffix, enbit) (RCC->bus ## RSTR ## suffix &= ~(enbit))
145 #define __HAL_RCC_RESET(bus, suffix, enbit, NewState) \
146 if (NewState == ENABLE) { \
147 __HAL_RCC_RELEASE_RESET(bus, suffix, enbit); \
149 __HAL_RCC_FORCE_RESET(bus, suffix, enbit); \
152 #if defined(USE_HAL_DRIVER)
156 __HAL_RCC_RESET(AHB1
, NOSUFFIX
, mask
, NewState
);
160 __HAL_RCC_RESET(AHB2
, NOSUFFIX
, mask
, NewState
);
163 #if !(defined(STM32H7) || defined(STM32G4))
165 __HAL_RCC_RESET(APB1
, NOSUFFIX
, mask
, NewState
);
170 __HAL_RCC_RESET(APB2
, NOSUFFIX
, mask
, NewState
);
176 __HAL_RCC_RESET(AHB3
, NOSUFFIX
, mask
, NewState
);
180 __HAL_RCC_RESET(AHB4
, NOSUFFIX
, mask
, NewState
);
184 __HAL_RCC_RESET(APB1L
, NOSUFFIX
, mask
, NewState
);
188 __HAL_RCC_RESET(APB1H
, NOSUFFIX
, mask
, NewState
);
192 __HAL_RCC_RESET(APB3
, NOSUFFIX
, mask
, NewState
);
196 __HAL_RCC_RESET(APB4
, NOSUFFIX
, mask
, NewState
);
203 __HAL_RCC_CLK(APB1
, 1, mask
, NewState
);
207 __HAL_RCC_CLK(APB1
, 2, mask
, NewState
);
216 #if defined(STM32F3) || defined(STM32F10X_CL)
218 RCC_AHBPeriphResetCmd(mask
, NewState
);
222 RCC_APB2PeriphResetCmd(mask
, NewState
);
225 RCC_APB1PeriphResetCmd(mask
, NewState
);
229 RCC_AHB1PeriphResetCmd(mask
, NewState
);