📄 lh7a400_dcdc_driver.c
字号:
/***********************************************************************
* $Workfile: LH7A400_dcdc_driver.c $
* $Revision: 1.1 $
* $Author: FergisJ $
* $Date: Aug 02 2002 09:13:24 $
*
* Project: LH7A400 EVB
*
* Description:
* Pulse Width Modulation (PWM) driver for the LH7A400.
* (Direct Current to Direct Current Converter Interface)
*
* See driver Header File (LH7A400_dcdc_driver.h) for more details.
*
*
* References:
* (1) LH7A400 User's Guide
*
* Revision History:
* $Log: //smaicnt2/pvcs/VM/CHIPS/archives/LH7A400/DCDC/Drivers/LH7A400_dcdc_driver.c-arc $
*
* Rev 1.1 Aug 02 2002 09:13:24 FergisJ
* Updated per C-Standard ... Modified some constant names for better understanding.
*
* Rev 1.0 Jul 30 2002 08:41:14 FergisJ
* Initial revision.
*
*
*
***********************************************************************
* SHARP MICROELECTRONICS OF THE AMERICAS MAKES NO REPRESENTATION
* OR WARRANTIES WITH RESPECT TO THE PERFORMANCE OF THIS SOFTWARE,
* AND SPECIFICALLY DISCLAIMS ANY RESPONSIBILITY FOR ANY DAMAGES,
* SPECIAL OR CONSEQUENTIAL, CONNECTED WITH THE USE OF THIS SOFTWARE.
*
* SHARP MICROELECTRONICS OF THE AMERICAS PROVIDES THIS SOFTWARE SOLELY
* FOR THE PURPOSE OF SOFTWARE DEVELOPMENT INCORPORATING THE USE OF A
* SHARP MICROCONTROLLER OR SYSTEM-ON-CHIP PRODUCT. USE OF THIS SOURCE
* FILE IMPLIES ACCEPTANCE OF THESE CONDITIONS.
*
* COPYRIGHT (C) 2002 SHARP MICROELECTRONICS OF THE AMERICAS, INC.
* CAMAS, WA
**********************************************************************/
/***********************************************************************
* Public user header files (#include)
**********************************************************************/
#include "LH7A400_dcdc_driver.h"
/***********************************************************************
* Constant definitions (#define)
**********************************************************************/
/*
* BIT MANIPULATION DEFINITIONS
*
* These definitions will work for either the duty-cycle
* or the clock-prescale registers.
*/
/*
* PWM0 & PWM1, HIGH & LOW MASKS
*/
// PWM0
#define PWM0_ALL_DATA_MASK (_BITMASK(8))
#define PWM0_HIGH_DATA_MASK (_BITMASK(4))
#define PWM0_LOW_DATA_MASK (_SBF(4, _BITMASK(4)))
// PWM1
#define PWM1_ALL_DATA_MASK ~(_BITMASK(8))
#define PWM1_HIGH_DATA_MASK (_SBF(8, _BITMASK(4)))
#define PWM1_LOW_DATA_MASK (_SBF(12, _BITMASK(4)))
/*
* Nybble selects for data fields used in registers
*/
// PWM0
#define PWM0_HIGH_DATA_SELECT 0x00000001
#define PWM0_LOW_DATA_SELECT 0x00000010
// PWM1
#define PWM1_HIGH_DATA_SELECT 0x00000100
#define PWM1_LOW_DATA_SELECT 0x00001000
/*
* Nybble field LSB positions (bit#) for data fields
*/
// PWM0
#define PWM0_HIGH_DATA_POSITION 0
#define PWM0_LOW_DATA_POSITION 4
// PWM1
#define PWM1_HIGH_DATA_POSITION 8
#define PWM1_LOW_DATA_POSITION 12
/***********************************************************************
*
* Function: LH7A400_pwm0_init
*
* Purpose:
* Initialize the PWM0 channel of the LH7A400
*
* Processing:
* - save PWM1 data
* - set the PWM0 duty-cycle field to the value specified
* - set the PWM0 clock-prescaler field to the value specified
* - write new values to appropriate registers
*
* Parameters:
*
* dutycycle_ext - PWM0 duty-cycle value when externally powered
* prescale_ext - PWM0 clock-prescale value when externally powered
* dutycycle_bat - PWM0 duty-cycle value when battery powered
* prescale_bat - PWM0 clock-prescale value when battery powered
*
* Values:
* UNS_8 dutycycle_* - 0 or 1-15: OFF or n/16 duty-cycle
* UNS_8 prescale_* - 0-7: clock / (16 * (2^n)) {prescaler}
*
* Outputs: None
*
* Returns:
* success: _NO_ERROR
* failure: _ERROR (currently just a parameter-check failure)
*
* Notes:
* It is not necessary to call this function first (before using PWM).
* It's only real function is to set ALL of the Duty-Cycle and
* Clock-Prescaler values at the same time.
*
* This function is "destructive": ALL previous PWM0 values are lost
* ( PWM1 values are saved )
*
* The PWM holds two sets of values for each duty-cycle & prescaler ..
* if power is switched to battery, a "less-power-consuming"
* duty-cycle & clock-prescale can be automatically switched to
* if pre-programmed.
*
* LOW field = EXTERN_POWER data
* HIGH field = BATTERY_POWER data
*
*
**********************************************************************/
INT_8 LH7A400_pwm0_init (UNS_8 dutycycle_ext,
UNS_8 prescale_ext,
UNS_8 dutycycle_bat,
UNS_8 prescale_bat
)
{
INT_8 ret;
UNS_32 old_pmpcon1_data, old_pmpfreq1_data;
ret = _NO_ERROR;
/* bounds check parameters */
if (dutycycle_ext > MAX_DUTYCYCLE_VALUE)
ret = _ERROR;
if (prescale_ext > MAX_PRESCALE_VALUE)
ret = _ERROR;
if (dutycycle_bat > MAX_DUTYCYCLE_VALUE)
ret = _ERROR;
if (prescale_bat > MAX_PRESCALE_VALUE)
ret = _ERROR;
/*
* if parameters are VALID,
* set appropriate register fields
*/
if (ret == _NO_ERROR)
{
/* save previous register contents .. clear out PWM0 data */
old_pmpcon1_data = DCDC->pmpcon & ~PWM0_ALL_DATA_MASK;
old_pmpfreq1_data = DCDC->pmpfreq & ~PWM0_ALL_DATA_MASK;
DCDC->pmpcon = old_pmpcon1_data |
PMPCON_DRV0_DTYLO(dutycycle_ext) |
PMPCON_DRV0_DTYHI(dutycycle_bat) ;
DCDC->pmpfreq = old_pmpfreq1_data |
PMPFREQ_DRV0_PRELO(prescale_ext) |
PMPFREQ_DRV0_PREHI(prescale_bat) ;
}
return ret;
}
/***********************************************************************
*
* Function: LH7A400_pwm1_init
*
* Purpose:
* Initialize the PWM0 channel of the LH7A400
*
* Processing:
* - save PWM1 data
* - set the PWM0 duty-cycle field to the value specified
* - set the PWM0 clock-prescaler field to the value specified
* - write new values to appropriate registers
*
* Parameters:
*
* dutycycle_ext - PWM1 duty-cycle value when externally powered
* prescale_ext - PWM1 clock-prescale value when externally powered
* dutycycle_bat - PWM1 duty-cycle value when battery powered
* prescale_bat - PWM1 clock-prescale value when battery powered
*
* Values:
* UNS_8 dutycycle_* - 0 or 1-15: OFF or n/16 duty-cycle
* UNS_8 prescale_* - 0-7: clock / (16 * (2^n)) {prescaler}
*
*
* Outputs: None
*
* Returns:
* success: _NO_ERROR
* failure: _ERROR (currently just a parameter-check failure)
*
* Notes:
* It is not necessary to call this function first (before using PWM).
* It's only real function is to set ALL of the Duty-Cycle and
* Clock-Prescaler values at the same time.
*
* This function is "destructive": ALL previous PWM1 values are lost
* ( PWM0 values are saved )
*
* The PWM holds two sets of values for each duty-cycle & prescaler ..
* if power is switched to battery, a "less-power-consuming"
* duty-cycle & clock-prescale can be automatically switched to
* if pre-programmed.
*
* LOW field = EXTERN_POWER data
* HIGH field = BATTERY_POWER data
*
*
**********************************************************************/
INT_8 LH7A400_pwm1_init (UNS_8 dutycycle_ext,
UNS_8 prescale_ext,
UNS_8 dutycycle_bat,
UNS_8 prescale_bat
)
{
INT_8 ret;
UNS_32 old_pmpcon0_data, old_pmpfreq0_data;
ret = _NO_ERROR;
/* bounds check parameters */
if (dutycycle_ext > MAX_DUTYCYCLE_VALUE)
ret = _ERROR;
if (prescale_ext > MAX_PRESCALE_VALUE)
ret = _ERROR;
if (dutycycle_bat > MAX_DUTYCYCLE_VALUE)
ret = _ERROR;
if (prescale_bat > MAX_PRESCALE_VALUE)
ret = _ERROR;
/*
* if parameters are VALID,
* set appropriate register fields
*/
if (ret == _NO_ERROR)
{
/* save previous register contents .. clear out PWM0 data */
old_pmpcon0_data = DCDC->pmpcon & ~PWM1_ALL_DATA_MASK;
old_pmpfreq0_data = DCDC->pmpfreq & ~PWM1_ALL_DATA_MASK;
DCDC->pmpcon = old_pmpcon0_data |
PMPCON_DRV1_DTYLO(dutycycle_ext) |
PMPCON_DRV1_DTYHI(dutycycle_bat) ;
DCDC->pmpfreq = old_pmpfreq0_data |
PMPFREQ_DRV1_PRELO(prescale_ext) |
PMPFREQ_DRV1_PREHI(prescale_bat) ;
}
return ret;
}
/***********************************************************************
*
* Function: LH7A400_pwm0_set_dutycycle
*
* Purpose:
* Sets the PWM0 duty cycle to the value specified
*
* Processing:
* - save appropriate old PWM duty-cycle data
* - set "power" field to "dutycycle" for PWM0 DCDCCON Register
* - write new Duty-Cycle Register data
*
* Parameters:
* dutycycle - PWM0 duty-cycle value. (0-15)
*
* power - Type of power connected to the LH7A400.
* This actually designates which register fields
* to access:
* EXTERN_POWER or BATTERY_POWER or ALL_POWER_TYPES
* {LH7A400_dcdc_driver.h}
*
* Values:
* UNS_8 dutycycle - 0 or 1-15: OFF or n/16 duty-cycle
* UNS_8 power - EXTERN_POWER | BATTERY_POWER | ALL_POWER_TYPES
*
*
* Outputs: None
*
* Returns:
* success: _NO_ERROR
* failure: _ERROR (currently just a parameter-check failure)
*
* Notes:
*
* ALL PWM data is preserved, except for parameter data field(s)
* overwritten by parameter value.
*
**********************************************************************/
INT_8 LH7A400_pwm0_set_dutycycle (UNS_8 dutycycle, UNS_8 power)
{
INT_8 ret;
UNS_32 old_pmpcon1_data;
ret = _NO_ERROR;
/* bounds check parameters */
if (dutycycle > 15)
ret = _ERROR;
if (power > NUMBER_OF_POWER_TYPES)
ret = _ERROR;
/*
* if parameters are VALID,
* set appropriate register fields based on Power-Type
*/
if (ret == _NO_ERROR)
{
/*
* save appropriate previous register contents,
* then set desired field accordingly
*/
switch (power)
{
case EXTERN_POWER: // set PWM0 LOW field
old_pmpcon1_data = DCDC->pmpcon &
~PWM0_LOW_DATA_MASK;
DCDC->pmpcon = old_pmpcon1_data |
PMPCON_DRV0_DTYLO(dutycycle);
break;
case BATTERY_POWER: // set PWM0 HIGH field
old_pmpcon1_data = DCDC->pmpcon &
~PWM0_HIGH_DATA_MASK;
DCDC->pmpcon = old_pmpcon1_data |
PMPCON_DRV0_DTYHI(dutycycle);
break;
case ALL_POWER_TYPES: // set PWM0 LOW & HIGH fields
old_pmpcon1_data = DCDC->pmpcon &
~PWM0_ALL_DATA_MASK;
DCDC->pmpcon = old_pmpcon1_data |
PMPCON_DRV0_DTYLO(dutycycle) |
PMPCON_DRV0_DTYHI(dutycycle) ;
break;
default:
break;
}
}
return ret;
}
/***********************************************************************
*
* Function: LH7A400_pwm1_set_dutycycle
*
* Purpose:
* Sets the PWM1 duty cycle to the value specified
*
* Processing:
* - save appropriate old PWM duty-cycle data
* - set "power" field to "dutycycle" for PWM1 DCDCCON Register
* - write new Duty-Cycle Register data
*
* Parameters:
* dutycycle - PWM1 duty-cycle value. (0-15)
*
* power - Type of power connected to the LH7A400.
* This actually designates which register fields
* to access:
* EXTERN_POWER or BATTERY_POWER or ALL_POWER_TYPES
* {LH7A400_dcdc_driver.h}
*
* Values:
* UNS_8 dutycycle - 0 or 1-15: OFF or n/16 duty-cycle
* UNS_8 power - EXTERN_POWER | BATTERY_POWER | ALL_POWER_TYPES
*
*
* Outputs: None
*
* Returns:
* success: _NO_ERROR
* failure: _ERROR (currently just a parameter-check failure)
*
* Notes:
*
* ALL PWM data is preserved, except for parameter data field(s)
* overwritten by parameter value.
*
**********************************************************************/
INT_8 LH7A400_pwm1_set_dutycycle (UNS_8 dutycycle, UNS_8 power)
{
INT_8 ret;
UNS_32 old_pmpcon0_data;
ret = _NO_ERROR;
/* bounds check parameters */
if (dutycycle > 15)
ret = _ERROR;
if (power > NUMBER_OF_POWER_TYPES)
ret = _ERROR;
/*
* if parameters are VALID,
* set appropriate register fields based on Power-Type
*/
if (ret == _NO_ERROR)
{
/*
* save appropriate previous register contents,
* then set desired field accordingly
*/
switch (power)
{
case EXTERN_POWER: // set PWM1 LOW field
old_pmpcon0_data = DCDC->pmpcon &
~PWM1_LOW_DATA_MASK;
DCDC->pmpcon = old_pmpcon0_data |
PMPCON_DRV1_DTYLO(dutycycle);
break;
case BATTERY_POWER: // set PWM1 HIGH field
old_pmpcon0_data = DCDC->pmpcon &
~PWM1_HIGH_DATA_MASK;
DCDC->pmpcon = old_pmpcon0_data |
PMPCON_DRV1_DTYHI(dutycycle);
break;
case ALL_POWER_TYPES: // set PWM1 LOW & HIGH fields
old_pmpcon0_data = DCDC->pmpcon &
~PWM1_ALL_DATA_MASK;
DCDC->pmpcon = old_pmpcon0_data |
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -