UniGraphics Library Fork to support mbed os 6.3 Release for ILI9341

Dependents:   TFT_ILI9341_UniGraphic TFT_ILI9341_os6

Committer:
amouroug
Date:
Thu Oct 08 17:58:07 2020 -0500
Revision:
1:6f267dbbafec
Parent:
0:bb2bda4f5846
Updated for ThisThread::sleep_for() pnemonics, cleaned up API Libs.

Who changed what in which revision?

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