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

📄 powermanage.c

📁 philips arm7 lpc2103的常用功能的库函数第二部分
💻 C
字号:
/***********************************************Copyright (c)*********************************************
**                                Guangzou ZLG-MCU Development Co.,LTD.
**                                     
**                                       http://www.zlgmcu.com
**
**--------------File Info---------------------------------------------------------------------------------
** File name:               PowerManage.c
** Last modified Date:      2007-11-09
** Last Version:		    2.0
** Descriptions:		    电源管理模块 函数实现,采用设备描述符来操作
**
**--------------------------------------------------------------------------------------------------------
** Created by:              WangGuoguang
** Created date:		    2007-11-08
** Version:	                1.0
** Descriptions:		    LPC22xx 系列软件库 电源管理功能软件包.
**
**--------------------------------------------------------------------------------------------------------
** Modified by:             WangGuoguang 
** Modified Date:		    2007-11-09
** Version:	                2.0
** Descriptions:		    完善功能,并按代码新规范修改.
**
*********************************************************************************************************/
#include "config.h"                                                     /*  系统头文件                  */ 
#include "PowerManagePrivate.h"                                         /*  软件包内部私有头文件        */              
#include "PowerManage.h"                                                /*  软件包内部模块头文件        */

/*********************************************************************************************************
   裁剪是否使用此软件包,当 PowerManage_FUN_LIB =0,从而不编译. 
*********************************************************************************************************/
#if     POWERMANAGE_FUN_LIB > 0 

/*********************************************************************************************************
   定义操作 ADC 器件的结构体,有多个器件就需要声明多个结构体
*********************************************************************************************************/
static  __POWER_INFO     __Power_Data;
 
/*********************************************************************************************************
   定义指针数组(结构体指针数组)来保存指向电源管理模块信息结构体的指针
*********************************************************************************************************/
const   __PPOWER_INFO    __GpPowerInfoData[__POWER_MAX_NUM] = {&__Power_Data};

/*********************************************************************************************************
   电源管理模块基地址值,如果有多器件可以在该位置添加相应的基地址即可
*********************************************************************************************************/
const   uint32           __GuiPowerBaseAddrTab[__POWER_MAX_NUM] = {SCB_BASE_ADDR};

/*********************************************************************************************************
   模数转换器基地址值,如果有多个ADC(ADC0 ,ADC1 ...),器件可以在该位置添加相应的基地址即可
*********************************************************************************************************/
const   uint32          __GuiPAdcBaseAddrTab[__PADC_NUM] = {AD0_BASE_ADDR };

/*********************************************************************************************************
** Function name:         __powerInit
**
** Descriptions:          初始化设备结构体
** Input parameters:      pRsv    --保留指针参数,可输入NULL.
**
** Returned value:        pRsv    --指针参数,留作扩展运用
**
*********************************************************************************************************/
static void*    __powerInit(void *pRsv) 

{
    static uint32 uiMutex = FALSE;                                      /*  定义函数重复调用互斥变量    */
	pRsv = pRsv ;                                                       /*  防止编译器警告              */
	
    /*
     *  设备信息结构体初始化
     */
   IRQDisable();                                                        /*  关中断                      */  
   if (uiMutex == FALSE) { 
	   uiMutex = TRUE;                                                  /*  记录已初始化过结构体信息    */
	   __GpPowerInfoData[__POWER_NUM]->uiOffsetBase     = __POWER_OFFSET_BASE;
	   __GpPowerInfoData[__POWER_NUM]->puiPowerAddrBase = (uint32*)__GuiPowerBaseAddrTab[__POWER_NUM];
	   __GpPowerInfoData[__POWER_NUM]->puiAdcAddrBase   = (uint32*)__GuiPAdcBaseAddrTab[__PADC_NUM - 1];
   } 
   IRQEnable();                                                         /*  开中断                      */  
   
   return (pRsv);                                                       /*  返回指针参数                */
}

/*********************************************************************************************************
** Function name:         lowPowerEnter
**d
** Descriptions:          设置处理器进入低功耗模式
** Input parameters:      uiCtrl  --控制模式宏参数
**                                  关键字:  POWER_IDLE  空闲模式
**                                           POWER_DOWN  掉电模式
**                        pRsv    --保留指针参数,可输入NULL.
**
** Output parameters:     OPERATE_FAIL    -- 参数错误
** Returned value:        OPERATE_SUCCESS -- 操作成功
**
*********************************************************************************************************/
uint32    lowPowerEnter (uint32 uiCtrl, char *pRsv)

{
    volatile uint32  uiOffsetBase;
    volatile uint32 *puiBaseAddr;

    if ((uiCtrl < POWER_IDLE) || (uiCtrl > POWER_DOWN)) {
        __GpPowerInfoData[__POWER_NUM]->uiPowerState = POWER_NOMAL;
        return (uint32)(OPERATE_FAIL); 
           
    }
   
	pRsv = pRsv ;                                                       /*  为了防止编译器警告          */
   
    __powerInit(pRsv);
    uiOffsetBase = __GpPowerInfoData[__POWER_NUM]->uiOffsetBase;
    puiBaseAddr  = __GpPowerInfoData[__POWER_NUM]->puiPowerAddrBase;
    
    if (uiCtrl == POWER_DOWN) {
        IRQDisable();                                                   /*  关中断                      */  
        puiBaseAddr[__B_POWER_PCON << uiOffsetBase] |= (0x01 << 1);     /*  掉电模式                    */
        __GpPowerInfoData[__POWER_NUM]->uiPowerState = POWER_DOWN;

    } else {
        IRQDisable();                                                   /*  关中断                      */  
        puiBaseAddr[__B_POWER_PCON << uiOffsetBase] |= (0x01 << 0);     /*  空闲模式                    */
        __GpPowerInfoData[__POWER_NUM]->uiPowerState = POWER_IDLE;
    }
    IRQEnable();                                                        /*  开中断                      */  
    return (uint32)(OPERATE_SUCCESS); 

}

/*********************************************************************************************************
** Function name:         partPowerManage
**
** Descriptions:          设置功能部件功率控制
**
** Input parameters:      uiPartID --具体见 PowerManage.h 头文件宏定义.例:PCTIM0,PCURT0.....
**                        uiCtrl   --控制模式宏参数
**                                   关键字:  PART_POWER_ON   正常模式
**                                            PART_POWER_OFF  节电模式
**                        pRsv     --保留指针参数,可输入NULL.
**
** Output parameters:     OPERATE_FAIL    -- 参数错误  ;  PART_RSV  -- 保留状态位.
** Returned value:        OPERATE_SUCCESS -- 操作成功
**
** Notice :               要调用此函数,必须确保,不是在掉电模式,或是空闲模式的前提下,即正常工作模式时进行.
*********************************************************************************************************/
uint32    partPowerManage (uint32 uiPartID, 
                           uint32 uiCtrl,
                           char  *pRsv) 
{
             uint32  uiRet;
    volatile uint32  uiOffsetBase;
    volatile uint32 *puiPowerBaseAddr;
    volatile uint32 *puiAdcBaseAddr;
    
    uiRet = (uint32)(OPERATE_FAIL);
    
    /*
     *  参数过滤
     */
    if ((uiCtrl >= PART_POWER_ON) && (uiCtrl <= PART_POWER_OFF)) {
	    if ((uiPartID >= PCTIM0) && (uiPartID <= PCAD)) {
		
			if (uiPartID == PCRSV) {                                    /*  如果是保留位,错误返回       */
               uiRet = PART_RSV;
               return (uiRet); 
			}
           
            IRQDisable();                                               /*  关中断                      */  
            if (__GpPowerInfoData[__POWER_NUM]->uiPowerState == POWER_DOWN) {
                uiRet = (uint32)(OPERATE_SUCCESS);
	            return (uiRet); 
            }
            IRQEnable();                                                /*  开中断                      */  
		
			pRsv = pRsv ;                                               /*  为了防止编译器警告          */
		   
		    __powerInit(pRsv);
		    
		    uiOffsetBase      = __GpPowerInfoData[__POWER_NUM]->uiOffsetBase;
		    puiPowerBaseAddr  = __GpPowerInfoData[__POWER_NUM]->puiPowerAddrBase;
		    puiAdcBaseAddr    = __GpPowerInfoData[__POWER_NUM]->puiAdcAddrBase;
		    
		    /*
		     *  功能部件节电模式设置
		     */
            IRQDisable();                                               /*  关中断                      */  
		    if (uiCtrl == PART_POWER_OFF) {
		        __PART_POWER_OFF(uiPartID);
		    } else {
		        __PART_POWER_ON(uiPartID);
		    }
		    IRQEnable();                                                /*  开中断                      */  
		    
	        uiRet = (uint32)(OPERATE_SUCCESS);
	        return (uiRet); 
	    }
    }
    return (uiRet); 

}

/*********************************************************************************************************
** Function name:         partGetPowerState
**
** Descriptions:          读取功能部件功率控制状态
**
** Input parameters:      uiPartID --具体见 PowerManage.h 头文件宏定义.例:PCTIM0,PCURT0.....
**                        pRsv     --保留指针参数,可输入NULL.
**
** Output parameters:     OPERATE_FAIL               -- 参数错误
** Returned value:        对应功能部件的功率控制状态 -- 操作成功 
**                                                20-PART_POWER_OFF: 节电状态 ;
**                                                21-PART_POWER_ON : 正常状态 ;
**                                                30-PART_RSV      : 保留状态位. 
**
** Notice :               在空闲与掉电模式下,处理器指令的执行都被挂起,而直至发生复位或中断唤醒事件为止.
**                        此时,不能通过指令来执行功率控制模式的查询,而只能查询相关功能部件节电状态.
*********************************************************************************************************/
uint32    partGetPowerState (uint32 uiPartID, char *pRsv) 

{
             uint32  uiRet;
    volatile uint32  uiOffsetBase;
    volatile uint32 *puiBaseAddr;
    
    if ((uiPartID >= PCTIM0) && (uiPartID <= PCAD)) {
		
	    if (uiPartID == PCRSV) {                                        /*  如果是保留位,错误返回       */
	        return (uint32)(PART_RSV); 
		}

        pRsv = pRsv ;                                                   /*  为了防止编译器警告          */
   
        __powerInit(pRsv);
        uiOffsetBase = __GpPowerInfoData[__POWER_NUM]->uiOffsetBase;
        puiBaseAddr  = __GpPowerInfoData[__POWER_NUM]->puiPowerAddrBase;
                                                                        /*  获取外设电源使用状态        */
        uiRet = ((puiBaseAddr[__B_POWER_PCONP << uiOffsetBase] >> uiPartID) & 0x01);
        return (uiRet == 0 ? PART_POWER_OFF : PART_POWER_ON);           /*  状态返回                    */
    }
    return (uint32)(OPERATE_FAIL);
}


#endif                                                                  /*  POWERMANAGE_FUN_LIB         */
/*********************************************************************************************************
  END FILE
*********************************************************************************************************/                    

⌨️ 快捷键说明

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