SECTION介绍

b_section.h定义段的操作。现有的段有如下几个:

bSECTION_DEF_FLASH(bos_polling, pbPoling_t);          
#define BOS_REG_POLLING_FUNC(func) //将func放入bos_polling段

#define bDRIVER_REG_INIT_0(drv_num, init_f)  //将init_f放入driver_init_0段
#define bDRIVER_REG_INIT(drv_num, init_f)    //将init_f放入driver_init段

bSECTION_DEF_FLASH(b_mod_shell, static_cmd_st);
#define bSHELL_REG_INSTANCE(cmd_name, cmd_handler)  //将cmd信息放入b_mod_shell段

bSECTION_DEF_FLASH(b_mod_param, bParamInstance_t);
#define bPARAM_REG_INSTANCE(param, param_size)   //将参数信息放入b_mod_param段

bSECTION_DEF_FLASH(b_mod_state, bStateInfo_t *);
#define bSTATE_REG_INSTANCE(state_info)         //将状态信息放入b_mod_state段  

驱动文件最后会有一行这样的代码:bDRIVER_REG_INIT(B_DRIVER_XXX, bXXX_Init);将初始化函数放入driver_init段。

//设备初始化时,将遍历driver_init_0和driver_init内的函数,并执行。
int bDeviceInit()
{
    uint32_t i = 0, j = 0;
    memset(bDriverInterfaceTable, 0, sizeof(bDriverInterfaceTable));
    bSECTION_FOR_EACH(driver_init_0, bDriverRegInit_t, pdriver_init_0)
    {
        j = 0;
        for (i = 0; i < B_REG_DEV_NUMBER; i++)
        {
            if (bDriverNumberTable[i] == pdriver_init_0->drv_number)
            {
                bDriverInterfaceTable[i].drv_no = j++;
                bDriverInterfaceTable[i].pdes   = bDeviceDescTable[i];
                bDriverInterfaceTable[i].status = pdriver_init_0->init(&bDriverInterfaceTable[i]);
            }
        }
    }
    bSECTION_FOR_EACH(driver_init, bDriverRegInit_t, pdriver_init)
    {
        j = 0;
        for (i = 0; i < B_REG_DEV_NUMBER; i++)
        {
            if (bDriverNumberTable[i] == pdriver_init->drv_number)
            {
                bDriverInterfaceTable[i].drv_no = j++;
                bDriverInterfaceTable[i].pdes   = bDeviceDescTable[i];
                bDriverInterfaceTable[i].status = pdriver_init->init(&bDriverInterfaceTable[i]);
            }
        }
    }
    return 0;
}
int bExec()
{
    //BabyOS的执行函数遍历需要轮询的函数即在bos_polling段的函数。
    bSECTION_FOR_EACH(bos_polling, pbPoling_t, polling)
    {
        (*polling)();
    }
    return 0;
}

当使用gcc编译时,需要编辑链接文件,在链接文件中补充这几个段,例如:

/* Define output sections */
SECTIONS
{
  ......
  /* BabyOS Section -------------*/
  .driver_init :
  {
    . = ALIGN(4);
    PROVIDE(__start_driver_init = .);
    KEEP(*(SORT(.driver_init*)))
    PROVIDE(__stop_driver_init = .);
    . = ALIGN(4);
  } > FLASH

  .driver_init_0 :
  {
    . = ALIGN(4);
    PROVIDE(__start_driver_init_0 = .);
    KEEP(*(SORT(.driver_init_0*)))
    PROVIDE(__stop_driver_init_0 = .);
    . = ALIGN(4);
  } > FLASH

  .bos_polling :
  {
    . = ALIGN(4);
    PROVIDE(__start_bos_polling = .);
    KEEP(*(SORT(.bos_polling*)))
    PROVIDE(__stop_bos_polling = .);
    . = ALIGN(4);
  } > FLASH

  .b_mod_shell :
  {
    . = ALIGN(4);
    PROVIDE(__start_b_mod_shell = .);
    KEEP(*(SORT(.b_mod_shell*)))
    PROVIDE(__stop_b_mod_shell = .);
    . = ALIGN(4);
  } > FLASH

  .b_mod_state :
  {
    . = ALIGN(4);
    PROVIDE(__start_b_mod_state = .);
    KEEP(*(SORT(.b_mod_state*)))
    PROVIDE(__stop_b_mod_state = .);
    . = ALIGN(4);
  } > FLASH      
      
  /* BabyOS Section ---------end----*/
  ......
}