3.5" inch TFT LCD Display Module 480X320 driven with FSMC.
TFT LCD Display Module 480X320 driven with FSMC
I have recently bought a 3.5" inch TFT LCD Touch Screen Display Module 480X320 with a www.mcufriend.com
label on the back side. The display was equipped with an 8bit parallel interface. First I decided to test it with the UniGraphic library using the BUS_8
protocol. The display was very slow but improved when I switched to the PAR_8
protocol. Because I heard about the possibility to use a Flexible Static Memory Controller (FSMC), built into some STM MCU's, to drive LCD's (read/write to LCD's memory rather than to an external SRAM) I thought it would be a fun to try it out.
Below is the brief story of what I did:
- Created a project for my STM32F407VE board in the STM32CubeIDE
- Set the
Clock Configuration
to match the one used by Mbed for the Seeed Arch Max board:
- Selected
FSMC
in theConnectivity
category and configured it as below: - Let the
STM32CubeIDE
generate the code (files). - Created a new program for the Seeed Arch Max target in the Mbed Online Compiler by selecting a
mbed os blinky
template. - Replaced the
main.cpp
with themain.c
content of theSTM32CubeIDE
project. Copy & Pasted
the other files with codes from theSTM32CubeIDE
project to the online compiler project.- Renamed and modified:
"stm32f4xx_it.h" to "stm32f4xx_it_msp.h"
"stm32f4xx_it.c" to "stm32f4xx_it_msp.c" - Added the UniGraphic library to the online compiler project.
- Extended the
UniGraphic
library with aFSMC_8
protocol and replaced theTFT::set_orientation(int orient)
function with the one used bymcufriend
for arduino. - Modified the
main.cpp
as needed.
Wiring
STM32F407VE | TFT LCD module |
---|---|
+3.3V | 3V3 |
GND | GND |
PB_12 | LCD_RST |
GND | LCD_CS |
PD_13 (RS) | LCD_RS |
PD_5 (WR) | LCD_WR |
PD_4 (RD) | LCD_RD |
PD_14 (DB00) | LCD_D0 |
PD_15 (DB01) | LCD_D1 |
PD_0 (DB02) | LCD_D2 |
PD_1 (DB03) | LCD_D3 |
PE_7 (DB04) | LCD_D4 |
PE_8 (DB05) | LCD_D5 |
PE_9 (DB06) | LCD_D6 |
PE_10 (DB07) | LCD_D7 |
Results
Execution times | ||
---|---|---|
Used protocol | BUS_8 | FSMC_8 |
Operation \ Time | ms | ms |
Clear | 2283.980 | 38.454 |
Plot | 192.066 | 11.365 |
8bit BMP | 63.805 | 41.338 |
Large Font | 163.872 | 7.895 |
Sparce pixels | 2072.265/1458.051 | 74.107/52.168 |
16bit BMP | 2288.589 | 59.904 |
UniGraphic/Protocols/FSMC8.cpp@0:fa952828e34c, 2020-05-10 (annotated)
- Committer:
- hudakz
- Date:
- Sun May 10 10:44:31 2020 +0000
- Revision:
- 0:fa952828e34c
3.5" inch TFT LCD Display Module 480X320 driven with FSMC.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:fa952828e34c | 1 | #include "FSMC8.h" |
hudakz | 0:fa952828e34c | 2 | #include "stdint.h" |
hudakz | 0:fa952828e34c | 3 | |
hudakz | 0:fa952828e34c | 4 | // Addres bit A18 is used to select LCD's REGISTER or RAM: A18 = 0b00000000000001000000000000000000 = 0x00040000 |
hudakz | 0:fa952828e34c | 5 | volatile unsigned char* lcd_reg = (unsigned char*)0x60000000; // FSMC BANK1, A18 = 0 -> write to LCD's REGISTER |
hudakz | 0:fa952828e34c | 6 | volatile unsigned char* lcd_ram = (unsigned char*)0x60040000; // FSMC BANK1, A18 = 1 -> read/write to LCD's RAM |
hudakz | 0:fa952828e34c | 7 | |
hudakz | 0:fa952828e34c | 8 | /** |
hudakz | 0:fa952828e34c | 9 | * @brief |
hudakz | 0:fa952828e34c | 10 | * @note |
hudakz | 0:fa952828e34c | 11 | * @param |
hudakz | 0:fa952828e34c | 12 | * @retval |
hudakz | 0:fa952828e34c | 13 | */ |
hudakz | 0:fa952828e34c | 14 | FSMC8::FSMC8(PinName reset) : |
hudakz | 0:fa952828e34c | 15 | _reset(reset) |
hudakz | 0:fa952828e34c | 16 | { |
hudakz | 0:fa952828e34c | 17 | _reset = 1; |
hudakz | 0:fa952828e34c | 18 | } |
hudakz | 0:fa952828e34c | 19 | |
hudakz | 0:fa952828e34c | 20 | |
hudakz | 0:fa952828e34c | 21 | /** |
hudakz | 0:fa952828e34c | 22 | * @brief |
hudakz | 0:fa952828e34c | 23 | * @note |
hudakz | 0:fa952828e34c | 24 | * @param |
hudakz | 0:fa952828e34c | 25 | * @retval |
hudakz | 0:fa952828e34c | 26 | */ |
hudakz | 0:fa952828e34c | 27 | void FSMC8::wr_cmd8(unsigned char cmd) |
hudakz | 0:fa952828e34c | 28 | { |
hudakz | 0:fa952828e34c | 29 | lcd_reg[0] = cmd; // Write to LCD's register |
hudakz | 0:fa952828e34c | 30 | } |
hudakz | 0:fa952828e34c | 31 | |
hudakz | 0:fa952828e34c | 32 | /** |
hudakz | 0:fa952828e34c | 33 | * @brief |
hudakz | 0:fa952828e34c | 34 | * @note |
hudakz | 0:fa952828e34c | 35 | * @param |
hudakz | 0:fa952828e34c | 36 | * @retval |
hudakz | 0:fa952828e34c | 37 | */ |
hudakz | 0:fa952828e34c | 38 | void FSMC8::wr_data8(unsigned char data) |
hudakz | 0:fa952828e34c | 39 | { |
hudakz | 0:fa952828e34c | 40 | lcd_ram[0] = data; // Write to LCD's RAM |
hudakz | 0:fa952828e34c | 41 | } |
hudakz | 0:fa952828e34c | 42 | |
hudakz | 0:fa952828e34c | 43 | /** |
hudakz | 0:fa952828e34c | 44 | * @brief |
hudakz | 0:fa952828e34c | 45 | * @note |
hudakz | 0:fa952828e34c | 46 | * @param |
hudakz | 0:fa952828e34c | 47 | * @retval |
hudakz | 0:fa952828e34c | 48 | */ |
hudakz | 0:fa952828e34c | 49 | void FSMC8::wr_cmd16(unsigned short cmd) |
hudakz | 0:fa952828e34c | 50 | { |
hudakz | 0:fa952828e34c | 51 | wr_cmd8(cmd >> 8); // Write MSB to LCD's register |
hudakz | 0:fa952828e34c | 52 | wr_cmd8(cmd & 0xFF); // Write LSB to LCD's register |
hudakz | 0:fa952828e34c | 53 | } |
hudakz | 0:fa952828e34c | 54 | |
hudakz | 0:fa952828e34c | 55 | /** |
hudakz | 0:fa952828e34c | 56 | * @brief |
hudakz | 0:fa952828e34c | 57 | * @note |
hudakz | 0:fa952828e34c | 58 | * @param |
hudakz | 0:fa952828e34c | 59 | * @retval |
hudakz | 0:fa952828e34c | 60 | */ |
hudakz | 0:fa952828e34c | 61 | void FSMC8::wr_data16(unsigned short data) |
hudakz | 0:fa952828e34c | 62 | { |
hudakz | 0:fa952828e34c | 63 | wr_data8(data >> 8); // Write MSB to LCD's RAM |
hudakz | 0:fa952828e34c | 64 | wr_data8(data & 0xFF); // Write LSB to LCD's RAM |
hudakz | 0:fa952828e34c | 65 | |
hudakz | 0:fa952828e34c | 66 | } |
hudakz | 0:fa952828e34c | 67 | |
hudakz | 0:fa952828e34c | 68 | /** |
hudakz | 0:fa952828e34c | 69 | * @brief |
hudakz | 0:fa952828e34c | 70 | * @note |
hudakz | 0:fa952828e34c | 71 | * @param |
hudakz | 0:fa952828e34c | 72 | * @retval |
hudakz | 0:fa952828e34c | 73 | */ |
hudakz | 0:fa952828e34c | 74 | void FSMC8::wr_gram(unsigned short data) |
hudakz | 0:fa952828e34c | 75 | { |
hudakz | 0:fa952828e34c | 76 | wr_data16(data); |
hudakz | 0:fa952828e34c | 77 | } |
hudakz | 0:fa952828e34c | 78 | |
hudakz | 0:fa952828e34c | 79 | /** |
hudakz | 0:fa952828e34c | 80 | * @brief |
hudakz | 0:fa952828e34c | 81 | * @note |
hudakz | 0:fa952828e34c | 82 | * @param |
hudakz | 0:fa952828e34c | 83 | * @retval |
hudakz | 0:fa952828e34c | 84 | */ |
hudakz | 0:fa952828e34c | 85 | void FSMC8::wr_gram(unsigned short data, unsigned int count) |
hudakz | 0:fa952828e34c | 86 | { |
hudakz | 0:fa952828e34c | 87 | if ((data >> 8) == (data & 0xFF)) { |
hudakz | 0:fa952828e34c | 88 | count <<= 1; |
hudakz | 0:fa952828e34c | 89 | while (count) { |
hudakz | 0:fa952828e34c | 90 | wr_data8(data); |
hudakz | 0:fa952828e34c | 91 | count--; |
hudakz | 0:fa952828e34c | 92 | } |
hudakz | 0:fa952828e34c | 93 | } |
hudakz | 0:fa952828e34c | 94 | else { |
hudakz | 0:fa952828e34c | 95 | while (count) { |
hudakz | 0:fa952828e34c | 96 | wr_data16(data); |
hudakz | 0:fa952828e34c | 97 | count--; |
hudakz | 0:fa952828e34c | 98 | } |
hudakz | 0:fa952828e34c | 99 | } |
hudakz | 0:fa952828e34c | 100 | } |
hudakz | 0:fa952828e34c | 101 | |
hudakz | 0:fa952828e34c | 102 | /** |
hudakz | 0:fa952828e34c | 103 | * @brief |
hudakz | 0:fa952828e34c | 104 | * @note |
hudakz | 0:fa952828e34c | 105 | * @param |
hudakz | 0:fa952828e34c | 106 | * @retval |
hudakz | 0:fa952828e34c | 107 | */ |
hudakz | 0:fa952828e34c | 108 | void FSMC8::wr_grambuf(unsigned short* data, unsigned int lenght) |
hudakz | 0:fa952828e34c | 109 | { |
hudakz | 0:fa952828e34c | 110 | while (lenght) { |
hudakz | 0:fa952828e34c | 111 | wr_data16(*data++); |
hudakz | 0:fa952828e34c | 112 | lenght--; |
hudakz | 0:fa952828e34c | 113 | } |
hudakz | 0:fa952828e34c | 114 | } |
hudakz | 0:fa952828e34c | 115 | |
hudakz | 0:fa952828e34c | 116 | /** |
hudakz | 0:fa952828e34c | 117 | * @brief |
hudakz | 0:fa952828e34c | 118 | * @note |
hudakz | 0:fa952828e34c | 119 | * @param |
hudakz | 0:fa952828e34c | 120 | * @retval |
hudakz | 0:fa952828e34c | 121 | */ |
hudakz | 0:fa952828e34c | 122 | unsigned char FSMC8::rd_data8(unsigned int i /* = 0 */) |
hudakz | 0:fa952828e34c | 123 | { |
hudakz | 0:fa952828e34c | 124 | return lcd_ram[i]; // Read from LCD's RAM |
hudakz | 0:fa952828e34c | 125 | } |
hudakz | 0:fa952828e34c | 126 | |
hudakz | 0:fa952828e34c | 127 | /** |
hudakz | 0:fa952828e34c | 128 | * @brief |
hudakz | 0:fa952828e34c | 129 | * @note |
hudakz | 0:fa952828e34c | 130 | * @param |
hudakz | 0:fa952828e34c | 131 | * @retval |
hudakz | 0:fa952828e34c | 132 | */ |
hudakz | 0:fa952828e34c | 133 | unsigned short FSMC8::rd_gram(bool convert) |
hudakz | 0:fa952828e34c | 134 | { |
hudakz | 0:fa952828e34c | 135 | unsigned int r = 0; |
hudakz | 0:fa952828e34c | 136 | |
hudakz | 0:fa952828e34c | 137 | rd_data8(); // dummy read |
hudakz | 0:fa952828e34c | 138 | r |= rd_data8(); |
hudakz | 0:fa952828e34c | 139 | r <<= 8; |
hudakz | 0:fa952828e34c | 140 | r |= rd_data8(); |
hudakz | 0:fa952828e34c | 141 | |
hudakz | 0:fa952828e34c | 142 | if (convert) { |
hudakz | 0:fa952828e34c | 143 | r <<= 8; |
hudakz | 0:fa952828e34c | 144 | r |= rd_data8(); |
hudakz | 0:fa952828e34c | 145 | |
hudakz | 0:fa952828e34c | 146 | // gram is 18bit/pixel, if you set 16bit/pixel (cmd 3A), during writing the 16bits are expanded to 18bit |
hudakz | 0:fa952828e34c | 147 | // during reading, you read the raw 18bit gram |
hudakz | 0:fa952828e34c | 148 | r = RGB24to16((r & 0xFF0000) >> 16, (r & 0xFF00) >> 8, r & 0xFF); // 18bit pixel padded to 24bits, rrrrrr00_gggggg00_bbbbbb00, converted to 16bit |
hudakz | 0:fa952828e34c | 149 | } |
hudakz | 0:fa952828e34c | 150 | |
hudakz | 0:fa952828e34c | 151 | // _bus.output(); |
hudakz | 0:fa952828e34c | 152 | return (unsigned short)r; |
hudakz | 0:fa952828e34c | 153 | } |
hudakz | 0:fa952828e34c | 154 | |
hudakz | 0:fa952828e34c | 155 | /** |
hudakz | 0:fa952828e34c | 156 | * @brief |
hudakz | 0:fa952828e34c | 157 | * @note |
hudakz | 0:fa952828e34c | 158 | * @param |
hudakz | 0:fa952828e34c | 159 | * @retval |
hudakz | 0:fa952828e34c | 160 | */ |
hudakz | 0:fa952828e34c | 161 | unsigned int FSMC8::rd_reg_data32(unsigned char reg) |
hudakz | 0:fa952828e34c | 162 | { |
hudakz | 0:fa952828e34c | 163 | unsigned char data[4] = { 0 }; |
hudakz | 0:fa952828e34c | 164 | unsigned int* r = (unsigned int*)&data[0]; |
hudakz | 0:fa952828e34c | 165 | |
hudakz | 0:fa952828e34c | 166 | wr_cmd8(reg); |
hudakz | 0:fa952828e34c | 167 | data[3] = rd_data8(0); |
hudakz | 0:fa952828e34c | 168 | data[2] = rd_data8(0); |
hudakz | 0:fa952828e34c | 169 | data[1] = rd_data8(0); |
hudakz | 0:fa952828e34c | 170 | data[0] = rd_data8(0); |
hudakz | 0:fa952828e34c | 171 | |
hudakz | 0:fa952828e34c | 172 | return *r; |
hudakz | 0:fa952828e34c | 173 | } |
hudakz | 0:fa952828e34c | 174 | |
hudakz | 0:fa952828e34c | 175 | // in Par mode EXTC regs (0xB0-0xFF) can be directly read |
hudakz | 0:fa952828e34c | 176 | unsigned int FSMC8::rd_extcreg_data32(unsigned char reg, unsigned char SPIreadenablecmd) |
hudakz | 0:fa952828e34c | 177 | { |
hudakz | 0:fa952828e34c | 178 | return rd_reg_data32(reg); |
hudakz | 0:fa952828e34c | 179 | } |
hudakz | 0:fa952828e34c | 180 | |
hudakz | 0:fa952828e34c | 181 | // ILI932x specific |
hudakz | 0:fa952828e34c | 182 | void FSMC8::dummyread() |
hudakz | 0:fa952828e34c | 183 | { |
hudakz | 0:fa952828e34c | 184 | rd_data8(); // dummy read |
hudakz | 0:fa952828e34c | 185 | } |
hudakz | 0:fa952828e34c | 186 | |
hudakz | 0:fa952828e34c | 187 | // ILI932x specific |
hudakz | 0:fa952828e34c | 188 | void FSMC8::reg_select(unsigned char reg, bool forread) |
hudakz | 0:fa952828e34c | 189 | { |
hudakz | 0:fa952828e34c | 190 | wr_cmd8(0); // write MSB |
hudakz | 0:fa952828e34c | 191 | wr_cmd8(reg); // write LSB |
hudakz | 0:fa952828e34c | 192 | } |
hudakz | 0:fa952828e34c | 193 | |
hudakz | 0:fa952828e34c | 194 | // ILI932x specific |
hudakz | 0:fa952828e34c | 195 | void FSMC8::reg_write(unsigned char reg, unsigned short data) |
hudakz | 0:fa952828e34c | 196 | { |
hudakz | 0:fa952828e34c | 197 | wr_cmd8(0); // write MSB |
hudakz | 0:fa952828e34c | 198 | wr_cmd8(reg); // write LSB |
hudakz | 0:fa952828e34c | 199 | wr_data16(data); |
hudakz | 0:fa952828e34c | 200 | } |
hudakz | 0:fa952828e34c | 201 | |
hudakz | 0:fa952828e34c | 202 | // ILI932x specific |
hudakz | 0:fa952828e34c | 203 | unsigned short FSMC8::reg_read(unsigned char reg) |
hudakz | 0:fa952828e34c | 204 | { |
hudakz | 0:fa952828e34c | 205 | unsigned short r = 0; |
hudakz | 0:fa952828e34c | 206 | |
hudakz | 0:fa952828e34c | 207 | wr_cmd8(0); |
hudakz | 0:fa952828e34c | 208 | wr_cmd8(reg); |
hudakz | 0:fa952828e34c | 209 | r |= rd_data8() & 0xFF; |
hudakz | 0:fa952828e34c | 210 | r <<= 8; |
hudakz | 0:fa952828e34c | 211 | r |= rd_data8() & 0xFF; |
hudakz | 0:fa952828e34c | 212 | |
hudakz | 0:fa952828e34c | 213 | return r; |
hudakz | 0:fa952828e34c | 214 | } |
hudakz | 0:fa952828e34c | 215 | |
hudakz | 0:fa952828e34c | 216 | /** |
hudakz | 0:fa952828e34c | 217 | * @brief |
hudakz | 0:fa952828e34c | 218 | * @note |
hudakz | 0:fa952828e34c | 219 | * @param |
hudakz | 0:fa952828e34c | 220 | * @retval |
hudakz | 0:fa952828e34c | 221 | */ |
hudakz | 0:fa952828e34c | 222 | void FSMC8::hw_reset() |
hudakz | 0:fa952828e34c | 223 | { |
hudakz | 0:fa952828e34c | 224 | _reset = 1; |
hudakz | 0:fa952828e34c | 225 | wait_ms(15); |
hudakz | 0:fa952828e34c | 226 | _reset = 0; // display reset |
hudakz | 0:fa952828e34c | 227 | wait_ms(2); |
hudakz | 0:fa952828e34c | 228 | _reset = 1; // end reset |
hudakz | 0:fa952828e34c | 229 | wait_ms(100); |
hudakz | 0:fa952828e34c | 230 | } |