Browse Source

add driver/init.c

Signed-off-by: surenyi <surenyi82@163.com>
master
surenyi 6 months ago
parent
commit
102128644d
  1. 3
      CMakeLists.txt
  2. 56
      driver/init.c
  3. 74
      driver/start.c

3
CMakeLists.txt

@ -81,6 +81,7 @@ target_sources(${tgt_name} PRIVATE
driver/libc/strchr.c
driver/libc/atoi.c
driver/libc/eabi_compat.c
driver/init.c
driver/vectors.c
driver/serial.c
driver/gpio.c
@ -202,6 +203,7 @@ add_executable(boot
driver/libc/strchr.c
driver/libc/crt0.S
driver/libc/sections.c
driver/init.c
driver/helpers.c
driver/vectors.c
driver/serial.c
@ -211,7 +213,6 @@ add_executable(boot
driver/flash_if.c
driver/ymodem.c
driver/common.c
driver/start.c
)
target_include_directories(boot PRIVATE

56
driver/init.c

@ -0,0 +1,56 @@
#include <stddef.h>
/* {{{ __libc_init_array */
/* These magic symbols are provided by the linker. */
/* clang-format off */
extern void (*__preinit_array_start[])(void)__attribute__((weak));
extern void (*__preinit_array_end [])(void)__attribute__((weak));
extern void (*__init_array_start [])(void)__attribute__((weak));
extern void (*__init_array_end [])(void)__attribute__((weak));
/* clang-format on */
static void __empty(void)
{
}
/* clang-format off */
void _init (void) __attribute__((weak, alias("__empty")));
/* clang-format on */
/* iterate over all the init routines. */
void __libc_init_array(void)
{
size_t count;
size_t i;
count = __preinit_array_end - __preinit_array_start;
for (i = 0; i < count; i++)
__preinit_array_start[i]();
_init();
#if !defined(__RTTHREAD__)
count = __init_array_end - __init_array_start;
for (i = 0; i < count; i++)
__init_array_start[i]();
#endif
}
/* }}} */
/* {{{ __libc_fini_array */
extern void (*__fini_array_start[])(void) __attribute__((weak));
extern void (*__fini_array_end[])(void) __attribute__((weak));
void _fini(void) __attribute__((weak, alias("__empty")));
/* Run all the cleanup routines. */
void __libc_fini_array(void)
{
size_t count;
size_t i;
count = __fini_array_end - __fini_array_start;
for (i = count; i > 0; i--)
__fini_array_start[i - 1]();
_fini();
}
/* }}} */

74
driver/start.c

@ -4,69 +4,24 @@
#include <stddef.h>
#include "config.h"
/* {{{ __libc_init_array */
/* These magic symbols are provided by the linker. */
/* clang-format off */
extern void (*__preinit_array_start[])(void)__attribute__((weak));
extern void (*__preinit_array_end [])(void)__attribute__((weak));
extern void (*__init_array_start [])(void)__attribute__((weak));
extern void (*__init_array_end [])(void)__attribute__((weak));
extern int main (void);
extern void SystemInit(void);
extern void SystemCoreClockUpdate(void);
extern void __libc_init_array(void);
extern void __libc_fini_array(void);
extern const void *const g_pfnVectors[];
/* clang-format on */
static void __empty(void)
{
}
static int __start(void)
static int __main(void)
{
return main();
}
/* clang-format off */
void _init (void) __attribute__((weak, alias("__empty")));
int entry (void) __attribute__((weak, alias("__start")));
int entry (void) __attribute__((weak, alias("__main")));
/* clang-format on */
/* iterate over all the init routines. */
void __libc_init_array (void)
{
size_t count;
size_t i;
count = __preinit_array_end - __preinit_array_start;
for (i = 0; i < count; i++)
__preinit_array_start[i] ();
_init ();
#if !defined(__RTTHREAD__)
count = __init_array_end - __init_array_start;
for (i = 0; i < count; i++)
__init_array_start[i] ();
#endif
}
/* }}} */
/* {{{ __libc_fini_array */
extern void (*__fini_array_start []) (void) __attribute__((weak));
extern void (*__fini_array_end []) (void) __attribute__((weak));
void _fini (void) __attribute__((weak, alias("__empty")));
/* Run all the cleanup routines. */
void __libc_fini_array (void)
{
size_t count;
size_t i;
count = __fini_array_end - __fini_array_start;
for (i = count; i > 0; i--)
__fini_array_start[i-1] ();
_fini ();
}
/* }}} */
/* {{{ Reset_Handler */
extern unsigned int _estack;
extern unsigned int _sidata;
@ -75,12 +30,12 @@ extern unsigned int _edata;
extern unsigned int _sbss;
extern unsigned int _ebss;
static void do_cpu_boot()
static void _start()
{
unsigned int *dstp, *srcp;
SystemCoreClockUpdate();
srcp = &_sidata;
dstp = &_sdata;
@ -103,22 +58,19 @@ static void do_cpu_boot()
__libc_fini_array();
}
void __attribute__((naked, section(".text.Reset_Handler")))
Reset_Handler(void)
void __attribute__((naked, section(".text.Reset_Handler"))) Reset_Handler(void)
{
#define VCTOR 0xE000ED08
SystemInit();
#if defined(BOARD_SYS_BASE)
#define VCTOR 0xE000ED08
*(volatile unsigned int *)(VCTOR) = BOARD_SYS_BASE;
#undef VCTOR
#endif
/* fix the vector */
*(volatile unsigned int *)(VCTOR) = (unsigned int)g_pfnVectors;
do_cpu_boot();
_start();
while (1) {
__asm volatile("wfe");
}
#undef VCTOR
}
/* }}} */

Loading…
Cancel
Save