Compare commits

..

No commits in common. "4ec412bf434ab5c837ea8e05a393ca517d9fb11d" and "9140ee66f9586d2c69c49e365b95d3083dbf1b83" have entirely different histories.

4 changed files with 44 additions and 43 deletions

View file

@ -1,9 +1,9 @@
#include "system.h" #include "system.h"
typedef __SIZE_TYPE__ size_t; typedef __SIZE_TYPE__ size_t;
extern "C" { extern "C" {
[[using gnu: naked,nothrow,used]] extern void handle_reset (); extern void handle_reset () __attribute__((naked,nothrow,used));
[[gnu::used]] extern void user_init (); extern void user_init () __attribute__((used));
[[gnu::used]] extern int main (); extern int main () __attribute__((used));
// This is required to allow pure virtual functions to be defined. // This is required to allow pure virtual functions to be defined.
extern void __cxa_pure_virtual() { while (1); } extern void __cxa_pure_virtual() { while (1); }
@ -14,17 +14,41 @@ extern "C" {
extern uint32_t _data_vma; extern uint32_t _data_vma;
extern uint32_t _edata; extern uint32_t _edata;
[[gnu::weak]] extern void (*__preinit_array_start[]) (void); extern void (*__preinit_array_start[]) (void) __attribute__((weak));
[[gnu::weak]] extern void (*__preinit_array_end[]) (void); extern void (*__preinit_array_end[]) (void) __attribute__((weak));
[[gnu::weak]] extern void (*__init_array_start[]) (void); extern void (*__init_array_start[]) (void) __attribute__((weak));
[[gnu::weak]] extern void (*__init_array_end[]) (void); extern void (*__init_array_end[]) (void) __attribute__((weak));
static void __init_array () {
uint32_t * dst, * end;
/* Zero fill the bss section */
dst = &_sbss;
end = &_ebss;
while (dst < end) * dst++ = 0U;
/* Copy data section from flash to RAM */
uint32_t * src;
src = &_data_lma;
dst = &_data_vma;
end = &_edata;
if (src != dst) {
while (dst < end) * dst++ = * src++;
}
size_t count;
/* Pro Cortex-Mx bylo toto zbytečné, lze předpokládat, že je to tak i zde.
count = __preinit_array_end - __preinit_array_start;
for (unsigned i = 0; i < count; i++) __preinit_array_start[i]();
*/
count = __init_array_end - __init_array_start;
for (unsigned i = 0; i < count; i++) __init_array_start[i]();
}
// If you don't override a specific handler, it will just spin forever. // If you don't override a specific handler, it will just spin forever.
[[gnu::interrupt]] void DefaultIRQHandler( void ) { void DefaultIRQHandler( void ) {
// Infinite Loop // Infinite Loop
for (;;); for (;;);
} }
[[gnu::interrupt]] void NMI_RCC_CSS_IRQHandler( void ) { void NMI_RCC_CSS_IRQHandler( void ) {
RCC.INTR.B.CSSC = RESET; // clear the clock security int flag RCC.INTR.B.CSSC = RESET; // clear the clock security int flag
} }
#define ALIAS(f) __attribute__((nothrow,weak,alias(#f),used)) #define ALIAS(f) __attribute__((nothrow,weak,alias(#f),used))
@ -231,35 +255,10 @@ R"---(
: : [main]"r"(user_init)/*, [InterruptVector]"r"(InterruptVector)*/ : : [main]"r"(user_init)/*, [InterruptVector]"r"(InterruptVector)*/
: "t0", "memory" ); : "t0", "memory" );
} }
static void init_variables () {
uint32_t * dst, * end;
/* Zero fill the bss section */
dst = &_sbss;
end = &_ebss;
while (dst < end) * dst++ = 0U;
/* Copy data section from flash to RAM */
uint32_t * src;
src = &_data_lma;
dst = &_data_vma;
end = &_edata;
if (src != dst) {
while (dst < end) * dst++ = * src++;
}
}
static void init_constructors () {
/* Pro Cortex-Mx bylo toto zbytečné, lze předpokládat, že je to tak i zde.
count = __preinit_array_end - __preinit_array_start;
for (unsigned i = 0; i < count; i++) __preinit_array_start[i]();
*/
const size_t count = __init_array_end - __init_array_start;
for (unsigned i = 0; i < count; i++) __init_array_start[i]();
}
void user_init () { void user_init () {
init_variables();
SystemInit(); SystemInit();
SystemCoreClockUpdate(); SystemCoreClockUpdate();
init_constructors(); // Konstruktory zavolat až po inicializaci hodin __init_array();
main (); main ();
for (;;); for (;;);
} }

View file

@ -10,13 +10,14 @@ enum CLKSRC : uint32_t {
static constexpr unsigned HSI_VALUE = 8000000u; /* Value of the Internal oscillator in Hz */ static constexpr unsigned HSI_VALUE = 8000000u; /* Value of the Internal oscillator in Hz */
static constexpr unsigned HSE_VALUE = 8000000u; /* Value of the External oscillator in Hz */ static constexpr unsigned HSE_VALUE = 8000000u; /* Value of the External oscillator in Hz */
/* In the following line adjust the External High Speed oscillator (HSE) Startup Timeout value */ /* In the following line adjust the External High Speed oscillator (HSE) Startup Timeout value */
static constexpr unsigned SYSCLK_FREQ_96MHz_HSE = SYSTEM_CORE_CLOCK;
static constexpr unsigned HSE_STARTUP_TIMEOUT = 0x1000u; /* Time out for HSE start up */ static constexpr unsigned HSE_STARTUP_TIMEOUT = 0x1000u; /* Time out for HSE start up */
// HSE i HSI mají frekvenci 8 MHz // HSE i HSI mají frekvenci 8 MHz
static constexpr uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9}; static constexpr uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
uint32_t SystemCoreClock = SYSTEM_CORE_CLOCK; /* System Clock Frequency (Core Clock) */ uint32_t SystemCoreClock = SYSCLK_FREQ_96MHz_HSE; /* System Clock Frequency (Core Clock) */
static void SetSysClock_HSE(void) { static void SetSysClockTo96_HSE(void) {
__IO uint32_t StartUpCounter = 0, HSEStatus = 0; __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
RCC.CTLR.B.HSEON = SET; RCC.CTLR.B.HSEON = SET;
@ -44,7 +45,7 @@ static void SetSysClock_HSE(void) {
*/ */
r.B.PLLSRC = SET; r.B.PLLSRC = SET;
r.B.PLLXTPRE = RESET; r.B.PLLXTPRE = RESET;
r.B.PLLMUL = 15u; // or 10u for 96 MHz r.B.PLLMUL = 10u;
return r.R; return r.R;
}); });
/* Enable PLL */ /* Enable PLL */
@ -70,7 +71,7 @@ void SystemInit(void) {
RCC.CTLR.R &= 0xFFFBFFFFu; RCC.CTLR.R &= 0xFFFBFFFFu;
RCC.CFGR0.R &= 0xFF00FFFFu; RCC.CFGR0.R &= 0xFF00FFFFu;
RCC.INTR.R = 0x009F0000u; RCC.INTR.R = 0x009F0000u;
SetSysClock_HSE(); SetSysClockTo96_HSE();
} }
/********************************************************************* /*********************************************************************
* @fn SystemCoreClockUpdate * @fn SystemCoreClockUpdate
@ -80,7 +81,7 @@ void SystemInit(void) {
* @return none * @return none
*/ */
void SystemCoreClockUpdate (void) { void SystemCoreClockUpdate (void) {
uint32_t tmp = 0, pllmull = 0, pllsource = 0; uint32_t tmp = 0, pllmull = 0, pllsource = 0, Pll_6_5 = 0;
tmp = RCC.CFGR0.B.SWS; tmp = RCC.CFGR0.B.SWS;
@ -111,6 +112,9 @@ void SystemCoreClockUpdate (void) {
SystemCoreClock = HSE_VALUE * pllmull; SystemCoreClock = HSE_VALUE * pllmull;
} }
} }
if(Pll_6_5 == 1) SystemCoreClock = (SystemCoreClock / 2);
break; break;
default: default:
SystemCoreClock = HSI_VALUE; SystemCoreClock = HSI_VALUE;
@ -124,7 +128,6 @@ void SystemCoreClockUpdate (void) {
static uint32_t p_us = 0u; static uint32_t p_us = 0u;
static bool timeout; static bool timeout;
void delay_init () { void delay_init () {
// default clock is HCLK / 8
p_us = SystemCoreClock / 8000000; p_us = SystemCoreClock / 8000000;
} }
void delay_us (const unsigned dly) { void delay_us (const unsigned dly) {

View file

@ -79,7 +79,7 @@ struct SysTick_Type {
} }
}; };
static SysTick_Type & SysTick = * reinterpret_cast<SysTick_Type * const> (0xE000F000); static SysTick_Type & SysTick = * reinterpret_cast<SysTick_Type * const> (0xE000F000);
static constexpr unsigned SYSTEM_CORE_CLOCK = 144'000'000u; // or 96'000'000u static constexpr unsigned SYSTEM_CORE_CLOCK = 96'000'000u;
extern "C" { extern "C" {
extern uint32_t SystemCoreClock; extern uint32_t SystemCoreClock;
extern void SystemCoreClockUpdate (void); extern void SystemCoreClockUpdate (void);

View file

@ -69,7 +69,6 @@ uint32_t Usart::Down(const char * data, const uint32_t len) {
return n; return n;
} }
void Usart::SetBaud (const uint32_t _baud) const { void Usart::SetBaud (const uint32_t _baud) const {
if (_baud == 0u) return; // ! zero divide
const ONE_BIT b = USART1.CTLR1.B.UE; const ONE_BIT b = USART1.CTLR1.B.UE;
if (b == SET) USART1.CTLR1.B.UE = RESET; if (b == SET) USART1.CTLR1.B.UE = RESET;
const uint32_t HCLK = SystemCoreClock; // hodiny pro USART zde nejsou děleny const uint32_t HCLK = SystemCoreClock; // hodiny pro USART zde nejsou děleny