以 SimpleBLEPeripheral 工程为例:
1. 首先要在初始化函数中创建一个软件定时 Util_constructClock()
- static void SimpleBLEPeripheral_init(void)
- {
- // ******************************************************************
- // N0 STACK API CALLS CAN OCCUR BEFORE THIS CALL TO ICall_registerApp
- // ******************************************************************
- // Register the current thread as an ICall dispatcher application
- // so that the application can send and receive messages.
- ICall_registerApp(&selfEntity, &sem);
-
- // Hard code the BD Address till CC2650 board gets its own IEEE address
- // uint8 bdAddress[B_ADDR_LEN] = { 0xF0, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA };
- // HCI_EXT_SetBDADDRCmd(bdAddress);
-
- // Set device's Sleep Clock Accuracy
- //HCI_EXT_SetSCACmd(40);
-
- // Create an RTOS queue for message from profile to be sent to app.
- appMsgQueue = Util_constructQueue(&appMsg);
-
- // Create one-shot clocks for internal periodic events.
- Util_constructClock(&periodicClock, SimpleBLEPeripheral_clockHandler,
- SBP_PERIODIC_EVT_PERIOD, 0, false, SBP_PERIODIC_EVT); //创建定时
-
- Board_openLCD();
-
-
- // Setup the GAP
- GAP_SetParamValue(TGAP_CONN_PAUSE_PERIPHERAL, DEFAULT_CONN_PAUSE_PERIPHERAL);
先看一下 Util_constructClock()中定义的回调函数 SimpleBLEPeripheral_clockHandler(),这个函数就是当定时时间到了之后的处理函数。
- static void SimpleBLEPeripheral_clockHandler(UArg arg)
- {
- // Store the event.
- events |= arg;
-
- // Wake up the application.
- Semaphore_post(sem);
- }
2. 第二步就是要启动这个定时器了,原例程中的代码如下:
- static void SimpleBLEPeripheral_taskFxn(UArg a0, UArg a1)
- {
- // Initialize application
- SimpleBLEPeripheral_init();
-
- // Application main loop
- for (;;)
- {
-
- //....省略
-
- if (events & SBP_PERIODIC_EVT)
- {
- events &= ~SBP_PERIODIC_EVT;
-
- Util_startClock(&periodicClock); //开启定时
-
- // Perform periodic application task
- SimpleBLEPeripheral_performPeriodicTask();
- }
- }
- }
注意这个 Util_startClock()函数是放在 for 循环里的,说明他是重复执行的,当一次定时结束后,要重新开启 Util_startClock,这个很像 CC2541 中的定时功能。所以如果你的定时功能出了问题,只能执行一次,那就说明你在第一次定时结束后没有重新启动定时。也就是说在定时功能执行一次之后,需要再次调用 Util_startClock()函数来开启定时功能。