tinyos学习之day 2

来源:互联网 发布:应届生毕业生简历知乎 编辑:程序博客网 时间:2024/05/17 22:28
  经过几天的摸爬滚打以及互联网搜索,终于配置好了基于cc2530硬件平台的tinyos系统。特别感谢 开源的6lowpan 博主大大提供的虚拟机镜像以及技术群里热心的大神们!  今天来大概翻译 http://tinyos.stanford.edu/tinyos-wiki/index.php/Modules_and_the_TinyOS_Execution_Model 这页。

1、Modules and State
编译tinyos应用会产生一个简单的二进制文件,假设它已经完全控制了整个硬件平台。所有的组件只有一个地址空间共享。所以很多组件都设为private以及禁止传递指针。
配件和模块的最大区别就是:配件(configuration)是表示组件的连接关系,模块是表示组件之间的逻辑关系。模块的实现大多数是用c写的,一部分用了nesc。
模块能声明不同的状态(如@safe,不知道什么意思)。然而配件就一定是private的,即不能直接访问或者通过其他组件命名它。
接下来是修改Blink例程,将三个计时器改为一个(略过)

  **引用群主大神一句话:TinyOS编程时每编写一个task函数,最后编译连接将在全局静态任务数组增加一项,没错也就是滥用内存。**

注:由于不同硬件平台的int之类的长度不一致且环绕式操作的负数会导致意想不到的序列,所以定义如下:
8 bits 16 bits 32 bits 64 bits
signed int8_t int16_t int32_t int64_t
unsigned uint8_t uint16_t uint32_t uint64_t

2、Interfaces, Commands, and Events
如果一个组件使用了接口,那么它可以使用接口的命令(command),但必须实现接口的事件(event)。唤醒一个接口的命令用到关键词call,而唤醒接口的事件用到关键词signal。
现在我们看到的代码都是synchronous(同步)的,即没什么优先权可言。同步代码会占据cpu一直运行直到完成再让给其他代码,这样做能最小化ram的消耗,但是这会削弱系统的应答性。
task函数有点类似于windows里面的线程,是一个可以被操作系统调度执行的函数,而不是立即执行,像是“后台处理”。但任务被认为是较为耗时的操作,执行完毕要进行结果输出,可以call command或者signal event。
task返回的一定是void,用关键词post调用。A component can post a task in a command, an event, or a task. We will see later that, by convention, commands do not signal events to avoid creating recursive loops across component boundaries.任务不能打断任务,但能被硬件中断打断。
以下是例程:
task void computeTask() {
static uint32_t i;
uint32_t start = i;
for (;i < start + 10000 && i < 400001; i++) {}
if (i >= 400000) {
i = 0;
}
else {
post computeTask();
}
}

3、Internal Functions
A component can define standard C functions, which other components cannot name and therefore cannot invoke directly. While these functions do not have the command or event modifier, they can freely call commands or signal events.
For example:

implementation
{

void startTimers() {
call Timer0.startPeriodic( 250 );
call Timer1.startPeriodic( 500 );
call Timer2.startPeriodic( 1000 );
}

event void Boot.booted()
{
startTimers();
}

4、Split-Phase Operations(分阶段操作)

0 0