dsPIC33E/Driver_Core_Power_Management_V100.c
/// Description	: 
/// This is a driver for core digital power management.
/// The micro-controller receives power from a LDO voltage regulator with
/// ON/&OFF& control.  Normally this pin is pulled to GND and connected to a
/// pushbutton switch, called the "Power" switch. When the user presses the
/// power switch, the LDO will supply power to the micro-controller and the
/// system will start up.  In order to continue to function even after the
/// user releases the pushbutton switch, the micro-controller will assert
/// the ON/&OFF& control to logic high state. In addition, the
/// micro-controller will:
/// 1. Monitors the state of the Power switch.
/// 2. If the user presses the Power switch again for longer than 2 seconds,
/// this is recognized as shut down command and the system will shut itself
/// down by asserting the ON/&OFF& control to GND.
///
/// An I/O pin of the micro-controller will be dedicated to this driver.  This
/// pin, denoted PIN_DPSW will be bi-directional.  It is input when polling the
/// Power switch, and becomes output when asserting the ON/&OFF& control on
/// the LDO voltage regulator.  A large capacitor is connect to the ON/&OFF&
/// control, so the PIN_DPSW pin can momentarily become input to check the
/// status of the Power switch.
///

Description    :
This is a driver for core digital power management.
The micro-controller receives power from a LDO voltage regulator with
ON/&OFF& control.  Normally this pin is pulled to GND and connected to a
pushbutton switch, called the "Power" switch. When the user presses the
power switch, the LDO will supply power to the micro-controller and the
system will start up.  In order to continue to function even after the
user releases the pushbutton switch, the micro-controller will assert
the ON/&OFF& control to logic high state. In addition, the
micro-controller will:
1. Monitors the state of the Power switch.
2. If the user presses the Power switch again for longer than 2 seconds,
this is recognized as shut down command and the system will shut itself
down by asserting the ON/&OFF& control to GND.

An I/O pin of the micro-controller will be dedicated to this driver.  This
pin, denoted PIN_DPSW will be bi-directional.  It is input when polling the
Power switch, and becomes output when asserting the ON/&OFF& control on
the LDO voltage regulator.  A large capacitor is connect to the ON/&OFF&
control, so the PIN_DPSW pin can momentarily become input to check the
status of the Power switch.
Description : This is a driver for core digital power management . The micro - controller receives power from a LDO voltage regulator with ON / &OFF& control . Normally this pin is pulled to GND and connected to a pushbutton switch called the " Power " switch . When the user presses the power switch the LDO will supply power to the micro - controller and the system will start up . In order to continue to function even after the user releases the pushbutton switch the micro - controller will assert the ON / &OFF& control to logic high state . In addition the micro - controller will : 1 . Monitors the state of the Power switch . 2 . If the user presses the Power switch again for longer than 2 seconds this is recognized as shut down command and the system will shut itself down by asserting the ON / &OFF& control to GND . An I / O pin of the micro - controller will be dedicated to this driver . This pin denoted PIN_DPSW will be bi - directional . It is input when polling the Power switch and becomes output when asserting the ON / &OFF& control on the LDO voltage regulator . A large capacitor is connect to the ON / &OFF& control so the PIN_DPSW pin can momentarily become input to check the status of the Power switch .
void Proce_Core_Power_Control_Driver(TASK_ATTRIBUTE *ptrTask)
{
    static int nCount = 0;
    if (ptrTask->nTimer == 0)
    {
        switch (ptrTask->nState)
	{
            case 0: 
                TRISBbits.TRISB8 = 0;   
                PIN_DPSW = 1;
                PIN_DPSW = 1;
                gnProcessorPower = __CPU_NORMAL;        
                OSSetTaskContext(ptrTask, 1, 100);
                break;
            case 1: 
                TRISBbits.TRISB8 = 1;   
                TRISBbits.TRISB8 = 1;   
                OSSetTaskContext(ptrTask, 2, 250000/__SYSTEMTICK_US); 
                break;
            case 2: 
                if (PIN_DPSW == 1)      
                {                       
                    nCount++;           
                    if (nCount > 3)     
                    {
                        OSSetTaskContext(ptrTask, 3, 100000/__SYSTEMTICK_US);  
                    }
                    else
                    {
                        OSSetTaskContext(ptrTask, 0, 100000/__SYSTEMTICK_US);
                    }
                }
                else
                {
                    nCount = 0;             
                    OSSetTaskContext(ptrTask, 0, 100000/__SYSTEMTICK_US);
                }
                break;
            case 3: 
                PIN_ILED2 = 1;          
                gnProcessorPower = __CPU_POWERDOWN;
                OSSetTaskContext(ptrTask, 4, 300000/__SYSTEMTICK_US);
                break;
            case 4: 
                TRISBbits.TRISB8 = 0;   
                PIN_DPSW = 0;           
                PIN_DPSW = 0;
                OSSetTaskContext(ptrTask, 4, 100);
                break;
            default:
		OSSetTaskContext(ptrTask, 0, 1); 
            break;
	}
    }
}
void Proce_Core_Power_Control_Driver ( TASK_ATTRIBUTE * ptrTask ) { static int nCount = 0 ; if ( ptrTask -> nTimer == 0 ) { switch ( ptrTask -> nState ) { case 0 : TRISBbits . TRISB8 = 0 ; PIN_DPSW = 1 ; PIN_DPSW = 1 ; gnProcessorPower = __CPU_NORMAL ; OSSetTaskContext ( ptrTask , 1 , 100 ) ; break ; case 1 : TRISBbits . TRISB8 = 1 ; TRISBbits . TRISB8 = 1 ; OSSetTaskContext ( ptrTask , 2 , 250000 / __SYSTEMTICK_US ) ; break ; case 2 : if ( PIN_DPSW == 1 ) { nCount ++ ; if ( nCount > 3 ) { OSSetTaskContext ( ptrTask , 3 , 100000 / __SYSTEMTICK_US ) ; } else { OSSetTaskContext ( ptrTask , 0 , 100000 / __SYSTEMTICK_US ) ; } } else { nCount = 0 ; OSSetTaskContext ( ptrTask , 0 , 100000 / __SYSTEMTICK_US ) ; } break ; case 3 : PIN_ILED2 = 1 ; gnProcessorPower = __CPU_POWERDOWN ; OSSetTaskContext ( ptrTask , 4 , 300000 / __SYSTEMTICK_US ) ; break ; case 4 : TRISBbits . TRISB8 = 0 ; PIN_DPSW = 0 ; PIN_DPSW = 0 ; OSSetTaskContext ( ptrTask , 4 , 100 ) ; break ; default : OSSetTaskContext ( ptrTask , 0 , 1 ) ; break ; } } }
Description    :
This is a driver for core digital power management.
Description : This is a driver for core digital power management .
// State 0 - Power up, assert ON/&OFF& control pin (PIN_DPSW)
 // to logic high.
 // Set RB8 (PIN_DPSW) as output.
 // Inform all other process
 // that CPU is power up.
 // State 1 - Change PIN_DPSW pin to input and delay.  Allowing
 // a short delay for the voltage on the pin to drop
 // before starts sampling.
 // Set RB8 as input.
 // Set RB8 as input.
 // Delay for 0.25 second.
 // State 2 - Check input for Power push button pressed.
 // Power switch is pressed.
 // Delay for another and check again.
 // Counter will be incremented
 // continously if Power switch is pressed.
 // Check if counter threshold is exceeded.
 // Delay for 0.1 second.
 // Reset counter if Power switch is
 // not pressed.
 // State 3 - Issue shutdown notice to all processes.
 // Lights up indicator LED2.
 // State 4 - Shut down system.
 // Set RB8 as output.
 // Turn off system.
 // Back to state = 0, timer = 1.
