.

Committer:
Geremia
Date:
Mon Mar 23 14:08:04 2015 +0000
Revision:
20:14daa48ffd4c
Parent:
11:b842b8e332cb
Child:
25:daacdcf34e52
Add  ILI 9320/9325/9328 custom TFT932x class, parallel/spi 8/16bit, with orientation, scroll, pixelread, fastwindow.; Par8 and 16 tested, SPI not at all, needs checking if the CS toggle is necessary (see SPI8.cpp SPI16.cpp).

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Geremia 4:12ba0ecc2c1f 1 /* mbed UniGraphic library - PAR16 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 #include "PAR16.h"
Geremia 4:12ba0ecc2c1f 19
Geremia 4:12ba0ecc2c1f 20 PAR16::PAR16(PortName port, PinName CS, PinName reset, PinName DC, PinName WR, PinName RD)
Geremia 4:12ba0ecc2c1f 21 : _port(port,0xFFFF), _CS(CS), _reset(reset), _DC(DC), _WR(WR), _RD(RD)
Geremia 4:12ba0ecc2c1f 22 {
Geremia 4:12ba0ecc2c1f 23 _reset = 1;
Geremia 4:12ba0ecc2c1f 24 _DC=1;
Geremia 4:12ba0ecc2c1f 25 _WR=1;
Geremia 4:12ba0ecc2c1f 26 _RD=1;
Geremia 4:12ba0ecc2c1f 27 _CS=1;
Geremia 4:12ba0ecc2c1f 28 _port.mode(PullNone);
Geremia 4:12ba0ecc2c1f 29 _port.output(); // will re-enable our GPIO port
Geremia 4:12ba0ecc2c1f 30 hw_reset();
Geremia 4:12ba0ecc2c1f 31 }
Geremia 4:12ba0ecc2c1f 32
Geremia 4:12ba0ecc2c1f 33 void PAR16::wr_cmd8(unsigned char cmd)
Geremia 20:14daa48ffd4c 34 {
Geremia 4:12ba0ecc2c1f 35 _DC = 0; // 0=cmd
Geremia 4:12ba0ecc2c1f 36 _port.write(cmd); // write 8bit
Geremia 4:12ba0ecc2c1f 37 _WR=0;
Geremia 4:12ba0ecc2c1f 38 _WR=1;
Geremia 20:14daa48ffd4c 39 _DC = 1; // 1=data next
Geremia 4:12ba0ecc2c1f 40 }
Geremia 4:12ba0ecc2c1f 41 void PAR16::wr_data8(unsigned char data)
Geremia 4:12ba0ecc2c1f 42 {
Geremia 4:12ba0ecc2c1f 43 _port.write(data); // write 8bit
Geremia 4:12ba0ecc2c1f 44 _WR=0;
Geremia 4:12ba0ecc2c1f 45 _WR=1;
Geremia 4:12ba0ecc2c1f 46 }
Geremia 4:12ba0ecc2c1f 47 void PAR16::wr_cmd16(unsigned short cmd)
Geremia 20:14daa48ffd4c 48 {
Geremia 4:12ba0ecc2c1f 49 _DC = 0; // 0=cmd
Geremia 4:12ba0ecc2c1f 50 _port.write(cmd>>8); // write 8bit
Geremia 4:12ba0ecc2c1f 51 _WR=0;
Geremia 4:12ba0ecc2c1f 52 _WR=1;
Geremia 4:12ba0ecc2c1f 53 _port.write(cmd&0xFF); // write 8bit
Geremia 4:12ba0ecc2c1f 54 _WR=0;
Geremia 4:12ba0ecc2c1f 55 _WR=1;
Geremia 20:14daa48ffd4c 56 _DC = 1; // 1=data next
Geremia 4:12ba0ecc2c1f 57 }
Geremia 4:12ba0ecc2c1f 58 void PAR16::wr_data16(unsigned short data)
Geremia 4:12ba0ecc2c1f 59 {
Geremia 4:12ba0ecc2c1f 60 _port.write(data>>8); // write 8bit
Geremia 4:12ba0ecc2c1f 61 _WR=0;
Geremia 4:12ba0ecc2c1f 62 _WR=1;
Geremia 4:12ba0ecc2c1f 63 _port.write(data&0xFF); // write 8bit
Geremia 4:12ba0ecc2c1f 64 _WR=0;
Geremia 4:12ba0ecc2c1f 65 _WR=1;
Geremia 4:12ba0ecc2c1f 66 }
Geremia 4:12ba0ecc2c1f 67 void PAR16::wr_gram(unsigned short data)
Geremia 4:12ba0ecc2c1f 68 {
Geremia 4:12ba0ecc2c1f 69 _port.write(data); // write 16bit
Geremia 4:12ba0ecc2c1f 70 _WR=0;
Geremia 4:12ba0ecc2c1f 71 _WR=1;
Geremia 4:12ba0ecc2c1f 72 }
Geremia 4:12ba0ecc2c1f 73 void PAR16::wr_gram(unsigned short data, unsigned int count)
Geremia 4:12ba0ecc2c1f 74 {
Geremia 4:12ba0ecc2c1f 75 while(count)
Geremia 4:12ba0ecc2c1f 76 {
Geremia 20:14daa48ffd4c 77 _port.write(data); // rewrite even if same data, otherwise too much fast
Geremia 4:12ba0ecc2c1f 78 _WR=0;
Geremia 4:12ba0ecc2c1f 79 _WR=1;
Geremia 4:12ba0ecc2c1f 80 count--;
Geremia 4:12ba0ecc2c1f 81 }
Geremia 4:12ba0ecc2c1f 82 }
Geremia 4:12ba0ecc2c1f 83 void PAR16::wr_grambuf(unsigned short* data, unsigned int lenght)
Geremia 4:12ba0ecc2c1f 84 {
Geremia 4:12ba0ecc2c1f 85 while(lenght)
Geremia 4:12ba0ecc2c1f 86 {
Geremia 4:12ba0ecc2c1f 87 _port.write(*data); // write 16bit
Geremia 4:12ba0ecc2c1f 88 _WR=0;
Geremia 4:12ba0ecc2c1f 89 _WR=1;
Geremia 4:12ba0ecc2c1f 90 data++;
Geremia 4:12ba0ecc2c1f 91 lenght--;
Geremia 4:12ba0ecc2c1f 92 }
Geremia 4:12ba0ecc2c1f 93 }
Geremia 11:b842b8e332cb 94 unsigned short PAR16::rd_gram(bool convert)
Geremia 5:b222a9461d6b 95 {
Geremia 11:b842b8e332cb 96 unsigned int r=0;
Geremia 7:bb0383b91104 97 _port.input();
Geremia 7:bb0383b91104 98
Geremia 7:bb0383b91104 99 _RD = 0;
Geremia 7:bb0383b91104 100 _port.read(); //dummy read
Geremia 7:bb0383b91104 101 _RD = 1;
Geremia 7:bb0383b91104 102
Geremia 7:bb0383b91104 103 _RD = 0;
Geremia 20:14daa48ffd4c 104 // _RD = 0; // add wait
Geremia 7:bb0383b91104 105 r |= _port.read();
Geremia 7:bb0383b91104 106 _RD = 1;
Geremia 11:b842b8e332cb 107 if(convert)
Geremia 11:b842b8e332cb 108 {
Geremia 11:b842b8e332cb 109 r <<= 8;
Geremia 11:b842b8e332cb 110 _RD = 0;
Geremia 11:b842b8e332cb 111 // _RD = 0; // add wait
Geremia 11:b842b8e332cb 112 r |= _port.read()>>8; //MSB of port read is blue, LSB is red of next pixel
Geremia 11:b842b8e332cb 113 _RD = 1;
Geremia 11:b842b8e332cb 114 // gram is 18bit/pixel, if you set 16bit/pixel (cmd 3A), during writing the 16bits are expanded to 18bit
Geremia 11:b842b8e332cb 115 // during reading, you read the raw 18bit gram
Geremia 11:b842b8e332cb 116 r = RGB24to16((r&0xFF0000)>>16, (r&0xFF00)>>8, r&0xFF);// 18bit pixel padded to 24bits, rrrrrr00_gggggg00_bbbbbb00, converted to 16bit
Geremia 11:b842b8e332cb 117 }
Geremia 7:bb0383b91104 118 _port.output();
Geremia 11:b842b8e332cb 119 return (unsigned short)r;
Geremia 7:bb0383b91104 120 }
Geremia 7:bb0383b91104 121 unsigned int PAR16::rd_reg_data32(unsigned char reg)
Geremia 7:bb0383b91104 122 {
Geremia 7:bb0383b91104 123 wr_cmd8(reg);
Geremia 5:b222a9461d6b 124 unsigned int r=0;
Geremia 20:14daa48ffd4c 125 // _DC = 1; // 1=data
Geremia 5:b222a9461d6b 126 _port.input();
Geremia 5:b222a9461d6b 127
Geremia 5:b222a9461d6b 128 _RD = 0;
Geremia 5:b222a9461d6b 129 _port.read(); //dummy read
Geremia 5:b222a9461d6b 130 _RD = 1;
Geremia 5:b222a9461d6b 131
Geremia 5:b222a9461d6b 132 _RD = 0;
Geremia 5:b222a9461d6b 133 // _RD = 0; // add wait
Geremia 5:b222a9461d6b 134 r |= (_port.read()&0xFF);
Geremia 5:b222a9461d6b 135 r <<= 8;
Geremia 5:b222a9461d6b 136 _RD = 1;
Geremia 5:b222a9461d6b 137
Geremia 5:b222a9461d6b 138 _RD = 0;
Geremia 5:b222a9461d6b 139 // _RD = 0; // add wait
Geremia 5:b222a9461d6b 140 r |= (_port.read()&0xFF);
Geremia 5:b222a9461d6b 141 r <<= 8;
Geremia 5:b222a9461d6b 142 _RD = 1;
Geremia 5:b222a9461d6b 143
Geremia 5:b222a9461d6b 144 _RD = 0;
Geremia 5:b222a9461d6b 145 // _RD = 0; // add wait
Geremia 5:b222a9461d6b 146 r |= (_port.read()&0xFF);
Geremia 5:b222a9461d6b 147 r <<= 8;
Geremia 5:b222a9461d6b 148 _RD = 1;
Geremia 5:b222a9461d6b 149
Geremia 5:b222a9461d6b 150 _RD = 0;
Geremia 5:b222a9461d6b 151 // _RD = 0; // add wait
Geremia 5:b222a9461d6b 152 r |= (_port.read()&0xFF);
Geremia 5:b222a9461d6b 153 _RD = 1;
Geremia 5:b222a9461d6b 154
Geremia 20:14daa48ffd4c 155 _CS = 1; // toggle CS to interupt the cmd in case was not supported
Geremia 5:b222a9461d6b 156 _CS = 0;
Geremia 20:14daa48ffd4c 157
Geremia 5:b222a9461d6b 158 _port.output();
Geremia 5:b222a9461d6b 159 return r;
Geremia 5:b222a9461d6b 160 }
Geremia 7:bb0383b91104 161 // in Par mode EXTC regs (0xB0-0xFF) can be directly read
Geremia 7:bb0383b91104 162 unsigned int PAR16::rd_extcreg_data32(unsigned char reg, unsigned char SPIreadenablecmd)
Geremia 5:b222a9461d6b 163 {
Geremia 7:bb0383b91104 164 return rd_reg_data32(reg);
Geremia 5:b222a9461d6b 165 }
Geremia 20:14daa48ffd4c 166 // ILI932x specific
Geremia 20:14daa48ffd4c 167 void PAR16::dummyread()
Geremia 20:14daa48ffd4c 168 {
Geremia 20:14daa48ffd4c 169 _port.input();
Geremia 20:14daa48ffd4c 170 _RD = 0;
Geremia 20:14daa48ffd4c 171 _port.read(); // dummy read
Geremia 20:14daa48ffd4c 172 _RD=1;
Geremia 20:14daa48ffd4c 173 // _port.output();
Geremia 20:14daa48ffd4c 174 }
Geremia 20:14daa48ffd4c 175 // ILI932x specific
Geremia 20:14daa48ffd4c 176 void PAR16::reg_select(unsigned char reg, bool forread)
Geremia 20:14daa48ffd4c 177 {
Geremia 20:14daa48ffd4c 178 _DC = 0;
Geremia 20:14daa48ffd4c 179 _port.write(reg); // write 16bit
Geremia 20:14daa48ffd4c 180 _WR=0;
Geremia 20:14daa48ffd4c 181 _WR=1;
Geremia 20:14daa48ffd4c 182 _DC = 1; // 1=data next
Geremia 20:14daa48ffd4c 183 }
Geremia 20:14daa48ffd4c 184 // ILI932x specific
Geremia 20:14daa48ffd4c 185 void PAR16::reg_write(unsigned char reg, unsigned short data)
Geremia 20:14daa48ffd4c 186 {
Geremia 20:14daa48ffd4c 187 _DC = 0;
Geremia 20:14daa48ffd4c 188 _port.write(reg); // write 16bit
Geremia 20:14daa48ffd4c 189 _WR=0;
Geremia 20:14daa48ffd4c 190 _WR=1;
Geremia 20:14daa48ffd4c 191 _DC = 1;
Geremia 20:14daa48ffd4c 192 _port.write(data); // write 16bit
Geremia 20:14daa48ffd4c 193 _WR=0;
Geremia 20:14daa48ffd4c 194 _WR=1;
Geremia 20:14daa48ffd4c 195 }
Geremia 20:14daa48ffd4c 196 // ILI932x specific
Geremia 20:14daa48ffd4c 197 unsigned short PAR16::reg_read(unsigned char reg)
Geremia 20:14daa48ffd4c 198 {
Geremia 20:14daa48ffd4c 199 unsigned short r=0;
Geremia 20:14daa48ffd4c 200 _DC = 0;
Geremia 20:14daa48ffd4c 201 _port.write(reg); // write 16bit
Geremia 20:14daa48ffd4c 202 _WR=0;
Geremia 20:14daa48ffd4c 203 _WR=1;
Geremia 20:14daa48ffd4c 204 _DC = 1;
Geremia 20:14daa48ffd4c 205 _port.input();
Geremia 20:14daa48ffd4c 206 _RD=0;
Geremia 20:14daa48ffd4c 207 r |= _port.read(); // read 16bit
Geremia 20:14daa48ffd4c 208 _RD=1;
Geremia 20:14daa48ffd4c 209 _port.output();
Geremia 20:14daa48ffd4c 210 return r;
Geremia 20:14daa48ffd4c 211 }
Geremia 4:12ba0ecc2c1f 212 void PAR16::hw_reset()
Geremia 4:12ba0ecc2c1f 213 {
Geremia 4:12ba0ecc2c1f 214 wait_ms(15);
Geremia 4:12ba0ecc2c1f 215 _DC = 1;
Geremia 4:12ba0ecc2c1f 216 _CS = 1;
Geremia 4:12ba0ecc2c1f 217 _WR = 1;
Geremia 4:12ba0ecc2c1f 218 _RD = 1;
Geremia 4:12ba0ecc2c1f 219 _reset = 0; // display reset
Geremia 20:14daa48ffd4c 220 wait_ms(2);
Geremia 4:12ba0ecc2c1f 221 _reset = 1; // end reset
Geremia 20:14daa48ffd4c 222 wait_ms(100);
Geremia 4:12ba0ecc2c1f 223 }
Geremia 4:12ba0ecc2c1f 224 void PAR16::BusEnable(bool enable)
Geremia 4:12ba0ecc2c1f 225 {
Geremia 4:12ba0ecc2c1f 226 _CS = enable ? 0:1;
Geremia 4:12ba0ecc2c1f 227 }