一个多星期都没有更新微博了,嗯,今天就分享一下stm32的函数段。
很多朋友对这个名字可能会比较陌生,函数段是什么,函数段是干嘛用的?不过做过linux驱动的同学都应该用过大名鼎鼎的 module_init() 这个驱动入口宏吧。我们把这个宏拿出来分析,不同的开发者之间为什么只需要宏定义一下这个函数,开机就会自动去调用这个函数呢?我们往下追踪看看
我们发现在/include/linux/init.h中
**
* module_init() - driver initialization entry point
* @x: function to be run at kernel boot time or module insertion
*
* module_init() will either be called during do_initcalls() (if
* builtin) or at module insertion time (if a module). There can only
* be one per module.
*/
#define module_init(x) __initcall(x);
/**
* module_exit() - driver exit entry point
* @x: function to be run when driver is removed
*
* module_exit() will wrap the driver clean-up code
* with cleanup_module() when used with rmmod when
* the driver is a module. If the driver is statically
* compiled into the kernel, module_exit() has no effect.
* There can only be one per module.
*/
#define module_exit(x) __exitcall(x);
继续往下看
typedef int (*initcall_t)(void);
#define __initcall(fn) device_initcall(fn)
#define device_initcall(fn) __define_initcall("6",fn,6)
/* initcalls are now grouped by functionality into separate
* subsections. Ordering inside the subsections is determined
* by link order.
* For backwards compatibility, initcall() puts the call in
* the device init subsection.
*
* The `id' arg to __define_initcall() is needed so that multiple initcalls
* can point at the same handler without causing duplicate-symbol build errors.
*/
#define __define_initcall(level,fn,id) \
static initcall_t __initcall_##fn##id __used \
__attribute__((__section_