
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 |
Diff: UniGraphic/Inits/SSD1306.cpp
- Revision:
- 0:fa952828e34c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/UniGraphic/Inits/SSD1306.cpp Sun May 10 10:44:31 2020 +0000 @@ -0,0 +1,188 @@ +/* mbed UniGraphic library - Device specific class + * Copyright (c) 2015 Peter Drescher + * Released under the MIT License: http://mbed.org/license/mit + */ + +#include "Protocols.h" +#include "SSD1306.h" + +////////////////////////////////////////////////////////////////////////////////// +// display settings /////////////////////////////////////////////////////// +///////////////////////////////////////////////////////////////////////// + + +#define IC_X_SEGS 128 // UC1608 SEG has range 0-239 (239-0 if MX=1), check your datasheet, important for the orientation +#define IC_Y_COMS 64 // UC1608 COM has range 0-127 (127-0 if MY=1), check your datasheet, important for the orientation + +#define SSD1306_SETCONTRAST 0x81 +#define SSD1306_DISPLAYALLON_RESUME 0xA4 +#define SSD1306_DISPLAYALLON 0xA5 +#define SSD1306_NORMALDISPLAY 0xA6 +#define SSD1306_INVERTDISPLAY 0xA7 +#define SSD1306_DISPLAYOFF 0xAE +#define SSD1306_DISPLAYON 0xAF +#define SSD1306_SETDISPLAYOFFSET 0xD3 +#define SSD1306_SETCOMPINS 0xDA +#define SSD1306_SETVCOMDETECT 0xDB +#define SSD1306_SETDISPLAYCLOCKDIV 0xD5 +#define SSD1306_SETPRECHARGE 0xD9 +#define SSD1306_SETMULTIPLEX 0xA8 +#define SSD1306_SETLOWCOLUMN 0x00 +#define SSD1306_SETHIGHCOLUMN 0x10 +#define SSD1306_SETSTARTLINE 0x40 +#define SSD1306_MEMORYMODE 0x20 +#define SSD1306_COMSCANINC 0xC0 +#define SSD1306_COMSCANDEC 0xC8 +#define SSD1306_SEGREMAP 0xA0 +#define SSD1306_CHARGEPUMP 0x8D + +SSD1306::SSD1306(proto_t displayproto, PortName port, PinName CS, PinName reset, PinName DC, PinName WR, PinName RD, const char *name , unsigned int LCDSIZE_X, unsigned int LCDSIZE_Y) + : LCD(displayproto, port, CS, reset, DC, WR, RD, LCDSIZE_X, LCDSIZE_Y, IC_X_SEGS, IC_Y_COMS, name) +{ + hw_reset(); + BusEnable(true); + init(); + cls(); + set_orientation(1); + locate(0,0); +} +SSD1306::SSD1306(proto_t displayproto, int Hz, PinName mosi, PinName miso, PinName sclk, PinName CS, PinName reset, PinName DC, const char *name, unsigned int LCDSIZE_X, unsigned int LCDSIZE_Y) + : LCD(displayproto, Hz, mosi, miso, sclk, CS, reset, DC, LCDSIZE_X, LCDSIZE_Y, IC_X_SEGS, IC_Y_COMS, name) +{ + hw_reset(); + BusEnable(true); + init(); + cls(); + set_orientation(1); + locate(0,0); +} + +SSD1306::SSD1306(proto_t displayproto, int Hz, int address, PinName sda, PinName scl, const char* name , unsigned int LCDSIZE_X, unsigned int LCDSIZE_Y) + : LCD(displayproto, Hz, address, sda, scl, LCDSIZE_X, LCDSIZE_Y, IC_X_SEGS, IC_Y_COMS, name) +{ + init(); + cls(); + set_orientation(1); + locate(0,0); +} + + +// reset and init the lcd controller +void SSD1306::init() +{ + /* Start Initial Sequence ----------------------------------------------------*/ + + // wr_cmd8(0xE2); // sw reset + wait_ms(15); + + wr_cmd8(SSD1306_DISPLAYOFF); // no problem in SPI_16 for single byte cmds + wr_cmd16((SSD1306_SETDISPLAYCLOCKDIV<<8)|0x80); // wr_cmd16 for multibyte cmds issue in SPI16 mode + // wr_cmd8(0x80); // in SPI_16 it would become 0xE380 and will break things up + wr_cmd16((SSD1306_SETMULTIPLEX<<8)|63); + // wr_cmd8(63); + + wr_cmd16((SSD1306_SETDISPLAYOFFSET<<8)|0x00); + // wr_cmd8(0x0); + + wr_cmd8(SSD1306_SETSTARTLINE | 0x0); // line #0 + + wr_cmd16((SSD1306_CHARGEPUMP<<8)|0x14); + // wr_cmd8(0x14); // 0x10 + + wr_cmd16((SSD1306_MEMORYMODE<<8)|0x00); + // wr_cmd8(0x00); // 0x0 act like ks0108 + + wr_cmd8(SSD1306_SEGREMAP ); //| 0x1); + + wr_cmd8(SSD1306_COMSCANDEC); + + wr_cmd16((SSD1306_SETCOMPINS<<8)|0x12); + // wr_cmd8(0x12); // LCDSIZE_Y == 32 ? 0x02 : 0x12); + + wr_cmd16((SSD1306_SETCONTRAST<<8)|0xCF); + // wr_cmd8(0xCF); // _rawHeight == 32 ? 0x8F : ((vccstate == SSD1306_EXTERNALVCC) ? 0x9F : 0xCF) ); + + wr_cmd16((SSD1306_SETPRECHARGE<<8)|0xF1); + // wr_cmd8(0xF1); // : 0x22); + + wr_cmd16((SSD1306_SETVCOMDETECT<<8)|0x40); + // wr_cmd8(0x40); + + wr_cmd8(SSD1306_DISPLAYALLON_RESUME); + + //wr_cmd8(SSD1306_NORMALDISPLAY); + wr_cmd8(SSD1306_INVERTDISPLAY); + + wr_cmd8(SSD1306_DISPLAYON); +} + +//////////////////////////////////////////////////////////////////// +// functions that overrides the standard ones implemented in LCD.cpp +//////////////////////////////////////////////////////////////////// + +void SSD1306::mirrorXY(mirror_t mode) +{ + switch (mode) + { + case(NONE): + wr_cmd16(0xA0C0); + break; + case(X): + wr_cmd16(0xA1C0); + break; + case(Y): + wr_cmd16(0xA0C8); + break; + case(XY): + wr_cmd16(0xA1C8); + break; + } +} + +void SSD1306::set_contrast(int o) +{ + contrast = o; + + wr_cmd16(0x8100|(o&0xFF)); +} + +//////////////////////////////////////////////////////////////////// +// functions that overrides the standard ones implemented in LCD.cpp +//////////////////////////////////////////////////////////////////// + + +const uint8_t scroll_speed[8]={3,2,1,6,0,5,4,7}; + +//////////////////////////////////////////////////////////////////// +// functions addon to LCD.cpp +//////////////////////////////////////////////////////////////////// +void SSD1306::horizontal_scroll(int l_r,int s_page,int e_page,int speed){ + wr_cmd8(0x2E); // deactivate scroll before change + if(l_r == 1){ + wr_cmd16(0x2700); // horizontal scroll left + } + else { + wr_cmd16(0x2600); + } + wr_cmd16((s_page & 0x07)<<8 | (scroll_speed[speed & 0x07])); + wr_cmd16((e_page & 0x07)<<8 ); + wr_cmd16(0xFF2F); +} + +void SSD1306::horiz_vert_scroll(int l_r,int s_page,int e_page,int v_off,int speed){ + wr_cmd8(0x2E); // deactivate scroll before change + if(l_r == 1){ + wr_cmd16(0x2A00); // horizontal scroll left + } + else { + wr_cmd16(0x2900); + } + wr_cmd16((s_page & 0x07)<<8 | (scroll_speed[speed & 0x07])); + wr_cmd16((e_page & 0x07)<<8 | (v_off & 0x3F) ); + wr_cmd8(0x2F); + +} + +void SSD1306::end_scroll(){ + wr_cmd8(0x2E); +}