]> the.earth.li Git - riso-kagaku-clone.git/blob - libs-device/osccal.h
Add .gitignore file
[riso-kagaku-clone.git] / libs-device / osccal.h
1 /* Name: osccal.h
2  * Author: Christian Starkjohann
3  * Creation Date: 2008-04-10
4  * Tabsize: 4
5  * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
6  * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
7  */
8
9 /*
10 General Description:
11 This module contains a function which calibrates the AVR's internal RC
12 oscillator so that the CPU runs at F_CPU (F_CPU is a macro which must be
13 defined when the module is compiled, best passed in the compiler command
14 line). The time reference is the USB frame clock of 1 kHz available
15 immediately after a USB RESET condition. Timing is done by counting CPU
16 cycles, so all interrupts must be disabled while the calibration runs. For
17 low level timing measurements, usbMeasureFrameLength() is called. This
18 function must be enabled in usbconfig.h by defining
19 USB_CFG_HAVE_MEASURE_FRAME_LENGTH to 1. It is recommended to call
20 calibrateOscillator() from the reset hook in usbconfig.h:
21
22 #ifndef __ASSEMBLER__
23 #include <avr/interrupt.h>  // for sei()
24 extern void calibrateOscillator(void);
25 #endif
26 #define USB_RESET_HOOK(resetStarts)  if(!resetStarts){cli(); calibrateOscillator(); sei();}
27
28 This routine is an alternative to the continuous synchronization described
29 in osctune.h.
30
31 Algorithm used:
32 calibrateOscillator() first does a binary search in the OSCCAL register for
33 the best matching oscillator frequency. Then it does a next neighbor search
34 to find the value with the lowest clock rate deviation. It is guaranteed to
35 find the best match among neighboring values, but for version 5 oscillators
36 (which have a discontinuous relationship between OSCCAL and frequency) a
37 better match might be available in another OSCCAL region.
38
39 Limitations:
40 This calibration algorithm may try OSCCAL values of up to 192 even if the
41 optimum value is far below 192. It may therefore exceed the allowed clock
42 frequency of the CPU in low voltage designs!
43 Precision depends on the OSCCAL vs. frequency dependency of the oscillator.
44 Typical precision for an ATMega168 (derived from the OSCCAL vs. F_RC diagram
45 in the data sheet) should be in the range of 0.4%. Only the 12.8 MHz and
46 16.5 MHz versions of V-USB (with built-in receiver PLL) can tolerate this
47 deviation! All other frequency modules require at least 0.2% precision.
48 */
49
50 #ifndef __OSCCAL_H_INCLUDED__
51 #define __OSCCAL_H_INCLUDED__
52
53 void    calibrateOscillator(void);
54 /* This function calibrates the RC oscillator so that the CPU runs at F_CPU.
55  * It MUST be called immediately after the end of a USB RESET condition!
56  * Disable all interrupts during the call!
57  * It is recommended that you store the resulting value in EEPROM so that a
58  * good guess value is available after the next reset.
59  */
60
61
62 #endif /* __OSCCAL_H_INCLUDED__ */