.
Protocols/PAR8.cpp@20:14daa48ffd4c, 2015-03-23 (annotated)
- 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?
User | Revision | Line number | New 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 | _port.mode(PullNone); |
Geremia | 0:75ec1b3cde17 | 30 | _port.output(); // will re-enable our GPIO port |
Geremia | 0:75ec1b3cde17 | 31 | hw_reset(); |
Geremia | 0:75ec1b3cde17 | 32 | } |
Geremia | 0:75ec1b3cde17 | 33 | |
Geremia | 1:ff019d22b275 | 34 | void PAR8::wr_cmd8(unsigned char cmd) |
Geremia | 20:14daa48ffd4c | 35 | { |
Geremia | 0:75ec1b3cde17 | 36 | _DC = 0; // 0=cmd |
Geremia | 20:14daa48ffd4c | 37 | _port.write(cmd); // write 8bit |
Geremia | 0:75ec1b3cde17 | 38 | _WR=0; |
Geremia | 0:75ec1b3cde17 | 39 | _WR=1; |
Geremia | 20:14daa48ffd4c | 40 | _DC = 1; // 1=data next |
Geremia | 0:75ec1b3cde17 | 41 | } |
Geremia | 1:ff019d22b275 | 42 | void PAR8::wr_data8(unsigned char data) |
Geremia | 0:75ec1b3cde17 | 43 | { |
Geremia | 20:14daa48ffd4c | 44 | _port.write(data); // write 8bit |
Geremia | 0:75ec1b3cde17 | 45 | _WR=0; |
Geremia | 0:75ec1b3cde17 | 46 | _WR=1; |
Geremia | 0:75ec1b3cde17 | 47 | } |
Geremia | 1:ff019d22b275 | 48 | void PAR8::wr_cmd16(unsigned short cmd) |
Geremia | 20:14daa48ffd4c | 49 | { |
Geremia | 1:ff019d22b275 | 50 | _DC = 0; // 0=cmd |
Geremia | 1:ff019d22b275 | 51 | _port.write(cmd>>8); // write 8bit |
Geremia | 1:ff019d22b275 | 52 | _WR=0; |
Geremia | 1:ff019d22b275 | 53 | _WR=1; |
Geremia | 20:14daa48ffd4c | 54 | _port.write(cmd&0xFF); // write 8bit |
Geremia | 20:14daa48ffd4c | 55 | _WR=0; |
Geremia | 20:14daa48ffd4c | 56 | _WR=1; |
Geremia | 20:14daa48ffd4c | 57 | _DC = 1; // 1=data next |
Geremia | 1:ff019d22b275 | 58 | } |
Geremia | 1:ff019d22b275 | 59 | void PAR8::wr_data16(unsigned short data) |
Geremia | 1:ff019d22b275 | 60 | { |
Geremia | 20:14daa48ffd4c | 61 | _port.write(data>>8); // write 8bit |
Geremia | 1:ff019d22b275 | 62 | _WR=0; |
Geremia | 1:ff019d22b275 | 63 | _WR=1; |
Geremia | 20:14daa48ffd4c | 64 | _port.write(data&0xFF); // write 8bit |
Geremia | 1:ff019d22b275 | 65 | _WR=0; |
Geremia | 1:ff019d22b275 | 66 | _WR=1; |
Geremia | 1:ff019d22b275 | 67 | } |
Geremia | 4:12ba0ecc2c1f | 68 | void PAR8::wr_gram(unsigned short data) |
Geremia | 4:12ba0ecc2c1f | 69 | { |
Geremia | 20:14daa48ffd4c | 70 | _port.write(data>>8); // write 8bit |
Geremia | 4:12ba0ecc2c1f | 71 | _WR=0; |
Geremia | 4:12ba0ecc2c1f | 72 | _WR=1; |
Geremia | 20:14daa48ffd4c | 73 | _port.write(data&0xFF); // write 8bit |
Geremia | 4:12ba0ecc2c1f | 74 | _WR=0; |
Geremia | 4:12ba0ecc2c1f | 75 | _WR=1; |
Geremia | 4:12ba0ecc2c1f | 76 | } |
Geremia | 4:12ba0ecc2c1f | 77 | void PAR8::wr_gram(unsigned short data, unsigned int count) |
Geremia | 1:ff019d22b275 | 78 | { |
Geremia | 1:ff019d22b275 | 79 | if((data>>8)==(data&0xFF)) |
Geremia | 1:ff019d22b275 | 80 | { |
Geremia | 1:ff019d22b275 | 81 | count<<=1; |
Geremia | 20:14daa48ffd4c | 82 | // _port.write(data); // write 8bit |
Geremia | 1:ff019d22b275 | 83 | while(count) |
Geremia | 1:ff019d22b275 | 84 | { |
Geremia | 20:14daa48ffd4c | 85 | _port.write(data); // rewrite even if same data, otherwise too much fast |
Geremia | 1:ff019d22b275 | 86 | _WR=0; |
Geremia | 1:ff019d22b275 | 87 | _WR=1; |
Geremia | 1:ff019d22b275 | 88 | count--; |
Geremia | 1:ff019d22b275 | 89 | } |
Geremia | 1:ff019d22b275 | 90 | } |
Geremia | 1:ff019d22b275 | 91 | else |
Geremia | 1:ff019d22b275 | 92 | { |
Geremia | 1:ff019d22b275 | 93 | while(count) |
Geremia | 1:ff019d22b275 | 94 | { |
Geremia | 20:14daa48ffd4c | 95 | _port.write(data>>8); // write 8bit |
Geremia | 1:ff019d22b275 | 96 | _WR=0; |
Geremia | 1:ff019d22b275 | 97 | _WR=1; |
Geremia | 20:14daa48ffd4c | 98 | _port.write(data&0xFF); // write 8bit |
Geremia | 1:ff019d22b275 | 99 | _WR=0; |
Geremia | 1:ff019d22b275 | 100 | _WR=1; |
Geremia | 1:ff019d22b275 | 101 | count--; |
Geremia | 1:ff019d22b275 | 102 | } |
Geremia | 1:ff019d22b275 | 103 | } |
Geremia | 1:ff019d22b275 | 104 | } |
Geremia | 4:12ba0ecc2c1f | 105 | void PAR8::wr_grambuf(unsigned short* data, unsigned int lenght) |
Geremia | 1:ff019d22b275 | 106 | { |
Geremia | 1:ff019d22b275 | 107 | while(lenght) |
Geremia | 1:ff019d22b275 | 108 | { |
Geremia | 20:14daa48ffd4c | 109 | _port.write((*data)>>8); // write 8bit |
Geremia | 1:ff019d22b275 | 110 | _WR=0; |
Geremia | 1:ff019d22b275 | 111 | _WR=1; |
Geremia | 20:14daa48ffd4c | 112 | _port.write((*data)&0xFF); // write 8bit |
Geremia | 1:ff019d22b275 | 113 | _WR=0; |
Geremia | 1:ff019d22b275 | 114 | _WR=1; |
Geremia | 1:ff019d22b275 | 115 | data++; |
Geremia | 1:ff019d22b275 | 116 | lenght--; |
Geremia | 1:ff019d22b275 | 117 | } |
Geremia | 1:ff019d22b275 | 118 | } |
Geremia | 11:b842b8e332cb | 119 | unsigned short PAR8::rd_gram(bool convert) |
Geremia | 5:b222a9461d6b | 120 | { |
Geremia | 11:b842b8e332cb | 121 | unsigned int r=0; |
Geremia | 7:bb0383b91104 | 122 | _port.input(); |
Geremia | 7:bb0383b91104 | 123 | |
Geremia | 7:bb0383b91104 | 124 | _RD = 0; |
Geremia | 20:14daa48ffd4c | 125 | _RD = 0; // add wait |
Geremia | 7:bb0383b91104 | 126 | _port.read(); //dummy read |
Geremia | 7:bb0383b91104 | 127 | _RD = 1; |
Geremia | 7:bb0383b91104 | 128 | |
Geremia | 7:bb0383b91104 | 129 | _RD = 0; |
Geremia | 20:14daa48ffd4c | 130 | _RD = 0; // add wait |
Geremia | 20:14daa48ffd4c | 131 | r |= _port.read(); |
Geremia | 20:14daa48ffd4c | 132 | _RD = 1; |
Geremia | 7:bb0383b91104 | 133 | r <<= 8; |
Geremia | 7:bb0383b91104 | 134 | |
Geremia | 7:bb0383b91104 | 135 | _RD = 0; |
Geremia | 20:14daa48ffd4c | 136 | _RD = 0; // add wait |
Geremia | 20:14daa48ffd4c | 137 | r |= _port.read(); |
Geremia | 7:bb0383b91104 | 138 | _RD = 1; |
Geremia | 11:b842b8e332cb | 139 | if(convert) |
Geremia | 11:b842b8e332cb | 140 | { |
Geremia | 11:b842b8e332cb | 141 | r <<= 8; |
Geremia | 11:b842b8e332cb | 142 | _RD = 0; |
Geremia | 11:b842b8e332cb | 143 | // _RD = 0; // add wait |
Geremia | 11:b842b8e332cb | 144 | r |= _port.read(); |
Geremia | 11:b842b8e332cb | 145 | _RD = 1; |
Geremia | 11:b842b8e332cb | 146 | // gram is 18bit/pixel, if you set 16bit/pixel (cmd 3A), during writing the 16bits are expanded to 18bit |
Geremia | 11:b842b8e332cb | 147 | // during reading, you read the raw 18bit gram |
Geremia | 11:b842b8e332cb | 148 | r = RGB24to16((r&0xFF0000)>>16, (r&0xFF00)>>8, r&0xFF);// 18bit pixel padded to 24bits, rrrrrr00_gggggg00_bbbbbb00, converted to 16bit |
Geremia | 11:b842b8e332cb | 149 | } |
Geremia | 7:bb0383b91104 | 150 | _port.output(); |
Geremia | 11:b842b8e332cb | 151 | return (unsigned short)r; |
Geremia | 7:bb0383b91104 | 152 | } |
Geremia | 7:bb0383b91104 | 153 | unsigned int PAR8::rd_reg_data32(unsigned char reg) |
Geremia | 7:bb0383b91104 | 154 | { |
Geremia | 7:bb0383b91104 | 155 | wr_cmd8(reg); |
Geremia | 5:b222a9461d6b | 156 | unsigned int r=0; |
Geremia | 5:b222a9461d6b | 157 | _port.input(); |
Geremia | 5:b222a9461d6b | 158 | |
Geremia | 5:b222a9461d6b | 159 | _RD = 0; |
Geremia | 5:b222a9461d6b | 160 | _port.read(); //dummy read |
Geremia | 5:b222a9461d6b | 161 | _RD = 1; |
Geremia | 5:b222a9461d6b | 162 | |
Geremia | 5:b222a9461d6b | 163 | _RD = 0; |
Geremia | 5:b222a9461d6b | 164 | // _RD = 0; // add wait |
Geremia | 5:b222a9461d6b | 165 | r |= (_port.read()&0xFF); |
Geremia | 5:b222a9461d6b | 166 | r <<= 8; |
Geremia | 5:b222a9461d6b | 167 | _RD = 1; |
Geremia | 5:b222a9461d6b | 168 | |
Geremia | 5:b222a9461d6b | 169 | _RD = 0; |
Geremia | 5:b222a9461d6b | 170 | // _RD = 0; // add wait |
Geremia | 5:b222a9461d6b | 171 | r |= (_port.read()&0xFF); |
Geremia | 5:b222a9461d6b | 172 | r <<= 8; |
Geremia | 5:b222a9461d6b | 173 | _RD = 1; |
Geremia | 5:b222a9461d6b | 174 | |
Geremia | 5:b222a9461d6b | 175 | _RD = 0; |
Geremia | 5:b222a9461d6b | 176 | // _RD = 0; // add wait |
Geremia | 5:b222a9461d6b | 177 | r |= (_port.read()&0xFF); |
Geremia | 5:b222a9461d6b | 178 | r <<= 8; |
Geremia | 5:b222a9461d6b | 179 | _RD = 1; |
Geremia | 5:b222a9461d6b | 180 | |
Geremia | 5:b222a9461d6b | 181 | _RD = 0; |
Geremia | 5:b222a9461d6b | 182 | // _RD = 0; // add wait |
Geremia | 5:b222a9461d6b | 183 | r |= (_port.read()&0xFF); |
Geremia | 5:b222a9461d6b | 184 | _RD = 1; |
Geremia | 5:b222a9461d6b | 185 | |
Geremia | 5:b222a9461d6b | 186 | _CS = 1; // force CS HIG to interupt the cmd in case was not supported |
Geremia | 5:b222a9461d6b | 187 | _CS = 0; |
Geremia | 5:b222a9461d6b | 188 | _port.output(); |
Geremia | 5:b222a9461d6b | 189 | return r; |
Geremia | 5:b222a9461d6b | 190 | } |
Geremia | 7:bb0383b91104 | 191 | // in Par mode EXTC regs (0xB0-0xFF) can be directly read |
Geremia | 7:bb0383b91104 | 192 | unsigned int PAR8::rd_extcreg_data32(unsigned char reg, unsigned char SPIreadenablecmd) |
Geremia | 5:b222a9461d6b | 193 | { |
Geremia | 7:bb0383b91104 | 194 | return rd_reg_data32(reg); |
Geremia | 5:b222a9461d6b | 195 | } |
Geremia | 20:14daa48ffd4c | 196 | // ILI932x specific |
Geremia | 20:14daa48ffd4c | 197 | void PAR8::dummyread() |
Geremia | 20:14daa48ffd4c | 198 | { |
Geremia | 20:14daa48ffd4c | 199 | _port.input(); |
Geremia | 20:14daa48ffd4c | 200 | _RD=0; |
Geremia | 20:14daa48ffd4c | 201 | _RD=0; // add wait |
Geremia | 20:14daa48ffd4c | 202 | _port.read(); // dummy read |
Geremia | 20:14daa48ffd4c | 203 | _RD=1; |
Geremia | 20:14daa48ffd4c | 204 | // _port.output(); |
Geremia | 20:14daa48ffd4c | 205 | } |
Geremia | 20:14daa48ffd4c | 206 | // ILI932x specific |
Geremia | 20:14daa48ffd4c | 207 | void PAR8::reg_select(unsigned char reg, bool forread) |
Geremia | 20:14daa48ffd4c | 208 | { |
Geremia | 20:14daa48ffd4c | 209 | _DC = 0; |
Geremia | 20:14daa48ffd4c | 210 | _port.write(0); // write MSB |
Geremia | 20:14daa48ffd4c | 211 | _WR=0; |
Geremia | 20:14daa48ffd4c | 212 | _WR=1; |
Geremia | 20:14daa48ffd4c | 213 | _port.write(reg); // write LSB |
Geremia | 20:14daa48ffd4c | 214 | _WR=0; |
Geremia | 20:14daa48ffd4c | 215 | _WR=1; |
Geremia | 20:14daa48ffd4c | 216 | _DC = 1; // 1=data next |
Geremia | 20:14daa48ffd4c | 217 | } |
Geremia | 20:14daa48ffd4c | 218 | // ILI932x specific |
Geremia | 20:14daa48ffd4c | 219 | void PAR8::reg_write(unsigned char reg, unsigned short data) |
Geremia | 20:14daa48ffd4c | 220 | { |
Geremia | 20:14daa48ffd4c | 221 | _DC = 0; |
Geremia | 20:14daa48ffd4c | 222 | _port.write(0); // write MSB |
Geremia | 20:14daa48ffd4c | 223 | _WR=0; |
Geremia | 20:14daa48ffd4c | 224 | _WR=1; |
Geremia | 20:14daa48ffd4c | 225 | _port.write(reg); // write MSB |
Geremia | 20:14daa48ffd4c | 226 | _WR=0; |
Geremia | 20:14daa48ffd4c | 227 | _WR=1; |
Geremia | 20:14daa48ffd4c | 228 | _DC = 1; |
Geremia | 20:14daa48ffd4c | 229 | _port.write(data>>8); |
Geremia | 20:14daa48ffd4c | 230 | _WR=0; |
Geremia | 20:14daa48ffd4c | 231 | _WR=1; |
Geremia | 20:14daa48ffd4c | 232 | _port.write(data&0xFF); |
Geremia | 20:14daa48ffd4c | 233 | _WR=0; |
Geremia | 20:14daa48ffd4c | 234 | _WR=1; |
Geremia | 20:14daa48ffd4c | 235 | } |
Geremia | 20:14daa48ffd4c | 236 | // ILI932x specific |
Geremia | 20:14daa48ffd4c | 237 | unsigned short PAR8::reg_read(unsigned char reg) |
Geremia | 20:14daa48ffd4c | 238 | { |
Geremia | 20:14daa48ffd4c | 239 | unsigned short r=0; |
Geremia | 20:14daa48ffd4c | 240 | _DC = 0; |
Geremia | 20:14daa48ffd4c | 241 | _port.write(0); |
Geremia | 20:14daa48ffd4c | 242 | _WR=0; |
Geremia | 20:14daa48ffd4c | 243 | _WR=1; |
Geremia | 20:14daa48ffd4c | 244 | _port.write(reg); |
Geremia | 20:14daa48ffd4c | 245 | _WR=0; |
Geremia | 20:14daa48ffd4c | 246 | _WR=1; |
Geremia | 20:14daa48ffd4c | 247 | _DC = 1; |
Geremia | 20:14daa48ffd4c | 248 | _port.input(); |
Geremia | 20:14daa48ffd4c | 249 | _RD=0; |
Geremia | 20:14daa48ffd4c | 250 | r |= _port.read(); // read 8bit |
Geremia | 20:14daa48ffd4c | 251 | _RD=1; |
Geremia | 20:14daa48ffd4c | 252 | r <<= 8; |
Geremia | 20:14daa48ffd4c | 253 | _RD=0; |
Geremia | 20:14daa48ffd4c | 254 | r |= _port.read(); // read 8bit |
Geremia | 20:14daa48ffd4c | 255 | _RD=1; |
Geremia | 20:14daa48ffd4c | 256 | _port.output(); |
Geremia | 20:14daa48ffd4c | 257 | |
Geremia | 20:14daa48ffd4c | 258 | return r; |
Geremia | 20:14daa48ffd4c | 259 | } |
Geremia | 0:75ec1b3cde17 | 260 | void PAR8::hw_reset() |
Geremia | 0:75ec1b3cde17 | 261 | { |
Geremia | 0:75ec1b3cde17 | 262 | wait_ms(15); |
Geremia | 0:75ec1b3cde17 | 263 | _DC = 1; |
Geremia | 0:75ec1b3cde17 | 264 | _CS = 1; |
Geremia | 0:75ec1b3cde17 | 265 | _WR = 1; |
Geremia | 0:75ec1b3cde17 | 266 | _RD = 1; |
Geremia | 0:75ec1b3cde17 | 267 | _reset = 0; // display reset |
Geremia | 20:14daa48ffd4c | 268 | wait_ms(2); |
Geremia | 0:75ec1b3cde17 | 269 | _reset = 1; // end reset |
Geremia | 20:14daa48ffd4c | 270 | wait_ms(100); |
Geremia | 0:75ec1b3cde17 | 271 | } |
Geremia | 0:75ec1b3cde17 | 272 | void PAR8::BusEnable(bool enable) |
Geremia | 0:75ec1b3cde17 | 273 | { |
Geremia | 0:75ec1b3cde17 | 274 | _CS = enable ? 0:1; |
Geremia | 0:75ec1b3cde17 | 275 | } |