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

  \brief the calibration of the internal RC generator.

  \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:
      29/06/07 E. Ivanov - Created
 ******************************************************************************/
/******************************************************************************
 *   WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK.  *
 *   EXPERT USERS SHOULD PROCEED WITH CAUTION.                                *
 ******************************************************************************/

/******************************************************************************
                   Includes section
******************************************************************************/
#include <halClkCtrl.h>
#include <calibration.h>
#include <atomic.h>
#include <halW1.h>

/******************************************************************************
                   Defines section
******************************************************************************/
#define INTERNAL_CLOCK F_CPU

// low frequency oscillator clock for 1 cycle measurement
#define EXTERNAL_TICKS 1
// mcu clocks number of one procedure measurement
#define CYCLE_LENGTH 7
// stability crystal oscillator frequency
#define REFERENCE_CLOCK (32768/1024)
// Etalon mcu clock number for 1 ticks of 32 Hz asynchronous timer
#define REFERENCE_COUNT (INTERNAL_CLOCK * EXTERNAL_TICKS) / (REFERENCE_CLOCK * CYCLE_LENGTH)
// up direction
#define UP_DIRECT      2
// down direction
#define DOWN_DIRECT    1

/******************************************************************************
                   Prototypes section
******************************************************************************/
/******************************************************************************
Calculates number of cycles during EXTERNAL_TICKS period.
Parameters:
  none
Returns:
  number of the cycles.
******************************************************************************/
uint16_t halMeasurement(void);

/******************************************************************************
                   Implementations section
******************************************************************************/
/******************************************************************************
Performs calibration of the internal RC generator.
Parameters:
  none.
Returns:
  none.
******************************************************************************/
void halCalibrateInternalRc(void)
{
  uint16_t count;
  uint8_t cycles = 0x80;
  uint16_t counterGate = REFERENCE_COUNT;
  uint8_t direct = 0;

  do
  {
    // perform clock measurement
    count = halMeasurement();
    if (count > REFERENCE_COUNT)
    {
      if ((counterGate < (count - REFERENCE_COUNT)) && (UP_DIRECT == direct))
      { // previous measurement was more correct
        OSCCAL--;
        NOP;
        break;
      }
      OSCCAL--;
      NOP;
      counterGate = count - REFERENCE_COUNT;
      direct = DOWN_DIRECT;
    }

    if (count < REFERENCE_COUNT)
    {
      if ((counterGate < (REFERENCE_COUNT - count)) && (DOWN_DIRECT == direct))
      { // previous measurement was more exactly
        OSCCAL++;
        NOP;
        break;
      }
      OSCCAL++;
      NOP;
      counterGate = REFERENCE_COUNT - count;
      direct = UP_DIRECT;
    }

    if (REFERENCE_COUNT == count)
      break;

  } while (--cycles);
}

/******************************************************************************
Performs calibration of the main clock generator.
Parameters:
  none.
Returns:
  none.
******************************************************************************/
void HAL_CalibrateMainClock(void)
{
  if (INTERNAL_RC == halGetClockSource())
    halCalibrateInternalRc();
}

/******************************************************************************
Starts calibration after program starting or waking up from power down.
******************************************************************************/
void halStartingCalibrate(void)
{
  uint16_t i;

  for (i = 0; i < 5000; i++)
  { /* wait for 1 second start up low frequency generator*/
    __delay_us(200);  // 200 us
  }
  HAL_CalibrateMainClock();
}
// eof calibration.c