Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
u8g_com_arduino_t6963.c
00001 /* 00002 00003 u8g_com_arduino_t6963.c 00004 00005 Universal 8bit Graphics Library 00006 00007 Copyright (c) 2011, olikraus@gmail.com 00008 All rights reserved. 00009 00010 Redistribution and use in source and binary forms, with or without modification, 00011 are permitted provided that the following conditions are met: 00012 00013 * Redistributions of source code must retain the above copyright notice, this list 00014 of conditions and the following disclaimer. 00015 00016 * Redistributions in binary form must reproduce the above copyright notice, this 00017 list of conditions and the following disclaimer in the documentation and/or other 00018 materials provided with the distribution. 00019 00020 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 00021 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 00022 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00023 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00024 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 00025 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00027 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 00030 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00031 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 00032 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 00034 00035 PIN_D0 8 00036 PIN_D1 9 00037 PIN_D2 10 00038 PIN_D3 11 00039 PIN_D4 4 00040 PIN_D5 5 00041 PIN_D6 6 00042 PIN_D7 7 00043 00044 PIN_CS 14 00045 PIN_A0 15 00046 PIN_RESET 16 00047 PIN_WR 17 00048 PIN_RD 18 00049 00050 u8g_InitRW8Bit(u8g, dev, d0, d1, d2, d3, d4, d5, d6, d7, cs, a0, wr, rd, reset) 00051 u8g_InitRW8Bit(u8g, dev, 8, 9, 10, 11, 4, 5, 6, 7, 14, 15, 17, 18, 16) 00052 00053 Update for ATOMIC operation done (01 Jun 2013) 00054 U8G_ATOMIC_OR(ptr, val) 00055 U8G_ATOMIC_AND(ptr, val) 00056 U8G_ATOMIC_START(); 00057 U8G_ATOMIC_END(); 00058 00059 00060 */ 00061 00062 #include "u8g.h" 00063 00064 #if defined(ARDUINO) 00065 00066 #if ARDUINO < 100 00067 //#include <WProgram.h> 00068 #include <wiring_private.h> 00069 #include <pins_arduino.h> 00070 #else 00071 #include <Arduino.h> 00072 #endif 00073 00074 00075 #if defined(__PIC32MX) 00076 /* CHIPKIT PIC32 */ 00077 static volatile uint32_t *u8g_output_data_port[8]; 00078 static volatile uint32_t *u8g_input_data_port[8]; 00079 static volatile uint32_t *u8g_mode_port[8]; 00080 static uint32_t u8g_data_mask[8]; 00081 #else 00082 static volatile uint8_t *u8g_output_data_port[8]; 00083 static volatile uint8_t *u8g_input_data_port[8]; 00084 static volatile uint8_t *u8g_mode_port[8]; 00085 static uint8_t u8g_data_mask[8]; 00086 #endif 00087 00088 00089 00090 static void u8g_com_arduino_t6963_init(u8g_t *u8g) 00091 { 00092 u8g_output_data_port[0] = portOutputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D0])); 00093 u8g_input_data_port[0] = portInputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D0])); 00094 u8g_mode_port[0] = portModeRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D0])); 00095 u8g_data_mask[0] = digitalPinToBitMask(u8g->pin_list[U8G_PI_D0]); 00096 00097 u8g_output_data_port[1] = portOutputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D1])); 00098 u8g_input_data_port[1] = portInputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D1])); 00099 u8g_mode_port[1] = portModeRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D1])); 00100 u8g_data_mask[1] = digitalPinToBitMask(u8g->pin_list[U8G_PI_D1]); 00101 00102 u8g_output_data_port[2] = portOutputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D2])); 00103 u8g_input_data_port[2] = portInputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D2])); 00104 u8g_mode_port[2] = portModeRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D2])); 00105 u8g_data_mask[2] = digitalPinToBitMask(u8g->pin_list[U8G_PI_D2]); 00106 00107 u8g_output_data_port[3] = portOutputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D3])); 00108 u8g_input_data_port[3] = portInputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D3])); 00109 u8g_mode_port[3] = portModeRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D3])); 00110 u8g_data_mask[3] = digitalPinToBitMask(u8g->pin_list[U8G_PI_D3]); 00111 00112 u8g_output_data_port[4] = portOutputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D4])); 00113 u8g_input_data_port[4] = portInputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D4])); 00114 u8g_mode_port[4] = portModeRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D4])); 00115 u8g_data_mask[4] = digitalPinToBitMask(u8g->pin_list[U8G_PI_D4]); 00116 00117 u8g_output_data_port[5] = portOutputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D5])); 00118 u8g_input_data_port[5] = portInputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D5])); 00119 u8g_mode_port[5] = portModeRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D5])); 00120 u8g_data_mask[5] = digitalPinToBitMask(u8g->pin_list[U8G_PI_D5]); 00121 00122 u8g_output_data_port[6] = portOutputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D6])); 00123 u8g_input_data_port[6] = portInputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D6])); 00124 u8g_mode_port[6] = portModeRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D6])); 00125 u8g_data_mask[6] = digitalPinToBitMask(u8g->pin_list[U8G_PI_D6]); 00126 00127 u8g_output_data_port[7] = portOutputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D7])); 00128 u8g_input_data_port[7] = portInputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D7])); 00129 u8g_mode_port[7] = portModeRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D7])); 00130 u8g_data_mask[7] = digitalPinToBitMask(u8g->pin_list[U8G_PI_D7]); 00131 } 00132 00133 00134 static void u8g_com_arduino_t6963_write_data_pin(uint8_t pin, uint8_t val) 00135 { 00136 /* no ATOMIC protection required here, this is done by calling procedure */ 00137 if ( val != 0 ) 00138 *u8g_output_data_port[pin] |= u8g_data_mask[pin]; 00139 else 00140 *u8g_output_data_port[pin] &= ~u8g_data_mask[pin]; 00141 } 00142 00143 static void u8g_com_arduino_t6963_set_port_output(void) 00144 { 00145 uint8_t i; 00146 U8G_ATOMIC_START(); 00147 for( i = 0; i < 8; i++ ) 00148 { 00149 #if defined(__PIC32MX) 00150 /* CHIPKIT PIC32 */ 00151 *u8g_mode_port[i] |= u8g_data_mask[i]; 00152 #elif defined(__AVR__) 00153 *u8g_mode_port[i] |= u8g_data_mask[i]; 00154 #else 00155 /* TODO: use generic Arduino API */ 00156 *u8g_mode_port[i] |= u8g_data_mask[i]; 00157 #endif 00158 00159 } 00160 U8G_ATOMIC_END(); 00161 } 00162 00163 static void u8g_com_arduino_t6963_set_port_input(void) 00164 { 00165 uint8_t i; 00166 U8G_ATOMIC_START(); 00167 for( i = 0; i < 8; i++ ) 00168 { 00169 #if defined(__PIC32MX) 00170 /* CHIPKIT PIC32 */ 00171 *u8g_mode_port[i] &= ~u8g_data_mask[i]; 00172 #elif defined(__AVR__) 00173 /* avr */ 00174 *u8g_mode_port[i] &= ~u8g_data_mask[i]; 00175 *u8g_output_data_port[i] &= ~u8g_data_mask[i]; // no pullup 00176 #else 00177 /* TODO: use generic Arduino API */ 00178 *u8g_mode_port[i] &= ~u8g_data_mask[i]; 00179 *u8g_output_data_port[i] &= ~u8g_data_mask[i]; // no pullup 00180 #endif 00181 } 00182 U8G_ATOMIC_END(); 00183 } 00184 00185 00186 static void u8g_com_arduino_t6963_write(u8g_t *u8g, uint8_t val) 00187 { 00188 U8G_ATOMIC_START(); 00189 00190 u8g_com_arduino_t6963_write_data_pin( 0, val&1 ); 00191 val >>= 1; 00192 u8g_com_arduino_t6963_write_data_pin( 1, val&1 ); 00193 val >>= 1; 00194 u8g_com_arduino_t6963_write_data_pin( 2, val&1 ); 00195 val >>= 1; 00196 u8g_com_arduino_t6963_write_data_pin( 3, val&1 ); 00197 val >>= 1; 00198 00199 u8g_com_arduino_t6963_write_data_pin( 4, val&1 ); 00200 val >>= 1; 00201 u8g_com_arduino_t6963_write_data_pin( 5, val&1 ); 00202 val >>= 1; 00203 u8g_com_arduino_t6963_write_data_pin( 6, val&1 ); 00204 val >>= 1; 00205 u8g_com_arduino_t6963_write_data_pin( 7, val&1 ); 00206 val >>= 1; 00207 U8G_ATOMIC_END(); 00208 00209 u8g_com_arduino_digital_write(u8g, U8G_PI_WR, 0); 00210 u8g_MicroDelay(); /* 80ns, reference: t6963 datasheet */ 00211 u8g_com_arduino_digital_write(u8g, U8G_PI_WR, 1); 00212 u8g_MicroDelay(); /* 10ns, reference: t6963 datasheet */ 00213 } 00214 00215 static uint8_t u8g_com_arduino_t6963_read(u8g_t *u8g) 00216 { 00217 uint8_t val = 0; 00218 00219 u8g_com_arduino_digital_write(u8g, U8G_PI_RD, 0); 00220 u8g_MicroDelay(); /* 150ns, reference: t6963 datasheet */ 00221 00222 U8G_ATOMIC_START(); 00223 /* only read bits 0, 1 and 3 */ 00224 if ( (*u8g_input_data_port[3] & u8g_data_mask[3]) != 0 ) 00225 val++; 00226 val <<= 1; 00227 val <<= 1; 00228 if ( (*u8g_input_data_port[1] & u8g_data_mask[1]) != 0 ) 00229 val++; 00230 val <<= 1; 00231 if ( (*u8g_input_data_port[0] & u8g_data_mask[0]) != 0 ) 00232 val++; 00233 U8G_ATOMIC_END(); 00234 00235 u8g_com_arduino_digital_write(u8g, U8G_PI_RD, 1); 00236 u8g_MicroDelay(); /* 10ns, reference: t6963 datasheet */ 00237 00238 return val; 00239 } 00240 00241 #define U8G_STATUS_TIMEOUT 50 00242 00243 static uint8_t u8g_com_arduino_t6963_until_01_ok(u8g_t *u8g) 00244 { 00245 long x; 00246 00247 u8g_com_arduino_t6963_set_port_input(); 00248 x = millis(); 00249 x += U8G_STATUS_TIMEOUT; 00250 00251 for(;;) 00252 { 00253 if ( (u8g_com_arduino_t6963_read(u8g) & 3) == 3 ) 00254 break; 00255 if ( x < millis() ) 00256 return 0; 00257 } 00258 u8g_com_arduino_t6963_set_port_output(); 00259 return 1; 00260 } 00261 00262 static uint8_t u8g_com_arduino_t6963_until_3_ok(u8g_t *u8g) 00263 { 00264 long x; 00265 00266 u8g_com_arduino_t6963_set_port_input(); 00267 x = millis(); 00268 x += U8G_STATUS_TIMEOUT; 00269 00270 for(;;) 00271 { 00272 if ( (u8g_com_arduino_t6963_read(u8g) & 8) == 8 ) 00273 break; 00274 if ( x < millis() ) 00275 return 0; 00276 } 00277 u8g_com_arduino_t6963_set_port_output(); 00278 return 1; 00279 } 00280 00281 static uint8_t u8g_com_arduino_t6963_write_cmd(u8g_t *u8g, uint8_t val) 00282 { 00283 u8g_com_arduino_digital_write(u8g, U8G_PI_A0, 1); 00284 if ( u8g_com_arduino_t6963_until_01_ok(u8g) == 0 ) 00285 return 0; 00286 u8g_com_arduino_digital_write(u8g, U8G_PI_A0, 1); 00287 u8g_com_arduino_t6963_write(u8g, val); 00288 return 1; 00289 } 00290 00291 static uint8_t u8g_com_arduino_t6963_write_data(u8g_t *u8g, uint8_t val) 00292 { 00293 u8g_com_arduino_digital_write(u8g, U8G_PI_A0, 1); 00294 if ( u8g_com_arduino_t6963_until_01_ok(u8g) == 0 ) 00295 return 0; 00296 u8g_com_arduino_digital_write(u8g, U8G_PI_A0, 0); 00297 u8g_com_arduino_t6963_write(u8g, val); 00298 return 1; 00299 } 00300 00301 static uint8_t u8g_com_arduino_t6963_write_auto_data(u8g_t *u8g, uint8_t val) 00302 { 00303 u8g_com_arduino_digital_write(u8g, U8G_PI_A0, 1); 00304 if ( u8g_com_arduino_t6963_until_3_ok(u8g) == 0 ) 00305 return 0; 00306 u8g_com_arduino_digital_write(u8g, U8G_PI_A0, 0); 00307 u8g_com_arduino_t6963_write(u8g, val); 00308 return 1; 00309 } 00310 00311 00312 uint8_t u8g_com_arduino_t6963_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) 00313 { 00314 switch(msg) 00315 { 00316 case U8G_COM_MSG_INIT: 00317 u8g->pin_list[U8G_PI_A0_STATE] = 0; 00318 u8g_com_arduino_t6963_init(u8g); 00319 /* setup the RW (equal to WR) pin as output and force it to high */ 00320 if ( u8g->pin_list[U8G_PI_WR] != U8G_PIN_NONE ) 00321 { 00322 pinMode(u8g->pin_list[U8G_PI_WR], OUTPUT); 00323 u8g_com_arduino_digital_write(u8g, U8G_PI_WR, HIGH); 00324 } 00325 /* set all pins (except WR pin) */ 00326 u8g_com_arduino_assign_pin_output_high(u8g); 00327 break; 00328 case U8G_COM_MSG_STOP: 00329 break; 00330 00331 case U8G_COM_MSG_CHIP_SELECT: 00332 if ( arg_val == 0 ) 00333 { 00334 /* disable, active low chip select */ 00335 u8g_com_arduino_digital_write(u8g, U8G_PI_CS, HIGH); 00336 } 00337 else 00338 { 00339 /* enable */ 00340 u8g_com_arduino_digital_write(u8g, U8G_PI_CS, LOW); 00341 } 00342 break; 00343 case U8G_COM_MSG_WRITE_BYTE: 00344 if ( u8g->pin_list[U8G_PI_A0_STATE] == 0 ) 00345 { 00346 u8g_com_arduino_t6963_write_data(u8g, arg_val); 00347 } 00348 else 00349 { 00350 u8g_com_arduino_t6963_write_cmd(u8g, arg_val); 00351 } 00352 break; 00353 case U8G_COM_MSG_WRITE_SEQ: 00354 { 00355 register uint8_t *ptr = arg_ptr; 00356 u8g_com_arduino_t6963_write_cmd(u8g, 0x0b0); /* auto write */ 00357 while( arg_val > 0 ) 00358 { 00359 if ( u8g_com_arduino_t6963_write_auto_data(u8g, *ptr++) == 0 ) 00360 break; 00361 arg_val--; 00362 } 00363 u8g_com_arduino_t6963_write_cmd(u8g, 0x0b2); /* auto reset */ 00364 } 00365 break; 00366 case U8G_COM_MSG_WRITE_SEQ_P: 00367 { 00368 register uint8_t *ptr = arg_ptr; 00369 u8g_com_arduino_t6963_write_cmd(u8g, 0x0b0); /* auto write */ 00370 while( arg_val > 0 ) 00371 { 00372 if ( u8g_com_arduino_t6963_write_auto_data(u8g, u8g_pgm_read(ptr)) == 0 ) 00373 break; 00374 ptr++; 00375 arg_val--; 00376 } 00377 u8g_com_arduino_t6963_write_cmd(u8g, 0x0b2); /* auto reset */ 00378 } 00379 break; 00380 case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 1) or data mode (arg_val = 0) */ 00381 u8g->pin_list[U8G_PI_A0_STATE] = arg_val; 00382 //u8g_com_arduino_digital_write(u8g, U8G_PI_DI, arg_val); 00383 break; 00384 case U8G_COM_MSG_RESET: 00385 if ( u8g->pin_list[U8G_PI_RESET] != U8G_PIN_NONE ) 00386 u8g_com_arduino_digital_write(u8g, U8G_PI_RESET, arg_val); 00387 break; 00388 00389 } 00390 return 1; 00391 } 00392 00393 #else 00394 00395 00396 uint8_t u8g_com_arduino_t6963_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) 00397 { 00398 return 1; 00399 } 00400 00401 00402 #endif /* ARDUINO */ 00403 00404
Generated on Tue Jul 12 2022 17:30:57 by
1.7.2