📄 power.sgml
字号:
<para>As a special case, it is possible for a power controller to call<function>power_set_mode</function> when invoked by the powermanagement thread. For example a power controller could decide that itis inappropriate for the system to go to sleep because the device itis associated with is still busy. The effect is as if the policymodule had called <function>power_set_mode</function> again before the mode change had completed.</para></listitem></orderedlist><para>If the power management package has been configured not to use aseparate thread then obviously the behaviour is somewhat different.The call to <function>power_set_mode</function> will now iterate overthe various power controllers immediately, rather than leaving this toa separate thread, and the whole mode change completes before<function>power_set_mode</function> returns. If some other thread or aDSR calls <function>power_set_mode</function> concurrently thebehaviour of the system is undefined. However, it is still legal for apower controller to call <function>power_set_mode</function>:effectively this is a recursive call; it is detected by the system,and internal state is updated; the recursive<function>power_set_mode</function> call now returns, and when thepower controller returns back to the original<function>power_set_mode</function> call it detects what has happened,aborts the previous mode change, and starts a new mode change asrequested by the controller.</para><para><function>power_set_mode</function> is normally invoked from threadcontext. If a separate power management thread is used it can beinvoked safely from DSR context. If the system is configured not touse such a thread, it may or may not be safe to invoke this functionfrom DSR context: essentially the function just iterates throughthe various power controllers, and the documentation or source code ofeach controller present in the current system will have to be examinedto determine whether or not this can happen safely in DSR context.<function>power_set_mode</function> should never be invoked fromISR context.</para></refsect1><refsect1 id="power-change-controller"><title>Manipulating an Individual Power Controller</title><para>In some cases it is desirable to set the power mode of an individualcontroller separately from the mode for the system as a whole. Forexample if a device is not currently being used then the associatedpower controller could be set to <literal>PowerMode_Off</literal>,even while the system as a whole is still active. This can be achievedby calling the function<function>power_set_controller_mode</function>. It takes twoarguments: the first identifies a particular controller; the secondspecifies the desired new power mode for that controller. The functionoperates in much the same way as <function>power_set_mode</function>,for example if a separate power management thread is being used then<function>power_set_controller_mode</function> operates bymanipulating some internal state and waking up that thread. Thelimitations are also much the same as for<function>power_set_mode</function>, so for example<function>power_set_controller_mode</function> should not be invokedfrom inside ISRs.</para><para>Manipulating individual controllers is often used in conjunction withthe function <linklinkend="power-attached"><function>power_set_controller_attached</function></link>,allowing the policy module to specify which controllers are affectedby global mode changes.</para></refsect1><refsect1 id="power-change-controller-now"><title>Direct Manipulation of a Power Controller</title><para>In exceptional circumstances it may be necessary to invoke a powercontroller directly, bypassing the power management thread andhigher-level functionality such as <linklinkend="power-policy-callback">callback functions</link>. Thefunction <function>power_set_controller_mode_now</function> allowsthis. It takes two arguments, a controller and a mode, just like<function>power_set_controller_mode</function>.</para><para>Use of <function>power_set_controller_mode_now</function> isdangerous. For example no attempt is made to synchronise with anyother power mode changes that might be happening concurrently. Apossible use is when the system gets woken up out of<type>sleep</type> mode: depending on the hardware, on which powercontrollers are present, and on the application code it may benecessary to wake up some power controllers immediately before thesystem as a whole is ready to run again.</para></refsect1></refentry><!-- }}} --><!-- {{{ Policy support --><refentry id="power-policy"><refmeta><refentrytitle>Support for Policy Modules</refentrytitle></refmeta><refnamediv><refname>Support for Policy Modules</refname><refpurpose>closer integration with higher-level code</refpurpose></refnamediv><refsynopsisdiv><funcsynopsis><funcsynopsisinfo>#include <cyg/power/power.h></funcsynopsisinfo><funcprototype> <funcdef> void <function>power_set_policy_callback</function> </funcdef> <paramdef> void (*)(PowerController*, PowerMode, PowerMode, PowerMode, PowerMode) <parameter>callback</parameter> </paramdef></funcprototype><funcprototype> <funcdef> void (*)(PowerController*, PowerMode, PowerMode, PowerMode, PowerMode) <function>power_get_policy_callback</function> </funcdef> <void></funcprototype><funcprototype> <funcdef> CYG_ADDRWORD <function>power_get_controller_policy_data</function> </funcdef> <paramdef> PowerController* <parameter>controller</parameter> </paramdef></funcprototype><funcprototype> <funcdef> void <function>power_set_controller_policy_data</function> </funcdef> <paramdef> PowerController* <parameter>controller</parameter> </paramdef> <paramdef> CYG_ADDRWORD <parameter>data</parameter> </paramdef></funcprototype></funcsynopsis></refsynopsisdiv><refsect1 id="power-policy-callback"><title>Policy Callbacks</title><para>The use of a separate thread to perform power mode changes in typicalconfigurations can cause problems for some policy modules.Specifically, the policy module can request a mode change for thesystem as a whole or for an individual controller, but it does notknow when the power management thread actually gets scheduled to runagain and carry out the request. Although it would be possible for thepolicy module to perform some sort of polling, in general that isundesirable.</para><para>To avoid such problems the policy module can install a callbackfunction using <function>power_set_policy_callback</function>. Thecurrent callback function can be retrieved using<function>power_get_policy_callback</function>. If a callback functionhas been installed then it will be called by the power managementpackage whenever a power controller has been invoked to perform a modechange. The callback will be called in the context of the powermanagement thread, so usually it will have to make use of threadsynchronisation primitives to interact with the main policy module. Itis passed five arguments:</para><orderedlist><listitem><para>The power controller that has just been invoked to perform a modechange. </para></listitem><listitem><para>The mode this controller was running at before the invocation.</para></listitem><listitem><para>The current mode this controller is now running at.</para></listitem><listitem><para>The desired mode before the power controller was invoked. Usually thiswill be the same as the current mode, unless the controller hasdecided for some reason that this was inappropriate.</para></listitem><listitem><para>The current desired mode. This will differ from the previous argumentonly if there has was another call to<function>power_set_mode</function> or<function>power_set_controller_mode</function> while the powercontroller was being invoked, probably by the power controller itself.</para></listitem></orderedlist><para>A simple example of a policy callback function would be:</para><programlisting>static voidpower_callback( PowerController* controller, PowerMode old_mode, PowerMode new_mode, PowerMode old_desired_mode, powerMode new_desired_mode){ printf("Power mode change: %s, %s -> %d\n", power_get_controller_id(controller), mode_to_string(old_mode), mode_to_string(new_mode)); CYG_UNUSED_PARAM(PowerMode, old_desired_mode); CYG_UNUSED_PARAM(PowerMode, new_desired_mode);}intmain(int argc, char** argv){ … power_set_policy_callback(&power_callback); …}</programlisting><para>If <function>power_set_controller_mode_now</function> is used tomanipulate an individual controller the policy callback will not beinvoked. This function may get called from any context including DSRs,and even if there is already a call to the policy callback happeningin some other context, so invoking the callback would usually beunsafe.</para><para>If the power management package has not been configured to use aseparate thread then <function>power_set_mode</function> and<function>power_set_controller_mode</function> will manipulate thepower controllers immediately and invoke the policy callbackafterwards. Therefore the policy callback will typically run in thesame context as the main policy module.</para></refsect1><refsect1 id="power-policy-data"><title>Policy-specific Controller Data</title><para>Some policy modules may want to associate some additional data witheach power controller. This could be achieved by for examplemaintaining a hash table or similar data structure, but forconvenience the power management package allows higher-level code,typically the policy module, to store and retrieve one word of data ineach power controller. The function<function>power_set_controller_policy_data</function> takes twoarguments, a pointer to a power controller and a<type>CYG_ADDRWORD</type> of data: by appropriate use of casts thisword could be an integer or a pointer to some data structure. Thematching function<function>power_get_controller_policy_data</function> retrieves theword previously installed, and can be cast back to an integer orpointer. The default value for the policy data is 0.</para><para>For example the following code fragment stores a simple index value ineach power controller. This could then be retrieved by the policycallback.</para><programlisting> unsigned int i = 0; PowerController* controller; for (controller = &(__POWER__[0]); controller != &(__POWER_END__); controller++) { power_set_controller_policy_data(controller, (CYG_ADDRWORD) i++); }</programlisting><para>Not all policy modules will require per-controller data. Theconfiguration option<varname>CYGIMP_POWER_PROVIDE_POLICY_DATA</varname> can be used tocontrol this functionality, thus avoiding wasting a small amount ofmemory inside each power controller structure.</para></refsect1></refentry><!-- }}} --><!-- {{{ Attached/detached controllers --><refentry id="power-attached"><refmeta><refentrytitle>Attached and Detached Controllers</refentrytitle></refmeta><refnamediv><refname>Attached and Detached Controllers</refname><refpurpose>control which power controllers are affected by global changes</refpurpose></refnamediv><refsynopsisdiv><funcsynopsis><funcsynopsisinfo>#include <cyg/power/power.h></funcsynopsisinfo><funcprototype> <funcdef> cyg_bool <function>power_get_controller_attached</function> </funcdef> <paramdef> PowerController* <parameter>controller</parameter> </paramdef></funcprototype><funcprototype> <funcdef> void <function>power_set_controller_attached</function> </funcdef> <paramdef> PowerController* <parameter>controller</parameter> </paramdef> <paramdef> cyg_bool <parameter>new_state</parameter> </paramdef></funcprototype></funcsynopsis></refsynopsisdiv><refsect1><title>Detaching Power Controllers</title><para>By default the global operation <function>power_set_mode</function>affects all power controllers. There may be circumstances when this isnot desirable. For example if a particular device is not currentlybeing used then it can be left switched off: the rest of the systemcould be moving between <type>active</type>, <type>idle</type> and<type>sleep</type> modes, but there is no point in invoking the powercontroller for the unused device. To support this the power managementpackage supports the concept of attached and detached controllers. Bydefault all controllers are attached, and hence will be affected byglobal mode changes. A specific controller can be detached using thefunction <function>power_set_controller_attached</function>. Thisfunction takes two arguments, one to specify a particular controller
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -