Skip to content

Commit 8cedb99

Browse files
committed
feat(System): Move System.c to ST-LIB
1 parent 9e7670b commit 8cedb99

4 files changed

Lines changed: 206 additions & 1 deletion

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ add_library(${STLIB_LIBRARY} OBJECT
414414
$<$<AND:$<BOOL:${CMAKE_CROSSCOMPILING}>,$<BOOL:${USE_ETHERNET}>>:${HALAL_C_ETH_PHY}>
415415

416416

417+
$<$<BOOL:${CMAKE_CROSSCOMPILING}>:${CMAKE_CURRENT_LIST_DIR}/System.c>
417418
$<$<BOOL:${CMAKE_CROSSCOMPILING}>:${CPP_UTILITIES_C}>
418419
$<$<BOOL:${CMAKE_CROSSCOMPILING}>:${CPP_UTILITIES_CPP}>
419420

Inc/ST-LIB.hpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,4 +348,17 @@ template <auto&... devs> struct Board {
348348
* before the global constructors and main() are called.
349349
* It is responsible for initializing the hardware and peripherals
350350
*/
351-
extern void BoardInit(void);
351+
#ifdef __cplusplus
352+
extern "C" {
353+
#endif
354+
355+
/**
356+
* @brief This is a function that gets called early in the startup process,
357+
* before the global constructors and main() are called.
358+
* It is responsible for initializing the hardware and peripherals
359+
*/
360+
void BoardInit(void);
361+
362+
#ifdef __cplusplus
363+
}
364+
#endif

Src/ST-LIB.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@ void STLIB::update() {
5050
ErrorHandlerModel::ErrorHandlerUpdate();
5151
MDMA::update();
5252
}
53+
54+
__attribute__((weak)) void BoardInit() {
55+
// Nothing for now
56+
}

System.c

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
#define BOOT_ATTR __attribute__((section(".boot"))) __used
2+
3+
#include "stm32h7xx.h"
4+
#include <math.h>
5+
6+
#if !defined(HSE_VALUE)
7+
#define HSE_VALUE ((uint32_t)25000000) /*!< Value of the External oscillator in Hz */
8+
#endif /* HSE_VALUE */
9+
10+
#if !defined(CSI_VALUE)
11+
#define CSI_VALUE ((uint32_t)4000000) /*!< Value of the Internal oscillator in Hz*/
12+
#endif /* CSI_VALUE */
13+
14+
#if !defined(HSI_VALUE)
15+
#define HSI_VALUE ((uint32_t)64000000) /*!< Value of the Internal oscillator in Hz*/
16+
#endif /* HSI_VALUE */
17+
18+
uint32_t SystemCoreClock = 64000000;
19+
uint32_t SystemD2Clock = 64000000;
20+
const uint8_t D1CorePrescTable[16] = {0, 0, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 6, 7, 8, 9};
21+
22+
BOOT_ATTR void SystemInit(void) {
23+
__IO uint32_t tmpreg;
24+
25+
SCB->CPACR |= ((3UL << (10 * 2)) | (3UL << (11 * 2))); /* set CP10 and CP11 Full Access */
26+
/* Reset the RCC clock configuration to the default reset state ------------*/
27+
28+
/* Increasing the CPU frequency */
29+
if (FLASH_LATENCY_DEFAULT > (READ_BIT((FLASH->ACR), FLASH_ACR_LATENCY))) {
30+
/* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
31+
MODIFY_REG(FLASH->ACR, FLASH_ACR_LATENCY, (uint32_t)(FLASH_LATENCY_DEFAULT));
32+
}
33+
34+
/* Set HSION bit */
35+
RCC->CR |= RCC_CR_HSION;
36+
37+
/* Reset CFGR register */
38+
RCC->CFGR = 0x00000000;
39+
40+
/* Reset HSEON, HSECSSON, CSION, HSI48ON, CSIKERON, PLL1ON, PLL2ON and PLL3ON bits */
41+
RCC->CR &= 0xEAF6ED7FU;
42+
43+
/* Decreasing the number of wait states because of lower CPU frequency */
44+
if (FLASH_LATENCY_DEFAULT < (READ_BIT((FLASH->ACR), FLASH_ACR_LATENCY))) {
45+
/* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */
46+
MODIFY_REG(FLASH->ACR, FLASH_ACR_LATENCY, (uint32_t)(FLASH_LATENCY_DEFAULT));
47+
}
48+
49+
/* Reset D1CFGR register */
50+
RCC->D1CFGR = 0x00000000;
51+
52+
/* Reset D2CFGR register */
53+
RCC->D2CFGR = 0x00000000;
54+
55+
/* Reset D3CFGR register */
56+
RCC->D3CFGR = 0x00000000;
57+
58+
/* Reset PLLCKSELR register */
59+
RCC->PLLCKSELR = 0x02020200;
60+
61+
/* Reset PLLCFGR register */
62+
RCC->PLLCFGR = 0x01FF0000;
63+
/* Reset PLL1DIVR register */
64+
RCC->PLL1DIVR = 0x01010280;
65+
/* Reset PLL1FRACR register */
66+
RCC->PLL1FRACR = 0x00000000;
67+
68+
/* Reset PLL2DIVR register */
69+
RCC->PLL2DIVR = 0x01010280;
70+
71+
/* Reset PLL2FRACR register */
72+
73+
RCC->PLL2FRACR = 0x00000000;
74+
/* Reset PLL3DIVR register */
75+
RCC->PLL3DIVR = 0x01010280;
76+
77+
/* Reset PLL3FRACR register */
78+
RCC->PLL3FRACR = 0x00000000;
79+
80+
/* Reset HSEBYP bit */
81+
RCC->CR &= 0xFFFBFFFFU;
82+
83+
/* Disable all interrupts */
84+
RCC->CIER = 0x00000000;
85+
86+
RCC->AHB2ENR |= (RCC_AHB2ENR_D2SRAM1EN | RCC_AHB2ENR_D2SRAM2EN);
87+
88+
tmpreg = RCC->AHB2ENR;
89+
(void)tmpreg;
90+
91+
/*
92+
* Disable the FMC bank1 (enabled after reset).
93+
* This, prevents CPU speculation access on this bank which blocks the use of FMC during
94+
* 24us. During this time the others FMC master (such as LTDC) cannot use it!
95+
*/
96+
FMC_Bank1_R->BTCR[0] = 0x000030D2;
97+
}
98+
99+
100+
BOOT_ATTR void SystemCoreClockUpdate(void) {
101+
uint32_t pllp, pllsource, pllm, pllfracen, hsivalue, tmp;
102+
uint32_t common_system_clock;
103+
float_t fracn1, pllvco;
104+
105+
/* Get SYSCLK source -------------------------------------------------------*/
106+
107+
switch (RCC->CFGR & RCC_CFGR_SWS) {
108+
case RCC_CFGR_SWS_HSI: /* HSI used as system clock source */
109+
common_system_clock = (uint32_t)(HSI_VALUE >> ((RCC->CR & RCC_CR_HSIDIV) >> 3));
110+
break;
111+
112+
case RCC_CFGR_SWS_CSI: /* CSI used as system clock source */
113+
common_system_clock = CSI_VALUE;
114+
break;
115+
116+
case RCC_CFGR_SWS_HSE: /* HSE used as system clock source */
117+
common_system_clock = HSE_VALUE;
118+
break;
119+
120+
case RCC_CFGR_SWS_PLL1: /* PLL1 used as system clock source */
121+
122+
/* PLL_VCO = (HSE_VALUE or HSI_VALUE or CSI_VALUE/ PLLM) * PLLN
123+
SYSCLK = PLL_VCO / PLLR
124+
*/
125+
pllsource = (RCC->PLLCKSELR & RCC_PLLCKSELR_PLLSRC);
126+
pllm = ((RCC->PLLCKSELR & RCC_PLLCKSELR_DIVM1) >> 4);
127+
pllfracen = ((RCC->PLLCFGR & RCC_PLLCFGR_PLL1FRACEN) >> RCC_PLLCFGR_PLL1FRACEN_Pos);
128+
fracn1 = (float_t)(uint32_t)(pllfracen * ((RCC->PLL1FRACR & RCC_PLL1FRACR_FRACN1) >> 3));
129+
130+
if (pllm != 0U) {
131+
switch (pllsource) {
132+
case RCC_PLLCKSELR_PLLSRC_HSI: /* HSI used as PLL clock source */
133+
134+
hsivalue = (HSI_VALUE >> ((RCC->CR & RCC_CR_HSIDIV) >> 3));
135+
pllvco = ((float_t)hsivalue / (float_t)pllm) *
136+
((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) +
137+
(fracn1 / (float_t)0x2000) + (float_t)1);
138+
139+
break;
140+
141+
case RCC_PLLCKSELR_PLLSRC_CSI: /* CSI used as PLL clock source */
142+
pllvco = ((float_t)CSI_VALUE / (float_t)pllm) *
143+
((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) +
144+
(fracn1 / (float_t)0x2000) + (float_t)1);
145+
break;
146+
147+
case RCC_PLLCKSELR_PLLSRC_HSE: /* HSE used as PLL clock source */
148+
pllvco = ((float_t)HSE_VALUE / (float_t)pllm) *
149+
((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) +
150+
(fracn1 / (float_t)0x2000) + (float_t)1);
151+
break;
152+
153+
default:
154+
hsivalue = (HSI_VALUE >> ((RCC->CR & RCC_CR_HSIDIV) >> 3));
155+
pllvco = ((float_t)hsivalue / (float_t)pllm) *
156+
((float_t)(uint32_t)(RCC->PLL1DIVR & RCC_PLL1DIVR_N1) +
157+
(fracn1 / (float_t)0x2000) + (float_t)1);
158+
break;
159+
}
160+
pllp = (((RCC->PLL1DIVR & RCC_PLL1DIVR_P1) >> 9) + 1U);
161+
common_system_clock = (uint32_t)(float_t)(pllvco / (float_t)pllp);
162+
} else {
163+
common_system_clock = 0U;
164+
}
165+
break;
166+
167+
default:
168+
common_system_clock = (uint32_t)(HSI_VALUE >> ((RCC->CR & RCC_CR_HSIDIV) >> 3));
169+
break;
170+
}
171+
172+
tmp = D1CorePrescTable[(RCC->D1CFGR & RCC_D1CFGR_D1CPRE) >> RCC_D1CFGR_D1CPRE_Pos];
173+
174+
/* common_system_clock frequency : CM7 CPU frequency */
175+
common_system_clock >>= tmp;
176+
177+
/* SystemD2Clock frequency : CM4 CPU, AXI and AHBs Clock frequency */
178+
SystemD2Clock =
179+
(common_system_clock >>
180+
((D1CorePrescTable[(RCC->D1CFGR & RCC_D1CFGR_HPRE) >> RCC_D1CFGR_HPRE_Pos]) & 0x1FU));
181+
182+
SystemCoreClock = common_system_clock;
183+
}
184+
185+
BOOT_ATTR void ConfigurationChecker() {
186+
// Nothing for now
187+
}

0 commit comments

Comments
 (0)