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

📄 power.sgml

📁 eCos操作系统源码
💻 SGML
📖 第 1 页 / 共 4 页
字号:
and another to specify the desired new state.<function>power_get_controller_attached</function> can be used todetermine whether or not a specific controller is currently attached.</para><para>The attached or detached state of a controller only affects whathappens during a global mode change, in other words following a callto <function>power_set_mode</function>. It is still possible tomanipulate a detached controller using<function>power_set_controller_mode</function> or<function>power_set_controller_mode_now</function>.</para></refsect1></refentry><!-- }}} --><!-- {{{ Implementing                   --><refentry id="power-controller"><refmeta><refentrytitle>Implementing a Power Controller</refentrytitle></refmeta><refnamediv><refname>Implementing a Power Controller</refname><refpurpose>adding power management support to device drivers andother packages</refpurpose></refnamediv><refsect1><title>Implementing a Power Controller</title><para>A system will have some number of power controllers. Usually therewill be one power controller for the cpu,<varname>power_controller_cpu</varname>, typically provided by one ofthe HAL packages and responsible for managing the processor itself andassociated critical components such as memory. Some or all of thedevice drivers will provide power controllers, allowing the powerconsumption of the associated devices to be controlled. There may besome arbitrary number of other controllers present in the system. Thepower management package does not impose any restrictions on thenumber or nature of the power controllers in the system, other thaninsisting that at most one <varname>power_controller_cpu</varname> beprovided.</para><para>Each power controller involves a single data structure of type<structname>PowerController</structname>, defined in the header file<filename class="headerfile">cyg/power/power.h</filename>. These datastructures should all be placed in the table<literal>__POWER__</literal>, so that the power management package andother code can easily locate all the controllers in the system. Thistable is constructed at link-time, avoiding code-size or run-timeoverheads. To facilitate this the package provides two macros whichshould be used to define a power controller,<literal>POWER_CONTROLLER()</literal> and<literal>POWER_CONTROLLER_CPU()</literal>.</para><para>The macro <literal>POWER_CONTROLLER</literal> takes four arguments:</para><orderedlist><listitem><para>A variable name. This can be used to access the power controllerdirectly, as well as via the table.</para></listitem><listitem><para>A priority. The table of power controllers is sorted, such that powercontrollers with a numerically lower priority come earlier in thetable. The special controller <varname>power_controller_cpu</varname>always comes at the end of the table. When moving from a high-powermode to a lower-powered mode, the power management package iteratesthrough the table from front to back. When moving to a higher-poweredmode the reverse direction is used. The intention is that the powercontroller for a software-only package such as a TCP/IP stack shouldappear near the start of the table, whereas the controllers for theethernet and similar devices would be near the end of the table. Hencewhen the policy module initiates a mode change to a lower-powered modethe TCP/IP stack gets a chance to cancel this mode change, before thedevices it depends on are powered down. Similarly when moving to ahigher-powered mode the devices will be re-activated before anysoftware that depends on those devices.</para><para>The header file <filenameclass="headerfile">cyg/power/power.h</filename> defines threepriorities <literal>PowerPri_Early</literal>,<literal>PowerPri_Typical</literal> and<literal>PowerPri_Late</literal>. For most controllers one of thesepriorities, possibly with a small number added or subtracted, willgive sufficient control. If an application developer is uncertainabout the relative priorities of the various controllers, a simple<link linkend="power-info-ids">test program</link> that iterates overthe table will quickly eliminate any confusion.</para></listitem><listitem><para>A constant string identifier. If the system has been configuredwithout support for such identifiers(<varname>CYGIMP_POWER_PROVIDE_STRINGS</varname>) then this identiferwill be discarded at compile-time. Otherwise it will be made availableto higher-level code using the function<function>power_get_controller_id</function>. </para></listitem><listitem><para>A function pointer. This will be invoked to perform actual modechanges, as described below.</para></listitem></orderedlist><para>A typical example of the use of the<literal>POWER_CONTROLLER</literal> macro would be as follows:</para><programlisting>#include &lt;pkgconf/system.h&gt;#ifdef CYGPKG_POWER# include &lt;cyg/power/power.h&gt;static voidxyzzy_device_power_mode_change(    PowerController* controller,    PowerMode        desired_mode,    PowerModeChange  change){   // Do the work}static POWER_CONTROLLER(xyzzy_power_controller, \                        PowerPri_Late,          \                        "xyzzy device",         \                        &amp;xyzzy_device_power_mode_change);#endif</programlisting><para>This creates a variable <varname>xyzzy_power_controller</varname>,which is a power controller data structure that will end up near theend of the table of power controllers. Higher-level code caniterate through this table and report the string <literal>"xyzzydevice"</literal> to the user. Whenever there is a mode changeoperation that affects this controller, the function<function>xyzzy_device_power_mode_change</function> will be invoked.The variable is declared static so this controller cannot bemanipulated by name in any other code. Alternatively, if the variablehad not been declared static other code could manipulate thiscontroller by name as well as through the table, especially if thepackage for the xyzzy device driver explicitly declared thisvariable in an exported header file. Obviously exporting the variableinvolves a slight risk of a name clash at link time.</para><para>The above code explicitly checks for the presence of the powermanagement package before including that package's header file orproviding any related functionality. Since power managementfunctionality is optional, such checks are recommended.</para><para>The macro <literal>POWER_CONTROLLER_CPU</literal> only takes twoarguments, a string identifier and a mode change function pointer.This macro always instantiates a variable<varname>power_controller_cpu</varname> so there is no need to providea variable name. The resulting power controller structure alwaysappears at the end of the table, so there is no need to specify apriority. Typical usage of the <literal>POWER_CONTROLLER_CPU</literal>macro would be:</para><programlisting>static voidwumpus_processor_power_mode_change(    PowerController* controller,    PowerMode        desired_mode,    PowerModeChange  change){   // Do the work}POWER_CONTROLLER_CPU("wumpus processor", \                     &amp;wumpus_processor_power_mode_change);</programlisting><para>This defines a power controller structure<varname>power_controller_cpu</varname>. It should not be declaredstatic since higher-level code may well want to manipulate the cpu'spower mode directly, and the variable is declared by the powermanagement package's header file.</para><para>Some care has to be taken to ensure that the power controllersactually end up in the final executable. If a power controllervariable ends up in an ordinary library and is never referenceddirectly then typically the linker will believe that the variable isnot needed and it will not end up in the executable. For eCos packagesthis can be achieved in the CDL, by specifying that the containingsource file should end up in <filename>libextras.a</filename> ratherthan the default <filename>libtarget.a</filename>:</para><programlisting>cdl_package CYGPKG_HAL_WUMPUS_ARCH {    &hellip;    compile -library=libextras.a data.c}</programlisting><para>If the file <filename>data.c</filename> instantiates a powercontroller this is now guaranteed to end up in the final executable,as intended. Typically HAL and device driver packages will alreadyhave some data that must not be eliminated by the linker, so they willalready contain a file that gets built into<filename>libextras.a</filename>. For power controllers defined insideapplication code it is important that the power controllers end up in<filename>.o</filename> object files rather than in<filename>.a</filename> library archive files.</para><para>All the real work of a power controller is done by the mode changefunction. If the power management package has been configured to use aseparate thread then this mode change function will be invoked by thatthread (except for the special case of <linklinkend="power-change-controller-now"><function>power_set_controller_mode_now</function></link>). If no separate thread is used then the mode change function will beinvoked directly by <function>power_set_mode</function> or<function>power_set_controller_mode</function>.</para><para>The mode change function will be invoked with three arguments. Thefirst argument identifies the power controller. Usually this argumentis not actually required since a given mode change function will onlyever be invoked for a single power controller. For example,<function>xyzzy_device_power_mode_change</function> will only ever beused in conjunction with <varname>xyzzy_power_controller</varname>.However there may be some packages which contain multiple controllers,all of which can share a single mode change function, and in that caseit is essential to identify the specific controller. The secondargument specifies the mode the controller should switch to, ifpossible: it will be one of <literal>PowerMode_Active</literal>,<literal>PowerMode_Idle</literal>, <literal>PowerMode_Sleep</literal>or <literal>PowerMode_Off</literal>. The final argument will be one of<literal>PowerModeChange_Controller</literal>,PowerModeChange_ControllerNow, or<literal>PowerModeChange_Global</literal>, and identifies the callthat caused this invocation. For example, if the mode change functionwas invoked because of a call to <function>power_set_mode</function>then this argument will be <literal>PowerModeChange_Global</literal>.It is up to each controller to decide how to interpret this finalargument. A typical controller might reject a global request to switchto <type>off</type> mode if the associated device is still busy, butif the request was aimed specifically at this controller then it couldinstead abort any current I/O operations and switch off the device.</para><para>The <structname>PowerController</structname> data structure containsone field, <structfield>mode</structfield>, that needs to be updatedby the power mode change function. At all times it should indicate thecurrent mode for this controller. When a mode change is requested thedesired mode is passed as the second argument. The exact operation ofthe power mode change function depends very much on what is beingcontrolled and the current circumstances, but some guidelines arepossible:</para><orderedlist><listitem><para>If the request can be satisfied without obvious detriment, do so andupdate the <structfield>mode</structfield> field. Reducing the powerconsumption of a device that is not currently being used is generallyharmless.</para></listitem><listitem><para>If a request is a no-op, for example if the system is switchingfrom <type>idle</type> to <type>sleep</type> mode and the controllerdoes not distinguish between these modes, simply act as if the requestwas satisfied.</para></listitem><listitem><para>If a request is felt to be unsafe, for example shutting down adevice that is still in use, then the controller may decideto reject this request. This is especially true if the request was aglobal mode change as opposed to one intended specifically for thiscontroller: in the latter case the policy module should be given duedeference. There are a number of ways in which a request can berejected:</para><orderedlist><listitem><para>If the request cannot be satisfied immediately but may be feasible ina short while, leave the <structfield>mode</structfield> fieldunchanged. Higher-level code in the policy module can interpret thisas a hint to retry the operation a little bit later. This approach isalso useful if the mode change can be started but will take some timeto complete, for example shutting down a socket connection, andadditional processing will be needed later on.</para></listitem><listitem><para>If the request is felt to be inappropriate, for example switching offa device that is still in use, the mode change function cancall <function>power_set_controller_mode</function> to reset thedesired mode for this controller back to the current mode.Higher-level code can then interpret this as a hint that there is moreactivity in the system than had been apparent.</para></listitem><listitem><para>For a global mode change, if the new mode is felt to be inappropriatethen the power controller can call <function>power_set_mode</function>to indicate this. An example of this would be the policy moduledeciding to switch off the whole unit while there is still I/Oactivity.</para></listitem></orderedlist></listitem></orderedlist><para>Mode change functions should not directly manipulate any other fieldsin the <structname>PowerController</structname> data structure. If itis necessary to keep track of additional data then static variablescan be used.</para><para>It should be noted that the above are only guidelines. Theirapplication in any given situation may be unclear. In addition thedetailed requirements of specific systems will vary, so even if thepower controller for a given device driver follows the aboveguidelines exactly it may turn out that slightly different behaviourwould be more appropriate for the actual system that is beingdeveloped. Fortunately the open source nature of<productname>eCos</productname> allows system developers to fine-tunepower controllers to meet their exact requirements.</para></refsect1></refentry><!-- }}} --><!-- /reference --></part>

⌨️ 快捷键说明

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