Help writing SPI library (FT813 lcd driver)

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
vitormhenrique
Posts: 18
Joined: Tue Jan 15, 2019 3:07 pm

Help writing SPI library (FT813 lcd driver)

Post by vitormhenrique » Tue Jan 29, 2019 10:58 pm

Hello Everyone, I'm basically a hobbyist on electronics, but a software developer, have being doing some electronic projects in a while but mostly consuming libraries, never had to interface with an chip on that had to write my library from scratch, but today is that day.

I'm trying to write a basic library to interface with the FT813 chip, and have some awesome UI on my pyboard, but I'm having some issues questions on how to do so.
I'm using the datasheet and the programing guide as a reference.

the initialization sequence for the chip is the following:

- Send te host command "CLKINT" (0x48) to select the internal clock
- Send Host command "ACTIVE" (0x00) to enable the clock to the FT81X. FT81X starts its self diagnosis process and may take up to 300ms. Alternatively, read REG_ID repeatedly until
0x7C is read

But I can not get the chip id to read.

REG_ID should be read by reading 8 bits from the intended address location 302000 (hex)

Writing a command is defined as: When sending a command, the host transmits a 3 byte command. On my case I need to send 0x48 followed by 0x00 twice (page 16 datasheet)

Some information regarding the protocol: Serial data is sent by the most significant bit first, use frequence <11MHz, PCLK polarity is on the rising edge

The code that I have so far is the following:

Code: Select all

import machine
import time

class FT1X_HAL:
    def __init__(self, spi, cs):
        self._spi = spi
        self._cs = cs

    def write_cmd(self, data):
        self._cs.low()
        self._spi.write(bytearray([data]))
        self._spi.write(bytearray([0x00]))
        self._spi.write(bytearray([0x00]))
        self._cs.high()

    def read8(self, address):
        data = bytearray(1)
        self._cs.low()
        self._spi.write(bytearray([address]))
        self._spi.readinto(data)
        self._cs.high()
        return data


class FT1X(FT1X_HAL):

    def begin(self):
        self.write_cmd(0x48)
        self.write_cmd(0x00)

        chipid = self.read8(0x302000)  # /* Read ID register */

        count = 0
        while chipid != 0x7C:
            chipid = self.read8(0x302000)
            time.sleep(0.01)
            count += 1
            if count > 350:
                print("timed out")
                break



spi = machine.SPI(2, baudrate=6000000, polarity=0)
gpu = FT1X(spi, cs=machine.Pin('Y5'))
gpu.begin()
I'm probably missing something :?:

Best regards,

Vitor Henrique

OutoftheBOTS_
Posts: 642
Joined: Mon Nov 20, 2017 10:18 am

Re: Help writing SPI library (FT813 lcd driver)

Post by OutoftheBOTS_ » Wed Jan 30, 2019 7:46 am

I have a screen here with this chip on it but haven't had the time to play with it yet.

I do believe the creator for Gamedunio is a bit of an expert on the chip and although his tutorials and blogs are for programming it with Ardunio I assume you maybe be able to find answers for what you need or maybe port the Ardunio library from here http://excamera.com/sphinx/gameduino2/code.html

A bit of info here http://excamera.com/sphinx/gameduino2/code.html

loboris
Posts: 317
Joined: Fri Oct 02, 2015 6:19 pm

Re: Help writing SPI library (FT813 lcd driver)

Post by loboris » Wed Jan 30, 2019 8:41 am

@vitormhenrique

You can check this EVE library used on my ESP32 MicroPython port, It is fully working and it should be no problem to convert it for PyBoard (change the low level SPI functions).
The MicroPython module is here.

User avatar
vitormhenrique
Posts: 18
Joined: Tue Jan 15, 2019 3:07 pm

Re: Help writing SPI library (FT813 lcd driver)

Post by vitormhenrique » Wed Jan 30, 2019 1:29 pm

@OutoftheBOTS_ I saw multiple libraries and studied all of them, but that does not help me . They are following the datasheet my problem is how to implement that on micropython.

@loboris I saw your port, my initial goal was to implement the library in python, instead of C with bindings, so it would be compatible amount different micropython boards.

Because it's basically few SPI transactions I thought that the performance would be good enough in python.

I'm probably doing something really silly that you guys (that have more experience with micropython) will be able to spot on my code, like sending the incorrect number of bits, incorrect configuration, etc....

User avatar
vitormhenrique
Posts: 18
Joined: Tue Jan 15, 2019 3:07 pm

Re: Help writing SPI library (FT813 lcd driver)

Post by vitormhenrique » Wed Jan 30, 2019 3:15 pm

Some progress but still not much,

I had forgotten to initialize the cs pin and set it's status when initializing the screen :roll:

But even after fixing this I'm getting 74 back from the screen when i should get 124

Code: Select all

import time
import machine
import ustruct 
from micropython import const

CLKINT = const(0x48)
ACTIVE = const(0X00)
REG_ID = const(0x302000)

class FT1X_HAL:
    def __init__(self, spi, cs):
        self._spi = spi
        self._cs = cs
        cs.init(cs.OUT, value=1)

    def write_cmd(self, command):
        self._cs.low()
        self._spi.write(bytearray([command]))
        self._spi.write(bytearray([0x00]))
        self._spi.write(bytearray([0x00]))
        self._cs.high()

    def read8(self, address):
        self._cs.low()
        self._spi.write(bytearray([address]))
        data = self._spi.read(1)
        self._cs.high()
        return ustruct.unpack(">B", data)[0]

class FT1X(FT1X_HAL):

    def begin(self):
        self.write_cmd(CLKINT)
        self.write_cmd(ACTIVE)

        time.sleep(0.3)

        chipid = self.read8(REG_ID)  # /* Read ID register */

        count = 0
        while chipid != 0x7C:
            chipid = self.read8(REG_ID)
            print(chipid)
            time.sleep(0.01)
            count += 1
            if count > 350:
                print("timed out")
                break

spi = machine.SPI(2, baudrate=6000000, polarity=0, phase=0)
gpu = FT1X(spi, cs=machine.Pin('Y5'))
gpu.begin()
Ideas?

OutoftheBOTS_
Posts: 642
Joined: Mon Nov 20, 2017 10:18 am

Re: Help writing SPI library (FT813 lcd driver)

Post by OutoftheBOTS_ » Wed Jan 30, 2019 9:30 pm

vitormhenrique wrote:
Wed Jan 30, 2019 1:29 pm
@OutoftheBOTS_ I saw multiple libraries and studied all of them, but that does not help me . They are following the datasheet my problem is how to implement that on micropython.

@loboris I saw your port, my initial goal was to implement the library in python, instead of C with bindings, so it would be compatible amount different micropython boards.

Because it's basically few SPI transactions I thought that the performance would be good enough in python.

I'm probably doing something really silly that you guys (that have more experience with micropython) will be able to spot on my code, like sending the incorrect number of bits, incorrect configuration, etc....
Although I am super interested in this chip and it is on my list to play with I don't have the time to sit down and read the data sheet at the moment. So can't tell if your setup code is correct

There is a little problem that in Micro-python the Machine Module doesn't have a standard syntax across all ports as it isn't a standard python module. What port of micropython are u using??

You are correct in thinking python will be plenty fast enough to implement a driver for this chip. Most of the demo stuff I have watched has been running on an 8bit AVR chip running at 16Mhz and anything we are running Micro-Python on is many times faster. This is in fact my interest in this chip is the fact it has very low requiremnt from the MCU in order to output high perform graphics and sound.

User avatar
vitormhenrique
Posts: 18
Joined: Tue Jan 15, 2019 3:07 pm

Re: Help writing SPI library (FT813 lcd driver)

Post by vitormhenrique » Thu Jan 31, 2019 6:50 pm

OutoftheBOTS_ I I'm using a pyboard.

I've used the SPI before, even converted some Adafruit Circuit python libraries to Micropython.

hopefully I'll be able to figure it out and write a library so whenever you wanna play with it you would have a head start.

I'll try check the data with a logic analyzer and try to understand what is going on.

Best regards,

Vitor Henriqe

User avatar
vitormhenrique
Posts: 18
Joined: Tue Jan 15, 2019 3:07 pm

Re: Help writing SPI library (FT813 lcd driver)

Post by vitormhenrique » Tue Feb 05, 2019 4:16 pm

I got a logic analyzer to find out what exactly my code was doing and could compare with a working example that worked on arduino.

Finally got my code to read the ID! So now is on to implement the other functions and get the display working no micropython! :D

Code: Select all

import time
from machine import Pin
import ustruct 
from micropython import const

CLKINT = const(0x48)
ACTIVE = const(0X00)
REG_ID = const(0x302000)

class FT1X_HAL:
    def __init__(self, spi, cs):
        self._spi = spi
        self._cs = cs
        cs.init(cs.OUT, value=1)

    def write_cmd(self, command):
        self._cs.low()
        self._spi.write(bytearray([command, 0x00, 0x00]))
        self._cs.high()

    def read8(self, address):
        self._cs.low()
        self._spi.write(bytearray([address >> 16, address >> 8, address, 0x00]))
        data = self._spi.read(1)
        self._cs.high()
        return ustruct.unpack(">B", data)[0]

class FT1X(FT1X_HAL):

    def begin(self):
        self.write_cmd(ACTIVE)
        self.write_cmd(CLKINT)

        time.sleep(0.3)

        chipid = self.read8(REG_ID) 

        count = 0
        while chipid != 0x7C:
            chipid = self.read8(REG_ID)
            print(chipid)
            time.sleep(0.1)
            count += 1
            if count > 20:
                print("timed out")
                break

        print("id read: {}".format(chipid))


spi = machine.SPI(2, baudrate=8000000, polarity=0, phase=0)
gpu = FT1X(spi, cs=machine.Pin('Y5'))
gpu.begin()

okideveloper
Posts: 2
Joined: Thu Mar 28, 2019 8:03 pm

Re: Help writing SPI library (FT813 lcd driver)

Post by okideveloper » Thu Mar 28, 2019 8:09 pm

Hi,
Your code is looks like same code as you posted at the beginning.
I am still get 74. Not 124.
Could you please send me osciloscope screen capture right Chip ID query.

Thanks

OkiDeveloper

okideveloper
Posts: 2
Joined: Thu Mar 28, 2019 8:03 pm

Re: Help writing SPI library (FT813 lcd driver)

Post by okideveloper » Fri Mar 29, 2019 3:19 pm

I got it. I did realize i have to send dummy byte and then one more byte for read 0x7c.

So after activate FT813 , I have to send [0x30, 0x20, 0x00, 0x00, 0x00] for reading x7C

Oki

Post Reply