IAR 实现类linux驱动模块框架module_init(init_fun)

其实在单片机上也能使用类linux驱动模块框架module_init(init_fun),从而给驱动管理提供了新的方式。

boot.icf文件


 
 
  1. /*###ICF### Section handled by ICF editor, don't touch! ****/
  2. /*-Editor annotation file-*/
  3. /* IcfEditorFile="$TOOLKIT_DIR$\config\ide\IcfEditor\cortex_v1_0.xml" */
  4. /*-Specials-*/
  5. define symbol __ICFEDIT_intvec_start__ = 0x08000000;
  6. /*-Memory Regions-*/
  7. define symbol __ICFEDIT_region_ROM_start__ = 0x08000000;
  8. define symbol __ICFEDIT_region_ROM_end__ = 0x0807FFFF;
  9. define symbol __ICFEDIT_region_RAM_start__ = 0x20000000;
  10. define symbol __ICFEDIT_region_RAM_end__ = 0x2001FFFF;
  11. /*-Sizes-*/
  12. define symbol __ICFEDIT_size_cstack__ = 0x8000;
  13. define symbol __ICFEDIT_size_heap__ = 0x0000;
  14. /**** End of ICF editor section. ###ICF###*/
  15. define memory mem with size = 4G;
  16. define region ROM_region = mem:[ from __ICFEDIT_region_ROM_start__ to __ICFEDIT_region_ROM_end__];
  17. define region RAM_region = mem:[ from __ICFEDIT_region_RAM_start__ to __ICFEDIT_region_RAM_end__];
  18. define block CSTACK with alignment = 8, size = __ICFEDIT_size_cstack__ { };
  19. define block HEAP with alignment = 8, size = __ICFEDIT_size_heap__ { };
  20. initialize by copy { readwrite };
  21. do not initialize { section .noinit };
  22. place at address mem:__ICFEDIT_intvec_start__ { readonly section .intvec };
  23. place in ROM_region { readonly, section mysection };
  24. place in RAM_region { readwrite,
  25. block CSTACK, block HEAP };
  26. export symbol __ICFEDIT_region_RAM_start__;
  27. export symbol __ICFEDIT_region_RAM_end__;

其实改icf文件很简单,就在 ROM区创建一个名为 “mysection ” 的段。

place in ROM_region   { readonly, section mysection };

main.c


 
 
  1. #include <stdio.h>
  2. #pragma section="mysection"
  3. #define MODULE_BEGIN (__segment_begin("mysection"))
  4. #define MODULE_END (__segment_end("mysection"))
  5. typedef void (*init_function_t)(void);
  6. #define _init __attribute__((used, section("mysection")))
  7. #define MODULE_INIT(func) init_function_t _fn_##func @ "mysection" = func // 方法一
  8. //#define MODULE_INIT(func) init_function_t _fn_##func _init = func // 方法二
  9. void drv_spi_init (void)
  10. {
  11. printf( "drv_spi_init...\n");
  12. }
  13. MODULE_INIT(drv_spi_init); //将函数放入 “mysection” 段
  14. void drv_uart_init (void)
  15. {
  16. printf( "drv_uart_init...\n");
  17. }
  18. MODULE_INIT(drv_uart_init); //将函数放入 “mysection” 段
  19. void drv_i2c_init (void)
  20. {
  21. printf( "drv_i2c_init...\n");
  22. }
  23. MODULE_INIT(drv_i2c_init); //将函数放入 “mysection” 段
  24. void do_initcall(void)
  25. {
  26. init_function_t *call = ( init_function_t *)MODULE_BEGIN;
  27. init_function_t *end = ( init_function_t *)MODULE_END;
  28. /* 逐个初始化驱动 */
  29. do
  30. {
  31. (*call)();
  32. call++;
  33. } while(call < end);
  34. }
  35. /*!
  36. \brief Boot main function
  37. \param[in] none
  38. \param[out] none
  39. \retval none
  40. */
  41. int main(void)
  42. {
  43. /* 驱动列表初始化 */
  44. do_initcall();
  45. /* user code */
  46. //...
  47. return 0;
  48. }

这样就可以在STM32等单片机上实现类linux风格的驱动管理了,同样的方法可以实现module_deinit。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值