-
Notifications
You must be signed in to change notification settings - Fork 0
/
online_synch.h
163 lines (136 loc) · 5.89 KB
/
online_synch.h
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
/* This file has been prepared for Doxygen automatic documentation generation.*/
/*! \file *********************************************************************
*
* \brief
* Configuration and macros.
*
* \par Application note:
* AVR054 Synchronization of the internal RC oscillator
*
* \par Documentation:
* For comprehensive code documentation, supported compilers, compiler
* settings and supported devices see readme.html
*
* \author
* Atmel Corporation: http://www.atmel.com \n
* Support email: [email protected] \n
*
* $Name: RELEASE_1_2 $
* $Revision: 1.3 $
* $RCSfile: online_synch.h,v $
* $Date: 2006/02/17 11:47:59 $ \n
******************************************************************************/
#if !defined(_ONLINE_SYNCH_H_)
#define _ONLINE_SYNCH_H_
// ***********************************************************************
// Defines controlling synchronization
// ***********************************************************************
// Synchronization method. Uncomment one of the following to choose
// Synchronization method.
#define SYNCH_METHOD_SINGLE_SYNCH_BYTE
//#define SYNCH_METHOD_DOUBLE_SYNCH_BYTE
#define TARGET_FREQUENCY 8000000 // CPU frequency
#define SYNCH_FREQUENCY 19200 // UART baud rate
#define SYNCH_UBRR 25 // Baud rate register setting to
// obtain SYNCH_FREQUENCY.
#define SYNCH_ACCURACY 10 // 10 equals +/-1% (Only
// for single synch byte method)
// DEFAULT_OSCCAL_ADDRESS: The location in EEPROM where the default OSCCAL
// value can be found. (Only needed for single synch byte method).
#define DEFAULT_OSCCAL_ADDRESS 0x00
// NINE_BIT_TIMER: utilize the overflow bit of Timer/Counter0 as the
// ninth bit. Comment out to use only 8 bits.
#define NINE_BIT_TIMER
// Only for devices with OSCCAL registers with two frequency ranges. Always use
// 0x00 for devices with one continous OSCCAL register.
#define DEFAULT_OSCCAL_MASK 0x00 // Use lower half
//#define DEFAULT_OSCCAL_MASK 0x80 // Use upper half
// This file must be included after user settings
#include "device_specific.h"
// ***********************************************************************
// Precalculated values
// ***********************************************************************
// Expected # of processor ticks between UART bit lengths
#define TARGET_COUNT ((TARGET_FREQUENCY / SYNCH_FREQUENCY) - COUNTER_READ_DELAY)
// Applies only to single synch byte method:
#define SYNCH_LIMIT ((TARGET_FREQUENCY / SYNCH_FREQUENCY) * SYNCH_ACCURACY / 1000)
#if defined(NINE_BIT_TIMER) & (TARGET_COUNT > 511)
#error TARGET_COUNT is larger than 9-bit counter
#elif (!defined(NINE_BIT_TIMER)) & (TARGET_COUNT > 255)
#error TARGET_COUNT is larger than 8-bit counter
#endif
#define COUNT_LOW_LIMIT (TARGET_COUNT - SYNCH_LIMIT)
#define COUNT_HIGH_LIMIT (TARGET_COUNT + SYNCH_LIMIT)
#if defined(SYNCH_METHOD_DOUBLE_SYNCH_BYTE)
#define DEFAULT_OSCCAL ((1 << OSCCAL_RESOLUTION - 1) | DEFAULT_OSCCAL_MASK)
#define INITIAL_STEP (1 << (OSCCAL_RESOLUTION - 2))
#endif
#if defined(SYNCH_METHOD_SINGLE_SYNCH_BYTE)
#define DEFAULT_OSCCAL defaultOSCCAL // Default value read from EEPROM.
#define INITIAL_STEP (1 << 4)
#endif
// ***********************************************************************
// Predefined symbols
// ***********************************************************************
#define SS_MEASURING 0
#define SS_BINARY_SEARCH 1
#define SS_NEIGHBOR_SEARCH 2
#define FALSE 0
#define TRUE 1
// ***********************************************************************
// Functions implemented as macros to avoid function calls from within
// interrupt service routines.
// ***********************************************************************
//Set external interrupt 0 to trigger on rising edge.
#define SET_INT0_RISING() \
EXT_INT_SENSE_CTRL_REGISTER |= (1 << ISC01) | (1 << ISC00); \
EXT_INT_FLAG_REGISTER = (1 << INTF0);
//Set external interrupt 0 to trigger on falling edge.
#define SET_INT0_FALLING() \
EXT_INT_SENSE_CTRL_REGISTER |= (1 << ISC01); \
EXT_INT_SENSE_CTRL_REGISTER &= ~(1 << ISC00); \
EXT_INT_FLAG_REGISTER = (1 << INTF0);
//Set external interrupt 0 to trigger on low level.
#define SET_INT0_LOW() \
EXT_INT_SENSE_CTRL_REGISTER &= ~((1 << ISC01) | (1 << ISC00)); \
EXT_INT_FLAG_REGISTER |= (1 << INTF0);
// Disable INT0 (external interrupt 0)
#define DIS_INT0() EXT_INT_MASK_REGISTER &= ~(1 << INT0)
// Enable INT0 (external interrupt 0)
#define EN_INT0() EXT_INT_MASK_REGISTER |= (1 << INT0)
#define PREPARE_FOR_SYNCH() \
breakDetected = TRUE; \
synchState = SS_MEASURING; \
calStep = INITIAL_STEP; \
SYNCH_USART_STATCTRL_REG_B &= ~(1 << SYNCH_RXEN); /*Disable UART receiver.*/\
SET_INT0_FALLING(); /*Set external interrupt 0 to trigger on falling edge.*/\
EXT_INT_FLAG_REGISTER |= (1 << INTF0); \
EN_INT0(); /*Enable external interrupt 0.*/\
OSCCAL = DEFAULT_OSCCAL; \
NOP();
// For ATmega64 and ATmega128, 8 nop instructions must be run after a
// change in OSCCAL to ensure stability (See errata in datasheet).
// For all other devices, one nop instruction should be run to let
// the RC oscillator stabilize.
// The NOP() macro takes care of this.
#if defined(__AT90Mega64__) | defined(__ATmega64__) | \
defined(__AT90Mega128__) | defined(__ATmega128__)
#define NOP() \
__no_operation(); \
__no_operation(); \
__no_operation(); \
__no_operation(); \
__no_operation(); \
__no_operation(); \
__no_operation(); \
__no_operation();
#else
#define NOP() __no_operation();
#endif
// Absolute value macro.
#define ABS(var) (((var) < 0) ? -(var) : (var))
// ***********************************************************************
// Function prototypes
// ***********************************************************************
void Initialize_Synchronization( void );
#endif