Util_constructClock()

来源:互联网 发布:云计算企业投融资政策 编辑:程序博客网 时间:2024/06/06 02:15

以 SimpleBLEPeripheral 工程为例:

1. 首先要在初始化函数中创建一个软件定时 Util_constructClock()

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. static void SimpleBLEPeripheral_init(void)  
  2. {  
  3.     // ******************************************************************  
  4.   // N0 STACK API CALLS CAN OCCUR BEFORE THIS CALL TO ICall_registerApp  
  5.   // ******************************************************************  
  6.   // Register the current thread as an ICall dispatcher application  
  7.   // so that the application can send and receive messages.  
  8.   ICall_registerApp(&selfEntity, &sem);  
  9.   
  10.   // Hard code the BD Address till CC2650 board gets its own IEEE address  
  11.  // uint8 bdAddress[B_ADDR_LEN] = { 0xF0, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA };  
  12.  // HCI_EXT_SetBDADDRCmd(bdAddress);  
  13.   
  14.   // Set device's Sleep Clock Accuracy  
  15.   //HCI_EXT_SetSCACmd(40);  
  16.   
  17.   // Create an RTOS queue for message from profile to be sent to app.  
  18.   appMsgQueue = Util_constructQueue(&appMsg);  
  19.   
  20.   // Create one-shot clocks for internal periodic events.  
  21.   Util_constructClock(&periodicClock, SimpleBLEPeripheral_clockHandler,  
  22.                       SBP_PERIODIC_EVT_PERIOD, 0, false, SBP_PERIODIC_EVT);  //创建定时  
  23.   
  24.   Board_openLCD();  
  25.   
  26.   
  27.   // Setup the GAP  
  28.   GAP_SetParamValue(TGAP_CONN_PAUSE_PERIPHERAL, DEFAULT_CONN_PAUSE_PERIPHERAL);  

先看一下 Util_constructClock()中定义的回调函数 SimpleBLEPeripheral_clockHandler(),这个函数就是当定时时间到了之后的处理函数。

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. static void SimpleBLEPeripheral_clockHandler(UArg arg)  
  2. {  
  3.   // Store the event.  
  4.   events |= arg;  
  5.   
  6.   // Wake up the application.  
  7.   Semaphore_post(sem);  
  8. }  

2. 第二步就是要启动这个定时器了,原例程中的代码如下:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. static void SimpleBLEPeripheral_taskFxn(UArg a0, UArg a1)  
  2. {  
  3.   // Initialize application  
  4.   SimpleBLEPeripheral_init();  
  5.   
  6.   // Application main loop  
  7.   for (;;)  
  8.   {  
  9.       
  10.       //....省略  
  11.   
  12.     if (events & SBP_PERIODIC_EVT)  
  13.     {  
  14.       events &= ~SBP_PERIODIC_EVT;  
  15.   
  16.       Util_startClock(&periodicClock);  //开启定时  
  17.   
  18.       // Perform periodic application task  
  19.       SimpleBLEPeripheral_performPeriodicTask();  
  20.     }  
  21.    }  
  22. }  

注意这个 Util_startClock()函数是放在 for 循环里的,说明他是重复执行的,当一次定时结束后,要重新开启 Util_startClock,这个很像 CC2541 中的定时功能。所以如果你的定时功能出了问题,只能执行一次,那就说明你在第一次定时结束后没有重新启动定时。也就是说在定时功能执行一次之后,需要再次调用 Util_startClock()函数来开启定时功能。

0 0
原创粉丝点击