summaryrefslogtreecommitdiffhomepage
path: root/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/src/halAppClock.c
blob: c462b89675f9df509890dfcf988a04624ade0ac0 (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
/**************************************************************************//**
  \file  halAppClock.c

  \brief Implementation of appTimer hardware-dependent module.

  \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:
      5/12/07 A. Khromykh - Created
 ******************************************************************************/
/******************************************************************************
 *   WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK.  *
 *   EXPERT USERS SHOULD PROCEED WITH CAUTION.                                *
 ******************************************************************************/
/******************************************************************************
                   Includes section
******************************************************************************/
#include <halAppClock.h>
#include <halDbg.h>
#include <halDiagnostic.h>

/******************************************************************************
                   Global variables section
******************************************************************************/
static uint32_t halAppTime = 0ul;     // time of application timer
uint8_t halAppTimeOvfw = 0;
static volatile uint8_t halAppIrqCount = 0;

/******************************************************************************
                   Implementations section
******************************************************************************/
/******************************************************************************
Initialization appTimer clock.
******************************************************************************/
void halInitAppClock(void)
{
  OCR4A = TOP_TIMER_COUNTER_VALUE; // 1 millisecond timer interrupt interval.
  TCCR4B = (1 << WGM12);           // CTC mode
  halStartAppClock();              // clock source is cpu divided by 8
  TIMSK4 |= (1 << OCIE4A);         // Enable TC4 interrupt
}

/******************************************************************************
Return time of sleep timer.

Returns:
  time in ms.
******************************************************************************/
uint32_t halGetTimeOfAppTimer(void)
{
  halAppSystemTimeSynchronize();
  return halAppTime;
}

/******************************************************************************
Return system time in us

Parameters:
  mem - memory for system time
Returns:
  none.
******************************************************************************/
void halGetSystemTimeUs(uint64_t *mem)
{
#if (F_CPU == 4000000ul)
  *mem = 1000ul * halAppTime + (TCNT4 << 1);
#endif
#if (F_CPU == 8000000ul)
  *mem = 1000ul * halAppTime + TCNT4;
#endif
}

/**************************************************************************//**
\brief Takes account of the sleep interval.

\param[in]
  interval - time of sleep
******************************************************************************/
void halAdjustSleepInterval(uint32_t interval)
{
  halAppTime += interval;
  halPostTask4(HAL_TIMER4_COMPA);
}

/**************************************************************************//**
Synchronization system time which based on application timer.
******************************************************************************/
void halAppSystemTimeSynchronize(void)
{
  uint8_t tmpCounter;
  uint32_t tmpValue;

  ATOMIC_SECTION_ENTER
  BEGIN_MEASURE
    tmpCounter = halAppIrqCount;
    halAppIrqCount = 0;
  END_MEASURE(HAL_APP_TIMER_SYNCHRONIZE_LIMIT)
  ATOMIC_SECTION_LEAVE

  tmpValue = tmpCounter * HAL_APPTIMERINTERVAL;
  halAppTime += tmpValue;
  if (halAppTime < tmpValue)
    halAppTimeOvfw++;
}

/******************************************************************************
Interrupt handler
******************************************************************************/
ISR(TIMER4_COMPA_vect)
{
  BEGIN_MEASURE
  halAppIrqCount++;
  halPostTask4(HAL_TIMER4_COMPA);
  // infinity loop spy
  SYS_InfinityLoopMonitoring();
  END_MEASURE(HALISR_TIMER4_COMPA_TIME_LIMIT)
}
// eof halAppClock.c