A few months ago i bought a 3.2” TFT display from banggood for my Raspberry PI. After a few days trying to put it to work, here’s how. My approach doesn’t uses any custom kernel
First, a few specs for the TFT
Display module: flexfb, fbtft
Touchscreen module: ads7846
The TFT needs SPI support, lets enable it
Note: If you get the following error:
ERROR: could not insert 'spi_bcm2708': No such device
Don’t worry – just reboot your Raspberry PI now
After rebooting the PI, let’s start configuring the necessary modules
Add the following lines to /etc/modules (the support for the display and touchscreen)
fbtft_device name=flexfb gpios=dc:22,reset:27 speed=48000000 flexfb width=320 height=240 buswidth=8 init=-1,0xCB,0x39,0x2C,0x00,0x34,0x02,-1,0xCF,0x00,0XC1,0X30,-1,0xE8,0x85,0x00,0x78,-1,0xEA,0x00,0x00,-1,0xED,0x64,0x03,0X12,0X81,-1,0xF7,0x20,-1,0xC0,0x23,-1,0xC1,0x10,-1,0xC5,0x3e,0x28,-1,0xC7,0x86,-1,0x36,0x28,-1,0x3A,0x55,-1,0xB1,0x00,0x18,-1,0xB6,0x08,0x82,0x27,-1,0xF2,0x00,-1,0x26,0x01,-1,0xE0,0x0F,0x31,0x2B,0x0C,0x0E,0x08,0x4E,0xF1,0x37,0x07,0x10,0x03,0x0E,0x09,0x00,-1,0XE1,0x00,0x0E,0x14,0x03,0x11,0x07,0x31,0xC1,0x48,0x08,0x0F,0x0C,0x31,0x36,0x0F,-1,0x11,-2,120,-1,0x29,-1,0x2c,-3 ads7846_device model=7846 cs=1 gpio_pendown=17 speed=1000000 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
For X, we need to configure it to start in the framebuffer /dev/fb1
Edit the configuration file for X display:
sudo vi /usr/share/X11/xorg.conf.d/99-fbturbo.conf
This is the contents of the file:
Section "Device" Identifier "Allwinner A10/A13 FBDEV" Driver "fbturbo" Option "fbdev" "/dev/fb0" Option "SwapbuffersWait" "true" EndSection
Change the line
Option "fbdev" "/dev/fb0"
Option "fbdev" "/dev/fb1"
Save the file and quit
In /boot/overlays there’s the device tree overlay to add support for the touchscreen this LCD brings – ads7846. We just need to tell to the RPi to add support for it.
Device Tree Overlays make possible the support of many hardware configurations with a single kernel and without the necessity to blacklist or load kernel modules.
More info about Device Tree Overlays in here.
There’s a small configuration file – config.txt – that controlls the usage of the Device Tree Overlays.
sudo vi /boot/config.txt
add the following line (at the end of the file):
# Touchscreen - to know what's this line about dtoverlay=ads7846,cs=1,penirq=17, speed=500000, swapxy, pmax=255, xohms=100, xmin=200, xmax=3900, ymin=200, ymax=3900
Make sure the SPI line is also there:
Reboot the PI
Now, check the device in /proc/bus/input/devices
and you should see something like:
I: Bus=0000 Vendor=0000 Product=0000 Version=0000 N: Name="ADS7846 Touchscreen" P: Phys=spi0.1/input0 S: Sysfs=/devices/platform/soc/20204000.spi/spi_master/spi0/spi0.1/input/input0 U: Uniq= H: Handlers=mouse0 event0 B: PROP=0 B: EV=b B: KEY=400 0 0 0 0 0 0 0 0 0 0 B: ABS=1000003
If you start X, you can now touch the screen, but it will not be calibrated.
To calibrate the screen, we must do this as root
apt-get install xinput evtest libts-bin
Set the environment for the calibration, exporting two variables
export TSLIB_TSDEVICE=/dev/input/event0 export TSLIB_FBDEVICE=/dev/fb1
Note: Every time you want to calibrate again, those two variables must be exported
Execute the calibration:
Just press were the crosshair is:
(and you should see the resolution in the console – given by ts_calibrate)
xres = 320, yres = 240
Your terminal should give some results:
Took 19 samples... Top left : X = 899 Y = 3070 Took 14 samples... Top right : X = 3072 Y = 3137 Took 17 samples... Bot right : X = 3263 Y = 880 Took 1 samples... Bot left : X = 712 Y = 895 Took 14 samples... Center : X = 786 Y = 939 46.247009 0.078900 -0.013472 203.000641 0.008692 -0.055027 Calibration constants: 3030844 5170 -882 13303850 569 -3606 65536
The data (Calibration constantes) will be written to /etc/pointercal file. Just cat /etc/pointercal to see the file contents.
To test the calibration, execute ts_test (as root) and in the console you should see some information:
1444847435.977810: 180 141 169 1444847435.987800: 179 141 170 1444847436.007834: 200 145 176 1444847436.017815: 179 147 176 1444847436.017815: 179 147 176 1444847436.027867: 179 147 168 1444847436.037893: 178 148 162 1444847436.057784: 165 152 145 1444847436.067846: 168 153 143
Press Ctrl+c to terminate
Now, reboot the PI
After the reboot, you should have the touchscreen calibrated
If you find out that the axis are reversed (It will probably happen), just create the file 99-configuration.conf (you can give it other name) in /usr/share/X11/xorg.conf.d/ with the following contents:
Section "InputClass" Identifier "calibration" MatchProduct "ADS7846 Touchscreen" Option "Calibration" "219 3835 3984 219" Option "SwapAxes" "0" EndSection
Start the display manager again and it should be ok
Note: You still have another option, that is adding the following line to /etc/X11/xinit/xinitrc, above the . /etc/X11/Xsession line, staying like the following
DISPLAY=:0 xinput --set-prop 'ADS7846 Touchscreen' 'Evdev Axis Inversion' 1 0 . /etc/X11/Xsession
Between the . (dot) and / there’s a space
Now, you’re able to use your TFT without any fuss or custom kernels
If you want to display the boot messages and have a command line in the tft screen, just add the following to /boot/cmdline.txt
full line (default options):
dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=a6fb46b2-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait fbcon=map:10
This is a kernel option that will tell it to map the display to tty 1 and fb (framebuffer) 0
The default font is just way to big for the small screen. Luckily there’s a way we can adequate that.
sudo apt-get install kbd
sudo dpkg-reconfigure console-setup
And choose the following options:
Encoding to use on the console: <UTF-8>
Character set to support: <Guess optimal character set>
Font for the console: <Terminus>
Font size: <6×12 (framebuffer only)>
Now you have a smaller font on the CLI and on the TFT display