TI-BLE协议栈初探

//TI-BLE协议栈main函数
int main(void)
{
  /* Initialize hardware */
  HAL_BOARD_INIT();    //初始化时钟稳定时钟等等

  // Initialize board I/O
  InitBoard( OB_COLD );//冷启动,关闭了led灯与中断, 一边接下来的各种初始化不受干扰

  /* Initialze the HAL driver */
  HalDriverInit();     //各种驱动的初始化、如按键、lcd、adc、usb、uart等

  /* Initialize NV system */
  osal_snv_init();     //snv内部用于保存配对数据或你的用户自定义数据的一段flash,4kB空间

  /* Initialize LL */

  /* Initialize the operating system */
  osal_init_system();  //oasl操作系统初始化, 包含内存分配、消息队列、定时器、电源管理和任务等

  /* Enable interrupts */
  HAL_ENABLE_INTERRUPTS();      //开启全局中断

  // Final board initialization
  InitBoard( OB_READY );        //设置标志标示系统初始化完毕 

  #if defined ( POWER_SAVING )  //如果使能了低功耗,就启动低功耗模式
    osal_pwrmgr_device( PWRMGR_BATTERY );
  #endif

  /* Start OSAL */
  osal_start_system();          //osal 操作系统启动,实际上是一个大循环
   
  return 0;
}

1.HAL_BOARD_INIT()详解

/* Board Initialization */
#define HAL_BOARD_INIT()                                                   \
{                                                                          \
  /* Set to 16Mhz to set 32kHz OSC, then back to 32MHz */                  \
  START_HSOSC_XOSC();/*启动所有晶振并等待晶振稳定*/                          \
  SET_OSC_TO_HSOSC();/*使用16MHzRC振荡电路为主时钟并等待主时钟稳定*/          \
  SET_32KHZ_OSC();   /*使用32KHz片内或片外)晶振(为休眠时钟并等待休眠时钟稳定*/ \
  SET_OSC_TO_XOSC(); /*使用32MHz晶振为主时钟并等待时钟主稳定*/                \
  STOP_HSOSC();      /*关闭16MHzRC振荡电路*/                                \
                                                                           \
  /* Enable cache prefetch mode. */                                        \
  PREFETCH_ENABLE();                                                       \
}

2.InitBoard(OB_COLD) & InitBoard(OB_READY)详解

void InitBoard( uint8 level )
{
  if ( level == OB_COLD )
  {
    // Interrupts off               关闭所有中断 
    osal_int_disable( INTS_ALL );// ->HAL_DISABLE_INTERRUPTS()->st( EA = 0; )
    // Turn all LEDs off            关闭所有LED灯
    HalLedSet( HAL_LED_ALL, HAL_LED_MODE_OFF );
    // Check for Brown-Out reset
//    ChkReset();
  }
  else  // !OB_COLD
  {
    /* Initialize Key stuff */    //初始化按键(中断)
    OnboardKeyIntEnable = HAL_KEY_INTERRUPT_ENABLE;
    HalKeyConfig( OnboardKeyIntEnable, OnBoard_KeyCallback);
  }
}

3.osal_init_system()详解

uint8 osal_init_system( void )
{
  // Initialize the Memory Allocation System
  osal_mem_init();   //初始化内存分配系统

  // Initialize the message queue
  osal_qHead = NULL; //初始化消息队列

  // Initialize the timers
  osalTimerInit();   //初始化定时器

  // Initialize the Power Management System
  osal_pwrmgr_init();//初始化电源管理系统

  // Initialize the system tasks.
  osalInitTasks();   //初始化系统任务,这一个任务初始化非常关键

  // Setup efficient search for the first free block of heap.
  osal_mem_kick();   //设置高效搜索堆的第一个空闲块

  return ( SUCCESS );
}

4.osal_start_system()详解

void osal_start_system( void )
{
#if !defined ( ZBIT ) && !defined ( UBIT )
  for(;;)  // Forever Loop  无限循环
#endif
  {
    osal_run_system(); //系统启动运行
  }
}
void osal_run_system( void )
{
  uint8 idx = 0;

#ifndef HAL_BOARD_CC2538
  osalTimeUpdate();
#endif
  
  Hal_ProcessPoll();    //查询数据,比如串口数据,usb数据等

  do {                  //循环查询是否有事件,有事件的话,就立马退出,app应用的优先级最低
    if (tasksEvents[idx])  // Task is highest priority that is ready.
    {
      break;
    }
  } while (++idx < tasksCnt);

  if (idx < tasksCnt)    //找到了事件
  {
    uint16 events;
    halIntState_t intState;

    HAL_ENTER_CRITICAL_SECTION(intState);  //关闭中断
    events = tasksEvents[idx];             //读取该任务的事件(事件可能不止1个)
    tasksEvents[idx] = 0;                  //清除事件记录,在执行任务处理函数期间有可能发生新的事件
    HAL_EXIT_CRITICAL_SECTION(intState);   //开启中断

    activeTaskID = idx;
    events = (tasksArr[idx])( idx, events );// tasksArr是任务处理函数指针
    activeTaskID = TASK_NO_TASK;

    HAL_ENTER_CRITICAL_SECTION(intState);   //关闭中断
    tasksEvents[idx] |= events;  // Add back unprocessed events to the current task.
    HAL_EXIT_CRITICAL_SECTION(intState);    //开启中断
  }
#if defined( POWER_SAVING )
  else  // Complete pass through all task events with no activity?
  {                               //系统睡眠,以便达到低功耗的目的
    osal_pwrmgr_powerconserve();  // Put the processor/system into sleep
  }
#endif

  /* Yield in case cooperative scheduling is being used. */
#if defined (configUSE_PREEMPTION) && (configUSE_PREEMPTION == 0)
  {
    osal_task_yield();
  }
#endif
}
/ 这个表相当重要, 都是定义了各个任务的事件执行函数,可以理解成回调函数,或事件执行函数
const pTaskEventHandlerFn tasksArr[] =
{
  LL_ProcessEvent,                                                  // task 0
  Hal_ProcessEvent,                                                 // task 1
  HCI_ProcessEvent,                                                 // task 2
#if defined ( OSAL_CBTIMER_NUM_TASKS )
  OSAL_CBTIMER_PROCESS_EVENT( osal_CbTimerProcessEvent ),           // task 3
#endif
  L2CAP_ProcessEvent,                                               // task 4
  GAP_ProcessEvent,                                                 // task 5
  GATT_ProcessEvent,                                                // task 6
  SM_ProcessEvent,                                                  // task 7
  GAPRole_ProcessEvent,                                             // task 8
  GAPBondMgr_ProcessEvent,                                          // task 9
  GATTServApp_ProcessEvent,                                         // task 10
  SimpleBLETest_ProcessEvent                                        // task 11  这个是我们的应用程序的事件处理函数
};

const uint8 tasksCnt = sizeof( tasksArr ) / sizeof( tasksArr[0] );
uint16 *tasksEvents;
void osalInitTasks( void )
{
  uint8 taskID = 0;
  // 分配任务事件空间,这里采用动态的方法来做,比较方便,在tasksArr而代码修改少
  tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt);
  osal_memset( tasksEvents, 0, (sizeof( uint16 ) * tasksCnt));

  /* LL Task */
  LL_Init( taskID++ );

  /* Hal Task */
  Hal_Init( taskID++ );

  /* HCI Task */
  HCI_Init( taskID++ );

#if defined ( OSAL_CBTIMER_NUM_TASKS )
  /* Callback Timer Tasks */
  osal_CbTimerInit( taskID );
  taskID += OSAL_CBTIMER_NUM_TASKS;
#endif

  /* L2CAP Task */
  L2CAP_Init( taskID++ );

  /* GAP Task */
  GAP_Init( taskID++ );

  /* GATT Task */
  GATT_Init( taskID++ );

  /* SM Task */
  SM_Init( taskID++ );

  /* Profiles */
  GAPRole_Init( taskID++ );        //角色初始化
  GAPBondMgr_Init( taskID++ );

  GATTServApp_Init( taskID++ );

  /* Application */
  SimpleBLETest_Init( taskID );    //应用程序初始化
}





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值