]> the.earth.li Git - riso-kagaku-clone.git/blob - libs-device/osccal.h
Move to micronucleus assembly for OSCCAL calibration
[riso-kagaku-clone.git] / libs-device / osccal.h
1 /* Name: osccal.h
2  * Author: Christian Starkjohann
3  * Changes 2013-11-04 cpldcpu@gmail.com
4  * Creation Date: 2008-04-10
5  * Tabsize: 4
6  * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
7  * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
8  */
9
10 /*
11 General Description:
12 This module contains a function which calibrates the AVR's internal RC
13 oscillator so that the CPU runs at F_CPU (F_CPU is a macro which must be
14 defined when the module is compiled, best passed in the compiler command
15 line). The time reference is the USB frame clock of 1 kHz available
16 immediately after a USB RESET condition. Timing is done by counting CPU
17 cycles, so all interrupts must be disabled while the calibration runs.
18 The size optimized assembler implementation includes its own implementation
19 of usbMeasureFrameLength. Therefore USB_CFG_HAVE_MEASURE_FRAME_LENGTH should
20 be set to 0 to avoid including unused code sections. It is recommended to call
21 calibrateOscillatorASM() from the reset hook in usbconfig.h by including osccal.h:
22
23 #include "osccal.h"
24
25 This routine is an alternative to the continuous synchronization described
26 in osctune.h.
27
28 Algorithm used: see osccalASM.S
29
30 Limitations:
31 This calibration algorithm may try OSCCAL values of up to 192 even if the
32 optimum value is far below 192. It may therefore exceed the allowed clock
33 frequency of the CPU in low voltage designs!
34 Precision depends on the OSCCAL vs. frequency dependency of the oscillator.
35 Typical precision for an ATMega168 (derived from the OSCCAL vs. F_RC diagram
36 in the data sheet) should be in the range of 0.4%. Only the 12.8 MHz and
37 16.5 MHz versions of V-USB (with built-in receiver PLL) can tolerate this
38 deviation! All other frequency modules require at least 0.2% precision.
39 */
40
41 #ifndef __OSCCAL_H_INCLUDED__
42 #define __OSCCAL_H_INCLUDED__
43
44 #ifndef __ASSEMBLER__
45 void    calibrateOscillatorASM(void);
46 #define USB_RESET_HOOK(resetStarts)  if(!resetStarts){ calibrateOscillatorASM();}
47 #endif
48 /* This function calibrates the RC oscillator so that the CPU runs at F_CPU.
49  * It MUST be called immediately after the end of a USB RESET condition!
50  * Disable all interrupts during the call!
51  * It is recommended that you store the resulting value in EEPROM so that a
52  * good guess value is available after the next reset.
53  */
54
55
56 #endif /* __OSCCAL_H_INCLUDED__ */