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

本文通过CC2538上的Blink例程,详细介绍了TinyOS中nesC的语法,包括Makefile、配置文件(BlinkApp.nc)和模块文件(BlinkC.nc)的使用,讲解了components、provides、uses、call、post、task、as、async和signal等关键字。

例程目录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

COMPONENT=BlinkAppC
CFLAGS += -DUSE_TIMER_HANDLER
include $(MAKERULES)

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

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

configuration BlinkAppC{
}
implementation{
  components MainC, BlinkC, LedsC;
  components new TimerMilliC() as Timer0;
  components new TimerMilliC() as Timer1;
  components new TimerMilliC() as Timer2;


  BlinkC -> MainC.Boot;
  BlinkC.Timer0 -> Timer0;
  BlinkC.Timer1 -> Timer1;
  BlinkC.Timer2 -> Timer2;
  BlinkC.Leds -> LedsC;
}
学习configuration的基础语法,当完成编写后对于使用者调用它就是components  BlinkAppC;
configuration的代码结构为 
configuration xxC{
    ... //1区
}
implementation{
    ... //2区
}
*注意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
interface Boot {
  /**
   * Signaled when the system has booted successfully. Components can
   * assume the system has been initialized properly. Services may
   * need to be started to work, however.
   *
   * @see StdControl
   * @see SplitConrol
   * @see TEP 107: Boot Sequence
   */
  event void booted();
}
基础语法:
interface xx{
  command 函数方法
  event   事件返回
}
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----led点灯实验
 *节点需求数1
 *编译命令make cc2538cb
 ********************************************************************/
#include "Timer.h"

module BlinkC @safe()
{
  uses interface Timer<TMilli> as Timer0;
  uses interface Timer<TMilli> as Timer1;
  uses interface Timer<TMilli> as Timer2;
  uses interface Leds;
  uses interface Boot;
  
}
implementation
{
 
  task void time1_Task();
  
  /***************************************************
  *启动事件
  ****************************************************/
  event void Boot.booted()
  {
    /***开启三个周期性定时器(单位毫秒) 分别250毫秒,500毫秒,1秒******/
    call Timer0.startPeriodic( 250 );
    call Timer1.startPeriodic( 500 );
    call Timer2.startPeriodic( 1000 );
  }
  
  /***************************************************
  *Timer0定时时间到事件
  ****************************************************/
  event void Timer0.fired()
  {
     /**翻转led0电平,对应cc2538cb的绿灯**/
     call Leds.led0Toggle();
  }
  
  /***************************************************
  *任务time1_Task
  ****************************************************/
  task void time1_Task()
  {
     /**翻转led1电平,对应cc2538cb的黄灯**/
     call Leds.led1Toggle();
  }
  
  /***************************************************
  *Timer1定时时间到事件
  ****************************************************/
  event void Timer1.fired()
  {
    /****提交time1_Task任务***/
    post time1_Task(); 
  }
  
  /***************************************************
  *Timer2定时时间到事件
  ****************************************************/
  event void Timer2.fired()
  { 
    /**翻转led2电平,对应cc2538cb的红灯**/
    call Leds.led2Toggle();
  }
 
}

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

module的基础写法:
module xx(){
  provides interface xx
  uses interface xx;
}
implementation
{
  
  command {
  }
  task{
  }
  event{
  }
}

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编程!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值