u8g_delay.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. u8g_delay.c
  3. Universal 8bit Graphics Library
  4. Copyright (c) 2011, olikraus@gmail.com
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice, this list
  9. of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice, this
  11. list of conditions and the following disclaimer in the documentation and/or other
  12. materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  14. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. void u8g_Delay(uint16_t val) Delay by "val" milliseconds
  27. void u8g_MicroDelay(void) Delay be one microsecond
  28. void u8g_10MicroDelay(void) Delay by 10 microseconds
  29. */
  30. #include "u8g.h"
  31. /*==== Part 1: Derive suitable delay procedure ====*/
  32. #if defined(ARDUINO)
  33. # if ARDUINO < 100
  34. # include <WProgram.h>
  35. # else
  36. # include <Arduino.h>
  37. # endif
  38. # if defined(__AVR__)
  39. # define USE_AVR_DELAY
  40. # elif defined(__PIC32MX)
  41. # define USE_PIC32_DELAY
  42. # elif defined(__arm__) /* Arduino Due & Teensy */
  43. # define USE_ARDUINO_DELAY
  44. # else
  45. # define USE_ARDUINO_DELAY
  46. # endif
  47. #elif defined(U8G_RASPBERRY_PI)
  48. # define USE_RASPBERRYPI_DELAY
  49. #elif defined(__AVR__)
  50. # define USE_AVR_DELAY
  51. #elif defined(__18CXX)
  52. # define USE_PIC18_DELAY
  53. #elif defined(__arm__)
  54. /* do not define anything, all procedures are expected to be defined outside u8glib */
  55. /*
  56. void u8g_Delay(uint16_t val);
  57. void u8g_MicroDelay(void);
  58. void u8g_10MicroDelay(void);
  59. */
  60. #else
  61. # define USE_DUMMY_DELAY
  62. #endif
  63. /*==== Part 2: Definition of the delay procedures ====*/
  64. /*== Raspberry Pi Delay ==*/
  65. #if defined (USE_RASPBERRYPI_DELAY)
  66. #include <wiringPi.h>
  67. //#include "/usr/local/include/wiringPi.h"
  68. void u8g_Delay(uint16_t val) {
  69. //delay(val);
  70. //usleep((uint32_t)val*(uint32_t)1000);
  71. delayMicroseconds((uint32_t)val*(uint32_t)1000);
  72. }
  73. void u8g_MicroDelay(void)
  74. {
  75. usleep(1);
  76. }
  77. void u8g_10MicroDelay(void)
  78. {
  79. usleep(10);
  80. }
  81. #endif
  82. /*== AVR Delay ==*/
  83. #if defined(USE_AVR_DELAY)
  84. #include <avr/interrupt.h>
  85. #include <avr/io.h>
  86. #include <util/delay.h>
  87. /*
  88. Delay by the provided number of milliseconds.
  89. Thus, a 16 bit value will allow a delay of 0..65 seconds
  90. Makes use of the _delay_loop_2
  91. _delay_loop_2 will do a delay of n * 4 prozessor cycles.
  92. with f = F_CPU cycles per second,
  93. n = f / (1000 * 4 )
  94. with f = 16000000 the result is 4000
  95. with f = 1000000 the result is 250
  96. the millisec loop, gcc requires the following overhead:
  97. - movev 1
  98. - subwi 2x2
  99. - bne i 2
  100. ==> 7 cycles
  101. ==> must be devided by 4, rounded up 7/4 = 2
  102. */
  103. void u8g_Delay(uint16_t val)
  104. {
  105. /* old version did a call to the arduino lib: delay(val); */
  106. while( val != 0 )
  107. {
  108. _delay_loop_2( (F_CPU / 4000 ) -2);
  109. val--;
  110. }
  111. }
  112. /* delay by one micro second */
  113. void u8g_MicroDelay(void)
  114. {
  115. #if (F_CPU / 4000000 ) > 0
  116. _delay_loop_2( (F_CPU / 4000000 ) );
  117. #endif
  118. }
  119. /* delay by 10 micro seconds */
  120. void u8g_10MicroDelay(void)
  121. {
  122. #if (F_CPU / 400000 ) > 0
  123. _delay_loop_2( (F_CPU / 400000 ) );
  124. #endif
  125. }
  126. #endif
  127. /*== Delay for PIC18 (not tested) ==*/
  128. #if defined(USE_PIC18_DELAY)
  129. #include <delays.h>
  130. #define GetSystemClock() (64000000ul) // Hz
  131. #define GetInstructionClock() (GetSystemClock()/4)
  132. void u8g_Delay(uint16_t val)
  133. {/*
  134. unsigned int _iTemp = (val);
  135. while(_iTemp--)
  136. Delay1KTCYx((GetInstructionClock()+999999)/1000000);
  137. */
  138. }
  139. void u8g_MicroDelay(void)
  140. {
  141. /* not implemented */
  142. }
  143. void u8g_10MicroDelay(void)
  144. {
  145. /* not implemented */
  146. }
  147. #endif
  148. /*== Arduino Delay ==*/
  149. #if defined(USE_ARDUINO_DELAY)
  150. void u8g_Delay(uint16_t val)
  151. {
  152. #if defined(__arm__)
  153. delayMicroseconds((uint32_t)val*(uint32_t)1000);
  154. #else
  155. delay(val);
  156. #endif
  157. }
  158. void u8g_MicroDelay(void)
  159. {
  160. delayMicroseconds(1);
  161. }
  162. void u8g_10MicroDelay(void)
  163. {
  164. delayMicroseconds(10);
  165. }
  166. #endif
  167. #if defined(USE_PIC32_DELAY)
  168. /*
  169. Assume chipkit here with F_CPU correctly defined
  170. The problem was, that u8g_Delay() is called within the constructor.
  171. It seems that the chipkit is not fully setup at this time, so a
  172. call to delay() will not work. So here is my own implementation.
  173. */
  174. #define CPU_COUNTS_PER_SECOND (F_CPU/2UL)
  175. #define TICKS_PER_MILLISECOND (CPU_COUNTS_PER_SECOND/1000UL)
  176. #include "plib.h"
  177. void u8g_Delay(uint16_t val)
  178. {
  179. uint32_t d;
  180. uint32_t s;
  181. d = val;
  182. d *= TICKS_PER_MILLISECOND;
  183. s = ReadCoreTimer();
  184. while ( (uint32_t)(ReadCoreTimer() - s) < d )
  185. ;
  186. }
  187. void u8g_MicroDelay(void)
  188. {
  189. uint32_t d;
  190. uint32_t s;
  191. d = TICKS_PER_MILLISECOND/1000;
  192. s = ReadCoreTimer();
  193. while ( (uint32_t)(ReadCoreTimer() - s) < d )
  194. ;
  195. }
  196. void u8g_10MicroDelay(void)
  197. {
  198. uint32_t d;
  199. uint32_t s;
  200. d = TICKS_PER_MILLISECOND/100;
  201. s = ReadCoreTimer();
  202. while ( (uint32_t)(ReadCoreTimer() - s) < d )
  203. ;
  204. }
  205. #endif
  206. /*== Any other systems: Dummy Delay ==*/
  207. #if defined(USE_DUMMY_DELAY)
  208. void u8g_Delay(uint16_t val)
  209. {
  210. /* do not know how to delay... */
  211. }
  212. void u8g_MicroDelay(void)
  213. {
  214. }
  215. void u8g_10MicroDelay(void)
  216. {
  217. }
  218. #endif