.

Committer:
Geremia
Date:
Fri Feb 20 21:32:25 2015 +0000
Revision:
11:b842b8e332cb
Parent:
7:bb0383b91104
Child:
20:14daa48ffd4c
added auto_gram_read_format() to TFt inits. Even if write is set to 16bit RGB color, for some controllers the read cmd outputs 18bit BGR. Now that function will autodetect and set internal flags accordingly, so pixelread() is always correct.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Geremia 4:12ba0ecc2c1f 1 /* mbed UniGraphic library - PAR8 protocol class
Geremia 4:12ba0ecc2c1f 2 * Copyright (c) 2015 Giuliano Dianda
Geremia 4:12ba0ecc2c1f 3 * Released under the MIT License: http://mbed.org/license/mit
Geremia 4:12ba0ecc2c1f 4 *
Geremia 4:12ba0ecc2c1f 5 * Derived work of:
Geremia 4:12ba0ecc2c1f 6 *
Geremia 4:12ba0ecc2c1f 7 * mbed library for 240*320 pixel display TFT based on ILI9341 LCD Controller
Geremia 4:12ba0ecc2c1f 8 * Copyright (c) 2013 Peter Drescher - DC2PD
Geremia 4:12ba0ecc2c1f 9 *
Geremia 4:12ba0ecc2c1f 10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Geremia 4:12ba0ecc2c1f 11 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Geremia 4:12ba0ecc2c1f 12 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Geremia 4:12ba0ecc2c1f 13 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Geremia 4:12ba0ecc2c1f 14 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Geremia 4:12ba0ecc2c1f 15 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Geremia 4:12ba0ecc2c1f 16 * THE SOFTWARE.
Geremia 4:12ba0ecc2c1f 17 */
Geremia 4:12ba0ecc2c1f 18
Geremia 0:75ec1b3cde17 19 #include "PAR8.h"
Geremia 0:75ec1b3cde17 20
Geremia 0:75ec1b3cde17 21 PAR8::PAR8(PortName port, PinName CS, PinName reset, PinName DC, PinName WR, PinName RD)
Geremia 0:75ec1b3cde17 22 : _port(port,0xFF), _CS(CS), _reset(reset), _DC(DC), _WR(WR), _RD(RD)
Geremia 0:75ec1b3cde17 23 {
Geremia 0:75ec1b3cde17 24 _reset = 1;
Geremia 0:75ec1b3cde17 25 _DC=1;
Geremia 0:75ec1b3cde17 26 _WR=1;
Geremia 0:75ec1b3cde17 27 _RD=1;
Geremia 0:75ec1b3cde17 28 _CS=1;
Geremia 0:75ec1b3cde17 29 #ifdef STMPORTDEBUG
Geremia 0:75ec1b3cde17 30 findport(port); //on return, GPIO get disabled
Geremia 0:75ec1b3cde17 31 #endif
Geremia 0:75ec1b3cde17 32 _port.mode(PullNone);
Geremia 0:75ec1b3cde17 33 _port.output(); // will re-enable our GPIO port
Geremia 0:75ec1b3cde17 34 hw_reset();
Geremia 0:75ec1b3cde17 35 }
Geremia 0:75ec1b3cde17 36
Geremia 0:75ec1b3cde17 37 #ifdef STMPORTDEBUG
Geremia 0:75ec1b3cde17 38 // create a port obj with STM HAL drivers, just to collect memorymapped regs
Geremia 0:75ec1b3cde17 39 void PAR8::findport(PortName port)
Geremia 0:75ec1b3cde17 40 {
Geremia 0:75ec1b3cde17 41 port_t tempport;
Geremia 0:75ec1b3cde17 42 port_init(&tempport, port, 0xFF, PIN_INPUT);
Geremia 0:75ec1b3cde17 43 outreg = tempport.reg_out;
Geremia 0:75ec1b3cde17 44 inreg = tempport.reg_in;
Geremia 0:75ec1b3cde17 45 // debug("out 0x%.8X in 0x%.8X\r\n", outreg, inreg);
Geremia 0:75ec1b3cde17 46 }
Geremia 0:75ec1b3cde17 47 #endif
Geremia 1:ff019d22b275 48 void PAR8::wr_cmd8(unsigned char cmd)
Geremia 0:75ec1b3cde17 49 {
Geremia 0:75ec1b3cde17 50 #ifdef USE_CS
Geremia 0:75ec1b3cde17 51 _CS = 0;
Geremia 0:75ec1b3cde17 52 #endif
Geremia 0:75ec1b3cde17 53 _DC = 0; // 0=cmd
Geremia 0:75ec1b3cde17 54 _WR=0;
Geremia 0:75ec1b3cde17 55 _port.write(cmd); // write 8bit
Geremia 0:75ec1b3cde17 56 _WR=1;
Geremia 0:75ec1b3cde17 57 #ifdef USE_CS
Geremia 0:75ec1b3cde17 58 _CS = 1;
Geremia 0:75ec1b3cde17 59 #endif
Geremia 0:75ec1b3cde17 60 }
Geremia 1:ff019d22b275 61 void PAR8::wr_data8(unsigned char data)
Geremia 0:75ec1b3cde17 62 {
Geremia 0:75ec1b3cde17 63 #ifdef USE_CS
Geremia 0:75ec1b3cde17 64 _CS = 0;
Geremia 0:75ec1b3cde17 65 #endif
Geremia 0:75ec1b3cde17 66 _DC = 1; // 1=data
Geremia 0:75ec1b3cde17 67 _WR=0;
Geremia 1:ff019d22b275 68 _port.write(data); // write 8bit
Geremia 0:75ec1b3cde17 69 _WR=1;
Geremia 0:75ec1b3cde17 70 #ifdef USE_CS
Geremia 0:75ec1b3cde17 71 _CS = 1;
Geremia 0:75ec1b3cde17 72 #endif
Geremia 0:75ec1b3cde17 73 }
Geremia 1:ff019d22b275 74 void PAR8::wr_cmd16(unsigned short cmd)
Geremia 1:ff019d22b275 75 {
Geremia 1:ff019d22b275 76 #ifdef USE_CS
Geremia 1:ff019d22b275 77 _CS = 0;
Geremia 1:ff019d22b275 78 #endif
Geremia 1:ff019d22b275 79 _DC = 0; // 0=cmd
Geremia 1:ff019d22b275 80 _WR=0;
Geremia 1:ff019d22b275 81 _port.write(cmd>>8); // write 8bit
Geremia 1:ff019d22b275 82 _WR=1;
Geremia 1:ff019d22b275 83 _WR=0;
Geremia 1:ff019d22b275 84 _port.write(cmd&0xFF); // write 8bit
Geremia 1:ff019d22b275 85 _WR=1;
Geremia 1:ff019d22b275 86 #ifdef USE_CS
Geremia 1:ff019d22b275 87 _CS = 1;
Geremia 1:ff019d22b275 88 #endif
Geremia 1:ff019d22b275 89 }
Geremia 1:ff019d22b275 90 void PAR8::wr_data16(unsigned short data)
Geremia 1:ff019d22b275 91 {
Geremia 1:ff019d22b275 92 #ifdef USE_CS
Geremia 1:ff019d22b275 93 _CS = 0;
Geremia 1:ff019d22b275 94 #endif
Geremia 1:ff019d22b275 95 _DC = 1; // 1=data
Geremia 1:ff019d22b275 96 _WR=0;
Geremia 1:ff019d22b275 97 _port.write(data>>8); // write 8bit
Geremia 1:ff019d22b275 98 _WR=1;
Geremia 1:ff019d22b275 99 _WR=0;
Geremia 1:ff019d22b275 100 _port.write(data&0xFF); // write 8bit
Geremia 1:ff019d22b275 101 _WR=1;
Geremia 1:ff019d22b275 102 #ifdef USE_CS
Geremia 1:ff019d22b275 103 _CS = 1;
Geremia 1:ff019d22b275 104 #endif
Geremia 1:ff019d22b275 105 }
Geremia 4:12ba0ecc2c1f 106 void PAR8::wr_gram(unsigned short data)
Geremia 4:12ba0ecc2c1f 107 {
Geremia 4:12ba0ecc2c1f 108 #ifdef USE_CS
Geremia 4:12ba0ecc2c1f 109 _CS = 0;
Geremia 4:12ba0ecc2c1f 110 #endif
Geremia 4:12ba0ecc2c1f 111 _DC = 1; // 1=data
Geremia 4:12ba0ecc2c1f 112 _WR=0;
Geremia 4:12ba0ecc2c1f 113 _port.write(data>>8); // write 8bit
Geremia 4:12ba0ecc2c1f 114 _WR=1;
Geremia 4:12ba0ecc2c1f 115 _WR=0;
Geremia 4:12ba0ecc2c1f 116 _port.write(data&0xFF); // write 8bit
Geremia 4:12ba0ecc2c1f 117 _WR=1;
Geremia 4:12ba0ecc2c1f 118 #ifdef USE_CS
Geremia 4:12ba0ecc2c1f 119 _CS = 1;
Geremia 4:12ba0ecc2c1f 120 #endif
Geremia 4:12ba0ecc2c1f 121 }
Geremia 4:12ba0ecc2c1f 122 void PAR8::wr_gram(unsigned short data, unsigned int count)
Geremia 1:ff019d22b275 123 {
Geremia 1:ff019d22b275 124 #ifdef USE_CS
Geremia 1:ff019d22b275 125 _CS = 0;
Geremia 1:ff019d22b275 126 #endif
Geremia 1:ff019d22b275 127 _DC = 1; // 1=data
Geremia 1:ff019d22b275 128 if((data>>8)==(data&0xFF))
Geremia 1:ff019d22b275 129 {
Geremia 1:ff019d22b275 130 count<<=1;
Geremia 1:ff019d22b275 131 _port.write(data); // write 8bit
Geremia 1:ff019d22b275 132 while(count)
Geremia 1:ff019d22b275 133 {
Geremia 1:ff019d22b275 134 _WR=0;
Geremia 1:ff019d22b275 135 _WR=1;
Geremia 1:ff019d22b275 136 count--;
Geremia 1:ff019d22b275 137 }
Geremia 1:ff019d22b275 138 }
Geremia 1:ff019d22b275 139 else
Geremia 1:ff019d22b275 140 {
Geremia 1:ff019d22b275 141 while(count)
Geremia 1:ff019d22b275 142 {
Geremia 1:ff019d22b275 143 _WR=0;
Geremia 1:ff019d22b275 144 _port.write(data>>8); // write 8bit
Geremia 1:ff019d22b275 145 _WR=1;
Geremia 1:ff019d22b275 146 _WR=0;
Geremia 1:ff019d22b275 147 _port.write(data&0xFF); // write 8bit
Geremia 1:ff019d22b275 148 _WR=1;
Geremia 1:ff019d22b275 149 count--;
Geremia 1:ff019d22b275 150 }
Geremia 1:ff019d22b275 151 }
Geremia 1:ff019d22b275 152 #ifdef USE_CS
Geremia 1:ff019d22b275 153 _CS = 1;
Geremia 1:ff019d22b275 154 #endif
Geremia 1:ff019d22b275 155 }
Geremia 4:12ba0ecc2c1f 156 void PAR8::wr_grambuf(unsigned short* data, unsigned int lenght)
Geremia 1:ff019d22b275 157 {
Geremia 1:ff019d22b275 158 #ifdef USE_CS
Geremia 1:ff019d22b275 159 _CS = 0;
Geremia 1:ff019d22b275 160 #endif
Geremia 1:ff019d22b275 161 _DC = 1; // 1=data
Geremia 1:ff019d22b275 162 while(lenght)
Geremia 1:ff019d22b275 163 {
Geremia 1:ff019d22b275 164 _WR=0;
Geremia 1:ff019d22b275 165 _port.write((*data)>>8); // write 8bit
Geremia 1:ff019d22b275 166 _WR=1;
Geremia 1:ff019d22b275 167 _WR=0;
Geremia 1:ff019d22b275 168 _port.write((*data)&0xFF); // write 8bit
Geremia 1:ff019d22b275 169 _WR=1;
Geremia 1:ff019d22b275 170 data++;
Geremia 1:ff019d22b275 171 lenght--;
Geremia 1:ff019d22b275 172 }
Geremia 1:ff019d22b275 173 #ifdef USE_CS
Geremia 1:ff019d22b275 174 _CS = 1;
Geremia 1:ff019d22b275 175 #endif
Geremia 1:ff019d22b275 176 }
Geremia 11:b842b8e332cb 177 unsigned short PAR8::rd_gram(bool convert)
Geremia 5:b222a9461d6b 178 {
Geremia 5:b222a9461d6b 179 #ifdef USE_CS
Geremia 5:b222a9461d6b 180 _CS = 0;
Geremia 5:b222a9461d6b 181 #endif
Geremia 11:b842b8e332cb 182 unsigned int r=0;
Geremia 7:bb0383b91104 183 _DC = 1; // 1=data
Geremia 7:bb0383b91104 184 _port.input();
Geremia 7:bb0383b91104 185
Geremia 7:bb0383b91104 186 _RD = 0;
Geremia 7:bb0383b91104 187 _port.read(); //dummy read
Geremia 7:bb0383b91104 188 _RD = 1;
Geremia 7:bb0383b91104 189
Geremia 7:bb0383b91104 190 _RD = 0;
Geremia 7:bb0383b91104 191 // _RD = 0; // add wait
Geremia 7:bb0383b91104 192 r |= (_port.read()&0xFF);
Geremia 7:bb0383b91104 193 r <<= 8;
Geremia 7:bb0383b91104 194 _RD = 1;
Geremia 7:bb0383b91104 195
Geremia 7:bb0383b91104 196 _RD = 0;
Geremia 7:bb0383b91104 197 // _RD = 0; // add wait
Geremia 7:bb0383b91104 198 r |= (_port.read()&0xFF);
Geremia 7:bb0383b91104 199 _RD = 1;
Geremia 11:b842b8e332cb 200 if(convert)
Geremia 11:b842b8e332cb 201 {
Geremia 11:b842b8e332cb 202 r <<= 8;
Geremia 11:b842b8e332cb 203 _RD = 0;
Geremia 11:b842b8e332cb 204 // _RD = 0; // add wait
Geremia 11:b842b8e332cb 205 r |= _port.read();
Geremia 11:b842b8e332cb 206 _RD = 1;
Geremia 11:b842b8e332cb 207 // gram is 18bit/pixel, if you set 16bit/pixel (cmd 3A), during writing the 16bits are expanded to 18bit
Geremia 11:b842b8e332cb 208 // during reading, you read the raw 18bit gram
Geremia 11:b842b8e332cb 209 r = RGB24to16((r&0xFF0000)>>16, (r&0xFF00)>>8, r&0xFF);// 18bit pixel padded to 24bits, rrrrrr00_gggggg00_bbbbbb00, converted to 16bit
Geremia 11:b842b8e332cb 210 }
Geremia 7:bb0383b91104 211 #ifdef USE_CS
Geremia 7:bb0383b91104 212 _CS = 1;
Geremia 7:bb0383b91104 213 #endif
Geremia 7:bb0383b91104 214 _port.output();
Geremia 11:b842b8e332cb 215 return (unsigned short)r;
Geremia 7:bb0383b91104 216 }
Geremia 7:bb0383b91104 217 unsigned int PAR8::rd_reg_data32(unsigned char reg)
Geremia 7:bb0383b91104 218 {
Geremia 7:bb0383b91104 219 #ifdef USE_CS
Geremia 7:bb0383b91104 220 _CS = 0;
Geremia 7:bb0383b91104 221 #endif
Geremia 7:bb0383b91104 222 wr_cmd8(reg);
Geremia 5:b222a9461d6b 223 unsigned int r=0;
Geremia 5:b222a9461d6b 224 _DC = 1; // 1=data
Geremia 5:b222a9461d6b 225 _port.input();
Geremia 5:b222a9461d6b 226
Geremia 5:b222a9461d6b 227 _RD = 0;
Geremia 5:b222a9461d6b 228 _port.read(); //dummy read
Geremia 5:b222a9461d6b 229 _RD = 1;
Geremia 5:b222a9461d6b 230
Geremia 5:b222a9461d6b 231 _RD = 0;
Geremia 5:b222a9461d6b 232 // _RD = 0; // add wait
Geremia 5:b222a9461d6b 233 r |= (_port.read()&0xFF);
Geremia 5:b222a9461d6b 234 r <<= 8;
Geremia 5:b222a9461d6b 235 _RD = 1;
Geremia 5:b222a9461d6b 236
Geremia 5:b222a9461d6b 237 _RD = 0;
Geremia 5:b222a9461d6b 238 // _RD = 0; // add wait
Geremia 5:b222a9461d6b 239 r |= (_port.read()&0xFF);
Geremia 5:b222a9461d6b 240 r <<= 8;
Geremia 5:b222a9461d6b 241 _RD = 1;
Geremia 5:b222a9461d6b 242
Geremia 5:b222a9461d6b 243 _RD = 0;
Geremia 5:b222a9461d6b 244 // _RD = 0; // add wait
Geremia 5:b222a9461d6b 245 r |= (_port.read()&0xFF);
Geremia 5:b222a9461d6b 246 r <<= 8;
Geremia 5:b222a9461d6b 247 _RD = 1;
Geremia 5:b222a9461d6b 248
Geremia 5:b222a9461d6b 249 _RD = 0;
Geremia 5:b222a9461d6b 250 // _RD = 0; // add wait
Geremia 5:b222a9461d6b 251 r |= (_port.read()&0xFF);
Geremia 5:b222a9461d6b 252 _RD = 1;
Geremia 5:b222a9461d6b 253
Geremia 5:b222a9461d6b 254 _CS = 1; // force CS HIG to interupt the cmd in case was not supported
Geremia 5:b222a9461d6b 255 #ifndef USE_CS //if CS is not used, force fixed LOW again
Geremia 5:b222a9461d6b 256 _CS = 0;
Geremia 5:b222a9461d6b 257 #endif
Geremia 5:b222a9461d6b 258 _port.output();
Geremia 5:b222a9461d6b 259 return r;
Geremia 5:b222a9461d6b 260 }
Geremia 7:bb0383b91104 261 // in Par mode EXTC regs (0xB0-0xFF) can be directly read
Geremia 7:bb0383b91104 262 unsigned int PAR8::rd_extcreg_data32(unsigned char reg, unsigned char SPIreadenablecmd)
Geremia 5:b222a9461d6b 263 {
Geremia 7:bb0383b91104 264 return rd_reg_data32(reg);
Geremia 5:b222a9461d6b 265 }
Geremia 0:75ec1b3cde17 266 void PAR8::hw_reset()
Geremia 0:75ec1b3cde17 267 {
Geremia 0:75ec1b3cde17 268 wait_ms(15);
Geremia 0:75ec1b3cde17 269 _DC = 1;
Geremia 0:75ec1b3cde17 270 _CS = 1;
Geremia 0:75ec1b3cde17 271 _WR = 1;
Geremia 0:75ec1b3cde17 272 _RD = 1;
Geremia 0:75ec1b3cde17 273 _reset = 0; // display reset
Geremia 0:75ec1b3cde17 274 wait_us(50);
Geremia 0:75ec1b3cde17 275 _reset = 1; // end reset
Geremia 0:75ec1b3cde17 276 wait_ms(15);
Geremia 0:75ec1b3cde17 277 #ifndef USE_CS
Geremia 0:75ec1b3cde17 278 _CS=0; // put CS low now and forever
Geremia 0:75ec1b3cde17 279 #endif
Geremia 0:75ec1b3cde17 280 }
Geremia 0:75ec1b3cde17 281 void PAR8::BusEnable(bool enable)
Geremia 0:75ec1b3cde17 282 {
Geremia 0:75ec1b3cde17 283 _CS = enable ? 0:1;
Geremia 0:75ec1b3cde17 284 }