Article 3 basic structure and initial Writing of tinyos/NESC Program (Lesson 1 blink)

Source: Internet
Author: User

Like everyone, I'm following ../tinyos/cygwin/opt/tinyos-1.x/doc/tutorial
The 8 lesson in the group performs operations and learning. Although it is painful, there is really no better way to learn this strange embedded language. I believe that when most of you are facing NESC, the biggest problem is that you don't know where to start or what to write to yourself. The following steps should at least let you know what to do with NESC.

 

Step 1: select the components to use based on the actual situation. Taking writing blink as an example:

 

First, we need main, which is the component starting with the program and required for every tinyos Program (application. Or it can be said that it is the entrance of the NESC program, similar to the main () in C language, "Main" calls other component to implement program functions.

 

Second, you need a component to control the program logic, or a component that specifically implements the program logic function. Generally, it expresses the logic of the program. It is named the same as the configuration file, but an M is added, indicating the module file. In this example, It is blinkm, that is, the component corresponding to the module File we mentioned in the previous article.

 

Third, because LED is used in the program, the ledc provided by the system is required. No way. You only need to read the system Lib.

 

Fourth, because the program requires time control, the system provides timer (or the User-Defined singletimer). In fact, the User-Defined singletimer still calls the system's timer. the modified blink code to remove simpletimer will be attached later. You can check it for yourself)

 

To sum up, there is no good method. Only when you are familiar with the system can you complete the control of the underlying layer. You must understand and learn the underlying interfaces. Otherwise, there is no way to learn NESC.

 

Step 2: select the appropriate component and write the configuration file)

Logically, after you select a component, you need the top-level configuration file for the wiring component to work together to complete the program functions you need.

In fact, a program may have multiple configuration files, but there must be a top-level configuration file, which is usually named by the Application name.


The configuration file configuration first declares the components in its application. Keyword: components.

In this example: Components
Main, blinkm, singletimer, ledsc;


After the component is declared, you can connect the interfaces of the two components through->.


In this example: Main. stdcontrol->
Blinkm. stdcontrol;

Main. stdcontrol-> singletimer. stdcontrol;
 

Blinkm. Timer
-> Singletimer. timer;

Linkm. LEDs
-> Ledsc

 

Recall the previous article, we said:

There are two keywords to implement wiring. I have translated it into "connection. The keyword "à" and "signature" are always connected to an interface that uses (uses) and provided (provides. That is to say, only the user component can call the interface of the provider component. This is not the case.

 

In tinyos, components and interfaces are a many-to-many relationship, that is, a component can be connected to many interfaces. In other words, many components can provide the same interface! (Core! What's hard to understand !)

 

As mentioned above, different components can provide the same interface. If the coma and comb components provide an interfacec interface, what should I do when the comd component needs to access the interfacec interface? Does it access interfacec provided by coma or interfacec provided by comb? You must know that although the interface name is the same, the same interface provided by different components implements different functions.


Here, Main. stdcontrol
-> Blinkm. stdcontrol. This line of code connects the main component and the stdcontrol component of blinkm. In this way, the connection between the two components is established. When Main. stdcontrol is called, blinkm. stdcontrol is called. Main. stdcontrol-> singletimer. stdcontrol; this line of code connects the main and the stdcontrol of singletimer, and establishes the connection between main and singletimer. You can see that the user "Main" is connected to two providers at the same time. When the stdcontrol of main is called, both blinkm. stdcontrol and singletimer. stdcontrol are called.

 

Now we know some interfaces provided and used by some components. For example, blinkm provides stdcontrol because it is behind the arrow (main. stdcontrol->
Blinkm. stdcontrol), he is the provider, and he also uses timer and LEDs, because he is in front of the arrow (blinkm. timer and linkm. LEDs), he is the user. Singletimer and ledsc are both providers, because they are the Lib provided by the system, allowing you to control the flashing and time of the lamp.

 

Conclusion: Components in tinyos are also hierarchical. Components at the bottom layer are close to the hardware. They are encapsulated at the upper layer. configuration files are used during encapsulation. An application requires a top-level configuration file. at a higher level of all other configuration files, the file will be compiled first.

 

Step 3: Now that you have the top-level configuration file, you can write the module File.

Having a top-level configuration file is equivalent to having drawings in our house, you know how many floors should be built in our house, how many rooms are there on each floor, and where the bathroom and kitchen are located. Then the module File is paving the way for your program. Make it really accessible.

As mentioned above, blinkm provides the stdcontrol interface, using the timer interface of singletimer and LEDs interface of ledsc. Therefore, blinkm should write as follows:

Blinkm. NC
Module blinkm {
Provides {
Interface stdcontrol;
}
Uses {
Interface timer;
Interface LEDs;
}

}

We have previously said:

If a component provide an interface, all the commands in the interface must be implemented.

Currently, blinkm provide stdcontrol must provide all the commands of stdcontrol. They are Init (), start (), stop (). Then blinkm becomes:

 

Blinkm. NC
Module blinkm {
Provides {
Interface stdcontrol;
}
Uses {
Interface timer;
Interface LEDs;
}
Implementation {
Command result_t stdcontrol. INIT (){
Return success;
}
Command result_t stdcontrol. Start (){
}
Command result_t stdcontrol. Stop (){
}

}

 

Principle: In tinyos, to use a component (module), you must first initialize it.

 

Main is the entry to start the entire application. Of course, main can start the Modules connected to it. Who has main been associated? Yes, main, blinkm, and singletimer are associated. What are the interfaces associated with main? Yes, it's stdcontrol. As mentioned above, when calling Main. stdcontrol, it is equivalent to calling blinkm. stdcontrol and singletimer. stdcontrol. Then both blinkm and singletimer are started.

As you can see, the four components in the top-level configuration file, main, blinkm, and singletimer, are all started, and ledc is not initialized yet. However, the problem is that ledc does not provide the stdcontrol interface, so it cannot be started using the method associated with main. Observe the LEDs interface provided by ledsc and find that the LEDs Interface contains the init () command. Call LEDs. INIT () through command result_t stdcontrol. INIT () to initialize ledc.

 

Command result_t stdcontrol. INIT (){

LEDs. INIT ();
Return success;
}

 

So far, all components have been initialized. Blinkm also provides the stdcontrol interface. But there is another problem:

If a component uses an interface, it must implement the event in the interface.

Blinkm uses the LEDs interface and timer interface.

Check the LEDs and timer interfaces to see if there is any event. If there is any event, it must be implemented. It is observed that LEDs does not have an event, while the timer interface has an event.
Timer. NC
Interface timer {
Command result_t start (char type, uint32_t interval );
Command result_t stop ();
Event result_t fired ();
}
The timer interface has two commands and one event. The START () command is used to specify the timer type and the time interval that is about to expire. We use milliseconds to calculate the time interval. There are two available types: timer_repeat and timer_one_shot. When the timer_repeat mode is used, we can use the START () command to form a loop. After the specified interval, timer will end and the next duplicate timer will continue to be executed, until it is terminated by the stop () command. When an interval is reached, the event fired () will be triggered.

 

Consider the logical process of the program:

After all the required components are started, timer starts to record the time. After a time interval, the fired () event is triggered and the LED is controlled to flash the light.

 

Therefore, put the START () of Timer In the result_t stdcontrol. Start () of blinkm, and put the stop () of Timer In the result_t stdcontrol. Stop () of blinkm. So the final code is:

Blinkm. NC
Implementation {
Command result_t stdcontrol. INIT (){
Call LEDs. INIT ();
Return success;
}
Command result_t stdcontrol. Start (){
Return call timer. Start (timer_repeat, 1000 );
}
Command result_t stdcontrol. Stop (){
Return call timer. Stop ();
}
Event result_t timer. Fired ()
{
Call LEDs. redtoggle ();
Return success;
}
}

 

Here, a standard NESC program is almost clear.

 

 

 

Finally, the tossim method is provided to simulate the blink. The key is to show the running result of the tinyos program to students without mote:

 

1. Start

Under cygwin, go to the directory: C:/cygwin/opt/tinyos-1.x/apps/blink

Run the command: Make
PC

Then run the command: Export
Dbg = led

Last run: build/PC/main.exe
3 (here 3 refers to setting 3 sensor nodes)

You can view the node output on the console.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.