注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
dan158185的个人空间 https://home.eeworld.com.cn/space-uid-349284.html [收藏] [复制] [分享] [RSS]
日志

CC2538之TinyOS例程实验:1-blink nesC语法

已有 1314 次阅读2016-1-5 16:09 |个人分类:CC2538之TinyOS例程

例程目录tinyos-main-release_tinyos_2_1_2\apps\cc2538_Test\blink,包含三个文件:Makefile,BlinkAppC.nc,BlinkC.nc

下面将通过该例程讲解TinyOS nesC的语法:

1,TinyOS的nesC文件分为四种,除了Makefile后缀为.nc文件,Makefile,configuration,module,interface,当然C语言的c,cpp,h文件TinyOS支持,可以去博客看一下TinyOS如何使用C文件

2,关键字介绍: components provides uses call post task as async signal

1,Makefile

  1. COMPONENT=BlinkAppC  
  2. CFLAGS += -DUSE_TIMER_HANDLER  
  3. include $(MAKERULES)  

通过例程视频第八部的讲解,大家应该知道了nesC的基础语法
configuration对应即为BlinkAppC.nc文件
moudle对应为BlinkC.nc文件
Makefile的基础写法
  1. COMPONENT=xx  
  2. ...  
  3. include $(MAKERULES)  
COMPONENT(configuration的名称,注意components关键字的区别)
include $(MAKERULES)  指定makerules,MAKERULES是环境变量,可以打开shell 输入:
echo $MAKERULES查看

2,BlinkApp.nc(TinyOS的configration文件)

  1. configuration BlinkAppC{  
  2. }  
  3. implementation{  
  4.   components MainC, BlinkC, LedsC;  
  5.   components new TimerMilliC() as Timer0;  
  6.   components new TimerMilliC() as Timer1;  
  7.   components new TimerMilliC() as Timer2;  
  8.   
  9.   
  10.   BlinkC -> MainC.Boot;  
  11.   BlinkC.Timer0 -> Timer0;  
  12.   BlinkC.Timer1 -> Timer1;  
  13.   BlinkC.Timer2 -> Timer2;  
  14.   BlinkC.Leds -> LedsC;  
  15. }  
学习configuration的基础语法,当完成编写后对于使用者调用它就是components  BlinkAppC;
configuration的代码结构为 
  1. configuration xxC{  
  2.     ... //1区  
  3. }  
  4. implementation{  
  5.     ... //2区  
  6. }  
*注意xxC的名称应和xx.nc的名称保持一致;
components--只有configration文件和module文件可以作为components,注意和Makefile的COMPONENT的区别
*1区应该编写
  1. provides interface xx;  
  1. uses interface xx;  
provides------本components提供的interface,供调用者使用;无需给别人提供interface故可以省去,如blink例程
uses-----------声明使用其它的components的interface
-> <- =符号--用来指明components连接关系,只在configuration文件使用,消费者->提供者,提供者<-消费者,=符号长多见于interface的使用
如果provides/uses有多个则写法分为两种:两种写法等价,推荐使用右侧写法
  1. provides interface xx1                            provides {  
  2. provides interface xx2                                               interface xx1;  
  3. ..                                                                                ...  
  4. provides interface xxn                                               interface xxn;  
  5.                                                   }  
  6.   
  7. uses     interface xx1                            uses{  
  8. uses     interface xx2                                           interface xx1 ;  
  9. ..                                                                          ...  
  10. uses     interface xxn                                           interface xxn ;  
  11.                                                   }  
implementation----固定写法,configuration和module使用
as---------------------重命名,使用对象为components和interface
interface-------------用于连接使用者(调用者/消费者)和提供者(被调用者/生产者)
用法分为两种:
1,在configuration和moduled的{ }中声明使用(uses)或者提供(provides)的interface
2,interface文件,这是nesC的精髓,C语言文件的调用是通过xx.h文件来关联,nesC则把这种关系称为wire,需要interface文件来声明,举个例子编写components的时候provides interface类似C语言的xx.h文件,声明xx.c文件的c函数,对于nesC来说函数的原型声明则在xx.nc(interface)文件中;
来看一个例子:tinyos-main-release_tinyos_2_1_2\tos\interfaces 的Boot.nc
  1. interface Boot {  
  2.   /**  
  3.    * Signaled when the system has booted successfully. Components can  
  4.    * assume the system has been initialized properly. Services may  
  5.    * need to be started to work, however.  
  6.    *  
  7.    * @see StdControl  
  8.    * @see SplitConrol  
  9.    * @see TEP 107: Boot Sequence  
  10.    */  
  11.   event void booted();  
  12. }  
基础语法:
  1. interface xx{  
  2.   command 函数方法  
  3.   event   事件返回  
  4. }  
xx.nc名称也是一样要和提供的interface名称一致;
内容常常包括两大类:
command----------nesC的函数方法,常常通过interface提供给消费者调用,类似于C语言的C函数,在interface文件中声明本质类似C语言的xx.h文件声明或者直接使用extern xx(...);的声明方法;使用call调用
event--------------TinyOS的精髓2,split-phase,事件返回,使用signal关键字触发事件,如Timer,先调用start的command设置好定时器并启动,然后消费者等待event(fired)事件处理;
components可以有多个command和event

async--------------常常用来修饰command和event,需要用到的时候是该command和event是和mcu硬件外设相关
.      ---------------符号“.”是常常使用的
1,components.interface,如BlinkC -> MainC.Boot;,这是隐式写法等价于BlinkC.Boot -> MainC.Boot;
2,  interface.[command/event];如call Timer0.startPeriodic( 250 );
3,结构体成员,这是C语言的内容不做介绍

分析BlinkApp.nc文件:
components有MainC,BlinkC,LedsC,TimerMilliC();
BlinkC使用了MainC的Boot;TimerMilliC()的Timer<TMilli>,LedsC的Leds等interface

3,BlinkC.nc(module)

  1.  /*******************************************************************  
  2.  *实验1----led点灯实验  
  3.  *节点需求数1  
  4.  *编译命令make cc2538cb  
  5.  ********************************************************************/  
  6. #include "Timer.h"  
  7.   
  8. module BlinkC @safe()  
  9. {  
  10.   uses interface Timer<TMilli> as Timer0;  
  11.   uses interface Timer<TMilli> as Timer1;  
  12.   uses interface Timer<TMilli> as Timer2;  
  13.   uses interface Leds;  
  14.   uses interface Boot;  
  15.     
  16. }  
  17. implementation  
  18. {  
  19.    
  20.   task void time1_Task();  
  21.     
  22.   /***************************************************  
  23.   *启动事件  
  24.   ****************************************************/  
  25.   event void Boot.booted()  
  26.   {  
  27.     /***开启三个周期性定时器(单位毫秒) 分别250毫秒,500毫秒,1秒******/  
  28.     call Timer0.startPeriodic( 250 );  
  29.     call Timer1.startPeriodic( 500 );  
  30.     call Timer2.startPeriodic( 1000 );  
  31.   }  
  32.     
  33.   /***************************************************  
  34.   *Timer0定时时间到事件  
  35.   ****************************************************/  
  36.   event void Timer0.fired()  
  37.   {  
  38.      /**翻转led0电平,对应cc2538cb的绿灯**/  
  39.      call Leds.led0Toggle();  
  40.   }  
  41.     
  42.   /***************************************************  
  43.   *任务time1_Task  
  44.   ****************************************************/  
  45.   task void time1_Task()  
  46.   {  
  47.      /**翻转led1电平,对应cc2538cb的黄灯**/  
  48.      call Leds.led1Toggle();  
  49.   }  
  50.     
  51.   /***************************************************  
  52.   *Timer1定时时间到事件  
  53.   ****************************************************/  
  54.   event void Timer1.fired()  
  55.   {  
  56.     /****提交time1_Task任务***/  
  57.     post time1_Task();   
  58.   }  
  59.     
  60.   /***************************************************  
  61.   *Timer2定时时间到事件  
  62.   ****************************************************/  
  63.   event void Timer2.fired()  
  64.   {   
  65.     /**翻转led2电平,对应cc2538cb的红灯**/  
  66.     call Leds.led2Toggle();  
  67.   }  
  68.    
  69. }  

module--------类似于C语言的c文件,前面介绍了Makefile,configuration和interface,这四个个分别都是TinyOS的xx.nc文件的写法,
interface文件则相当于C语言的xx.h文件;
configuration本质更加像GCC等编译器的makefile,声明文件关联关系

module的基础写法:
  1. module xx(){  
  2.   provides interface xx  
  3.   uses interface xx;  
  4. }  
  5. implementation  
  6. {  
  7.     
  8.   command {  
  9.   }  
  10.   task{  
  11.   }  
  12.   event{  
  13.   }  
  14. }  
module本质就是command,event, task等的实现部分,command/event已经介绍
task-------TinyOS的任务
原型为: task void task_name(..){
                    .......
               }
post-------使用task关键字,如post task_name(...);
切记,使用的interface如果有event事件返回,消费者的module中必须有该event的处理



通过上面的基础讲解,现在看blink例程应该发现例程的实现就是led闪烁
对于cc2538cb套件:
led0~PC0~绿灯
led1~PC1~黄灯
led2~PC2~红灯
进入例程blink目录,执行编译命令: make cc2538cb完成编译下载固件到cc2538cb节点即可看见
实验结果;

blink学习意义:
1,cc2538cb的IO口的操作,为以后自己编写传感器的驱动打下基础;
2,初步了解2538 fwlib的使用,通过上面的nesC的语法讲解,自身去分析代码时候将看到如何wire到
tinyos-main-release_tinyos_2_1_2\tos\chips\cc2538\fwlib下的gpio.c

    
附注:
1,对于一个TinyOS的代码的必须组件MainC是必不可少的,提供int main(),C语言写法
包括MainC组件提供的Boot事件,可以理解为启动完成事件,也就是类似于单片机bootloader完成后进入main函数,Boot中开始编写任务;
2,参考tep103文档,configuration文件为xxC.nc,module为xxP.nc,interface则后缀不可有C/P,且必须小写
3,对于应用,如果不需要提供给他人使用给以省略providers自然也就不用编写interface.nc文件,如blink例程

更多参考访问TinyOS官网参考nesC编程手册或者我的百度网盘下载相关文档!
可以参考第8第9部视频学习nesC编程!
评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章