Comtrol eCos User Manual
Page 674

Implementing a Power Controller
#include
<
pkgconf/system.h
>
#ifdef CYGPKG_POWER
# include
<
cyg/power/power.h
>
static void
xyzzy_device_power_mode_change(
PowerController* controller,
PowerMode
desired_mode,
PowerModeChange
change)
{
// Do the work
}
static POWER_CONTROLLER(xyzzy_power_controller, \
PowerPri_Late,
\
"xyzzy device",
\
&xyzzy_device_power_mode_change);
#endif
This creates a variable
xyzzy_power_controller
, which is a power controller data structure that will end up
near the end of the table of power controllers. Higher-level code can iterate through this table and report the string
"xyzzy device"
to the user. Whenever there is a mode change operation that affects this controller, the function
xyzzy_device_power_mode_change
will be invoked. The variable is declared static so this controller cannot be
manipulated by name in any other code. Alternatively, if the variable had not been declared static other code could
manipulate this controller by name as well as through the table, especially if the package for the xyzzy device
driver explicitly declared this variable in an exported header file. Obviously exporting the variable involves a slight
risk of a name clash at link time.
The above code explicitly checks for the presence of the power management package before including that pack-
age’s header file or providing any related functionality. Since power management functionality is optional, such
checks are recommended.
The macro
POWER_CONTROLLER_CPU
only takes two arguments, a string identifier and a mode change function
pointer. This macro always instantiates a variable
power_controller_cpu
so there is no need to provide a variable
name. The resulting power controller structure always appears at the end of the table, so there is no need to specify
a priority. Typical usage of the
POWER_CONTROLLER_CPU
macro would be:
static void
wumpus_processor_power_mode_change(
PowerController* controller,
PowerMode
desired_mode,
PowerModeChange
change)
{
// Do the work
}
POWER_CONTROLLER_CPU("wumpus processor", \
&wumpus_processor_power_mode_change);
This defines a power controller structure
power_controller_cpu
. It should not be declared static since higher-
level code may well want to manipulate the cpu’s power mode directly, and the variable is declared by the power
management package’s header file.
570