sketchbook
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
osctune.h
Go to the documentation of this file.
1 /* Name: osctune.h
2  * Author: Christian Starkjohann
3  * Creation Date: 2008-10-18
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 file is declared as C-header file although it is mostly documentation
12 how the RC oscillator can be kept in sync to the USB frame rate. The code
13 shown here must be added to usbconfig.h or this header file is included from
14 there. This code works only if D- is wired to the interrupt, not D+!!!
15 
16 This is an alternative to the osccal routine in osccal.c. It has the advantage
17 that the synchronization is done continuously and that it has more compact
18 code size. The disadvantages are slow synchronization (it may take a while
19 until the driver works), that messages immediately after the SOF pulse may be
20 lost (and need to be retried by the host) and that the interrupt is on D-
21 contrary to most examples.
22 
23 You may want to store a good calibration value in EEPROM for the next startup.
24 You know that the calibration value is good when the first USB message is
25 received. Do not store the value on every received message because the EEPROM
26 has a limited endurance.
27 
28 Notes:
29 (*) You must declare the global character variable "lastTimer0Value" in your
30 main code.
31 
32 (*) Timer 0 must be free running (not written by your code) and the prescaling
33 must be consistent with the TIMER0_PRESCALING define.
34 
35 (*) Good values for Timer 0 prescaling depend on how precise the clock must
36 be tuned and how far away from the default clock rate the target clock is.
37 For precise tuning, choose a low prescaler factor, for a broad range of tuning
38 choose a high one. A prescaler factor of 64 is good for the entire OSCCAL
39 range and allows a precision of better than +/-1%. A prescaler factor of 8
40 allows tuning to slightly more than +/-6% of the default frequency and is
41 more precise than one step of OSCCAL. It is therefore not suitable to tune an
42 8 MHz oscillator to 12.5 MHz.
43 
44 Thanks to Henrik Haftmann for the idea to this routine!
45 */
46 
47 #define TIMER0_PRESCALING 64 /* must match the configuration for TIMER0 in main */
48 #define TOLERATED_DEVIATION_PPT 5 /* max clock deviation before we tune in 1/10 % */
49 /* derived constants: */
50 #define EXPECTED_TIMER0_INCREMENT ((F_CPU / (1000 * TIMER0_PRESCALING)) & 0xff)
51 #define TOLERATED_DEVIATION (TOLERATED_DEVIATION_PPT * F_CPU / (1000000 * TIMER0_PRESCALING))
52 
53 #ifdef __ASSEMBLER__
54 macro tuneOsccal
55  push YH ;[0]
56  in YL, TCNT0 ;[2]
57  lds YH, lastTimer0Value ;[3]
58  sts lastTimer0Value, YL ;[5]
59  sub YL, YH ;[7] time passed since last frame
60  subi YL, EXPECTED_TIMER0_INCREMENT ;[8]
61 #if OSCCAL > 0x3f /* outside I/O addressable range */
62  lds YH, OSCCAL ;[6]
63 #else
64  in YH, OSCCAL ;[6] assembler modle uses __SFR_OFFSET == 0
65 #endif
66  cpi YL, TOLERATED_DEVIATION + 1 ;[10]
67  brmi notTooHigh ;[11]
68  subi YH, 1 ;[12] clock rate was too high
69 ; brcs tuningOverflow ; optionally check for overflow
70  rjmp osctuneDone ;[13]
71 notTooHigh:
72  cpi YL, -TOLERATED_DEVIATION ;[13]
73  brpl osctuneDone ;[14] not too low
74  inc YH ;[15] clock rate was too low
75 ; breq tuningOverflow ; optionally check for overflow
76 osctuneDone:
77 #if OSCCAL > 0x3f /* outside I/O addressable range */
78  sts OSCCAL, YH ;[12-13] store tuned value
79 #else
80  out OSCCAL, YH ;[12-13] store tuned value
81 #endif
82 tuningOverflow:
83  pop YH ;[17]
84  endm ;[19] max number of cycles
85 #endif
86 
87 #define USB_SOF_HOOK tuneOsccal
string out
Definition: rf-id-control.py:34
#define macro
Definition: usbportability.h:133
#define max(a, b)
Definition: Arduino.h:83
#define endm
Definition: usbportability.h:134
#define TOLERATED_DEVIATION
Definition: osctune.h:51
#define EXPECTED_TIMER0_INCREMENT
Definition: osctune.h:50
#define not
Definition: iso646.h:35