《蓝牙4.0实战演练》笔记
一. Peripheral流程
osal_init_system
-->osalInitTasks
--> SimpleBLEPeripheral_Init( taskID );
{
//1. 对gap与gatt进行配置
注意: 这儿区分工程CC2541DK-MINI keyfob 与CC2541,
只是工程的配置选项不一样(option-->c/c++ compile-->prepocess),代码是一样的,
CC2541DK-MINI keyfob定义了CC2540_MINIDK,需要按键才能进入广播状态
CC2541中没有定义CC2540_MINIDK,开机进入广播状态
//2. profile
SimpleProfile_AddService( GATT_ALL_SERVICES ); //添加profile
VOID SimpleProfile_RegisterAppCBs( &simpleBLEPeripheral_SimpleProfileCBs );
//3. 设置事件SBP_START_DEVICE_EVT
osal_set_event( simpleBLETaskId, SBP_START_DEVICE_EVT );
}
osal_start_system
--> 死循环调用osal_run_system
--> SimpleBLEPeripheral_ProcessEvent //处理所有的事件
SimpleBLEPeripheral_ProcessEvent //进行按键处理
{
SYS_EVENT_MSG --> 这个不关心
SBP_START_DEVICE_EVT
-->处理在初始化时osal_set_event设置的事件
-->注册回调:peripheralStateNotificationCB profile状态改变回调
-->osal_start_timerEx产生一个时间事件SBP_PERIODIC_EVT
SBP_PERIODIC_EVT
-->第一次进入是SBP_START_DEVICE_EVT的最后产生的时间事件
-->同时自己osal_start_timerEx产生一个时间事件SBP_PERIODIC_EVT,这就是一个循环
--> performPeriodicTask(); 可以认为是一个时间处理函数
{
获取CHAR3的值,并赋给CHAR4
}
}
二.Peripheral代码流程
SimpleBLEPeripheral_Main的执行流程
1. APP--> SimpleBLEPeripheral_Main.c找到main函数
-
int main(void)
-
{
-
HAL_BOARD_INIT();
-
InitBoard( OB_COLD );
-
HalDriverInit();
-
osal_snv_init();
-
osal_init_system(); //1.1 初始化操作系统
-
HAL_ENABLE_INTERRUPTS();
-
InitBoard( OB_READY ); //1.2 初始化按键
-
#if defined ( POWER_SAVING )
-
osal_pwrmgr_device( PWRMGR_BATTERY );
-
#endif
-
osal_start_system(); //1.3
-
return 0;
-
}
OSAL-->OSAL.c中
-
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 );
-
}
APP--> OSAL_SimpleBLEPeripheral.c中又回到APP了
-
void osalInitTasks( void )
-
{
-
uint8 taskID = 0;
-
-
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 */
-
SimpleBLEPeripheral_Init( taskID ); //用户的初始化函数
-
}
1.3
OSAL-->OSAL.c中
-
void osal_start_system( void )
-
{
-
for(;;) //死循环
-
{
-
osal_run_system();
-
}
-
}
1.3.1
OSAL-->OSAL.c中
-
void osal_run_system( void )
-
{
-
uint8 idx = 0;
-
-
#ifndef HAL_BOARD_CC2538
-
osalTimeUpdate();
-
#endif
-
-
Hal_ProcessPoll();
-
-
do {
-
if (tasksEvents[idx]) //osalInitTasks中说id越小,等级越高
-
{
-
break;
-
}
-
} while (++idx < tasksCnt);
-
-
if (idx < tasksCnt)
-
{
-
uint16 events;
-
halIntState_t intState;
-
-
//清除任务标志
-
HAL_ENTER_CRITICAL_SECTION(intState);
-
events = tasksEvents[idx];
-
tasksEvents[idx] = 0; // Clear the Events for this task.
-
HAL_EXIT_CRITICAL_SECTION(intState);
-
-
//开始执行具体的任务
-
activeTaskID = idx;
-
events = (tasksArr[idx])( idx, events ); //任务处理
-
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
-
}
两个结构体:
taskEvents : 数组管理任务,每一项都代表一个任务,这一项不为0时代表有任务发生,为0代表没有任务,
taskArr : 具体的任务,函数指针数组,与taskEvents有同样多的项,这样
taskEvents中的每一项就可以代表一个任务
APP->simpleBLEPeripheral.c
-
void SimpleBLEPeripheral_Init( uint8 task_id )
-
{
-
simpleBLEPeripheral_TaskID = task_id;
-
VOID GAP_SetParamValue( TGAP_CONN_PAUSE_PERIPHERAL, DEFAULT_CONN_PAUSE_PERIPHERAL );
-
{ //GAP设置
-
GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &initial_advertising_enable );
-
GAPRole_SetParameter( GAPROLE_ADVERT_OFF_TIME, sizeof( uint16 ), &gapRole_AdvertOffTime );
-
-
GAPRole_SetParameter( GAPROLE_SCAN_RSP_DATA, sizeof ( scanRspData ), scanRspData );
-
GAPRole_SetParameter( GAPROLE_ADVERT_DATA, sizeof( advertData ), advertData );
-
-
GAPRole_SetParameter( GAPROLE_PARAM_UPDATE_ENABLE, sizeof( uint8 ), &enable_update_request );
-
GAPRole_SetParameter( GAPROLE_MIN_CONN_INTERVAL, sizeof( uint16 ), &desired_min_interval );
-
GAPRole_SetParameter( GAPROLE_MAX_CONN_INTERVAL, sizeof( uint16 ), &desired_max_interval );
-
GAPRole_SetParameter( GAPROLE_SLAVE_LATENCY, sizeof( uint16 ), &desired_slave_latency );
-
GAPRole_SetParameter( GAPROLE_TIMEOUT_MULTIPLIER, sizeof( uint16 ), &desired_conn_timeout );
-
}
-
GGS_SetParameter( GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, attDeviceName );
-
{ //ADV设置
-
uint16 advInt = DEFAULT_ADVERTISING_INTERVAL;
-
GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MIN, advInt );
-
GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MAX, advInt );
-
GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, advInt );
-
GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, advInt );
-
}
-
{ //GAP与BOND设置
-
uint32 passkey = 0;
-
uint8 pairMode = GAPBOND_PAIRING_MODE_WAIT_FOR_REQ;
-
uint8 mitm = TRUE;
-
uint8 ioCap = GAPBOND_IO_CAP_DISPLAY_ONLY;
-
uint8 bonding = TRUE;
-
GAPBondMgr_SetParameter( GAPBOND_DEFAULT_PASSCODE, sizeof ( uint32 ), &passkey );
-
GAPBondMgr_SetParameter( GAPBOND_PAIRING_MODE, sizeof ( uint8 ), &pairMode );
-
GAPBondMgr_SetParameter( GAPBOND_MITM_PROTECTION, sizeof ( uint8 ), &mitm );
-
GAPBondMgr_SetParameter( GAPBOND_IO_CAPABILITIES, sizeof ( uint8 ), &ioCap );
-
GAPBondMgr_SetParameter( GAPBOND_BONDING_ENABLED, sizeof ( uint8 ), &bonding );
-
}
-
-
GGS_AddService( GATT_ALL_SERVICES );
-
GATTServApp_AddService( GATT_ALL_SERVICES );
-
DevInfo_AddService();
-
SimpleProfile_AddService( GATT_ALL_SERVICES ); //添加simpleProfile
-
{ //设置simpleProfile属性
-
uint8 charValue1 = 1;
-
uint8 charValue2 = 2;
-
uint8 charValue3 = 3;
-
uint8 charValue4 = 4;
-
uint8 charValue5[SIMPLEPROFILE_CHAR5_LEN] = { 1, 2, 3, 4, 5 };
-
SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR1, sizeof ( uint8 ), &charValue1 );
-
SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR2, sizeof ( uint8 ), &charValue2 );
-
SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR3, sizeof ( uint8 ), &charValue3 );
-
SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof ( uint8 ), &charValue4 );
-
SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR5, SIMPLEPROFILE_CHAR5_LEN, charValue5 );
-
}
-
//注册simpleProfile回调
-
VOID SimpleProfile_RegisterAppCBs( &simpleBLEPeripheral_SimpleProfileCBs );
-
-
osal_set_event( simpleBLEPeripheral_TaskID, SBP_START_DEVICE_EVT );
-
}
Profile--> simpleGATTprofile.c
阅读(11137) | 评论(0) | 转发(0) |