iforce2d Chris / Mbed 2 deprecated ubxDistanceMeter

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers u8g_delay.c Source File

u8g_delay.c

00001 /*
00002 
00003   u8g_delay.c
00004 
00005   Universal 8bit Graphics Library
00006   
00007   Copyright (c) 2011, olikraus@gmail.com
00008   All rights reserved.
00009 
00010   Redistribution and use in source and binary forms, with or without modification, 
00011   are permitted provided that the following conditions are met:
00012 
00013   * Redistributions of source code must retain the above copyright notice, this list 
00014     of conditions and the following disclaimer.
00015     
00016   * Redistributions in binary form must reproduce the above copyright notice, this 
00017     list of conditions and the following disclaimer in the documentation and/or other 
00018     materials provided with the distribution.
00019 
00020   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
00021   CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
00022   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00023   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00024   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
00025   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
00026   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
00027   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
00028   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
00029   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
00030   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
00031   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00032   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
00033 
00034 
00035   void u8g_Delay(uint16_t val)      Delay by "val" milliseconds
00036   void u8g_MicroDelay(void)     Delay be one microsecond
00037   void u8g_10MicroDelay(void)   Delay by 10 microseconds
00038 
00039   
00040 */
00041 
00042 
00043 #include "u8g.h"
00044 
00045 /*==== Part 1: Derive suitable delay procedure ====*/
00046 
00047 #if defined(ARDUINO)
00048 
00049 #  if ARDUINO < 100 
00050 #    include <WProgram.h> 
00051 #  else 
00052 #    include <Arduino.h> 
00053 #  endif
00054 
00055 #  if defined(__AVR__)
00056 #    define USE_AVR_DELAY
00057 #  elif defined(__PIC32MX)
00058 #    define USE_PIC32_DELAY
00059 #  elif defined(__arm__)        /* Arduino Due */
00060 #    define USE_ARDUINO_DELAY
00061 #  else
00062 #    define USE_ARDUINO_DELAY
00063 #  endif
00064 #elif defined(__AVR__)
00065 #  define USE_AVR_DELAY
00066 #elif defined(__18CXX)
00067 #  define USE_PIC18_DELAY
00068 #elif defined(__arm__)
00069 /* do not define anything, all procedures are expected to be defined outside u8glib */
00070 
00071 /*
00072 void u8g_Delay(uint16_t val);
00073 void u8g_MicroDelay(void);
00074 void u8g_10MicroDelay(void);
00075 */
00076 
00077 #else
00078 #  define USE_DUMMY_DELAY
00079 #endif
00080 
00081 
00082 
00083 /*==== Part 2: Definition of the delay procedures ====*/
00084 
00085 /*== AVR Delay ==*/
00086 
00087 #if defined(USE_AVR_DELAY)
00088 #include <avr/interrupt.h>
00089 #include <avr/io.h>
00090 #include <util/delay.h>
00091 
00092 /*
00093   Delay by the provided number of milliseconds.
00094   Thus, a 16 bit value will allow a delay of 0..65 seconds
00095   Makes use of the _delay_loop_2
00096   
00097   _delay_loop_2 will do a delay of n * 4 prozessor cycles.
00098   with f = F_CPU cycles per second,
00099   n = f / (1000 * 4 )
00100   with f = 16000000 the result is 4000
00101   with f = 1000000 the result is 250
00102   
00103   the millisec loop, gcc requires the following overhead:
00104   - movev 1
00105   - subwi 2x2
00106   - bne i 2
00107   ==> 7 cycles
00108   ==> must be devided by 4, rounded up 7/4 = 2
00109 */
00110 void u8g_Delay(uint16_t val)
00111 {
00112   /* old version did a call to the arduino lib: delay(val); */
00113   while( val != 0 )
00114   {
00115     _delay_loop_2( (F_CPU / 4000 ) -2);
00116     val--;
00117   }
00118 }
00119 
00120 /* delay by one micro second */
00121 void u8g_MicroDelay(void)
00122 {
00123 #if (F_CPU / 4000000 ) > 0 
00124   _delay_loop_2( (F_CPU / 4000000 ) );
00125 #endif
00126 }
00127 
00128 /* delay by 10 micro seconds */
00129 void u8g_10MicroDelay(void)
00130 {
00131 #if (F_CPU / 400000 ) > 0 
00132   _delay_loop_2( (F_CPU / 400000 ) );
00133 #endif
00134 }
00135 
00136 #endif 
00137 
00138 
00139 /*== Delay for PIC18 (not tested) ==*/
00140 
00141 #if defined(USE_PIC18_DELAY)
00142 #include <delays.h>
00143 #define GetSystemClock()        (64000000ul)      // Hz
00144 #define GetInstructionClock()   (GetSystemClock()/4)
00145 
00146 void u8g_Delay(uint16_t val)
00147 {/*
00148     unsigned int _iTemp = (val);
00149     while(_iTemp--)     
00150         Delay1KTCYx((GetInstructionClock()+999999)/1000000);
00151         */
00152 }
00153 void u8g_MicroDelay(void)
00154 {
00155   /* not implemented */
00156 }
00157 void u8g_10MicroDelay(void)
00158 {
00159   /* not implemented */
00160 }
00161 #endif
00162 
00163 
00164 /*== Arduino Delay ==*/
00165 #if defined(USE_ARDUINO_DELAY)
00166 void u8g_Delay(uint16_t val)
00167 {
00168 #if defined(__arm__)
00169     delayMicroseconds((uint32_t)val*(uint32_t)1000);
00170 #else
00171     delay(val);
00172 #endif
00173 }
00174 void u8g_MicroDelay(void)
00175 {
00176     delayMicroseconds(1);
00177 }
00178 void u8g_10MicroDelay(void)
00179 {
00180     delayMicroseconds(10);
00181 }
00182 #endif
00183 
00184 #if defined(USE_PIC32_DELAY)
00185 /* 
00186   Assume chipkit here with F_CPU correctly defined
00187   The problem was, that u8g_Delay() is called within the constructor.
00188   It seems that the chipkit is not fully setup at this time, so a
00189   call to delay() will not work. So here is my own implementation.
00190 
00191 */
00192 #define CPU_COUNTS_PER_SECOND (F_CPU/2UL)
00193 #define TICKS_PER_MILLISECOND  (CPU_COUNTS_PER_SECOND/1000UL)
00194 #include "plib.h"
00195 void u8g_Delay(uint16_t val)
00196 {
00197     uint32_t d;
00198     uint32_t s;
00199     d = val;
00200     d *= TICKS_PER_MILLISECOND;
00201     s = ReadCoreTimer();
00202     while ( (uint32_t)(ReadCoreTimer() - s) < d )
00203         ;
00204 } 
00205 
00206 void u8g_MicroDelay(void)
00207 {
00208     uint32_t d;
00209     uint32_t s;
00210     d = TICKS_PER_MILLISECOND/1000;
00211     s = ReadCoreTimer();
00212     while ( (uint32_t)(ReadCoreTimer() - s) < d )
00213         ;
00214 } 
00215 
00216 void u8g_10MicroDelay(void)
00217 {
00218     uint32_t d;
00219     uint32_t s;
00220     d = TICKS_PER_MILLISECOND/100;
00221     s = ReadCoreTimer();
00222     while ( (uint32_t)(ReadCoreTimer() - s) < d )
00223         ;
00224 } 
00225 
00226 #endif
00227 
00228 /*== Any other systems: Dummy Delay ==*/
00229 #if defined(USE_DUMMY_DELAY)
00230 void u8g_Delay(uint16_t val)
00231 {
00232     /* do not know how to delay... */
00233 }
00234 void u8g_MicroDelay(void)
00235 {
00236 }
00237 void u8g_10MicroDelay(void)
00238 {
00239 }
00240 #endif
00241