My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

u8g_delay.c 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. */
  27. #include "u8g.h"
  28. /*==== Part 1: Derive suitable delay procedure ====*/
  29. #if defined(ARDUINO)
  30. # if defined(__AVR__)
  31. # define USE_AVR_DELAY
  32. # elif defined(__PIC32MX)
  33. # define USE_PIC32_DELAY
  34. # elif defined(__arm__) /* Arduino Due */
  35. # define USE_ARDUINO_DELAY
  36. # else
  37. # define USE_ARDUINO_DELAY
  38. # endif
  39. #elif defined(__AVR__)
  40. # define USE_AVR_DELAY
  41. #elif defined(__18CXX)
  42. # define USE_PIC18_DELAY
  43. #else
  44. # define USE_DUMMY_DELAY
  45. #endif
  46. /*==== Part 2: Definition of the delay procedures ====*/
  47. /*== AVR Delay ==*/
  48. #if defined(USE_AVR_DELAY)
  49. #include <avr/interrupt.h>
  50. #include <avr/io.h>
  51. #include <util/delay.h>
  52. /*
  53. Delay by the provided number of milliseconds.
  54. Thus, a 16 bit value will allow a delay of 0..65 seconds
  55. Makes use of the _delay_loop_2
  56. _delay_loop_2 will do a delay of n * 4 prozessor cycles.
  57. with f = F_CPU cycles per second,
  58. n = f / (1000 * 4 )
  59. with f = 16000000 the result is 4000
  60. with f = 1000000 the result is 250
  61. the millisec loop, gcc requires the following overhead:
  62. - movev 1
  63. - subwi 2x2
  64. - bne i 2
  65. ==> 7 cycles
  66. ==> must be devided by 4, rounded up 7/4 = 2
  67. */
  68. void u8g_Delay(uint16_t val)
  69. {
  70. /* old version did a call to the arduino lib: delay(val); */
  71. while( val != 0 )
  72. {
  73. _delay_loop_2( (F_CPU / 4000 ) -2);
  74. val--;
  75. }
  76. }
  77. /* delay by one micro second */
  78. void u8g_MicroDelay(void)
  79. {
  80. #if (F_CPU / 4000000 ) > 0
  81. _delay_loop_2( (F_CPU / 4000000 ) );
  82. #endif
  83. }
  84. /* delay by 10 micro seconds */
  85. void u8g_10MicroDelay(void)
  86. {
  87. #if (F_CPU / 400000 ) > 0
  88. _delay_loop_2( (F_CPU / 400000 ) );
  89. #endif
  90. }
  91. #endif
  92. /*== Delay for PIC18 (not tested) ==*/
  93. #if defined(USE_PIC18_DELAY)
  94. #include <delays.h>
  95. #define GetSystemClock() (64000000ul) // Hz
  96. #define GetInstructionClock() (GetSystemClock()/4)
  97. void u8g_Delay(uint16_t val)
  98. {/*
  99. unsigned int _iTemp = (val);
  100. while(_iTemp--)
  101. Delay1KTCYx((GetInstructionClock()+999999)/1000000);
  102. */
  103. }
  104. void u8g_MicroDelay(void)
  105. {
  106. /* not implemented */
  107. }
  108. void u8g_10MicroDelay(void)
  109. {
  110. /* not implemented */
  111. }
  112. #endif
  113. /*== Arduino Delay ==*/
  114. #if defined(USE_ARDUINO_DELAY)
  115. void u8g_Delay(uint16_t val)
  116. {
  117. delay(val);
  118. }
  119. void u8g_MicroDelay(void)
  120. {
  121. delayMicroseconds(1);
  122. }
  123. void u8g_10MicroDelay(void)
  124. {
  125. delayMicroseconds(10);
  126. }
  127. #endif
  128. #if defined(USE_PIC32_DELAY)
  129. /*
  130. Assume chipkit here with F_CPU correctly defined
  131. The problem was, that u8g_Delay() is called within the constructor.
  132. It seems that the chipkit is not fully setup at this time, so a
  133. call to delay() will not work. So here is my own implementation.
  134. */
  135. #define CPU_COUNTS_PER_SECOND (F_CPU/2UL)
  136. #define TICKS_PER_MILLISECOND (CPU_COUNTS_PER_SECOND/1000UL)
  137. #include "plib.h"
  138. void u8g_Delay(uint16_t val)
  139. {
  140. uint32_t d;
  141. uint32_t s;
  142. d = val;
  143. d *= TICKS_PER_MILLISECOND;
  144. s = ReadCoreTimer();
  145. while ( (uint32_t)(ReadCoreTimer() - s) < d )
  146. ;
  147. }
  148. void u8g_MicroDelay(void)
  149. {
  150. uint32_t d;
  151. uint32_t s;
  152. d = TICKS_PER_MILLISECOND/1000;
  153. s = ReadCoreTimer();
  154. while ( (uint32_t)(ReadCoreTimer() - s) < d )
  155. ;
  156. }
  157. void u8g_10MicroDelay(void)
  158. {
  159. uint32_t d;
  160. uint32_t s;
  161. d = TICKS_PER_MILLISECOND/100;
  162. s = ReadCoreTimer();
  163. while ( (uint32_t)(ReadCoreTimer() - s) < d )
  164. ;
  165. }
  166. #endif
  167. /*== Any other systems: Dummy Delay ==*/
  168. #if defined(USE_DUMMY_DELAY)
  169. void u8g_Delay(uint16_t val)
  170. {
  171. /* do not know how to delay... */
  172. }
  173. void u8g_MicroDelay(void)
  174. {
  175. }
  176. void u8g_10MicroDelay(void)
  177. {
  178. }
  179. #endif