Пример #1
0
/******************************************************************************
  Prepares system for power-save, power-down.
  Power-down the mode is possible only when internal RC is used
  Parameters:
  none.
  Returns:
  -1 there is no possibility to power-down system.
******************************************************************************/
int HAL_Sleep(void)
{
  if (INTERNAL_RC != halGetClockSource())
  {
    if (HAL_SLEEP_TIMER_IS_STOPPED == halSleepControl.sleepTimerState)
    { // sleep timer isn't started
      return -1;
    }
    GPIO_RF_SLP_TR_make_in();

    while (ASSR & HAL_ASSR_FLAGS);
    if (!(TIMSK2 & (1 << OCIE2A)))
    { // compare interrupt is disabled
      OCR2A = 0xFF;
      while (ASSR & HAL_ASSR_FLAGS);
    }

    TCCR2A |= ((1 << COM2A1) | (1 << COM2A0)); // set OC2A on compare
    while (ASSR & HAL_ASSR_FLAGS);
    TCCR2B |= (1 << FOC2A); // force output to set OC2A
    while (ASSR & HAL_ASSR_FLAGS);
    TCCR2A &= ~((1 << COM2A1) | (1 << COM2A0)); // no compare
    while (ASSR & HAL_ASSR_FLAGS);
    TCCR2A |= (1 << COM2A1); // clear OC2A on compare
    while (ASSR & HAL_ASSR_FLAGS);
  }

  halSleepControl.wakeupStation = HAL_SLEEP_MODE;  // the reset of sign of entry to the sleep mode.
  while (ASSR & HAL_ASSR_FLAGS);
  halPostTask4(HAL_SLEEP);
  return 0;
}
Пример #2
0
/**************************************************************************//**
\brief Switch on system power.

\param[in]
  wakeupSource - wake up source
******************************************************************************/
void halPowerOn(const uint8_t wakeupSource)
{
  halSleepControl.wakeupStation = HAL_ACTIVE_MODE;
  halSleepControl.wakeupSource = wakeupSource;

  if (INTERNAL_RC == halGetClockSource())
  {
    GPIO_RF_SLP_TR_clr();
  }
  else
  {
    GPIO_RF_SLP_TR_make_in();
    TCCR2A &= ~((1 << COM2A1) | (1 << COM2A0)); // no compare
    while (ASSR & HAL_ASSR_FLAGS);
  }
  GPIO_RF_SLP_TR_make_out();

  #ifdef _HAL_USE_AMPLIFIER_
    // set one on pin. Enable power amplifier.
    GPIO_POW_AMPLF_SLP_set();
  #endif

  halPostTask4(HAL_WAKEUP);
}
Пример #3
0
/******************************************************************************
Performs calibration of the main clock generator.
Parameters:
  none.
Returns:
  none.
******************************************************************************/
void HAL_CalibrateMainClock(void)
{
  if (INTERNAL_RC == halGetClockSource())
    halCalibrateInternalRc();
}
Пример #4
0
/******************************************************************************
Shutdowns system.
  NOTES:
  the application should be sure the poweroff will not be
  interrupted after the execution of the sleep().
******************************************************************************/
void halPowerOff(void)
{
  if (HAL_ACTIVE_MODE == halSleepControl.wakeupStation)
    return;  // it is a too late to sleep.

  // stop application timer clock
  halStopAppClock(); // will be shutdown
  if (0ul == halTimeStartOfSleep)
  { // start of sleep procedure
    // save time of stopping of the application timer
    halTimeStartOfSleep = halGetTimeOfSleepTimer();
  }

  #ifdef _HAL_USE_AMPLIFIER_
    // set zero on pin. Disable power amplifier.
    GPIO_POW_AMPLF_SLP_clr();
  #endif

  #ifdef _HAL_RF_RX_TX_INDICATOR_   
   
  // disable front end driver if that is supported
  phyRxTxSwitcherOff();

  #endif
  
  #ifdef _HAL_ANT_DIVERSITY_
  
  // disable antenna diversity switcher
  phyAntennaSwitcherOff();
  
  #endif 
  
  if (halEnableDtrWakeUp)
  { /* enable DTR (irq 4) wake up */
    halEnableIrqInterrupt(IRQ_4);
  } /* enable DTR (irq 4) wake up */

  // wait for end of eeprom writing
  halWaitEepromReady();

  if (INTERNAL_RC == halGetClockSource())
  {
    GPIO_RF_SLP_TR_set();
    GPIO_RF_SLP_TR_make_out();

    if (HAL_SLEEP_TIMER_IS_STARTED == halSleepControl.sleepTimerState)
    { // sleep timer is started
      SMCR = (1 << SM1) | (1 << SM0) | (1 << SE); // power-save
      __SLEEP;
      SMCR = 0;
    }
    else
    {
      halStopSleepTimerClock();
      SMCR = (1 << SM1) | (1 << SE); // power-down
      __SLEEP;
      SMCR = 0;
      halStartSleepTimerClock();
      halStartingCalibrate();
    }
  }
  else
  {
    uint8_t timsk4 = TIMSK4;
    uint8_t twcr = TWCR;
    uint8_t adcsra =  ADCSRA;
    TIMSK4 = 0;
    TWCR = 0;
    ADCSRA = 0;
    GPIO_RF_SLP_TR_make_out();
    SMCR = (1 << SM1) | (1 << SM0) | (1 << SE); // power-save
    __SLEEP;
    SMCR = 0;
    TIMSK4 = timsk4;
    TWCR = twcr;
    ADCSRA = adcsra;
  }

  // wait for time about 1 TOSC1 cycle for correct re-entering from power save mode to power save mode
  // wait for time about 1 TOSC1 cycle for correct reading TCNT2 after wake up to
  OCR2B = SOME_VALUE_FOR_SYNCHRONIZATION;
  while (ASSR & HAL_ASSR_FLAGS);
}