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