.

Dependents:   projekat

Committer:
2018US_EminaTahirovi
Date:
Sun Jun 16 20:26:22 2019 +0000
Revision:
0:1b3481785d59
.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
2018US_EminaTahirovi 0:1b3481785d59 1 /* mbed library for 240*320 pixel display TFT based on ILI9341 LCD Controller
2018US_EminaTahirovi 0:1b3481785d59 2 * Copyright (c) 2014 Peter Drescher - DC2PD
2018US_EminaTahirovi 0:1b3481785d59 3 * Special version for NXP LPC1768
2018US_EminaTahirovi 0:1b3481785d59 4 *
2018US_EminaTahirovi 0:1b3481785d59 5 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2018US_EminaTahirovi 0:1b3481785d59 6 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2018US_EminaTahirovi 0:1b3481785d59 7 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2018US_EminaTahirovi 0:1b3481785d59 8 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2018US_EminaTahirovi 0:1b3481785d59 9 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2018US_EminaTahirovi 0:1b3481785d59 10 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2018US_EminaTahirovi 0:1b3481785d59 11 * THE SOFTWARE.
2018US_EminaTahirovi 0:1b3481785d59 12 */
2018US_EminaTahirovi 0:1b3481785d59 13
2018US_EminaTahirovi 0:1b3481785d59 14 // 25.06.14 initial version
2018US_EminaTahirovi 0:1b3481785d59 15
2018US_EminaTahirovi 0:1b3481785d59 16 // only include this file if target is LPC1768
2018US_EminaTahirovi 0:1b3481785d59 17 #if defined TARGET_LPC1768
2018US_EminaTahirovi 0:1b3481785d59 18
2018US_EminaTahirovi 0:1b3481785d59 19 #include "SPI_TFT_ILI9341.h"
2018US_EminaTahirovi 0:1b3481785d59 20 #include "mbed.h"
2018US_EminaTahirovi 0:1b3481785d59 21
2018US_EminaTahirovi 0:1b3481785d59 22 #if defined TARGET_LPC1768
2018US_EminaTahirovi 0:1b3481785d59 23 #define use_ram
2018US_EminaTahirovi 0:1b3481785d59 24 #endif
2018US_EminaTahirovi 0:1b3481785d59 25
2018US_EminaTahirovi 0:1b3481785d59 26 // some defines for the DMA use
2018US_EminaTahirovi 0:1b3481785d59 27 #define DMA_CHANNEL_ENABLE 1
2018US_EminaTahirovi 0:1b3481785d59 28 #define DMA_TRANSFER_TYPE_M2P (1UL << 11)
2018US_EminaTahirovi 0:1b3481785d59 29 #define DMA_CHANNEL_TCIE (1UL << 31)
2018US_EminaTahirovi 0:1b3481785d59 30 #define DMA_CHANNEL_SRC_INC (1UL << 26)
2018US_EminaTahirovi 0:1b3481785d59 31 #define DMA_MASK_IE (1UL << 14)
2018US_EminaTahirovi 0:1b3481785d59 32 #define DMA_MASK_ITC (1UL << 15)
2018US_EminaTahirovi 0:1b3481785d59 33 #define DMA_SSP1_TX (1UL << 2)
2018US_EminaTahirovi 0:1b3481785d59 34 #define DMA_SSP0_TX (0)
2018US_EminaTahirovi 0:1b3481785d59 35 #define DMA_DEST_SSP1_TX (2UL << 6)
2018US_EminaTahirovi 0:1b3481785d59 36 #define DMA_DEST_SSP0_TX (0UL << 6)
2018US_EminaTahirovi 0:1b3481785d59 37
2018US_EminaTahirovi 0:1b3481785d59 38 #define BPP 16 // Bits per pixel
2018US_EminaTahirovi 0:1b3481785d59 39
2018US_EminaTahirovi 0:1b3481785d59 40 //extern Serial pc;
2018US_EminaTahirovi 0:1b3481785d59 41 //extern DigitalOut xx; // debug !!
2018US_EminaTahirovi 0:1b3481785d59 42
2018US_EminaTahirovi 0:1b3481785d59 43
2018US_EminaTahirovi 0:1b3481785d59 44 SPI_TFT_ILI9341::SPI_TFT_ILI9341(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, PinName dc, const char *name)
2018US_EminaTahirovi 0:1b3481785d59 45 : GraphicsDisplay(name), SPI(mosi,miso,sclk), _cs(cs), _reset(reset), _dc(dc)
2018US_EminaTahirovi 0:1b3481785d59 46 {
2018US_EminaTahirovi 0:1b3481785d59 47
2018US_EminaTahirovi 0:1b3481785d59 48 format(8,3); // 8 bit spi mode 3
2018US_EminaTahirovi 0:1b3481785d59 49 frequency(10000000); // 10 Mhz SPI clock : result 2 / 4 = 8
2018US_EminaTahirovi 0:1b3481785d59 50 orientation = 0;
2018US_EminaTahirovi 0:1b3481785d59 51 char_x = 0;
2018US_EminaTahirovi 0:1b3481785d59 52 if((int)_spi.spi == SPI_0) { // test which SPI is in use
2018US_EminaTahirovi 0:1b3481785d59 53 spi_num = 0;
2018US_EminaTahirovi 0:1b3481785d59 54 }
2018US_EminaTahirovi 0:1b3481785d59 55 if((int)_spi.spi == SPI_1) {
2018US_EminaTahirovi 0:1b3481785d59 56 spi_num = 1;
2018US_EminaTahirovi 0:1b3481785d59 57 }
2018US_EminaTahirovi 0:1b3481785d59 58 tft_reset();
2018US_EminaTahirovi 0:1b3481785d59 59 }
2018US_EminaTahirovi 0:1b3481785d59 60
2018US_EminaTahirovi 0:1b3481785d59 61 // we define a fast write to the SPI port
2018US_EminaTahirovi 0:1b3481785d59 62 void inline SPI_TFT_ILI9341::f_write(int data)
2018US_EminaTahirovi 0:1b3481785d59 63 {
2018US_EminaTahirovi 0:1b3481785d59 64 while(((_spi.spi->SR) & 0x02) == 0);
2018US_EminaTahirovi 0:1b3481785d59 65 _spi.spi->DR = data;
2018US_EminaTahirovi 0:1b3481785d59 66 }
2018US_EminaTahirovi 0:1b3481785d59 67
2018US_EminaTahirovi 0:1b3481785d59 68 // wait for SPI not busy
2018US_EminaTahirovi 0:1b3481785d59 69 // we have to wait for the last bit to switch the cs off
2018US_EminaTahirovi 0:1b3481785d59 70 void inline SPI_TFT_ILI9341::spi_bsy(void)
2018US_EminaTahirovi 0:1b3481785d59 71 {
2018US_EminaTahirovi 0:1b3481785d59 72 while ((_spi.spi->SR & 0x10) == 0x10); // SPI not idle
2018US_EminaTahirovi 0:1b3481785d59 73 }
2018US_EminaTahirovi 0:1b3481785d59 74
2018US_EminaTahirovi 0:1b3481785d59 75
2018US_EminaTahirovi 0:1b3481785d59 76 // switch fast between 8 and 16 bit mode
2018US_EminaTahirovi 0:1b3481785d59 77 void SPI_TFT_ILI9341::spi_16(bool s)
2018US_EminaTahirovi 0:1b3481785d59 78 {
2018US_EminaTahirovi 0:1b3481785d59 79 if(s) _spi.spi->CR0 |= 0x08; // switch to 16 bit Mode
2018US_EminaTahirovi 0:1b3481785d59 80 else _spi.spi->CR0 &= ~(0x08); // switch to 8 bit Mode
2018US_EminaTahirovi 0:1b3481785d59 81
2018US_EminaTahirovi 0:1b3481785d59 82 }
2018US_EminaTahirovi 0:1b3481785d59 83
2018US_EminaTahirovi 0:1b3481785d59 84
2018US_EminaTahirovi 0:1b3481785d59 85 int SPI_TFT_ILI9341::width()
2018US_EminaTahirovi 0:1b3481785d59 86 {
2018US_EminaTahirovi 0:1b3481785d59 87 if (orientation == 0 || orientation == 2) return 240;
2018US_EminaTahirovi 0:1b3481785d59 88 else return 320;
2018US_EminaTahirovi 0:1b3481785d59 89 }
2018US_EminaTahirovi 0:1b3481785d59 90
2018US_EminaTahirovi 0:1b3481785d59 91
2018US_EminaTahirovi 0:1b3481785d59 92 int SPI_TFT_ILI9341::height()
2018US_EminaTahirovi 0:1b3481785d59 93 {
2018US_EminaTahirovi 0:1b3481785d59 94 if (orientation == 0 || orientation == 2) return 320;
2018US_EminaTahirovi 0:1b3481785d59 95 else return 240;
2018US_EminaTahirovi 0:1b3481785d59 96 }
2018US_EminaTahirovi 0:1b3481785d59 97
2018US_EminaTahirovi 0:1b3481785d59 98
2018US_EminaTahirovi 0:1b3481785d59 99 void SPI_TFT_ILI9341::set_orientation(unsigned int o)
2018US_EminaTahirovi 0:1b3481785d59 100 {
2018US_EminaTahirovi 0:1b3481785d59 101 orientation = o;
2018US_EminaTahirovi 0:1b3481785d59 102 wr_cmd(0x36); // MEMORY_ACCESS_CONTROL
2018US_EminaTahirovi 0:1b3481785d59 103 switch (orientation) {
2018US_EminaTahirovi 0:1b3481785d59 104 case 0:
2018US_EminaTahirovi 0:1b3481785d59 105 f_write(0x48);
2018US_EminaTahirovi 0:1b3481785d59 106 break;
2018US_EminaTahirovi 0:1b3481785d59 107 case 1:
2018US_EminaTahirovi 0:1b3481785d59 108 f_write(0x28);
2018US_EminaTahirovi 0:1b3481785d59 109 break;
2018US_EminaTahirovi 0:1b3481785d59 110 case 2:
2018US_EminaTahirovi 0:1b3481785d59 111 f_write(0x88);
2018US_EminaTahirovi 0:1b3481785d59 112 break;
2018US_EminaTahirovi 0:1b3481785d59 113 case 3:
2018US_EminaTahirovi 0:1b3481785d59 114 f_write(0xE8);
2018US_EminaTahirovi 0:1b3481785d59 115 break;
2018US_EminaTahirovi 0:1b3481785d59 116 }
2018US_EminaTahirovi 0:1b3481785d59 117 spi_bsy(); // wait for end of transfer
2018US_EminaTahirovi 0:1b3481785d59 118 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 119 WindowMax();
2018US_EminaTahirovi 0:1b3481785d59 120 }
2018US_EminaTahirovi 0:1b3481785d59 121
2018US_EminaTahirovi 0:1b3481785d59 122
2018US_EminaTahirovi 0:1b3481785d59 123 // write command to tft register
2018US_EminaTahirovi 0:1b3481785d59 124 // use fast command
2018US_EminaTahirovi 0:1b3481785d59 125 void SPI_TFT_ILI9341::wr_cmd(unsigned char cmd)
2018US_EminaTahirovi 0:1b3481785d59 126 {
2018US_EminaTahirovi 0:1b3481785d59 127 _dc = 0;
2018US_EminaTahirovi 0:1b3481785d59 128 _cs = 0;
2018US_EminaTahirovi 0:1b3481785d59 129 f_write(cmd);
2018US_EminaTahirovi 0:1b3481785d59 130 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 131 _dc = 1;
2018US_EminaTahirovi 0:1b3481785d59 132 }
2018US_EminaTahirovi 0:1b3481785d59 133
2018US_EminaTahirovi 0:1b3481785d59 134 void SPI_TFT_ILI9341::wr_dat(unsigned char dat)
2018US_EminaTahirovi 0:1b3481785d59 135 {
2018US_EminaTahirovi 0:1b3481785d59 136 f_write(dat);
2018US_EminaTahirovi 0:1b3481785d59 137 spi_bsy(); // wait for SPI send
2018US_EminaTahirovi 0:1b3481785d59 138 }
2018US_EminaTahirovi 0:1b3481785d59 139
2018US_EminaTahirovi 0:1b3481785d59 140 // the ILI9341 can read
2018US_EminaTahirovi 0:1b3481785d59 141 char SPI_TFT_ILI9341::rd_byte(unsigned char cmd)
2018US_EminaTahirovi 0:1b3481785d59 142 {
2018US_EminaTahirovi 0:1b3481785d59 143 // has to change !!
2018US_EminaTahirovi 0:1b3481785d59 144 return(0);
2018US_EminaTahirovi 0:1b3481785d59 145 }
2018US_EminaTahirovi 0:1b3481785d59 146
2018US_EminaTahirovi 0:1b3481785d59 147 // read 32 bit
2018US_EminaTahirovi 0:1b3481785d59 148 int SPI_TFT_ILI9341::rd_32(unsigned char cmd)
2018US_EminaTahirovi 0:1b3481785d59 149 {
2018US_EminaTahirovi 0:1b3481785d59 150 // has to change !!!
2018US_EminaTahirovi 0:1b3481785d59 151 return(0);
2018US_EminaTahirovi 0:1b3481785d59 152 }
2018US_EminaTahirovi 0:1b3481785d59 153
2018US_EminaTahirovi 0:1b3481785d59 154 int SPI_TFT_ILI9341::Read_ID(void)
2018US_EminaTahirovi 0:1b3481785d59 155 {
2018US_EminaTahirovi 0:1b3481785d59 156 int r;
2018US_EminaTahirovi 0:1b3481785d59 157 r = rd_byte(0x0A);
2018US_EminaTahirovi 0:1b3481785d59 158 r = rd_byte(0x0A);
2018US_EminaTahirovi 0:1b3481785d59 159 r = rd_byte(0x0A);
2018US_EminaTahirovi 0:1b3481785d59 160 r = rd_byte(0x0A);
2018US_EminaTahirovi 0:1b3481785d59 161 return(r);
2018US_EminaTahirovi 0:1b3481785d59 162 }
2018US_EminaTahirovi 0:1b3481785d59 163
2018US_EminaTahirovi 0:1b3481785d59 164
2018US_EminaTahirovi 0:1b3481785d59 165 // Init code based on MI0283QT datasheet
2018US_EminaTahirovi 0:1b3481785d59 166 // this code is called only at start
2018US_EminaTahirovi 0:1b3481785d59 167 // no need to be optimized
2018US_EminaTahirovi 0:1b3481785d59 168
2018US_EminaTahirovi 0:1b3481785d59 169 void SPI_TFT_ILI9341::tft_reset()
2018US_EminaTahirovi 0:1b3481785d59 170 {
2018US_EminaTahirovi 0:1b3481785d59 171 _cs = 1; // cs high
2018US_EminaTahirovi 0:1b3481785d59 172 _dc = 1; // dc high
2018US_EminaTahirovi 0:1b3481785d59 173 _reset = 0; // display reset
2018US_EminaTahirovi 0:1b3481785d59 174
2018US_EminaTahirovi 0:1b3481785d59 175 wait_us(50);
2018US_EminaTahirovi 0:1b3481785d59 176 _reset = 1; // end hardware reset
2018US_EminaTahirovi 0:1b3481785d59 177 wait_ms(5);
2018US_EminaTahirovi 0:1b3481785d59 178
2018US_EminaTahirovi 0:1b3481785d59 179 wr_cmd(0x01); // SW reset
2018US_EminaTahirovi 0:1b3481785d59 180 wait_ms(5);
2018US_EminaTahirovi 0:1b3481785d59 181 wr_cmd(0x28); // display off
2018US_EminaTahirovi 0:1b3481785d59 182
2018US_EminaTahirovi 0:1b3481785d59 183 /* Start Initial Sequence ----------------------------------------------------*/
2018US_EminaTahirovi 0:1b3481785d59 184 wr_cmd(0xCF);
2018US_EminaTahirovi 0:1b3481785d59 185 f_write(0x00);
2018US_EminaTahirovi 0:1b3481785d59 186 f_write(0x83);
2018US_EminaTahirovi 0:1b3481785d59 187 f_write(0x30);
2018US_EminaTahirovi 0:1b3481785d59 188 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 189 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 190
2018US_EminaTahirovi 0:1b3481785d59 191 wr_cmd(0xED);
2018US_EminaTahirovi 0:1b3481785d59 192 f_write(0x64);
2018US_EminaTahirovi 0:1b3481785d59 193 f_write(0x03);
2018US_EminaTahirovi 0:1b3481785d59 194 f_write(0x12);
2018US_EminaTahirovi 0:1b3481785d59 195 f_write(0x81);
2018US_EminaTahirovi 0:1b3481785d59 196 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 197 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 198
2018US_EminaTahirovi 0:1b3481785d59 199 wr_cmd(0xE8);
2018US_EminaTahirovi 0:1b3481785d59 200 f_write(0x85);
2018US_EminaTahirovi 0:1b3481785d59 201 f_write(0x01);
2018US_EminaTahirovi 0:1b3481785d59 202 f_write(0x79);
2018US_EminaTahirovi 0:1b3481785d59 203 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 204 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 205
2018US_EminaTahirovi 0:1b3481785d59 206 wr_cmd(0xCB);
2018US_EminaTahirovi 0:1b3481785d59 207 f_write(0x39);
2018US_EminaTahirovi 0:1b3481785d59 208 f_write(0x2C);
2018US_EminaTahirovi 0:1b3481785d59 209 f_write(0x00);
2018US_EminaTahirovi 0:1b3481785d59 210 f_write(0x34);
2018US_EminaTahirovi 0:1b3481785d59 211 f_write(0x02);
2018US_EminaTahirovi 0:1b3481785d59 212 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 213 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 214
2018US_EminaTahirovi 0:1b3481785d59 215 wr_cmd(0xF7);
2018US_EminaTahirovi 0:1b3481785d59 216 f_write(0x20);
2018US_EminaTahirovi 0:1b3481785d59 217 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 218 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 219
2018US_EminaTahirovi 0:1b3481785d59 220 wr_cmd(0xEA);
2018US_EminaTahirovi 0:1b3481785d59 221 f_write(0x00);
2018US_EminaTahirovi 0:1b3481785d59 222 f_write(0x00);
2018US_EminaTahirovi 0:1b3481785d59 223 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 224 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 225
2018US_EminaTahirovi 0:1b3481785d59 226 wr_cmd(0xC0); // POWER_CONTROL_1
2018US_EminaTahirovi 0:1b3481785d59 227 f_write(0x26);
2018US_EminaTahirovi 0:1b3481785d59 228 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 229 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 230
2018US_EminaTahirovi 0:1b3481785d59 231 wr_cmd(0xC1); // POWER_CONTROL_2
2018US_EminaTahirovi 0:1b3481785d59 232 f_write(0x11);
2018US_EminaTahirovi 0:1b3481785d59 233 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 234 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 235
2018US_EminaTahirovi 0:1b3481785d59 236 wr_cmd(0xC5); // VCOM_CONTROL_1
2018US_EminaTahirovi 0:1b3481785d59 237 f_write(0x35);
2018US_EminaTahirovi 0:1b3481785d59 238 f_write(0x3E);
2018US_EminaTahirovi 0:1b3481785d59 239 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 240 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 241
2018US_EminaTahirovi 0:1b3481785d59 242 wr_cmd(0xC7); // VCOM_CONTROL_2
2018US_EminaTahirovi 0:1b3481785d59 243 f_write(0xBE);
2018US_EminaTahirovi 0:1b3481785d59 244 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 245 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 246
2018US_EminaTahirovi 0:1b3481785d59 247 wr_cmd(0x36); // MEMORY_ACCESS_CONTROL
2018US_EminaTahirovi 0:1b3481785d59 248 f_write(0x48);
2018US_EminaTahirovi 0:1b3481785d59 249 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 250 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 251
2018US_EminaTahirovi 0:1b3481785d59 252 wr_cmd(0x3A); // COLMOD_PIXEL_FORMAT_SET
2018US_EminaTahirovi 0:1b3481785d59 253 f_write(0x55); // 16 bit pixel
2018US_EminaTahirovi 0:1b3481785d59 254 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 255 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 256
2018US_EminaTahirovi 0:1b3481785d59 257 wr_cmd(0xB1); // Frame Rate
2018US_EminaTahirovi 0:1b3481785d59 258 f_write(0x00);
2018US_EminaTahirovi 0:1b3481785d59 259 f_write(0x1B);
2018US_EminaTahirovi 0:1b3481785d59 260 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 261 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 262
2018US_EminaTahirovi 0:1b3481785d59 263 wr_cmd(0xF2); // Gamma Function Disable
2018US_EminaTahirovi 0:1b3481785d59 264 f_write(0x08);
2018US_EminaTahirovi 0:1b3481785d59 265 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 266 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 267
2018US_EminaTahirovi 0:1b3481785d59 268 wr_cmd(0x26);
2018US_EminaTahirovi 0:1b3481785d59 269 f_write(0x01); // gamma set for curve 01/2/04/08
2018US_EminaTahirovi 0:1b3481785d59 270 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 271 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 272
2018US_EminaTahirovi 0:1b3481785d59 273 wr_cmd(0xE0); // positive gamma correction
2018US_EminaTahirovi 0:1b3481785d59 274 f_write(0x1F);
2018US_EminaTahirovi 0:1b3481785d59 275 f_write(0x1A);
2018US_EminaTahirovi 0:1b3481785d59 276 f_write(0x18);
2018US_EminaTahirovi 0:1b3481785d59 277 f_write(0x0A);
2018US_EminaTahirovi 0:1b3481785d59 278 f_write(0x0F);
2018US_EminaTahirovi 0:1b3481785d59 279 f_write(0x06);
2018US_EminaTahirovi 0:1b3481785d59 280 f_write(0x45);
2018US_EminaTahirovi 0:1b3481785d59 281 f_write(0x87);
2018US_EminaTahirovi 0:1b3481785d59 282 f_write(0x32);
2018US_EminaTahirovi 0:1b3481785d59 283 f_write(0x0A);
2018US_EminaTahirovi 0:1b3481785d59 284 f_write(0x07);
2018US_EminaTahirovi 0:1b3481785d59 285 f_write(0x02);
2018US_EminaTahirovi 0:1b3481785d59 286 f_write(0x07);
2018US_EminaTahirovi 0:1b3481785d59 287 f_write(0x05);
2018US_EminaTahirovi 0:1b3481785d59 288 f_write(0x00);
2018US_EminaTahirovi 0:1b3481785d59 289 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 290 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 291
2018US_EminaTahirovi 0:1b3481785d59 292 wr_cmd(0xE1); // negativ gamma correction
2018US_EminaTahirovi 0:1b3481785d59 293 f_write(0x00);
2018US_EminaTahirovi 0:1b3481785d59 294 f_write(0x25);
2018US_EminaTahirovi 0:1b3481785d59 295 f_write(0x27);
2018US_EminaTahirovi 0:1b3481785d59 296 f_write(0x05);
2018US_EminaTahirovi 0:1b3481785d59 297 f_write(0x10);
2018US_EminaTahirovi 0:1b3481785d59 298 f_write(0x09);
2018US_EminaTahirovi 0:1b3481785d59 299 f_write(0x3A);
2018US_EminaTahirovi 0:1b3481785d59 300 f_write(0x78);
2018US_EminaTahirovi 0:1b3481785d59 301 f_write(0x4D);
2018US_EminaTahirovi 0:1b3481785d59 302 f_write(0x05);
2018US_EminaTahirovi 0:1b3481785d59 303 f_write(0x18);
2018US_EminaTahirovi 0:1b3481785d59 304 f_write(0x0D);
2018US_EminaTahirovi 0:1b3481785d59 305 f_write(0x38);
2018US_EminaTahirovi 0:1b3481785d59 306 f_write(0x3A);
2018US_EminaTahirovi 0:1b3481785d59 307 f_write(0x1F);
2018US_EminaTahirovi 0:1b3481785d59 308 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 309 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 310
2018US_EminaTahirovi 0:1b3481785d59 311 WindowMax ();
2018US_EminaTahirovi 0:1b3481785d59 312
2018US_EminaTahirovi 0:1b3481785d59 313 //wr_cmd(0x34); // tearing effect off
2018US_EminaTahirovi 0:1b3481785d59 314 //_cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 315
2018US_EminaTahirovi 0:1b3481785d59 316 //wr_cmd(0x35); // tearing effect on
2018US_EminaTahirovi 0:1b3481785d59 317 //_cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 318
2018US_EminaTahirovi 0:1b3481785d59 319 wr_cmd(0xB7); // entry mode
2018US_EminaTahirovi 0:1b3481785d59 320 f_write(0x07);
2018US_EminaTahirovi 0:1b3481785d59 321 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 322 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 323
2018US_EminaTahirovi 0:1b3481785d59 324 wr_cmd(0xB6); // display function control
2018US_EminaTahirovi 0:1b3481785d59 325 f_write(0x0A);
2018US_EminaTahirovi 0:1b3481785d59 326 f_write(0x82);
2018US_EminaTahirovi 0:1b3481785d59 327 f_write(0x27);
2018US_EminaTahirovi 0:1b3481785d59 328 f_write(0x00);
2018US_EminaTahirovi 0:1b3481785d59 329 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 330 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 331
2018US_EminaTahirovi 0:1b3481785d59 332 wr_cmd(0x11); // sleep out
2018US_EminaTahirovi 0:1b3481785d59 333 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 334 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 335
2018US_EminaTahirovi 0:1b3481785d59 336 wait_ms(100);
2018US_EminaTahirovi 0:1b3481785d59 337
2018US_EminaTahirovi 0:1b3481785d59 338 wr_cmd(0x29); // display on
2018US_EminaTahirovi 0:1b3481785d59 339 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 340 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 341
2018US_EminaTahirovi 0:1b3481785d59 342 wait_ms(100);
2018US_EminaTahirovi 0:1b3481785d59 343
2018US_EminaTahirovi 0:1b3481785d59 344 // setup DMA channel 0
2018US_EminaTahirovi 0:1b3481785d59 345 LPC_SC->PCONP |= (1UL << 29); // Power up the GPDMA.
2018US_EminaTahirovi 0:1b3481785d59 346 LPC_GPDMA->DMACConfig = 1; // enable DMA controller
2018US_EminaTahirovi 0:1b3481785d59 347 LPC_GPDMA->DMACIntTCClear = 0x1; // Reset the Interrupt status
2018US_EminaTahirovi 0:1b3481785d59 348 LPC_GPDMA->DMACIntErrClr = 0x1;
2018US_EminaTahirovi 0:1b3481785d59 349 LPC_GPDMACH0->DMACCLLI = 0;
2018US_EminaTahirovi 0:1b3481785d59 350 }
2018US_EminaTahirovi 0:1b3481785d59 351
2018US_EminaTahirovi 0:1b3481785d59 352
2018US_EminaTahirovi 0:1b3481785d59 353 // speed optimized
2018US_EminaTahirovi 0:1b3481785d59 354 // write direct to SPI1 register !
2018US_EminaTahirovi 0:1b3481785d59 355 void SPI_TFT_ILI9341::pixel(int x, int y, int color)
2018US_EminaTahirovi 0:1b3481785d59 356 {
2018US_EminaTahirovi 0:1b3481785d59 357 wr_cmd(0x2A);
2018US_EminaTahirovi 0:1b3481785d59 358 spi_16(1); // switch to 8 bit Mode
2018US_EminaTahirovi 0:1b3481785d59 359 f_write(x);
2018US_EminaTahirovi 0:1b3481785d59 360 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 361 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 362
2018US_EminaTahirovi 0:1b3481785d59 363 spi_16(0); // switch to 8 bit Mode
2018US_EminaTahirovi 0:1b3481785d59 364 wr_cmd(0x2B);
2018US_EminaTahirovi 0:1b3481785d59 365 spi_16(1);
2018US_EminaTahirovi 0:1b3481785d59 366 f_write(y);
2018US_EminaTahirovi 0:1b3481785d59 367 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 368 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 369 spi_16(0);
2018US_EminaTahirovi 0:1b3481785d59 370
2018US_EminaTahirovi 0:1b3481785d59 371 wr_cmd(0x2C); // send pixel
2018US_EminaTahirovi 0:1b3481785d59 372 spi_16(1);
2018US_EminaTahirovi 0:1b3481785d59 373 f_write(color);
2018US_EminaTahirovi 0:1b3481785d59 374 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 375 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 376 spi_16(0);
2018US_EminaTahirovi 0:1b3481785d59 377 }
2018US_EminaTahirovi 0:1b3481785d59 378
2018US_EminaTahirovi 0:1b3481785d59 379 // optimized
2018US_EminaTahirovi 0:1b3481785d59 380 // write direct to SPI1 register !
2018US_EminaTahirovi 0:1b3481785d59 381 void SPI_TFT_ILI9341::window (unsigned int x, unsigned int y, unsigned int w, unsigned int h)
2018US_EminaTahirovi 0:1b3481785d59 382 {
2018US_EminaTahirovi 0:1b3481785d59 383 wr_cmd(0x2A);
2018US_EminaTahirovi 0:1b3481785d59 384 spi_16(1);
2018US_EminaTahirovi 0:1b3481785d59 385 f_write(x);
2018US_EminaTahirovi 0:1b3481785d59 386 f_write(x+w-1);
2018US_EminaTahirovi 0:1b3481785d59 387 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 388 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 389 spi_16(0);
2018US_EminaTahirovi 0:1b3481785d59 390
2018US_EminaTahirovi 0:1b3481785d59 391 wr_cmd(0x2B);
2018US_EminaTahirovi 0:1b3481785d59 392 spi_16(1);
2018US_EminaTahirovi 0:1b3481785d59 393 f_write(y) ;
2018US_EminaTahirovi 0:1b3481785d59 394 f_write(y+h-1);
2018US_EminaTahirovi 0:1b3481785d59 395 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 396 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 397 spi_16(0);
2018US_EminaTahirovi 0:1b3481785d59 398 }
2018US_EminaTahirovi 0:1b3481785d59 399
2018US_EminaTahirovi 0:1b3481785d59 400
2018US_EminaTahirovi 0:1b3481785d59 401 void SPI_TFT_ILI9341::WindowMax (void)
2018US_EminaTahirovi 0:1b3481785d59 402 {
2018US_EminaTahirovi 0:1b3481785d59 403 window (0, 0, width(), height());
2018US_EminaTahirovi 0:1b3481785d59 404 }
2018US_EminaTahirovi 0:1b3481785d59 405
2018US_EminaTahirovi 0:1b3481785d59 406 // optimized
2018US_EminaTahirovi 0:1b3481785d59 407 // use DMA to transfer pixel data to the screen
2018US_EminaTahirovi 0:1b3481785d59 408 void SPI_TFT_ILI9341::cls (void)
2018US_EminaTahirovi 0:1b3481785d59 409 {
2018US_EminaTahirovi 0:1b3481785d59 410 // we can use the fillrect function
2018US_EminaTahirovi 0:1b3481785d59 411 fillrect(0,0,width()-1,height()-1,_background);
2018US_EminaTahirovi 0:1b3481785d59 412 }
2018US_EminaTahirovi 0:1b3481785d59 413
2018US_EminaTahirovi 0:1b3481785d59 414 void SPI_TFT_ILI9341::circle(int x0, int y0, int r, int color)
2018US_EminaTahirovi 0:1b3481785d59 415 {
2018US_EminaTahirovi 0:1b3481785d59 416
2018US_EminaTahirovi 0:1b3481785d59 417 int x = -r, y = 0, err = 2-2*r, e2;
2018US_EminaTahirovi 0:1b3481785d59 418 do {
2018US_EminaTahirovi 0:1b3481785d59 419 pixel(x0-x, y0+y,color);
2018US_EminaTahirovi 0:1b3481785d59 420 pixel(x0+x, y0+y,color);
2018US_EminaTahirovi 0:1b3481785d59 421 pixel(x0+x, y0-y,color);
2018US_EminaTahirovi 0:1b3481785d59 422 pixel(x0-x, y0-y,color);
2018US_EminaTahirovi 0:1b3481785d59 423 e2 = err;
2018US_EminaTahirovi 0:1b3481785d59 424 if (e2 <= y) {
2018US_EminaTahirovi 0:1b3481785d59 425 err += ++y*2+1;
2018US_EminaTahirovi 0:1b3481785d59 426 if (-x == y && e2 <= x) e2 = 0;
2018US_EminaTahirovi 0:1b3481785d59 427 }
2018US_EminaTahirovi 0:1b3481785d59 428 if (e2 > x) err += ++x*2+1;
2018US_EminaTahirovi 0:1b3481785d59 429 } while (x <= 0);
2018US_EminaTahirovi 0:1b3481785d59 430 }
2018US_EminaTahirovi 0:1b3481785d59 431
2018US_EminaTahirovi 0:1b3481785d59 432 void SPI_TFT_ILI9341::fillcircle(int x0, int y0, int r, int color)
2018US_EminaTahirovi 0:1b3481785d59 433 {
2018US_EminaTahirovi 0:1b3481785d59 434 int x = -r, y = 0, err = 2-2*r, e2;
2018US_EminaTahirovi 0:1b3481785d59 435 do {
2018US_EminaTahirovi 0:1b3481785d59 436 vline(x0-x, y0-y, y0+y, color);
2018US_EminaTahirovi 0:1b3481785d59 437 vline(x0+x, y0-y, y0+y, color);
2018US_EminaTahirovi 0:1b3481785d59 438 e2 = err;
2018US_EminaTahirovi 0:1b3481785d59 439 if (e2 <= y) {
2018US_EminaTahirovi 0:1b3481785d59 440 err += ++y*2+1;
2018US_EminaTahirovi 0:1b3481785d59 441 if (-x == y && e2 <= x) e2 = 0;
2018US_EminaTahirovi 0:1b3481785d59 442 }
2018US_EminaTahirovi 0:1b3481785d59 443 if (e2 > x) err += ++x*2+1;
2018US_EminaTahirovi 0:1b3481785d59 444 } while (x <= 0);
2018US_EminaTahirovi 0:1b3481785d59 445 }
2018US_EminaTahirovi 0:1b3481785d59 446
2018US_EminaTahirovi 0:1b3481785d59 447
2018US_EminaTahirovi 0:1b3481785d59 448 // optimized for speed
2018US_EminaTahirovi 0:1b3481785d59 449 void SPI_TFT_ILI9341::hline(int x0, int x1, int y, int color)
2018US_EminaTahirovi 0:1b3481785d59 450 {
2018US_EminaTahirovi 0:1b3481785d59 451 int w,j;
2018US_EminaTahirovi 0:1b3481785d59 452 w = x1 - x0 + 1;
2018US_EminaTahirovi 0:1b3481785d59 453 window(x0,y,w,1);
2018US_EminaTahirovi 0:1b3481785d59 454 _dc = 0;
2018US_EminaTahirovi 0:1b3481785d59 455 _cs = 0;
2018US_EminaTahirovi 0:1b3481785d59 456 f_write(0x2C); // send pixel
2018US_EminaTahirovi 0:1b3481785d59 457 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 458 _dc = 1;
2018US_EminaTahirovi 0:1b3481785d59 459 spi_16(1);
2018US_EminaTahirovi 0:1b3481785d59 460
2018US_EminaTahirovi 0:1b3481785d59 461 for (j=0; j<w; j++) {
2018US_EminaTahirovi 0:1b3481785d59 462 f_write(color);
2018US_EminaTahirovi 0:1b3481785d59 463 }
2018US_EminaTahirovi 0:1b3481785d59 464 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 465 spi_16(0);
2018US_EminaTahirovi 0:1b3481785d59 466 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 467 WindowMax();
2018US_EminaTahirovi 0:1b3481785d59 468 return;
2018US_EminaTahirovi 0:1b3481785d59 469 }
2018US_EminaTahirovi 0:1b3481785d59 470
2018US_EminaTahirovi 0:1b3481785d59 471 // optimized for speed
2018US_EminaTahirovi 0:1b3481785d59 472 void SPI_TFT_ILI9341::vline(int x, int y0, int y1, int color)
2018US_EminaTahirovi 0:1b3481785d59 473 {
2018US_EminaTahirovi 0:1b3481785d59 474 int h,y;
2018US_EminaTahirovi 0:1b3481785d59 475 h = y1 - y0 + 1;
2018US_EminaTahirovi 0:1b3481785d59 476 window(x,y0,1,h);
2018US_EminaTahirovi 0:1b3481785d59 477 _dc = 0;
2018US_EminaTahirovi 0:1b3481785d59 478 _cs = 0;
2018US_EminaTahirovi 0:1b3481785d59 479 f_write(0x2C); // send pixel
2018US_EminaTahirovi 0:1b3481785d59 480 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 481 _dc = 1;
2018US_EminaTahirovi 0:1b3481785d59 482 spi_16(1);
2018US_EminaTahirovi 0:1b3481785d59 483 // switch to 16 bit Mode 3
2018US_EminaTahirovi 0:1b3481785d59 484 for (y=0; y<h; y++) {
2018US_EminaTahirovi 0:1b3481785d59 485 f_write(color);
2018US_EminaTahirovi 0:1b3481785d59 486 }
2018US_EminaTahirovi 0:1b3481785d59 487 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 488 spi_16(0);
2018US_EminaTahirovi 0:1b3481785d59 489 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 490 WindowMax();
2018US_EminaTahirovi 0:1b3481785d59 491 return;
2018US_EminaTahirovi 0:1b3481785d59 492 }
2018US_EminaTahirovi 0:1b3481785d59 493
2018US_EminaTahirovi 0:1b3481785d59 494
2018US_EminaTahirovi 0:1b3481785d59 495 void SPI_TFT_ILI9341::line(int x0, int y0, int x1, int y1, int color)
2018US_EminaTahirovi 0:1b3481785d59 496 {
2018US_EminaTahirovi 0:1b3481785d59 497 //WindowMax();
2018US_EminaTahirovi 0:1b3481785d59 498 int dx = 0, dy = 0;
2018US_EminaTahirovi 0:1b3481785d59 499 int dx_sym = 0, dy_sym = 0;
2018US_EminaTahirovi 0:1b3481785d59 500 int dx_x2 = 0, dy_x2 = 0;
2018US_EminaTahirovi 0:1b3481785d59 501 int di = 0;
2018US_EminaTahirovi 0:1b3481785d59 502
2018US_EminaTahirovi 0:1b3481785d59 503 dx = x1-x0;
2018US_EminaTahirovi 0:1b3481785d59 504 dy = y1-y0;
2018US_EminaTahirovi 0:1b3481785d59 505
2018US_EminaTahirovi 0:1b3481785d59 506 if (dx == 0) { /* vertical line */
2018US_EminaTahirovi 0:1b3481785d59 507 if (y1 > y0) vline(x0,y0,y1,color);
2018US_EminaTahirovi 0:1b3481785d59 508 else vline(x0,y1,y0,color);
2018US_EminaTahirovi 0:1b3481785d59 509 return;
2018US_EminaTahirovi 0:1b3481785d59 510 }
2018US_EminaTahirovi 0:1b3481785d59 511
2018US_EminaTahirovi 0:1b3481785d59 512 if (dx > 0) {
2018US_EminaTahirovi 0:1b3481785d59 513 dx_sym = 1;
2018US_EminaTahirovi 0:1b3481785d59 514 } else {
2018US_EminaTahirovi 0:1b3481785d59 515 dx_sym = -1;
2018US_EminaTahirovi 0:1b3481785d59 516 }
2018US_EminaTahirovi 0:1b3481785d59 517 if (dy == 0) { /* horizontal line */
2018US_EminaTahirovi 0:1b3481785d59 518 if (x1 > x0) hline(x0,x1,y0,color);
2018US_EminaTahirovi 0:1b3481785d59 519 else hline(x1,x0,y0,color);
2018US_EminaTahirovi 0:1b3481785d59 520 return;
2018US_EminaTahirovi 0:1b3481785d59 521 }
2018US_EminaTahirovi 0:1b3481785d59 522
2018US_EminaTahirovi 0:1b3481785d59 523 if (dy > 0) {
2018US_EminaTahirovi 0:1b3481785d59 524 dy_sym = 1;
2018US_EminaTahirovi 0:1b3481785d59 525 } else {
2018US_EminaTahirovi 0:1b3481785d59 526 dy_sym = -1;
2018US_EminaTahirovi 0:1b3481785d59 527 }
2018US_EminaTahirovi 0:1b3481785d59 528
2018US_EminaTahirovi 0:1b3481785d59 529 dx = dx_sym*dx;
2018US_EminaTahirovi 0:1b3481785d59 530 dy = dy_sym*dy;
2018US_EminaTahirovi 0:1b3481785d59 531
2018US_EminaTahirovi 0:1b3481785d59 532 dx_x2 = dx*2;
2018US_EminaTahirovi 0:1b3481785d59 533 dy_x2 = dy*2;
2018US_EminaTahirovi 0:1b3481785d59 534
2018US_EminaTahirovi 0:1b3481785d59 535 if (dx >= dy) {
2018US_EminaTahirovi 0:1b3481785d59 536 di = dy_x2 - dx;
2018US_EminaTahirovi 0:1b3481785d59 537 while (x0 != x1) {
2018US_EminaTahirovi 0:1b3481785d59 538
2018US_EminaTahirovi 0:1b3481785d59 539 pixel(x0, y0, color);
2018US_EminaTahirovi 0:1b3481785d59 540 x0 += dx_sym;
2018US_EminaTahirovi 0:1b3481785d59 541 if (di<0) {
2018US_EminaTahirovi 0:1b3481785d59 542 di += dy_x2;
2018US_EminaTahirovi 0:1b3481785d59 543 } else {
2018US_EminaTahirovi 0:1b3481785d59 544 di += dy_x2 - dx_x2;
2018US_EminaTahirovi 0:1b3481785d59 545 y0 += dy_sym;
2018US_EminaTahirovi 0:1b3481785d59 546 }
2018US_EminaTahirovi 0:1b3481785d59 547 }
2018US_EminaTahirovi 0:1b3481785d59 548 pixel(x0, y0, color);
2018US_EminaTahirovi 0:1b3481785d59 549 } else {
2018US_EminaTahirovi 0:1b3481785d59 550 di = dx_x2 - dy;
2018US_EminaTahirovi 0:1b3481785d59 551 while (y0 != y1) {
2018US_EminaTahirovi 0:1b3481785d59 552 pixel(x0, y0, color);
2018US_EminaTahirovi 0:1b3481785d59 553 y0 += dy_sym;
2018US_EminaTahirovi 0:1b3481785d59 554 if (di < 0) {
2018US_EminaTahirovi 0:1b3481785d59 555 di += dx_x2;
2018US_EminaTahirovi 0:1b3481785d59 556 } else {
2018US_EminaTahirovi 0:1b3481785d59 557 di += dx_x2 - dy_x2;
2018US_EminaTahirovi 0:1b3481785d59 558 x0 += dx_sym;
2018US_EminaTahirovi 0:1b3481785d59 559 }
2018US_EminaTahirovi 0:1b3481785d59 560 }
2018US_EminaTahirovi 0:1b3481785d59 561 pixel(x0, y0, color);
2018US_EminaTahirovi 0:1b3481785d59 562 }
2018US_EminaTahirovi 0:1b3481785d59 563 return;
2018US_EminaTahirovi 0:1b3481785d59 564 }
2018US_EminaTahirovi 0:1b3481785d59 565
2018US_EminaTahirovi 0:1b3481785d59 566
2018US_EminaTahirovi 0:1b3481785d59 567 void SPI_TFT_ILI9341::rect(int x0, int y0, int x1, int y1, int color)
2018US_EminaTahirovi 0:1b3481785d59 568 {
2018US_EminaTahirovi 0:1b3481785d59 569
2018US_EminaTahirovi 0:1b3481785d59 570 if (x1 > x0) hline(x0,x1,y0,color);
2018US_EminaTahirovi 0:1b3481785d59 571 else hline(x1,x0,y0,color);
2018US_EminaTahirovi 0:1b3481785d59 572
2018US_EminaTahirovi 0:1b3481785d59 573 if (y1 > y0) vline(x0,y0,y1,color);
2018US_EminaTahirovi 0:1b3481785d59 574 else vline(x0,y1,y0,color);
2018US_EminaTahirovi 0:1b3481785d59 575
2018US_EminaTahirovi 0:1b3481785d59 576 if (x1 > x0) hline(x0,x1,y1,color);
2018US_EminaTahirovi 0:1b3481785d59 577 else hline(x1,x0,y1,color);
2018US_EminaTahirovi 0:1b3481785d59 578
2018US_EminaTahirovi 0:1b3481785d59 579 if (y1 > y0) vline(x1,y0,y1,color);
2018US_EminaTahirovi 0:1b3481785d59 580 else vline(x1,y1,y0,color);
2018US_EminaTahirovi 0:1b3481785d59 581
2018US_EminaTahirovi 0:1b3481785d59 582 return;
2018US_EminaTahirovi 0:1b3481785d59 583 }
2018US_EminaTahirovi 0:1b3481785d59 584
2018US_EminaTahirovi 0:1b3481785d59 585
2018US_EminaTahirovi 0:1b3481785d59 586
2018US_EminaTahirovi 0:1b3481785d59 587 // optimized for speed
2018US_EminaTahirovi 0:1b3481785d59 588 // use DMA
2018US_EminaTahirovi 0:1b3481785d59 589 void SPI_TFT_ILI9341::fillrect(int x0, int y0, int x1, int y1, int color)
2018US_EminaTahirovi 0:1b3481785d59 590 {
2018US_EminaTahirovi 0:1b3481785d59 591 int h = y1 - y0 + 1;
2018US_EminaTahirovi 0:1b3481785d59 592 int w = x1 - x0 + 1;
2018US_EminaTahirovi 0:1b3481785d59 593 int pixel = h * w;
2018US_EminaTahirovi 0:1b3481785d59 594 unsigned int dma_count;
2018US_EminaTahirovi 0:1b3481785d59 595 window(x0,y0,w,h);
2018US_EminaTahirovi 0:1b3481785d59 596
2018US_EminaTahirovi 0:1b3481785d59 597 wr_cmd(0x2C); // send pixel
2018US_EminaTahirovi 0:1b3481785d59 598 spi_16(1);
2018US_EminaTahirovi 0:1b3481785d59 599
2018US_EminaTahirovi 0:1b3481785d59 600 LPC_GPDMACH0->DMACCSrcAddr = (uint32_t)&color;
2018US_EminaTahirovi 0:1b3481785d59 601
2018US_EminaTahirovi 0:1b3481785d59 602 switch(spi_num) { // decide which SPI is to use
2018US_EminaTahirovi 0:1b3481785d59 603 case (0):
2018US_EminaTahirovi 0:1b3481785d59 604 LPC_GPDMACH0->DMACCDestAddr = (uint32_t)&LPC_SSP0->DR; // we send to SSP0
2018US_EminaTahirovi 0:1b3481785d59 605 LPC_SSP0->DMACR = 0x2;
2018US_EminaTahirovi 0:1b3481785d59 606 break;
2018US_EminaTahirovi 0:1b3481785d59 607 case (1):
2018US_EminaTahirovi 0:1b3481785d59 608 LPC_GPDMACH0->DMACCDestAddr = (uint32_t)&LPC_SSP1->DR; // we send to SSP1
2018US_EminaTahirovi 0:1b3481785d59 609 LPC_SSP1->DMACR = 0x2;
2018US_EminaTahirovi 0:1b3481785d59 610 break;
2018US_EminaTahirovi 0:1b3481785d59 611 }
2018US_EminaTahirovi 0:1b3481785d59 612
2018US_EminaTahirovi 0:1b3481785d59 613 // start DMA
2018US_EminaTahirovi 0:1b3481785d59 614 do {
2018US_EminaTahirovi 0:1b3481785d59 615 if (pixel > 4095) {
2018US_EminaTahirovi 0:1b3481785d59 616 dma_count = 4095;
2018US_EminaTahirovi 0:1b3481785d59 617 pixel = pixel - 4095;
2018US_EminaTahirovi 0:1b3481785d59 618 } else {
2018US_EminaTahirovi 0:1b3481785d59 619 dma_count = pixel;
2018US_EminaTahirovi 0:1b3481785d59 620 pixel = 0;
2018US_EminaTahirovi 0:1b3481785d59 621 }
2018US_EminaTahirovi 0:1b3481785d59 622 LPC_GPDMA->DMACIntTCClear = 0x1;
2018US_EminaTahirovi 0:1b3481785d59 623 LPC_GPDMA->DMACIntErrClr = 0x1;
2018US_EminaTahirovi 0:1b3481785d59 624 LPC_GPDMACH0->DMACCControl = dma_count | (1UL << 18) | (1UL << 21) | (1UL << 31) ; // 16 bit transfer , no address increment, interrupt
2018US_EminaTahirovi 0:1b3481785d59 625 LPC_GPDMACH0->DMACCConfig = DMA_CHANNEL_ENABLE | DMA_TRANSFER_TYPE_M2P | (spi_num ? DMA_DEST_SSP1_TX : DMA_DEST_SSP0_TX);
2018US_EminaTahirovi 0:1b3481785d59 626 LPC_GPDMA->DMACSoftSReq = 0x1; // DMA request
2018US_EminaTahirovi 0:1b3481785d59 627 do {
2018US_EminaTahirovi 0:1b3481785d59 628 } while ((LPC_GPDMA->DMACRawIntTCStat & 0x01) == 0); // DMA is running
2018US_EminaTahirovi 0:1b3481785d59 629 } while (pixel > 0);
2018US_EminaTahirovi 0:1b3481785d59 630
2018US_EminaTahirovi 0:1b3481785d59 631 spi_bsy(); // wait for end of transfer
2018US_EminaTahirovi 0:1b3481785d59 632 spi_16(0);
2018US_EminaTahirovi 0:1b3481785d59 633 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 634 WindowMax();
2018US_EminaTahirovi 0:1b3481785d59 635 return;
2018US_EminaTahirovi 0:1b3481785d59 636 }
2018US_EminaTahirovi 0:1b3481785d59 637
2018US_EminaTahirovi 0:1b3481785d59 638 void SPI_TFT_ILI9341::locate(int x, int y)
2018US_EminaTahirovi 0:1b3481785d59 639 {
2018US_EminaTahirovi 0:1b3481785d59 640 char_x = x;
2018US_EminaTahirovi 0:1b3481785d59 641 char_y = y;
2018US_EminaTahirovi 0:1b3481785d59 642 }
2018US_EminaTahirovi 0:1b3481785d59 643
2018US_EminaTahirovi 0:1b3481785d59 644 int SPI_TFT_ILI9341::columns()
2018US_EminaTahirovi 0:1b3481785d59 645 {
2018US_EminaTahirovi 0:1b3481785d59 646 return width() / font[1];
2018US_EminaTahirovi 0:1b3481785d59 647 }
2018US_EminaTahirovi 0:1b3481785d59 648
2018US_EminaTahirovi 0:1b3481785d59 649
2018US_EminaTahirovi 0:1b3481785d59 650 int SPI_TFT_ILI9341::rows()
2018US_EminaTahirovi 0:1b3481785d59 651 {
2018US_EminaTahirovi 0:1b3481785d59 652 return height() / font[2];
2018US_EminaTahirovi 0:1b3481785d59 653 }
2018US_EminaTahirovi 0:1b3481785d59 654
2018US_EminaTahirovi 0:1b3481785d59 655
2018US_EminaTahirovi 0:1b3481785d59 656 int SPI_TFT_ILI9341::_putc(int value)
2018US_EminaTahirovi 0:1b3481785d59 657 {
2018US_EminaTahirovi 0:1b3481785d59 658 if (value == '\n') { // new line
2018US_EminaTahirovi 0:1b3481785d59 659 char_x = 0;
2018US_EminaTahirovi 0:1b3481785d59 660 char_y = char_y + font[2];
2018US_EminaTahirovi 0:1b3481785d59 661 if (char_y >= height() - font[2]) {
2018US_EminaTahirovi 0:1b3481785d59 662 char_y = 0;
2018US_EminaTahirovi 0:1b3481785d59 663 }
2018US_EminaTahirovi 0:1b3481785d59 664 } else {
2018US_EminaTahirovi 0:1b3481785d59 665 character(char_x, char_y, value);
2018US_EminaTahirovi 0:1b3481785d59 666 }
2018US_EminaTahirovi 0:1b3481785d59 667 return value;
2018US_EminaTahirovi 0:1b3481785d59 668 }
2018US_EminaTahirovi 0:1b3481785d59 669
2018US_EminaTahirovi 0:1b3481785d59 670
2018US_EminaTahirovi 0:1b3481785d59 671 // speed optimized
2018US_EminaTahirovi 0:1b3481785d59 672 // will use dma
2018US_EminaTahirovi 0:1b3481785d59 673 void SPI_TFT_ILI9341::character(int x, int y, int c)
2018US_EminaTahirovi 0:1b3481785d59 674 {
2018US_EminaTahirovi 0:1b3481785d59 675 unsigned int hor,vert,offset,bpl,j,i,b;
2018US_EminaTahirovi 0:1b3481785d59 676 unsigned char* zeichen;
2018US_EminaTahirovi 0:1b3481785d59 677 unsigned char z,w;
2018US_EminaTahirovi 0:1b3481785d59 678 #ifdef use_ram
2018US_EminaTahirovi 0:1b3481785d59 679 unsigned int pixel;
2018US_EminaTahirovi 0:1b3481785d59 680 unsigned int p;
2018US_EminaTahirovi 0:1b3481785d59 681 unsigned int dma_count,dma_off;
2018US_EminaTahirovi 0:1b3481785d59 682 uint16_t *buffer;
2018US_EminaTahirovi 0:1b3481785d59 683 #endif
2018US_EminaTahirovi 0:1b3481785d59 684
2018US_EminaTahirovi 0:1b3481785d59 685 if ((c < 31) || (c > 127)) return; // test char range
2018US_EminaTahirovi 0:1b3481785d59 686
2018US_EminaTahirovi 0:1b3481785d59 687 // read font parameter from start of array
2018US_EminaTahirovi 0:1b3481785d59 688 offset = font[0]; // bytes / char
2018US_EminaTahirovi 0:1b3481785d59 689 hor = font[1]; // get hor size of font
2018US_EminaTahirovi 0:1b3481785d59 690 vert = font[2]; // get vert size of font
2018US_EminaTahirovi 0:1b3481785d59 691 bpl = font[3]; // bytes per line
2018US_EminaTahirovi 0:1b3481785d59 692
2018US_EminaTahirovi 0:1b3481785d59 693 if (char_x + hor > width()) {
2018US_EminaTahirovi 0:1b3481785d59 694 char_x = 0;
2018US_EminaTahirovi 0:1b3481785d59 695 char_y = char_y + vert;
2018US_EminaTahirovi 0:1b3481785d59 696 if (char_y >= height() - font[2]) {
2018US_EminaTahirovi 0:1b3481785d59 697 char_y = 0;
2018US_EminaTahirovi 0:1b3481785d59 698 }
2018US_EminaTahirovi 0:1b3481785d59 699 }
2018US_EminaTahirovi 0:1b3481785d59 700 window(char_x, char_y,hor,vert); // setup char box
2018US_EminaTahirovi 0:1b3481785d59 701 wr_cmd(0x2C);
2018US_EminaTahirovi 0:1b3481785d59 702 spi_16(1); // switch to 16 bit Mode
2018US_EminaTahirovi 0:1b3481785d59 703 #ifdef use_ram
2018US_EminaTahirovi 0:1b3481785d59 704 pixel = hor * vert; // calculate buffer size
2018US_EminaTahirovi 0:1b3481785d59 705 buffer = (uint16_t *) malloc (2*pixel); // we need a buffer for the font
2018US_EminaTahirovi 0:1b3481785d59 706 if(buffer != NULL) { // there is memory space -> use dma
2018US_EminaTahirovi 0:1b3481785d59 707 zeichen = &font[((c -32) * offset) + 4]; // start of char bitmap
2018US_EminaTahirovi 0:1b3481785d59 708 w = zeichen[0]; // width of actual char
2018US_EminaTahirovi 0:1b3481785d59 709 p = 0;
2018US_EminaTahirovi 0:1b3481785d59 710 // construct the font into the buffer
2018US_EminaTahirovi 0:1b3481785d59 711 for (j=0; j<vert; j++) { // vert line
2018US_EminaTahirovi 0:1b3481785d59 712 for (i=0; i<hor; i++) { // horz line
2018US_EminaTahirovi 0:1b3481785d59 713 z = zeichen[bpl * i + ((j & 0xF8) >> 3)+1];
2018US_EminaTahirovi 0:1b3481785d59 714 b = 1 << (j & 0x07);
2018US_EminaTahirovi 0:1b3481785d59 715 if (( z & b ) == 0x00) {
2018US_EminaTahirovi 0:1b3481785d59 716 buffer[p] = _background;
2018US_EminaTahirovi 0:1b3481785d59 717 } else {
2018US_EminaTahirovi 0:1b3481785d59 718 buffer[p] = _foreground;
2018US_EminaTahirovi 0:1b3481785d59 719 }
2018US_EminaTahirovi 0:1b3481785d59 720 p++;
2018US_EminaTahirovi 0:1b3481785d59 721 }
2018US_EminaTahirovi 0:1b3481785d59 722 }
2018US_EminaTahirovi 0:1b3481785d59 723 // copy the buffer with DMA SPI to display
2018US_EminaTahirovi 0:1b3481785d59 724 dma_off = 0; // offset for DMA transfer
2018US_EminaTahirovi 0:1b3481785d59 725
2018US_EminaTahirovi 0:1b3481785d59 726 switch(spi_num) { // decide which SPI is to use
2018US_EminaTahirovi 0:1b3481785d59 727 case (0):
2018US_EminaTahirovi 0:1b3481785d59 728 LPC_GPDMACH0->DMACCDestAddr = (uint32_t)&LPC_SSP0->DR; // we send to SSP0
2018US_EminaTahirovi 0:1b3481785d59 729 LPC_SSP0->DMACR = 0x2;
2018US_EminaTahirovi 0:1b3481785d59 730 break;
2018US_EminaTahirovi 0:1b3481785d59 731 case (1):
2018US_EminaTahirovi 0:1b3481785d59 732 LPC_GPDMACH0->DMACCDestAddr = (uint32_t)&LPC_SSP1->DR; // we send to SSP1
2018US_EminaTahirovi 0:1b3481785d59 733 LPC_SSP1->DMACR = 0x2;
2018US_EminaTahirovi 0:1b3481785d59 734 break;
2018US_EminaTahirovi 0:1b3481785d59 735 }
2018US_EminaTahirovi 0:1b3481785d59 736 // start DMA
2018US_EminaTahirovi 0:1b3481785d59 737 do {
2018US_EminaTahirovi 0:1b3481785d59 738 if (pixel > 4095) { // this is a giant font !
2018US_EminaTahirovi 0:1b3481785d59 739 dma_count = 4095;
2018US_EminaTahirovi 0:1b3481785d59 740 pixel = pixel - 4095;
2018US_EminaTahirovi 0:1b3481785d59 741 } else {
2018US_EminaTahirovi 0:1b3481785d59 742 dma_count = pixel;
2018US_EminaTahirovi 0:1b3481785d59 743 pixel = 0;
2018US_EminaTahirovi 0:1b3481785d59 744 }
2018US_EminaTahirovi 0:1b3481785d59 745 LPC_GPDMA->DMACIntTCClear = 0x1;
2018US_EminaTahirovi 0:1b3481785d59 746 LPC_GPDMA->DMACIntErrClr = 0x1;
2018US_EminaTahirovi 0:1b3481785d59 747 LPC_GPDMACH0->DMACCSrcAddr = (uint32_t) (buffer + dma_off);
2018US_EminaTahirovi 0:1b3481785d59 748 LPC_GPDMACH0->DMACCControl = dma_count | (1UL << 18) | (1UL << 21) | (1UL << 31) | DMA_CHANNEL_SRC_INC ; // 16 bit transfer , address increment, interrupt
2018US_EminaTahirovi 0:1b3481785d59 749 LPC_GPDMACH0->DMACCConfig = DMA_CHANNEL_ENABLE | DMA_TRANSFER_TYPE_M2P | (spi_num ? DMA_DEST_SSP1_TX : DMA_DEST_SSP0_TX);
2018US_EminaTahirovi 0:1b3481785d59 750 LPC_GPDMA->DMACSoftSReq = 0x1;
2018US_EminaTahirovi 0:1b3481785d59 751 do {
2018US_EminaTahirovi 0:1b3481785d59 752 } while ((LPC_GPDMA->DMACRawIntTCStat & 0x01) == 0); // DMA is running
2018US_EminaTahirovi 0:1b3481785d59 753 dma_off = dma_off + dma_count;
2018US_EminaTahirovi 0:1b3481785d59 754 } while (pixel > 0);
2018US_EminaTahirovi 0:1b3481785d59 755
2018US_EminaTahirovi 0:1b3481785d59 756
2018US_EminaTahirovi 0:1b3481785d59 757 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 758 free ((uint16_t *) buffer);
2018US_EminaTahirovi 0:1b3481785d59 759 spi_16(0);
2018US_EminaTahirovi 0:1b3481785d59 760 }
2018US_EminaTahirovi 0:1b3481785d59 761
2018US_EminaTahirovi 0:1b3481785d59 762 else {
2018US_EminaTahirovi 0:1b3481785d59 763 #endif
2018US_EminaTahirovi 0:1b3481785d59 764 zeichen = &font[((c -32) * offset) + 4]; // start of char bitmap
2018US_EminaTahirovi 0:1b3481785d59 765 w = zeichen[0]; // width of actual char
2018US_EminaTahirovi 0:1b3481785d59 766 for (j=0; j<vert; j++) { // vert line
2018US_EminaTahirovi 0:1b3481785d59 767 for (i=0; i<hor; i++) { // horz line
2018US_EminaTahirovi 0:1b3481785d59 768 z = zeichen[bpl * i + ((j & 0xF8) >> 3)+1];
2018US_EminaTahirovi 0:1b3481785d59 769 b = 1 << (j & 0x07);
2018US_EminaTahirovi 0:1b3481785d59 770 if (( z & b ) == 0x00) {
2018US_EminaTahirovi 0:1b3481785d59 771 f_write(_background);
2018US_EminaTahirovi 0:1b3481785d59 772 } else {
2018US_EminaTahirovi 0:1b3481785d59 773 f_write(_foreground);
2018US_EminaTahirovi 0:1b3481785d59 774 }
2018US_EminaTahirovi 0:1b3481785d59 775 }
2018US_EminaTahirovi 0:1b3481785d59 776 }
2018US_EminaTahirovi 0:1b3481785d59 777 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 778 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 779 spi_16(0);
2018US_EminaTahirovi 0:1b3481785d59 780 #ifdef use_ram
2018US_EminaTahirovi 0:1b3481785d59 781 }
2018US_EminaTahirovi 0:1b3481785d59 782 #endif
2018US_EminaTahirovi 0:1b3481785d59 783 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 784 WindowMax();
2018US_EminaTahirovi 0:1b3481785d59 785 if ((w + 2) < hor) { // x offset to next char
2018US_EminaTahirovi 0:1b3481785d59 786 char_x += w + 2;
2018US_EminaTahirovi 0:1b3481785d59 787 } else char_x += hor;
2018US_EminaTahirovi 0:1b3481785d59 788 }
2018US_EminaTahirovi 0:1b3481785d59 789
2018US_EminaTahirovi 0:1b3481785d59 790
2018US_EminaTahirovi 0:1b3481785d59 791 void SPI_TFT_ILI9341::set_font(unsigned char* f)
2018US_EminaTahirovi 0:1b3481785d59 792 {
2018US_EminaTahirovi 0:1b3481785d59 793 font = f;
2018US_EminaTahirovi 0:1b3481785d59 794 }
2018US_EminaTahirovi 0:1b3481785d59 795
2018US_EminaTahirovi 0:1b3481785d59 796
2018US_EminaTahirovi 0:1b3481785d59 797 void SPI_TFT_ILI9341::Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap)
2018US_EminaTahirovi 0:1b3481785d59 798 {
2018US_EminaTahirovi 0:1b3481785d59 799 unsigned int j;
2018US_EminaTahirovi 0:1b3481785d59 800 int padd;
2018US_EminaTahirovi 0:1b3481785d59 801 unsigned short *bitmap_ptr = (unsigned short *)bitmap;
2018US_EminaTahirovi 0:1b3481785d59 802
2018US_EminaTahirovi 0:1b3481785d59 803 unsigned int i;
2018US_EminaTahirovi 0:1b3481785d59 804
2018US_EminaTahirovi 0:1b3481785d59 805 // the lines are padded to multiple of 4 bytes in a bitmap
2018US_EminaTahirovi 0:1b3481785d59 806 padd = -1;
2018US_EminaTahirovi 0:1b3481785d59 807 do {
2018US_EminaTahirovi 0:1b3481785d59 808 padd ++;
2018US_EminaTahirovi 0:1b3481785d59 809 } while (2*(w + padd)%4 != 0);
2018US_EminaTahirovi 0:1b3481785d59 810 window(x, y, w, h);
2018US_EminaTahirovi 0:1b3481785d59 811 bitmap_ptr += ((h - 1)* (w + padd));
2018US_EminaTahirovi 0:1b3481785d59 812 wr_cmd(0x2C); // send pixel
2018US_EminaTahirovi 0:1b3481785d59 813 spi_16(1);
2018US_EminaTahirovi 0:1b3481785d59 814 for (j = 0; j < h; j++) { //Lines
2018US_EminaTahirovi 0:1b3481785d59 815 for (i = 0; i < w; i++) { // one line
2018US_EminaTahirovi 0:1b3481785d59 816 f_write(*bitmap_ptr); // one line
2018US_EminaTahirovi 0:1b3481785d59 817 bitmap_ptr++;
2018US_EminaTahirovi 0:1b3481785d59 818 }
2018US_EminaTahirovi 0:1b3481785d59 819 bitmap_ptr -= 2*w;
2018US_EminaTahirovi 0:1b3481785d59 820 bitmap_ptr -= padd;
2018US_EminaTahirovi 0:1b3481785d59 821 }
2018US_EminaTahirovi 0:1b3481785d59 822 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 823 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 824 spi_16(0);
2018US_EminaTahirovi 0:1b3481785d59 825 WindowMax();
2018US_EminaTahirovi 0:1b3481785d59 826 }
2018US_EminaTahirovi 0:1b3481785d59 827
2018US_EminaTahirovi 0:1b3481785d59 828
2018US_EminaTahirovi 0:1b3481785d59 829 // local filesystem is not implemented but you can add a SD card to a different SPI
2018US_EminaTahirovi 0:1b3481785d59 830
2018US_EminaTahirovi 0:1b3481785d59 831 int SPI_TFT_ILI9341::BMP_16(unsigned int x, unsigned int y, const char *Name_BMP)
2018US_EminaTahirovi 0:1b3481785d59 832 {
2018US_EminaTahirovi 0:1b3481785d59 833
2018US_EminaTahirovi 0:1b3481785d59 834 #define OffsetPixelWidth 18
2018US_EminaTahirovi 0:1b3481785d59 835 #define OffsetPixelHeigh 22
2018US_EminaTahirovi 0:1b3481785d59 836 #define OffsetFileSize 34
2018US_EminaTahirovi 0:1b3481785d59 837 #define OffsetPixData 10
2018US_EminaTahirovi 0:1b3481785d59 838 #define OffsetBPP 28
2018US_EminaTahirovi 0:1b3481785d59 839
2018US_EminaTahirovi 0:1b3481785d59 840 char filename[50];
2018US_EminaTahirovi 0:1b3481785d59 841 unsigned char BMP_Header[54];
2018US_EminaTahirovi 0:1b3481785d59 842 unsigned short BPP_t;
2018US_EminaTahirovi 0:1b3481785d59 843 unsigned int PixelWidth,PixelHeigh,start_data;
2018US_EminaTahirovi 0:1b3481785d59 844 unsigned int i,off;
2018US_EminaTahirovi 0:1b3481785d59 845 int padd,j;
2018US_EminaTahirovi 0:1b3481785d59 846 unsigned short *line;
2018US_EminaTahirovi 0:1b3481785d59 847
2018US_EminaTahirovi 0:1b3481785d59 848 // get the filename
2018US_EminaTahirovi 0:1b3481785d59 849 i=0;
2018US_EminaTahirovi 0:1b3481785d59 850 while (*Name_BMP!='\0') {
2018US_EminaTahirovi 0:1b3481785d59 851 filename[i++]=*Name_BMP++;
2018US_EminaTahirovi 0:1b3481785d59 852 }
2018US_EminaTahirovi 0:1b3481785d59 853 filename[i] = 0;
2018US_EminaTahirovi 0:1b3481785d59 854
2018US_EminaTahirovi 0:1b3481785d59 855 FILE *Image = fopen((const char *)&filename[0], "rb"); // open the bmp file
2018US_EminaTahirovi 0:1b3481785d59 856 if (!Image) {
2018US_EminaTahirovi 0:1b3481785d59 857 return(0); // error file not found !
2018US_EminaTahirovi 0:1b3481785d59 858 }
2018US_EminaTahirovi 0:1b3481785d59 859
2018US_EminaTahirovi 0:1b3481785d59 860 fread(&BMP_Header[0],1,54,Image); // get the BMP Header
2018US_EminaTahirovi 0:1b3481785d59 861
2018US_EminaTahirovi 0:1b3481785d59 862 if (BMP_Header[0] != 0x42 || BMP_Header[1] != 0x4D) { // check magic byte
2018US_EminaTahirovi 0:1b3481785d59 863 fclose(Image);
2018US_EminaTahirovi 0:1b3481785d59 864 return(-1); // error no BMP file
2018US_EminaTahirovi 0:1b3481785d59 865 }
2018US_EminaTahirovi 0:1b3481785d59 866
2018US_EminaTahirovi 0:1b3481785d59 867 BPP_t = BMP_Header[OffsetBPP] + (BMP_Header[OffsetBPP + 1] << 8);
2018US_EminaTahirovi 0:1b3481785d59 868 if (BPP_t != 0x0010) {
2018US_EminaTahirovi 0:1b3481785d59 869 fclose(Image);
2018US_EminaTahirovi 0:1b3481785d59 870 return(-2); // error no 16 bit BMP
2018US_EminaTahirovi 0:1b3481785d59 871 }
2018US_EminaTahirovi 0:1b3481785d59 872
2018US_EminaTahirovi 0:1b3481785d59 873 PixelHeigh = BMP_Header[OffsetPixelHeigh] + (BMP_Header[OffsetPixelHeigh + 1] << 8) + (BMP_Header[OffsetPixelHeigh + 2] << 16) + (BMP_Header[OffsetPixelHeigh + 3] << 24);
2018US_EminaTahirovi 0:1b3481785d59 874 PixelWidth = BMP_Header[OffsetPixelWidth] + (BMP_Header[OffsetPixelWidth + 1] << 8) + (BMP_Header[OffsetPixelWidth + 2] << 16) + (BMP_Header[OffsetPixelWidth + 3] << 24);
2018US_EminaTahirovi 0:1b3481785d59 875 if (PixelHeigh > height() + y || PixelWidth > width() + x) {
2018US_EminaTahirovi 0:1b3481785d59 876 fclose(Image);
2018US_EminaTahirovi 0:1b3481785d59 877 return(-3); // to big
2018US_EminaTahirovi 0:1b3481785d59 878 }
2018US_EminaTahirovi 0:1b3481785d59 879
2018US_EminaTahirovi 0:1b3481785d59 880 start_data = BMP_Header[OffsetPixData] + (BMP_Header[OffsetPixData + 1] << 8) + (BMP_Header[OffsetPixData + 2] << 16) + (BMP_Header[OffsetPixData + 3] << 24);
2018US_EminaTahirovi 0:1b3481785d59 881
2018US_EminaTahirovi 0:1b3481785d59 882 line = (unsigned short *) malloc (2 * PixelWidth); // we need a buffer for a line
2018US_EminaTahirovi 0:1b3481785d59 883 if (line == NULL) {
2018US_EminaTahirovi 0:1b3481785d59 884 return(-4); // error no memory
2018US_EminaTahirovi 0:1b3481785d59 885 }
2018US_EminaTahirovi 0:1b3481785d59 886
2018US_EminaTahirovi 0:1b3481785d59 887 // the bmp lines are padded to multiple of 4 bytes
2018US_EminaTahirovi 0:1b3481785d59 888 padd = -1;
2018US_EminaTahirovi 0:1b3481785d59 889 do {
2018US_EminaTahirovi 0:1b3481785d59 890 padd ++;
2018US_EminaTahirovi 0:1b3481785d59 891 } while ((PixelWidth * 2 + padd)%4 != 0);
2018US_EminaTahirovi 0:1b3481785d59 892
2018US_EminaTahirovi 0:1b3481785d59 893 window(x, y,PixelWidth ,PixelHeigh);
2018US_EminaTahirovi 0:1b3481785d59 894 wr_cmd(0x2C); // send pixel
2018US_EminaTahirovi 0:1b3481785d59 895 spi_16(1);
2018US_EminaTahirovi 0:1b3481785d59 896 for (j = PixelHeigh - 1; j >= 0; j--) { //Lines bottom up
2018US_EminaTahirovi 0:1b3481785d59 897 off = j * (PixelWidth * 2 + padd) + start_data; // start of line
2018US_EminaTahirovi 0:1b3481785d59 898 fseek(Image, off ,SEEK_SET);
2018US_EminaTahirovi 0:1b3481785d59 899 fread(line,1,PixelWidth * 2,Image); // read a line - slow
2018US_EminaTahirovi 0:1b3481785d59 900 for (i = 0; i < PixelWidth; i++) { // copy pixel data to TFT
2018US_EminaTahirovi 0:1b3481785d59 901 f_write(line[i]); // one 16 bit pixel
2018US_EminaTahirovi 0:1b3481785d59 902 }
2018US_EminaTahirovi 0:1b3481785d59 903 }
2018US_EminaTahirovi 0:1b3481785d59 904 spi_bsy();
2018US_EminaTahirovi 0:1b3481785d59 905 _cs = 1;
2018US_EminaTahirovi 0:1b3481785d59 906 spi_16(0);
2018US_EminaTahirovi 0:1b3481785d59 907 free (line);
2018US_EminaTahirovi 0:1b3481785d59 908 fclose(Image);
2018US_EminaTahirovi 0:1b3481785d59 909 WindowMax();
2018US_EminaTahirovi 0:1b3481785d59 910 return(1);
2018US_EminaTahirovi 0:1b3481785d59 911 }
2018US_EminaTahirovi 0:1b3481785d59 912
2018US_EminaTahirovi 0:1b3481785d59 913 #endif
2018US_EminaTahirovi 0:1b3481785d59 914