summaryrefslogtreecommitdiff
path: root/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/sleep.c
blob: 1ad72df26c67988d9eb1815873fb1a2e3d12d3ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**************************************************************************//**
  \file   sleep.c

  \brief  The implementation of common sleep and wake up.

  \author
      Atmel Corporation: http://www.atmel.com \n
      Support email: avr@atmel.com

    Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
    Licensed under Atmel's Limited License Agreement (BitCloudTM).

  \internal
    History:
      1/12/09 A. Khromykh - Created
 ******************************************************************************/
/******************************************************************************
 *   WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK.  *
 *   EXPERT USERS SHOULD PROCEED WITH CAUTION.                                *
 ******************************************************************************/
/******************************************************************************
                   Includes section
******************************************************************************/
#include <halSleep.h>

/******************************************************************************
                   Define(s) section
******************************************************************************/
#define HAL_NULL_POINTER                      -1
#define HAL_SLEEP_TIMER_HAS_ALREADY_STARTED   -3
#define HAL_SLEEP_TIMER_IS_BUSY               -2
#define HAL_SLEEP_SYSTEM_HAS_ALREADY_STARTED  -3

/******************************************************************************
                   Global variables section
******************************************************************************/
HalSleepControl_t halSleepControl =
{
  .wakeupStation = HAL_ACTIVE_MODE,
  .sleepTimerState = HAL_SLEEP_TIMER_IS_STOPPED
};

/******************************************************************************
                   Implementations section
******************************************************************************/
/**************************************************************************//**
\brief Starts sleep timer and HAL sleep. When system is wake up send callback
\param[in]
    sleepParam - pointer to sleep structure.
\return
    -1 - bad pointer,   \n
    -2 - sleep timer is busy, \n
    -3 - sleep system has been started.
     0 - success.
******************************************************************************/
int HAL_StartSystemSleep(HAL_Sleep_t *sleepParam)
{
  HAL_SleepTimer_t sleepTimer;
  int sleepTimerStatus;

  if (!sleepParam)
    return HAL_NULL_POINTER;

  halSleepControl.callback = sleepParam->callback;
  sleepTimer.interval = sleepParam->sleepTime;
  sleepTimer.mode = TIMER_ONE_SHOT_MODE;
  sleepTimer.callback = NULL;

  sleepTimerStatus = HAL_StartSleepTimer(&sleepTimer);
  if ((HAL_NULL_POINTER == sleepTimerStatus) || (HAL_SLEEP_TIMER_HAS_ALREADY_STARTED == sleepTimerStatus))
    return HAL_SLEEP_TIMER_IS_BUSY;

  if (-1 == HAL_Sleep())
    return HAL_SLEEP_SYSTEM_HAS_ALREADY_STARTED;

  return 0;
}

//eof sleep.c