diff options
Diffstat (limited to 'lib/chibios-contrib/os/hal/ports/HT32/HT32F165x/hal_lld.c')
-rw-r--r-- | lib/chibios-contrib/os/hal/ports/HT32/HT32F165x/hal_lld.c | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/lib/chibios-contrib/os/hal/ports/HT32/HT32F165x/hal_lld.c b/lib/chibios-contrib/os/hal/ports/HT32/HT32F165x/hal_lld.c new file mode 100644 index 000000000..31ccd5fc4 --- /dev/null +++ b/lib/chibios-contrib/os/hal/ports/HT32/HT32F165x/hal_lld.c | |||
@@ -0,0 +1,123 @@ | |||
1 | /* | ||
2 | ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio | ||
3 | Copyright (C) 2020 Yaotian Feng | ||
4 | |||
5 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
6 | you may not use this file except in compliance with the License. | ||
7 | You may obtain a copy of the License at | ||
8 | |||
9 | http://www.apache.org/licenses/LICENSE-2.0 | ||
10 | |||
11 | Unless required by applicable law or agreed to in writing, software | ||
12 | distributed under the License is distributed on an "AS IS" BASIS, | ||
13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
14 | See the License for the specific language governing permissions and | ||
15 | limitations under the License. | ||
16 | */ | ||
17 | |||
18 | /** | ||
19 | * @file hal_lld.c | ||
20 | * @brief PLATFORM HAL subsystem low level driver source. | ||
21 | * | ||
22 | * @addtogroup HAL | ||
23 | * @{ | ||
24 | */ | ||
25 | |||
26 | |||
27 | #include "hal.h" | ||
28 | |||
29 | /*===========================================================================*/ | ||
30 | /* Driver local definitions. */ | ||
31 | /*===========================================================================*/ | ||
32 | |||
33 | /*===========================================================================*/ | ||
34 | /* Driver exported variables. */ | ||
35 | /*===========================================================================*/ | ||
36 | |||
37 | /*===========================================================================*/ | ||
38 | /* Driver local variables and types. */ | ||
39 | /*===========================================================================*/ | ||
40 | |||
41 | /*===========================================================================*/ | ||
42 | /* Driver local functions. */ | ||
43 | /*===========================================================================*/ | ||
44 | |||
45 | /*===========================================================================*/ | ||
46 | /* Driver interrupt handlers. */ | ||
47 | /*===========================================================================*/ | ||
48 | |||
49 | /*===========================================================================*/ | ||
50 | /* Driver exported functions. */ | ||
51 | /*===========================================================================*/ | ||
52 | |||
53 | void ht32_clock_init(void) { | ||
54 | // Enable backup domain. Needed for USB | ||
55 | CKCU->LPCR = CKCU_LPCR_BKISO; | ||
56 | CKCU->APBCCR1 |= CKCU_APBCCR1_BKPREN; | ||
57 | while (PWRCU->BAKTEST != 0x27); | ||
58 | |||
59 | #if HT32_CKCU_SW == CKCU_GCCR_SW_HSE | ||
60 | // Enable HSE | ||
61 | CKCU->GCCR |= CKCU_GCCR_HSEEN; | ||
62 | while ((CKCU->GCSR & CKCU_GCSR_HSERDY) == 0); // wait for HSE ready | ||
63 | #endif | ||
64 | |||
65 | #if HT32_CKCU_SW == CKCU_GCCR_SW_PLL | ||
66 | // Configure PLL | ||
67 | #if HT32_PLL_USE_HSE == TRUE | ||
68 | CKCU->GCFGR &= ~CKCU_GCFGR_PLLSRC; // HSE as PLL source | ||
69 | #else | ||
70 | CKCU->GCFGR |= CKCU_GCFGR_PLLSRC; // HSI as PLL source | ||
71 | #endif | ||
72 | CKCU->PLLCFGR = ((HT32_PLL_FBDIV & 0x3F) << 23) | ((HT32_PLL_OTDIV & 0x3) << 21); | ||
73 | CKCU->GCCR |= CKCU_GCCR_PLLEN; // enable PLL | ||
74 | while ((CKCU->GCSR & CKCU_GCSR_PLLRDY) == 0); // wait for PLL ready | ||
75 | #endif | ||
76 | |||
77 | // flash wait states for core clock frequencies | ||
78 | #if HT32_CK_AHB_FREQUENCY > 48000000 | ||
79 | FMC->CFCR = (FMC->CFCR & ~FMC_CFCR_WAIT_MASK) | FMC_CFCR_WAIT_2; | ||
80 | #elif HT32_CK_AHB_FREQUENCY > 24000000 | ||
81 | FMC->CFCR = (FMC->CFCR & ~FMC_CFCR_WAIT_MASK) | FMC_CFCR_WAIT_1; | ||
82 | #else | ||
83 | FMC->CFCR = (FMC->CFCR & ~FMC_CFCR_WAIT_MASK) | FMC_CFCR_WAIT_0; | ||
84 | #endif | ||
85 | |||
86 | // AHB prescaler | ||
87 | #if HT32_AHB_PRESCALER == 1 || HT32_AHB_PRESCALER == 2 | ||
88 | CKCU->AHBCFGR = (CKCU->AHBCFGR & ~CKCU_AHBCFGR_AHBPRE_MASK) | (HT32_AHB_PRESCALER - 1); | ||
89 | #elif HT32_AHB_PRESCALER == 4 | ||
90 | CKCU->AHBCFGR = (CKCU->AHBCFGR & ~CKCU_AHBCFGR_AHBPRE_MASK) | (2); | ||
91 | #elif HT32_AHB_PRESCALER == 8 | ||
92 | CKCU->AHBCFGR = (CKCU->AHBCFGR & ~CKCU_AHBCFGR_AHBPRE_MASK) | (3); | ||
93 | #else | ||
94 | #error "Invalid AHB_PRESCALER value" | ||
95 | #endif | ||
96 | |||
97 | // Clock switch | ||
98 | CKCU->GCCR = (CKCU->GCCR & ~CKCU_GCCR_SW_MASK) | HT32_CKCU_SW; | ||
99 | while ((CKCU->GCCR & CKCU_GCCR_SW_MASK) != HT32_CKCU_SW); // wait for clock switch | ||
100 | |||
101 | // HSI is needed for flash erase/write for some reason. | ||
102 | // Only disable if you will not need to erase/write memory | ||
103 | // with your debug probe after this firmware has booted. | ||
104 | // /* CKCU->GCCR &= ~CKCU_GCCR_HSIEN; */ | ||
105 | |||
106 | #if defined(HT32F1653_4) | ||
107 | // Peripheral prescalers are not available on HT32F1655/6 | ||
108 | // So make sure all prescalers are 1x on HT32F1653/4 | ||
109 | CKCU->APBPCSR0 = 0; | ||
110 | CKCU->APBPCSR1 = 0; | ||
111 | #endif | ||
112 | } | ||
113 | |||
114 | /** | ||
115 | * @brief Low level HAL driver initialization. | ||
116 | * | ||
117 | * @notapi | ||
118 | */ | ||
119 | void hal_lld_init(void) { | ||
120 | ht32_clock_init(); | ||
121 | } | ||
122 | |||
123 | /** @} */ | ||