BLE蓝牙开发空白模版

OSAL_SimpleBLEtemplate.c

/******************************************************************************                                
* 名    称:        OSAL_SimpleBLETemplate.c                   
* 功    能:        蓝牙开发空白模版            
* 作    者:        neha
* 版 本 号:       v1.0                                              
* 修改时间:        2017/12/20
******************************************************************************/  

/**************************************************************************************************
 *                                            INCLUDES
 **************************************************************************************************/
#include "hal_types.h"
#include "OSAL.h"
#include "OSAL_Tasks.h"

/* HAL */
#include "hal_drivers.h"

/* LL */
#include "ll.h"

/* HCI */
#include "hci_tl.h"

#if defined ( OSAL_CBTIMER_NUM_TASKS )
  #include "osal_cbTimer.h"
#endif

/* L2CAP */
#include "l2cap.h"

/* gap */
#include "gap.h"
#include "gapgattserver.h"
#include "gapbondmgr.h"

/* GATT */
#include "gatt.h"

#include "gattservapp.h"

/* Profiles */
#if defined ( PLUS_BROADCASTER )
  #include "peripheralBroadcaster.h"
#else
  #include "peripheral.h"
#endif

/* Application */
#include "simpleBLETemplate.h"

/*********************************************************************
 * GLOBAL VARIABLES
 */

// 定义了各个任务的事件执行函数,可以理解成回调函数,或事件执行函数
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
  SimpleBLETemplate_ProcessEvent                                    // task 11  这个是我们的应用程序的事件处理函数
};

const uint8 tasksCnt = sizeof( tasksArr ) / sizeof( tasksArr[0] );
uint16 *tasksEvents;

/*********************************************************************
 * FUNCTIONS
 *********************************************************************/

/*********************************************************************
 * @fn      osalInitTasks
 *
 * @brief   This function invokes the initialization function for each task.
 *
 * @param   void
 *
 * @return  none
 */
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 */
  SimpleBLETemplate_Init( taskID );     //这个就是我们的应用程序初始化
}

/*********************************************************************
*********************************************************************/

simpleBLETemplate.c

/******************************************************************************                                
* 名    称:        simpleBLETemplate.c                   
* 功    能:        蓝牙开发空白模版            
* 作    者:        neha
* 版 本 号:       v1.0                                              
* 修改时间:        2017/12/20
******************************************************************************/  

/*********************************************************************
 * 包含的头文件
 */

#include "bcomdef.h"
#include "OSAL.h"
#include "OSAL_PwrMgr.h"

#include "OnBoard.h"
#include "hal_adc.h"
#include "hal_led.h"
#include "hal_key.h"
#include "hal_lcd.h"
#include "simpleBLETemplate.h"
   
/*********************************************************************
 * 宏常量
 */
#define SBP_PERIODIC_EVT_PERIOD                   100 //毫秒

/*********************************************************************
 * 重命名
 */

/*********************************************************************
 * 外部全局变量
 */

/*********************************************************************
 * 外部功能函数
 */

/*********************************************************************
 * 本地全局变量
 */
static uint8 SimpleBLETemplate_TaskID;   

/*********************************************************************
 * 本地函数声明
 */

/*****************************************************************************
 * 服务回调函数注册
 */

/*********************************************************************
 * 本地函数实现
 */

/****************************************************************************
* 名    称: SimpleBLETemplate_Init()
* 功    能: 应用程序的初始化函数
* 入口参数: task_id 任务ID
* 出口参数: 无  
****************************************************************************/  
void SimpleBLETemplate_Init( uint8 task_id )
{ 
  SimpleBLETemplate_TaskID = task_id;       

  osal_set_event( SimpleBLETemplate_TaskID, SBP_START_DEVICE_EVT );
}

/****************************************************************************
* 名    称: SimpleBLETemplate_ProcessEvent()
* 功    能: 应用程序的事件处理函数
* 入口参数: task_id 任务ID  events 任务事件
* 出口参数: 未处理的任务  
****************************************************************************/ 
uint16 SimpleBLETemplate_ProcessEvent( uint8 task_id, uint16 events )
{

  VOID task_id; 

  if ( events & SYS_EVENT_MSG )
  {
    return (events ^ SYS_EVENT_MSG);
  }

  if ( events & SBP_START_DEVICE_EVT )
  {
    HalLedSet( (HAL_LED_1), HAL_LED_MODE_ON );
  
    return ( events ^ SBP_START_DEVICE_EVT );   
  }

  return 0;
}


/*********************************************************************
*********************************************************************/

simpleBLETemplate.h

/******************************************************************************                                
* 名    称:        simpleBLETemplate.c                   
* 功    能:        蓝牙开发空白模版            
* 作    者:        neha
* 版 本 号:       v1.0                                              
* 修改时间:        2017/12/20
******************************************************************************/  

#ifndef SIMPLEBLETEMPLATE_H
#define SIMPLEBLETEMPLATE_H

#ifdef __cplusplus
extern "C"
{
#endif

/*********************************************************************
 * 包含的头文件
 */

/*********************************************************************
 * 宏常量
 */

#define SBP_START_DEVICE_EVT                              0x0001
#define SBP_PERIODIC_EVT                                  0x0002

/*********************************************************************
 * 服务回调注册
 */

/*********************************************************************
 * 应用函数API
 */

extern void SimpleBLETemplate_Init( uint8 task_id );
extern uint16 SimpleBLETemplate_ProcessEvent( uint8 task_id, uint16 events );

/*********************************************************************
*********************************************************************/

#ifdef __cplusplus
}
#endif

#endif 

SimpleBLETemplate_Main.c

/******************************************************************************                                
* 名    称:        SimpleBLETemplate_Main.c                   
* 功    能:        蓝牙开发空白模版            
* 作    者:        neha
* 版 本 号:       v1.0                                              
* 修改时间:        2017/12/20
******************************************************************************/  

/**************************************************************************************************
 *                                           Includes
 **************************************************************************************************/
/* Hal Drivers */
#include "hal_types.h"
#include "hal_key.h"
#include "hal_timer.h"
#include "hal_drivers.h"
#include "hal_led.h"

/* OSAL */
#include "OSAL.h"
#include "OSAL_Tasks.h"
#include "OSAL_PwrMgr.h"
#include "osal_snv.h"
#include "OnBoard.h"

/**************************************************************************************************
 * FUNCTIONS
 **************************************************************************************************/

/* This callback is triggered when a key is pressed */
void MSA_Main_KeyCallback(uint8 keys, uint8 state);

/**************************************************************************************************
 * @fn          main
 *
 * @brief       Start of application.
 *
 * @param       none
 *
 * @return      none
 **************************************************************************************************
 */
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;
}

/**************************************************************************************************
                                           CALL-BACKS
**************************************************************************************************/



猜你喜欢

转载自blog.csdn.net/weixin_38491862/article/details/80090960