u8g_delay.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. /* issue 353 */
  39. #if defined(ARDUINO_ARCH_SAMD)
  40. # include <delay.h>
  41. #endif
  42. # if defined(__AVR__)
  43. # define USE_AVR_DELAY
  44. # elif defined(__PIC32MX)
  45. # define USE_PIC32_DELAY
  46. # elif defined(__arm__) /* Arduino Due & Teensy */
  47. # define USE_ARDUINO_DELAY
  48. # else
  49. # define USE_ARDUINO_DELAY
  50. # endif
  51. #elif defined(_GNU_SOURCE)
  52. # define USE_LINUX_DELAY
  53. #elif defined(__MSP430__)
  54. # define USE_MSP430_DELAY
  55. #elif defined(U8G_RASPBERRY_PI)
  56. # define USE_RASPBERRYPI_DELAY
  57. #elif defined(__AVR__)
  58. # define USE_AVR_DELAY
  59. #elif defined(__18CXX)
  60. # define USE_PIC18_DELAY
  61. #elif defined(U8G_CYPRESS_PSOC5)
  62. #define USE_PSOC5_DELAY
  63. #elif defined(__arm__) || defined(__XTENSA__)
  64. /* do not define anything, all procedures are expected to be defined outside u8glib */
  65. /*
  66. void u8g_Delay(uint16_t val);
  67. void u8g_MicroDelay(void);
  68. void u8g_10MicroDelay(void);
  69. */
  70. #else
  71. # define USE_DUMMY_DELAY
  72. #endif
  73. /*==== Part 2: Definition of the delay procedures ====*/
  74. /*== Raspberry Pi Delay ==*/
  75. #if defined (USE_RASPBERRYPI_DELAY)
  76. #include <wiringPi.h>
  77. //#include "/usr/local/include/wiringPi.h"
  78. void u8g_Delay(uint16_t val) {
  79. //delay(val);
  80. //usleep((uint32_t)val*(uint32_t)1000);
  81. delayMicroseconds((uint32_t)val*(uint32_t)1000);
  82. }
  83. void u8g_MicroDelay(void)
  84. {
  85. usleep(1);
  86. }
  87. void u8g_10MicroDelay(void)
  88. {
  89. usleep(10);
  90. }
  91. #endif
  92. #if defined(USE_LINUX_DELAY)
  93. void u8g_Delay(uint16_t val) {
  94. //delay(val);
  95. usleep((uint32_t)val*(uint32_t)1000);
  96. }
  97. void u8g_MicroDelay(void)
  98. {
  99. usleep(1);
  100. }
  101. void u8g_10MicroDelay(void)
  102. {
  103. usleep(10);
  104. }
  105. #endif
  106. /*== AVR Delay ==*/
  107. #if defined(USE_AVR_DELAY)
  108. #include <avr/interrupt.h>
  109. #include <avr/io.h>
  110. #include <util/delay.h>
  111. /*
  112. Delay by the provided number of milliseconds.
  113. Thus, a 16 bit value will allow a delay of 0..65 seconds
  114. Makes use of the _delay_loop_2
  115. _delay_loop_2 will do a delay of n * 4 prozessor cycles.
  116. with f = F_CPU cycles per second,
  117. n = f / (1000 * 4 )
  118. with f = 16000000 the result is 4000
  119. with f = 1000000 the result is 250
  120. the millisec loop, gcc requires the following overhead:
  121. - movev 1
  122. - subwi 2x2
  123. - bne i 2
  124. ==> 7 cycles
  125. ==> must be devided by 4, rounded up 7/4 = 2
  126. */
  127. void u8g_Delay(uint16_t val)
  128. {
  129. /* old version did a call to the arduino lib: delay(val); */
  130. while( val != 0 )
  131. {
  132. _delay_loop_2( (F_CPU / 4000 ) -2);
  133. val--;
  134. }
  135. }
  136. /* delay by one micro second */
  137. void u8g_MicroDelay(void)
  138. {
  139. #if (F_CPU / 4000000 ) > 0
  140. _delay_loop_2( (F_CPU / 4000000 ) );
  141. #endif
  142. }
  143. /* delay by 10 micro seconds */
  144. void u8g_10MicroDelay(void)
  145. {
  146. #if (F_CPU / 400000 ) > 0
  147. _delay_loop_2( (F_CPU / 400000 ) );
  148. #endif
  149. }
  150. #endif
  151. /*== Delay for PIC18 (not tested) ==*/
  152. #if defined(USE_PIC18_DELAY)
  153. #include <delays.h>
  154. #define GetSystemClock() (64000000ul) // Hz
  155. #define GetInstructionClock() (GetSystemClock()/4)
  156. void u8g_Delay(uint16_t val)
  157. {/*
  158. unsigned int _iTemp = (val);
  159. while(_iTemp--)
  160. Delay1KTCYx((GetInstructionClock()+999999)/1000000);
  161. */
  162. }
  163. void u8g_MicroDelay(void)
  164. {
  165. /* not implemented */
  166. }
  167. void u8g_10MicroDelay(void)
  168. {
  169. /* not implemented */
  170. }
  171. #endif
  172. /*== Arduino Delay ==*/
  173. #if defined(USE_ARDUINO_DELAY)
  174. void u8g_Delay(uint16_t val)
  175. {
  176. #if defined(__arm__)
  177. delayMicroseconds((uint32_t)val*(uint32_t)1000);
  178. #else
  179. delay(val);
  180. #endif
  181. }
  182. void u8g_MicroDelay(void)
  183. {
  184. delayMicroseconds(1);
  185. }
  186. void u8g_10MicroDelay(void)
  187. {
  188. delayMicroseconds(10);
  189. }
  190. #endif
  191. #if defined(USE_PIC32_DELAY)
  192. /*
  193. Assume chipkit here with F_CPU correctly defined
  194. The problem was, that u8g_Delay() is called within the constructor.
  195. It seems that the chipkit is not fully setup at this time, so a
  196. call to delay() will not work. So here is my own implementation.
  197. */
  198. #define CPU_COUNTS_PER_SECOND (F_CPU/2UL)
  199. #define TICKS_PER_MILLISECOND (CPU_COUNTS_PER_SECOND/1000UL)
  200. #include "plib.h"
  201. void u8g_Delay(uint16_t val)
  202. {
  203. uint32_t d;
  204. uint32_t s;
  205. d = val;
  206. d *= TICKS_PER_MILLISECOND;
  207. s = ReadCoreTimer();
  208. while ( (uint32_t)(ReadCoreTimer() - s) < d )
  209. ;
  210. }
  211. void u8g_MicroDelay(void)
  212. {
  213. uint32_t d;
  214. uint32_t s;
  215. d = TICKS_PER_MILLISECOND/1000;
  216. s = ReadCoreTimer();
  217. while ( (uint32_t)(ReadCoreTimer() - s) < d )
  218. ;
  219. }
  220. void u8g_10MicroDelay(void)
  221. {
  222. uint32_t d;
  223. uint32_t s;
  224. d = TICKS_PER_MILLISECOND/100;
  225. s = ReadCoreTimer();
  226. while ( (uint32_t)(ReadCoreTimer() - s) < d )
  227. ;
  228. }
  229. #endif
  230. #if defined(USE_MSP430_DELAY)
  231. #include <msp430.h>
  232. #ifndef F_CPU
  233. #define F_CPU 1000000UL
  234. #endif
  235. void u8g_Delay(uint16_t val)
  236. {
  237. int t;
  238. for (t=0; t < val; t++)
  239. {
  240. __delay_cycles(F_CPU/1000UL);
  241. }
  242. }
  243. void u8g_MicroDelay(void)
  244. {
  245. __delay_cycles(F_CPU/1000000UL);
  246. }
  247. void u8g_10MicroDelay(void)
  248. {
  249. __delay_cycles(F_CPU/100000UL);
  250. }
  251. #endif
  252. #if defined USE_PSOC5_DELAY
  253. #include <project.h>
  254. void u8g_Delay(uint16_t val) {CyDelay(val);};
  255. void u8g_MicroDelay(void) {CyDelay(1);};
  256. void u8g_10MicroDelay(void) {CyDelay(10);};
  257. #endif
  258. /*== Any other systems: Dummy Delay ==*/
  259. #if defined(USE_DUMMY_DELAY)
  260. void u8g_Delay(uint16_t val)
  261. {
  262. /* do not know how to delay... */
  263. }
  264. void u8g_MicroDelay(void)
  265. {
  266. }
  267. void u8g_10MicroDelay(void)
  268. {
  269. }
  270. #endif