Displaying an Image on a TFT Display from an SD Card, Using the SSD1289 as an Example.

In the previous article we converted an image into an array and wrote this array to an SD card; now let's try to display it on a TFT display. To work with the SD card, we'll use the Petit FatFs library, which is designed for 8-bit microcontrollers with little memory.
 
The library consists of 5 files:
integer.h - a header file that describes the basic data types.

diskio.h - a header file that declares the prototypes of the low-level disk functions and the status codes they return.

diskio.c - the low-level functions must be implemented in this file; initially it contains "stubs".

pffconf.h - the configuration file.

pff.h - a header file that declares the prototypes of the functions for interacting with the disk's file system.

pff.c - the file contains the implementations of the functions for interacting with the disk's file system.
 
Let's look at the functions we'll need to work with the disk.

FRESULT pf_mount (FATFS*) — the function mounts/unmounts a disk. This function must be called before starting to work with the disk; if the function is called with a null pointer, the disk is unmounted. The function can be called at any time.
 
Parameters
FATFS* fs — a pointer to an object of type FATFS; the description of this structure can be found in pff.h. We just need to declare a variable of this type.
 
Return values:
FR_OK (0) — returned if the function succeeds
FR_NOT_READY — the device could not be initialized
FR_DISK_ERR — an error occurred while reading from the disk
FR_NO_FILESYSTEM — there is no valid FAT partition on the disk
 

FRESULT pf_open (const char* path) — the function opens an existing file. Once a file is open, you can work with it, i.e. read from and write to it. You can work with an open file until another file is opened. The function can be called at any time.
 
Parameters
const char* path — a pointer to a string specifying the path to the file. The path must be specified in full relative to the root directory, with directories separated by slashes.
 
Return values:
FR_OK (0) — returned if the function succeeds
FR_NO_FILE — file not found
FR_DISK_ERR — disk error
FR_NOT_ENABLED — the disk was not mounted
 

FRESULT pf_read(void* buff, WORD btr, WORD* br) — the function reads the specified number of bytes from a file and stores them in a buffer. If the number of bytes read is less than specified, the end of the file has been reached. For the function to work, you need to write the following in pffconf.h
#define _USE_READ 1
 
Parameters:
void* buff — a pointer to the buffer where the read data is stored
WORD btr — the number of bytes to read
WORD* br — a pointer to a variable that stores the number of bytes read.

Return values:
FR_OK (0) — returned if the function succeeds
FR_DISK_ERR — disk error
FR_NOT_OPENED — the file was not opened
FR_NOT_ENABLED — the disk was not mounted

The function descriptions were taken from here; the translation isn't quite complete, but these functions are enough to display an image. To make them work, you need to implement the low-level functions in the diskio.c file and the card communication protocol itself, SPI. The website has an example of using the library containing an mmc.c file with the low-level functions implemented; we'll take it, and the SPI implementation was found online. Now let's add all the necessary files to the project.
Displaying an Image on a TFT Display from an SD Card, Using the SSD1289 as an Example.


The code for displaying the image looks like this

//define the buffer size
#define BUF_SIZE    512UL
#define HALF_BUF   ((BUF_SIZE)/2)

#define F_CPU 8000000UL

#include "ssd1289_i8080.h"
#include "diskio.h"
#include "pff.h"

const uint8_t string_1 [] PROGMEM = "hub";
const uint8_t string_2 [] PROGMEM = "stub.ru";

//initialize the buffer and a pointer to it
uint16_t buffer[256] = {0};
uint16_t *buf = &buffer[0];

//declare variables for SD access
FATFS fs;
DIR dir;
FILINFO fno;
UINT s1;
FRESULT res;

int main(void)
{	
	
	_delay_ms(1000);
	DATA_DDR_0 = 0XFF;
	DATA_DDR_8 = 0XFF;
	COMMAND_DDR = 0XFF;
	COMMAND_DDR1 = 0X01;
	
	Init_SSD1289();
	Lcd_Clear(blue);

	Draw_String(4,2,0X6A9F,0X3185,3,string_1);
	Draw_String(4,5,0XFEE6,0X3185,3,string_2);
	
		//mount the disk
		res = pf_mount(&fs);
			
		 if (res == FR_OK)
		 {	  
			  //open the file
			  res = pf_open("hub_stub.bin");
			 if (res == FR_OK)
			 {
				//set the cursor to the upper left corner 
				Set_Cursor(0,0); 
			
				 for (register uint16_t i = 0; i<300; i++)
				 {
					 //read data into the buffer
					 pf_read((uint16_t*)buf, BUF_SIZE, &s1);
					
					 for (register uint16_t  j = 0; j < 256; j++)
					 {
						 //output the data to the display
						 Lcd_Write_Data(buffer[j]);
					 }
					 
				 } 
				 
			 } 
			 
		 }
	
    while(1)
    {	

    }
}

Displaying an Image on a TFT Display from an SD Card, Using the SSD1289 as an Example.

Displaying an Image on a TFT Display from an SD Card, Using the SSD1289 as an Example.

The wiring diagram is shown below.
Displaying an Image on a TFT Display from an SD Card, Using the SSD1289 as an Example.

The project for Atmel_Studio_6.2 is in the archive ssd1289_8080__sd_card_example.rar.
I should also say that if the supply voltage dropped below 3.3V, the file couldn't be read; the SD card turned out to be very sensitive to the supply voltage, so the TFT module was connected to 5V, and I still use it that way today. That's all; we've covered all the components of the TFT module, and I plan to try connecting it to an stm32f103VET6; you can see the result here.