test for the SPI_TFT Lib
Dependencies: SPI_TFT TFT_fonts mbed
Fork of TFT by
SPI_TFT/SPI_TFT.cpp@0:f5b62bef3680, 2012-03-13 (annotated)
- Committer:
- dreschpe
- Date:
- Tue Mar 13 19:50:20 2012 +0000
- Revision:
- 0:f5b62bef3680
- Child:
- 1:b295f3228ea2
code with mbed spi
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dreschpe | 0:f5b62bef3680 | 1 | /* mbed library for 240*320 pixel display TFT based on HX8347D LCD Controller |
dreschpe | 0:f5b62bef3680 | 2 | * Copyright (c) 2011 Peter Drescher - DC2PD |
dreschpe | 0:f5b62bef3680 | 3 | * |
dreschpe | 0:f5b62bef3680 | 4 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
dreschpe | 0:f5b62bef3680 | 5 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
dreschpe | 0:f5b62bef3680 | 6 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
dreschpe | 0:f5b62bef3680 | 7 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
dreschpe | 0:f5b62bef3680 | 8 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
dreschpe | 0:f5b62bef3680 | 9 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
dreschpe | 0:f5b62bef3680 | 10 | * THE SOFTWARE. |
dreschpe | 0:f5b62bef3680 | 11 | */ |
dreschpe | 0:f5b62bef3680 | 12 | |
dreschpe | 0:f5b62bef3680 | 13 | |
dreschpe | 0:f5b62bef3680 | 14 | // fix bmp padding for Bitmap function |
dreschpe | 0:f5b62bef3680 | 15 | // speed up pixel |
dreschpe | 0:f5b62bef3680 | 16 | // 30.12.11 fix cls |
dreschpe | 0:f5b62bef3680 | 17 | // 11.03.12 use DMA to speed up |
dreschpe | 0:f5b62bef3680 | 18 | |
dreschpe | 0:f5b62bef3680 | 19 | |
dreschpe | 0:f5b62bef3680 | 20 | #include "SPI_TFT.h" |
dreschpe | 0:f5b62bef3680 | 21 | #include "mbed.h" |
dreschpe | 0:f5b62bef3680 | 22 | |
dreschpe | 0:f5b62bef3680 | 23 | |
dreschpe | 0:f5b62bef3680 | 24 | #define BPP 16 // Bits per pixel |
dreschpe | 0:f5b62bef3680 | 25 | |
dreschpe | 0:f5b62bef3680 | 26 | DigitalOut led(LED2); // debug !! |
dreschpe | 0:f5b62bef3680 | 27 | extern Serial pc; |
dreschpe | 0:f5b62bef3680 | 28 | |
dreschpe | 0:f5b62bef3680 | 29 | SPI_TFT::SPI_TFT(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, const char *name) |
dreschpe | 0:f5b62bef3680 | 30 | : _spi(mosi, miso, sclk), _cs(cs), _reset(reset),GraphicsDisplay(name) { |
dreschpe | 0:f5b62bef3680 | 31 | tft_reset(); |
dreschpe | 0:f5b62bef3680 | 32 | orientation = 0; |
dreschpe | 0:f5b62bef3680 | 33 | char_x = 0; |
dreschpe | 0:f5b62bef3680 | 34 | if (mosi == p11 || mosi == P0_18) spi_port = 0; // we must know the SPI port to setup the DMA |
dreschpe | 0:f5b62bef3680 | 35 | else spi_port = 1; |
dreschpe | 0:f5b62bef3680 | 36 | } |
dreschpe | 0:f5b62bef3680 | 37 | |
dreschpe | 0:f5b62bef3680 | 38 | int SPI_TFT::width() { |
dreschpe | 0:f5b62bef3680 | 39 | if (orientation == 0 || orientation == 2) return 240; |
dreschpe | 0:f5b62bef3680 | 40 | else return 320; |
dreschpe | 0:f5b62bef3680 | 41 | } |
dreschpe | 0:f5b62bef3680 | 42 | |
dreschpe | 0:f5b62bef3680 | 43 | |
dreschpe | 0:f5b62bef3680 | 44 | int SPI_TFT::height() { |
dreschpe | 0:f5b62bef3680 | 45 | if (orientation == 0 || orientation == 2) return 320; |
dreschpe | 0:f5b62bef3680 | 46 | else return 240; |
dreschpe | 0:f5b62bef3680 | 47 | } |
dreschpe | 0:f5b62bef3680 | 48 | |
dreschpe | 0:f5b62bef3680 | 49 | |
dreschpe | 0:f5b62bef3680 | 50 | |
dreschpe | 0:f5b62bef3680 | 51 | void SPI_TFT::set_orientation(unsigned int o) { |
dreschpe | 0:f5b62bef3680 | 52 | orientation = o; |
dreschpe | 0:f5b62bef3680 | 53 | switch (orientation) { |
dreschpe | 0:f5b62bef3680 | 54 | case 0: |
dreschpe | 0:f5b62bef3680 | 55 | wr_reg(0x16, 0x0008); |
dreschpe | 0:f5b62bef3680 | 56 | break; |
dreschpe | 0:f5b62bef3680 | 57 | case 1: |
dreschpe | 0:f5b62bef3680 | 58 | wr_reg(0x16, 0x0068); |
dreschpe | 0:f5b62bef3680 | 59 | break; |
dreschpe | 0:f5b62bef3680 | 60 | case 2: |
dreschpe | 0:f5b62bef3680 | 61 | wr_reg(0x16, 0x00C8); |
dreschpe | 0:f5b62bef3680 | 62 | break; |
dreschpe | 0:f5b62bef3680 | 63 | case 3: |
dreschpe | 0:f5b62bef3680 | 64 | wr_reg(0x16, 0x00A8); |
dreschpe | 0:f5b62bef3680 | 65 | break; |
dreschpe | 0:f5b62bef3680 | 66 | } |
dreschpe | 0:f5b62bef3680 | 67 | WindowMax(); |
dreschpe | 0:f5b62bef3680 | 68 | } |
dreschpe | 0:f5b62bef3680 | 69 | |
dreschpe | 0:f5b62bef3680 | 70 | |
dreschpe | 0:f5b62bef3680 | 71 | |
dreschpe | 0:f5b62bef3680 | 72 | void SPI_TFT::wr_cmd(int cmd) { |
dreschpe | 0:f5b62bef3680 | 73 | _cs = 0; |
dreschpe | 0:f5b62bef3680 | 74 | _spi.write(SPI_START | SPI_WR | SPI_INDEX); /* Write : RS = 0, RW = 0 */ |
dreschpe | 0:f5b62bef3680 | 75 | _spi.write(cmd); |
dreschpe | 0:f5b62bef3680 | 76 | _cs = 1; |
dreschpe | 0:f5b62bef3680 | 77 | } |
dreschpe | 0:f5b62bef3680 | 78 | |
dreschpe | 0:f5b62bef3680 | 79 | |
dreschpe | 0:f5b62bef3680 | 80 | |
dreschpe | 0:f5b62bef3680 | 81 | void SPI_TFT::wr_dat(char dat) { |
dreschpe | 0:f5b62bef3680 | 82 | _cs = 0; |
dreschpe | 0:f5b62bef3680 | 83 | _spi.write(SPI_START | SPI_WR | SPI_DATA); // Write : RS = 1, RW = 0 |
dreschpe | 0:f5b62bef3680 | 84 | //_spi.format(16,3); // switch to 16 bit Mode 3 |
dreschpe | 0:f5b62bef3680 | 85 | _spi.write(dat & 0xff); // Write D0..D15 |
dreschpe | 0:f5b62bef3680 | 86 | //_spi.format(8,3); // 8 bit Mode 3 |
dreschpe | 0:f5b62bef3680 | 87 | _cs = 1; |
dreschpe | 0:f5b62bef3680 | 88 | } |
dreschpe | 0:f5b62bef3680 | 89 | |
dreschpe | 0:f5b62bef3680 | 90 | void SPI_TFT::wr_dat(int dat) { |
dreschpe | 0:f5b62bef3680 | 91 | _cs = 0; |
dreschpe | 0:f5b62bef3680 | 92 | _spi.write(SPI_START | SPI_WR | SPI_DATA); // Write : RS = 1, RW = 0 |
dreschpe | 0:f5b62bef3680 | 93 | _spi.format(16,3); // switch to 16 bit Mode 3 |
dreschpe | 0:f5b62bef3680 | 94 | _spi.write(dat); // Write D0..D15 |
dreschpe | 0:f5b62bef3680 | 95 | _spi.format(8,3); // 8 bit Mode 3 |
dreschpe | 0:f5b62bef3680 | 96 | _cs = 1; |
dreschpe | 0:f5b62bef3680 | 97 | } |
dreschpe | 0:f5b62bef3680 | 98 | |
dreschpe | 0:f5b62bef3680 | 99 | |
dreschpe | 0:f5b62bef3680 | 100 | void SPI_TFT::wr_dat_start(void) { |
dreschpe | 0:f5b62bef3680 | 101 | _cs = 0; |
dreschpe | 0:f5b62bef3680 | 102 | _spi.write(SPI_START | SPI_WR | SPI_DATA); /* Write : RS = 1, RW = 0 */ |
dreschpe | 0:f5b62bef3680 | 103 | } |
dreschpe | 0:f5b62bef3680 | 104 | |
dreschpe | 0:f5b62bef3680 | 105 | |
dreschpe | 0:f5b62bef3680 | 106 | |
dreschpe | 0:f5b62bef3680 | 107 | void SPI_TFT::wr_dat_stop (void) { |
dreschpe | 0:f5b62bef3680 | 108 | _cs = 1; |
dreschpe | 0:f5b62bef3680 | 109 | } |
dreschpe | 0:f5b62bef3680 | 110 | |
dreschpe | 0:f5b62bef3680 | 111 | |
dreschpe | 0:f5b62bef3680 | 112 | |
dreschpe | 0:f5b62bef3680 | 113 | void SPI_TFT::wr_dat_only (unsigned short dat) { |
dreschpe | 0:f5b62bef3680 | 114 | |
dreschpe | 0:f5b62bef3680 | 115 | _spi.format(16,3); // switch to 16 bit Mode 3 |
dreschpe | 0:f5b62bef3680 | 116 | _spi.write(dat); // Write D0..D15 |
dreschpe | 0:f5b62bef3680 | 117 | _spi.format(8,3); // 8 bit Mode 3 |
dreschpe | 0:f5b62bef3680 | 118 | } |
dreschpe | 0:f5b62bef3680 | 119 | |
dreschpe | 0:f5b62bef3680 | 120 | |
dreschpe | 0:f5b62bef3680 | 121 | |
dreschpe | 0:f5b62bef3680 | 122 | unsigned short SPI_TFT::rd_dat (void) { |
dreschpe | 0:f5b62bef3680 | 123 | unsigned short val = 0; |
dreschpe | 0:f5b62bef3680 | 124 | |
dreschpe | 0:f5b62bef3680 | 125 | _cs = 0; |
dreschpe | 0:f5b62bef3680 | 126 | _spi.write(SPI_START | SPI_RD | SPI_DATA); /* Read: RS = 1, RW = 1 */ |
dreschpe | 0:f5b62bef3680 | 127 | _spi.write(0); /* Dummy read 1 */ |
dreschpe | 0:f5b62bef3680 | 128 | val = _spi.write(0); /* Read D8..D15 */ |
dreschpe | 0:f5b62bef3680 | 129 | val <<= 8; |
dreschpe | 0:f5b62bef3680 | 130 | val |= _spi.write(0); /* Read D0..D7 */ |
dreschpe | 0:f5b62bef3680 | 131 | _cs = 1; |
dreschpe | 0:f5b62bef3680 | 132 | return (val); |
dreschpe | 0:f5b62bef3680 | 133 | } |
dreschpe | 0:f5b62bef3680 | 134 | |
dreschpe | 0:f5b62bef3680 | 135 | |
dreschpe | 0:f5b62bef3680 | 136 | |
dreschpe | 0:f5b62bef3680 | 137 | void SPI_TFT::wr_reg (unsigned char reg, unsigned char val) { |
dreschpe | 0:f5b62bef3680 | 138 | |
dreschpe | 0:f5b62bef3680 | 139 | wr_cmd(reg); |
dreschpe | 0:f5b62bef3680 | 140 | wr_dat(val); |
dreschpe | 0:f5b62bef3680 | 141 | } |
dreschpe | 0:f5b62bef3680 | 142 | |
dreschpe | 0:f5b62bef3680 | 143 | |
dreschpe | 0:f5b62bef3680 | 144 | |
dreschpe | 0:f5b62bef3680 | 145 | unsigned short SPI_TFT::rd_reg (unsigned char reg) { |
dreschpe | 0:f5b62bef3680 | 146 | |
dreschpe | 0:f5b62bef3680 | 147 | wr_cmd(reg); |
dreschpe | 0:f5b62bef3680 | 148 | return(rd_dat()); |
dreschpe | 0:f5b62bef3680 | 149 | } |
dreschpe | 0:f5b62bef3680 | 150 | |
dreschpe | 0:f5b62bef3680 | 151 | |
dreschpe | 0:f5b62bef3680 | 152 | |
dreschpe | 0:f5b62bef3680 | 153 | void SPI_TFT::tft_reset() { |
dreschpe | 0:f5b62bef3680 | 154 | static unsigned short driverCode; |
dreschpe | 0:f5b62bef3680 | 155 | _spi.format(8,3); // 8 bit spi mode 3 |
dreschpe | 0:f5b62bef3680 | 156 | _spi.frequency(48000000); // 48Mhz SPI clock |
dreschpe | 0:f5b62bef3680 | 157 | _reset = 0; // reset |
dreschpe | 0:f5b62bef3680 | 158 | _cs = 1; |
dreschpe | 0:f5b62bef3680 | 159 | wait_us(50); |
dreschpe | 0:f5b62bef3680 | 160 | _reset = 1; // end reset |
dreschpe | 0:f5b62bef3680 | 161 | wait_ms(5); |
dreschpe | 0:f5b62bef3680 | 162 | |
dreschpe | 0:f5b62bef3680 | 163 | driverCode = rd_reg(0x00); // read controller ID |
dreschpe | 0:f5b62bef3680 | 164 | //printf("Disp_ID = %x",driverCode); |
dreschpe | 0:f5b62bef3680 | 165 | |
dreschpe | 0:f5b62bef3680 | 166 | /* Start Initial Sequence ----------------------------------------------------*/ |
dreschpe | 0:f5b62bef3680 | 167 | wr_reg(0xEA, 0x0000); /* Reset Power Control 1 */ |
dreschpe | 0:f5b62bef3680 | 168 | wr_reg(0xEB, 0x0020); /* Power Control 2 */ |
dreschpe | 0:f5b62bef3680 | 169 | wr_reg(0xEC, 0x000C); /* Power Control 3 */ |
dreschpe | 0:f5b62bef3680 | 170 | wr_reg(0xED, 0x00C4); /* Power Control 4 */ |
dreschpe | 0:f5b62bef3680 | 171 | wr_reg(0xE8, 0x0040); /* Source OPON_N */ |
dreschpe | 0:f5b62bef3680 | 172 | wr_reg(0xE9, 0x0038); /* Source OPON_I */ |
dreschpe | 0:f5b62bef3680 | 173 | wr_reg(0xF1, 0x0001); /* */ |
dreschpe | 0:f5b62bef3680 | 174 | wr_reg(0xF2, 0x0010); /* */ |
dreschpe | 0:f5b62bef3680 | 175 | wr_reg(0x27, 0x00A3); /* Display Control 2 */ |
dreschpe | 0:f5b62bef3680 | 176 | |
dreschpe | 0:f5b62bef3680 | 177 | /* Power On sequence ---------------------------------------------------------*/ |
dreschpe | 0:f5b62bef3680 | 178 | wr_reg(0x1B, 0x001B); /* Power Control 2 */ |
dreschpe | 0:f5b62bef3680 | 179 | wr_reg(0x1A, 0x0001); /* Power Control 1 */ |
dreschpe | 0:f5b62bef3680 | 180 | wr_reg(0x24, 0x002F); /* Vcom Control 2 */ |
dreschpe | 0:f5b62bef3680 | 181 | wr_reg(0x25, 0x0057); /* Vcom Control 3 */ |
dreschpe | 0:f5b62bef3680 | 182 | wr_reg(0x23, 0x008D); /* Vcom Control 1 */ |
dreschpe | 0:f5b62bef3680 | 183 | |
dreschpe | 0:f5b62bef3680 | 184 | /* Gamma settings -----------------------------------------------------------*/ |
dreschpe | 0:f5b62bef3680 | 185 | wr_reg(0x40,0x00); // |
dreschpe | 0:f5b62bef3680 | 186 | wr_reg(0x41,0x00); // |
dreschpe | 0:f5b62bef3680 | 187 | wr_reg(0x42,0x01); // |
dreschpe | 0:f5b62bef3680 | 188 | wr_reg(0x43,0x13); // |
dreschpe | 0:f5b62bef3680 | 189 | wr_reg(0x44,0x10); // |
dreschpe | 0:f5b62bef3680 | 190 | wr_reg(0x45,0x26); // |
dreschpe | 0:f5b62bef3680 | 191 | wr_reg(0x46,0x08); // |
dreschpe | 0:f5b62bef3680 | 192 | wr_reg(0x47,0x51); // |
dreschpe | 0:f5b62bef3680 | 193 | wr_reg(0x48,0x02); // |
dreschpe | 0:f5b62bef3680 | 194 | wr_reg(0x49,0x12); // |
dreschpe | 0:f5b62bef3680 | 195 | wr_reg(0x4A,0x18); // |
dreschpe | 0:f5b62bef3680 | 196 | wr_reg(0x4B,0x19); // |
dreschpe | 0:f5b62bef3680 | 197 | wr_reg(0x4C,0x14); // |
dreschpe | 0:f5b62bef3680 | 198 | wr_reg(0x50,0x19); // |
dreschpe | 0:f5b62bef3680 | 199 | wr_reg(0x51,0x2F); // |
dreschpe | 0:f5b62bef3680 | 200 | wr_reg(0x52,0x2C); // |
dreschpe | 0:f5b62bef3680 | 201 | wr_reg(0x53,0x3E); // |
dreschpe | 0:f5b62bef3680 | 202 | wr_reg(0x54,0x3F); // |
dreschpe | 0:f5b62bef3680 | 203 | wr_reg(0x55,0x3F); // |
dreschpe | 0:f5b62bef3680 | 204 | wr_reg(0x56,0x2E); // |
dreschpe | 0:f5b62bef3680 | 205 | wr_reg(0x57,0x77); // |
dreschpe | 0:f5b62bef3680 | 206 | wr_reg(0x58,0x0B); // |
dreschpe | 0:f5b62bef3680 | 207 | wr_reg(0x59,0x06); // |
dreschpe | 0:f5b62bef3680 | 208 | wr_reg(0x5A,0x07); // |
dreschpe | 0:f5b62bef3680 | 209 | wr_reg(0x5B,0x0D); // |
dreschpe | 0:f5b62bef3680 | 210 | wr_reg(0x5C,0x1D); // |
dreschpe | 0:f5b62bef3680 | 211 | wr_reg(0x5D,0xCC); // |
dreschpe | 0:f5b62bef3680 | 212 | |
dreschpe | 0:f5b62bef3680 | 213 | /* Power + Osc ---------------------------------------------------------------*/ |
dreschpe | 0:f5b62bef3680 | 214 | wr_reg(0x18, 0x0036); /* OSC Control 1 */ |
dreschpe | 0:f5b62bef3680 | 215 | wr_reg(0x19, 0x0001); /* OSC Control 2 */ |
dreschpe | 0:f5b62bef3680 | 216 | wr_reg(0x01, 0x0000); /* Display Mode Control */ |
dreschpe | 0:f5b62bef3680 | 217 | wr_reg(0x1F, 0x0088); /* Power Control 6 */ |
dreschpe | 0:f5b62bef3680 | 218 | wait_ms(5); /* Delay 5 ms */ |
dreschpe | 0:f5b62bef3680 | 219 | wr_reg(0x1F, 0x0080); /* Power Control 6 */ |
dreschpe | 0:f5b62bef3680 | 220 | wait_ms(5); /* Delay 5 ms */ |
dreschpe | 0:f5b62bef3680 | 221 | wr_reg(0x1F, 0x0090); /* Power Control 6 */ |
dreschpe | 0:f5b62bef3680 | 222 | wait_ms(5); /* Delay 5 ms */ |
dreschpe | 0:f5b62bef3680 | 223 | wr_reg(0x1F, 0x00D0); /* Power Control 6 */ |
dreschpe | 0:f5b62bef3680 | 224 | wait_ms(5); /* Delay 5 ms */ |
dreschpe | 0:f5b62bef3680 | 225 | |
dreschpe | 0:f5b62bef3680 | 226 | wr_reg(0x17, 0x0005); /* Colmod 16Bit/Pixel */ |
dreschpe | 0:f5b62bef3680 | 227 | |
dreschpe | 0:f5b62bef3680 | 228 | wr_reg(0x36, 0x0000); /* Panel Characteristic */ |
dreschpe | 0:f5b62bef3680 | 229 | wr_reg(0x28, 0x0038); /* Display Control 3 */ |
dreschpe | 0:f5b62bef3680 | 230 | wait_ms(40); |
dreschpe | 0:f5b62bef3680 | 231 | wr_reg(0x28, 0x003C); /* Display Control 3 */ |
dreschpe | 0:f5b62bef3680 | 232 | switch (orientation) { |
dreschpe | 0:f5b62bef3680 | 233 | case 0: |
dreschpe | 0:f5b62bef3680 | 234 | wr_reg(0x16, 0x0008); |
dreschpe | 0:f5b62bef3680 | 235 | break; |
dreschpe | 0:f5b62bef3680 | 236 | case 1: |
dreschpe | 0:f5b62bef3680 | 237 | wr_reg(0x16, 0x0068); |
dreschpe | 0:f5b62bef3680 | 238 | break; |
dreschpe | 0:f5b62bef3680 | 239 | case 2: |
dreschpe | 0:f5b62bef3680 | 240 | wr_reg(0x16, 0x00C8); |
dreschpe | 0:f5b62bef3680 | 241 | break; |
dreschpe | 0:f5b62bef3680 | 242 | case 3: |
dreschpe | 0:f5b62bef3680 | 243 | wr_reg(0x16, 0x00A8); |
dreschpe | 0:f5b62bef3680 | 244 | break; |
dreschpe | 0:f5b62bef3680 | 245 | } |
dreschpe | 0:f5b62bef3680 | 246 | |
dreschpe | 0:f5b62bef3680 | 247 | // setup DMA channel 0 |
dreschpe | 0:f5b62bef3680 | 248 | |
dreschpe | 0:f5b62bef3680 | 249 | /* Power up the GPDMA. */ |
dreschpe | 0:f5b62bef3680 | 250 | LPC_SC->PCONP |= (1UL << 29); |
dreschpe | 0:f5b62bef3680 | 251 | LPC_GPDMA->DMACConfig = 1; // enable DMA controller |
dreschpe | 0:f5b62bef3680 | 252 | // Reset the Interrupt status |
dreschpe | 0:f5b62bef3680 | 253 | LPC_GPDMA->DMACIntTCClear = 0x1; |
dreschpe | 0:f5b62bef3680 | 254 | LPC_GPDMA->DMACIntErrClr = 0x1; |
dreschpe | 0:f5b62bef3680 | 255 | LPC_GPDMACH0->DMACCLLI = 0; |
dreschpe | 0:f5b62bef3680 | 256 | if (spi_port == 0) { // TFT on SSP0 |
dreschpe | 0:f5b62bef3680 | 257 | LPC_GPDMACH0->DMACCDestAddr = (uint32_t)&LPC_SSP0->DR; // we send to SSP0 |
dreschpe | 0:f5b62bef3680 | 258 | /* Enable SSP0 for DMA. */ |
dreschpe | 0:f5b62bef3680 | 259 | LPC_SSP0->DMACR = 0x2; |
dreschpe | 0:f5b62bef3680 | 260 | } else { |
dreschpe | 0:f5b62bef3680 | 261 | LPC_GPDMACH0->DMACCDestAddr = (uint32_t)&LPC_SSP1->DR; // we send to SSP1 |
dreschpe | 0:f5b62bef3680 | 262 | /* Enable SSP1 for DMA. */ |
dreschpe | 0:f5b62bef3680 | 263 | LPC_SSP1->DMACR = 0x2; |
dreschpe | 0:f5b62bef3680 | 264 | } |
dreschpe | 0:f5b62bef3680 | 265 | |
dreschpe | 0:f5b62bef3680 | 266 | WindowMax (); |
dreschpe | 0:f5b62bef3680 | 267 | } |
dreschpe | 0:f5b62bef3680 | 268 | |
dreschpe | 0:f5b62bef3680 | 269 | |
dreschpe | 0:f5b62bef3680 | 270 | |
dreschpe | 0:f5b62bef3680 | 271 | |
dreschpe | 0:f5b62bef3680 | 272 | void SPI_TFT::pixel(int x, int y, int color) { |
dreschpe | 0:f5b62bef3680 | 273 | wr_reg(0x03, (x >> 0)); |
dreschpe | 0:f5b62bef3680 | 274 | wr_reg(0x02, (x >> 8)); |
dreschpe | 0:f5b62bef3680 | 275 | wr_reg(0x07, (y >> 0)); |
dreschpe | 0:f5b62bef3680 | 276 | wr_reg(0x06, (y >> 8)); |
dreschpe | 0:f5b62bef3680 | 277 | //wr_reg(0x05, (x+1 >> 0)); |
dreschpe | 0:f5b62bef3680 | 278 | //wr_reg(0x04, (x+1 >> 8)); |
dreschpe | 0:f5b62bef3680 | 279 | //wr_reg(0x09, (y+1 >> 0)); |
dreschpe | 0:f5b62bef3680 | 280 | //wr_reg(0x08, (y+1 >> 8)); |
dreschpe | 0:f5b62bef3680 | 281 | wr_cmd(0x22); |
dreschpe | 0:f5b62bef3680 | 282 | wr_dat(color); |
dreschpe | 0:f5b62bef3680 | 283 | } |
dreschpe | 0:f5b62bef3680 | 284 | |
dreschpe | 0:f5b62bef3680 | 285 | |
dreschpe | 0:f5b62bef3680 | 286 | |
dreschpe | 0:f5b62bef3680 | 287 | |
dreschpe | 0:f5b62bef3680 | 288 | void SPI_TFT::window (unsigned int x, unsigned int y, unsigned int w, unsigned int h) { |
dreschpe | 0:f5b62bef3680 | 289 | wr_reg(0x03, (x >> 0)); |
dreschpe | 0:f5b62bef3680 | 290 | wr_reg(0x02, (x >> 8)); |
dreschpe | 0:f5b62bef3680 | 291 | wr_reg(0x05, (x+w-1 >> 0)); |
dreschpe | 0:f5b62bef3680 | 292 | wr_reg(0x04, (x+w-1 >> 8)); |
dreschpe | 0:f5b62bef3680 | 293 | wr_reg(0x07, ( y >> 0)); |
dreschpe | 0:f5b62bef3680 | 294 | wr_reg(0x06, ( y >> 8)); |
dreschpe | 0:f5b62bef3680 | 295 | wr_reg(0x09, ( y+h-1 >> 0)); |
dreschpe | 0:f5b62bef3680 | 296 | wr_reg(0x08, ( y+h-1 >> 8)); |
dreschpe | 0:f5b62bef3680 | 297 | //wr_cmd(0x22); |
dreschpe | 0:f5b62bef3680 | 298 | } |
dreschpe | 0:f5b62bef3680 | 299 | |
dreschpe | 0:f5b62bef3680 | 300 | |
dreschpe | 0:f5b62bef3680 | 301 | void SPI_TFT::WindowMax (void) { |
dreschpe | 0:f5b62bef3680 | 302 | window (0, 0, width(), height()); |
dreschpe | 0:f5b62bef3680 | 303 | } |
dreschpe | 0:f5b62bef3680 | 304 | |
dreschpe | 0:f5b62bef3680 | 305 | |
dreschpe | 0:f5b62bef3680 | 306 | void SPI_TFT::cls (void) { |
dreschpe | 0:f5b62bef3680 | 307 | //unsigned int i; |
dreschpe | 0:f5b62bef3680 | 308 | int pixel = ( width() * height()); |
dreschpe | 0:f5b62bef3680 | 309 | int dma_count; |
dreschpe | 0:f5b62bef3680 | 310 | int color = _background; |
dreschpe | 0:f5b62bef3680 | 311 | WindowMax(); |
dreschpe | 0:f5b62bef3680 | 312 | wr_cmd(0x22); |
dreschpe | 0:f5b62bef3680 | 313 | wr_dat_start(); |
dreschpe | 0:f5b62bef3680 | 314 | _spi.format(16,3); // 16 bit Mode 3 |
dreschpe | 0:f5b62bef3680 | 315 | do { |
dreschpe | 0:f5b62bef3680 | 316 | if (pixel > 4095) { |
dreschpe | 0:f5b62bef3680 | 317 | dma_count = 4095; |
dreschpe | 0:f5b62bef3680 | 318 | pixel = pixel - 4095; |
dreschpe | 0:f5b62bef3680 | 319 | } else { |
dreschpe | 0:f5b62bef3680 | 320 | dma_count = pixel; |
dreschpe | 0:f5b62bef3680 | 321 | pixel = 0; |
dreschpe | 0:f5b62bef3680 | 322 | } |
dreschpe | 0:f5b62bef3680 | 323 | LPC_GPDMA->DMACIntTCClear = 0x1; |
dreschpe | 0:f5b62bef3680 | 324 | LPC_GPDMA->DMACIntErrClr = 0x1; |
dreschpe | 0:f5b62bef3680 | 325 | LPC_GPDMACH0->DMACCSrcAddr = (uint32_t)&color; |
dreschpe | 0:f5b62bef3680 | 326 | LPC_GPDMACH0->DMACCControl = dma_count | (1UL << 18) | (1UL << 21) | (1UL << 31) ; // 16 bit transfer , no address increment, interrupt |
dreschpe | 0:f5b62bef3680 | 327 | LPC_GPDMACH0->DMACCConfig = DMA_CHANNEL_ENABLE | DMA_TRANSFER_TYPE_M2P ; |
dreschpe | 0:f5b62bef3680 | 328 | LPC_GPDMA->DMACSoftSReq = 0x1; |
dreschpe | 0:f5b62bef3680 | 329 | do { |
dreschpe | 0:f5b62bef3680 | 330 | } while ((LPC_GPDMA->DMACRawIntTCStat & 0x01) == 0); // DMA is running |
dreschpe | 0:f5b62bef3680 | 331 | |
dreschpe | 0:f5b62bef3680 | 332 | } while (pixel > 0); |
dreschpe | 0:f5b62bef3680 | 333 | if (spi_port == 0) { // TFT on SSP0 |
dreschpe | 0:f5b62bef3680 | 334 | do { |
dreschpe | 0:f5b62bef3680 | 335 | } while ((LPC_SSP0->SR & 0x01) == 0); // SPI FIFO not empty |
dreschpe | 0:f5b62bef3680 | 336 | } else { |
dreschpe | 0:f5b62bef3680 | 337 | do { |
dreschpe | 0:f5b62bef3680 | 338 | } while ((LPC_SSP1->SR & 0x01) == 0); // SPI FIFO not empty |
dreschpe | 0:f5b62bef3680 | 339 | } |
dreschpe | 0:f5b62bef3680 | 340 | |
dreschpe | 0:f5b62bef3680 | 341 | // for (i = 0; i < ( width() * height()); i++) |
dreschpe | 0:f5b62bef3680 | 342 | // _spi.write(_background); |
dreschpe | 0:f5b62bef3680 | 343 | |
dreschpe | 0:f5b62bef3680 | 344 | _spi.format(8,3); // 8 bit Mode 3 |
dreschpe | 0:f5b62bef3680 | 345 | wr_dat_stop(); |
dreschpe | 0:f5b62bef3680 | 346 | } |
dreschpe | 0:f5b62bef3680 | 347 | |
dreschpe | 0:f5b62bef3680 | 348 | |
dreschpe | 0:f5b62bef3680 | 349 | void SPI_TFT::circle(int x0, int y0, int r, int color) { |
dreschpe | 0:f5b62bef3680 | 350 | |
dreschpe | 0:f5b62bef3680 | 351 | int draw_x0, draw_y0; |
dreschpe | 0:f5b62bef3680 | 352 | int draw_x1, draw_y1; |
dreschpe | 0:f5b62bef3680 | 353 | int draw_x2, draw_y2; |
dreschpe | 0:f5b62bef3680 | 354 | int draw_x3, draw_y3; |
dreschpe | 0:f5b62bef3680 | 355 | int draw_x4, draw_y4; |
dreschpe | 0:f5b62bef3680 | 356 | int draw_x5, draw_y5; |
dreschpe | 0:f5b62bef3680 | 357 | int draw_x6, draw_y6; |
dreschpe | 0:f5b62bef3680 | 358 | int draw_x7, draw_y7; |
dreschpe | 0:f5b62bef3680 | 359 | int xx, yy; |
dreschpe | 0:f5b62bef3680 | 360 | int di; |
dreschpe | 0:f5b62bef3680 | 361 | //WindowMax(); |
dreschpe | 0:f5b62bef3680 | 362 | if (r == 0) { /* no radius */ |
dreschpe | 0:f5b62bef3680 | 363 | return; |
dreschpe | 0:f5b62bef3680 | 364 | } |
dreschpe | 0:f5b62bef3680 | 365 | |
dreschpe | 0:f5b62bef3680 | 366 | draw_x0 = draw_x1 = x0; |
dreschpe | 0:f5b62bef3680 | 367 | draw_y0 = draw_y1 = y0 + r; |
dreschpe | 0:f5b62bef3680 | 368 | if (draw_y0 < height()) { |
dreschpe | 0:f5b62bef3680 | 369 | pixel(draw_x0, draw_y0, color); /* 90 degree */ |
dreschpe | 0:f5b62bef3680 | 370 | } |
dreschpe | 0:f5b62bef3680 | 371 | |
dreschpe | 0:f5b62bef3680 | 372 | draw_x2 = draw_x3 = x0; |
dreschpe | 0:f5b62bef3680 | 373 | draw_y2 = draw_y3 = y0 - r; |
dreschpe | 0:f5b62bef3680 | 374 | if (draw_y2 >= 0) { |
dreschpe | 0:f5b62bef3680 | 375 | pixel(draw_x2, draw_y2, color); /* 270 degree */ |
dreschpe | 0:f5b62bef3680 | 376 | } |
dreschpe | 0:f5b62bef3680 | 377 | |
dreschpe | 0:f5b62bef3680 | 378 | draw_x4 = draw_x6 = x0 + r; |
dreschpe | 0:f5b62bef3680 | 379 | draw_y4 = draw_y6 = y0; |
dreschpe | 0:f5b62bef3680 | 380 | if (draw_x4 < width()) { |
dreschpe | 0:f5b62bef3680 | 381 | pixel(draw_x4, draw_y4, color); /* 0 degree */ |
dreschpe | 0:f5b62bef3680 | 382 | } |
dreschpe | 0:f5b62bef3680 | 383 | |
dreschpe | 0:f5b62bef3680 | 384 | draw_x5 = draw_x7 = x0 - r; |
dreschpe | 0:f5b62bef3680 | 385 | draw_y5 = draw_y7 = y0; |
dreschpe | 0:f5b62bef3680 | 386 | if (draw_x5>=0) { |
dreschpe | 0:f5b62bef3680 | 387 | pixel(draw_x5, draw_y5, color); /* 180 degree */ |
dreschpe | 0:f5b62bef3680 | 388 | } |
dreschpe | 0:f5b62bef3680 | 389 | |
dreschpe | 0:f5b62bef3680 | 390 | if (r == 1) { |
dreschpe | 0:f5b62bef3680 | 391 | return; |
dreschpe | 0:f5b62bef3680 | 392 | } |
dreschpe | 0:f5b62bef3680 | 393 | |
dreschpe | 0:f5b62bef3680 | 394 | di = 3 - 2*r; |
dreschpe | 0:f5b62bef3680 | 395 | xx = 0; |
dreschpe | 0:f5b62bef3680 | 396 | yy = r; |
dreschpe | 0:f5b62bef3680 | 397 | while (xx < yy) { |
dreschpe | 0:f5b62bef3680 | 398 | |
dreschpe | 0:f5b62bef3680 | 399 | if (di < 0) { |
dreschpe | 0:f5b62bef3680 | 400 | di += 4*xx + 6; |
dreschpe | 0:f5b62bef3680 | 401 | } else { |
dreschpe | 0:f5b62bef3680 | 402 | di += 4*(xx - yy) + 10; |
dreschpe | 0:f5b62bef3680 | 403 | yy--; |
dreschpe | 0:f5b62bef3680 | 404 | draw_y0--; |
dreschpe | 0:f5b62bef3680 | 405 | draw_y1--; |
dreschpe | 0:f5b62bef3680 | 406 | draw_y2++; |
dreschpe | 0:f5b62bef3680 | 407 | draw_y3++; |
dreschpe | 0:f5b62bef3680 | 408 | draw_x4--; |
dreschpe | 0:f5b62bef3680 | 409 | draw_x5++; |
dreschpe | 0:f5b62bef3680 | 410 | draw_x6--; |
dreschpe | 0:f5b62bef3680 | 411 | draw_x7++; |
dreschpe | 0:f5b62bef3680 | 412 | } |
dreschpe | 0:f5b62bef3680 | 413 | xx++; |
dreschpe | 0:f5b62bef3680 | 414 | draw_x0++; |
dreschpe | 0:f5b62bef3680 | 415 | draw_x1--; |
dreschpe | 0:f5b62bef3680 | 416 | draw_x2++; |
dreschpe | 0:f5b62bef3680 | 417 | draw_x3--; |
dreschpe | 0:f5b62bef3680 | 418 | draw_y4++; |
dreschpe | 0:f5b62bef3680 | 419 | draw_y5++; |
dreschpe | 0:f5b62bef3680 | 420 | draw_y6--; |
dreschpe | 0:f5b62bef3680 | 421 | draw_y7--; |
dreschpe | 0:f5b62bef3680 | 422 | |
dreschpe | 0:f5b62bef3680 | 423 | if ( (draw_x0 <= width()) && (draw_y0>=0) ) { |
dreschpe | 0:f5b62bef3680 | 424 | pixel(draw_x0, draw_y0, color); |
dreschpe | 0:f5b62bef3680 | 425 | } |
dreschpe | 0:f5b62bef3680 | 426 | |
dreschpe | 0:f5b62bef3680 | 427 | if ( (draw_x1 >= 0) && (draw_y1 >= 0) ) { |
dreschpe | 0:f5b62bef3680 | 428 | pixel(draw_x1, draw_y1, color); |
dreschpe | 0:f5b62bef3680 | 429 | } |
dreschpe | 0:f5b62bef3680 | 430 | |
dreschpe | 0:f5b62bef3680 | 431 | if ( (draw_x2 <= width()) && (draw_y2 <= height()) ) { |
dreschpe | 0:f5b62bef3680 | 432 | pixel(draw_x2, draw_y2, color); |
dreschpe | 0:f5b62bef3680 | 433 | } |
dreschpe | 0:f5b62bef3680 | 434 | |
dreschpe | 0:f5b62bef3680 | 435 | if ( (draw_x3 >=0 ) && (draw_y3 <= height()) ) { |
dreschpe | 0:f5b62bef3680 | 436 | pixel(draw_x3, draw_y3, color); |
dreschpe | 0:f5b62bef3680 | 437 | } |
dreschpe | 0:f5b62bef3680 | 438 | |
dreschpe | 0:f5b62bef3680 | 439 | if ( (draw_x4 <= width()) && (draw_y4 >= 0) ) { |
dreschpe | 0:f5b62bef3680 | 440 | pixel(draw_x4, draw_y4, color); |
dreschpe | 0:f5b62bef3680 | 441 | } |
dreschpe | 0:f5b62bef3680 | 442 | |
dreschpe | 0:f5b62bef3680 | 443 | if ( (draw_x5 >= 0) && (draw_y5 >= 0) ) { |
dreschpe | 0:f5b62bef3680 | 444 | pixel(draw_x5, draw_y5, color); |
dreschpe | 0:f5b62bef3680 | 445 | } |
dreschpe | 0:f5b62bef3680 | 446 | if ( (draw_x6 <=width()) && (draw_y6 <= height()) ) { |
dreschpe | 0:f5b62bef3680 | 447 | pixel(draw_x6, draw_y6, color); |
dreschpe | 0:f5b62bef3680 | 448 | } |
dreschpe | 0:f5b62bef3680 | 449 | if ( (draw_x7 >= 0) && (draw_y7 <= height()) ) { |
dreschpe | 0:f5b62bef3680 | 450 | pixel(draw_x7, draw_y7, color); |
dreschpe | 0:f5b62bef3680 | 451 | } |
dreschpe | 0:f5b62bef3680 | 452 | } |
dreschpe | 0:f5b62bef3680 | 453 | return; |
dreschpe | 0:f5b62bef3680 | 454 | } |
dreschpe | 0:f5b62bef3680 | 455 | |
dreschpe | 0:f5b62bef3680 | 456 | void SPI_TFT::fillcircle(int x, int y, int r, int color) { |
dreschpe | 0:f5b62bef3680 | 457 | int i; |
dreschpe | 0:f5b62bef3680 | 458 | for (i = 0; i <= r; i++) |
dreschpe | 0:f5b62bef3680 | 459 | circle(x,y,i,color); |
dreschpe | 0:f5b62bef3680 | 460 | } |
dreschpe | 0:f5b62bef3680 | 461 | |
dreschpe | 0:f5b62bef3680 | 462 | |
dreschpe | 0:f5b62bef3680 | 463 | |
dreschpe | 0:f5b62bef3680 | 464 | void SPI_TFT::hline(int x0, int x1, int y, int color) { |
dreschpe | 0:f5b62bef3680 | 465 | int w; |
dreschpe | 0:f5b62bef3680 | 466 | w = x1 - x0 + 1; |
dreschpe | 0:f5b62bef3680 | 467 | window(x0,y,w,1); |
dreschpe | 0:f5b62bef3680 | 468 | wr_cmd(0x22); |
dreschpe | 0:f5b62bef3680 | 469 | wr_dat_start(); |
dreschpe | 0:f5b62bef3680 | 470 | _spi.format(16,3); // pixel are send in 16 bit mode to speed up |
dreschpe | 0:f5b62bef3680 | 471 | LPC_GPDMA->DMACIntTCClear = 0x1; |
dreschpe | 0:f5b62bef3680 | 472 | LPC_GPDMA->DMACIntErrClr = 0x1; |
dreschpe | 0:f5b62bef3680 | 473 | LPC_GPDMACH0->DMACCSrcAddr = (uint32_t)&color; |
dreschpe | 0:f5b62bef3680 | 474 | LPC_GPDMACH0->DMACCControl = w | (1UL << 18) | (1UL << 21) | (1UL << 31) ; // 16 bit transfer , no address increment, interrupt |
dreschpe | 0:f5b62bef3680 | 475 | LPC_GPDMACH0->DMACCConfig = DMA_CHANNEL_ENABLE | DMA_TRANSFER_TYPE_M2P ; |
dreschpe | 0:f5b62bef3680 | 476 | LPC_GPDMA->DMACSoftSReq = 0x1; // start DMA |
dreschpe | 0:f5b62bef3680 | 477 | do { |
dreschpe | 0:f5b62bef3680 | 478 | } while ((LPC_GPDMA->DMACRawIntTCStat & 0x01) == 0); // DMA is running |
dreschpe | 0:f5b62bef3680 | 479 | //for (int x=0; x<w; x++) { |
dreschpe | 0:f5b62bef3680 | 480 | // _spi.write(color); |
dreschpe | 0:f5b62bef3680 | 481 | //} |
dreschpe | 0:f5b62bef3680 | 482 | if (spi_port == 0) { // TFT on SSP0 |
dreschpe | 0:f5b62bef3680 | 483 | do { |
dreschpe | 0:f5b62bef3680 | 484 | } while ((LPC_SSP0->SR & 0x01) == 0); // SPI FIFO not empty |
dreschpe | 0:f5b62bef3680 | 485 | } else { |
dreschpe | 0:f5b62bef3680 | 486 | do { |
dreschpe | 0:f5b62bef3680 | 487 | } while ((LPC_SSP1->SR & 0x01) == 0); // SPI FIFO not empty |
dreschpe | 0:f5b62bef3680 | 488 | } |
dreschpe | 0:f5b62bef3680 | 489 | |
dreschpe | 0:f5b62bef3680 | 490 | _spi.format(8,3); |
dreschpe | 0:f5b62bef3680 | 491 | wr_dat_stop(); |
dreschpe | 0:f5b62bef3680 | 492 | WindowMax(); |
dreschpe | 0:f5b62bef3680 | 493 | return; |
dreschpe | 0:f5b62bef3680 | 494 | } |
dreschpe | 0:f5b62bef3680 | 495 | |
dreschpe | 0:f5b62bef3680 | 496 | |
dreschpe | 0:f5b62bef3680 | 497 | |
dreschpe | 0:f5b62bef3680 | 498 | void SPI_TFT::vline(int x, int y0, int y1, int color) { |
dreschpe | 0:f5b62bef3680 | 499 | int h; |
dreschpe | 0:f5b62bef3680 | 500 | h = y1 - y0 + 1; |
dreschpe | 0:f5b62bef3680 | 501 | window(x,y0,1,h); |
dreschpe | 0:f5b62bef3680 | 502 | wr_cmd(0x22); |
dreschpe | 0:f5b62bef3680 | 503 | wr_dat_start(); |
dreschpe | 0:f5b62bef3680 | 504 | _spi.format(16,3); // pixel are send in 16 bit mode to speed up |
dreschpe | 0:f5b62bef3680 | 505 | LPC_GPDMA->DMACIntTCClear = 0x1; |
dreschpe | 0:f5b62bef3680 | 506 | LPC_GPDMA->DMACIntErrClr = 0x1; |
dreschpe | 0:f5b62bef3680 | 507 | LPC_GPDMACH0->DMACCSrcAddr = (uint32_t)&color; |
dreschpe | 0:f5b62bef3680 | 508 | LPC_GPDMACH0->DMACCControl = h | (1UL << 18) | (1UL << 21) | (1UL << 31) ; // 16 bit transfer , no address increment, interrupt |
dreschpe | 0:f5b62bef3680 | 509 | LPC_GPDMACH0->DMACCConfig = DMA_CHANNEL_ENABLE | DMA_TRANSFER_TYPE_M2P ; |
dreschpe | 0:f5b62bef3680 | 510 | LPC_GPDMA->DMACSoftSReq = 0x1; |
dreschpe | 0:f5b62bef3680 | 511 | do { |
dreschpe | 0:f5b62bef3680 | 512 | } while ((LPC_GPDMA->DMACRawIntTCStat & 0x01) == 0); // DMA is running |
dreschpe | 0:f5b62bef3680 | 513 | |
dreschpe | 0:f5b62bef3680 | 514 | if (spi_port == 0) { // TFT on SSP0 |
dreschpe | 0:f5b62bef3680 | 515 | do { |
dreschpe | 0:f5b62bef3680 | 516 | } while ((LPC_SSP0->SR & 0x01) == 0); // SPI FIFO not empty |
dreschpe | 0:f5b62bef3680 | 517 | } else { |
dreschpe | 0:f5b62bef3680 | 518 | do { |
dreschpe | 0:f5b62bef3680 | 519 | } while ((LPC_SSP1->SR & 0x01) == 0); // SPI FIFO not empty |
dreschpe | 0:f5b62bef3680 | 520 | } |
dreschpe | 0:f5b62bef3680 | 521 | |
dreschpe | 0:f5b62bef3680 | 522 | //for (int y=0; y<h; y++) { |
dreschpe | 0:f5b62bef3680 | 523 | // _spi.write(color); |
dreschpe | 0:f5b62bef3680 | 524 | //} |
dreschpe | 0:f5b62bef3680 | 525 | _spi.format(8,3); |
dreschpe | 0:f5b62bef3680 | 526 | wr_dat_stop(); |
dreschpe | 0:f5b62bef3680 | 527 | WindowMax(); |
dreschpe | 0:f5b62bef3680 | 528 | return; |
dreschpe | 0:f5b62bef3680 | 529 | } |
dreschpe | 0:f5b62bef3680 | 530 | |
dreschpe | 0:f5b62bef3680 | 531 | |
dreschpe | 0:f5b62bef3680 | 532 | |
dreschpe | 0:f5b62bef3680 | 533 | void SPI_TFT::line(int x0, int y0, int x1, int y1, int color) { |
dreschpe | 0:f5b62bef3680 | 534 | //WindowMax(); |
dreschpe | 0:f5b62bef3680 | 535 | int dx = 0, dy = 0; |
dreschpe | 0:f5b62bef3680 | 536 | int dx_sym = 0, dy_sym = 0; |
dreschpe | 0:f5b62bef3680 | 537 | int dx_x2 = 0, dy_x2 = 0; |
dreschpe | 0:f5b62bef3680 | 538 | int di = 0; |
dreschpe | 0:f5b62bef3680 | 539 | |
dreschpe | 0:f5b62bef3680 | 540 | dx = x1-x0; |
dreschpe | 0:f5b62bef3680 | 541 | dy = y1-y0; |
dreschpe | 0:f5b62bef3680 | 542 | |
dreschpe | 0:f5b62bef3680 | 543 | if (dx == 0) { /* vertical line */ |
dreschpe | 0:f5b62bef3680 | 544 | if (y1 > y0) vline(x0,y0,y1,color); |
dreschpe | 0:f5b62bef3680 | 545 | else vline(x0,y1,y0,color); |
dreschpe | 0:f5b62bef3680 | 546 | return; |
dreschpe | 0:f5b62bef3680 | 547 | } |
dreschpe | 0:f5b62bef3680 | 548 | |
dreschpe | 0:f5b62bef3680 | 549 | if (dx > 0) { |
dreschpe | 0:f5b62bef3680 | 550 | dx_sym = 1; |
dreschpe | 0:f5b62bef3680 | 551 | } else { |
dreschpe | 0:f5b62bef3680 | 552 | dx_sym = -1; |
dreschpe | 0:f5b62bef3680 | 553 | } |
dreschpe | 0:f5b62bef3680 | 554 | if (dy == 0) { /* horizontal line */ |
dreschpe | 0:f5b62bef3680 | 555 | if (x1 > x0) hline(x0,x1,y0,color); |
dreschpe | 0:f5b62bef3680 | 556 | else hline(x1,x0,y0,color); |
dreschpe | 0:f5b62bef3680 | 557 | return; |
dreschpe | 0:f5b62bef3680 | 558 | } |
dreschpe | 0:f5b62bef3680 | 559 | |
dreschpe | 0:f5b62bef3680 | 560 | if (dy > 0) { |
dreschpe | 0:f5b62bef3680 | 561 | dy_sym = 1; |
dreschpe | 0:f5b62bef3680 | 562 | } else { |
dreschpe | 0:f5b62bef3680 | 563 | dy_sym = -1; |
dreschpe | 0:f5b62bef3680 | 564 | } |
dreschpe | 0:f5b62bef3680 | 565 | |
dreschpe | 0:f5b62bef3680 | 566 | dx = dx_sym*dx; |
dreschpe | 0:f5b62bef3680 | 567 | dy = dy_sym*dy; |
dreschpe | 0:f5b62bef3680 | 568 | |
dreschpe | 0:f5b62bef3680 | 569 | dx_x2 = dx*2; |
dreschpe | 0:f5b62bef3680 | 570 | dy_x2 = dy*2; |
dreschpe | 0:f5b62bef3680 | 571 | |
dreschpe | 0:f5b62bef3680 | 572 | if (dx >= dy) { |
dreschpe | 0:f5b62bef3680 | 573 | di = dy_x2 - dx; |
dreschpe | 0:f5b62bef3680 | 574 | while (x0 != x1) { |
dreschpe | 0:f5b62bef3680 | 575 | |
dreschpe | 0:f5b62bef3680 | 576 | pixel(x0, y0, color); |
dreschpe | 0:f5b62bef3680 | 577 | x0 += dx_sym; |
dreschpe | 0:f5b62bef3680 | 578 | if (di<0) { |
dreschpe | 0:f5b62bef3680 | 579 | di += dy_x2; |
dreschpe | 0:f5b62bef3680 | 580 | } else { |
dreschpe | 0:f5b62bef3680 | 581 | di += dy_x2 - dx_x2; |
dreschpe | 0:f5b62bef3680 | 582 | y0 += dy_sym; |
dreschpe | 0:f5b62bef3680 | 583 | } |
dreschpe | 0:f5b62bef3680 | 584 | } |
dreschpe | 0:f5b62bef3680 | 585 | pixel(x0, y0, color); |
dreschpe | 0:f5b62bef3680 | 586 | } else { |
dreschpe | 0:f5b62bef3680 | 587 | di = dx_x2 - dy; |
dreschpe | 0:f5b62bef3680 | 588 | while (y0 != y1) { |
dreschpe | 0:f5b62bef3680 | 589 | pixel(x0, y0, color); |
dreschpe | 0:f5b62bef3680 | 590 | y0 += dy_sym; |
dreschpe | 0:f5b62bef3680 | 591 | if (di < 0) { |
dreschpe | 0:f5b62bef3680 | 592 | di += dx_x2; |
dreschpe | 0:f5b62bef3680 | 593 | } else { |
dreschpe | 0:f5b62bef3680 | 594 | di += dx_x2 - dy_x2; |
dreschpe | 0:f5b62bef3680 | 595 | x0 += dx_sym; |
dreschpe | 0:f5b62bef3680 | 596 | } |
dreschpe | 0:f5b62bef3680 | 597 | } |
dreschpe | 0:f5b62bef3680 | 598 | pixel(x0, y0, color); |
dreschpe | 0:f5b62bef3680 | 599 | } |
dreschpe | 0:f5b62bef3680 | 600 | return; |
dreschpe | 0:f5b62bef3680 | 601 | } |
dreschpe | 0:f5b62bef3680 | 602 | |
dreschpe | 0:f5b62bef3680 | 603 | |
dreschpe | 0:f5b62bef3680 | 604 | |
dreschpe | 0:f5b62bef3680 | 605 | |
dreschpe | 0:f5b62bef3680 | 606 | void SPI_TFT::rect(int x0, int y0, int x1, int y1, int color) { |
dreschpe | 0:f5b62bef3680 | 607 | |
dreschpe | 0:f5b62bef3680 | 608 | if (x1 > x0) hline(x0,x1,y0,color); |
dreschpe | 0:f5b62bef3680 | 609 | else hline(x1,x0,y0,color); |
dreschpe | 0:f5b62bef3680 | 610 | |
dreschpe | 0:f5b62bef3680 | 611 | if (y1 > y0) vline(x0,y0,y1,color); |
dreschpe | 0:f5b62bef3680 | 612 | else vline(x0,y1,y0,color); |
dreschpe | 0:f5b62bef3680 | 613 | |
dreschpe | 0:f5b62bef3680 | 614 | if (x1 > x0) hline(x0,x1,y1,color); |
dreschpe | 0:f5b62bef3680 | 615 | else hline(x1,x0,y1,color); |
dreschpe | 0:f5b62bef3680 | 616 | |
dreschpe | 0:f5b62bef3680 | 617 | if (y1 > y0) vline(x1,y0,y1,color); |
dreschpe | 0:f5b62bef3680 | 618 | else vline(x1,y1,y0,color); |
dreschpe | 0:f5b62bef3680 | 619 | |
dreschpe | 0:f5b62bef3680 | 620 | return; |
dreschpe | 0:f5b62bef3680 | 621 | } |
dreschpe | 0:f5b62bef3680 | 622 | |
dreschpe | 0:f5b62bef3680 | 623 | |
dreschpe | 0:f5b62bef3680 | 624 | |
dreschpe | 0:f5b62bef3680 | 625 | void SPI_TFT::fillrect(int x0, int y0, int x1, int y1, int color) { |
dreschpe | 0:f5b62bef3680 | 626 | |
dreschpe | 0:f5b62bef3680 | 627 | int h = y1 - y0 + 1; |
dreschpe | 0:f5b62bef3680 | 628 | int w = x1 - x0 + 1; |
dreschpe | 0:f5b62bef3680 | 629 | int pixel = h * w; |
dreschpe | 0:f5b62bef3680 | 630 | int dma_count; |
dreschpe | 0:f5b62bef3680 | 631 | window(x0,y0,w,h); |
dreschpe | 0:f5b62bef3680 | 632 | wr_cmd(0x22); |
dreschpe | 0:f5b62bef3680 | 633 | wr_dat_start(); |
dreschpe | 0:f5b62bef3680 | 634 | _spi.format(16,3); // pixel are send in 16 bit mode to speed up |
dreschpe | 0:f5b62bef3680 | 635 | |
dreschpe | 0:f5b62bef3680 | 636 | do { |
dreschpe | 0:f5b62bef3680 | 637 | if (pixel > 4095) { |
dreschpe | 0:f5b62bef3680 | 638 | dma_count = 4095; |
dreschpe | 0:f5b62bef3680 | 639 | pixel = pixel - 4095; |
dreschpe | 0:f5b62bef3680 | 640 | } else { |
dreschpe | 0:f5b62bef3680 | 641 | dma_count = pixel; |
dreschpe | 0:f5b62bef3680 | 642 | pixel = 0; |
dreschpe | 0:f5b62bef3680 | 643 | } |
dreschpe | 0:f5b62bef3680 | 644 | LPC_GPDMA->DMACIntTCClear = 0x1; |
dreschpe | 0:f5b62bef3680 | 645 | LPC_GPDMA->DMACIntErrClr = 0x1; |
dreschpe | 0:f5b62bef3680 | 646 | LPC_GPDMACH0->DMACCSrcAddr = (uint32_t)&color; |
dreschpe | 0:f5b62bef3680 | 647 | LPC_GPDMACH0->DMACCControl = dma_count | (1UL << 18) | (1UL << 21) | (1UL << 31) ; // 16 bit transfer , no address increment, interrupt |
dreschpe | 0:f5b62bef3680 | 648 | LPC_GPDMACH0->DMACCConfig = DMA_CHANNEL_ENABLE | DMA_TRANSFER_TYPE_M2P ; |
dreschpe | 0:f5b62bef3680 | 649 | LPC_GPDMA->DMACSoftSReq = 0x1; |
dreschpe | 0:f5b62bef3680 | 650 | do { |
dreschpe | 0:f5b62bef3680 | 651 | } while ((LPC_GPDMA->DMACRawIntTCStat & 0x01) == 0); // DMA is running |
dreschpe | 0:f5b62bef3680 | 652 | |
dreschpe | 0:f5b62bef3680 | 653 | } while (pixel > 0); |
dreschpe | 0:f5b62bef3680 | 654 | |
dreschpe | 0:f5b62bef3680 | 655 | if (spi_port == 0) { // TFT on SSP0 |
dreschpe | 0:f5b62bef3680 | 656 | do { |
dreschpe | 0:f5b62bef3680 | 657 | } while ((LPC_SSP0->SR & 0x01) == 0); // SPI FIFO not empty |
dreschpe | 0:f5b62bef3680 | 658 | } else { |
dreschpe | 0:f5b62bef3680 | 659 | do { |
dreschpe | 0:f5b62bef3680 | 660 | } while ((LPC_SSP1->SR & 0x01) == 0); // SPI FIFO not empty |
dreschpe | 0:f5b62bef3680 | 661 | } |
dreschpe | 0:f5b62bef3680 | 662 | |
dreschpe | 0:f5b62bef3680 | 663 | //for (int p=0; p<pixel; p++) { |
dreschpe | 0:f5b62bef3680 | 664 | // _spi.write(color); |
dreschpe | 0:f5b62bef3680 | 665 | // } |
dreschpe | 0:f5b62bef3680 | 666 | _spi.format(8,3); |
dreschpe | 0:f5b62bef3680 | 667 | wr_dat_stop(); |
dreschpe | 0:f5b62bef3680 | 668 | WindowMax(); |
dreschpe | 0:f5b62bef3680 | 669 | return; |
dreschpe | 0:f5b62bef3680 | 670 | } |
dreschpe | 0:f5b62bef3680 | 671 | |
dreschpe | 0:f5b62bef3680 | 672 | |
dreschpe | 0:f5b62bef3680 | 673 | |
dreschpe | 0:f5b62bef3680 | 674 | void SPI_TFT::locate(int x, int y) { |
dreschpe | 0:f5b62bef3680 | 675 | char_x = x; |
dreschpe | 0:f5b62bef3680 | 676 | char_y = y; |
dreschpe | 0:f5b62bef3680 | 677 | } |
dreschpe | 0:f5b62bef3680 | 678 | |
dreschpe | 0:f5b62bef3680 | 679 | |
dreschpe | 0:f5b62bef3680 | 680 | |
dreschpe | 0:f5b62bef3680 | 681 | int SPI_TFT::columns() { |
dreschpe | 0:f5b62bef3680 | 682 | return width() / font[1]; |
dreschpe | 0:f5b62bef3680 | 683 | } |
dreschpe | 0:f5b62bef3680 | 684 | |
dreschpe | 0:f5b62bef3680 | 685 | |
dreschpe | 0:f5b62bef3680 | 686 | |
dreschpe | 0:f5b62bef3680 | 687 | int SPI_TFT::rows() { |
dreschpe | 0:f5b62bef3680 | 688 | return height() / font[2]; |
dreschpe | 0:f5b62bef3680 | 689 | } |
dreschpe | 0:f5b62bef3680 | 690 | |
dreschpe | 0:f5b62bef3680 | 691 | |
dreschpe | 0:f5b62bef3680 | 692 | |
dreschpe | 0:f5b62bef3680 | 693 | int SPI_TFT::_putc(int value) { |
dreschpe | 0:f5b62bef3680 | 694 | if (value == '\n') { // new line |
dreschpe | 0:f5b62bef3680 | 695 | char_x = 0; |
dreschpe | 0:f5b62bef3680 | 696 | char_y = char_y + font[2]; |
dreschpe | 0:f5b62bef3680 | 697 | if (char_y >= height() - font[2]) { |
dreschpe | 0:f5b62bef3680 | 698 | char_y = 0; |
dreschpe | 0:f5b62bef3680 | 699 | } |
dreschpe | 0:f5b62bef3680 | 700 | } else { |
dreschpe | 0:f5b62bef3680 | 701 | character(char_x, char_y, value); |
dreschpe | 0:f5b62bef3680 | 702 | } |
dreschpe | 0:f5b62bef3680 | 703 | return value; |
dreschpe | 0:f5b62bef3680 | 704 | } |
dreschpe | 0:f5b62bef3680 | 705 | |
dreschpe | 0:f5b62bef3680 | 706 | |
dreschpe | 0:f5b62bef3680 | 707 | |
dreschpe | 0:f5b62bef3680 | 708 | |
dreschpe | 0:f5b62bef3680 | 709 | void SPI_TFT::character(int x, int y, int c) { |
dreschpe | 0:f5b62bef3680 | 710 | unsigned int hor,vert,offset,bpl,j,i,b,p; |
dreschpe | 0:f5b62bef3680 | 711 | unsigned char* zeichen; |
dreschpe | 0:f5b62bef3680 | 712 | unsigned char z,w; |
dreschpe | 0:f5b62bef3680 | 713 | unsigned int pixel; |
dreschpe | 0:f5b62bef3680 | 714 | unsigned int dma_count,dma_off; |
dreschpe | 0:f5b62bef3680 | 715 | uint16_t *buffer; |
dreschpe | 0:f5b62bef3680 | 716 | // pc.printf("character(%i,%i,%c)\n\r",x,y,c); |
dreschpe | 0:f5b62bef3680 | 717 | // led = 0; |
dreschpe | 0:f5b62bef3680 | 718 | |
dreschpe | 0:f5b62bef3680 | 719 | if ((c < 31) || (c > 127)) return; // test char range |
dreschpe | 0:f5b62bef3680 | 720 | |
dreschpe | 0:f5b62bef3680 | 721 | // read font parameter from start of array |
dreschpe | 0:f5b62bef3680 | 722 | offset = font[0]; // bytes / char |
dreschpe | 0:f5b62bef3680 | 723 | hor = font[1]; // get hor size of font |
dreschpe | 0:f5b62bef3680 | 724 | vert = font[2]; // get vert size of font |
dreschpe | 0:f5b62bef3680 | 725 | bpl = font[3]; // bytes per line |
dreschpe | 0:f5b62bef3680 | 726 | |
dreschpe | 0:f5b62bef3680 | 727 | if (char_x + hor > width()) { |
dreschpe | 0:f5b62bef3680 | 728 | char_x = 0; |
dreschpe | 0:f5b62bef3680 | 729 | char_y = char_y + vert; |
dreschpe | 0:f5b62bef3680 | 730 | if (char_y >= height() - font[2]) { |
dreschpe | 0:f5b62bef3680 | 731 | char_y = 0; |
dreschpe | 0:f5b62bef3680 | 732 | } |
dreschpe | 0:f5b62bef3680 | 733 | } |
dreschpe | 0:f5b62bef3680 | 734 | |
dreschpe | 0:f5b62bef3680 | 735 | pixel = hor * vert; // calculate buffer size |
dreschpe | 0:f5b62bef3680 | 736 | // pc.printf("malloc(%i)\n\r",pixel); |
dreschpe | 0:f5b62bef3680 | 737 | buffer = (uint16_t *) malloc (pixel); // we need a buffer for the character |
dreschpe | 0:f5b62bef3680 | 738 | if (buffer == NULL) { |
dreschpe | 0:f5b62bef3680 | 739 | led = 1; |
dreschpe | 0:f5b62bef3680 | 740 | pc.printf("Malloc error !\n\r"); |
dreschpe | 0:f5b62bef3680 | 741 | return; // error no memory |
dreschpe | 0:f5b62bef3680 | 742 | } |
dreschpe | 0:f5b62bef3680 | 743 | // pc.printf("buffer %x\n\r",buffer); |
dreschpe | 0:f5b62bef3680 | 744 | window(char_x, char_y,hor,vert); // char box |
dreschpe | 0:f5b62bef3680 | 745 | wr_cmd(0x22); |
dreschpe | 0:f5b62bef3680 | 746 | wr_dat_start(); |
dreschpe | 0:f5b62bef3680 | 747 | zeichen = &font[((c -32) * offset) + 4]; // start of char bitmap |
dreschpe | 0:f5b62bef3680 | 748 | w = zeichen[0]; // width of actual char |
dreschpe | 0:f5b62bef3680 | 749 | _spi.format(16,3); // pixel are 16 bit |
dreschpe | 0:f5b62bef3680 | 750 | p = 0; |
dreschpe | 0:f5b62bef3680 | 751 | //pc.printf("write buffer\n\r"); |
dreschpe | 0:f5b62bef3680 | 752 | // construct the char into the buffer |
dreschpe | 0:f5b62bef3680 | 753 | for (j=0; j<vert; j++) { // vert line |
dreschpe | 0:f5b62bef3680 | 754 | for (i=0; i<hor; i++) { // horz line |
dreschpe | 0:f5b62bef3680 | 755 | z = zeichen[bpl * i + ((j & 0xF8) >> 3)+1]; |
dreschpe | 0:f5b62bef3680 | 756 | b = 1 << (j & 0x07); |
dreschpe | 0:f5b62bef3680 | 757 | if (( z & b ) == 0x00) { |
dreschpe | 0:f5b62bef3680 | 758 | buffer[p] = _background; |
dreschpe | 0:f5b62bef3680 | 759 | //_spi.write(_background); |
dreschpe | 0:f5b62bef3680 | 760 | } else { |
dreschpe | 0:f5b62bef3680 | 761 | buffer[p] = _foreground; |
dreschpe | 0:f5b62bef3680 | 762 | //_spi.write(_foreground); |
dreschpe | 0:f5b62bef3680 | 763 | } |
dreschpe | 0:f5b62bef3680 | 764 | p++; |
dreschpe | 0:f5b62bef3680 | 765 | } |
dreschpe | 0:f5b62bef3680 | 766 | } |
dreschpe | 0:f5b62bef3680 | 767 | dma_off = 0; |
dreschpe | 0:f5b62bef3680 | 768 | // copy the buffer with DMA to SPI |
dreschpe | 0:f5b62bef3680 | 769 | // pc.printf("start DMA\n\r"); |
dreschpe | 0:f5b62bef3680 | 770 | do { |
dreschpe | 0:f5b62bef3680 | 771 | if (pixel > 4095) { // this is a giant font ! |
dreschpe | 0:f5b62bef3680 | 772 | dma_count = 4095; |
dreschpe | 0:f5b62bef3680 | 773 | pixel = pixel - 4095; |
dreschpe | 0:f5b62bef3680 | 774 | } else { |
dreschpe | 0:f5b62bef3680 | 775 | dma_count = pixel; |
dreschpe | 0:f5b62bef3680 | 776 | pixel = 0; |
dreschpe | 0:f5b62bef3680 | 777 | } |
dreschpe | 0:f5b62bef3680 | 778 | LPC_GPDMA->DMACIntTCClear = 0x1; |
dreschpe | 0:f5b62bef3680 | 779 | LPC_GPDMA->DMACIntErrClr = 0x1; |
dreschpe | 0:f5b62bef3680 | 780 | LPC_GPDMACH0->DMACCSrcAddr = (uint32_t) (buffer + dma_off); |
dreschpe | 0:f5b62bef3680 | 781 | LPC_GPDMACH0->DMACCControl = dma_count | (1UL << 18) | (1UL << 21) | (1UL << 31) | DMA_CHANNEL_SRC_INC ; // 16 bit transfer , address increment, interrupt |
dreschpe | 0:f5b62bef3680 | 782 | LPC_GPDMACH0->DMACCConfig = DMA_CHANNEL_ENABLE | DMA_TRANSFER_TYPE_M2P ; |
dreschpe | 0:f5b62bef3680 | 783 | LPC_GPDMA->DMACSoftSReq = 0x1; |
dreschpe | 0:f5b62bef3680 | 784 | do { |
dreschpe | 0:f5b62bef3680 | 785 | } while ((LPC_GPDMA->DMACRawIntTCStat & 0x01) == 0); // DMA is running |
dreschpe | 0:f5b62bef3680 | 786 | dma_off = dma_off + dma_count; |
dreschpe | 0:f5b62bef3680 | 787 | } while (pixel > 0); |
dreschpe | 0:f5b62bef3680 | 788 | // pc.printf("free %x\n\r",buffer); |
dreschpe | 0:f5b62bef3680 | 789 | free ((uint16_t *) buffer); |
dreschpe | 0:f5b62bef3680 | 790 | |
dreschpe | 0:f5b62bef3680 | 791 | if(spi_port == 0){ // TFT on SSP0 |
dreschpe | 0:f5b62bef3680 | 792 | do { |
dreschpe | 0:f5b62bef3680 | 793 | } while ((LPC_SSP0->SR & 0x01) == 0); // SPI FIFO not empty |
dreschpe | 0:f5b62bef3680 | 794 | } |
dreschpe | 0:f5b62bef3680 | 795 | else{ |
dreschpe | 0:f5b62bef3680 | 796 | do { |
dreschpe | 0:f5b62bef3680 | 797 | } while ((LPC_SSP1->SR & 0x01) == 0); // SPI FIFO not empty |
dreschpe | 0:f5b62bef3680 | 798 | } |
dreschpe | 0:f5b62bef3680 | 799 | |
dreschpe | 0:f5b62bef3680 | 800 | _spi.format(8,3); // 8 bit |
dreschpe | 0:f5b62bef3680 | 801 | wr_dat_stop(); |
dreschpe | 0:f5b62bef3680 | 802 | WindowMax(); |
dreschpe | 0:f5b62bef3680 | 803 | if ((w + 2) < hor) { // x offset to next char |
dreschpe | 0:f5b62bef3680 | 804 | char_x += w + 2; |
dreschpe | 0:f5b62bef3680 | 805 | } else char_x += hor; |
dreschpe | 0:f5b62bef3680 | 806 | |
dreschpe | 0:f5b62bef3680 | 807 | } |
dreschpe | 0:f5b62bef3680 | 808 | |
dreschpe | 0:f5b62bef3680 | 809 | |
dreschpe | 0:f5b62bef3680 | 810 | |
dreschpe | 0:f5b62bef3680 | 811 | |
dreschpe | 0:f5b62bef3680 | 812 | |
dreschpe | 0:f5b62bef3680 | 813 | void SPI_TFT::set_font(unsigned char* f) { |
dreschpe | 0:f5b62bef3680 | 814 | font = f; |
dreschpe | 0:f5b62bef3680 | 815 | } |
dreschpe | 0:f5b62bef3680 | 816 | |
dreschpe | 0:f5b62bef3680 | 817 | |
dreschpe | 0:f5b62bef3680 | 818 | |
dreschpe | 0:f5b62bef3680 | 819 | void SPI_TFT::Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap) { |
dreschpe | 0:f5b62bef3680 | 820 | unsigned int j; |
dreschpe | 0:f5b62bef3680 | 821 | int padd; |
dreschpe | 0:f5b62bef3680 | 822 | unsigned short *bitmap_ptr = (unsigned short *)bitmap; |
dreschpe | 0:f5b62bef3680 | 823 | // the lines are padded to multiple of 4 bytes in a bitmap |
dreschpe | 0:f5b62bef3680 | 824 | padd = -1; |
dreschpe | 0:f5b62bef3680 | 825 | do { |
dreschpe | 0:f5b62bef3680 | 826 | padd ++; |
dreschpe | 0:f5b62bef3680 | 827 | } while (2*(w + padd)%4 != 0); |
dreschpe | 0:f5b62bef3680 | 828 | window(x, y, w, h); |
dreschpe | 0:f5b62bef3680 | 829 | wr_cmd(0x22); |
dreschpe | 0:f5b62bef3680 | 830 | wr_dat_start(); |
dreschpe | 0:f5b62bef3680 | 831 | _spi.format(16,3); |
dreschpe | 0:f5b62bef3680 | 832 | bitmap_ptr += ((h - 1)* (w + padd)); |
dreschpe | 0:f5b62bef3680 | 833 | //bitmap_ptr -= padd; |
dreschpe | 0:f5b62bef3680 | 834 | for (j = 0; j < h; j++) { //Lines |
dreschpe | 0:f5b62bef3680 | 835 | LPC_GPDMA->DMACIntTCClear = 0x1; |
dreschpe | 0:f5b62bef3680 | 836 | LPC_GPDMA->DMACIntErrClr = 0x1; |
dreschpe | 0:f5b62bef3680 | 837 | LPC_GPDMACH0->DMACCSrcAddr = (uint32_t)bitmap_ptr; |
dreschpe | 0:f5b62bef3680 | 838 | LPC_GPDMACH0->DMACCControl = w | (1UL << 18) | (1UL << 21) | (1UL << 31) | DMA_CHANNEL_SRC_INC ; // 16 bit transfer , address increment, interrupt |
dreschpe | 0:f5b62bef3680 | 839 | LPC_GPDMACH0->DMACCConfig = DMA_CHANNEL_ENABLE | DMA_TRANSFER_TYPE_M2P ; |
dreschpe | 0:f5b62bef3680 | 840 | LPC_GPDMA->DMACSoftSReq = 0x1; |
dreschpe | 0:f5b62bef3680 | 841 | do { |
dreschpe | 0:f5b62bef3680 | 842 | } while ((LPC_GPDMA->DMACRawIntTCStat & 0x01) == 0); // DMA is running |
dreschpe | 0:f5b62bef3680 | 843 | |
dreschpe | 0:f5b62bef3680 | 844 | //for (i = 0; i < w; i++) { // copy pixel data to TFT |
dreschpe | 0:f5b62bef3680 | 845 | // _spi.write(*bitmap_ptr); // one line |
dreschpe | 0:f5b62bef3680 | 846 | // bitmap_ptr++; |
dreschpe | 0:f5b62bef3680 | 847 | //} |
dreschpe | 0:f5b62bef3680 | 848 | bitmap_ptr -= w; |
dreschpe | 0:f5b62bef3680 | 849 | bitmap_ptr -= padd; |
dreschpe | 0:f5b62bef3680 | 850 | } |
dreschpe | 0:f5b62bef3680 | 851 | |
dreschpe | 0:f5b62bef3680 | 852 | if(spi_port == 0){ // TFT on SSP0 |
dreschpe | 0:f5b62bef3680 | 853 | do { |
dreschpe | 0:f5b62bef3680 | 854 | } while ((LPC_SSP0->SR & 0x01) == 0); // SPI FIFO not empty |
dreschpe | 0:f5b62bef3680 | 855 | } |
dreschpe | 0:f5b62bef3680 | 856 | else{ |
dreschpe | 0:f5b62bef3680 | 857 | do { |
dreschpe | 0:f5b62bef3680 | 858 | } while ((LPC_SSP1->SR & 0x01) == 0); // SPI FIFO not empty |
dreschpe | 0:f5b62bef3680 | 859 | } |
dreschpe | 0:f5b62bef3680 | 860 | |
dreschpe | 0:f5b62bef3680 | 861 | _spi.format(8,3); |
dreschpe | 0:f5b62bef3680 | 862 | wr_dat_stop(); |
dreschpe | 0:f5b62bef3680 | 863 | WindowMax(); |
dreschpe | 0:f5b62bef3680 | 864 | } |
dreschpe | 0:f5b62bef3680 | 865 | |
dreschpe | 0:f5b62bef3680 | 866 | |
dreschpe | 0:f5b62bef3680 | 867 | int SPI_TFT::BMP_16(unsigned int x, unsigned int y, const char *Name_BMP) { |
dreschpe | 0:f5b62bef3680 | 868 | |
dreschpe | 0:f5b62bef3680 | 869 | #define OffsetPixelWidth 18 |
dreschpe | 0:f5b62bef3680 | 870 | #define OffsetPixelHeigh 22 |
dreschpe | 0:f5b62bef3680 | 871 | #define OffsetFileSize 34 |
dreschpe | 0:f5b62bef3680 | 872 | #define OffsetPixData 10 |
dreschpe | 0:f5b62bef3680 | 873 | #define OffsetBPP 28 |
dreschpe | 0:f5b62bef3680 | 874 | |
dreschpe | 0:f5b62bef3680 | 875 | char filename[50]; |
dreschpe | 0:f5b62bef3680 | 876 | unsigned char BMP_Header[54]; |
dreschpe | 0:f5b62bef3680 | 877 | unsigned short BPP_t; |
dreschpe | 0:f5b62bef3680 | 878 | unsigned int PixelWidth,PixelHeigh,start_data; |
dreschpe | 0:f5b62bef3680 | 879 | unsigned int i,off; |
dreschpe | 0:f5b62bef3680 | 880 | int padd,j; |
dreschpe | 0:f5b62bef3680 | 881 | unsigned short *line; |
dreschpe | 0:f5b62bef3680 | 882 | |
dreschpe | 0:f5b62bef3680 | 883 | // get the filename |
dreschpe | 0:f5b62bef3680 | 884 | LocalFileSystem local("local"); |
dreschpe | 0:f5b62bef3680 | 885 | sprintf(&filename[0],"/local/"); |
dreschpe | 0:f5b62bef3680 | 886 | i=7; |
dreschpe | 0:f5b62bef3680 | 887 | while (*Name_BMP!='\0') { |
dreschpe | 0:f5b62bef3680 | 888 | filename[i++]=*Name_BMP++; |
dreschpe | 0:f5b62bef3680 | 889 | } |
dreschpe | 0:f5b62bef3680 | 890 | FILE *Image = fopen((const char *)&filename[0], "r"); // open the bmp file |
dreschpe | 0:f5b62bef3680 | 891 | if (!Image) { |
dreschpe | 0:f5b62bef3680 | 892 | return(0); // error file not found ! |
dreschpe | 0:f5b62bef3680 | 893 | } |
dreschpe | 0:f5b62bef3680 | 894 | |
dreschpe | 0:f5b62bef3680 | 895 | fread(&BMP_Header[0],1,54,Image); // get the BMP Header |
dreschpe | 0:f5b62bef3680 | 896 | |
dreschpe | 0:f5b62bef3680 | 897 | if (BMP_Header[0] != 0x42 || BMP_Header[1] != 0x4D) { // check magic byte |
dreschpe | 0:f5b62bef3680 | 898 | fclose(Image); |
dreschpe | 0:f5b62bef3680 | 899 | return(-1); // error no BMP file |
dreschpe | 0:f5b62bef3680 | 900 | } |
dreschpe | 0:f5b62bef3680 | 901 | |
dreschpe | 0:f5b62bef3680 | 902 | BPP_t = BMP_Header[OffsetBPP] + (BMP_Header[OffsetBPP + 1] << 8); |
dreschpe | 0:f5b62bef3680 | 903 | if (BPP_t != 0x0010) { |
dreschpe | 0:f5b62bef3680 | 904 | fclose(Image); |
dreschpe | 0:f5b62bef3680 | 905 | return(-2); // error no 16 bit BMP |
dreschpe | 0:f5b62bef3680 | 906 | } |
dreschpe | 0:f5b62bef3680 | 907 | |
dreschpe | 0:f5b62bef3680 | 908 | PixelHeigh = BMP_Header[OffsetPixelHeigh] + (BMP_Header[OffsetPixelHeigh + 1] << 8) + (BMP_Header[OffsetPixelHeigh + 2] << 16) + (BMP_Header[OffsetPixelHeigh + 3] << 24); |
dreschpe | 0:f5b62bef3680 | 909 | PixelWidth = BMP_Header[OffsetPixelWidth] + (BMP_Header[OffsetPixelWidth + 1] << 8) + (BMP_Header[OffsetPixelWidth + 2] << 16) + (BMP_Header[OffsetPixelWidth + 3] << 24); |
dreschpe | 0:f5b62bef3680 | 910 | if (PixelHeigh > height() + y || PixelWidth > width() + x) { |
dreschpe | 0:f5b62bef3680 | 911 | fclose(Image); |
dreschpe | 0:f5b62bef3680 | 912 | return(-3); // to big |
dreschpe | 0:f5b62bef3680 | 913 | } |
dreschpe | 0:f5b62bef3680 | 914 | |
dreschpe | 0:f5b62bef3680 | 915 | start_data = BMP_Header[OffsetPixData] + (BMP_Header[OffsetPixData + 1] << 8) + (BMP_Header[OffsetPixData + 2] << 16) + (BMP_Header[OffsetPixData + 3] << 24); |
dreschpe | 0:f5b62bef3680 | 916 | |
dreschpe | 0:f5b62bef3680 | 917 | line = (unsigned short *) malloc (PixelWidth); // we need a buffer for a line |
dreschpe | 0:f5b62bef3680 | 918 | if (line == NULL) { |
dreschpe | 0:f5b62bef3680 | 919 | return(-4); // error no memory |
dreschpe | 0:f5b62bef3680 | 920 | } |
dreschpe | 0:f5b62bef3680 | 921 | |
dreschpe | 0:f5b62bef3680 | 922 | // the lines are padded to multiple of 4 bytes |
dreschpe | 0:f5b62bef3680 | 923 | padd = -1; |
dreschpe | 0:f5b62bef3680 | 924 | do { |
dreschpe | 0:f5b62bef3680 | 925 | padd ++; |
dreschpe | 0:f5b62bef3680 | 926 | } while ((PixelWidth * 2 + padd)%4 != 0); |
dreschpe | 0:f5b62bef3680 | 927 | |
dreschpe | 0:f5b62bef3680 | 928 | window(x, y,PixelWidth,PixelHeigh); |
dreschpe | 0:f5b62bef3680 | 929 | wr_cmd(0x22); |
dreschpe | 0:f5b62bef3680 | 930 | wr_dat_start(); |
dreschpe | 0:f5b62bef3680 | 931 | _spi.format(16,3); |
dreschpe | 0:f5b62bef3680 | 932 | for (j = PixelHeigh - 1; j >= 0; j--) { //Lines bottom up |
dreschpe | 0:f5b62bef3680 | 933 | off = j * (PixelWidth * 2 + padd) + start_data; // start of line |
dreschpe | 0:f5b62bef3680 | 934 | fseek(Image, off ,SEEK_SET); |
dreschpe | 0:f5b62bef3680 | 935 | fread(line,1,PixelWidth * 2,Image); // read a line - slow ! |
dreschpe | 0:f5b62bef3680 | 936 | LPC_GPDMA->DMACIntTCClear = 0x1; |
dreschpe | 0:f5b62bef3680 | 937 | LPC_GPDMA->DMACIntErrClr = 0x1; |
dreschpe | 0:f5b62bef3680 | 938 | LPC_GPDMACH0->DMACCSrcAddr = (uint32_t)line; |
dreschpe | 0:f5b62bef3680 | 939 | LPC_GPDMACH0->DMACCControl = PixelWidth | (1UL << 18) | (1UL << 21) | (1UL << 31) | DMA_CHANNEL_SRC_INC ; // 16 bit transfer , address increment, interrupt |
dreschpe | 0:f5b62bef3680 | 940 | LPC_GPDMACH0->DMACCConfig = DMA_CHANNEL_ENABLE | DMA_TRANSFER_TYPE_M2P ; |
dreschpe | 0:f5b62bef3680 | 941 | LPC_GPDMA->DMACSoftSReq = 0x1; |
dreschpe | 0:f5b62bef3680 | 942 | do { |
dreschpe | 0:f5b62bef3680 | 943 | } while ((LPC_GPDMA->DMACRawIntTCStat & 0x01) == 0); // DMA is running |
dreschpe | 0:f5b62bef3680 | 944 | |
dreschpe | 0:f5b62bef3680 | 945 | //for (i = 0; i < PixelWidth; i++) { // copy pixel data to TFT |
dreschpe | 0:f5b62bef3680 | 946 | // _spi.write(line[i]); // one 16 bit pixel |
dreschpe | 0:f5b62bef3680 | 947 | //} |
dreschpe | 0:f5b62bef3680 | 948 | } |
dreschpe | 0:f5b62bef3680 | 949 | |
dreschpe | 0:f5b62bef3680 | 950 | if(spi_port == 0){ // TFT on SSP0 |
dreschpe | 0:f5b62bef3680 | 951 | do { |
dreschpe | 0:f5b62bef3680 | 952 | } while ((LPC_SSP0->SR & 0x01) == 0); // SPI FIFO not empty |
dreschpe | 0:f5b62bef3680 | 953 | } |
dreschpe | 0:f5b62bef3680 | 954 | else{ |
dreschpe | 0:f5b62bef3680 | 955 | do { |
dreschpe | 0:f5b62bef3680 | 956 | } while ((LPC_SSP1->SR & 0x01) == 0); // SPI FIFO not empty |
dreschpe | 0:f5b62bef3680 | 957 | } |
dreschpe | 0:f5b62bef3680 | 958 | |
dreschpe | 0:f5b62bef3680 | 959 | _spi.format(8,3); |
dreschpe | 0:f5b62bef3680 | 960 | wr_dat_stop(); |
dreschpe | 0:f5b62bef3680 | 961 | free (line); |
dreschpe | 0:f5b62bef3680 | 962 | fclose(Image); |
dreschpe | 0:f5b62bef3680 | 963 | WindowMax(); |
dreschpe | 0:f5b62bef3680 | 964 | return(1); |
dreschpe | 0:f5b62bef3680 | 965 | } |