power_ctrl.c

来自「最新版IAR FOR ARM(EWARM)5.11中的代码例子」· C语言 代码 · 共 189 行

C
189
字号
/*************************************************************************
 *
 *    Used with ICCARM and AARM.
 *
 *    (c) Copyright IAR Systems 2007
 *
 *    File name   : power_ctrl.h
 *    Description :Power control low level functions
 *
 *    History :
 *    1. Date        : January 18, 2006
 *       Author      : Stanimir Bonev
 *       Description : Create
 *
 *    $Revision: 15135 $
 **************************************************************************/
#include <ST\iostr750.h>
#include "power_ctrl.h"

/*************************************************************************
 * Function Name: EXTIT_Handler
 * Parameters: none
 * Return: none
 *
 * Description: External interrupt handler. Enable JTAG interface and
 * release JLink controller reset and turn off battery supply
 *
 *************************************************************************/
static void EXTIT_Handler(void)
{
  // disable interrupt
  OS_ARM_DisableISR(WAKEUP_VECT_ID);
  BattSuppOff();
  GPIO_REMAP1R_bit.DBGOFF = 0;
  GPIO_P0D  |= _P0_JTAG_STANDBY_MASK;
  GPIO_P2D  |= _P2_JATG_RST_MASK;
  GPIO_P1D  |= _P1_JTAG_CLK_MASK;
#if (defined(NDEBUG) || BOARD_REV == 2 || BOARD_REV == 0)
  for(volatile int i = 20; i; --i);
  GPIO_P2D  &= ~_P2_JATG_RST_MASK;
  for(volatile int i = 20; i; --i);
  GPIO_P2D  |=  _P2_JATG_RST_MASK;
#endif // (defined(NDEBUG) || BOARD_REV == 2 || BOARD_REV == 0)
}

/*************************************************************************
 * Function Name: PowerCrltInit
 * Parameters: none
 * Return: none
 *
 * Description: Init power type detection and turn off/on J-Link controller
 *
 *************************************************************************/
void PowerCrltInit(void)
{
  // Enable GPIO and EXTIN clk and release reset of these modules
  MRCC_PCLKEN |= ((1UL << 24) |  // GPIO
                  (1UL << 28));  // EXTIN
  MRCC_PSWRES &=~((1UL << 24) |  // GPIO
                  (1UL << 28));  // EXTIN
  // Set all unused pins to GIO input with Pull-down
  GPIO_P1D  |= _P0_JTAG_STANDBY_MASK;
  GPIO_P1D  &=   (1UL << 31) | (1UL << 29) | _P0_JTAG_STANDBY_MASK;
  GPIO_P0C0 |= ~((1UL << 31) | (1UL << 29) | _P0_JTAG_STANDBY_MASK);
  GPIO_P0C0 &= ~_P0_JTAG_STANDBY_MASK;
  GPIO_P0C1 |= ~((1UL << 31) | (1UL << 29 | _P0_JTAG_STANDBY_MASK));
  GPIO_P0C1 &= ~_P0_JTAG_STANDBY_MASK;
  GPIO_P0C2 |=  _P0_JTAG_STANDBY_MASK;
  GPIO_P0C2 &=   (1UL << 31) | (1UL << 29) | _P0_JTAG_STANDBY_MASK;

  GPIO_P1D  |=  _P1_JTAG_CLK_MASK;
  GPIO_P1D  &=  (1UL <<  9) | _P1_JTAG_CLK_MASK;
  GPIO_P1C0 |= ~((1UL <<  9) | _P1_EXT_POWER_MASK);
  GPIO_P1C0 &= ~_P1_EXT_POWER_MASK;
  GPIO_P1C1 |= ~((1UL <<  9) | _P1_BATT_CTRL_MASK | _P1_JTAG_CLK_MASK);
  GPIO_P1C1 &= ~(_P1_BATT_CTRL_MASK | _P1_JTAG_CLK_MASK);
  GPIO_P1C2 |=  _P1_JTAG_CLK_MASK | _P1_BATT_CTRL_MASK;
  GPIO_P1C2 &=  (1UL <<  9) | _P1_JTAG_CLK_MASK | _P1_BATT_CTRL_MASK;

  GPIO_P2D  |=  _P2_JATG_RST_MASK;
  GPIO_P2D  &=  _P2_JATG_RST_MASK | 0xFFFF;
  GPIO_P2C0 |= 0xFFFF0000;
  GPIO_P2C1 |= 0xFFFE0000;
  GPIO_P2C1 &=~0x00010000;
  GPIO_P2C2 |= 0x00010000;
  GPIO_P2C2 &=~0xFFFE0000;

  // Init external interrupt
  EXTIT_TSR_bit.TR15 = 1; // Interrupt request on rising edge
  EXTIT_MR = 1UL << 15;   // EIT15 enabled

  // assign EIT to interrupt
  // EIT interrupt vector.
  OS_ARM_InstallISRHandler(WAKEUP_VECT_ID, &EXTIT_Handler);
  // EIT interrupt level
  OS_ARM_ISRSetPrio(WAKEUP_VECT_ID, 15);
  // Enable OS EIT interrupt
  OS_ARM_EnableISR(WAKEUP_VECT_ID);

  if(GetSupplyType() == BATT_SUPPLY)
  {
#ifdef NDEBUG
    // Reset and put in stand by the jlink controller
    GPIO_P1D &= ~_P1_JTAG_CLK_MASK;
#if BOARD_REV == 1
    GPIO_P2D &= ~_P2_JATG_RST_MASK;
#else
    GPIO_P0D &= ~_P0_JTAG_STANDBY_MASK;
#endif // BOARD_REV == 1
    // turn off jtag interface
    GPIO_REMAP1R_bit.DBGOFF = 1;
#endif // NDEBUG
    // Turn on battery supply
    BattSuppOn();
  }
}

/*************************************************************************
 * Function Name: GetSupplyType
 * Parameters: none
 * Return: SupplyType_t
 *
 * Description: Return current supply type (external / battery)
 *
 *************************************************************************/
SupplyType_t GetSupplyType(void)
{
  return((GPIO_P1D & _P1_EXT_POWER_MASK)?EXT_SUPPLY:BATT_SUPPLY);
}

/*************************************************************************
 * Function Name: BattSuppOff
 * Parameters: none
 * Return: none
 *
 * Description: Turn off battery supply
 *
 *************************************************************************/
void BattSuppOff(void)
{
  GPIO_P1D  &= ~_P1_BATT_CTRL_MASK;
}

/*************************************************************************
 * Function Name: BattSuppOn
 * Parameters: none
 * Return: none
 *
 * Description: Turn on battery supply
 *
 *************************************************************************/
void BattSuppOn(void)
{
  GPIO_P1D  = _P1_BATT_CTRL_MASK;
}
/*************************************************************************
 * Function Name: Discharge
 * Parameters: none
 * Return: none
 *
 * Description: Discharge 3.3 volts supply capacitors
 *
 *************************************************************************/
void Discharge(void)
{
}

/*************************************************************************
 * Function Name: StandBy
 * Parameters: none
 * Return: none
 *
 * Description: Turn off board's supply
 *
 *************************************************************************/
void StandBy (void)
{
  if(GetSupplyType() == BATT_SUPPLY)
  {
    OS_DeleteTimer(&PowerOffTimer);
    BattSuppOff();
    Discharge();
  }
  else
  {
    OS_RetriggerTimer(&PowerOffTimer);
  }
}

⌨️ 快捷键说明

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