iforce2d Chris
/
ubxDistanceMeter
Displays distance to start location on OLED screen.
u8g_com_arduino_t6963.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_com_arduino_t6963.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 | PIN_D0 8 |
iforce2d | 0:972874f31c98 | 36 | PIN_D1 9 |
iforce2d | 0:972874f31c98 | 37 | PIN_D2 10 |
iforce2d | 0:972874f31c98 | 38 | PIN_D3 11 |
iforce2d | 0:972874f31c98 | 39 | PIN_D4 4 |
iforce2d | 0:972874f31c98 | 40 | PIN_D5 5 |
iforce2d | 0:972874f31c98 | 41 | PIN_D6 6 |
iforce2d | 0:972874f31c98 | 42 | PIN_D7 7 |
iforce2d | 0:972874f31c98 | 43 | |
iforce2d | 0:972874f31c98 | 44 | PIN_CS 14 |
iforce2d | 0:972874f31c98 | 45 | PIN_A0 15 |
iforce2d | 0:972874f31c98 | 46 | PIN_RESET 16 |
iforce2d | 0:972874f31c98 | 47 | PIN_WR 17 |
iforce2d | 0:972874f31c98 | 48 | PIN_RD 18 |
iforce2d | 0:972874f31c98 | 49 | |
iforce2d | 0:972874f31c98 | 50 | u8g_InitRW8Bit(u8g, dev, d0, d1, d2, d3, d4, d5, d6, d7, cs, a0, wr, rd, reset) |
iforce2d | 0:972874f31c98 | 51 | u8g_InitRW8Bit(u8g, dev, 8, 9, 10, 11, 4, 5, 6, 7, 14, 15, 17, 18, 16) |
iforce2d | 0:972874f31c98 | 52 | |
iforce2d | 0:972874f31c98 | 53 | Update for ATOMIC operation done (01 Jun 2013) |
iforce2d | 0:972874f31c98 | 54 | U8G_ATOMIC_OR(ptr, val) |
iforce2d | 0:972874f31c98 | 55 | U8G_ATOMIC_AND(ptr, val) |
iforce2d | 0:972874f31c98 | 56 | U8G_ATOMIC_START(); |
iforce2d | 0:972874f31c98 | 57 | U8G_ATOMIC_END(); |
iforce2d | 0:972874f31c98 | 58 | |
iforce2d | 0:972874f31c98 | 59 | |
iforce2d | 0:972874f31c98 | 60 | */ |
iforce2d | 0:972874f31c98 | 61 | |
iforce2d | 0:972874f31c98 | 62 | #include "u8g.h" |
iforce2d | 0:972874f31c98 | 63 | |
iforce2d | 0:972874f31c98 | 64 | #if defined(ARDUINO) |
iforce2d | 0:972874f31c98 | 65 | |
iforce2d | 0:972874f31c98 | 66 | #if ARDUINO < 100 |
iforce2d | 0:972874f31c98 | 67 | //#include <WProgram.h> |
iforce2d | 0:972874f31c98 | 68 | #include <wiring_private.h> |
iforce2d | 0:972874f31c98 | 69 | #include <pins_arduino.h> |
iforce2d | 0:972874f31c98 | 70 | #else |
iforce2d | 0:972874f31c98 | 71 | #include <Arduino.h> |
iforce2d | 0:972874f31c98 | 72 | #endif |
iforce2d | 0:972874f31c98 | 73 | |
iforce2d | 0:972874f31c98 | 74 | |
iforce2d | 0:972874f31c98 | 75 | #if defined(__PIC32MX) |
iforce2d | 0:972874f31c98 | 76 | /* CHIPKIT PIC32 */ |
iforce2d | 0:972874f31c98 | 77 | static volatile uint32_t *u8g_output_data_port[8]; |
iforce2d | 0:972874f31c98 | 78 | static volatile uint32_t *u8g_input_data_port[8]; |
iforce2d | 0:972874f31c98 | 79 | static volatile uint32_t *u8g_mode_port[8]; |
iforce2d | 0:972874f31c98 | 80 | static uint32_t u8g_data_mask[8]; |
iforce2d | 0:972874f31c98 | 81 | #else |
iforce2d | 0:972874f31c98 | 82 | static volatile uint8_t *u8g_output_data_port[8]; |
iforce2d | 0:972874f31c98 | 83 | static volatile uint8_t *u8g_input_data_port[8]; |
iforce2d | 0:972874f31c98 | 84 | static volatile uint8_t *u8g_mode_port[8]; |
iforce2d | 0:972874f31c98 | 85 | static uint8_t u8g_data_mask[8]; |
iforce2d | 0:972874f31c98 | 86 | #endif |
iforce2d | 0:972874f31c98 | 87 | |
iforce2d | 0:972874f31c98 | 88 | |
iforce2d | 0:972874f31c98 | 89 | |
iforce2d | 0:972874f31c98 | 90 | static void u8g_com_arduino_t6963_init(u8g_t *u8g) |
iforce2d | 0:972874f31c98 | 91 | { |
iforce2d | 0:972874f31c98 | 92 | u8g_output_data_port[0] = portOutputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D0])); |
iforce2d | 0:972874f31c98 | 93 | u8g_input_data_port[0] = portInputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D0])); |
iforce2d | 0:972874f31c98 | 94 | u8g_mode_port[0] = portModeRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D0])); |
iforce2d | 0:972874f31c98 | 95 | u8g_data_mask[0] = digitalPinToBitMask(u8g->pin_list[U8G_PI_D0]); |
iforce2d | 0:972874f31c98 | 96 | |
iforce2d | 0:972874f31c98 | 97 | u8g_output_data_port[1] = portOutputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D1])); |
iforce2d | 0:972874f31c98 | 98 | u8g_input_data_port[1] = portInputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D1])); |
iforce2d | 0:972874f31c98 | 99 | u8g_mode_port[1] = portModeRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D1])); |
iforce2d | 0:972874f31c98 | 100 | u8g_data_mask[1] = digitalPinToBitMask(u8g->pin_list[U8G_PI_D1]); |
iforce2d | 0:972874f31c98 | 101 | |
iforce2d | 0:972874f31c98 | 102 | u8g_output_data_port[2] = portOutputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D2])); |
iforce2d | 0:972874f31c98 | 103 | u8g_input_data_port[2] = portInputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D2])); |
iforce2d | 0:972874f31c98 | 104 | u8g_mode_port[2] = portModeRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D2])); |
iforce2d | 0:972874f31c98 | 105 | u8g_data_mask[2] = digitalPinToBitMask(u8g->pin_list[U8G_PI_D2]); |
iforce2d | 0:972874f31c98 | 106 | |
iforce2d | 0:972874f31c98 | 107 | u8g_output_data_port[3] = portOutputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D3])); |
iforce2d | 0:972874f31c98 | 108 | u8g_input_data_port[3] = portInputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D3])); |
iforce2d | 0:972874f31c98 | 109 | u8g_mode_port[3] = portModeRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D3])); |
iforce2d | 0:972874f31c98 | 110 | u8g_data_mask[3] = digitalPinToBitMask(u8g->pin_list[U8G_PI_D3]); |
iforce2d | 0:972874f31c98 | 111 | |
iforce2d | 0:972874f31c98 | 112 | u8g_output_data_port[4] = portOutputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D4])); |
iforce2d | 0:972874f31c98 | 113 | u8g_input_data_port[4] = portInputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D4])); |
iforce2d | 0:972874f31c98 | 114 | u8g_mode_port[4] = portModeRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D4])); |
iforce2d | 0:972874f31c98 | 115 | u8g_data_mask[4] = digitalPinToBitMask(u8g->pin_list[U8G_PI_D4]); |
iforce2d | 0:972874f31c98 | 116 | |
iforce2d | 0:972874f31c98 | 117 | u8g_output_data_port[5] = portOutputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D5])); |
iforce2d | 0:972874f31c98 | 118 | u8g_input_data_port[5] = portInputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D5])); |
iforce2d | 0:972874f31c98 | 119 | u8g_mode_port[5] = portModeRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D5])); |
iforce2d | 0:972874f31c98 | 120 | u8g_data_mask[5] = digitalPinToBitMask(u8g->pin_list[U8G_PI_D5]); |
iforce2d | 0:972874f31c98 | 121 | |
iforce2d | 0:972874f31c98 | 122 | u8g_output_data_port[6] = portOutputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D6])); |
iforce2d | 0:972874f31c98 | 123 | u8g_input_data_port[6] = portInputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D6])); |
iforce2d | 0:972874f31c98 | 124 | u8g_mode_port[6] = portModeRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D6])); |
iforce2d | 0:972874f31c98 | 125 | u8g_data_mask[6] = digitalPinToBitMask(u8g->pin_list[U8G_PI_D6]); |
iforce2d | 0:972874f31c98 | 126 | |
iforce2d | 0:972874f31c98 | 127 | u8g_output_data_port[7] = portOutputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D7])); |
iforce2d | 0:972874f31c98 | 128 | u8g_input_data_port[7] = portInputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D7])); |
iforce2d | 0:972874f31c98 | 129 | u8g_mode_port[7] = portModeRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D7])); |
iforce2d | 0:972874f31c98 | 130 | u8g_data_mask[7] = digitalPinToBitMask(u8g->pin_list[U8G_PI_D7]); |
iforce2d | 0:972874f31c98 | 131 | } |
iforce2d | 0:972874f31c98 | 132 | |
iforce2d | 0:972874f31c98 | 133 | |
iforce2d | 0:972874f31c98 | 134 | static void u8g_com_arduino_t6963_write_data_pin(uint8_t pin, uint8_t val) |
iforce2d | 0:972874f31c98 | 135 | { |
iforce2d | 0:972874f31c98 | 136 | /* no ATOMIC protection required here, this is done by calling procedure */ |
iforce2d | 0:972874f31c98 | 137 | if ( val != 0 ) |
iforce2d | 0:972874f31c98 | 138 | *u8g_output_data_port[pin] |= u8g_data_mask[pin]; |
iforce2d | 0:972874f31c98 | 139 | else |
iforce2d | 0:972874f31c98 | 140 | *u8g_output_data_port[pin] &= ~u8g_data_mask[pin]; |
iforce2d | 0:972874f31c98 | 141 | } |
iforce2d | 0:972874f31c98 | 142 | |
iforce2d | 0:972874f31c98 | 143 | static void u8g_com_arduino_t6963_set_port_output(void) |
iforce2d | 0:972874f31c98 | 144 | { |
iforce2d | 0:972874f31c98 | 145 | uint8_t i; |
iforce2d | 0:972874f31c98 | 146 | U8G_ATOMIC_START(); |
iforce2d | 0:972874f31c98 | 147 | for( i = 0; i < 8; i++ ) |
iforce2d | 0:972874f31c98 | 148 | { |
iforce2d | 0:972874f31c98 | 149 | #if defined(__PIC32MX) |
iforce2d | 0:972874f31c98 | 150 | /* CHIPKIT PIC32 */ |
iforce2d | 0:972874f31c98 | 151 | *u8g_mode_port[i] |= u8g_data_mask[i]; |
iforce2d | 0:972874f31c98 | 152 | #elif defined(__AVR__) |
iforce2d | 0:972874f31c98 | 153 | *u8g_mode_port[i] |= u8g_data_mask[i]; |
iforce2d | 0:972874f31c98 | 154 | #else |
iforce2d | 0:972874f31c98 | 155 | /* TODO: use generic Arduino API */ |
iforce2d | 0:972874f31c98 | 156 | *u8g_mode_port[i] |= u8g_data_mask[i]; |
iforce2d | 0:972874f31c98 | 157 | #endif |
iforce2d | 0:972874f31c98 | 158 | |
iforce2d | 0:972874f31c98 | 159 | } |
iforce2d | 0:972874f31c98 | 160 | U8G_ATOMIC_END(); |
iforce2d | 0:972874f31c98 | 161 | } |
iforce2d | 0:972874f31c98 | 162 | |
iforce2d | 0:972874f31c98 | 163 | static void u8g_com_arduino_t6963_set_port_input(void) |
iforce2d | 0:972874f31c98 | 164 | { |
iforce2d | 0:972874f31c98 | 165 | uint8_t i; |
iforce2d | 0:972874f31c98 | 166 | U8G_ATOMIC_START(); |
iforce2d | 0:972874f31c98 | 167 | for( i = 0; i < 8; i++ ) |
iforce2d | 0:972874f31c98 | 168 | { |
iforce2d | 0:972874f31c98 | 169 | #if defined(__PIC32MX) |
iforce2d | 0:972874f31c98 | 170 | /* CHIPKIT PIC32 */ |
iforce2d | 0:972874f31c98 | 171 | *u8g_mode_port[i] &= ~u8g_data_mask[i]; |
iforce2d | 0:972874f31c98 | 172 | #elif defined(__AVR__) |
iforce2d | 0:972874f31c98 | 173 | /* avr */ |
iforce2d | 0:972874f31c98 | 174 | *u8g_mode_port[i] &= ~u8g_data_mask[i]; |
iforce2d | 0:972874f31c98 | 175 | *u8g_output_data_port[i] &= ~u8g_data_mask[i]; // no pullup |
iforce2d | 0:972874f31c98 | 176 | #else |
iforce2d | 0:972874f31c98 | 177 | /* TODO: use generic Arduino API */ |
iforce2d | 0:972874f31c98 | 178 | *u8g_mode_port[i] &= ~u8g_data_mask[i]; |
iforce2d | 0:972874f31c98 | 179 | *u8g_output_data_port[i] &= ~u8g_data_mask[i]; // no pullup |
iforce2d | 0:972874f31c98 | 180 | #endif |
iforce2d | 0:972874f31c98 | 181 | } |
iforce2d | 0:972874f31c98 | 182 | U8G_ATOMIC_END(); |
iforce2d | 0:972874f31c98 | 183 | } |
iforce2d | 0:972874f31c98 | 184 | |
iforce2d | 0:972874f31c98 | 185 | |
iforce2d | 0:972874f31c98 | 186 | static void u8g_com_arduino_t6963_write(u8g_t *u8g, uint8_t val) |
iforce2d | 0:972874f31c98 | 187 | { |
iforce2d | 0:972874f31c98 | 188 | U8G_ATOMIC_START(); |
iforce2d | 0:972874f31c98 | 189 | |
iforce2d | 0:972874f31c98 | 190 | u8g_com_arduino_t6963_write_data_pin( 0, val&1 ); |
iforce2d | 0:972874f31c98 | 191 | val >>= 1; |
iforce2d | 0:972874f31c98 | 192 | u8g_com_arduino_t6963_write_data_pin( 1, val&1 ); |
iforce2d | 0:972874f31c98 | 193 | val >>= 1; |
iforce2d | 0:972874f31c98 | 194 | u8g_com_arduino_t6963_write_data_pin( 2, val&1 ); |
iforce2d | 0:972874f31c98 | 195 | val >>= 1; |
iforce2d | 0:972874f31c98 | 196 | u8g_com_arduino_t6963_write_data_pin( 3, val&1 ); |
iforce2d | 0:972874f31c98 | 197 | val >>= 1; |
iforce2d | 0:972874f31c98 | 198 | |
iforce2d | 0:972874f31c98 | 199 | u8g_com_arduino_t6963_write_data_pin( 4, val&1 ); |
iforce2d | 0:972874f31c98 | 200 | val >>= 1; |
iforce2d | 0:972874f31c98 | 201 | u8g_com_arduino_t6963_write_data_pin( 5, val&1 ); |
iforce2d | 0:972874f31c98 | 202 | val >>= 1; |
iforce2d | 0:972874f31c98 | 203 | u8g_com_arduino_t6963_write_data_pin( 6, val&1 ); |
iforce2d | 0:972874f31c98 | 204 | val >>= 1; |
iforce2d | 0:972874f31c98 | 205 | u8g_com_arduino_t6963_write_data_pin( 7, val&1 ); |
iforce2d | 0:972874f31c98 | 206 | val >>= 1; |
iforce2d | 0:972874f31c98 | 207 | U8G_ATOMIC_END(); |
iforce2d | 0:972874f31c98 | 208 | |
iforce2d | 0:972874f31c98 | 209 | u8g_com_arduino_digital_write(u8g, U8G_PI_WR, 0); |
iforce2d | 0:972874f31c98 | 210 | u8g_MicroDelay(); /* 80ns, reference: t6963 datasheet */ |
iforce2d | 0:972874f31c98 | 211 | u8g_com_arduino_digital_write(u8g, U8G_PI_WR, 1); |
iforce2d | 0:972874f31c98 | 212 | u8g_MicroDelay(); /* 10ns, reference: t6963 datasheet */ |
iforce2d | 0:972874f31c98 | 213 | } |
iforce2d | 0:972874f31c98 | 214 | |
iforce2d | 0:972874f31c98 | 215 | static uint8_t u8g_com_arduino_t6963_read(u8g_t *u8g) |
iforce2d | 0:972874f31c98 | 216 | { |
iforce2d | 0:972874f31c98 | 217 | uint8_t val = 0; |
iforce2d | 0:972874f31c98 | 218 | |
iforce2d | 0:972874f31c98 | 219 | u8g_com_arduino_digital_write(u8g, U8G_PI_RD, 0); |
iforce2d | 0:972874f31c98 | 220 | u8g_MicroDelay(); /* 150ns, reference: t6963 datasheet */ |
iforce2d | 0:972874f31c98 | 221 | |
iforce2d | 0:972874f31c98 | 222 | U8G_ATOMIC_START(); |
iforce2d | 0:972874f31c98 | 223 | /* only read bits 0, 1 and 3 */ |
iforce2d | 0:972874f31c98 | 224 | if ( (*u8g_input_data_port[3] & u8g_data_mask[3]) != 0 ) |
iforce2d | 0:972874f31c98 | 225 | val++; |
iforce2d | 0:972874f31c98 | 226 | val <<= 1; |
iforce2d | 0:972874f31c98 | 227 | val <<= 1; |
iforce2d | 0:972874f31c98 | 228 | if ( (*u8g_input_data_port[1] & u8g_data_mask[1]) != 0 ) |
iforce2d | 0:972874f31c98 | 229 | val++; |
iforce2d | 0:972874f31c98 | 230 | val <<= 1; |
iforce2d | 0:972874f31c98 | 231 | if ( (*u8g_input_data_port[0] & u8g_data_mask[0]) != 0 ) |
iforce2d | 0:972874f31c98 | 232 | val++; |
iforce2d | 0:972874f31c98 | 233 | U8G_ATOMIC_END(); |
iforce2d | 0:972874f31c98 | 234 | |
iforce2d | 0:972874f31c98 | 235 | u8g_com_arduino_digital_write(u8g, U8G_PI_RD, 1); |
iforce2d | 0:972874f31c98 | 236 | u8g_MicroDelay(); /* 10ns, reference: t6963 datasheet */ |
iforce2d | 0:972874f31c98 | 237 | |
iforce2d | 0:972874f31c98 | 238 | return val; |
iforce2d | 0:972874f31c98 | 239 | } |
iforce2d | 0:972874f31c98 | 240 | |
iforce2d | 0:972874f31c98 | 241 | #define U8G_STATUS_TIMEOUT 50 |
iforce2d | 0:972874f31c98 | 242 | |
iforce2d | 0:972874f31c98 | 243 | static uint8_t u8g_com_arduino_t6963_until_01_ok(u8g_t *u8g) |
iforce2d | 0:972874f31c98 | 244 | { |
iforce2d | 0:972874f31c98 | 245 | long x; |
iforce2d | 0:972874f31c98 | 246 | |
iforce2d | 0:972874f31c98 | 247 | u8g_com_arduino_t6963_set_port_input(); |
iforce2d | 0:972874f31c98 | 248 | x = millis(); |
iforce2d | 0:972874f31c98 | 249 | x += U8G_STATUS_TIMEOUT; |
iforce2d | 0:972874f31c98 | 250 | |
iforce2d | 0:972874f31c98 | 251 | for(;;) |
iforce2d | 0:972874f31c98 | 252 | { |
iforce2d | 0:972874f31c98 | 253 | if ( (u8g_com_arduino_t6963_read(u8g) & 3) == 3 ) |
iforce2d | 0:972874f31c98 | 254 | break; |
iforce2d | 0:972874f31c98 | 255 | if ( x < millis() ) |
iforce2d | 0:972874f31c98 | 256 | return 0; |
iforce2d | 0:972874f31c98 | 257 | } |
iforce2d | 0:972874f31c98 | 258 | u8g_com_arduino_t6963_set_port_output(); |
iforce2d | 0:972874f31c98 | 259 | return 1; |
iforce2d | 0:972874f31c98 | 260 | } |
iforce2d | 0:972874f31c98 | 261 | |
iforce2d | 0:972874f31c98 | 262 | static uint8_t u8g_com_arduino_t6963_until_3_ok(u8g_t *u8g) |
iforce2d | 0:972874f31c98 | 263 | { |
iforce2d | 0:972874f31c98 | 264 | long x; |
iforce2d | 0:972874f31c98 | 265 | |
iforce2d | 0:972874f31c98 | 266 | u8g_com_arduino_t6963_set_port_input(); |
iforce2d | 0:972874f31c98 | 267 | x = millis(); |
iforce2d | 0:972874f31c98 | 268 | x += U8G_STATUS_TIMEOUT; |
iforce2d | 0:972874f31c98 | 269 | |
iforce2d | 0:972874f31c98 | 270 | for(;;) |
iforce2d | 0:972874f31c98 | 271 | { |
iforce2d | 0:972874f31c98 | 272 | if ( (u8g_com_arduino_t6963_read(u8g) & 8) == 8 ) |
iforce2d | 0:972874f31c98 | 273 | break; |
iforce2d | 0:972874f31c98 | 274 | if ( x < millis() ) |
iforce2d | 0:972874f31c98 | 275 | return 0; |
iforce2d | 0:972874f31c98 | 276 | } |
iforce2d | 0:972874f31c98 | 277 | u8g_com_arduino_t6963_set_port_output(); |
iforce2d | 0:972874f31c98 | 278 | return 1; |
iforce2d | 0:972874f31c98 | 279 | } |
iforce2d | 0:972874f31c98 | 280 | |
iforce2d | 0:972874f31c98 | 281 | static uint8_t u8g_com_arduino_t6963_write_cmd(u8g_t *u8g, uint8_t val) |
iforce2d | 0:972874f31c98 | 282 | { |
iforce2d | 0:972874f31c98 | 283 | u8g_com_arduino_digital_write(u8g, U8G_PI_A0, 1); |
iforce2d | 0:972874f31c98 | 284 | if ( u8g_com_arduino_t6963_until_01_ok(u8g) == 0 ) |
iforce2d | 0:972874f31c98 | 285 | return 0; |
iforce2d | 0:972874f31c98 | 286 | u8g_com_arduino_digital_write(u8g, U8G_PI_A0, 1); |
iforce2d | 0:972874f31c98 | 287 | u8g_com_arduino_t6963_write(u8g, val); |
iforce2d | 0:972874f31c98 | 288 | return 1; |
iforce2d | 0:972874f31c98 | 289 | } |
iforce2d | 0:972874f31c98 | 290 | |
iforce2d | 0:972874f31c98 | 291 | static uint8_t u8g_com_arduino_t6963_write_data(u8g_t *u8g, uint8_t val) |
iforce2d | 0:972874f31c98 | 292 | { |
iforce2d | 0:972874f31c98 | 293 | u8g_com_arduino_digital_write(u8g, U8G_PI_A0, 1); |
iforce2d | 0:972874f31c98 | 294 | if ( u8g_com_arduino_t6963_until_01_ok(u8g) == 0 ) |
iforce2d | 0:972874f31c98 | 295 | return 0; |
iforce2d | 0:972874f31c98 | 296 | u8g_com_arduino_digital_write(u8g, U8G_PI_A0, 0); |
iforce2d | 0:972874f31c98 | 297 | u8g_com_arduino_t6963_write(u8g, val); |
iforce2d | 0:972874f31c98 | 298 | return 1; |
iforce2d | 0:972874f31c98 | 299 | } |
iforce2d | 0:972874f31c98 | 300 | |
iforce2d | 0:972874f31c98 | 301 | static uint8_t u8g_com_arduino_t6963_write_auto_data(u8g_t *u8g, uint8_t val) |
iforce2d | 0:972874f31c98 | 302 | { |
iforce2d | 0:972874f31c98 | 303 | u8g_com_arduino_digital_write(u8g, U8G_PI_A0, 1); |
iforce2d | 0:972874f31c98 | 304 | if ( u8g_com_arduino_t6963_until_3_ok(u8g) == 0 ) |
iforce2d | 0:972874f31c98 | 305 | return 0; |
iforce2d | 0:972874f31c98 | 306 | u8g_com_arduino_digital_write(u8g, U8G_PI_A0, 0); |
iforce2d | 0:972874f31c98 | 307 | u8g_com_arduino_t6963_write(u8g, val); |
iforce2d | 0:972874f31c98 | 308 | return 1; |
iforce2d | 0:972874f31c98 | 309 | } |
iforce2d | 0:972874f31c98 | 310 | |
iforce2d | 0:972874f31c98 | 311 | |
iforce2d | 0:972874f31c98 | 312 | uint8_t u8g_com_arduino_t6963_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) |
iforce2d | 0:972874f31c98 | 313 | { |
iforce2d | 0:972874f31c98 | 314 | switch(msg) |
iforce2d | 0:972874f31c98 | 315 | { |
iforce2d | 0:972874f31c98 | 316 | case U8G_COM_MSG_INIT: |
iforce2d | 0:972874f31c98 | 317 | u8g->pin_list[U8G_PI_A0_STATE] = 0; |
iforce2d | 0:972874f31c98 | 318 | u8g_com_arduino_t6963_init(u8g); |
iforce2d | 0:972874f31c98 | 319 | /* setup the RW (equal to WR) pin as output and force it to high */ |
iforce2d | 0:972874f31c98 | 320 | if ( u8g->pin_list[U8G_PI_WR] != U8G_PIN_NONE ) |
iforce2d | 0:972874f31c98 | 321 | { |
iforce2d | 0:972874f31c98 | 322 | pinMode(u8g->pin_list[U8G_PI_WR], OUTPUT); |
iforce2d | 0:972874f31c98 | 323 | u8g_com_arduino_digital_write(u8g, U8G_PI_WR, HIGH); |
iforce2d | 0:972874f31c98 | 324 | } |
iforce2d | 0:972874f31c98 | 325 | /* set all pins (except WR pin) */ |
iforce2d | 0:972874f31c98 | 326 | u8g_com_arduino_assign_pin_output_high(u8g); |
iforce2d | 0:972874f31c98 | 327 | break; |
iforce2d | 0:972874f31c98 | 328 | case U8G_COM_MSG_STOP: |
iforce2d | 0:972874f31c98 | 329 | break; |
iforce2d | 0:972874f31c98 | 330 | |
iforce2d | 0:972874f31c98 | 331 | case U8G_COM_MSG_CHIP_SELECT: |
iforce2d | 0:972874f31c98 | 332 | if ( arg_val == 0 ) |
iforce2d | 0:972874f31c98 | 333 | { |
iforce2d | 0:972874f31c98 | 334 | /* disable, active low chip select */ |
iforce2d | 0:972874f31c98 | 335 | u8g_com_arduino_digital_write(u8g, U8G_PI_CS, HIGH); |
iforce2d | 0:972874f31c98 | 336 | } |
iforce2d | 0:972874f31c98 | 337 | else |
iforce2d | 0:972874f31c98 | 338 | { |
iforce2d | 0:972874f31c98 | 339 | /* enable */ |
iforce2d | 0:972874f31c98 | 340 | u8g_com_arduino_digital_write(u8g, U8G_PI_CS, LOW); |
iforce2d | 0:972874f31c98 | 341 | } |
iforce2d | 0:972874f31c98 | 342 | break; |
iforce2d | 0:972874f31c98 | 343 | case U8G_COM_MSG_WRITE_BYTE: |
iforce2d | 0:972874f31c98 | 344 | if ( u8g->pin_list[U8G_PI_A0_STATE] == 0 ) |
iforce2d | 0:972874f31c98 | 345 | { |
iforce2d | 0:972874f31c98 | 346 | u8g_com_arduino_t6963_write_data(u8g, arg_val); |
iforce2d | 0:972874f31c98 | 347 | } |
iforce2d | 0:972874f31c98 | 348 | else |
iforce2d | 0:972874f31c98 | 349 | { |
iforce2d | 0:972874f31c98 | 350 | u8g_com_arduino_t6963_write_cmd(u8g, arg_val); |
iforce2d | 0:972874f31c98 | 351 | } |
iforce2d | 0:972874f31c98 | 352 | break; |
iforce2d | 0:972874f31c98 | 353 | case U8G_COM_MSG_WRITE_SEQ: |
iforce2d | 0:972874f31c98 | 354 | { |
iforce2d | 0:972874f31c98 | 355 | register uint8_t *ptr = arg_ptr; |
iforce2d | 0:972874f31c98 | 356 | u8g_com_arduino_t6963_write_cmd(u8g, 0x0b0); /* auto write */ |
iforce2d | 0:972874f31c98 | 357 | while( arg_val > 0 ) |
iforce2d | 0:972874f31c98 | 358 | { |
iforce2d | 0:972874f31c98 | 359 | if ( u8g_com_arduino_t6963_write_auto_data(u8g, *ptr++) == 0 ) |
iforce2d | 0:972874f31c98 | 360 | break; |
iforce2d | 0:972874f31c98 | 361 | arg_val--; |
iforce2d | 0:972874f31c98 | 362 | } |
iforce2d | 0:972874f31c98 | 363 | u8g_com_arduino_t6963_write_cmd(u8g, 0x0b2); /* auto reset */ |
iforce2d | 0:972874f31c98 | 364 | } |
iforce2d | 0:972874f31c98 | 365 | break; |
iforce2d | 0:972874f31c98 | 366 | case U8G_COM_MSG_WRITE_SEQ_P: |
iforce2d | 0:972874f31c98 | 367 | { |
iforce2d | 0:972874f31c98 | 368 | register uint8_t *ptr = arg_ptr; |
iforce2d | 0:972874f31c98 | 369 | u8g_com_arduino_t6963_write_cmd(u8g, 0x0b0); /* auto write */ |
iforce2d | 0:972874f31c98 | 370 | while( arg_val > 0 ) |
iforce2d | 0:972874f31c98 | 371 | { |
iforce2d | 0:972874f31c98 | 372 | if ( u8g_com_arduino_t6963_write_auto_data(u8g, u8g_pgm_read(ptr)) == 0 ) |
iforce2d | 0:972874f31c98 | 373 | break; |
iforce2d | 0:972874f31c98 | 374 | ptr++; |
iforce2d | 0:972874f31c98 | 375 | arg_val--; |
iforce2d | 0:972874f31c98 | 376 | } |
iforce2d | 0:972874f31c98 | 377 | u8g_com_arduino_t6963_write_cmd(u8g, 0x0b2); /* auto reset */ |
iforce2d | 0:972874f31c98 | 378 | } |
iforce2d | 0:972874f31c98 | 379 | break; |
iforce2d | 0:972874f31c98 | 380 | case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 1) or data mode (arg_val = 0) */ |
iforce2d | 0:972874f31c98 | 381 | u8g->pin_list[U8G_PI_A0_STATE] = arg_val; |
iforce2d | 0:972874f31c98 | 382 | //u8g_com_arduino_digital_write(u8g, U8G_PI_DI, arg_val); |
iforce2d | 0:972874f31c98 | 383 | break; |
iforce2d | 0:972874f31c98 | 384 | case U8G_COM_MSG_RESET: |
iforce2d | 0:972874f31c98 | 385 | if ( u8g->pin_list[U8G_PI_RESET] != U8G_PIN_NONE ) |
iforce2d | 0:972874f31c98 | 386 | u8g_com_arduino_digital_write(u8g, U8G_PI_RESET, arg_val); |
iforce2d | 0:972874f31c98 | 387 | break; |
iforce2d | 0:972874f31c98 | 388 | |
iforce2d | 0:972874f31c98 | 389 | } |
iforce2d | 0:972874f31c98 | 390 | return 1; |
iforce2d | 0:972874f31c98 | 391 | } |
iforce2d | 0:972874f31c98 | 392 | |
iforce2d | 0:972874f31c98 | 393 | #else |
iforce2d | 0:972874f31c98 | 394 | |
iforce2d | 0:972874f31c98 | 395 | |
iforce2d | 0:972874f31c98 | 396 | uint8_t u8g_com_arduino_t6963_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) |
iforce2d | 0:972874f31c98 | 397 | { |
iforce2d | 0:972874f31c98 | 398 | return 1; |
iforce2d | 0:972874f31c98 | 399 | } |
iforce2d | 0:972874f31c98 | 400 | |
iforce2d | 0:972874f31c98 | 401 | |
iforce2d | 0:972874f31c98 | 402 | #endif /* ARDUINO */ |
iforce2d | 0:972874f31c98 | 403 | |
iforce2d | 0:972874f31c98 | 404 |