⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clocks-and-alarm-handlers.html

📁 有关ecos2。0介绍了实时嵌入式的结构以及线程调度的实现和内存的管理等
💻 HTML
字号:
<!-- Copyright (C) 2003 Red Hat, Inc.                                --><!-- This material may be distributed only subject to the terms      --><!-- and conditions set forth in the Open Publication License, v1.0  --><!-- or later (the latest version is presently available at          --><!-- http://www.opencontent.org/openpub/).                           --><!-- Distribution of the work or derivative of the work in any       --><!-- standard (paper) book form is prohibited unless prior           --><!-- permission is obtained from the copyright holder.               --><HTML><HEAD><TITLE>More Features &#8212; Clocks and AlarmHandlers</TITLE><meta name="MSSmartTagsPreventParsing" content="TRUE"><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="eCos User Guide"HREF="ecos-user-guide.html"><LINKREL="UP"TITLE="Programming With eCos"HREF="user-guide-programming.html"><LINKREL="PREVIOUS"TITLE="A Sample Program with Two Threads"HREF="sample-twothreads.html"><LINKREL="NEXT"TITLE="The eCos Configuration Tool"HREF="the-ecos-configuration-tool.html"></HEAD><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">eCos User Guide</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="sample-twothreads.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="the-ecos-configuration-tool.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="CLOCKS-AND-ALARM-HANDLERS">Chapter 14. More Features &#8212; Clocks and AlarmHandlers</H1><P>If a program wanted to execute a task at a given time, orperiodically, it could do it in an inefficient way by sitting in aloop and checking the real-time clock to see if the proper amount oftime has elapsed. But operating systems usually provide system callswhich allow the program to be informed at the desired time.</P><P><SPANCLASS="PRODUCTNAME">eCos</SPAN> provides a rich timekeeping formalism, involving<SPANCLASS="emphasis"><ICLASS="EMPHASIS">counters</I></SPAN>, <SPANCLASS="emphasis"><ICLASS="EMPHASIS">clocks</I></SPAN>,<SPANCLASS="emphasis"><ICLASS="EMPHASIS">alarms</I></SPAN>, and <SPANCLASS="emphasis"><ICLASS="EMPHASIS">timers</I></SPAN>.  Theprecise definition, relationship, and motivation of these features isbeyond the scope of this tutorial, but these examples illustrate howto set up basic periodic tasks.</P><P>Alarms are events that happen ata given time, either once or periodically. A thread associates analarm handling function with the alarm, so that the function willbe invoked every time the alarm &#8220;goes off&#8221;.</P><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="SAMPLE-ALARMS">A Sample Program with Alarms</H1><P><TTCLASS="FILENAME">simple-alarm.c</TT> (inthe examples directory) is a short program that creates a thread thatcreates an alarm. The alarm is handled by the function<TTCLASS="FUNCTION">test_alarm_func()</TT>, which sets a globalvariable. When the main thread of execution sees that the variable haschanged, it prints a message.</P><DIVCLASS="EXAMPLE"><ANAME="AEN910"><P><B>Example 14-1. A sample program that creates an alarm</B></P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">/* this is a very simple program meant to demonstrate a basic use of time, alarms and alarm-handling functions  in eCos */#include &lt;cyg/kernel/kapi.h&#62;#include &lt;stdio.h&#62;#define NTHREADS 1#define STACKSIZE 4096static cyg_handle_t thread[NTHREADS];static cyg_thread thread_obj[NTHREADS];static char stack[NTHREADS][STACKSIZE];static void alarm_prog( cyg_addrword_t data );/* we install our own startup routine which sets up  threads and starts the scheduler */void cyg_user_start(void){ cyg_thread_create(4, alarm_prog, (cyg_addrword_t) 0,	"alarm_thread", (void *) stack[0],	STACKSIZE, &amp;thread[0], &amp;thread_obj[0]); cyg_thread_resume(thread[0]);}/* we need to declare the alarm handling function (which is defined below), so that we can pass it to  cyg_alarm_initialize() */cyg_alarm_t test_alarm_func;/* alarm_prog() is a thread which sets up an alarm which is then handled by test_alarm_func() */static void alarm_prog(cyg_addrword_t data){ cyg_handle_t test_counterH, system_clockH, test_alarmH; cyg_tick_count_t ticks; cyg_alarm test_alarm; unsigned how_many_alarms = 0, prev_alarms = 0, tmp_how_many; system_clockH = cyg_real_time_clock(); cyg_clock_to_counter(system_clockH, &amp;test_counterH); cyg_alarm_create(test_counterH, test_alarm_func,	(cyg_addrword_t) &amp;how_many_alarms,	&amp;test_alarmH, &amp;test_alarm); cyg_alarm_initialize(test_alarmH, cyg_current_time()+200, 200); /* get in a loop in which we read the current time and    print it out, just to have something scrolling by */ for (;;) {   ticks = cyg_current_time();   printf("Time is %llu\n", ticks);   /* note that we must lock access to how_many_alarms, since the   alarm handler might change it. this involves using the   annoying temporary variable tmp_how_many so that I can keep the   critical region short */   cyg_scheduler_lock();   tmp_how_many = how_many_alarms;   cyg_scheduler_unlock();   if (prev_alarms != tmp_how_many) {     printf(" --- alarm calls so far: %u\n", tmp_how_many);     prev_alarms = tmp_how_many;   }   cyg_thread_delay(30); }}/* test_alarm_func() is invoked as an alarm handler, so   it should be quick and simple. in this case it increments   the data that is passed to it. */void test_alarm_func(cyg_handle_t alarmH, cyg_addrword_t data){ ++*((unsigned *) data);}</PRE></TD></TR></TABLE></DIV><P>When you run this program (by typing <BCLASS="COMMAND">continue</B> atthe (<SPANCLASS="emphasis"><ICLASS="EMPHASIS">gdb</I></SPAN>) prompt) the output should look likethis:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="SCREEN">Starting program: <TTCLASS="REPLACEABLE"><I>BASE_DIR</I></TT>/examples/simple-alarm.exeTime is 0Time is 30Time is 60Time is 90Time is 120Time is 150Time is 180Time is 210  --- alarm calls so far: 1Time is 240Time is 270Time is 300Time is 330Time is 360Time is 390Time is 420  --- alarm calls so far: 2Time is 450Time is 480</PRE></TD></TR></TABLE><DIVCLASS="NOTE"><BLOCKQUOTECLASS="NOTE"><P><B>Note: </B>When running in a simulator the  delaysmight be quite long. On a hardware board (where the clock speed is 100ticks/second) the delays should average to about 0.3 seconds (and 2seconds between alarms). In simulation, the delay will depend on thespeed of the host processor and will almost always be much slower thanthe actual board. You might want to reduce the delay parameter whenrunning in simulation.</P></BLOCKQUOTE></DIV><P>Here are a few things you might notice about this program:</P><P></P><UL><LI><P>It used the <TTCLASS="FUNCTION">cyg_real_time_clock()</TT> function;this always returns a handle to the default system real-time  clock. </P></LI><LI><P>Clocks are based on  counters, so the function <TTCLASS="FUNCTION">cyg_alarm_create()</TT>uses a counter handle. The program used the function<TTCLASS="FUNCTION">cyg_clock_to_counter()</TT> to strip the clock handleto the underlying counter handle. </P></LI><LI><P>Once the alarm is created it is initialized with <TTCLASS="FUNCTION">cyg_alarm_initialize()</TT>, whichsets the time at which the alarm should go off, as well as the periodfor repeating alarms. It is set to go off at the current time andthen to repeat every 200 ticks. </P></LI><LI><P>The alarm handler function<TTCLASS="FUNCTION">test_alarm_func()</TT> conforms to the guidelines forwriting alarm handlers and other  delayed service routines: it does not invoke anyfunctions which might lock the scheduler.  This is discussed in detailin the <ICLASS="CITETITLE"><SPANCLASS="PRODUCTNAME">eCos</SPAN> Reference Manual</I>, in the chapter<ICLASS="CITETITLE">The <SPANCLASS="PRODUCTNAME">eCos</SPAN> Kernel</I>.</P></LI><LI><P>There is a <SPANCLASS="emphasis"><ICLASS="EMPHASIS">critical region</I></SPAN> in this program:the variable <TTCLASS="LITERAL">how_many_alarms</TT> is accessed in themain thread of control and is also modified in the alarm handler. Toprevent a possible (though unlikely) race condition on this variable,access to <TTCLASS="LITERAL">how_many_alarms</TT> in the principal threadis protected by calls to <TTCLASS="FUNCTION">cyg_scheduler_lock()</TT> and<TTCLASS="FUNCTION">cyg_scheduler_unlock()</TT>. When the scheduler islocked, the alarm handler will not be invoked, so the problem isaverted. </P></LI></UL></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="sample-twothreads.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="ecos-user-guide.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="the-ecos-configuration-tool.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">A Sample Program with Two Threads</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="user-guide-programming.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">The eCos Configuration Tool</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -