.
SPI_TFT_ILI9341_NUCLEO.cpp@0:1b3481785d59, 2019-06-16 (annotated)
- Committer:
- 2018US_EminaTahirovi
- Date:
- Sun Jun 16 20:26:22 2019 +0000
- Revision:
- 0:1b3481785d59
.
Who changed what in which revision?
User | Revision | Line number | New 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) 2013, 2014 Peter Drescher - DC2PD |
2018US_EminaTahirovi | 0:1b3481785d59 | 3 | * Special version for STM Nucleo -L152 |
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 | // 24.06.14 initial version |
2018US_EminaTahirovi | 0:1b3481785d59 | 15 | // 25.06.14 add Nucleo F103RB |
2018US_EminaTahirovi | 0:1b3481785d59 | 16 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 17 | // only include this file if target is L152 or F103RB : |
2018US_EminaTahirovi | 0:1b3481785d59 | 18 | #if defined TARGET_NUCLEO_L152RE || defined TARGET_NUCLEO_F103RB |
2018US_EminaTahirovi | 0:1b3481785d59 | 19 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 20 | #include "SPI_TFT_ILI9341.h" |
2018US_EminaTahirovi | 0:1b3481785d59 | 21 | #include "mbed.h" |
2018US_EminaTahirovi | 0:1b3481785d59 | 22 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 23 | #if defined TARGET_NUCLEO_L152RE |
2018US_EminaTahirovi | 0:1b3481785d59 | 24 | #include "stm32l1xx_dma.h" |
2018US_EminaTahirovi | 0:1b3481785d59 | 25 | #define use_ram |
2018US_EminaTahirovi | 0:1b3481785d59 | 26 | #endif |
2018US_EminaTahirovi | 0:1b3481785d59 | 27 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 28 | #if defined TARGET_NUCLEO_F103RB |
2018US_EminaTahirovi | 0:1b3481785d59 | 29 | #include "stm32f10x_dma.h" |
2018US_EminaTahirovi | 0:1b3481785d59 | 30 | #endif |
2018US_EminaTahirovi | 0:1b3481785d59 | 31 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 32 | #define BPP 16 // Bits per pixel |
2018US_EminaTahirovi | 0:1b3481785d59 | 33 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 34 | //extern Serial pc; |
2018US_EminaTahirovi | 0:1b3481785d59 | 35 | //extern DigitalOut xx; // debug !! |
2018US_EminaTahirovi | 0:1b3481785d59 | 36 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 37 | DMA_InitTypeDef DMA_InitStructure; |
2018US_EminaTahirovi | 0:1b3481785d59 | 38 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 39 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 40 | 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 | 41 | : GraphicsDisplay(name), SPI(mosi,miso,sclk,NC), _cs(cs), _reset(reset), _dc(dc) |
2018US_EminaTahirovi | 0:1b3481785d59 | 42 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 43 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 44 | format(8,3); // 8 bit spi mode 3 |
2018US_EminaTahirovi | 0:1b3481785d59 | 45 | frequency(10000000); // 10 Mhz SPI clock : result 2 / 4 = 8 |
2018US_EminaTahirovi | 0:1b3481785d59 | 46 | orientation = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 47 | char_x = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 48 | if(_spi.spi == SPI_1){ // test which SPI is in use |
2018US_EminaTahirovi | 0:1b3481785d59 | 49 | spi_num = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 50 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 51 | if(_spi.spi == SPI_2){ |
2018US_EminaTahirovi | 0:1b3481785d59 | 52 | spi_num = 2; |
2018US_EminaTahirovi | 0:1b3481785d59 | 53 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 54 | #ifdef SPI_3 // there is no SPI 3 on all devices |
2018US_EminaTahirovi | 0:1b3481785d59 | 55 | if(_spi.spi == SPI_3){ |
2018US_EminaTahirovi | 0:1b3481785d59 | 56 | spi_num = 3; |
2018US_EminaTahirovi | 0:1b3481785d59 | 57 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 58 | #endif |
2018US_EminaTahirovi | 0:1b3481785d59 | 59 | tft_reset(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 60 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 61 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 62 | // we define a fast write to the SPI port |
2018US_EminaTahirovi | 0:1b3481785d59 | 63 | // we use the bit banding address to get the flag without masking |
2018US_EminaTahirovi | 0:1b3481785d59 | 64 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 65 | #define bit_SPI1_txe *((volatile unsigned int *)0x42260104) |
2018US_EminaTahirovi | 0:1b3481785d59 | 66 | #define SPI1_DR *((volatile unsigned int *)0x4001300C) |
2018US_EminaTahirovi | 0:1b3481785d59 | 67 | #define bit_SPI2_txe *((volatile unsigned int *)0x42070104) |
2018US_EminaTahirovi | 0:1b3481785d59 | 68 | #define SPI2_DR *((volatile unsigned int *)0x4000380C) |
2018US_EminaTahirovi | 0:1b3481785d59 | 69 | #define bit_SPI3_txe *((volatile unsigned int *)0x42078104) |
2018US_EminaTahirovi | 0:1b3481785d59 | 70 | #define SPI3_DR *((volatile unsigned int *)0x40003C0C) |
2018US_EminaTahirovi | 0:1b3481785d59 | 71 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 72 | void SPI_TFT_ILI9341::f_write(int data){ |
2018US_EminaTahirovi | 0:1b3481785d59 | 73 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 74 | switch(spi_num){ // used SPI port |
2018US_EminaTahirovi | 0:1b3481785d59 | 75 | case (1): |
2018US_EminaTahirovi | 0:1b3481785d59 | 76 | while(bit_SPI1_txe == 0); // wait for SPI1->SR TXE flag |
2018US_EminaTahirovi | 0:1b3481785d59 | 77 | SPI1_DR = data; |
2018US_EminaTahirovi | 0:1b3481785d59 | 78 | break; |
2018US_EminaTahirovi | 0:1b3481785d59 | 79 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 80 | case (2): |
2018US_EminaTahirovi | 0:1b3481785d59 | 81 | while( bit_SPI2_txe == 0); // wait for SPI2->SR TXE flag |
2018US_EminaTahirovi | 0:1b3481785d59 | 82 | SPI2_DR = data; |
2018US_EminaTahirovi | 0:1b3481785d59 | 83 | break; |
2018US_EminaTahirovi | 0:1b3481785d59 | 84 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 85 | case (3): |
2018US_EminaTahirovi | 0:1b3481785d59 | 86 | while( bit_SPI3_txe == 0); // wait for SPI3->SR TXE flag |
2018US_EminaTahirovi | 0:1b3481785d59 | 87 | SPI3_DR = data; |
2018US_EminaTahirovi | 0:1b3481785d59 | 88 | break; |
2018US_EminaTahirovi | 0:1b3481785d59 | 89 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 90 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 91 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 92 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 93 | // wait for SPI not busy |
2018US_EminaTahirovi | 0:1b3481785d59 | 94 | // we have to wait for the last bit to switch the cs off |
2018US_EminaTahirovi | 0:1b3481785d59 | 95 | // we use the bit banding address to get the flag without masking |
2018US_EminaTahirovi | 0:1b3481785d59 | 96 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 97 | #define bit_SPI1_bsy *((volatile unsigned int *)0x4226011C) |
2018US_EminaTahirovi | 0:1b3481785d59 | 98 | #define bit_SPI2_bsy *((volatile unsigned int *)0x4207011C) |
2018US_EminaTahirovi | 0:1b3481785d59 | 99 | #define bit_SPI3_bsy *((volatile unsigned int *)0x4207811C) |
2018US_EminaTahirovi | 0:1b3481785d59 | 100 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 101 | void inline SPI_TFT_ILI9341::spi_bsy(void){ |
2018US_EminaTahirovi | 0:1b3481785d59 | 102 | switch(spi_num){ // decide which SPI is to use |
2018US_EminaTahirovi | 0:1b3481785d59 | 103 | case (1): |
2018US_EminaTahirovi | 0:1b3481785d59 | 104 | while(bit_SPI1_bsy == 1); // SPI1->SR bit 7 |
2018US_EminaTahirovi | 0:1b3481785d59 | 105 | break; |
2018US_EminaTahirovi | 0:1b3481785d59 | 106 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 107 | case (2): |
2018US_EminaTahirovi | 0:1b3481785d59 | 108 | while(bit_SPI2_bsy == 1); // SPI2->SR bit 7 |
2018US_EminaTahirovi | 0:1b3481785d59 | 109 | break; |
2018US_EminaTahirovi | 0:1b3481785d59 | 110 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 111 | case (3): |
2018US_EminaTahirovi | 0:1b3481785d59 | 112 | while(bit_SPI3_bsy == 1); // SPI2->SR bit 7 |
2018US_EminaTahirovi | 0:1b3481785d59 | 113 | break; |
2018US_EminaTahirovi | 0:1b3481785d59 | 114 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 115 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 116 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 117 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 118 | // switch fast between 8 and 16 bit mode |
2018US_EminaTahirovi | 0:1b3481785d59 | 119 | #define bit_SPI1_dff *((volatile unsigned int *)0x4226002C) |
2018US_EminaTahirovi | 0:1b3481785d59 | 120 | #define bit_SPI2_dff *((volatile unsigned int *)0x4207002C) |
2018US_EminaTahirovi | 0:1b3481785d59 | 121 | #define bit_SPI3_dff *((volatile unsigned int *)0x4207802C) |
2018US_EminaTahirovi | 0:1b3481785d59 | 122 | void SPI_TFT_ILI9341::spi_16(bool s){ |
2018US_EminaTahirovi | 0:1b3481785d59 | 123 | switch(spi_num){ // decide which SPI is to use |
2018US_EminaTahirovi | 0:1b3481785d59 | 124 | case(1): |
2018US_EminaTahirovi | 0:1b3481785d59 | 125 | if(s) bit_SPI1_dff = 1; // switch to 16 bit Mode |
2018US_EminaTahirovi | 0:1b3481785d59 | 126 | else bit_SPI1_dff = 0; // switch to 8 bit Mode |
2018US_EminaTahirovi | 0:1b3481785d59 | 127 | break; |
2018US_EminaTahirovi | 0:1b3481785d59 | 128 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 129 | case(2): |
2018US_EminaTahirovi | 0:1b3481785d59 | 130 | if(s) bit_SPI2_dff = 1; // switch to 16 bit Mode |
2018US_EminaTahirovi | 0:1b3481785d59 | 131 | else bit_SPI2_dff = 0; // switch to 8 bit Mode |
2018US_EminaTahirovi | 0:1b3481785d59 | 132 | break; |
2018US_EminaTahirovi | 0:1b3481785d59 | 133 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 134 | case(3): |
2018US_EminaTahirovi | 0:1b3481785d59 | 135 | if(s) bit_SPI3_dff = 1; // switch to 16 bit Mode |
2018US_EminaTahirovi | 0:1b3481785d59 | 136 | else bit_SPI3_dff = 0; // switch to 8 bit Mode |
2018US_EminaTahirovi | 0:1b3481785d59 | 137 | break; |
2018US_EminaTahirovi | 0:1b3481785d59 | 138 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 139 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 140 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 141 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 142 | int SPI_TFT_ILI9341::width() |
2018US_EminaTahirovi | 0:1b3481785d59 | 143 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 144 | if (orientation == 0 || orientation == 2) return 240; |
2018US_EminaTahirovi | 0:1b3481785d59 | 145 | else return 320; |
2018US_EminaTahirovi | 0:1b3481785d59 | 146 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 147 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 148 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 149 | int SPI_TFT_ILI9341::height() |
2018US_EminaTahirovi | 0:1b3481785d59 | 150 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 151 | if (orientation == 0 || orientation == 2) return 320; |
2018US_EminaTahirovi | 0:1b3481785d59 | 152 | else return 240; |
2018US_EminaTahirovi | 0:1b3481785d59 | 153 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 154 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 155 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 156 | void SPI_TFT_ILI9341::set_orientation(unsigned int o) |
2018US_EminaTahirovi | 0:1b3481785d59 | 157 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 158 | orientation = o; |
2018US_EminaTahirovi | 0:1b3481785d59 | 159 | wr_cmd(0x36); // MEMORY_ACCESS_CONTROL |
2018US_EminaTahirovi | 0:1b3481785d59 | 160 | switch (orientation) { |
2018US_EminaTahirovi | 0:1b3481785d59 | 161 | case 0: |
2018US_EminaTahirovi | 0:1b3481785d59 | 162 | f_write(0x48); |
2018US_EminaTahirovi | 0:1b3481785d59 | 163 | break; |
2018US_EminaTahirovi | 0:1b3481785d59 | 164 | case 1: |
2018US_EminaTahirovi | 0:1b3481785d59 | 165 | f_write(0x28); |
2018US_EminaTahirovi | 0:1b3481785d59 | 166 | break; |
2018US_EminaTahirovi | 0:1b3481785d59 | 167 | case 2: |
2018US_EminaTahirovi | 0:1b3481785d59 | 168 | f_write(0x88); |
2018US_EminaTahirovi | 0:1b3481785d59 | 169 | break; |
2018US_EminaTahirovi | 0:1b3481785d59 | 170 | case 3: |
2018US_EminaTahirovi | 0:1b3481785d59 | 171 | f_write(0xE8); |
2018US_EminaTahirovi | 0:1b3481785d59 | 172 | break; |
2018US_EminaTahirovi | 0:1b3481785d59 | 173 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 174 | spi_bsy(); // wait for end of transfer |
2018US_EminaTahirovi | 0:1b3481785d59 | 175 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 176 | WindowMax(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 177 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 178 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 179 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 180 | // write command to tft register |
2018US_EminaTahirovi | 0:1b3481785d59 | 181 | // use fast command |
2018US_EminaTahirovi | 0:1b3481785d59 | 182 | void SPI_TFT_ILI9341::wr_cmd(unsigned char cmd) |
2018US_EminaTahirovi | 0:1b3481785d59 | 183 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 184 | _dc = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 185 | _cs = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 186 | f_write(cmd); |
2018US_EminaTahirovi | 0:1b3481785d59 | 187 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 188 | _dc = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 189 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 190 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 191 | void SPI_TFT_ILI9341::wr_dat(unsigned char dat) |
2018US_EminaTahirovi | 0:1b3481785d59 | 192 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 193 | f_write(dat); |
2018US_EminaTahirovi | 0:1b3481785d59 | 194 | spi_bsy(); // wait for SPI send |
2018US_EminaTahirovi | 0:1b3481785d59 | 195 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 196 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 197 | // the ILI9341 can read |
2018US_EminaTahirovi | 0:1b3481785d59 | 198 | char SPI_TFT_ILI9341::rd_byte(unsigned char cmd) |
2018US_EminaTahirovi | 0:1b3481785d59 | 199 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 200 | // has to change !! |
2018US_EminaTahirovi | 0:1b3481785d59 | 201 | return(0); |
2018US_EminaTahirovi | 0:1b3481785d59 | 202 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 203 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 204 | // read 32 bit |
2018US_EminaTahirovi | 0:1b3481785d59 | 205 | int SPI_TFT_ILI9341::rd_32(unsigned char cmd) |
2018US_EminaTahirovi | 0:1b3481785d59 | 206 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 207 | // has to change !!! |
2018US_EminaTahirovi | 0:1b3481785d59 | 208 | return(0); |
2018US_EminaTahirovi | 0:1b3481785d59 | 209 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 210 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 211 | int SPI_TFT_ILI9341::Read_ID(void){ |
2018US_EminaTahirovi | 0:1b3481785d59 | 212 | int r; |
2018US_EminaTahirovi | 0:1b3481785d59 | 213 | r = rd_byte(0x0A); |
2018US_EminaTahirovi | 0:1b3481785d59 | 214 | r = rd_byte(0x0A); |
2018US_EminaTahirovi | 0:1b3481785d59 | 215 | r = rd_byte(0x0A); |
2018US_EminaTahirovi | 0:1b3481785d59 | 216 | r = rd_byte(0x0A); |
2018US_EminaTahirovi | 0:1b3481785d59 | 217 | return(r); |
2018US_EminaTahirovi | 0:1b3481785d59 | 218 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 219 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 220 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 221 | // Init code based on MI0283QT datasheet |
2018US_EminaTahirovi | 0:1b3481785d59 | 222 | // this code is called only at start |
2018US_EminaTahirovi | 0:1b3481785d59 | 223 | // no need to be optimized |
2018US_EminaTahirovi | 0:1b3481785d59 | 224 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 225 | void SPI_TFT_ILI9341::tft_reset() |
2018US_EminaTahirovi | 0:1b3481785d59 | 226 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 227 | _cs = 1; // cs high |
2018US_EminaTahirovi | 0:1b3481785d59 | 228 | _dc = 1; // dc high |
2018US_EminaTahirovi | 0:1b3481785d59 | 229 | _reset = 0; // display reset |
2018US_EminaTahirovi | 0:1b3481785d59 | 230 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 231 | wait_us(50); |
2018US_EminaTahirovi | 0:1b3481785d59 | 232 | _reset = 1; // end hardware reset |
2018US_EminaTahirovi | 0:1b3481785d59 | 233 | wait_ms(5); |
2018US_EminaTahirovi | 0:1b3481785d59 | 234 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 235 | wr_cmd(0x01); // SW reset |
2018US_EminaTahirovi | 0:1b3481785d59 | 236 | wait_ms(5); |
2018US_EminaTahirovi | 0:1b3481785d59 | 237 | wr_cmd(0x28); // display off |
2018US_EminaTahirovi | 0:1b3481785d59 | 238 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 239 | /* Start Initial Sequence ----------------------------------------------------*/ |
2018US_EminaTahirovi | 0:1b3481785d59 | 240 | wr_cmd(0xCF); |
2018US_EminaTahirovi | 0:1b3481785d59 | 241 | f_write(0x00); |
2018US_EminaTahirovi | 0:1b3481785d59 | 242 | f_write(0x83); |
2018US_EminaTahirovi | 0:1b3481785d59 | 243 | f_write(0x30); |
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(0xED); |
2018US_EminaTahirovi | 0:1b3481785d59 | 248 | f_write(0x64); |
2018US_EminaTahirovi | 0:1b3481785d59 | 249 | f_write(0x03); |
2018US_EminaTahirovi | 0:1b3481785d59 | 250 | f_write(0x12); |
2018US_EminaTahirovi | 0:1b3481785d59 | 251 | f_write(0x81); |
2018US_EminaTahirovi | 0:1b3481785d59 | 252 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 253 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 254 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 255 | wr_cmd(0xE8); |
2018US_EminaTahirovi | 0:1b3481785d59 | 256 | f_write(0x85); |
2018US_EminaTahirovi | 0:1b3481785d59 | 257 | f_write(0x01); |
2018US_EminaTahirovi | 0:1b3481785d59 | 258 | f_write(0x79); |
2018US_EminaTahirovi | 0:1b3481785d59 | 259 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 260 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 261 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 262 | wr_cmd(0xCB); |
2018US_EminaTahirovi | 0:1b3481785d59 | 263 | f_write(0x39); |
2018US_EminaTahirovi | 0:1b3481785d59 | 264 | f_write(0x2C); |
2018US_EminaTahirovi | 0:1b3481785d59 | 265 | f_write(0x00); |
2018US_EminaTahirovi | 0:1b3481785d59 | 266 | f_write(0x34); |
2018US_EminaTahirovi | 0:1b3481785d59 | 267 | f_write(0x02); |
2018US_EminaTahirovi | 0:1b3481785d59 | 268 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 269 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 270 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 271 | wr_cmd(0xF7); |
2018US_EminaTahirovi | 0:1b3481785d59 | 272 | f_write(0x20); |
2018US_EminaTahirovi | 0:1b3481785d59 | 273 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 274 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 275 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 276 | wr_cmd(0xEA); |
2018US_EminaTahirovi | 0:1b3481785d59 | 277 | f_write(0x00); |
2018US_EminaTahirovi | 0:1b3481785d59 | 278 | f_write(0x00); |
2018US_EminaTahirovi | 0:1b3481785d59 | 279 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 280 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 281 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 282 | wr_cmd(0xC0); // POWER_CONTROL_1 |
2018US_EminaTahirovi | 0:1b3481785d59 | 283 | f_write(0x26); |
2018US_EminaTahirovi | 0:1b3481785d59 | 284 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 285 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 286 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 287 | wr_cmd(0xC1); // POWER_CONTROL_2 |
2018US_EminaTahirovi | 0:1b3481785d59 | 288 | f_write(0x11); |
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(0xC5); // VCOM_CONTROL_1 |
2018US_EminaTahirovi | 0:1b3481785d59 | 293 | f_write(0x35); |
2018US_EminaTahirovi | 0:1b3481785d59 | 294 | f_write(0x3E); |
2018US_EminaTahirovi | 0:1b3481785d59 | 295 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 296 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 297 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 298 | wr_cmd(0xC7); // VCOM_CONTROL_2 |
2018US_EminaTahirovi | 0:1b3481785d59 | 299 | f_write(0xBE); |
2018US_EminaTahirovi | 0:1b3481785d59 | 300 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 301 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 302 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 303 | wr_cmd(0x36); // MEMORY_ACCESS_CONTROL |
2018US_EminaTahirovi | 0:1b3481785d59 | 304 | f_write(0x48); |
2018US_EminaTahirovi | 0:1b3481785d59 | 305 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 306 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 307 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 308 | wr_cmd(0x3A); // COLMOD_PIXEL_FORMAT_SET |
2018US_EminaTahirovi | 0:1b3481785d59 | 309 | f_write(0x55); // 16 bit pixel |
2018US_EminaTahirovi | 0:1b3481785d59 | 310 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 311 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 312 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 313 | wr_cmd(0xB1); // Frame Rate |
2018US_EminaTahirovi | 0:1b3481785d59 | 314 | f_write(0x00); |
2018US_EminaTahirovi | 0:1b3481785d59 | 315 | f_write(0x1B); |
2018US_EminaTahirovi | 0:1b3481785d59 | 316 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 317 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 318 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 319 | wr_cmd(0xF2); // Gamma Function Disable |
2018US_EminaTahirovi | 0:1b3481785d59 | 320 | f_write(0x08); |
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(0x26); |
2018US_EminaTahirovi | 0:1b3481785d59 | 325 | f_write(0x01); // gamma set for curve 01/2/04/08 |
2018US_EminaTahirovi | 0:1b3481785d59 | 326 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 327 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 328 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 329 | wr_cmd(0xE0); // positive gamma correction |
2018US_EminaTahirovi | 0:1b3481785d59 | 330 | f_write(0x1F); |
2018US_EminaTahirovi | 0:1b3481785d59 | 331 | f_write(0x1A); |
2018US_EminaTahirovi | 0:1b3481785d59 | 332 | f_write(0x18); |
2018US_EminaTahirovi | 0:1b3481785d59 | 333 | f_write(0x0A); |
2018US_EminaTahirovi | 0:1b3481785d59 | 334 | f_write(0x0F); |
2018US_EminaTahirovi | 0:1b3481785d59 | 335 | f_write(0x06); |
2018US_EminaTahirovi | 0:1b3481785d59 | 336 | f_write(0x45); |
2018US_EminaTahirovi | 0:1b3481785d59 | 337 | f_write(0x87); |
2018US_EminaTahirovi | 0:1b3481785d59 | 338 | f_write(0x32); |
2018US_EminaTahirovi | 0:1b3481785d59 | 339 | f_write(0x0A); |
2018US_EminaTahirovi | 0:1b3481785d59 | 340 | f_write(0x07); |
2018US_EminaTahirovi | 0:1b3481785d59 | 341 | f_write(0x02); |
2018US_EminaTahirovi | 0:1b3481785d59 | 342 | f_write(0x07); |
2018US_EminaTahirovi | 0:1b3481785d59 | 343 | f_write(0x05); |
2018US_EminaTahirovi | 0:1b3481785d59 | 344 | f_write(0x00); |
2018US_EminaTahirovi | 0:1b3481785d59 | 345 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 346 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 347 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 348 | wr_cmd(0xE1); // negativ gamma correction |
2018US_EminaTahirovi | 0:1b3481785d59 | 349 | f_write(0x00); |
2018US_EminaTahirovi | 0:1b3481785d59 | 350 | f_write(0x25); |
2018US_EminaTahirovi | 0:1b3481785d59 | 351 | f_write(0x27); |
2018US_EminaTahirovi | 0:1b3481785d59 | 352 | f_write(0x05); |
2018US_EminaTahirovi | 0:1b3481785d59 | 353 | f_write(0x10); |
2018US_EminaTahirovi | 0:1b3481785d59 | 354 | f_write(0x09); |
2018US_EminaTahirovi | 0:1b3481785d59 | 355 | f_write(0x3A); |
2018US_EminaTahirovi | 0:1b3481785d59 | 356 | f_write(0x78); |
2018US_EminaTahirovi | 0:1b3481785d59 | 357 | f_write(0x4D); |
2018US_EminaTahirovi | 0:1b3481785d59 | 358 | f_write(0x05); |
2018US_EminaTahirovi | 0:1b3481785d59 | 359 | f_write(0x18); |
2018US_EminaTahirovi | 0:1b3481785d59 | 360 | f_write(0x0D); |
2018US_EminaTahirovi | 0:1b3481785d59 | 361 | f_write(0x38); |
2018US_EminaTahirovi | 0:1b3481785d59 | 362 | f_write(0x3A); |
2018US_EminaTahirovi | 0:1b3481785d59 | 363 | f_write(0x1F); |
2018US_EminaTahirovi | 0:1b3481785d59 | 364 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 365 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 366 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 367 | WindowMax (); |
2018US_EminaTahirovi | 0:1b3481785d59 | 368 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 369 | //wr_cmd(0x34); // tearing effect off |
2018US_EminaTahirovi | 0:1b3481785d59 | 370 | //_cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 371 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 372 | //wr_cmd(0x35); // tearing effect on |
2018US_EminaTahirovi | 0:1b3481785d59 | 373 | //_cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 374 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 375 | wr_cmd(0xB7); // entry mode |
2018US_EminaTahirovi | 0:1b3481785d59 | 376 | f_write(0x07); |
2018US_EminaTahirovi | 0:1b3481785d59 | 377 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 378 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 379 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 380 | wr_cmd(0xB6); // display function control |
2018US_EminaTahirovi | 0:1b3481785d59 | 381 | f_write(0x0A); |
2018US_EminaTahirovi | 0:1b3481785d59 | 382 | f_write(0x82); |
2018US_EminaTahirovi | 0:1b3481785d59 | 383 | f_write(0x27); |
2018US_EminaTahirovi | 0:1b3481785d59 | 384 | f_write(0x00); |
2018US_EminaTahirovi | 0:1b3481785d59 | 385 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 386 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 387 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 388 | wr_cmd(0x11); // sleep out |
2018US_EminaTahirovi | 0:1b3481785d59 | 389 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 390 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 391 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 392 | wait_ms(100); |
2018US_EminaTahirovi | 0:1b3481785d59 | 393 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 394 | wr_cmd(0x29); // display on |
2018US_EminaTahirovi | 0:1b3481785d59 | 395 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 396 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 397 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 398 | wait_ms(100); |
2018US_EminaTahirovi | 0:1b3481785d59 | 399 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 400 | // Configure the DMA controller init-structure |
2018US_EminaTahirovi | 0:1b3481785d59 | 401 | DMA_StructInit(&DMA_InitStructure); |
2018US_EminaTahirovi | 0:1b3481785d59 | 402 | switch(spi_num){ // decide which SPI is to use |
2018US_EminaTahirovi | 0:1b3481785d59 | 403 | case (1): |
2018US_EminaTahirovi | 0:1b3481785d59 | 404 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); // SPI1 and SPI2 are using DMA 1 |
2018US_EminaTahirovi | 0:1b3481785d59 | 405 | DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &(SPI1->DR); |
2018US_EminaTahirovi | 0:1b3481785d59 | 406 | break; |
2018US_EminaTahirovi | 0:1b3481785d59 | 407 | case (2): |
2018US_EminaTahirovi | 0:1b3481785d59 | 408 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); // SPI1 and SPI2 are using DMA 1 |
2018US_EminaTahirovi | 0:1b3481785d59 | 409 | DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &(SPI2->DR); |
2018US_EminaTahirovi | 0:1b3481785d59 | 410 | break; |
2018US_EminaTahirovi | 0:1b3481785d59 | 411 | case (3): |
2018US_EminaTahirovi | 0:1b3481785d59 | 412 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE); // SPI3 is using DMA 2 |
2018US_EminaTahirovi | 0:1b3481785d59 | 413 | DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &(SPI3->DR); |
2018US_EminaTahirovi | 0:1b3481785d59 | 414 | break; |
2018US_EminaTahirovi | 0:1b3481785d59 | 415 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 416 | DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; |
2018US_EminaTahirovi | 0:1b3481785d59 | 417 | DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; |
2018US_EminaTahirovi | 0:1b3481785d59 | 418 | DMA_InitStructure.DMA_BufferSize = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 419 | DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; |
2018US_EminaTahirovi | 0:1b3481785d59 | 420 | DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; |
2018US_EminaTahirovi | 0:1b3481785d59 | 421 | DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; |
2018US_EminaTahirovi | 0:1b3481785d59 | 422 | DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; |
2018US_EminaTahirovi | 0:1b3481785d59 | 423 | DMA_InitStructure.DMA_Priority = DMA_Priority_High; |
2018US_EminaTahirovi | 0:1b3481785d59 | 424 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 425 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 426 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 427 | // speed optimized |
2018US_EminaTahirovi | 0:1b3481785d59 | 428 | // write direct to SPI1 register ! |
2018US_EminaTahirovi | 0:1b3481785d59 | 429 | void SPI_TFT_ILI9341::pixel(int x, int y, int color) |
2018US_EminaTahirovi | 0:1b3481785d59 | 430 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 431 | wr_cmd(0x2A); |
2018US_EminaTahirovi | 0:1b3481785d59 | 432 | spi_16(1); // switch to 8 bit Mode |
2018US_EminaTahirovi | 0:1b3481785d59 | 433 | f_write(x); |
2018US_EminaTahirovi | 0:1b3481785d59 | 434 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 435 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 436 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 437 | spi_16(0); // switch to 8 bit Mode |
2018US_EminaTahirovi | 0:1b3481785d59 | 438 | wr_cmd(0x2B); |
2018US_EminaTahirovi | 0:1b3481785d59 | 439 | spi_16(1); |
2018US_EminaTahirovi | 0:1b3481785d59 | 440 | f_write(y); |
2018US_EminaTahirovi | 0:1b3481785d59 | 441 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 442 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 443 | spi_16(0); |
2018US_EminaTahirovi | 0:1b3481785d59 | 444 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 445 | wr_cmd(0x2C); // send pixel |
2018US_EminaTahirovi | 0:1b3481785d59 | 446 | spi_16(1); |
2018US_EminaTahirovi | 0:1b3481785d59 | 447 | f_write(color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 448 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 449 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 450 | spi_16(0); |
2018US_EminaTahirovi | 0:1b3481785d59 | 451 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 452 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 453 | // optimized |
2018US_EminaTahirovi | 0:1b3481785d59 | 454 | // write direct to SPI1 register ! |
2018US_EminaTahirovi | 0:1b3481785d59 | 455 | void SPI_TFT_ILI9341::window (unsigned int x, unsigned int y, unsigned int w, unsigned int h) |
2018US_EminaTahirovi | 0:1b3481785d59 | 456 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 457 | wr_cmd(0x2A); |
2018US_EminaTahirovi | 0:1b3481785d59 | 458 | spi_16(1); |
2018US_EminaTahirovi | 0:1b3481785d59 | 459 | f_write(x); |
2018US_EminaTahirovi | 0:1b3481785d59 | 460 | f_write(x+w-1); |
2018US_EminaTahirovi | 0:1b3481785d59 | 461 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 462 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 463 | spi_16(0); |
2018US_EminaTahirovi | 0:1b3481785d59 | 464 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 465 | wr_cmd(0x2B); |
2018US_EminaTahirovi | 0:1b3481785d59 | 466 | spi_16(1); |
2018US_EminaTahirovi | 0:1b3481785d59 | 467 | f_write(y) ; |
2018US_EminaTahirovi | 0:1b3481785d59 | 468 | f_write(y+h-1); |
2018US_EminaTahirovi | 0:1b3481785d59 | 469 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 470 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 471 | spi_16(0); |
2018US_EminaTahirovi | 0:1b3481785d59 | 472 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 473 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 474 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 475 | void SPI_TFT_ILI9341::WindowMax (void) |
2018US_EminaTahirovi | 0:1b3481785d59 | 476 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 477 | window (0, 0, width(), height()); |
2018US_EminaTahirovi | 0:1b3481785d59 | 478 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 479 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 480 | // optimized |
2018US_EminaTahirovi | 0:1b3481785d59 | 481 | // use DMA to transfer pixel data to the screen |
2018US_EminaTahirovi | 0:1b3481785d59 | 482 | void SPI_TFT_ILI9341::cls (void) |
2018US_EminaTahirovi | 0:1b3481785d59 | 483 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 484 | // we can use the fillrect function |
2018US_EminaTahirovi | 0:1b3481785d59 | 485 | fillrect(0,0,width()-1,height()-1,_background); |
2018US_EminaTahirovi | 0:1b3481785d59 | 486 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 487 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 488 | void SPI_TFT_ILI9341::circle(int x0, int y0, int r, int color) |
2018US_EminaTahirovi | 0:1b3481785d59 | 489 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 490 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 491 | int x = -r, y = 0, err = 2-2*r, e2; |
2018US_EminaTahirovi | 0:1b3481785d59 | 492 | do { |
2018US_EminaTahirovi | 0:1b3481785d59 | 493 | pixel(x0-x, y0+y,color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 494 | pixel(x0+x, y0+y,color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 495 | pixel(x0+x, y0-y,color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 496 | pixel(x0-x, y0-y,color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 497 | e2 = err; |
2018US_EminaTahirovi | 0:1b3481785d59 | 498 | if (e2 <= y) { |
2018US_EminaTahirovi | 0:1b3481785d59 | 499 | err += ++y*2+1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 500 | if (-x == y && e2 <= x) e2 = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 501 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 502 | if (e2 > x) err += ++x*2+1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 503 | } while (x <= 0); |
2018US_EminaTahirovi | 0:1b3481785d59 | 504 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 505 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 506 | void SPI_TFT_ILI9341::fillcircle(int x0, int y0, int r, int color) |
2018US_EminaTahirovi | 0:1b3481785d59 | 507 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 508 | int x = -r, y = 0, err = 2-2*r, e2; |
2018US_EminaTahirovi | 0:1b3481785d59 | 509 | do { |
2018US_EminaTahirovi | 0:1b3481785d59 | 510 | vline(x0-x, y0-y, y0+y, color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 511 | vline(x0+x, y0-y, y0+y, color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 512 | e2 = err; |
2018US_EminaTahirovi | 0:1b3481785d59 | 513 | if (e2 <= y) { |
2018US_EminaTahirovi | 0:1b3481785d59 | 514 | err += ++y*2+1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 515 | if (-x == y && e2 <= x) e2 = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 516 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 517 | if (e2 > x) err += ++x*2+1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 518 | } while (x <= 0); |
2018US_EminaTahirovi | 0:1b3481785d59 | 519 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 520 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 521 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 522 | // optimized for speed |
2018US_EminaTahirovi | 0:1b3481785d59 | 523 | void SPI_TFT_ILI9341::hline(int x0, int x1, int y, int color) |
2018US_EminaTahirovi | 0:1b3481785d59 | 524 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 525 | int w,j; |
2018US_EminaTahirovi | 0:1b3481785d59 | 526 | w = x1 - x0 + 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 527 | window(x0,y,w,1); |
2018US_EminaTahirovi | 0:1b3481785d59 | 528 | _dc = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 529 | _cs = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 530 | f_write(0x2C); // send pixel |
2018US_EminaTahirovi | 0:1b3481785d59 | 531 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 532 | _dc = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 533 | spi_16(1); |
2018US_EminaTahirovi | 0:1b3481785d59 | 534 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 535 | for (j=0; j<w; j++) { |
2018US_EminaTahirovi | 0:1b3481785d59 | 536 | f_write(color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 537 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 538 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 539 | spi_16(0); |
2018US_EminaTahirovi | 0:1b3481785d59 | 540 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 541 | WindowMax(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 542 | return; |
2018US_EminaTahirovi | 0:1b3481785d59 | 543 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 544 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 545 | // optimized for speed |
2018US_EminaTahirovi | 0:1b3481785d59 | 546 | void SPI_TFT_ILI9341::vline(int x, int y0, int y1, int color) |
2018US_EminaTahirovi | 0:1b3481785d59 | 547 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 548 | int h,y; |
2018US_EminaTahirovi | 0:1b3481785d59 | 549 | h = y1 - y0 + 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 550 | window(x,y0,1,h); |
2018US_EminaTahirovi | 0:1b3481785d59 | 551 | _dc = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 552 | _cs = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 553 | f_write(0x2C); // send pixel |
2018US_EminaTahirovi | 0:1b3481785d59 | 554 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 555 | _dc = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 556 | spi_16(1); |
2018US_EminaTahirovi | 0:1b3481785d59 | 557 | // switch to 16 bit Mode 3 |
2018US_EminaTahirovi | 0:1b3481785d59 | 558 | for (y=0; y<h; y++) { |
2018US_EminaTahirovi | 0:1b3481785d59 | 559 | f_write(color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 560 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 561 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 562 | spi_16(0); |
2018US_EminaTahirovi | 0:1b3481785d59 | 563 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 564 | WindowMax(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 565 | return; |
2018US_EminaTahirovi | 0:1b3481785d59 | 566 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 567 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 568 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 569 | void SPI_TFT_ILI9341::line(int x0, int y0, int x1, int y1, int color) |
2018US_EminaTahirovi | 0:1b3481785d59 | 570 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 571 | //WindowMax(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 572 | int dx = 0, dy = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 573 | int dx_sym = 0, dy_sym = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 574 | int dx_x2 = 0, dy_x2 = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 575 | int di = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 576 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 577 | dx = x1-x0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 578 | dy = y1-y0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 579 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 580 | if (dx == 0) { /* vertical line */ |
2018US_EminaTahirovi | 0:1b3481785d59 | 581 | if (y1 > y0) vline(x0,y0,y1,color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 582 | else vline(x0,y1,y0,color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 583 | return; |
2018US_EminaTahirovi | 0:1b3481785d59 | 584 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 585 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 586 | if (dx > 0) { |
2018US_EminaTahirovi | 0:1b3481785d59 | 587 | dx_sym = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 588 | } else { |
2018US_EminaTahirovi | 0:1b3481785d59 | 589 | dx_sym = -1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 590 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 591 | if (dy == 0) { /* horizontal line */ |
2018US_EminaTahirovi | 0:1b3481785d59 | 592 | if (x1 > x0) hline(x0,x1,y0,color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 593 | else hline(x1,x0,y0,color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 594 | return; |
2018US_EminaTahirovi | 0:1b3481785d59 | 595 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 596 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 597 | if (dy > 0) { |
2018US_EminaTahirovi | 0:1b3481785d59 | 598 | dy_sym = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 599 | } else { |
2018US_EminaTahirovi | 0:1b3481785d59 | 600 | dy_sym = -1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 601 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 602 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 603 | dx = dx_sym*dx; |
2018US_EminaTahirovi | 0:1b3481785d59 | 604 | dy = dy_sym*dy; |
2018US_EminaTahirovi | 0:1b3481785d59 | 605 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 606 | dx_x2 = dx*2; |
2018US_EminaTahirovi | 0:1b3481785d59 | 607 | dy_x2 = dy*2; |
2018US_EminaTahirovi | 0:1b3481785d59 | 608 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 609 | if (dx >= dy) { |
2018US_EminaTahirovi | 0:1b3481785d59 | 610 | di = dy_x2 - dx; |
2018US_EminaTahirovi | 0:1b3481785d59 | 611 | while (x0 != x1) { |
2018US_EminaTahirovi | 0:1b3481785d59 | 612 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 613 | pixel(x0, y0, color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 614 | x0 += dx_sym; |
2018US_EminaTahirovi | 0:1b3481785d59 | 615 | if (di<0) { |
2018US_EminaTahirovi | 0:1b3481785d59 | 616 | di += dy_x2; |
2018US_EminaTahirovi | 0:1b3481785d59 | 617 | } else { |
2018US_EminaTahirovi | 0:1b3481785d59 | 618 | di += dy_x2 - dx_x2; |
2018US_EminaTahirovi | 0:1b3481785d59 | 619 | y0 += dy_sym; |
2018US_EminaTahirovi | 0:1b3481785d59 | 620 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 621 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 622 | pixel(x0, y0, color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 623 | } else { |
2018US_EminaTahirovi | 0:1b3481785d59 | 624 | di = dx_x2 - dy; |
2018US_EminaTahirovi | 0:1b3481785d59 | 625 | while (y0 != y1) { |
2018US_EminaTahirovi | 0:1b3481785d59 | 626 | pixel(x0, y0, color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 627 | y0 += dy_sym; |
2018US_EminaTahirovi | 0:1b3481785d59 | 628 | if (di < 0) { |
2018US_EminaTahirovi | 0:1b3481785d59 | 629 | di += dx_x2; |
2018US_EminaTahirovi | 0:1b3481785d59 | 630 | } else { |
2018US_EminaTahirovi | 0:1b3481785d59 | 631 | di += dx_x2 - dy_x2; |
2018US_EminaTahirovi | 0:1b3481785d59 | 632 | x0 += dx_sym; |
2018US_EminaTahirovi | 0:1b3481785d59 | 633 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 634 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 635 | pixel(x0, y0, color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 636 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 637 | return; |
2018US_EminaTahirovi | 0:1b3481785d59 | 638 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 639 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 640 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 641 | void SPI_TFT_ILI9341::rect(int x0, int y0, int x1, int y1, int color) |
2018US_EminaTahirovi | 0:1b3481785d59 | 642 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 643 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 644 | if (x1 > x0) hline(x0,x1,y0,color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 645 | else hline(x1,x0,y0,color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 646 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 647 | if (y1 > y0) vline(x0,y0,y1,color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 648 | else vline(x0,y1,y0,color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 649 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 650 | if (x1 > x0) hline(x0,x1,y1,color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 651 | else hline(x1,x0,y1,color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 652 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 653 | if (y1 > y0) vline(x1,y0,y1,color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 654 | else vline(x1,y1,y0,color); |
2018US_EminaTahirovi | 0:1b3481785d59 | 655 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 656 | return; |
2018US_EminaTahirovi | 0:1b3481785d59 | 657 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 658 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 659 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 660 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 661 | // optimized for speed |
2018US_EminaTahirovi | 0:1b3481785d59 | 662 | // use DMA |
2018US_EminaTahirovi | 0:1b3481785d59 | 663 | void SPI_TFT_ILI9341::fillrect(int x0, int y0, int x1, int y1, int color) |
2018US_EminaTahirovi | 0:1b3481785d59 | 664 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 665 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 666 | int h = y1 - y0 + 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 667 | int w = x1 - x0 + 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 668 | int pixel = h * w; |
2018US_EminaTahirovi | 0:1b3481785d59 | 669 | unsigned int dma_transfer; |
2018US_EminaTahirovi | 0:1b3481785d59 | 670 | window(x0,y0,w,h); |
2018US_EminaTahirovi | 0:1b3481785d59 | 671 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 672 | wr_cmd(0x2C); // send pixel |
2018US_EminaTahirovi | 0:1b3481785d59 | 673 | spi_16(1); |
2018US_EminaTahirovi | 0:1b3481785d59 | 674 | DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) &color; |
2018US_EminaTahirovi | 0:1b3481785d59 | 675 | DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; |
2018US_EminaTahirovi | 0:1b3481785d59 | 676 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 677 | switch(spi_num){ // decide which SPI is to use |
2018US_EminaTahirovi | 0:1b3481785d59 | 678 | case (1): |
2018US_EminaTahirovi | 0:1b3481785d59 | 679 | DMA_Init(DMA1_Channel3, &DMA_InitStructure); // init the DMA |
2018US_EminaTahirovi | 0:1b3481785d59 | 680 | do{ |
2018US_EminaTahirovi | 0:1b3481785d59 | 681 | if(pixel < 0x10000) { |
2018US_EminaTahirovi | 0:1b3481785d59 | 682 | dma_transfer = pixel; |
2018US_EminaTahirovi | 0:1b3481785d59 | 683 | pixel = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 684 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 685 | else { |
2018US_EminaTahirovi | 0:1b3481785d59 | 686 | dma_transfer = 0xffff; |
2018US_EminaTahirovi | 0:1b3481785d59 | 687 | pixel = pixel - 0xffff; |
2018US_EminaTahirovi | 0:1b3481785d59 | 688 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 689 | DMA_SetCurrDataCounter(DMA1_Channel3, dma_transfer); |
2018US_EminaTahirovi | 0:1b3481785d59 | 690 | SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx,ENABLE); |
2018US_EminaTahirovi | 0:1b3481785d59 | 691 | DMA_Cmd(DMA1_Channel3, ENABLE); |
2018US_EminaTahirovi | 0:1b3481785d59 | 692 | while(DMA_GetCurrDataCounter(DMA1_Channel3) != 0); // wait for end of transfer |
2018US_EminaTahirovi | 0:1b3481785d59 | 693 | DMA_Cmd(DMA1_Channel3, DISABLE); |
2018US_EminaTahirovi | 0:1b3481785d59 | 694 | }while(pixel > 0); |
2018US_EminaTahirovi | 0:1b3481785d59 | 695 | break; |
2018US_EminaTahirovi | 0:1b3481785d59 | 696 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 697 | case (2): |
2018US_EminaTahirovi | 0:1b3481785d59 | 698 | DMA_Init(DMA1_Channel5, &DMA_InitStructure); // init the DMA |
2018US_EminaTahirovi | 0:1b3481785d59 | 699 | do{ |
2018US_EminaTahirovi | 0:1b3481785d59 | 700 | if(pixel < 0x10000) { |
2018US_EminaTahirovi | 0:1b3481785d59 | 701 | dma_transfer = pixel; |
2018US_EminaTahirovi | 0:1b3481785d59 | 702 | pixel = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 703 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 704 | else { |
2018US_EminaTahirovi | 0:1b3481785d59 | 705 | dma_transfer = 0xffff; |
2018US_EminaTahirovi | 0:1b3481785d59 | 706 | pixel = pixel - 0xffff; |
2018US_EminaTahirovi | 0:1b3481785d59 | 707 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 708 | DMA_SetCurrDataCounter(DMA1_Channel5, dma_transfer); |
2018US_EminaTahirovi | 0:1b3481785d59 | 709 | SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx,ENABLE); |
2018US_EminaTahirovi | 0:1b3481785d59 | 710 | DMA_Cmd(DMA1_Channel5, ENABLE); |
2018US_EminaTahirovi | 0:1b3481785d59 | 711 | while(DMA_GetCurrDataCounter(DMA1_Channel5) != 0); // wait for end of transfer |
2018US_EminaTahirovi | 0:1b3481785d59 | 712 | DMA_Cmd(DMA1_Channel5, DISABLE); |
2018US_EminaTahirovi | 0:1b3481785d59 | 713 | }while(pixel > 0); |
2018US_EminaTahirovi | 0:1b3481785d59 | 714 | break; |
2018US_EminaTahirovi | 0:1b3481785d59 | 715 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 716 | case (3): |
2018US_EminaTahirovi | 0:1b3481785d59 | 717 | DMA_Init(DMA2_Channel2, &DMA_InitStructure); // init the DMA |
2018US_EminaTahirovi | 0:1b3481785d59 | 718 | do{ |
2018US_EminaTahirovi | 0:1b3481785d59 | 719 | if(pixel < 0x10000) { |
2018US_EminaTahirovi | 0:1b3481785d59 | 720 | dma_transfer = pixel; |
2018US_EminaTahirovi | 0:1b3481785d59 | 721 | pixel = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 722 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 723 | else { |
2018US_EminaTahirovi | 0:1b3481785d59 | 724 | dma_transfer = 0xffff; |
2018US_EminaTahirovi | 0:1b3481785d59 | 725 | pixel = pixel - 0xffff; |
2018US_EminaTahirovi | 0:1b3481785d59 | 726 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 727 | DMA_SetCurrDataCounter(DMA2_Channel2, dma_transfer); |
2018US_EminaTahirovi | 0:1b3481785d59 | 728 | SPI_I2S_DMACmd(SPI3, SPI_I2S_DMAReq_Tx,ENABLE); |
2018US_EminaTahirovi | 0:1b3481785d59 | 729 | DMA_Cmd(DMA2_Channel2, ENABLE); |
2018US_EminaTahirovi | 0:1b3481785d59 | 730 | while(DMA_GetCurrDataCounter(DMA2_Channel2) != 0); // wait for end of transfer |
2018US_EminaTahirovi | 0:1b3481785d59 | 731 | DMA_Cmd(DMA2_Channel2, DISABLE); |
2018US_EminaTahirovi | 0:1b3481785d59 | 732 | }while(pixel > 0); |
2018US_EminaTahirovi | 0:1b3481785d59 | 733 | break; |
2018US_EminaTahirovi | 0:1b3481785d59 | 734 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 735 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 736 | spi_16(0); |
2018US_EminaTahirovi | 0:1b3481785d59 | 737 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 738 | WindowMax(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 739 | return; |
2018US_EminaTahirovi | 0:1b3481785d59 | 740 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 741 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 742 | void SPI_TFT_ILI9341::locate(int x, int y) |
2018US_EminaTahirovi | 0:1b3481785d59 | 743 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 744 | char_x = x; |
2018US_EminaTahirovi | 0:1b3481785d59 | 745 | char_y = y; |
2018US_EminaTahirovi | 0:1b3481785d59 | 746 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 747 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 748 | int SPI_TFT_ILI9341::columns() |
2018US_EminaTahirovi | 0:1b3481785d59 | 749 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 750 | return width() / font[1]; |
2018US_EminaTahirovi | 0:1b3481785d59 | 751 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 752 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 753 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 754 | int SPI_TFT_ILI9341::rows() |
2018US_EminaTahirovi | 0:1b3481785d59 | 755 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 756 | return height() / font[2]; |
2018US_EminaTahirovi | 0:1b3481785d59 | 757 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 758 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 759 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 760 | int SPI_TFT_ILI9341::_putc(int value) |
2018US_EminaTahirovi | 0:1b3481785d59 | 761 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 762 | if (value == '\n') { // new line |
2018US_EminaTahirovi | 0:1b3481785d59 | 763 | char_x = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 764 | char_y = char_y + font[2]; |
2018US_EminaTahirovi | 0:1b3481785d59 | 765 | if (char_y >= height() - font[2]) { |
2018US_EminaTahirovi | 0:1b3481785d59 | 766 | char_y = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 767 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 768 | } else { |
2018US_EminaTahirovi | 0:1b3481785d59 | 769 | character(char_x, char_y, value); |
2018US_EminaTahirovi | 0:1b3481785d59 | 770 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 771 | return value; |
2018US_EminaTahirovi | 0:1b3481785d59 | 772 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 773 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 774 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 775 | // speed optimized |
2018US_EminaTahirovi | 0:1b3481785d59 | 776 | // will use dma |
2018US_EminaTahirovi | 0:1b3481785d59 | 777 | void SPI_TFT_ILI9341::character(int x, int y, int c) |
2018US_EminaTahirovi | 0:1b3481785d59 | 778 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 779 | unsigned int hor,vert,offset,bpl,j,i,b; |
2018US_EminaTahirovi | 0:1b3481785d59 | 780 | unsigned char* zeichen; |
2018US_EminaTahirovi | 0:1b3481785d59 | 781 | unsigned char z,w; |
2018US_EminaTahirovi | 0:1b3481785d59 | 782 | #ifdef use_ram |
2018US_EminaTahirovi | 0:1b3481785d59 | 783 | unsigned int pixel; |
2018US_EminaTahirovi | 0:1b3481785d59 | 784 | unsigned int p; |
2018US_EminaTahirovi | 0:1b3481785d59 | 785 | unsigned int dma_count,dma_off; |
2018US_EminaTahirovi | 0:1b3481785d59 | 786 | uint16_t *buffer; |
2018US_EminaTahirovi | 0:1b3481785d59 | 787 | #endif |
2018US_EminaTahirovi | 0:1b3481785d59 | 788 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 789 | if ((c < 31) || (c > 127)) return; // test char range |
2018US_EminaTahirovi | 0:1b3481785d59 | 790 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 791 | // read font parameter from start of array |
2018US_EminaTahirovi | 0:1b3481785d59 | 792 | offset = font[0]; // bytes / char |
2018US_EminaTahirovi | 0:1b3481785d59 | 793 | hor = font[1]; // get hor size of font |
2018US_EminaTahirovi | 0:1b3481785d59 | 794 | vert = font[2]; // get vert size of font |
2018US_EminaTahirovi | 0:1b3481785d59 | 795 | bpl = font[3]; // bytes per line |
2018US_EminaTahirovi | 0:1b3481785d59 | 796 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 797 | if (char_x + hor > width()) { |
2018US_EminaTahirovi | 0:1b3481785d59 | 798 | char_x = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 799 | char_y = char_y + vert; |
2018US_EminaTahirovi | 0:1b3481785d59 | 800 | if (char_y >= height() - font[2]) { |
2018US_EminaTahirovi | 0:1b3481785d59 | 801 | char_y = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 802 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 803 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 804 | window(char_x, char_y,hor,vert); // setup char box |
2018US_EminaTahirovi | 0:1b3481785d59 | 805 | wr_cmd(0x2C); |
2018US_EminaTahirovi | 0:1b3481785d59 | 806 | spi_16(1); // switch to 16 bit Mode |
2018US_EminaTahirovi | 0:1b3481785d59 | 807 | #ifdef use_ram |
2018US_EminaTahirovi | 0:1b3481785d59 | 808 | pixel = hor * vert; // calculate buffer size |
2018US_EminaTahirovi | 0:1b3481785d59 | 809 | buffer = (uint16_t *) malloc (2*pixel); // we need a buffer for the font |
2018US_EminaTahirovi | 0:1b3481785d59 | 810 | if(buffer != NULL) { // there is memory space -> use dma |
2018US_EminaTahirovi | 0:1b3481785d59 | 811 | zeichen = &font[((c -32) * offset) + 4]; // start of char bitmap |
2018US_EminaTahirovi | 0:1b3481785d59 | 812 | w = zeichen[0]; // width of actual char |
2018US_EminaTahirovi | 0:1b3481785d59 | 813 | p = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 814 | // construct the font into the buffer |
2018US_EminaTahirovi | 0:1b3481785d59 | 815 | for (j=0; j<vert; j++) { // vert line |
2018US_EminaTahirovi | 0:1b3481785d59 | 816 | for (i=0; i<hor; i++) { // horz line |
2018US_EminaTahirovi | 0:1b3481785d59 | 817 | z = zeichen[bpl * i + ((j & 0xF8) >> 3)+1]; |
2018US_EminaTahirovi | 0:1b3481785d59 | 818 | b = 1 << (j & 0x07); |
2018US_EminaTahirovi | 0:1b3481785d59 | 819 | if (( z & b ) == 0x00) { |
2018US_EminaTahirovi | 0:1b3481785d59 | 820 | buffer[p] = _background; |
2018US_EminaTahirovi | 0:1b3481785d59 | 821 | } else { |
2018US_EminaTahirovi | 0:1b3481785d59 | 822 | buffer[p] = _foreground; |
2018US_EminaTahirovi | 0:1b3481785d59 | 823 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 824 | p++; |
2018US_EminaTahirovi | 0:1b3481785d59 | 825 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 826 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 827 | // copy the buffer with DMA SPI to display |
2018US_EminaTahirovi | 0:1b3481785d59 | 828 | dma_off = 0; // offset for DMA transfer |
2018US_EminaTahirovi | 0:1b3481785d59 | 829 | DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) (buffer + dma_off); |
2018US_EminaTahirovi | 0:1b3481785d59 | 830 | DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; |
2018US_EminaTahirovi | 0:1b3481785d59 | 831 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 832 | switch(spi_num){ // decide which SPI is to use |
2018US_EminaTahirovi | 0:1b3481785d59 | 833 | case (1): |
2018US_EminaTahirovi | 0:1b3481785d59 | 834 | DMA_Init(DMA1_Channel3, &DMA_InitStructure); // init the DMA |
2018US_EminaTahirovi | 0:1b3481785d59 | 835 | // start DMA |
2018US_EminaTahirovi | 0:1b3481785d59 | 836 | do { |
2018US_EminaTahirovi | 0:1b3481785d59 | 837 | if (pixel > 0X10000) { // this is a giant font ! |
2018US_EminaTahirovi | 0:1b3481785d59 | 838 | dma_count = 0Xffff; |
2018US_EminaTahirovi | 0:1b3481785d59 | 839 | pixel = pixel - 0Xffff; |
2018US_EminaTahirovi | 0:1b3481785d59 | 840 | } else { |
2018US_EminaTahirovi | 0:1b3481785d59 | 841 | dma_count = pixel; |
2018US_EminaTahirovi | 0:1b3481785d59 | 842 | pixel = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 843 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 844 | DMA_SetCurrDataCounter(DMA1_Channel3, dma_count); |
2018US_EminaTahirovi | 0:1b3481785d59 | 845 | SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx,ENABLE); |
2018US_EminaTahirovi | 0:1b3481785d59 | 846 | DMA_Cmd(DMA1_Channel3, ENABLE); |
2018US_EminaTahirovi | 0:1b3481785d59 | 847 | while(DMA_GetCurrDataCounter(DMA1_Channel3) != 0); // wait for end of transfer |
2018US_EminaTahirovi | 0:1b3481785d59 | 848 | DMA_Cmd(DMA1_Channel3, DISABLE); |
2018US_EminaTahirovi | 0:1b3481785d59 | 849 | }while(pixel > 0); |
2018US_EminaTahirovi | 0:1b3481785d59 | 850 | break; |
2018US_EminaTahirovi | 0:1b3481785d59 | 851 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 852 | case (2): |
2018US_EminaTahirovi | 0:1b3481785d59 | 853 | DMA_Init(DMA1_Channel5, &DMA_InitStructure); // init the DMA |
2018US_EminaTahirovi | 0:1b3481785d59 | 854 | // start DMA |
2018US_EminaTahirovi | 0:1b3481785d59 | 855 | do { |
2018US_EminaTahirovi | 0:1b3481785d59 | 856 | if (pixel > 0X10000) { // this is a giant font ! |
2018US_EminaTahirovi | 0:1b3481785d59 | 857 | dma_count = 0Xffff; |
2018US_EminaTahirovi | 0:1b3481785d59 | 858 | pixel = pixel - 0Xffff; |
2018US_EminaTahirovi | 0:1b3481785d59 | 859 | } else { |
2018US_EminaTahirovi | 0:1b3481785d59 | 860 | dma_count = pixel; |
2018US_EminaTahirovi | 0:1b3481785d59 | 861 | pixel = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 862 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 863 | DMA_SetCurrDataCounter(DMA1_Channel5, dma_count); |
2018US_EminaTahirovi | 0:1b3481785d59 | 864 | SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx,ENABLE); |
2018US_EminaTahirovi | 0:1b3481785d59 | 865 | DMA_Cmd(DMA1_Channel5, ENABLE); |
2018US_EminaTahirovi | 0:1b3481785d59 | 866 | while(DMA_GetCurrDataCounter(DMA1_Channel5) != 0); // wait for end of transfer |
2018US_EminaTahirovi | 0:1b3481785d59 | 867 | DMA_Cmd(DMA1_Channel5, DISABLE); |
2018US_EminaTahirovi | 0:1b3481785d59 | 868 | }while(pixel > 0); |
2018US_EminaTahirovi | 0:1b3481785d59 | 869 | break; |
2018US_EminaTahirovi | 0:1b3481785d59 | 870 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 871 | case (3): |
2018US_EminaTahirovi | 0:1b3481785d59 | 872 | DMA_Init(DMA2_Channel2, &DMA_InitStructure); // init the DMA |
2018US_EminaTahirovi | 0:1b3481785d59 | 873 | // start DMA |
2018US_EminaTahirovi | 0:1b3481785d59 | 874 | do { |
2018US_EminaTahirovi | 0:1b3481785d59 | 875 | if (pixel > 0X10000) { // this is a giant font ! |
2018US_EminaTahirovi | 0:1b3481785d59 | 876 | dma_count = 0Xffff; |
2018US_EminaTahirovi | 0:1b3481785d59 | 877 | pixel = pixel - 0Xffff; |
2018US_EminaTahirovi | 0:1b3481785d59 | 878 | } else { |
2018US_EminaTahirovi | 0:1b3481785d59 | 879 | dma_count = pixel; |
2018US_EminaTahirovi | 0:1b3481785d59 | 880 | pixel = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 881 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 882 | DMA_SetCurrDataCounter(DMA2_Channel2, dma_count); |
2018US_EminaTahirovi | 0:1b3481785d59 | 883 | SPI_I2S_DMACmd(SPI3, SPI_I2S_DMAReq_Tx,ENABLE); |
2018US_EminaTahirovi | 0:1b3481785d59 | 884 | DMA_Cmd(DMA2_Channel2, ENABLE); |
2018US_EminaTahirovi | 0:1b3481785d59 | 885 | while(DMA_GetCurrDataCounter(DMA2_Channel2) != 0); // wait for end of transfer |
2018US_EminaTahirovi | 0:1b3481785d59 | 886 | DMA_Cmd(DMA2_Channel2, DISABLE); |
2018US_EminaTahirovi | 0:1b3481785d59 | 887 | }while(pixel > 0); |
2018US_EminaTahirovi | 0:1b3481785d59 | 888 | break; |
2018US_EminaTahirovi | 0:1b3481785d59 | 889 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 890 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 891 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 892 | free ((uint16_t *) buffer); |
2018US_EminaTahirovi | 0:1b3481785d59 | 893 | spi_16(0); |
2018US_EminaTahirovi | 0:1b3481785d59 | 894 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 895 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 896 | else{ |
2018US_EminaTahirovi | 0:1b3481785d59 | 897 | #endif |
2018US_EminaTahirovi | 0:1b3481785d59 | 898 | zeichen = &font[((c -32) * offset) + 4]; // start of char bitmap |
2018US_EminaTahirovi | 0:1b3481785d59 | 899 | w = zeichen[0]; // width of actual char |
2018US_EminaTahirovi | 0:1b3481785d59 | 900 | for (j=0; j<vert; j++) { // vert line |
2018US_EminaTahirovi | 0:1b3481785d59 | 901 | for (i=0; i<hor; i++) { // horz line |
2018US_EminaTahirovi | 0:1b3481785d59 | 902 | z = zeichen[bpl * i + ((j & 0xF8) >> 3)+1]; |
2018US_EminaTahirovi | 0:1b3481785d59 | 903 | b = 1 << (j & 0x07); |
2018US_EminaTahirovi | 0:1b3481785d59 | 904 | if (( z & b ) == 0x00) { |
2018US_EminaTahirovi | 0:1b3481785d59 | 905 | f_write(_background); |
2018US_EminaTahirovi | 0:1b3481785d59 | 906 | } else { |
2018US_EminaTahirovi | 0:1b3481785d59 | 907 | f_write(_foreground); |
2018US_EminaTahirovi | 0:1b3481785d59 | 908 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 909 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 910 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 911 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 912 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 913 | spi_16(0); |
2018US_EminaTahirovi | 0:1b3481785d59 | 914 | #ifdef use_ram |
2018US_EminaTahirovi | 0:1b3481785d59 | 915 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 916 | #endif |
2018US_EminaTahirovi | 0:1b3481785d59 | 917 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 918 | WindowMax(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 919 | if ((w + 2) < hor) { // x offset to next char |
2018US_EminaTahirovi | 0:1b3481785d59 | 920 | char_x += w + 2; |
2018US_EminaTahirovi | 0:1b3481785d59 | 921 | } else char_x += hor; |
2018US_EminaTahirovi | 0:1b3481785d59 | 922 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 923 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 924 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 925 | void SPI_TFT_ILI9341::set_font(unsigned char* f) |
2018US_EminaTahirovi | 0:1b3481785d59 | 926 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 927 | font = f; |
2018US_EminaTahirovi | 0:1b3481785d59 | 928 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 929 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 930 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 931 | void SPI_TFT_ILI9341::Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap) |
2018US_EminaTahirovi | 0:1b3481785d59 | 932 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 933 | unsigned int j; |
2018US_EminaTahirovi | 0:1b3481785d59 | 934 | int padd; |
2018US_EminaTahirovi | 0:1b3481785d59 | 935 | unsigned short *bitmap_ptr = (unsigned short *)bitmap; |
2018US_EminaTahirovi | 0:1b3481785d59 | 936 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 937 | unsigned int i; |
2018US_EminaTahirovi | 0:1b3481785d59 | 938 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 939 | // the lines are padded to multiple of 4 bytes in a bitmap |
2018US_EminaTahirovi | 0:1b3481785d59 | 940 | padd = -1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 941 | do { |
2018US_EminaTahirovi | 0:1b3481785d59 | 942 | padd ++; |
2018US_EminaTahirovi | 0:1b3481785d59 | 943 | } while (2*(w + padd)%4 != 0); |
2018US_EminaTahirovi | 0:1b3481785d59 | 944 | window(x, y, w, h); |
2018US_EminaTahirovi | 0:1b3481785d59 | 945 | bitmap_ptr += ((h - 1)* (w + padd)); |
2018US_EminaTahirovi | 0:1b3481785d59 | 946 | wr_cmd(0x2C); // send pixel |
2018US_EminaTahirovi | 0:1b3481785d59 | 947 | spi_16(1); |
2018US_EminaTahirovi | 0:1b3481785d59 | 948 | for (j = 0; j < h; j++) { //Lines |
2018US_EminaTahirovi | 0:1b3481785d59 | 949 | for (i = 0; i < w; i++) { // one line |
2018US_EminaTahirovi | 0:1b3481785d59 | 950 | f_write(*bitmap_ptr); // one line |
2018US_EminaTahirovi | 0:1b3481785d59 | 951 | bitmap_ptr++; |
2018US_EminaTahirovi | 0:1b3481785d59 | 952 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 953 | bitmap_ptr -= 2*w; |
2018US_EminaTahirovi | 0:1b3481785d59 | 954 | bitmap_ptr -= padd; |
2018US_EminaTahirovi | 0:1b3481785d59 | 955 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 956 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 957 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 958 | spi_16(0); |
2018US_EminaTahirovi | 0:1b3481785d59 | 959 | WindowMax(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 960 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 961 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 962 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 963 | // local filesystem is not implemented but you can add a SD card to a different SPI |
2018US_EminaTahirovi | 0:1b3481785d59 | 964 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 965 | int SPI_TFT_ILI9341::BMP_16(unsigned int x, unsigned int y, const char *Name_BMP) |
2018US_EminaTahirovi | 0:1b3481785d59 | 966 | { |
2018US_EminaTahirovi | 0:1b3481785d59 | 967 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 968 | #define OffsetPixelWidth 18 |
2018US_EminaTahirovi | 0:1b3481785d59 | 969 | #define OffsetPixelHeigh 22 |
2018US_EminaTahirovi | 0:1b3481785d59 | 970 | #define OffsetFileSize 34 |
2018US_EminaTahirovi | 0:1b3481785d59 | 971 | #define OffsetPixData 10 |
2018US_EminaTahirovi | 0:1b3481785d59 | 972 | #define OffsetBPP 28 |
2018US_EminaTahirovi | 0:1b3481785d59 | 973 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 974 | char filename[50]; |
2018US_EminaTahirovi | 0:1b3481785d59 | 975 | unsigned char BMP_Header[54]; |
2018US_EminaTahirovi | 0:1b3481785d59 | 976 | unsigned short BPP_t; |
2018US_EminaTahirovi | 0:1b3481785d59 | 977 | unsigned int PixelWidth,PixelHeigh,start_data; |
2018US_EminaTahirovi | 0:1b3481785d59 | 978 | unsigned int i,off; |
2018US_EminaTahirovi | 0:1b3481785d59 | 979 | int padd,j; |
2018US_EminaTahirovi | 0:1b3481785d59 | 980 | unsigned short *line; |
2018US_EminaTahirovi | 0:1b3481785d59 | 981 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 982 | // get the filename |
2018US_EminaTahirovi | 0:1b3481785d59 | 983 | i=0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 984 | while (*Name_BMP!='\0') { |
2018US_EminaTahirovi | 0:1b3481785d59 | 985 | filename[i++]=*Name_BMP++; |
2018US_EminaTahirovi | 0:1b3481785d59 | 986 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 987 | filename[i] = 0; |
2018US_EminaTahirovi | 0:1b3481785d59 | 988 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 989 | FILE *Image = fopen((const char *)&filename[0], "rb"); // open the bmp file |
2018US_EminaTahirovi | 0:1b3481785d59 | 990 | if (!Image) { |
2018US_EminaTahirovi | 0:1b3481785d59 | 991 | return(0); // error file not found ! |
2018US_EminaTahirovi | 0:1b3481785d59 | 992 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 993 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 994 | fread(&BMP_Header[0],1,54,Image); // get the BMP Header |
2018US_EminaTahirovi | 0:1b3481785d59 | 995 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 996 | if (BMP_Header[0] != 0x42 || BMP_Header[1] != 0x4D) { // check magic byte |
2018US_EminaTahirovi | 0:1b3481785d59 | 997 | fclose(Image); |
2018US_EminaTahirovi | 0:1b3481785d59 | 998 | return(-1); // error no BMP file |
2018US_EminaTahirovi | 0:1b3481785d59 | 999 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 1000 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 1001 | BPP_t = BMP_Header[OffsetBPP] + (BMP_Header[OffsetBPP + 1] << 8); |
2018US_EminaTahirovi | 0:1b3481785d59 | 1002 | if (BPP_t != 0x0010) { |
2018US_EminaTahirovi | 0:1b3481785d59 | 1003 | fclose(Image); |
2018US_EminaTahirovi | 0:1b3481785d59 | 1004 | return(-2); // error no 16 bit BMP |
2018US_EminaTahirovi | 0:1b3481785d59 | 1005 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 1006 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 1007 | PixelHeigh = BMP_Header[OffsetPixelHeigh] + (BMP_Header[OffsetPixelHeigh + 1] << 8) + (BMP_Header[OffsetPixelHeigh + 2] << 16) + (BMP_Header[OffsetPixelHeigh + 3] << 24); |
2018US_EminaTahirovi | 0:1b3481785d59 | 1008 | PixelWidth = BMP_Header[OffsetPixelWidth] + (BMP_Header[OffsetPixelWidth + 1] << 8) + (BMP_Header[OffsetPixelWidth + 2] << 16) + (BMP_Header[OffsetPixelWidth + 3] << 24); |
2018US_EminaTahirovi | 0:1b3481785d59 | 1009 | if (PixelHeigh > height() + y || PixelWidth > width() + x) { |
2018US_EminaTahirovi | 0:1b3481785d59 | 1010 | fclose(Image); |
2018US_EminaTahirovi | 0:1b3481785d59 | 1011 | return(-3); // to big |
2018US_EminaTahirovi | 0:1b3481785d59 | 1012 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 1013 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 1014 | start_data = BMP_Header[OffsetPixData] + (BMP_Header[OffsetPixData + 1] << 8) + (BMP_Header[OffsetPixData + 2] << 16) + (BMP_Header[OffsetPixData + 3] << 24); |
2018US_EminaTahirovi | 0:1b3481785d59 | 1015 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 1016 | line = (unsigned short *) malloc (2 * PixelWidth); // we need a buffer for a line |
2018US_EminaTahirovi | 0:1b3481785d59 | 1017 | if (line == NULL) { |
2018US_EminaTahirovi | 0:1b3481785d59 | 1018 | return(-4); // error no memory |
2018US_EminaTahirovi | 0:1b3481785d59 | 1019 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 1020 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 1021 | // the bmp lines are padded to multiple of 4 bytes |
2018US_EminaTahirovi | 0:1b3481785d59 | 1022 | padd = -1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 1023 | do { |
2018US_EminaTahirovi | 0:1b3481785d59 | 1024 | padd ++; |
2018US_EminaTahirovi | 0:1b3481785d59 | 1025 | } while ((PixelWidth * 2 + padd)%4 != 0); |
2018US_EminaTahirovi | 0:1b3481785d59 | 1026 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 1027 | window(x, y,PixelWidth ,PixelHeigh); |
2018US_EminaTahirovi | 0:1b3481785d59 | 1028 | wr_cmd(0x2C); // send pixel |
2018US_EminaTahirovi | 0:1b3481785d59 | 1029 | spi_16(1); |
2018US_EminaTahirovi | 0:1b3481785d59 | 1030 | for (j = PixelHeigh - 1; j >= 0; j--) { //Lines bottom up |
2018US_EminaTahirovi | 0:1b3481785d59 | 1031 | off = j * (PixelWidth * 2 + padd) + start_data; // start of line |
2018US_EminaTahirovi | 0:1b3481785d59 | 1032 | fseek(Image, off ,SEEK_SET); |
2018US_EminaTahirovi | 0:1b3481785d59 | 1033 | fread(line,1,PixelWidth * 2,Image); // read a line - slow |
2018US_EminaTahirovi | 0:1b3481785d59 | 1034 | for (i = 0; i < PixelWidth; i++) { // copy pixel data to TFT |
2018US_EminaTahirovi | 0:1b3481785d59 | 1035 | f_write(line[i]); // one 16 bit pixel |
2018US_EminaTahirovi | 0:1b3481785d59 | 1036 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 1037 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 1038 | spi_bsy(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 1039 | _cs = 1; |
2018US_EminaTahirovi | 0:1b3481785d59 | 1040 | spi_16(0); |
2018US_EminaTahirovi | 0:1b3481785d59 | 1041 | free (line); |
2018US_EminaTahirovi | 0:1b3481785d59 | 1042 | fclose(Image); |
2018US_EminaTahirovi | 0:1b3481785d59 | 1043 | WindowMax(); |
2018US_EminaTahirovi | 0:1b3481785d59 | 1044 | return(1); |
2018US_EminaTahirovi | 0:1b3481785d59 | 1045 | } |
2018US_EminaTahirovi | 0:1b3481785d59 | 1046 | |
2018US_EminaTahirovi | 0:1b3481785d59 | 1047 | #endif |
2018US_EminaTahirovi | 0:1b3481785d59 | 1048 |