hadif azli / Mbed 2 deprecated TEST123

Dependencies:   mbed Blynk

Committer:
lixianyu
Date:
Fri Jun 10 15:20:20 2016 +0000
Revision:
0:d8f4c441e032
u8glib???????????i2c???

Who changed what in which revision?

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