STM32F103C8 & TFT (ili9325)

Working libraries, libraries being ported and related hardware
neresus
Posts: 7
Joined: Mon Mar 06, 2017 12:57 am

Re: STM32F103C8 & TFT (ili9325)

Postby neresus » Wed Mar 08, 2017 11:07 am

stevestrong wrote:The wiring should be changed in the header file:

Code: Select all

Adafruit_TFTLCD_8bit_STM32.h


yes I rewrite it but still with no luck.
lcd test works ok return lcd id 9325 and others readings are like on arduino uno than I think wiring is good (tested more than one time).
But on your library cannot read LCD id, it return all the time id 0.

lcd test:

Code: Select all

// adapted from LCD_ID_Reader from http://misc.ws/lcd_information
// controllers either read as 16-bit or as a sequence of 8-bit values

//-- Arduino UNO or Mega 2560 Plugged as shield
#define LCD_RST PC13
#define LCD_CS PB11
#define LCD_RS PB10
#define LCD_WR PB9
#define LCD_RD PB8

#define LCD_D0 PA0
#define LCD_D1 PA1
#define LCD_D2 PA2
#define LCD_D3 PA3
#define LCD_D4 PA4
#define LCD_D5 PA5
#define LCD_D6 PA6
#define LCD_D7 PA7

void setup()
{
    Serial.begin(9600);
    while (!Serial) ;
    Serial.println("Read Registers on MCUFRIEND UNO shield");
    Serial.println("controllers either read as single 16-bit");
    Serial.println("e.g. the ID is at readReg(0)");
    Serial.println("or as a sequence of 8-bit values");
    Serial.println("in special locations (first is dummy)");
    Serial.println("");
    lcdInit();
    lcdReset();      //ensures that controller is in default state
//    for (uint16_t i = 0; i < 256; i++) readReg(i, 7, "f.k");
    readReg(0x00, 2, "ID: ILI9320, ILI9325, ILI9335, ...");
    readReg(0x04, 4, "Manufacturer ID");
    readReg(0x09, 5, "Status Register");
    readReg(0x0A, 2, "Get Power Mode");
    readReg(0x0C, 2, "Get Pixel Format");
    readReg(0x61, 2, "RDID1 HX8347-G");
    readReg(0x62, 2, "RDID2 HX8347-G");
    readReg(0x63, 2, "RDID3 HX8347-G");
    readReg(0x64, 2, "RDID1 HX8347-A");
    readReg(0x65, 2, "RDID2 HX8347-A");
    readReg(0x66, 2, "RDID3 HX8347-A");
    readReg(0x67, 2, "RDID Himax HX8347-A");
    readReg(0x70, 2, "Panel Himax HX8347-A");
    readReg(0xA1, 5, "RD_DDB SSD1963");
    readReg(0xB0, 2, "RGB Interface Signal Control");
    readReg(0xB4, 2, "Inversion Control");
    readReg(0xB6, 5, "Display Control");
    readReg(0xB7, 2, "Entry Mode Set");
    readReg(0xBF, 6, "ILI9481, HX8357-B");
    readReg(0xC0, 9, "Panel Control");
    readReg(0xC8, 13, "GAMMA");
    readReg(0xCC, 2, "Panel Control");
    readReg(0xD0, 3, "Power Control");
    readReg(0xD2, 5, "NVM Read");
    readReg(0xD3, 4, "ILI9341, ILI9488");
    readReg(0xD4, 4, "Novatek ID");
    readReg(0xDA, 2, "RDID1");
    readReg(0xDB, 2, "RDID2");
    readReg(0xDC, 2, "RDID3");
    readReg(0xE0, 16, "GAMMA-P");
    readReg(0xE1, 16, "GAMMA-N");
    readReg(0xEF, 6, "ILI9327");
    readReg(0xF2, 12, "Adjust Control 2");
    readReg(0xF6, 4, "Interface Control");
}

void loop()
{
    // put your main code here, to run repeatedly:

}

void printhex(uint8_t val)
{
    if (val < 0x10) Serial.print("0");
    Serial.print(val, HEX);
}

void readReg(uint16_t reg, uint8_t n, const char *msg)
{
    uint8_t val8;
    lcdReset();
    lcdSetWriteDir();
    lcdWriteCommand(0xB0);     //Command Access Protect
    lcdWriteData(0x00);        //looks wrong
/*
    lcdWriteCommand(0xF6);
    lcdWriteData(0x01);
    lcdWriteData(0x01);
    lcdWriteData(0x03);
*/
    lcdWriteCommand(reg);
    Serial.print("reg(0x");
    printhex(reg >> 8);
    printhex(reg);
    Serial.print(")");
    lcdSetReadDir();
    while (n--) {
        val8 = lcdReadData8();
        Serial.print(" ");
        printhex(val8);
    }
    lcdSetWriteDir();
    Serial.print("\t");
    Serial.println(msg);
}

void lcdInit()
{
    pinMode(LCD_CS, OUTPUT);
    digitalWrite(LCD_CS, HIGH);
    pinMode(LCD_RS, OUTPUT);
    digitalWrite(LCD_RS, HIGH);
    pinMode(LCD_WR, OUTPUT);
    digitalWrite(LCD_WR, HIGH);
    pinMode(LCD_RD, OUTPUT);
    digitalWrite(LCD_RD, HIGH);
    pinMode(LCD_RST, OUTPUT);
    digitalWrite(LCD_RST, HIGH);
}

void lcdReset()
{
    digitalWrite(LCD_RST, LOW);
    delay(2);
    digitalWrite(LCD_RST, HIGH);
    delay(10);             //allow controller to re-start
}

void lcdWrite8(uint16_t data)
{
    digitalWrite(LCD_D0, data & 1);
    digitalWrite(LCD_D1, (data & 2) >> 1);
    digitalWrite(LCD_D2, (data & 4) >> 2);
    digitalWrite(LCD_D3, (data & 8) >> 3);
    digitalWrite(LCD_D4, (data & 16) >> 4);
    digitalWrite(LCD_D5, (data & 32) >> 5);
    digitalWrite(LCD_D6, (data & 64) >> 6);
    digitalWrite(LCD_D7, (data & 128) >> 7);
}

uint16_t lcdRead8()
{
    uint16_t result = digitalRead(LCD_D7);
    result <<= 1;
    result |= digitalRead(LCD_D6);
    result <<= 1;
    result |= digitalRead(LCD_D5);
    result <<= 1;
    result |= digitalRead(LCD_D4);
    result <<= 1;
    result |= digitalRead(LCD_D3);
    result <<= 1;
    result |= digitalRead(LCD_D2);
    result <<= 1;
    result |= digitalRead(LCD_D1);
    result <<= 1;
    result |= digitalRead(LCD_D0);

    return result;
}

void lcdSetWriteDir()
{
    pinMode(LCD_D0, OUTPUT);
    pinMode(LCD_D1, OUTPUT);
    pinMode(LCD_D2, OUTPUT);
    pinMode(LCD_D3, OUTPUT);
    pinMode(LCD_D4, OUTPUT);
    pinMode(LCD_D5, OUTPUT);
    pinMode(LCD_D6, OUTPUT);
    pinMode(LCD_D7, OUTPUT);
}


void lcdSetReadDir()
{
    pinMode(LCD_D0, INPUT);
    pinMode(LCD_D1, INPUT);
    pinMode(LCD_D2, INPUT);
    pinMode(LCD_D3, INPUT);
    pinMode(LCD_D4, INPUT);
    pinMode(LCD_D5, INPUT);
    pinMode(LCD_D6, INPUT);
    pinMode(LCD_D7, INPUT);
}

void lcdWriteData(uint16_t data)
{
    lcdSetWriteDir();
    digitalWrite(LCD_CS, LOW);
    digitalWrite(LCD_RS, HIGH);
    digitalWrite(LCD_RD, HIGH);
    digitalWrite(LCD_WR, HIGH);

    lcdWrite8(data >> 8);

    digitalWrite(LCD_WR, LOW);
    delayMicroseconds(10);
    digitalWrite(LCD_WR, HIGH);

    lcdWrite8(data);

    digitalWrite(LCD_WR, LOW);
    delayMicroseconds(10);
    digitalWrite(LCD_WR, HIGH);

    digitalWrite(LCD_CS, HIGH);
}

void lcdWriteCommand(uint16_t command)
{
    lcdSetWriteDir();
    digitalWrite(LCD_CS, LOW);
    digitalWrite(LCD_RS, LOW);
    digitalWrite(LCD_RD, HIGH);
    digitalWrite(LCD_WR, HIGH);
    lcdWrite8(command >> 8);
    digitalWrite(LCD_WR, LOW);
    delayMicroseconds(10);
    digitalWrite(LCD_WR, HIGH);
    lcdWrite8(command);
    digitalWrite(LCD_WR, LOW);
    delayMicroseconds(10);
    digitalWrite(LCD_WR, HIGH);
    digitalWrite(LCD_CS, HIGH);
}

uint8_t lcdReadData8()
{
    uint8_t result;
    lcdSetReadDir();
    digitalWrite(LCD_CS, LOW);
    digitalWrite(LCD_RS, HIGH);
    digitalWrite(LCD_RD, HIGH);
    digitalWrite(LCD_WR, HIGH);

    digitalWrite(LCD_RD, LOW);
    delayMicroseconds(10);
    result = lcdRead8();
    digitalWrite(LCD_RD, HIGH);

    delayMicroseconds(10);

    return result;
}


uint16_t lcdReadData16()
{
    uint16_t result;
    result = lcdReadData8() << 8;
    result |= lcdReadData8();
    return result;
}


void lcdWriteRegister(uint16_t addr, uint16_t data)
{
    lcdWriteCommand(addr);
    lcdWriteData(data);
}



Adafruit_TFTLCD_8bit_STM32.h:

Code: Select all


// IMPORTANT: SEE COMMENTS @ LINE 15 REGARDING SHIELD VS BREAKOUT BOARD USAGE.

// Graphics library by ladyada/adafruit with init code from Rossum
// MIT license

#ifndef _ADAFRUIT_TFTLCD_8BIT_STM32_H_
#define _ADAFRUIT_TFTLCD_8BIT_STM32_H_

    #define PROGMEM
    #define pgm_read_byte(addr) (*(const unsigned char *)(addr))
    #define pgm_read_word(addr) (*(const unsigned short *)(addr))

#if ARDUINO >= 100
 #include "Arduino.h"
#else
 #include "WProgram.h"
#endif

#include <Adafruit_GFX.h>

#include <libmaple/gpio.h>

/*****************************************************************************/
// LCD controller chip identifiers
#define ID_932X    0
#define ID_7575    1
#define ID_9341    2
#define ID_HX8357D    3
#define ID_UNKNOWN 0xFF

/*****************************************************************************/
#define TFTWIDTH   240
#define TFTHEIGHT  320

// Initialization command tables for different LCD controllers
#define TFTLCD_DELAY 0xFF

// For compatibility with sketches written for older versions of library.
// Color function name was changed to 'color565' for parity with 2.2" LCD
// library.
#define Color565 color565

/*****************************************************************************/
#define   BLACK   0x0000
#define   BLUE    0x001F
#define   RED     0xF800
#define   GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
/*****************************************************************************/
// Define pins and Output Data Registers
/*****************************************************************************/

#define TFT_DATA_PORT   GPIOA
// Port data bits D0..D7:
// enable only one from below lines corresponding to your HW setup:
#define TFT_DATA_LOW_NIBBLE   1 // take the lower 8 bits: 0..7
//#define TFT_DATA_HIGH_NIBBLE   1 // take the higher 8 bits: 8..15

//Control pins |RD |WR |RS |CS |RST|
#define TFT_CNTRL_PORT   GPIOB
#define TFT_RD         PB8
#define TFT_WR         PB9
#define TFT_RS         PB10
#define TFT_CS         PB11
#define TFT_RST         PC13 //PB0

//#define TFT_DATA_PORT   GPIOB
// Port data bits D0..D7:
// enable only one from below lines corresponding to your HW setup:
//#define TFT_DATA_LOW_NIBBLE   1 // take the lower 8 bits: 0..7
//#define TFT_DATA_HIGH_NIBBLE   1 // take the higher 8 bits: 8..15

//Control pins |RD |WR |RS |CS |RST|
//#define TFT_CNTRL_PORT   GPIOA
//#define TFT_RD         PA0
//#define TFT_WR         PA1
//#define TFT_RS         PA2
//#define TFT_CS         PA3
//#define TFT_RST         PB10 //PB0

#define TFT_RD_MASK      BIT0 // digitalPinToBitMask(TFT_RD) //
#define TFT_WR_MASK      BIT1 // digitalPinToBitMask(TFT_WR) //
#define TFT_RS_MASK      BIT2 // digitalPinToBitMask(TFT_RS) //
#define TFT_CS_MASK      BIT3 // digitalPinToBitMask(TFT_CS) //

#if 0
   // use old definition, standard bit toggling, low speed
   #define RD_ACTIVE    digitalWrite(TFT_RD, LOW)
   #define RD_IDLE      digitalWrite(TFT_RD, HIGH)
   #define WR_ACTIVE    digitalWrite(TFT_WR, LOW)
   #define WR_IDLE      digitalWrite(TFT_WR, HIGH)
   #define CD_COMMAND   digitalWrite(TFT_RS, LOW)
   #define CD_DATA      digitalWrite(TFT_RS, HIGH)
   #define CS_ACTIVE    digitalWrite(TFT_CS, LOW)
   #define CS_IDLE      digitalWrite(TFT_CS, HIGH)
   #define CS_ACTIVE_CD_COMMAND   { CS_ACTIVE; CD_COMMAND; }
#else
   // use fast bit toggling, very fast speed!
extern gpio_reg_map * cntrlRegs;
   #define RD_ACTIVE            { cntrlRegs->BRR  = TFT_RD_MASK; }
   #define RD_IDLE               { cntrlRegs->BSRR = TFT_RD_MASK; }
   #define WR_ACTIVE            { cntrlRegs->BRR  = TFT_WR_MASK; }
   #define WR_IDLE               { cntrlRegs->BSRR = TFT_WR_MASK; }
   #define CD_COMMAND            { cntrlRegs->BRR  = TFT_RS_MASK; }
   #define CD_DATA               { cntrlRegs->BSRR = TFT_RS_MASK; }
   #define CS_ACTIVE            { cntrlRegs->BRR  = TFT_CS_MASK; }
   #define CS_IDLE               { cntrlRegs->BSRR = TFT_CS_MASK; }
   #define CS_ACTIVE_CD_COMMAND   { cntrlRegs->BRR  = (TFT_CS_MASK|TFT_RS_MASK); }
#endif

#define WR_STROBE { WR_ACTIVE; WR_IDLE; }

extern uint8_t read8_(void);
#define read8(x) ( x = read8_() )

extern gpio_reg_map * dataRegs;

#if defined(TFT_DATA_LOW_NIBBLE)
  //#warning "Using lower data nibble..."
   // set the pins to input mode
   #define setReadDir() ( dataRegs->CRL = 0x88888888 )   // set the lower 8 bits as input
   //#define setReadDir() ( dataRegs->CRL = 0x44444444 )   // set the lower 8 bits as input floating
   // set the pins to output mode
   #define setWriteDir() ( dataRegs->CRL = 0x33333333 )   // set the lower 8 bits as output
   #define TFT_DATA_SHIFT 0
#elif defined(TFT_DATA_HIGH_NIBBLE)
  #warning "Using high data nibble..."
   // set the pins to input mode
   #define setReadDir() ( dataRegs->CRH = 0x88888888 )   // set the upper 8 bits as input
   // set the pins to output mode
   #define setWriteDir() ( dataRegs->CRH = 0x33333333 )   // set the lower 8 bits as output
   #define TFT_DATA_SHIFT 8
#endif

// set pins to output the 8 bit value
#if 0 // slow write
 #define write8(c) { Serial.print(" write8: "); Serial.print(c,HEX); Serial.write(','); \
               digitalWrite(PB0, (c&BIT0)?HIGH:LOW); \
               digitalWrite(PB1, (c&BIT1)?HIGH:LOW); \
               digitalWrite(PB2, (c&BIT2)?HIGH:LOW); \
               digitalWrite(PB3, (c&BIT3)?HIGH:LOW); \
               digitalWrite(PB4, (c&BIT4)?HIGH:LOW); \
               digitalWrite(PB5, (c&BIT5)?HIGH:LOW); \
               digitalWrite(PB6, (c&BIT6)?HIGH:LOW); \
               digitalWrite(PB7, (c&BIT7)?HIGH:LOW); \
               WR_STROBE; }
#else
 #define write8(c) { uint32_t val = (((c^0x00FF)<<16) | c)<<TFT_DATA_SHIFT; \
               /*Serial.print(" write8: "); Serial.print(val,HEX); Serial.write(',');*/ \
               dataRegs->BSRR = val; WR_STROBE; }
#endif

/*****************************************************************************/

#define swap(a, b) { int16_t t = a; a = b; b = t; }

/*****************************************************************************/
// **** IF USING THE LCD BREAKOUT BOARD, COMMENT OUT THIS NEXT LINE. ****
// **** IF USING THE LCD SHIELD, LEAVE THE LINE ENABLED:             ****

//#define USE_ADAFRUIT_SHIELD_PINOUT 1

/*****************************************************************************/
class Adafruit_TFTLCD_8bit_STM32 : public Adafruit_GFX {

 public:

  //Adafruit_TFTLCD_8bit_STM32(uint8_t cs, uint8_t cd, uint8_t wr, uint8_t rd, uint8_t rst);
  Adafruit_TFTLCD_8bit_STM32(void);

  void     begin(uint16_t id = 0x9328);
  void     drawPixel(int16_t x, int16_t y, uint16_t color);
  void     drawFastHLine(int16_t x0, int16_t y0, int16_t w, uint16_t color);
  void     drawFastVLine(int16_t x0, int16_t y0, int16_t h, uint16_t color);
  void     fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t c);
  void     fillScreen(uint16_t color);
  void     reset(void);
  void     setRegisters8(uint8_t *ptr, uint8_t n);
  void     setRegisters16(uint16_t *ptr, uint8_t n);
  void     setRotation(uint8_t x);
       // These methods are public in order for BMP examples to work:
  void     setAddrWindow(int16_t x1, int16_t y1, int16_t x2, int16_t y2);
  void     invertDisplay(boolean i),
         pushColors(uint16_t *data, int16_t len, boolean first),
           drawBitmap(int16_t x, int16_t y, int16_t w, int16_t h, const uint16_t * bitmap);

  uint16_t readPixel(int16_t x, int16_t y),
           readID(void);

 private:

  void     init(),
           // These items may have previously been defined as macros
           // in pin_magic.h.  If not, function versions are declared:
           //setLR(void),
           flood(uint16_t color, uint32_t len);
  uint8_t  driver;
};

extern uint16_t color565(uint8_t r, uint8_t g, uint8_t b);
extern uint16_t readReg(uint8_t r);
extern uint32_t readReg32(uint8_t r);
extern void writeCommand(uint16_t c);
extern void writeRegister8(uint16_t a, uint8_t d);
extern void writeRegister16(uint16_t a, uint16_t d);
extern void writeRegister24(uint16_t a, uint32_t d);
extern void writeRegister32(uint16_t a, uint32_t d);
extern void writeRegisterPair(uint16_t aH, uint16_t aL, uint16_t d);

extern Adafruit_TFTLCD_8bit_STM32 TFT;

#endif


stevestrong
Posts: 722
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: STM32F103C8 & TFT (ili9325)

Postby stevestrong » Wed Mar 08, 2017 12:56 pm

There is one more thing you should do, adapt the mask bits according to the used port pins for the control lines.
For example, TFT_RD_MASK should be BIT8 if you use PB8, TFT_WR_MASK is BIT9 if you use PB9 and so on (BITX for PBX).

neresus
Posts: 7
Joined: Mon Mar 06, 2017 12:57 am

Re: STM32F103C8 & TFT (ili9325)

Postby neresus » Thu Mar 09, 2017 1:28 pm

stevestrong wrote:There is one more thing you should do, adapt the mask bits according to the used port pins for the control lines.
For example, TFT_RD_MASK should be BIT8 if you use PB8, TFT_WR_MASK is BIT9 if you use PB9 and so on (BITX for PBX).


I try mask bits and it is working. Thank you very much. :D

But after few restarts it stops showing things on display, I do not know why... I try do few more tests what is wrong with stm board.

stevestrong
Posts: 722
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: STM32F103C8 & TFT (ili9325)

Postby stevestrong » Thu Mar 09, 2017 7:38 pm

Check the reset line.

neresus
Posts: 7
Joined: Mon Mar 06, 2017 12:57 am

Re: STM32F103C8 & TFT (ili9325)

Postby neresus » Thu Mar 09, 2017 10:12 pm

I thing all wires are checked more than good. It is something with stm board. Because LCD is working on UNO.
I can read id of LCD but now is showing nothing.
Is some way to check whole board? I can check pins separately by connecting power on them but if is some other way..


Return to “Libraries & Hardware”

Who is online

Users browsing this forum: No registered users and 3 guests