一、入口函数main
int main()
{
#if defined( USE_FPGA )
HWREG(PRCM_BASE + PRCM_O_PDCTL0) &= ~PRCM_PDCTL0_RFC_ON;
HWREG(PRCM_BASE + PRCM_O_PDCTL1) &= ~PRCM_PDCTL1_RFC_ON;
#endif // USE_FPGA
/* Register Application callback to trap asserts raised in the Stack */
//注册断言回调函数,断言是处理异常程序的方法之一
RegisterAssertCback(AssertHandler);
//初始化硬件GPIO口
PIN_init(BoardGpioInitTable);
#ifdef CC1350_LAUNCHXL
// Enable 2.4GHz Radio
radCtrlHandle = PIN_open(&radCtrlState, radCtrlCfg);
#ifdef POWER_SAVING
Power_registerNotify(&rFSwitchPowerNotifyObj,
PowerCC26XX_ENTERING_STANDBY | PowerCC26XX_AWAKE_STANDBY,
(Power_NotifyFxn) rFSwitchNotifyCb, NULL);
#endif //POWER_SAVING
#endif //CC1350_LAUNCHXL
#if defined( USE_FPGA )
// set RFC mode to support BLE
// Note: This must be done before the RF Core is released from reset!
SET_RFC_BLE_MODE(RFC_MODE_BLE);
#endif // USE_FPGA
// Enable iCache prefetching
VIMSConfigure(VIMS_BASE, TRUE, TRUE);
// Enable cache
VIMSModeSet(VIMS_BASE, VIMS_MODE_ENABLED);
#if !defined( POWER_SAVING ) || defined( USE_FPGA )
/* Set constraints for Standby, powerdown and idle mode */
// PowerCC26XX_SB_DISALLOW may be redundant
Power_setConstraint(PowerCC26XX_SB_DISALLOW);
Power_setConstraint(PowerCC26XX_IDLE_PD_DISALLOW);
#endif // POWER_SAVING | USE_FPGA
/* Initialize ICall module */
//初始化ICall软件模块,用于app和stack之间的通信
ICall_init();
/* Start tasks of external images - Priority 5 */
//启动远程任务,对应的ICall的入口点,包含stack任务
ICall_createRemoteTasks();
/* Kick off profile - Priority 3 */
//GAPRole任务初始化
GAPRole_createTask();
//app任务初始化
SimpleBLEPeripheral_createTask();
/* enable interrupts and start SYS/BIOS */
//启动SYS/BIOS,永不反回
BIOS_start();
//防止编译器警告用,代码无法执行到这里
return 0;
}
二、多任务的创建
main函数执行时需要穿件多个任务,包括协议栈任务,GAPRole任务,app任务,以及用户自定义的其他任务。
stack子工程是作为一个任务,在App工程中创建,对应mian函数中的代码如下:
/* Start tasks of external images - Priority 5 */
//启动远程任务,对应的ICall的入口点,包含stack任务
ICall_createRemoteTasks();
ICall_createRemoteTasks函数来自于icall.c文件
/* See header file for comments */
//启动远程任务,对应的ICall的入口点,包含stack任务
void ICall_createRemoteTasks(void)
{
size_t i;
UInt keytask;
/* Cheap locking mechanism to lock tasks
* which may attempt to access the service call dispatcher
* till all services are registered.
*/
keytask = Task_disable();
//ICall具有多个入口点
for (i = 0; i < ICALL_REMOTE_THREAD_COUNT; i++)
{
Task_Params params;
Task_Handle task;
ICall_CSState key;
//初始化任务参数
Task_Params_init(¶ms);
params.priority = ICall_threadPriorities[i];
params.stackSize = ICall_threadStackSizes[i];
params.arg0 = (UArg) icall_threadEntries[i];
params.arg1 = (UArg) ICall_getInitParams(i);
//创建任务
task = Task_create(ICall_taskEntry, ¶ms, NULL);
if (task == NULL)
{
/* abort */
ICALL_HOOK_ABORT_FUNC();
}
key = ICall_enterCSImpl();
if (ICall_newTask(task) == NULL)
{
/* abort */
ICALL_HOOK_ABORT_FUNC();
}
ICall_leaveCSImpl(key);
}
Task_restore(keytask);
}
GAPRole管理蓝牙设备角色等事务,GAPRole任务创建函数在peripheral.c或central.c等角色文件
//GAPRole管理蓝牙设备角色等事务
void GAPRole_createTask(void)
{
Task_Params taskParams;
// Configure task
Task_Params_init(&taskParams);
//设置任务堆栈缓冲区
taskParams.stack = gapRoleTaskStack;
//任务堆栈大小
taskParams.stackSize = GAPROLE_TASK_STACK_SIZE;
//任务优先级
taskParams.priority = GAPROLE_TASK_PRIORITY;
//构建任务,注意并未使用Task_create
Task_construct(&gapRoleTask, gapRole_taskFxn, &taskParams, NULL);
}
最后创建的任务是app,就是SimpleBLEPeripheral Task
void SimpleBLEPeripheral_createTask(void)
{
Task_Params taskParams;
// Configure task
Task_Params_init(&taskParams);
taskParams.stack = sbpTaskStack;
taskParams.stackSize = SBP_TASK_STACK_SIZE;
taskParams.priority = SBP_TASK_PRIORITY;
//SimpleBLEPeripheral_taskFxn是任务回调函数
Task_construct(&sbpTask, SimpleBLEPeripheral_taskFxn, &taskParams, NULL);
}
三、app任务初始化
任务创建/构建并启动SYS/BIOS内核调度程序后,app任务准备就绪运行:SimpleBLEPeripheral_taskFxn,此函数必须首先调用app的任务初始化函数。
//app任务初始化函数
SimpleBLEPeripheral_init();
// Application main loop
//app应用大循环
for (;;)
{
....
}
这个初始化函数simple_peripheral_init()为任务配置了几个服务,并设置了几个硬件和软件参数。代码如下:
App任务初始化函数一般需要做下列几件事情。
(1)ICall_registerApp注册,必须首先调用
(2)设置GAP层,例如广播间隔等参数
(3)设置GAPRole,例如是否开启广播等、连接参数等
(4)设置绑定管理器,例如是否开启绑定,以及绑定模式
(5)添加profile,并注册profile回调函数
(6)启动设备
(7)其他硬件配置
未完待续....