ILI9341 display driver (with SPI DMA) for STM32F4 targets. Fork of https://os.mbed.com/users/beaglescout007/code/Nucleo_Ex06_EMU/file/3dac1f1bc9e0/TFT/. Added support for STM32F407 (Seeed Arch Max).
Dependents: STM32F407VET6_Pong
tft.cpp
00001 /*===================================================================*/ 00002 /* */ 00003 /* tft.cpp : TFT(ILI9341) function */ 00004 /* */ 00005 /* 2016/1/20 Racoon */ 00006 /* */ 00007 /*===================================================================*/ 00008 #include "mbed.h" 00009 #include "tft.h" 00010 00011 DigitalOut cs(PB_7, PullUp); // TFT chipselect pin 00012 DigitalOut dc(PB_6, PullUp); // TFT data command select pin 00013 DigitalOut rst(PB_8, PullUp); // TFT reset pin 00014 00015 /*-------------------------------------------------------------------*/ 00016 /* Write command */ 00017 00018 /*-------------------------------------------------------------------*/ 00019 void write_cmd(uint8_t cmd) 00020 { 00021 dc = 0; 00022 spi_write(cmd); 00023 } 00024 00025 /*-------------------------------------------------------------------*/ 00026 /* Write data */ 00027 00028 /*-------------------------------------------------------------------*/ 00029 void write_data(uint8_t data) 00030 { 00031 dc = 1; 00032 spi_write(data); 00033 } 00034 00035 /*-------------------------------------------------------------------*/ 00036 /* TFT reset */ 00037 00038 /*-------------------------------------------------------------------*/ 00039 void tft_reset() 00040 { 00041 wait_ms(200); 00042 cs = 1; 00043 dc = 1; 00044 rst = 1; 00045 wait_ms(200); 00046 rst = 0; 00047 wait_us(10); 00048 rst = 1; 00049 wait_ms(120); 00050 cs = 0; 00051 wait_ms(10); 00052 00053 write_cmd(0x3A); // Pixel Format 00054 write_data(0x55); // 16bit Color 00055 write_cmd(0xB1); // Frame Control 00056 write_data(0); 00057 write_data(0x1f); 00058 00059 write_cmd(0x36); // Memory Access Control 00060 write_data(0xE8); // MY MX MV BGR 00061 write_cmd(0x11); // Sleep Out 00062 wait_ms(5); 00063 00064 write_cmd(0x29); // Display On 00065 } 00066 00067 /*-------------------------------------------------------------------*/ 00068 /* Set windows size, start memory write */ 00069 00070 /*-------------------------------------------------------------------*/ 00071 void tft_set_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) 00072 { 00073 write_cmd(0x2A); // Column Address Set 00074 write_data(x0 >> 8); 00075 write_data(x0); 00076 write_data(x1 >> 8); 00077 write_data(x1); 00078 00079 write_cmd(0x2B); // Page Address Set 00080 write_data(y0 >> 8); 00081 write_data(y0); 00082 write_data(y1 >> 8); 00083 write_data(y1); 00084 00085 write_cmd(0x2C); // Memory Write 00086 wait_us(20); 00087 00088 dc = 1; 00089 } 00090 00091 /*-------------------------------------------------------------------*/ 00092 /* Clear screen */ 00093 00094 /*-------------------------------------------------------------------*/ 00095 void tft_clear(uint16_t color) 00096 { 00097 uint16_t pixel[320]; 00098 00099 tft_set_window(0, 0, TFT_WIDTH, TFT_HEIGHT); 00100 00101 //for (int i = 0; i < TFT_WIDTH * TFT_HEIGHT; ++i) { 00102 //pixel[i++] = color; 00103 //} 00104 00105 00106 for (int i = 0; i < TFT_HEIGHT; i++) { 00107 for (int j = 0; j < TFT_WIDTH; j++) { 00108 pixel[j] = color; 00109 } 00110 HAL_SPI_Transmit(&SpiHandle, (uint8_t*)pixel, TFT_WIDTH * 2, 100); 00111 } 00112 00113 } 00114 00115 /*-------------------------------------------------------------------*/ 00116 /* Put char */ 00117 00118 /*-------------------------------------------------------------------*/ 00119 void tft_put_char(int x, int y, char chr, uint16_t color, uint16_t bgcolor) 00120 { 00121 if (chr < 0x20 || chr > 0x7f) { 00122 chr = 0x3f; 00123 } 00124 else { 00125 chr = (chr < 0x60) ? chr - 0x20 : chr - 0x40; 00126 } 00127 00128 tft_set_window(x, y, x + 7, y + 6); 00129 00130 for (int dy = 0; dy < 7; ++dy) { 00131 unsigned char img = chrimg[chr][dy]; 00132 for (int dx = 0; dx < 8; ++dx) { 00133 if (img & 0x80) { 00134 spi_writew(color); 00135 } 00136 else { 00137 spi_writew(bgcolor); 00138 } 00139 00140 img <<= 1; 00141 } 00142 } 00143 } 00144 00145 /*-------------------------------------------------------------------*/ 00146 /* Text out */ 00147 00148 /*-------------------------------------------------------------------*/ 00149 void tft_text(int x, int y, char* text, uint16_t color, uint16_t bgcolor) 00150 { 00151 while (*text != 0) { 00152 tft_put_char(x, y, *text, color, bgcolor); 00153 x += 8; 00154 text++; 00155 } 00156 } 00157 00158 /*-------------------------------------------------------------------*/ 00159 /* Horizontal Line */ 00160 00161 /*-------------------------------------------------------------------*/ 00162 void tft_hline(int x1, int y, int x2, uint16_t color) 00163 { 00164 uint16_t pixel[320]; 00165 int i = 0; 00166 00167 tft_set_window(x1, y, x2, y); 00168 00169 for (; x1 < x2; ++x1) { 00170 //spi_writew(color); 00171 pixel[i++] = color; 00172 } 00173 HAL_SPI_Transmit(&SpiHandle, (uint8_t*)pixel, (i - 1) * 2, 100); 00174 } 00175 00176 /*-------------------------------------------------------------------*/ 00177 /* Vertical Line */ 00178 00179 /*-------------------------------------------------------------------*/ 00180 void tft_vline(int x, int y1, int y2, uint16_t color) 00181 { 00182 uint16_t pixel[240]; 00183 int i = 0; 00184 00185 tft_set_window(x, y1, x, y2); 00186 00187 for (; y1 < y2; ++y1) { 00188 //spi_writew(color); 00189 pixel[i++] = color; 00190 } 00191 HAL_SPI_Transmit(&SpiHandle, (uint8_t*)pixel, (i - 1) * 2, 100); 00192 } 00193 00194 /*-------------------------------------------------------------------*/ 00195 /* Box */ 00196 00197 /*-------------------------------------------------------------------*/ 00198 void tft_box(int x1, int y1, int x2, int y2, uint16_t color) 00199 { 00200 tft_hline(x1, y1, x2, color); 00201 tft_vline(x1, y1, y2, color); 00202 tft_vline(x2, y1, y2, color); 00203 tft_hline(x1, y2, x2, color); 00204 } 00205 00206 /*-------------------------------------------------------------------*/ 00207 /* Box Fill */ 00208 00209 /*-------------------------------------------------------------------*/ 00210 void tft_boxfill(int x1, int y1, int x2, int y2, uint16_t color) 00211 { 00212 uint16_t pixel[320]; 00213 int i; 00214 int j; 00215 00216 tft_set_window(x1, y1, x2, y2); 00217 00218 //for (int i = 0; i < (x2 - x1 + 1) * (y2 - y1 + 1); ++i) { 00219 //spi_writew(color); 00220 //} 00221 00222 for (i = 0; i < (y2 - y1 + 1); i++) { 00223 for (j = 0; j < (x2 - x1 + 1); j++) { 00224 pixel[j] = color; 00225 } 00226 HAL_SPI_Transmit(&SpiHandle, (uint8_t*)pixel, (j - 1) * 2, 100); 00227 } 00228 } 00229 00230 /*-------------------------------------------------------------------*/ 00231 /* Draw 4bit BMP */ 00232 00233 /*-------------------------------------------------------------------*/ 00234 bool draw_bmp_4bpp(const unsigned char* imgdata, int x, int y) 00235 { 00236 BITMAPFILEHEADER* bf = (BITMAPFILEHEADER*)imgdata; 00237 BITMAPINFOHEADER* bi = (BITMAPINFOHEADER *) (imgdata + sizeof(BITMAPFILEHEADER)); 00238 00239 if (bi->biBitCount != 4) { 00240 return false; 00241 } 00242 00243 unsigned char* pRGBPal = (unsigned char*)imgdata + sizeof(BITMAPFILEHEADER) + bi->biSize; 00244 unsigned short palette[16]; 00245 00246 for (int i = 0; pRGBPal < imgdata + bf->bfOffBits && i < 16; ++i) { 00247 unsigned short r, g, b; 00248 b = *pRGBPal++ >> 3; 00249 g = *pRGBPal++ >> 2; 00250 r = *pRGBPal++ >> 3; 00251 pRGBPal++; 00252 palette[i] = ((g & 7) << 13) | (b << 8) | (r << 3) | (g >> 3); 00253 } 00254 00255 unsigned short HLine[320]; 00256 int linesize = (bi->biWidth / 2 + 3) & 0xfffc; 00257 00258 tft_set_window(x, y, x + bi->biWidth - 1, y + bi->biHeight - 1); 00259 00260 unsigned char* bmp; 00261 00262 for (int y = bi->biHeight - 1; y >= 0; --y) { 00263 bmp = (unsigned char*)imgdata + bf->bfOffBits + y * linesize; 00264 00265 for (int x = 0; x < bi->biWidth; ++x) { 00266 char pal; 00267 if (x & 1) { 00268 pal = *bmp & 0xf; 00269 bmp++; 00270 } 00271 else { 00272 pal = *bmp >> 4; 00273 } 00274 00275 HLine[x] = palette[pal]; 00276 } 00277 00278 HAL_SPI_Transmit(&SpiHandle, (uint8_t*)HLine, bi->biWidth * 2, 100); 00279 } 00280 00281 return true; 00282 } 00283 00284 /*-------------------------------------------------------------------*/ 00285 /* Initialize TFT */ 00286 00287 /*-------------------------------------------------------------------*/ 00288 void tft_init(void) 00289 { 00290 spi_init(); 00291 00292 tft_reset(); 00293 }
Generated on Sat Jul 16 2022 06:08:34 by 1.7.2