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