Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2584886
  • 博文数量: 877
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5920
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-05 12:25
个人简介

技术的乐趣在于分享,欢迎多多交流,多多沟通。

文章分类

全部博文(877)

文章存档

2021年(2)

2016年(20)

2015年(471)

2014年(358)

2013年(26)

分类: 嵌入式

2015-01-30 12:58:37


http://blog.csdn.net/zhjr1220/article/details/9416763

最近一段时间由于项目需要,便开始在阅读TI的cc2541的BLE Stack源码,对于蓝牙4.0这块知识基本是小白,所以几乎从0开始。在没接触蓝牙之前,就知道该部分的内容较为复杂(涉及到通信等协议栈),所以本着只有会使用为目的开始了BLE的学习(哎,一直都站在别人的肩膀上),学到点新的知识就积累一点,以免忘记。

 

看CC2541的开源BLE,一开始还以为51单片机结构会相当的简单,未曾想过TI的大牛们进行了软件架构的封装(OSAL+HAL,一种比较类似于但非操作系统的架构)。的确,对于我们这些小鸟来说,这样的设计对于我们可能很复杂,但是对于TI的开发人员来说却带来了更多的方便,移植简单化,改点配置就又用到别的处理器上。这都有点类似于Linux了,这套框架我还是比较佩服的,下面和大家分享一些OSAL+HAL在BLE中的使用,和大家解析一些函数。

 

由于这部分内容,网上也可以看到很多,本人也学习很多,喜欢开源的世界,所以就和大家分享自己深入看过的内容。

1.OSAL和HAL的好处

HAL:硬件抽象层,典型的操作系统硬件封装架构,远点的类似于Linux中常见的BSP,Android中的HAL等,都可以见到。相比于之前我所记得的51编程,在HAL里面各种宏定义,将所有可以操作的寄存器都进行封装,连中断都不放过,让搞裸驱的突然会有点不适应。

OSAL:操作系统抽象层,在cc2541的世界里,OSAL主要用于应用程序的开发,API大行其道,看看API手册就能完成编码。

整套类操作系统围绕着:Event任务,Task事件,Message消息来进行。事件发生,由该事件对应的任务来处理,任务有时会将消息做为另一个事件从而去触发别的任务进行消息的处理。

2.OSAL中的时间API函数

因为OSAL的资源比较多,这里和大家讲讲自己看到时间相关的API,主要是osal_run_system中的osalTimeUpdate()函数,该函数会做2件主要的事情:更新系统运行的时间和更新各个软件定时器。

  1. void osalTimeUpdate( void )//得到消逝时间的整数和小数,小数以余数形式存在  
  2. {  
  3.   uint16 tmp;  
  4.   uint16 ticks625us; //微妙  
  5.   uint16 elapsedMSec = 0;//毫秒  
  6.   
  7.   // Get the free-running count of 625us timer ticks ,一个计数代表625us  
  8.   tmp = ll_McuPrecisionCount();  
  9.   
  10.   if ( tmp != previousLLTimerTick )  
  11.   {  
  12.     // Calculate the elapsed ticks of the free-running timer.  
  13.     ticks625us = tmp - previousLLTimerTick;  
  14.   
  15.     // Store the LL Timer tick count for the next time through this function.  
  16.     previousLLTimerTick = tmp;  
  17.   
  18.     /* It is necessary to loop to convert the usecs to msecs in increments so as  
  19.      * not to overflow the 16-bit variables.  
  20.      */  
  21.     while ( ticks625us > MAXCALCTICKS )  //主要是防止数据过大转换为毫秒时发生溢出,MAXCALCTICKS = 13105,13105是就会溢出从0开始重新计数  
  22.     {  
  23.       ticks625us -MAXCALCTICKS;  
  24.       elapsedMSec += MAXCALCTICKS * 5 / 8;      //滴答数us转换为ms单位8190ms  
  25.       remUsTicks += MAXCALCTICKS * 5 % 8;    //余数为5,0.625ms  
  26.     }  
  27.   
  28.     // update converted number with remaining ticks from loop and the  
  29.     // accumulated remainder from loop  
  30.     tmp = (ticks625us * 5) + remUsTicks;//将余数加上去  
  31.   
  32.     // Convert the 625 us ticks into milliseconds and a remainder (  余数)  
  33.     elapsedMSec += tmp / 8;  
  34.     remUsTicks = tmp % 8;//计算出整个时间后,进行取整和余数  
  35.   
  36.     // Update OSAL Clock and Timers  
  37.     if ( elapsedMSec )//前后两次调用该函数时已经过去的时间值,该值只是ms位,有余数未计入  
  38.     {  
  39.       osalClockUpdate( elapsedMSec );//更新整个系统的时间,UTCTime以秒为单位记录  
  40.       osalTimerUpdate( elapsedMSec );//传入过去流逝的时间值,进一步更新每个软件定时器,减去一定的时间  
  41.     }  
  42.   }  
  43. }  
[html] view plaincopy
  1. void osalTimeUpdate( void )//得到消逝时间的整数和小数,小数以余数形式存在  
  2. {  
  3.   uint16 tmp;  
  4.   uint16 ticks625us; //微妙  
  5.   uint16 elapsedMSec = 0;//毫秒  
  6.   
  7.   // Get the free-running count of 625us timer ticks ,一个计数代表625us  
  8.   tmp = ll_McuPrecisionCount();  
  9.   
  10.   if ( tmp != previousLLTimerTick )  
  11.   {  
  12.     // Calculate the elapsed ticks of the free-running timer.  
  13.     ticks625us = tmp - previousLLTimerTick;  
  14.   
  15.     // Store the LL Timer tick count for the next time through this function.  
  16.     previousLLTimerTick = tmp;  
  17.   
  18.     /* It is necessary to loop to convert the usecs to msecs in increments so as  
  19.      * not to overflow the 16-bit variables.  
  20.      */  
  21.     while ( ticks625us > MAXCALCTICKS )  //主要是防止数据过大转换为毫秒时发生溢出,MAXCALCTICKS = 13105,13105是就会溢出从0开始重新计数  
  22.     {  
  23.       ticks625us -MAXCALCTICKS;  
  24.       elapsedMSec += MAXCALCTICKS * 5 / 8;      //滴答数us转换为ms单位8190ms  
  25.       remUsTicks += MAXCALCTICKS * 5 % 8;    //余数为5,0.625ms  
  26.     }  
  27.   
  28.     // update converted number with remaining ticks from loop and the  
  29.     // accumulated remainder from loop  
  30.     tmp = (ticks625us * 5) + remUsTicks;//将余数加上去  
  31.   
  32.     // Convert the 625 us ticks into milliseconds and a remainder (  余数)  
  33.     elapsedMSec += tmp / 8;  
  34.     remUsTicks = tmp % 8;//计算出整个时间后,进行取整和余数  
  35.   
  36.     // Update OSAL Clock and Timers  
  37.     if ( elapsedMSec )//前后两次调用该函数时已经过去的时间值,该值只是ms位,有余数未计入  
  38.     {  
  39.       osalClockUpdate( elapsedMSec );//更新整个系统的时间,UTCTime以秒为单位记录  
  40.       osalTimerUpdate( elapsedMSec );//传入过去流逝的时间值,进一步更新每个软件定时器,减去一定的时间  
  41.     }  
  42.   }  
  43. }  

整个系统的时间都由LL(Link Layer)层的一个16位硬件定时器来维护,没深入去看因为是在BLE栈协议中,源码没有公开。这部分代码在获得当前LL Timer的计数值后进行时间计算,主要包括计算elapsed(逝去的)时间,该定时器,1个tick就代表625us,相对于频率1.6MHz。该us时间转换为ms的理想状况是Tms=Num*5/8,NUM为前后两次的tick之差,但是需要考虑的时,NUM如果很大,使得上面程序的temp一下就溢出,因此需要把握在Num到13015之内,超过部分直接获得其消逝的时间值和余数,当然为了精确这里不抛弃余数部分,在下次轮询到时继续加入进去,保证系统时间的精确,也正是remUsTicks = tmp % 8;出现的原因。

 

下面两个函数,前者是维护整个系统系统,保存到一个全局变量中,所有时间以s为单位记录。
      osalClockUpdate( elapsedMSec );//更新整个系统的时间,UTCTime以秒为单位记录

  1. void osalTimerUpdate( uint32 updateTime )  
  2. {  
  3.   halIntState_t intState;  
  4.   osalTimerRec_t *srchTimer;  
  5.   osalTimerRec_t *prevTimer;  
  6.   
  7.   osalTime_t timeUnion;  
  8.   timeUnion.time32 = updateTime;  
  9.   
  10.   HAL_ENTER_CRITICAL_SECTION( intState );  // Hold off interrupts.  
  11.   // Update the system time  
  12.   osal_systemClock += updateTime;  
  13.   HAL_EXIT_CRITICAL_SECTION( intState );   // Re-enable interrupts.  
  14.   
  15.   // Look for open timer slot  
  16.   if ( timerHead != NULL )  
  17.   {  
  18.     // Add it to the end of the timer list  
  19.     srchTimer = timerHead;  
  20.     prevTimer = (void *)NULL;  
  21.   
  22.     // Look for open timer slot  
  23.     while ( srchTimer )  
  24.     {  
  25.       osalTimerRec_t *freeTimer = NULL;  
  26.   
  27.       HAL_ENTER_CRITICAL_SECTION( intState );  // Hold off interrupts.  
  28.   
  29.       // To minimize time in this critical section, avoid 32-bit math。  
  30.       //这边的计数算法总体为了节省时间。  
  31.       if ((timeUnion.time16[1] == 0) && (timeUnion.time8[1] == 0))//消逝的时间数据只有1Byte  
  32.       {  
  33.         // If upper 24 bits are zero, check lower 8 bits for roll over  
  34.         if (srchTimer->timeout.time8[0] >= timeUnion.time8[0])//定时器的时间还没有到0  
  35.         {  
  36.           // 8-bit math  
  37.           srchTimer->timeout.time8[0] -timeUnion.time8[0];//对软定时器进行减计时,只有一个字节且定时器数要大于消逝的时间  
  38.         }  
  39.         else  
  40.         {  
  41.           // 32-bit math  
  42.           if (srchTimer->timeout.time32 > timeUnion.time32)  
  43.           {  
  44.             srchTimer->timeout.time32 -timeUnion.time32;  
  45.           }  
  46.           else  
  47.           {  
  48.             srchTimer->timeout.time32 = 0;  
  49.           }  
  50.         }  
  51.       }  
  52.       else //消逝的时间至少2个Byte,16位  
  53.       {  
  54.           // 32-bit math  
  55.         if (srchTimer->timeout.time32 > timeUnion.time32)  
  56.         {  
  57.           srchTimer->timeout.time32 -timeUnion.time32;  
  58.         }  
  59.         else  
  60.         {  
  61.           srchTimer->timeout.time32 = 0;//一旦定时器时间值小于了消耗的时间值,直接定时器回0  
  62.         }  
  63.       }  
  64.   
  65.       // Check for reloading  
  66.       if ( (srchTimer->timeout.time16[0] == 0) && (srchTimer->timeout.time16[1] == 0) &&  
  67.            (srchTimer->reloadTimeout) && (srchTimer->event_flag) )//是否需要定时器重载  
  68.       {  
  69.         // Notify the task of a timeout  
  70.         osal_set_event( srchTimer->task_id, srchTimer->event_flag );//事件延时事件一刀就立马让进行任务处理  
  71.   
  72.         // Reload the timer timeout value  
  73.         srchTimer->timeout.time32 = srchTimer->reloadTimeout;  
  74.       }  
  75.   
  76.       // When timeout or delete (event_flag == 0)  
  77.       if ( ((srchTimer->timeout.time16[0] == 0) && (srchTimer->timeout.time16[1] == 0)) ||  
  78.             (srchTimer->event_flag == 0) )//在定时器事件处理标志位0或者定时器计数完成,就进行下面的操作  
  79.       {  
  80.         // Take out of list  
  81.         if ( prevTimer == NULL )  
  82.         {  
  83.           timerHead = srchTimer->next;//出现需要清空的定时器为头链表,则需要进行重定位  
  84.         }  
  85.         else  
  86.         {  
  87.           prevTimer->next = srchTimer->next;//删除链表节点,做前后的链接  
  88.         }  
  89.   
  90.         // Setup to free memory  
  91.         freeTimer = srchTimer;//记录当前需要清空的定时器  
  92.   
  93.         // Next  
  94.         srchTimer = srchTimer->next;  
  95.       }  
  96.       else//无满足的定时器,就继续收索到下一个定时器节点进行操作。  
  97.       {  
  98.         // Get next  
  99.         prevTimer = srchTimer;  
  100.         srchTimer = srchTimer->next;  
  101.       }  
  102.   
  103.       HAL_EXIT_CRITICAL_SECTION( intState );   // Re-enable interrupts.  
  104.   
  105.       if ( freeTimer )//找到需要清除的软件定时器  
  106.       {  
  107.         if ( (freeTimer->timeout.time16[0] == 0) && (freeTimer->timeout.time16[1] == 0) )//定时这里意味着有事件发生  
  108.         {  
  109.           osal_set_event( freeTimer->task_id, freeTimer->event_flag );//触发事件,启动任务处理  
  110.         }  
  111.         osal_mem_free( freeTimer );//清除无用定时器(定时结束或者事件发生标志清空)  
  112.       }  
  113.     }  
  114.   }  
  115. }  
[html] view plaincopy
  1. void osalTimerUpdate( uint32 updateTime )  
  2. {  
  3.   halIntState_t intState;  
  4.   osalTimerRec_t *srchTimer;  
  5.   osalTimerRec_t *prevTimer;  
  6.   
  7.   osalTime_t timeUnion;  
  8.   timeUnion.time32 = updateTime;  
  9.   
  10.   HAL_ENTER_CRITICAL_SECTION( intState );  // Hold off interrupts.  
  11.   // Update the system time  
  12.   osal_systemClock += updateTime;  
  13.   HAL_EXIT_CRITICAL_SECTION( intState );   // Re-enable interrupts.  
  14.   
  15.   // Look for open timer slot  
  16.   if ( timerHead != NULL )  
  17.   {  
  18.     // Add it to the end of the timer list  
  19.     srchTimer = timerHead;  
  20.     prevTimer = (void *)NULL;  
  21.   
  22.     // Look for open timer slot  
  23.     while ( srchTimer )  
  24.     {  
  25.       osalTimerRec_t *freeTimer = NULL;  
  26.   
  27.       HAL_ENTER_CRITICAL_SECTION( intState );  // Hold off interrupts.  
  28.   
  29.       // To minimize time in this critical section, avoid 32-bit math。  
  30.       //这边的计数算法总体为了节省时间。  
  31.       if ((timeUnion.time16[1] == 0) && (timeUnion.time8[1] == 0))//消逝的时间数据只有1Byte  
  32.       {  
  33.         // If upper 24 bits are zero, check lower 8 bits for roll over  
  34.         if (srchTimer->timeout.time8[0] >= timeUnion.time8[0])//定时器的时间还没有到0  
  35.         {  
  36.           // 8-bit math  
  37.           srchTimer->timeout.time8[0] -timeUnion.time8[0];//对软定时器进行减计时,只有一个字节且定时器数要大于消逝的时间  
  38.         }  
  39.         else  
  40.         {  
  41.           // 32-bit math  
  42.           if (srchTimer->timeout.time32 > timeUnion.time32)  
  43.           {  
  44.             srchTimer->timeout.time32 -timeUnion.time32;  
  45.           }  
  46.           else  
  47.           {  
  48.             srchTimer->timeout.time32 = 0;  
  49.           }  
  50.         }  
  51.       }  
  52.       else //消逝的时间至少2个Byte,16位  
  53.       {  
  54.           // 32-bit math  
  55.         if (srchTimer->timeout.time32 > timeUnion.time32)  
  56.         {  
  57.           srchTimer->timeout.time32 -timeUnion.time32;  
  58.         }  
  59.         else  
  60.         {  
  61.           srchTimer->timeout.time32 = 0;//一旦定时器时间值小于了消耗的时间值,直接定时器回0  
  62.         }  
  63.       }  
  64.   
  65.       // Check for reloading  
  66.       if ( (srchTimer->timeout.time16[0] == 0) && (srchTimer->timeout.time16[1] == 0) &&  
  67.            (srchTimer->reloadTimeout) && (srchTimer->event_flag) )//是否需要定时器重载  
  68.       {  
  69.         // Notify the task of a timeout  
  70.         osal_set_event( srchTimer->task_id, srchTimer->event_flag );//事件延时事件一刀就立马让进行任务处理  
  71.   
  72.         // Reload the timer timeout value  
  73.         srchTimer->timeout.time32 = srchTimer->reloadTimeout;  
  74.       }  
  75.   
  76.       // When timeout or delete (event_flag == 0)  
  77.       if ( ((srchTimer->timeout.time16[0] == 0) && (srchTimer->timeout.time16[1] == 0)) ||  
  78.             (srchTimer->event_flag == 0) )//在定时器事件处理标志位0或者定时器计数完成,就进行下面的操作  
  79.       {  
  80.         // Take out of list  
  81.         if ( prevTimer == NULL )  
  82.         {  
  83.           timerHead = srchTimer->next;//出现需要清空的定时器为头链表,则需要进行重定位  
  84.         }  
  85.         else  
  86.         {  
  87.           prevTimer->next = srchTimer->next;//删除链表节点,做前后的链接  
  88.         }  
  89.   
  90.         // Setup to free memory  
  91.         freeTimer = srchTimer;//记录当前需要清空的定时器  
  92.   
  93.         // Next  
  94.         srchTimer = srchTimer->next;  
  95.       }  
  96.       else//无满足的定时器,就继续收索到下一个定时器节点进行操作。  
  97.       {  
  98.         // Get next  
  99.         prevTimer = srchTimer;  
  100.         srchTimer = srchTimer->next;  
  101.       }  
  102.   
  103.       HAL_EXIT_CRITICAL_SECTION( intState );   // Re-enable interrupts.  
  104.   
  105.       if ( freeTimer )//找到需要清除的软件定时器  
  106.       {  
  107.         if ( (freeTimer->timeout.time16[0] == 0) && (freeTimer->timeout.time16[1] == 0) )//定时这里意味着有事件发生  
  108.         {  
  109.           osal_set_event( freeTimer->task_id, freeTimer->event_flag );//触发事件,启动任务处理  
  110.         }  
  111.         osal_mem_free( freeTimer );//清除无用定时器(定时结束或者事件发生标志清空)  
  112.       }  
  113.     }  
  114.   }  
  115. }  

 

该函数是处理整个软件定时器的核心所在,设计的稍微复杂一点:之所有存在这个软件定时器,其实也是有LL Timer来完成的。它的存在是为了给某些场合提供方便,比如设备启动之后,就会设计成这样 ,延时5s都会触发一次事件的发生,以表示系统在正常运行,类似于心跳包。

osal_start_timerEx( simpleBLEPeripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD ),该函数是OSAL内实现启动一个事件的函数类型与osal_set_event(),只是前者需要有一个延时,延时事件到后触发事件,而这个延时就是由上面的软件定时器来完成的。

OSAL维护着一个定时器全局链表timerHead,每当调用osal_start_timerEx实际再调用osalAddTimer,来创建一个新的软件Timer。

  1. osalTimerRec_t * osalAddTimer( uint8 task_id, uint16 event_flag, uint32 timeout )  
  2. {  
  3.   osalTimerRec_t *newTimer;  
  4.   osalTimerRec_t *srchTimer;  
  5.   
  6.   // Look for an existing timer first  
  7.   newTimer = osalFindTimer( task_id, event_flag );//通过对任务ID和具体的事件标志来查询定时器链表中是否已经存在  
  8.   if ( newTimer )  
  9.   {  
  10.     // Timer is found - update it.  
  11.     newTimer->timeout.time32 = timeout;//如果定时器事件对应的任务处理定时器已经存在直接更新时间  
  12.   
  13.     return ( newTimer );  
  14.   }  
  15.   else  
  16.   {  
  17.     // New Timer  
  18.     newTimer = osal_mem_alloc( sizeof( osalTimerRec_t ) );//新分配一个软定时器内存  
  19.   
  20.     if ( newTimer )  
  21.     {  
  22.       // Fill in new timer  
  23.       newTimer->task_id = task_id;  
  24.       newTimer->event_flag = event_flag;  
  25.       newTimer->timeout.time32 = timeout;  
  26.       newTimer->next = (void *)NULL;//该定时器在定时器链表尾  
  27.       newTimer->reloadTimeout = 0;  
  28.   
  29.       // Does the timer list already exist  
  30.       if ( timerHead == NULL )  
  31.       {  
  32.         // Start task list  
  33.         timerHead = newTimer;  
  34.       }  
  35.       else  //定时器链表头非空  
  36.       {  
  37.         // Add it to the end of the timer list  
  38.         srchTimer = timerHead;  
  39.   
  40.         // Stop at the last record  
  41.         while ( srchTimer->next )  
  42.           srchTimer = srchTimer->next;//找到链表尾  
  43.   
  44.         // Add to the list  
  45.         srchTimer->next = newTimer;//将最新的定时器加入到链表尾中,其next为NULL  
  46.       }  
  47.   
  48.       return ( newTimer );  
  49.     }  
  50.     else  
  51.     {  
  52.       return ( (osalTimerRec_t *)NULL );  
  53.     }  
  54.   }  
  55. }  
[html] view plaincopy
  1. osalTimerRec_t * osalAddTimer( uint8 task_id, uint16 event_flag, uint32 timeout )  
  2. {  
  3.   osalTimerRec_t *newTimer;  
  4.   osalTimerRec_t *srchTimer;  
  5.   
  6.   // Look for an existing timer first  
  7.   newTimer = osalFindTimer( task_id, event_flag );//通过对任务ID和具体的事件标志来查询定时器链表中是否已经存在  
  8.   if ( newTimer )  
  9.   {  
  10.     // Timer is found - update it.  
  11.     newTimer->timeout.time32 = timeout;//如果定时器事件对应的任务处理定时器已经存在直接更新时间  
  12.   
  13.     return ( newTimer );  
  14.   }  
  15.   else  
  16.   {  
  17.     // New Timer  
  18.     newTimer = osal_mem_alloc( sizeof( osalTimerRec_t ) );//新分配一个软定时器内存  
  19.   
  20.     if ( newTimer )  
  21.     {  
  22.       // Fill in new timer  
  23.       newTimer->task_id = task_id;  
  24.       newTimer->event_flag = event_flag;  
  25.       newTimer->timeout.time32 = timeout;  
  26.       newTimer->next = (void *)NULL;//该定时器在定时器链表尾  
  27.       newTimer->reloadTimeout = 0;  
  28.   
  29.       // Does the timer list already exist  
  30.       if ( timerHead == NULL )  
  31.       {  
  32.         // Start task list  
  33.         timerHead = newTimer;  
  34.       }  
  35.       else  //定时器链表头非空  
  36.       {  
  37.         // Add it to the end of the timer list  
  38.         srchTimer = timerHead;  
  39.   
  40.         // Stop at the last record  
  41.         while ( srchTimer->next )  
  42.           srchTimer = srchTimer->next;//找到链表尾  
  43.   
  44.         // Add to the list  
  45.         srchTimer->next = newTimer;//将最新的定时器加入到链表尾中,其next为NULL  
  46.       }  
  47.   
  48.       return ( newTimer );  
  49.     }  
  50.     else  
  51.     {  
  52.       return ( (osalTimerRec_t *)NULL );  
  53.     }  
  54.   }  
  55. }  

结合上面两个函数,可以很好的理解软件定时器的工作模式,是一个减计算模式,加入了点小算法,对定时完成后的事件直接进行事件触发操作,以达到延时效果。
 

3.OSAL中的消息处理相关API解析

消息的出现,比如按键事件触发,引起了硬件task任务处理,处理时会调用向应用层注册的一个回调函数Callback(初始化时完成),在回调函数中将按键的ID打包,通过消息发给应用层APP,应用层再进一步做出处理。

  1. uint8 OnBoard_SendKeys( uint8 keys, uint8 state )  
  2. {  
  3.   keyChange_t *msgPtr;  
  4.   
  5.   if ( registeredKeysTaskID != NO_TASK_ID )  
  6.   {  
  7.     // Send the address to the task  
  8.     msgPtr = (keyChange_t *)osal_msg_allocate( sizeof(keyChange_t) ); //其实分配的内存大小为osal_msg_hdr_t + keyChange_t  
  9.     if ( msgPtr )  
  10.     {  
  11.       msgPtr->hdr.event = KEY_CHANGE;//事件类型按键变化  
  12.       msgPtr->state = state;  
  13.       msgPtr->keys = keys;  
  14.   
  15.       osal_msg_send( registeredKeysTaskID, (uint8 *)msgPtr );//将按键事件产生的消息发送到注册啦按键这一事件的任务  
  16.     }  
  17.     return ( SUCCESS );  
  18.   }  
  19.   else  
  20.     return ( FAILURE );  
[html] view plaincopy
  1. uint8 OnBoard_SendKeys( uint8 keys, uint8 state )  
  2. {  
  3.   keyChange_t *msgPtr;  
  4.   
  5.   if ( registeredKeysTaskID != NO_TASK_ID )  
  6.   {  
  7.     // Send the address to the task  
  8.     msgPtr = (keyChange_t *)osal_msg_allocate( sizeof(keyChange_t) ); //其实分配的内存大小为osal_msg_hdr_t + keyChange_t  
  9.     if ( msgPtr )  
  10.     {  
  11.       msgPtr->hdr.event = KEY_CHANGE;//事件类型按键变化  
  12.       msgPtr->state = state;  
  13.       msgPtr->keys = keys;  
  14.   
  15.       osal_msg_send( registeredKeysTaskID, (uint8 *)msgPtr );//将按键事件产生的消息发送到注册啦按键这一事件的任务  
  16.     }  
  17.     return ( SUCCESS );  
  18.   }  
  19.   else  
  20.     return ( FAILURE );  

在这里进行消息的发送,OSAL维护一个全局的osal_qHead队列,允许你将新的消息插入队列头或者尾,整个消息内容包括固定的链表头:

  1. typedef struct  
  2. {  
  3.   void   *next;  
  4.   uint16 len;  
  5.   uint8  dest_id;  
  6. } osal_msg_hdr_t;  
[html] view plaincopy
  1. typedef struct  
  2. {  
  3.   void   *next;  
  4.   uint16 len;  
  5.   uint8  dest_id;  
  6. } osal_msg_hdr_t;  

再加上相关消息需要携带的内容。在收到消息的地方,遍历链表,找到对应的消息,再会对数据进行提取。

  1. uint8 *osal_msg_receive( uint8 task_id )  
  2. {  
  3.   osal_msg_hdr_t *listHdr;  
  4.   osal_msg_hdr_t *prevHdr = NULL;  
  5.   osal_msg_hdr_t *foundHdr = NULL;  
  6.   halIntState_t   intState;  
  7.   
  8.   // Hold off interrupts  
  9.   HAL_ENTER_CRITICAL_SECTION(intState);  
  10.   
  11.   // Point to the top of the queue  
  12.   listHdr = osal_qHead;  
  13.   
  14.   // Look through the queue for a message that belongs to the asking task  
  15.   while ( listHdr != NULL )  
  16.   {  
  17.     if ( (listHdr - 1)->dest_id == task_id )//判断消息处理函数是否就是当前接收的任务处理函数  
  18.     {  
  19.       if ( foundHdr == NULL )  
  20.       {  
  21.         // Save the first one  
  22.         foundHdr = listHdr;  
  23.       }  
  24.       else  
  25.       {  
  26.         // Second msg found, stop looking  
  27.         break;  
  28.       }  
  29.     }  
  30.     if ( foundHdr == NULL )  
  31.     {  
  32.       prevHdr = listHdr;  
  33.     }  
  34.     listHdr = OSAL_MSG_NEXT( listHdr );  
  35.   }  
  36.   
  37.   // Is there more than one?  
  38.   if ( listHdr != NULL )  
  39.   {  
  40.     // Yes, Signal the task that a message is waiting  
  41.     osal_set_event( task_id, SYS_EVENT_MSG );  
  42.   }  
  43.   else  
  44.   {  
  45.     // No more  
  46.     osal_clear_event( task_id, SYS_EVENT_MSG );//清楚这个任务事件消息标志  
  47.   }  
  48.   
  49.   // Did we find a message?  
  50.   if ( foundHdr != NULL )  
  51.   {  
  52.     // Take out of the link list  
  53.     osal_msg_extract( &osal_qHead, foundHdr, prevHdr );//解析出消息内容  
  54.   }  
  55.   
  56.   // Release interrupts  
  57.   HAL_EXIT_CRITICAL_SECTION(intState);  
  58.   
  59.   return ( (uint8*) foundHdr );  
  60. }  
[html] view plaincopy
  1. uint8 *osal_msg_receive( uint8 task_id )  
  2. {  
  3.   osal_msg_hdr_t *listHdr;  
  4.   osal_msg_hdr_t *prevHdr = NULL;  
  5.   osal_msg_hdr_t *foundHdr = NULL;  
  6.   halIntState_t   intState;  
  7.   
  8.   // Hold off interrupts  
  9.   HAL_ENTER_CRITICAL_SECTION(intState);  
  10.   
  11.   // Point to the top of the queue  
  12.   listHdr = osal_qHead;  
  13.   
  14.   // Look through the queue for a message that belongs to the asking task  
  15.   while ( listHdr != NULL )  
  16.   {  
  17.     if ( (listHdr - 1)->dest_id == task_id )//判断消息处理函数是否就是当前接收的任务处理函数  
  18.     {  
  19.       if ( foundHdr == NULL )  
  20.       {  
  21.         // Save the first one  
  22.         foundHdr = listHdr;  
  23.       }  
  24.       else  
  25.       {  
  26.         // Second msg found, stop looking  
  27.         break;  
  28.       }  
  29.     }  
  30.     if ( foundHdr == NULL )  
  31.     {  
  32.       prevHdr = listHdr;  
  33.     }  
  34.     listHdr = OSAL_MSG_NEXT( listHdr );  
  35.   }  
  36.   
  37.   // Is there more than one?  
  38.   if ( listHdr != NULL )  
  39.   {  
  40.     // Yes, Signal the task that a message is waiting  
  41.     osal_set_event( task_id, SYS_EVENT_MSG );  
  42.   }  
  43.   else  
  44.   {  
  45.     // No more  
  46.     osal_clear_event( task_id, SYS_EVENT_MSG );//清楚这个任务事件消息标志  
  47.   }  
  48.   
  49.   // Did we find a message?  
  50.   if ( foundHdr != NULL )  
  51.   {  
  52.     // Take out of the link list  
  53.     osal_msg_extract( &osal_qHead, foundHdr, prevHdr );//解析出消息内容  
  54.   }  
  55.   
  56.   // Release interrupts  
  57.   HAL_EXIT_CRITICAL_SECTION(intState);  
  58.   
  59.   return ( (uint8*) foundHdr );  
  60. }  

以上的过程就是整个事件触发消息,消息再打包发生,由目的任务处理函数进行进一步处理,直到完成后进行清空。

以上是部分OSAL中的内容,下次继续分析BLE中的主从机简单的工作模型。

阅读(1751) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~