Jump to content

Guide: How to configure TFT Display + Touchscreen on Orange Pi PC (Mainline Kernel)


Recommended Posts

Hey guys, I've spent the last couple of weeks trying to get a TFT display with touch screen to work on my Orange Pi PC board, and I've decided to share my step-by-step solution here. This tutorial is heavily based on Guide: How to use Touchscreen + LCD on H3 devices by Kutysam, but I had to do some extra steps for it to work properly. This tutorial is only for Mainline kernel, I was able to get the graphical screen working with Kutysam's guide for Legacy, but couldn't make the touch work. 

 

First of all this is the display I'm using: LCD module Pi TFT 3.5 inch (320*480) Touchscreen Display Module TFT for Raspberry Pi 3. I believe it is a clone of this Waveshare screen.

Also, I am using the image Armbian_5.38_Orangepipc_Debian_stretch_next_4.14.14_desktop, but it should work for the headless version (server) too, if you install a display manager, desktop environment and the X server.

 

All the commands below are assumed to be run as root user. If you're not root, add "sudo" to the beginning of each command.

---

 

Preparation

First of all make sure you have the latest package updates by running 

apt-get update && apt-get upgrade

This might take a while. If the packages have been installed successfully, reboot. On a fresh install I was getting the following error message:

Quote

E: Could not open file /var/lib/apt/lists/[...] open (2: No such file or directory)

If that happens to you, just run the command again and it should download everything properly. If it still isn't working you could try cleaning the apt cache.

 

After that, install these Make prerequisites:

apt-get install build-essentials

For some reason the linux-headers package for sunxi is not included in the repository, this thread might explain it better than me. Either way, download and install the package with dpkg:

wget http://apt.armbian.com/pool/main/l/linux-4.14.18-sunxi/linux-headers-next-sunxi_5.41_armhf.deb
dpkg -i linux-headers-next-sunxi_5.41_armhf.deb

Now we need to edit armbianEnv.txt to enable the overlays for spi and cs1

nano /boot/armbianEnv.txt

Add the following lines to the end of the file (Be careful with spaces in the end of the lines... I lost a couple of days trying to figure out what the problem was when I had an extra space after "param_spidev_spi_bus=0" <_<)

overlays=spi-spidev spi-add-cs1
param_spidev_spi_bus=0
param_spidev_spi_cs=1

And reboot.

 

Screen

Now we need to configure fbtft and fbtft_device on boot. Note: I had to put "98" in the start of the filename, or else I'd get the following error: "fbtft_device: spi_busnum_to_master(0) returned NULL" in dmesg after I installed the touchscreen. I believe it has something to do with the load order, so if you're having problems with this file you could try changing the prefix to 99 or removing it.

nano /etc/modules-load.d/98-fbtft.conf 

Insert the following on the file:

fbtft
fbtft_device

Now we have to load fbtft_device options on boot. Open the file with:

nano /etc/modprobe.d/fbtft.conf

Add the following:

options fbtft_device rotate=90 name=piscreen speed=16000000 gpios=reset:2,dc:71 txbuflen=32768 fps=25

And reboot. At this point your screen should at least turn black. For me, the GUI wouldn't load unless I typed 'startx' on the console. So this is how I fixed it to always display the GUI on boot:

apt-get install xserver-xorg-video-fbdev
nano /usr/share/X11/xorg.conf.d/99-fbdev.conf

Insert this in the file:

Section "Device"  
  Identifier "piscreen"
  Driver "fbdev"
  Option "fbdev" "/dev/fb0"
EndSection

At this point the screen should be displaying the Armbian GUI, with mouse and keyboard working, but without touch screen. Let's fix that.

 

Touch

First we need to download and compile the ads7846 driver (apparently it is compatible with xpt2046).

mkdir ds7846 
cd ds7846 
wget https://sourceforge.net/p/openipmi/linux-ipmi/ci/master/tree/drivers/input/touchscreen/ads7846.c?format=raw
mv ads7846.c?format=raw ads7846.c 
nano Makefile

Insert the following on the makefile:

obj-m := ads7846.o 
KDIR := /lib/modules/$(shell uname -r)/build 
PWD := $(shell pwd) 
all:
	$(MAKE) -C $(KDIR) M=$(PWD) modules 
clean:
	$(MAKE) -C $(KDIR) M=$(PWD) clean 
install:
	$(MAKE) -C $(KDIR) M=$(PWD) modules_install

Now let's compile and load the module into the kernel:

make
make install
depmod

Now we'll build and install the ads7846_device module from fbtft_tools:

cd .. 
git clone https://github.com/notro/fbtft_tools/
cd fbtft_tools/ads7846_device 
make 
make install 
depmod 

Let's load ads7846 and ads7846_device on boot

sudo nano /etc/modules-load.d/99-ads7846.conf

After that let's load the options for ads7846_device. These configs worked best for me, but you can play with them and tweak if needed.

nano /etc/modprobe.d/ads7846_device.conf

Insert the following:

options ads7846_device model=7846 cs=1 gpio_pendown=1 keep_vref_on=1 swap_xy=1 pressure_max=255 x_plate_ohms=60 x_min=200 x_max=3900 y_min=200 y_max=3900

Reboot, and the touch should be working! 

Well, for me not entirely. The Y axis seemed to be reversed, so here are the steps I took to configure it:

First of all, we need xinput to configure the touch screen options. Install it with this command:

apt-get install xinput

Now we need to set the "swap_xy" option to 0 in ads7846_device configuration file. Open it with:

nano /etc/modprobe.d/ads7846_device.conf

Replace its contents with this:

options ads7846_device model=7846 cs=0 gpio_pendown=1 keep_vref_on=1 swap_xy=0 pressure_max=255 x_plate_ohms=60 x_min=200 x_max=3900 y_min=200 y_max=3900

Reboot to apply the changes. Now we need to find out the touchscreen name on xinput. Run this command:

DISPLAY=:0.0 xinput list

You should get a list of pointer devices, and the touchscreen should be on it. In my case the name is 'ADS7846 Touchscreen'. At this point you might get an "unable to connect to X server error". If that's the case, you can add the required X permissions for your user with the command (taken from this ask ubuntu answer):

export XAUTHORITY=$(eval echo ~`who | grep tty7 | sed 's/\([a-z]*\).*/\1/'`)/.Xauthority

And "xinput list" should be working. Now we can try to configure it with xinput's "set-prop" parameter:

DISPLAY=:0.0 xinput --set-prop 'ADS7846 Touchscreen' 'Coordinate Transformation Matrix' 0 -1 1 1 0 0 0 0 1

Test your display to see if it works. This matrix worked best for me, but you might need to tweak it. Refer to this guide for more info on how coordinate transformation matrices work.

Now you need to run this command every time the session starts. To automate it, I added the command to the .xsessionrc file:

nano /home/{your username}/.xsessionrc

Append the xinput set-prop command:

DISPLAY=:0.0 xinput --set-prop 'ADS7846 Touchscreen' 'Coordinate Transformation Matrix' 0 -1 1 1 0 0 0 0 1

If you have multiple users logging in the session displayed on your screen, you might need to add this file for every user. ".xsessionrc" was the only file where I could get this working.

 

And that's it! Your display + touch screen should be working properly now!

 

I am still very newbie with Armbian and single board computers, and there is much I don't understand yet, so if you have any questions, comments or suggestions on this guide please post them below. See you all.

Link to comment
Share on other sites

Some commands that might be useful for debugging:
 

Running a program as a new window through SSH:

DISPLAY=:0.0 {program name}

Running the program as "full screen" (no desktop environment):

First we need to find the executable path. You can find it with "whereis". I'll use "glxgears" to illustrate:

whereis glxgears

You'll get something like:

Quote

glxgears: /usr/bin/glxgears /usr/share/man/man1/glxgears.1.gz

The executable is usually in a "bin" folder.  For glxgears the path is /usr/bin/glxgears.

Now we can run the program with startx through the framebuffer we're using for the screen:

FRAMEBUFFER=/dev/fb0 startx /usr/bin/glxgears

 

Link to comment
Share on other sites

1 hour ago, milaad said:

hi. i dot first step . i just see a black color in lcd. please help me.

thanks

i  change this line
Option "fbdev" "/dev/fb0"

to

Option "fbdev" "/dev/fb8"

 and lcd show .

 

now i cant use touch screen

Edited by milaad
write mistake
Link to comment
Share on other sites

so now i can see screen in lcd but touch screen dont work. when i go to make  this file ads7846.c i got error.

 

 

 

obj-m := ads7846.o KDIR := /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) all: $(MAKE) -C $(KDIR) M=$(PWD) modules clean: $(MAKE) -C $(KDIR) M=$(PWD) clean install: $(MAKE) -C $(KDIR) M=$(PWD) modules_install

 

when i terminal type

$ make

 

make -c /lib/modules/3.4.113-sun8i/build M=/root/ds7846 modules

make[1]: Entering directory '/lib/modules/3.4.113-suni/build'

make[1]: *** No rule to make target 'modules'. stop

make[1]: *** Leaving directory '/libmodules/3.4113-suni/build'

Makefile:5: recipe for target 'all'  faild

make: *** [all] Error 2

 

why happing this thing?

 

Link to comment
Share on other sites

On 5/2/2018 at 8:55 PM, GusAntoniassi said:

screen work but touch dont work

hi . my screen now work but my touch dont work.

when my screeen dont work my orange pi lite recoginize my touch but when my screen work orange pi lite can not recoginize my touch .

it is meybe for spi????

 

Link to comment
Share on other sites

Hello everyone, either if the author of this topic isn't answering anymore, i've followed this guide. I've an OPI PC, and i've exactly the same lcd display shown in the guide. The only difference is that i run Armbian UBUNTU and not Debian.
Everything i have is a white screen. I've tried to reconnect the opi pc to the old display through the hdmi and i've discovered that, now, the desktop interface has disappeared,
Any suggestion to how to make this display to work with the opi pc? The GPIO pinout is exactly the same of the raspberry?
Thank you for any suggestion.

Link to comment
Share on other sites

Has anyone had any success with this? I have also a Orange PI and the same touchscreen 3.5'', but only I get the white screen as some of you, I have now flashed Armbian (on the post says Orangepipc, but I have downloaded Orangepizero, which one is it?) into my Orange Pi's SD Card and I will try to follow along, if I succeed I will plan to create a .img image to ease this process, and some setup scripts... But back on the original question, Someone has succeeded? if yes an .img would be lifesaver, very easy to generate with dd

 

Edited by nuvhandra
Distro name correction
Link to comment
Share on other sites

Hello,

 

Thanks a lot for this post!

 

I have an OrangePi PC with a 3.5" LCD Screen XPT2046 on GPIO

 

I have spent a lot of time with a lot of Armbian versions to try to make it running.

 

The download link of  Armbian_5.38_Orangepipc_Debian_stretch_next_4.14.14_desktop you have provided does not work anymore.

 

Do you have please the .img file ???

 

Regards,

 

Olivier

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...

Important Information

Terms of Use - Privacy Policy - Guidelines