Connecting a TFT Display to the STM32F103VET6 via FSMC, Using the SSD1289 as an Example.

Recently a board with an STM32VET6 that I ordered on AliExpress arrived, and in this article we'll try to connect a TFT display to it; by the way, the board has a dedicated header for it.
Connecting a TFT Display to the STM32F103VET6 via FSMC, Using the SSD1289 as an Example.

Unlike the Atmega, STM32 has a special interface for connecting a display, called FSMC, also known as the external memory controller. The idea of FSMC is as follows: when a display or external memory is connected via FSMC, we can access the registers of the connected device as if they were in the microcontroller's memory. We don't need to toggle pins and count time intervals as we did on the Atmega; we just write data to a register, and the interface takes care of everything else.
Let's start with the connection; the general wiring diagram looks like this.
Connecting a TFT Display to the STM32F103VET6 via FSMC, Using the SSD1289 as an Example.

Now we need to figure out which pins NEx, NOE, NWE, Ax and the data bus are on. To do this, download STM32CUBEMX, create a new project for our controller and select FSMC in the Peripherals tab; you can enlarge the pictures by clicking on them.
Connecting a TFT Display to the STM32F103VET6 via FSMC, Using the SSD1289 as an Example.

Connecting a TFT Display to the STM32F103VET6 via FSMC, Using the SSD1289 as an Example.

Next, set the settings as in the picture above and see which pins the signals we need are on. Matching them up, we get the following wiring diagram: the left column is the display pins, the right column the microcontroller pins.
Connecting a TFT Display to the STM32F103VET6 via FSMC, Using the SSD1289 as an Example.

We'll connect the backlight to 5V through a 200 Ohm resistor to reduce the load on the 3.3V regulator.

And now attention, this is where it gets interesting!!!
Connecting a TFT Display to the STM32F103VET6 via FSMC, Using the SSD1289 as an Example.

The picture shows that the address space is divided into 4 banks.
Connecting a TFT Display to the STM32F103VET6 via FSMC, Using the SSD1289 as an Example.

And the first bank is divided into 4 parts. So when we write to the range from 0x60000000 to 0x63FFFFFF, the NE1 pin automatically becomes active; if we write to the range from 0x64000000 to 0x67FFFFFF, NE2 becomes active.

As for the FSMC_Ax pin, its state determines whether we're sending a register (command) or data.
When RS is connected to the FSMC_A(X) pin, bit X+1 must be set to one in LCD_DATA, not forgetting to specify the bank address.
If we want to choose FSMC_A18 as the switching pin, the addresses must be specified as follows.

#define LCD_DATA 0x60080000        //1000 0000 0000 0000 000 
#define LCD_REG   0x60000000


Now that the low-level functions are implemented, all that's left is to initialize the ports and FSMC. We'll take the Atmega project as a basis and rewrite the low-level functions in it.

// Define the addresses
// for writing data
#define LCD_DATA 0x60020000
// for writing commands
#define LCD_REG 0x60000000

__inline void Lcd_Write_Index(uint16_t index)
{
   *(uint16_t *) (LCD_REG) = index;
}
////////////////////////
__inline void Lcd_Write_Data(uint16_t data)
{
   *(uint16_t *) (LCD_DATA)= data;
}
///////////////////
uint16_t Lcd_Read_Data()
{
   uint16_t data = * (uint16_t *)(LCD_DATA);
   return data;
}
////////////////////////
uint16_t Lcd_Read_Reg(uint16_t reg_addr)
{
   volatile uint16_t data = 0;
   Lcd_Write_Index(reg_addr);
   data = Lcd_Read_Data();
   return data;
}
///////////////////////
void Lcd_Write_Reg(uint16_t reg,uint16_t value)
{
   *(uint16_t *) (LCD_REG) = reg;
   *(uint16_t *) (LCD_DATA) = value;
}


Sending data looks like this:

*(uint16_t *) ( LCD_DATA)= data;
We write data to the address allocated for writing data)))
LCD_DATA is an address (0x60020000); we can't simply take it and write a value to this address like this

*LCD_DATA = data;

because this way we specify the start address of the variable but not its size and where it ends.

But if we specify (uint16_t *), it becomes clear that the variable is 16 bits in size and 16 bits should be counted from the specified address.


FSMC_NORSRAMInitTypeDef fsmc;
FSMC_NORSRAMTimingInitTypeDef fsmcTiming;
GPIO_InitTypeDef gpio;

// Enable port clocking
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE , ENABLE);

// Enable FSMC clocking
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);

// Initialize the pins used for FSMC communication
gpio.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_14 | GPIO_Pin_15;
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &gpio);

gpio.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &gpio);
// Reset
gpio.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOE, &gpio);
/////////////////////////////////
// CS -> 1
// Reset -> 0
// RD -> 1
// RW -> 1

GPIO_SetBits(GPIOD, GPIO_Pin_7);
GPIO_ResetBits(GPIOE, GPIO_Pin_1);
GPIO_SetBits(GPIOD, GPIO_Pin_4);
GPIO_SetBits(GPIOD, GPIO_Pin_5);

// Configure FSMC
fsmcTiming.FSMC_AddressSetupTime = 0x02;
fsmcTiming.FSMC_AddressHoldTime = 0x00;
fsmcTiming.FSMC_DataSetupTime = 0x05;
fsmcTiming.FSMC_BusTurnAroundDuration = 0x00;
fsmcTiming.FSMC_CLKDivision = 0x00;
fsmcTiming.FSMC_DataLatency = 0x00;
fsmcTiming.FSMC_AccessMode = FSMC_AccessMode_B;


fsmc.FSMC_Bank = FSMC_Bank1_NORSRAM1;
fsmc.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
fsmc.FSMC_MemoryType = FSMC_MemoryType_NOR;
fsmc.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
fsmc.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
fsmc.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
fsmc.FSMC_WrapMode = FSMC_WrapMode_Disable;
fsmc.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
fsmc.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
fsmc.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
fsmc.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
fsmc.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
fsmc.FSMC_ReadWriteTimingStruct = &fsmcTiming;
fsmc.FSMC_WriteTimingStruct = &fsmcTiming;

FSMC_NORSRAMInit(&fsmc);
FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE);

Connecting a TFT Display to the STM32F103VET6 via FSMC, Using the SSD1289 as an Example.

Now all the display functions written for the Atmega16 work on the STM32F103VET6. That's all; the archive contains a project that displays the image shown in the preview. In the project settings, on the Target tab, set Xtal = 8MHz. ssd1289_stm32f103VET6_example.rar.