summaryrefslogtreecommitdiffhomepage
path: root/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halSleep.c
blob: 305dfd3933344f8498c75cb6ff52bb6de6beebb2 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/**************************************************************************//**
  \file  halSleep.c

  \brief Implementation of sleep modes.

  \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 <sleepTimer.h>
#include <halSleepTimerClock.h>
#include <halRfPio.h>
#include <halRfCtrl.h>
#include <halSleep.h>
#include <halIrq.h>
#include <halDbg.h>
#include <halAppClock.h>
#include <halEeprom.h>

/******************************************************************************
                   Prototypes section
******************************************************************************/
/**************************************************************************//**
\brief Performs calibration of the main clock generator.
******************************************************************************/
void halStartingCalibrate(void);

#ifdef _HAL_RF_RX_TX_INDICATOR_
/**************************************************************************//**
\brief  Turn on pin 1 (DIG3) and pin 2 (DIG4) to indicate the transmit state of
the radio transceiver.
******************************************************************************/
void phyRxTxSwitcherOn(void);

/**************************************************************************//**
\brief  Turn off pin 1 (DIG3) and pin 2 (DIG4) to indicate the transmit state of
the radio transceiver.
******************************************************************************/
void phyRxTxSwitcherOff(void);

#endif

#ifdef _HAL_ANT_DIVERSITY_
/**************************************************************************//**
\brief  Turn on pin 9 (DIG1) and pin 10 (DIG2) to enable antenna select.
******************************************************************************/
void phyAntennaSwitcherOn(void);

/**************************************************************************//**
\brief  Turn off pin 9 (DIG1) and pin 10 (DIG2) to disable antenna select.
******************************************************************************/
void phyAntennaSwitcherOff(void);

#endif // _HAL_ANT_DIVERSITY_

/******************************************************************************
                   External global variables section
******************************************************************************/
extern volatile bool halEnableDtrWakeUp;
extern HalSleepControl_t halSleepControl;

/******************************************************************************
                   Global variables section
******************************************************************************/
static uint32_t halTimeStartOfSleep = 0ul;

/******************************************************************************
                   Implementations section
******************************************************************************/
/**************************************************************************//**
\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);
}

/******************************************************************************
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);
}

/******************************************************************************
  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;
}

/******************************************************************************
 Handler for task manager. It is executed when system has waked up.
******************************************************************************/
void halWakeupHandler(void)
{
  uint32_t timeEndOfSleep;

  // save time of stopping of the application timer
  timeEndOfSleep = halGetTimeOfSleepTimer();

  timeEndOfSleep -= halTimeStartOfSleep;  // time of sleep
  halTimeStartOfSleep = 0ul;
  // adjust application timer interval
  halAdjustSleepInterval(timeEndOfSleep);
  // start application timer clock
  halStartAppClock();
  // Wait for when radio will be waked up.
  halWaitRadio();
  
  #ifdef _HAL_ANT_DIVERSITY_
  
  // enable antenna diversity switcher
  phyAntennaSwitcherOn();
  
  #endif
  
  #ifdef _HAL_RF_RX_TX_INDICATOR_
  
  // enable front end driver if that is supported
  phyRxTxSwitcherOn();
  
  #endif
  
  if (HAL_SLEEP_TIMER_IS_WAKEUP_SOURCE == halSleepControl.wakeupSource)
  {
    if (halSleepControl.callback)
      halSleepControl.callback();
  }
}

/*******************************************************************************
  Makes MCU enter Idle mode.
*******************************************************************************/
void HAL_IdleMode(void)
{
  SMCR = 0x1;
  __SLEEP;
  SMCR = 0;
}

// eof sleep.c