2833|0

111

帖子

1

TA的资源

一粒金砂(高级)

楼主
 

CC2538之TinyOS例程实验:1-blink nesC语法基础 [复制链接]

本帖最后由 dan158185 于 2015-12-30 17:26 编辑

例程目录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的基础写法:
COMPONENT=?(configuration的名称,注意components关键字的区别)
include $(MAKERULES)  指定makerules,MAKERULES是环境变量,可以打开shell 输入:
echo $MAKERULES查看

2,BlinkAppC.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区应该编写
provides interface xx;
uses interface xx;

provides------本components提供的interface,供调用者使用;无需给别人提供interface故可以省去,如blink例程
uses-----------声明使用其它的components的interface
-> <- =符号--用来指明components连接关系,只在configuration文件使用,消费者->提供者,提供者<-消费者,=符号长多见于interface的使用
如果provides/uses有多个则写法分为两种:两种写法等价,推荐使用右侧写法
provides interface xx1                            provides {
provides interface xx2                                               interface xx1;
..                                                                                ...
provides interface xxn                                               interface xxn;
                                                            }

uses     interface xx1                             uses{
uses     interface xx2                                           interface xx1 ;
..                                                                          ...
uses     interface xxn                                           interface xxn ;
                                                            }

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()
interface:BlinkC使用了MainC的Boot;TimerMilliC()的Timer,LedsC的Leds等interface

3,BlinkC.nc(module)


  1. /*******************************************************************
  2. *实验1----led点灯实验
  3. *节点需求数1
  4. *编译命令make cc2538cb
  5. ********************************************************************/
  6. #include "Timer.h"

  7. module BlinkC @safe()
  8. {
  9.   uses interface Timer<TMilli> as Timer0;
  10.   uses interface Timer<TMilli> as Timer1;
  11.   uses interface Timer<TMilli> as Timer2;
  12.   uses interface Leds;
  13.   uses interface Boot;
  14.   
  15. }
  16. implementation
  17. {

  18.   task void time1_Task();
  19.   
  20.   /***************************************************
  21.   *启动事件
  22.   ****************************************************/
  23.   event void Boot.booted()
  24.   {
  25.     /***开启三个周期性定时器(单位毫秒) 分别250毫秒,500毫秒,1秒******/
  26.     call Timer0.startPeriodic( 250 );
  27.     call Timer1.startPeriodic( 500 );
  28.     call Timer2.startPeriodic( 1000 );
  29.   }
  30.   
  31.   /***************************************************
  32.   *Timer0定时时间到事件
  33.   ****************************************************/
  34.   event void Timer0.fired()
  35.   {
  36.      /**翻转led0电平,对应cc2538cb的绿灯**/
  37.      call Leds.led0Toggle();
  38.   }
  39.   
  40.   /***************************************************
  41.   *任务time1_Task
  42.   ****************************************************/
  43.   task void time1_Task()
  44.   {
  45.      /**翻转led1电平,对应cc2538cb的黄灯**/
  46.      call Leds.led1Toggle();
  47.   }
  48.   
  49.   /***************************************************
  50.   *Timer1定时时间到事件
  51.   ****************************************************/
  52.   event void Timer1.fired()
  53.   {
  54.     /****提交time1_Task任务***/
  55.     post time1_Task();
  56.   }
  57.   
  58.   /***************************************************
  59.   *Timer2定时时间到事件
  60.   ****************************************************/
  61.   event void Timer2.fired()
  62.   {
  63.     /**翻转led2电平,对应cc2538cb的红灯**/
  64.     call Leds.led2Toggle();
  65.   }

  66. }
复制代码
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的任务
原型为:
  1. task void task_name(..){
  2.                     .......
  3.                }
复制代码
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编程手册或者我的百度网盘下载相关文档!





此帖出自无线连接论坛
点赞 关注(1)
个人签名https://open6lowpan.taobao.com/
 

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
快速回复 返回顶部 返回列表