]> the.earth.li Git - riso-kagaku-clone.git/blobdiff - libs-device/osccal.h
Move to micronucleus assembly for OSCCAL calibration
[riso-kagaku-clone.git] / libs-device / osccal.h
index 1ed600697ca7d1c8a02f2d584af549d4b70a711e..3452aa77e8c8b70a24df6647186c1c1c529827a1 100644 (file)
@@ -1,5 +1,6 @@
 /* Name: osccal.h
  * Author: Christian Starkjohann
 /* Name: osccal.h
  * Author: Christian Starkjohann
+ * Changes 2013-11-04 cpldcpu@gmail.com
  * Creation Date: 2008-04-10
  * Tabsize: 4
  * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
  * Creation Date: 2008-04-10
  * Tabsize: 4
  * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
@@ -13,28 +14,18 @@ oscillator so that the CPU runs at F_CPU (F_CPU is a macro which must be
 defined when the module is compiled, best passed in the compiler command
 line). The time reference is the USB frame clock of 1 kHz available
 immediately after a USB RESET condition. Timing is done by counting CPU
 defined when the module is compiled, best passed in the compiler command
 line). The time reference is the USB frame clock of 1 kHz available
 immediately after a USB RESET condition. Timing is done by counting CPU
-cycles, so all interrupts must be disabled while the calibration runs. For
-low level timing measurements, usbMeasureFrameLength() is called. This
-function must be enabled in usbconfig.h by defining
-USB_CFG_HAVE_MEASURE_FRAME_LENGTH to 1. It is recommended to call
-calibrateOscillator() from the reset hook in usbconfig.h:
+cycles, so all interrupts must be disabled while the calibration runs.
+The size optimized assembler implementation includes its own implementation
+of usbMeasureFrameLength. Therefore USB_CFG_HAVE_MEASURE_FRAME_LENGTH should
+be set to 0 to avoid including unused code sections. It is recommended to call
+calibrateOscillatorASM() from the reset hook in usbconfig.h by including osccal.h:
 
 
-#ifndef __ASSEMBLER__
-#include <avr/interrupt.h>  // for sei()
-extern void calibrateOscillator(void);
-#endif
-#define USB_RESET_HOOK(resetStarts)  if(!resetStarts){cli(); calibrateOscillator(); sei();}
+#include "osccal.h"
 
 This routine is an alternative to the continuous synchronization described
 in osctune.h.
 
 
 This routine is an alternative to the continuous synchronization described
 in osctune.h.
 
-Algorithm used:
-calibrateOscillator() first does a binary search in the OSCCAL register for
-the best matching oscillator frequency. Then it does a next neighbor search
-to find the value with the lowest clock rate deviation. It is guaranteed to
-find the best match among neighboring values, but for version 5 oscillators
-(which have a discontinuous relationship between OSCCAL and frequency) a
-better match might be available in another OSCCAL region.
+Algorithm used: see osccalASM.S
 
 Limitations:
 This calibration algorithm may try OSCCAL values of up to 192 even if the
 
 Limitations:
 This calibration algorithm may try OSCCAL values of up to 192 even if the
@@ -50,7 +41,10 @@ deviation! All other frequency modules require at least 0.2% precision.
 #ifndef __OSCCAL_H_INCLUDED__
 #define __OSCCAL_H_INCLUDED__
 
 #ifndef __OSCCAL_H_INCLUDED__
 #define __OSCCAL_H_INCLUDED__
 
-void    calibrateOscillator(void);
+#ifndef __ASSEMBLER__
+void    calibrateOscillatorASM(void);
+#define USB_RESET_HOOK(resetStarts)  if(!resetStarts){ calibrateOscillatorASM();}
+#endif
 /* This function calibrates the RC oscillator so that the CPU runs at F_CPU.
  * It MUST be called immediately after the end of a USB RESET condition!
  * Disable all interrupts during the call!
 /* This function calibrates the RC oscillator so that the CPU runs at F_CPU.
  * It MUST be called immediately after the end of a USB RESET condition!
  * Disable all interrupts during the call!