stm32 函数段使用

本文介绍了如何在STM32中实现类似Linux驱动的函数段机制,通过宏定义和编译器特性将初始化函数划分到特定段,实现驱动程序的模块化和独立性。内容包括函数指针定义、宏展开、链接器脚本配置,以及通过map文件验证。通过这种方式,可以简化主函数,提高代码复用和扩展性。
摘要由CSDN通过智能技术生成

一个多星期都没有更新微博了,嗯,今天就分享一下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_
  • 14
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值